Created
April 20, 2013 15:49
-
-
Save szkrd/5426418 to your computer and use it in GitHub Desktop.
search for files in a folder (rhino + swing gui)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// launcher: | |
//#!/bin/bash | |
//export CLASSPATH=$CLASSPATH:~/libs/js.jar:~/libs/swt.jar | |
//cd ~/bin/searchfiles/ | |
//java org.mozilla.javascript.tools.shell.Main -version 170 search_files.js $@ | |
importPackage(java.io); | |
importPackage(java.lang); | |
var swt = JavaImporter( | |
org.eclipse.swt.SWT, | |
org.eclipse.swt.layout.RowData, | |
org.eclipse.swt.layout.RowLayout, | |
org.eclipse.swt.layout.FillLayout, | |
org.eclipse.swt.widgets.List, | |
org.eclipse.swt.widgets.Button, | |
org.eclipse.swt.widgets.Display, | |
org.eclipse.swt.widgets.Label, | |
org.eclipse.swt.widgets.Shell, | |
org.eclipse.swt.widgets.Text | |
); | |
var widgets = {}, defaults = {}, display, shell; | |
if (!arguments.length) { | |
print("not enough parameters."); | |
quit(); | |
} | |
defaults.escQuits = false; | |
defaults.closeOnAction = false; | |
defaults.preFilter = /\.(js|html|php|css)$/; | |
defaults.path = arguments[0]; | |
defaults.search = (arguments.length > 1) ? arguments[1] : ""; | |
function run () { // {{{ | |
collectFiles(); | |
display = new swt.Display(); | |
shell = new swt.Shell(display, swt.SWT.CLOSE | swt.SWT.TITLE | swt.SWT.MIN); | |
shell.setSize(780, 400); | |
shell.setText("SWT Search: " + defaults.path); | |
AddTextWidget(); | |
addListWidget(); | |
widgets.text.setText(defaults.search); | |
widgets.text_keyListener({widget: widgets.text}, "keyReleased"); | |
shell.open(); | |
guiLoop(); | |
} // }}} | |
function finalAction () { // {{{ | |
var isWin = (System.getProperty("os.name").indexOf("Windows") > -1), | |
userName = System.getProperty("user.name"), | |
cmd, sel = widgets.list.getSelection(); | |
sel = sel && sel.length ? sel[0] : null; | |
if (!sel) | |
return; | |
sel = ("" + sel).replace(/^\.\.\. /, defaults.path); | |
if (sel.indexOf(" ") > -1) { | |
sel = '"' + sel + '"'; | |
} | |
cmd = "jedit.jar -reuseview " + sel; | |
if (isWin) { | |
cmd = "java -jar C:/Users/" + userName + "/bin/jedit/" + cmd; | |
} else { | |
cmd = "java -jar /home/" + userName + "/bin/jedit/" + cmd; | |
} | |
Runtime.getRuntime().exec(cmd); | |
if (defaults.closeOnAction) { | |
quit(); | |
} | |
} // }}} | |
function listFiles(dir, comp) { // {{{ | |
var files = [], listFilesRec; | |
if (typeof(dir) == "string") { | |
dir = new File(dir); | |
} | |
if (!comp) { | |
comp = function () { return true; }; | |
} | |
(listFilesRec = function (directory) { | |
entries = directory.listFiles(); | |
for each (entry in entries) { | |
if (comp(entry)) { | |
files.push(entry); | |
} | |
if (entry.isDirectory()) { | |
files.concat(listFilesRec(entry)); | |
} | |
} | |
})(dir); | |
return files; | |
} // }}} | |
function filesComp (entry) { // {{{ | |
var n = entry.getName(); | |
if (n.indexOf(".") === 0) { | |
return false; | |
} | |
return defaults.preFilter.test(n); | |
} // }}} | |
function collectFiles () { // {{{ | |
timer = {start: new Date()}; | |
files = listFiles(defaults.path, filesComp); | |
if (!files.length) { | |
quit(); | |
} | |
timer.end = (new Date - timer.start) / 1000; | |
print("found " + files.length + " files. In " + timer.end + "s."); | |
} // }}} | |
function AddTextWidget () { // {{{ | |
widgets.text = new swt.Text(shell, swt.SWT.SINGLE | swt.SWT.BORDER); | |
widgets.text.setBounds(5, 5, 764, 25); | |
widgets.text_keyListener = function (e, method) { | |
if (method != "keyReleased") { | |
return; | |
} | |
// ctrl + r | |
if (((e.stateMask & swt.SWT.CTRL) == swt.SWT.CTRL) && (e.keyCode == "r".charCodeAt(0))) { | |
collectFiles(); | |
} | |
// esc | |
if (e.keyCode == swt.SWT.ESC && defaults.escQuits) { | |
return quit(); | |
} | |
// down | |
if (e.keyCode == swt.SWT.ARROW_DOWN) { | |
widgets.list.setFocus(); | |
} | |
var s = e.widget.text, split, s1, s2, | |
listables, filter, | |
i = 0; | |
if (widgets.text_prevText == s) { | |
return; | |
} | |
// one can do a double search, with the ">" as separator | |
split = s.split(">"); | |
s1 = split[0]; | |
s2 = split.length > 1 ? split[1] : ""; | |
filter = function(el){ | |
var sB = s, elB = el + ""; | |
if (sB.toLowerCase() == sB) { // case insensitive mode | |
sB = sB.toLowerCase(); | |
elB = elB.toLowerCase(); | |
} | |
return elB.indexOf(sB) !== -1; | |
}; | |
if (s1) { | |
s = s1; | |
listables = files.filter(filter); | |
if (s2) { | |
s = s2; | |
listables = listables.filter(filter); | |
} | |
} | |
widgets.list.removeAll(); | |
for each (item in listables) { | |
if ((++i) > 200) break; | |
widgets.list.add((item + "").replace(defaults.path, "... ")); | |
} | |
widgets.text_prevText = s; | |
}; | |
widgets.text.addKeyListener(widgets.text_keyListener); | |
} // }}} | |
function addListWidget () { // {{{ | |
widgets.list = new swt.List(shell, swt.SWT.BORDER); | |
widgets.list.setBounds(5, 35, 764, 331); | |
widgets.list.addMouseListener(function (e, methodName) { | |
if (methodName == "mouseDoubleClick") | |
finalAction(); | |
}); | |
widgets.list.addKeyListener(function (e, methodName) { | |
var sel; | |
if (methodName != "keyReleased") { | |
return; | |
} | |
if (e.keyCode == swt.SWT.ESC && defaults.escQuits) { | |
return quit(); | |
} | |
if (e.character == swt.SWT.CR) { | |
finalAction(); | |
} | |
}); | |
} // }}} | |
function guiLoop () { // {{{ | |
while (!shell.isDisposed()) { | |
if (!display.readAndDispatch()) { | |
display.sleep(); | |
} | |
} | |
display.dispose(); | |
}; // }}} | |
run(); | |
// :collapseFolds=0:mode=javascript:folding=explicit: |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment