Skip to content

Instantly share code, notes, and snippets.

@oscarryz
Last active December 14, 2015 19:49
Show Gist options
  • Save oscarryz/5139202 to your computer and use it in GitHub Desktop.
Save oscarryz/5139202 to your computer and use it in GitHub Desktop.
/*
Copyright (c) 2013, Oscar Reyes
All rights reserved.
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
- Redistributions of source code must retain the above copyright notice,
this list of conditions and the folLowing disclaimer.
- Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation and/or
other materials provided with the distribution.
- Neither the name of GoDir nor the names of its contributors may be used
to endorse or promote products derived from this software without specific
prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES ( INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
AND ON ANY THEORY OR LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
( INCLUDING NEGLIGENCE OR OTHERWISE ) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package godir;
import javax.swing.text.*;
import java.awt.Color;
import java.awt.Point;
import java.awt.event.KeyEvent;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import com.sun.awt.AWTUtilities;
import java.awt.image.*;
import javax.swing.filechooser.*;
import sun.awt.shell.ShellFolder;
import static java.lang.System.out;
public class GoDir {
public static void main( String ... args ) {
GoDir app = new GoDir();
app.show();
}
private JFrame frame = new JFrame();
private Color background = new Color(0f,0f,0f,1f);
private Color foreground = new Color(0f,1f,0f,.75f);
private Color HILIT_COLOR = new Color(0f,1f,0f,.35f);
private JLabel icon = new JLabel();
private JTextField address = new JTextField(40);
private FileSystemView fsv;
private Desktop desktop;
private KeyboardFocusManager kbfm;
private File currentDir = null;
public void show() {
initFrame();
initAddress();
frame.add(initPanel());
frame.pack();
frame.setLocationRelativeTo(null);
Point p = frame.getLocation();
frame.setLocation(p.x, p.y / 3 );
frame.setVisible(true);
initResources();
}
private JPanel initPanel() {
JPanel panel = new JPanel();
panel.setBackground(new Color(0,0,0,0));
setIcon("c:\\");
//icon.setBackground(new Color(0,0,0));
panel.add(icon);
panel.add(address);
return panel;
}
private void initAddress() {
address = new JTextField(40);
address.setBackground( background );
address.setForeground( foreground );
address.setBorder( BorderFactory.createLineBorder(HILIT_COLOR));
address.setFont(new Font("Consolas", Font.PLAIN, 42));
address.addActionListener( new ActionListener() {
public void actionPerformed(ActionEvent e){
open( address.getText() );
}
});
address.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent e) {
if (isPrintableChar(e.getKeyChar())
&& address.getCaretPosition() > 0
&& address.getText().length() > address.getCaretPosition()
&& address.getText().charAt(address.getCaretPosition()) == e.getKeyChar()) try {
int ccp = address.getCaretPosition();
String newText = (address.getText().substring(0, ccp) + address.getText().substring(ccp + 1)).replace('\\', '/');
address.setText(newText);
hl.changeHighlight(hl.getHighlights()[0], ccp, address.getText().length());
address.setCaretPosition(ccp);
return;
} catch (BadLocationException ble) {
}
}
public void keyReleased(KeyEvent e) {
switch (e.getKeyCode()) {
default:
if (isPrintableChar(e.getKeyChar())) {
hl.removeAllHighlights();
address.setText(address.getText().substring(0, address.getCaretPosition()));
out.printf("%c %d %n", e.getKeyCode(), e.getKeyCode());
setIcon(address.getText());
autoComplete();
}
}
}
});
address.setHighlighter(hl);
address.setSelectionColor(HILIT_COLOR);
address.setText("C:/U");
autoComplete();
//address.setCaretPosition(address.getText().length());
}
private void initFrame() {
frame.setAlwaysOnTop(true);
frame.setUndecorated( true );
AWTUtilities.setWindowOpacity(frame, .75f);
frame.setBackground( background );
frame.setForeground(foreground);
}
private void initResources() {
fsv = FileSystemView.getFileSystemView();
desktop = Desktop.getDesktop();
kbfm = KeyboardFocusManager.getCurrentKeyboardFocusManager();
kbfm.addKeyEventDispatcher(new KeyEventDispatcher() {
public boolean dispatchKeyEvent(KeyEvent e){
switch( e.getKeyCode()) {
case KeyEvent.VK_DELETE:
case KeyEvent.VK_BACK_SPACE:
if ( address.getCaretPosition() == address.getText().length()) {
return false;
} else {
address.setText( address.getText().substring(0, address.getCaretPosition()));
return true;
}
case KeyEvent.VK_ESCAPE: frame.dispose();
return true;
case KeyEvent.VK_TAB:
out.println("Tab");
if ( address.getCaretPosition() == address.getText().length() ) {
autoComplete();
} else {
address.setCaretPosition( address.getText().length() );
hl.removeAllHighlights();
}
return true;
default:
}
return false;
}
});
}
private void open( String text ) {
File file = new File( text );
if ( file.exists() ) try {
icon.setIcon(fsv.getSystemIcon(file));
desktop.open( file );
frame.dispose();
} catch ( IOException ioe ) {}
}
private void setIcon( String f ) {
File file = new File( f );
if ( file.exists() ) try {
out.println( file );
ShellFolder shellFolder = ShellFolder.getShellFolder(file);
icon.setIcon( new ImageIcon(shellFolder.getIcon(true)) );
frame.repaint();
if ( file.isDirectory() ) {
currentDir = file;
}
} catch ( FileNotFoundException e ){
} else {
/*
out.println( "Nap : " + file );
if ( currentDir != null ) {
for ( File c : currentDir.listFiles() ) {
if ( c.getAbsolutePath().startsWith( f )) {
address.setText( c.getAbsolutePath() );
address.select( c.getAbsolutePath().length() , f.length() );
address.setCaretPosition(f.length());
//currentDir = null;
}
}
}
*/
}
}
int index = 0;
Highlighter hl = new DefaultHighlighter();
Highlighter.HighlightPainter painter = new DefaultHighlighter.DefaultHighlightPainter(HILIT_COLOR);
private void autoComplete() {
String a = address.getText();
a = a.replace('/','\\');
int i = a.lastIndexOf("\\");
if ( i < 0 ) {
return;
}
String d = a.substring(0, i+1);
out.println("sub :[" + d +"]");
File dir = new File( d );
out.println( dir );
if ( dir.exists() ) {
out.println("dir: "+ dir.getAbsolutePath() );
for ( File f : dir.listFiles() ) {
out.println("s : "+f.getAbsolutePath() );
String suggestion = f.getAbsolutePath();
if ( suggestion.startsWith(a) ) try {
address.setText(suggestion.replace('\\','/')+"/");
hl.addHighlight(a.length(), f.getAbsolutePath().length()+1, painter);
address.setCaretPosition(a.length());
out.println("sug: "+suggestion );
return;
} catch ( BadLocationException ble ) {}
}
}
}
public boolean isPrintableChar( char c ) {
Character.UnicodeBlock block = Character.UnicodeBlock.of( c );
return (!Character.isISOControl(c)) &&
c != KeyEvent.CHAR_UNDEFINED &&
block != null &&
block != Character.UnicodeBlock.SPECIALS;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment