X-Git-Url: http://www2.svjatoslav.eu/gitweb/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Feu%2Fsvjatoslav%2Finstantlauncher%2Fmenu%2FMultiLevelMenu.java;h=de2e4c58be80e7dda99aa3e1eee464182259523b;hb=617c93e31fc6f020323b503fe0028a8e10c746b6;hp=f66f952d02770669e8ba7614c875befbaedb400a;hpb=e83e5b610b7fe67d66fc01c634975a6af7f54dad;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 f66f952..de2e4c5 100755 --- a/src/main/java/eu/svjatoslav/instantlauncher/menu/MultiLevelMenu.java +++ b/src/main/java/eu/svjatoslav/instantlauncher/menu/MultiLevelMenu.java @@ -1,170 +1,162 @@ package eu.svjatoslav.instantlauncher.menu; -import java.awt.BorderLayout; -import java.awt.Color; -import java.awt.Dimension; +import eu.svjatoslav.instantlauncher.InstantLauncher; +import eu.svjatoslav.instantlauncher.Utils; + +import javax.swing.*; +import java.awt.*; import java.io.File; import java.io.IOException; import java.util.ArrayList; -import javax.swing.Box; -import javax.swing.BoxLayout; -import javax.swing.JPanel; - -import eu.svjatoslav.commons.gui.dialog.ExceptionDialog; -import eu.svjatoslav.instantlauncher.FileAssociationManager; -import eu.svjatoslav.instantlauncher.InstantLauncher; -import eu.svjatoslav.instantlauncher.Utils; +import static eu.svjatoslav.instantlauncher.Utils.executeCommand; 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); + private static final int VERTICAL_MENUS_COUNT = 7; + private static final Dimension CONTENT_PANEL_SIZE = new Dimension(1024, 900); + private final InstantLauncher instantLauncher; + private final ArrayList menus = new ArrayList<>(); + + private final ArrayList panels = new ArrayList<>(); + private 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. + */ + private 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.configurationManager.getConfiguration().getNavigationRootDirectory(); + addMenu(directory); + + return contentPane; + } + + private 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 File chosenFile) { + if (chosenFile.isFile()) { + if (chosenFile.canExecute()) { + executeCommand(chosenFile.getAbsolutePath()); + } else { + boolean fileOpened; + try { + fileOpened = instantLauncher.openFile(chosenFile.getCanonicalFile()); + if (fileOpened) + InstantLauncher.exitProgram(); + + } catch (IOException e) { + e.printStackTrace(); + } + } + } + + if (chosenFile.isDirectory()) { + try { + instantLauncher.openDirectory(chosenFile.getCanonicalFile()); + InstantLauncher.exitProgram(); + } catch (IOException e) { + e.printStackTrace(); + } + } + } + + @Override + public void menuItemSelectedAlternative(final File chosenFile) { + if (chosenFile.isFile()) + chosenFile.setExecutable(!chosenFile.canExecute()); + else { + instantLauncher.openDirectoryInTerminal(chosenFile); + InstantLauncher.exitProgram(); + } + } + + private 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(File chosenFile) { + // TODO: define some middle mouse click functions + } - 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()) { - executeCommand(chosenFile.getAbsolutePath()); - } else { - final boolean fileOpened = FileAssociationManager.openFile(chosenFile); - if (fileOpened) { - InstantLauncher.exitProgram(); - } - } - } - - if (chosenFile.isDirectory()) { - executeCommand("nautilus", "-w", 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()); - } - } }