From: Svjatoslav Agejenko Date: Thu, 17 Jan 2013 13:12:30 +0000 (+0200) Subject: right click on directory opens it in terminal X-Git-Url: http://www2.svjatoslav.eu/gitweb/?a=commitdiff_plain;h=8e3d8292673f5fc9c0611f419d7425c8fe151ca8;p=instantlauncher.git right click on directory opens it in terminal --- diff --git a/src/main/java/eu/svjatoslav/instantlauncher/menu/Menu.java b/src/main/java/eu/svjatoslav/instantlauncher/menu/Menu.java index a99c7c0..948672a 100755 --- a/src/main/java/eu/svjatoslav/instantlauncher/menu/Menu.java +++ b/src/main/java/eu/svjatoslav/instantlauncher/menu/Menu.java @@ -22,31 +22,122 @@ import eu.svjatoslav.instantlauncher.Utils; public class Menu { + public static class ButtonMouseListener implements MouseListener { + + JButton parentButton; + Menu parentMenu; + File file; + + public ButtonMouseListener(final JButton button, final Menu parentMenu, final File file) { + parentButton = button; + this.parentMenu = parentMenu; + this.file = file; + } + + @Override + public void mouseClicked(final MouseEvent e) { + try { + + if (e.getButton() == 1) + parentMenu.menuItemSelected(file); + if (e.getButton() == 3) + parentMenu.menuItemSelectedAlternative(file); + } catch (final Exception ex) { + new ExceptionDialog(ex); + } + } + + @Override + public void mouseEntered(final MouseEvent e) { + try { + if (parentButton != parentMenu.getSelectedButton()) { + + if (parentMenu.getSelectedButton() != null) + parentMenu.getSelectedButton().setBackground(getFileColor(parentMenu.getSelectedFile(), false)); + + parentMenu.setSelected(parentButton, file); + parentButton.setBackground(getFileColor(file, true)); + + parentMenu.menuItemHighlighted(file); + } + } catch (final Exception ex) { + new ExceptionDialog(ex); + } + } + + @Override + public void mouseExited(final MouseEvent e) { + } + + @Override + public void mousePressed(final MouseEvent e) { + } + + @Override + public void mouseReleased(final MouseEvent e) { + } + + } + public static final Color BUTTON_BG_DIRECTORY = new Color(0, 0, 60); - public static final Color BUTTON_BG_DIRECTORY_SELECTED = new Color(0, 0, 100); + public static final Color BUTTON_BG_DIRECTORY_SELECTED = new Color(0, 0, 100); public static final Color BUTTON_BG_LINK = new Color(60, 60, 0); - public static final Color BUTTON_BG_LINK_SELECTED = new Color(100, 100, 0); + public static final Color BUTTON_BG_LINK_SELECTED = new Color(100, 100, 0); public static final Color BUTTON_BG_EXECUTABLE_FILE = new Color(60, 0, 0); - public static final Color BUTTON_BG_EXECUTABLE_FILE_SELECTED = new Color(100, 0, 0); + public static final Color BUTTON_BG_EXECUTABLE_FILE_SELECTED = new Color(100, 0, 0); public static final Color BUTTON_BG_NONEXECUTABLE_FILE = new Color(0, 60, 0); - public static final Color BUTTON_BG_NONEXECUTABLE_FILE_SELECTED = new Color(0, 100, 0); + public static final Color BUTTON_BG_NONEXECUTABLE_FILE_SELECTED = new Color(0, 100, 0); public static final Dimension SIZE_BUTTON = new Dimension(200, 30); + public static final Dimension SIZE_MENU_PANEL = new Dimension(210, 900); + /** + *
+     * Assign specific colors to:
+     *    Directory
+     *    Executable file
+     *    Non executable file
+     * 
+ */ + public static Color getFileColor(final File file, final boolean selected) { + if (file.isDirectory()) + if (selected) + return BUTTON_BG_DIRECTORY_SELECTED; + else + return BUTTON_BG_DIRECTORY; + + if (file.isFile()) + if (file.canExecute()) { + if (selected) + return BUTTON_BG_EXECUTABLE_FILE_SELECTED; + else + return BUTTON_BG_EXECUTABLE_FILE; + } else if (selected) + return BUTTON_BG_NONEXECUTABLE_FILE_SELECTED; + else + return BUTTON_BG_NONEXECUTABLE_FILE; + + if (selected) + return BUTTON_BG_LINK_SELECTED; + else + return BUTTON_BG_LINK; + } + private JPanel menuPanel; public JButton selectedButton; - public File selectedFile; + + public File selectedFile;; private final Set menuListeners = new HashSet(); public Menu(final File directory) { buildContentPanel(directory); - }; + } public void addMenuListener(final MenuListener menuListener) { menuListeners.add(menuListener); @@ -54,11 +145,10 @@ public class Menu { public JButton buildButton(final File file) { JButton button; - if (file.isDirectory()) { + if (file.isDirectory()) button = new JButton("[ " + file.getName() + " ]"); - } else { + else button = new JButton(file.getName()); - } button.setBackground(getFileColor(file, false)); button.setForeground(new Color(255, 255, 255)); @@ -93,12 +183,10 @@ public class Menu { public int compare(final File f1, final File f2) { // directories come before files - if (f1.isFile() && f2.isDirectory()) { + if (f1.isFile() && f2.isDirectory()) return 1; - } - if (f2.isFile() && f1.isDirectory()) { + if (f2.isFile() && f1.isDirectory()) return -1; - } // order by file name return f1.getName().toUpperCase().compareTo(f2.getName().toUpperCase()); @@ -106,13 +194,12 @@ public class Menu { }); // add files to the panel - for (final File file : listFiles) { + for (final File file : listFiles) // ignore hidden files if (!file.getName().startsWith(".")) { menuPanel.add(Box.createVerticalStrut(10)); menuPanel.add(buildButton(file)); } - } } public JPanel getMenuPanel() { @@ -127,16 +214,19 @@ public class Menu { return selectedFile; } - public void notifyAlternativeEventListeners(final File chosenFile) { - for (final MenuListener menuListener : menuListeners) { - menuListener.menuItemSelectedAlternative(this, chosenFile); - } + public void menuItemHighlighted(final File chosenFile) { + for (final MenuListener menuListener : menuListeners) + menuListener.menuItemHighlighted(this, chosenFile); } - public void notifyEventListeners(final File chosenFile) { - for (final MenuListener menuListener : menuListeners) { + public void menuItemSelected(final File chosenFile) { + for (final MenuListener menuListener : menuListeners) menuListener.menuItemSelected(this, chosenFile); - } + } + + public void menuItemSelectedAlternative(final File chosenFile) { + for (final MenuListener menuListener : menuListeners) + menuListener.menuItemSelectedAlternative(this, chosenFile); } public void setSelected(final JButton selectedButton, final File selectedFile) { @@ -144,106 +234,4 @@ public class Menu { this.selectedFile = selectedFile; } - /** - *
-     * Assign specific colors to:
-     *    Directory
-     *    Executable file
-     *    Non executable file
-     * 
- */ - public static Color getFileColor(final File file, final boolean selected) { - if (file.isDirectory()) { - if (selected) { - return BUTTON_BG_DIRECTORY_SELECTED; - } else { - return BUTTON_BG_DIRECTORY; - } - } - - if (file.isFile()) { - if (file.canExecute()) { - if (selected) { - return BUTTON_BG_EXECUTABLE_FILE_SELECTED; - } else { - return BUTTON_BG_EXECUTABLE_FILE; - } - } else { - if (selected) { - return BUTTON_BG_NONEXECUTABLE_FILE_SELECTED; - } else { - return BUTTON_BG_NONEXECUTABLE_FILE; - } - } - } - - if (selected) { - return BUTTON_BG_LINK_SELECTED; - } else { - return BUTTON_BG_LINK; - } - } - - public static class ButtonMouseListener implements MouseListener { - - JButton parentButton; - Menu parentMenu; - File file; - - public ButtonMouseListener(final JButton button, final Menu parentMenu, final File file) { - parentButton = button; - this.parentMenu = parentMenu; - this.file = file; - } - - @Override - public void mouseClicked(final MouseEvent e) { - try { - - if (e.getButton() == 1) { - parentMenu.notifyEventListeners(file); - } - if (e.getButton() == 3) { - parentMenu.notifyAlternativeEventListeners(file); - } - } catch (final Exception ex) { - new ExceptionDialog(ex); - } - } - - @Override - public void mouseEntered(final MouseEvent e) { - try { - if (parentButton != parentMenu.getSelectedButton()) { - - if (parentMenu.getSelectedButton() != null) { - parentMenu.getSelectedButton().setBackground(getFileColor(parentMenu.getSelectedFile(), false)); - } - - parentMenu.setSelected(parentButton, file); - parentButton.setBackground(getFileColor(file, true)); - - if (file.isDirectory()) { - parentMenu.notifyEventListeners(file); - } - } - } catch (final Exception ex) { - new ExceptionDialog(ex); - } - } - - @Override - public void mouseExited(final MouseEvent e) { - } - - @Override - public void mousePressed(final MouseEvent e) { - } - - @Override - public void mouseReleased(final MouseEvent e) { - } - - } - } \ No newline at end of file diff --git a/src/main/java/eu/svjatoslav/instantlauncher/menu/MenuListener.java b/src/main/java/eu/svjatoslav/instantlauncher/menu/MenuListener.java index 40cb680..8080297 100755 --- a/src/main/java/eu/svjatoslav/instantlauncher/menu/MenuListener.java +++ b/src/main/java/eu/svjatoslav/instantlauncher/menu/MenuListener.java @@ -2,8 +2,19 @@ package eu.svjatoslav.instantlauncher.menu; public interface MenuListener { + /** + * mouse over + */ + public void menuItemHighlighted(Menu menu, java.io.File chosenFile); + + /** + * left click + */ public void menuItemSelected(Menu menu, java.io.File chosenFile); + /** + * right click + */ public void menuItemSelectedAlternative(Menu menu, java.io.File chosenFile); } diff --git a/src/main/java/eu/svjatoslav/instantlauncher/menu/MultiLevelMenu.java b/src/main/java/eu/svjatoslav/instantlauncher/menu/MultiLevelMenu.java index 308f5ba..700f842 100755 --- a/src/main/java/eu/svjatoslav/instantlauncher/menu/MultiLevelMenu.java +++ b/src/main/java/eu/svjatoslav/instantlauncher/menu/MultiLevelMenu.java @@ -93,7 +93,7 @@ public class MultiLevelMenu implements MenuListener { } @Override - public void menuItemSelected(final Menu menu, final File chosenFile) { + public void menuItemHighlighted(final Menu menu, final File chosenFile) { if (chosenFile.isDirectory()) { final int menuIndex = getMenuIndex(menu); @@ -107,9 +107,12 @@ public class MultiLevelMenu implements MenuListener { } } - if (chosenFile.isFile()) { + } - if (chosenFile.canExecute()) { + @Override + public void menuItemSelected(final Menu menu, final File chosenFile) { + if (chosenFile.isFile()) + if (chosenFile.canExecute()) try { // Execute a command without arguments final String command = chosenFile.getAbsolutePath(); @@ -122,18 +125,12 @@ public class MultiLevelMenu implements MenuListener { } catch (final IOException e) { new ExceptionDialog(e); } - - } else { + else { final boolean fileOpened = FileAssociationManager.openFile(chosenFile); - if (fileOpened) { + if (fileOpened) InstantLauncher.exitProgram(); - } } - } - } - @Override - public void menuItemSelectedAlternative(final Menu menu, final File chosenFile) { if (chosenFile.isDirectory()) { // Execute a command without arguments final String directoryPath = chosenFile.getAbsolutePath(); @@ -149,8 +146,27 @@ public class MultiLevelMenu implements MenuListener { } catch (final IOException e) { new ExceptionDialog(e); } - } else { + } + } + + @Override + public void menuItemSelectedAlternative(final Menu menu, final File chosenFile) { + if (chosenFile.isFile()) chosenFile.setExecutable(!chosenFile.canExecute()); + else { + // Execute a command without arguments + final String[] c = new String[2]; + + c[0] = "gnome-terminal"; + c[1] = "--working-directory=" + chosenFile.getAbsolutePath(); + + try { + Runtime.getRuntime().exec(c); + InstantLauncher.exitProgram(); + + } catch (final IOException e) { + new ExceptionDialog(e); + } } } @@ -161,9 +177,8 @@ public class MultiLevelMenu implements MenuListener { jPanel.removeAll(); } - while (menus.size() > fromIndex) { + while (menus.size() > fromIndex) menus.remove(fromIndex); - } } }