X-Git-Url: http://www2.svjatoslav.eu/gitweb/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Feu%2Fsvjatoslav%2Finstantlauncher%2Fmenu%2FMultiLevelMenu.java;h=7c323b32b22d3415c59cec2b5b445db5cfeea510;hb=629244b2665bfde057efc66aa333ca8e8cacd6cd;hp=f3f1d93ba9beb0a99388a84b10eddb6d1d594484;hpb=5188164dc3e239f8a69224858d6a5a2150a61045;p=instantlauncher.git 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()); + } + } }