1 package eu.svjatoslav.instantlauncher.menu;
3 import java.awt.BorderLayout;
5 import java.awt.Dimension;
7 import java.io.IOException;
8 import java.util.ArrayList;
10 import javax.swing.Box;
11 import javax.swing.BoxLayout;
12 import javax.swing.JPanel;
14 import eu.svjatoslav.commons.gui.dialog.ExceptionDialog;
15 import eu.svjatoslav.instantlauncher.FileAssociationManager;
16 import eu.svjatoslav.instantlauncher.InstantLauncher;
17 import eu.svjatoslav.instantlauncher.Utils;
19 public class MultiLevelMenu implements MenuListener {
21 private final InstantLauncher instantLauncher;
23 private static final int VERTICAL_MENUS_COUNT = 7;
25 public static final Dimension CONTENT_PANEL_SIZE = new Dimension(1024, 900);
27 ArrayList<Menu> menus = new ArrayList<Menu>();
29 ArrayList<JPanel> panels = new ArrayList<JPanel>();
32 public MultiLevelMenu(final InstantLauncher instantLauncher) {
33 this.instantLauncher = instantLauncher;
37 * Adds new vertical menu.
39 * @return <code>true</code> if operation succeeded, or <code>false</code>
40 * if there was not enough space left for a new menu.
42 public boolean addMenu(final File directory) {
43 final int newMenuHorizontalIndex = menus.size();
45 if (newMenuHorizontalIndex >= VERTICAL_MENUS_COUNT)
48 final Menu menu = new Menu(directory);
49 menu.addMenuListener(this);
51 final JPanel placeholderPanel = panels.get(newMenuHorizontalIndex);
52 final JPanel menuPanel = menu.getMenuPanel();
53 placeholderPanel.add(menuPanel, BorderLayout.CENTER);
59 public JPanel buildContentPanel() {
61 contentPane = new JPanel();
62 contentPane.setBackground(Color.BLACK);
63 contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.X_AXIS));
64 Utils.setComponentSize(contentPane, CONTENT_PANEL_SIZE);
67 for (int i = 0; i < VERTICAL_MENUS_COUNT; i++) {
68 contentPane.add(Box.createHorizontalStrut(10));
70 final JPanel panel = new JPanel(new BorderLayout());
71 panel.setBackground(Color.BLACK);
73 Utils.setComponentSize(panel, Menu.SIZE_MENU_PANEL);
76 contentPane.add(panel);
79 final File directory = instantLauncher.configuration.getRootDirectory();
85 public int getMenuIndex(final Menu menu) {
87 for (final Menu m : menus) {
96 public void menuItemHighlighted(final Menu menu, final File chosenFile) {
98 if (chosenFile.isDirectory()) {
99 final int menuIndex = getMenuIndex(menu);
101 if (menuIndex >= 0) {
102 removeMenus(menuIndex + 1);
103 if (addMenu(chosenFile)) {
104 contentPane.validate();
105 contentPane.repaint();
113 public void menuItemSelected(final Menu menu, final File chosenFile) {
114 if (chosenFile.isFile())
115 if (chosenFile.canExecute())
117 // Execute a command without arguments
118 final String command = chosenFile.getAbsolutePath();
120 final String[] c = new String[1];
123 Runtime.getRuntime().exec(c);
124 InstantLauncher.exitProgram();
125 } catch (final IOException e) {
126 new ExceptionDialog(e);
129 final boolean fileOpened = FileAssociationManager.openFile(chosenFile);
131 InstantLauncher.exitProgram();
134 if (chosenFile.isDirectory()) {
135 // Execute a command without arguments
136 final String directoryPath = chosenFile.getAbsolutePath();
138 final String[] c = new String[3];
141 c[2] = directoryPath;
144 Runtime.getRuntime().exec(c);
145 InstantLauncher.exitProgram();
147 } catch (final IOException e) {
148 new ExceptionDialog(e);
154 public void menuItemSelectedAlternative(final Menu menu, final File chosenFile) {
155 if (chosenFile.isFile())
156 chosenFile.setExecutable(!chosenFile.canExecute());
158 // Execute a command without arguments
159 final String[] c = new String[2];
161 c[0] = "gnome-terminal";
162 c[1] = "--working-directory=" + chosenFile.getAbsolutePath();
165 Runtime.getRuntime().exec(c);
166 InstantLauncher.exitProgram();
168 } catch (final IOException e) {
169 new ExceptionDialog(e);
174 public void removeMenus(final int fromIndex) {
176 for (int i = fromIndex; i < menus.size(); i++) {
177 final JPanel jPanel = panels.get(i);
181 while (menus.size() > fromIndex)
182 menus.remove(fromIndex);