From 629244b2665bfde057efc66aa333ca8e8cacd6cd Mon Sep 17 00:00:00 2001 From: Svjatoslav Agejenko Date: Wed, 27 Aug 2014 09:30:47 +0300 Subject: [PATCH] possibility to open file with emacs using with middle mouse click --- .../svjatoslav/instantlauncher/menu/Menu.java | 419 +++++++++--------- .../instantlauncher/menu/MenuListener.java | 29 +- .../instantlauncher/menu/MultiLevelMenu.java | 311 +++++++------ 3 files changed, 378 insertions(+), 381 deletions(-) diff --git a/src/main/java/eu/svjatoslav/instantlauncher/menu/Menu.java b/src/main/java/eu/svjatoslav/instantlauncher/menu/Menu.java index 948672a..916f8ef 100755 --- a/src/main/java/eu/svjatoslav/instantlauncher/menu/Menu.java +++ b/src/main/java/eu/svjatoslav/instantlauncher/menu/Menu.java @@ -22,216 +22,223 @@ 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) { - } + 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() == 2) + parentMenu.menuItemSelectedAlternative2(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) { + } - @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_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_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_NONEXECUTABLE_FILE = new Color(0, 60, 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;; + + private final Set menuListeners = new HashSet(); + + public Menu(final File directory) { + buildContentPanel(directory); + } + + public void addMenuListener(final MenuListener menuListener) { + menuListeners.add(menuListener); + } + + public JButton buildButton(final File file) { + JButton button; + if (file.isDirectory()) + button = new JButton("[ " + file.getName() + " ]"); + else + button = new JButton(file.getName()); + + button.setBackground(getFileColor(file, false)); + button.setForeground(new Color(255, 255, 255)); + + button.setBorder(new EmptyBorder(5, 10, 5, 10)); + + final Menu.ButtonMouseListener mouseListener = new ButtonMouseListener(button, this, file); + button.addMouseListener(mouseListener); + + button.setHorizontalTextPosition(SwingConstants.CENTER); + + // set button size + Utils.setComponentSize(button, SIZE_BUTTON); + + return button; + } + + private void buildContentPanel(final File directory) { + menuPanel = new JPanel(); + + Utils.setComponentSize(menuPanel, SIZE_MENU_PANEL); + + final BoxLayout layout = new BoxLayout(menuPanel, BoxLayout.Y_AXIS); + menuPanel.setLayout(layout); + menuPanel.setBackground(Color.BLACK); + + final File[] listFiles = directory.listFiles(); + + // Sort files by name + Arrays.sort(listFiles, new Comparator() { + @Override + public int compare(final File f1, final File f2) { + + // directories come before files + if (f1.isFile() && f2.isDirectory()) + return 1; + if (f2.isFile() && f1.isDirectory()) + return -1; + + // order by file name + return f1.getName().toUpperCase().compareTo(f2.getName().toUpperCase()); + } + }); + + // add files to the panel + for (final File file : listFiles) + // ignore hidden files + if (!file.getName().startsWith(".")) { + menuPanel.add(Box.createVerticalStrut(10)); + menuPanel.add(buildButton(file)); + } + } + + public JPanel getMenuPanel() { + return menuPanel; + } + + public JButton getSelectedButton() { + return selectedButton; + } + + public File getSelectedFile() { + return selectedFile; + } + + public void menuItemHighlighted(final File chosenFile) { + for (final MenuListener menuListener : menuListeners) + menuListener.menuItemHighlighted(this, chosenFile); + } - } + public void menuItemSelected(final File chosenFile) { + for (final MenuListener menuListener : menuListeners) + menuListener.menuItemSelected(this, chosenFile); + } - public static final Color BUTTON_BG_DIRECTORY = new Color(0, 0, 60); + public void menuItemSelectedAlternative(final File chosenFile) { + for (final MenuListener menuListener : menuListeners) + menuListener.menuItemSelectedAlternative(this, chosenFile); + } - 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_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_NONEXECUTABLE_FILE = new Color(0, 60, 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;; - - private final Set menuListeners = new HashSet(); - - public Menu(final File directory) { - buildContentPanel(directory); - } - - public void addMenuListener(final MenuListener menuListener) { - menuListeners.add(menuListener); - } - - public JButton buildButton(final File file) { - JButton button; - if (file.isDirectory()) - button = new JButton("[ " + file.getName() + " ]"); - else - button = new JButton(file.getName()); - - button.setBackground(getFileColor(file, false)); - button.setForeground(new Color(255, 255, 255)); - - button.setBorder(new EmptyBorder(5, 10, 5, 10)); - - final Menu.ButtonMouseListener mouseListener = new ButtonMouseListener(button, this, file); - button.addMouseListener(mouseListener); - - button.setHorizontalTextPosition(SwingConstants.CENTER); - - // set button size - Utils.setComponentSize(button, SIZE_BUTTON); - - return button; - } - - private void buildContentPanel(final File directory) { - menuPanel = new JPanel(); - - Utils.setComponentSize(menuPanel, SIZE_MENU_PANEL); - - final BoxLayout layout = new BoxLayout(menuPanel, BoxLayout.Y_AXIS); - menuPanel.setLayout(layout); - menuPanel.setBackground(Color.BLACK); - - final File[] listFiles = directory.listFiles(); - - // Sort files by name - Arrays.sort(listFiles, new Comparator() { - @Override - public int compare(final File f1, final File f2) { - - // directories come before files - if (f1.isFile() && f2.isDirectory()) - return 1; - if (f2.isFile() && f1.isDirectory()) - return -1; - - // order by file name - return f1.getName().toUpperCase().compareTo(f2.getName().toUpperCase()); - } - }); - - // add files to the panel - for (final File file : listFiles) - // ignore hidden files - if (!file.getName().startsWith(".")) { - menuPanel.add(Box.createVerticalStrut(10)); - menuPanel.add(buildButton(file)); - } - } - - public JPanel getMenuPanel() { - return menuPanel; - } - - public JButton getSelectedButton() { - return selectedButton; - } - - public File getSelectedFile() { - return selectedFile; - } - - public void menuItemHighlighted(final File chosenFile) { - for (final MenuListener menuListener : menuListeners) - menuListener.menuItemHighlighted(this, chosenFile); - } - - 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) { - this.selectedButton = selectedButton; - this.selectedFile = selectedFile; - } + public void menuItemSelectedAlternative2(final File chosenFile) { + for (final MenuListener menuListener : menuListeners) + menuListener.menuItemSelectedAlternative2(this, chosenFile); + } + + public void setSelected(final JButton selectedButton, final File selectedFile) { + this.selectedButton = selectedButton; + this.selectedFile = selectedFile; + } } \ 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 8080297..82e1a57 100755 --- a/src/main/java/eu/svjatoslav/instantlauncher/menu/MenuListener.java +++ b/src/main/java/eu/svjatoslav/instantlauncher/menu/MenuListener.java @@ -2,19 +2,24 @@ package eu.svjatoslav.instantlauncher.menu; public interface MenuListener { - /** - * mouse over - */ - public void menuItemHighlighted(Menu menu, java.io.File chosenFile); + /** + * mouse over + */ + public void menuItemHighlighted(Menu menu, java.io.File chosenFile); - /** - * left click - */ - public void menuItemSelected(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); + /** + * right click + */ + public void menuItemSelectedAlternative(Menu menu, java.io.File chosenFile); + + /** + * middle click + */ + public void menuItemSelectedAlternative2(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 f3f1d93..7c323b3 100755 --- a/src/main/java/eu/svjatoslav/instantlauncher/menu/MultiLevelMenu.java +++ b/src/main/java/eu/svjatoslav/instantlauncher/menu/MultiLevelMenu.java @@ -18,168 +18,153 @@ import eu.svjatoslav.instantlauncher.Utils; public class MultiLevelMenu implements MenuListener { - private final InstantLauncher instantLauncher; - - private static final int VERTICAL_MENUS_COUNT = 7; - - public static final Dimension CONTENT_PANEL_SIZE = new Dimension(1024, 900); - - ArrayList menus = new ArrayList(); - - ArrayList panels = new ArrayList(); - JPanel contentPane; - - public MultiLevelMenu(final InstantLauncher instantLauncher) { - this.instantLauncher = instantLauncher; - } - - /** - * Adds new vertical menu. - * - * @return true if operation succeeded, or false - * if there was not enough space left for a new menu. - */ - public boolean addMenu(final File directory) { - final int newMenuHorizontalIndex = menus.size(); - - if (newMenuHorizontalIndex >= VERTICAL_MENUS_COUNT) - return false; - - final Menu menu = new Menu(directory); - menu.addMenuListener(this); - - final JPanel placeholderPanel = panels.get(newMenuHorizontalIndex); - final JPanel menuPanel = menu.getMenuPanel(); - placeholderPanel.add(menuPanel, BorderLayout.CENTER); - - menus.add(menu); - return true; - } - - public JPanel buildContentPanel() { - - contentPane = new JPanel(); - contentPane.setBackground(Color.BLACK); - contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.X_AXIS)); - Utils.setComponentSize(contentPane, CONTENT_PANEL_SIZE); - - // initialize panels - for (int i = 0; i < VERTICAL_MENUS_COUNT; i++) { - contentPane.add(Box.createHorizontalStrut(10)); - - final JPanel panel = new JPanel(new BorderLayout()); - panel.setBackground(Color.BLACK); - - Utils.setComponentSize(panel, Menu.SIZE_MENU_PANEL); - - panels.add(panel); - contentPane.add(panel); - } - - final File directory = instantLauncher.configuration.getRootDirectory(); - addMenu(directory); - - return contentPane; - } - - public int getMenuIndex(final Menu menu) { - int i = 0; - for (final Menu m : menus) { - if (m == menu) - return i; - i++; - } - return -1; - } - - @Override - public void menuItemHighlighted(final Menu menu, final File chosenFile) { - - if (chosenFile.isDirectory()) { - final int menuIndex = getMenuIndex(menu); - - if (menuIndex >= 0) { - removeMenus(menuIndex + 1); - if (addMenu(chosenFile)) { - contentPane.validate(); - contentPane.repaint(); - } - } - } - - } - - @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(); - - final String[] c = new String[1]; - c[0] = command; - - Runtime.getRuntime().exec(c); - InstantLauncher.exitProgram(); - } catch (final IOException e) { - new ExceptionDialog(e); - } - else { - final boolean fileOpened = FileAssociationManager.openFile(chosenFile); - if (fileOpened) - InstantLauncher.exitProgram(); - } - - if (chosenFile.isDirectory()) { - // Execute a command without arguments - final String directoryPath = chosenFile.getAbsolutePath(); - - final String[] c = new String[3]; - c[0] = "nautilus"; - c[1] = "-n"; - c[2] = directoryPath; - - try { - Runtime.getRuntime().exec(c); - InstantLauncher.exitProgram(); - - } catch (final IOException e) { - new ExceptionDialog(e); - } - } - } - - @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); - } - } - } - - public void removeMenus(final int fromIndex) { - - for (int i = fromIndex; i < menus.size(); i++) { - final JPanel jPanel = panels.get(i); - jPanel.removeAll(); - } - - while (menus.size() > fromIndex) - menus.remove(fromIndex); - } + private final InstantLauncher instantLauncher; + + private static final int VERTICAL_MENUS_COUNT = 7; + + public static final Dimension CONTENT_PANEL_SIZE = new Dimension(1024, 900); + + ArrayList menus = new ArrayList(); + + ArrayList panels = new ArrayList(); + JPanel contentPane; + + public MultiLevelMenu(final InstantLauncher instantLauncher) { + this.instantLauncher = instantLauncher; + } + + /** + * Adds new vertical menu. + * + * @return true if operation succeeded, or false + * if there was not enough space left for a new menu. + */ + public boolean addMenu(final File directory) { + final int newMenuHorizontalIndex = menus.size(); + + if (newMenuHorizontalIndex >= VERTICAL_MENUS_COUNT) + return false; + + final Menu menu = new Menu(directory); + menu.addMenuListener(this); + + final JPanel placeholderPanel = panels.get(newMenuHorizontalIndex); + final JPanel menuPanel = menu.getMenuPanel(); + placeholderPanel.add(menuPanel, BorderLayout.CENTER); + + menus.add(menu); + return true; + } + + public JPanel buildContentPanel() { + + contentPane = new JPanel(); + contentPane.setBackground(Color.BLACK); + contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.X_AXIS)); + Utils.setComponentSize(contentPane, CONTENT_PANEL_SIZE); + + // initialize panels + for (int i = 0; i < VERTICAL_MENUS_COUNT; i++) { + contentPane.add(Box.createHorizontalStrut(10)); + + final JPanel panel = new JPanel(new BorderLayout()); + panel.setBackground(Color.BLACK); + + Utils.setComponentSize(panel, Menu.SIZE_MENU_PANEL); + + panels.add(panel); + contentPane.add(panel); + } + + final File directory = instantLauncher.configuration.getRootDirectory(); + addMenu(directory); + + return contentPane; + } + + public int getMenuIndex(final Menu menu) { + int i = 0; + for (final Menu m : menus) { + if (m == menu) + return i; + i++; + } + return -1; + } + + @Override + public void menuItemHighlighted(final Menu menu, final File chosenFile) { + + if (chosenFile.isDirectory()) { + final int menuIndex = getMenuIndex(menu); + + if (menuIndex >= 0) { + removeMenus(menuIndex + 1); + if (addMenu(chosenFile)) { + contentPane.validate(); + contentPane.repaint(); + } + } + } + + } + + @Override + public void menuItemSelected(final Menu menu, final File chosenFile) { + if (chosenFile.isFile()) + if (chosenFile.canExecute()) + try { + Runtime.getRuntime().exec(chosenFile.getAbsolutePath()); + InstantLauncher.exitProgram(); + } catch (final IOException e) { + new ExceptionDialog(e); + } + else { + final boolean fileOpened = FileAssociationManager.openFile(chosenFile); + if (fileOpened) + InstantLauncher.exitProgram(); + } + + if (chosenFile.isDirectory()) { + executeCommand("nautilus", "-n", chosenFile.getAbsolutePath()); + } + } + + private void executeCommand(String... c) { + try { + Runtime.getRuntime().exec(c); + InstantLauncher.exitProgram(); + + } catch (final IOException e) { + new ExceptionDialog(e); + } + } + + @Override + public void menuItemSelectedAlternative(final Menu menu, final File chosenFile) { + if (chosenFile.isFile()) + chosenFile.setExecutable(!chosenFile.canExecute()); + else { + executeCommand("gnome-terminal", "--working-directory=" + chosenFile.getAbsolutePath()); + } + } + + public void removeMenus(final int fromIndex) { + + for (int i = fromIndex; i < menus.size(); i++) { + final JPanel jPanel = panels.get(i); + jPanel.removeAll(); + } + + while (menus.size() > fromIndex) + menus.remove(fromIndex); + } + + @Override + public void menuItemSelectedAlternative2(Menu menu, File chosenFile) { + if (chosenFile.isFile()) { + executeCommand("emacs", chosenFile.getAbsolutePath()); + } + } } -- 2.20.1