initial commit
[instantlauncher.git] / src / main / java / eu / svjatoslav / instantlauncher / menu / MultiLevelMenu.java
1 package eu.svjatoslav.instantlauncher.menu;
2
3 import java.awt.BorderLayout;
4 import java.awt.Color;
5 import java.awt.Dimension;
6 import java.io.File;
7 import java.io.IOException;
8 import java.util.ArrayList;
9
10 import javax.swing.Box;
11 import javax.swing.BoxLayout;
12 import javax.swing.JPanel;
13
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;
18
19 public class MultiLevelMenu implements MenuListener {
20
21     private final InstantLauncher instantLauncher;
22
23     private static final int VERTICAL_MENUS_COUNT = 5;
24
25     public static final Dimension CONTENT_PANEL_SIZE = new Dimension(1024, 900);
26
27     ArrayList<Menu> menus = new ArrayList<Menu>();
28
29     ArrayList<JPanel> panels = new ArrayList<JPanel>();
30     JPanel contentPane;
31
32     public MultiLevelMenu(final InstantLauncher instantLauncher) {
33         this.instantLauncher = instantLauncher;
34     }
35
36     /**
37      * Adds new vertical menu.
38      * 
39      * @return <code>true</code> if operation succeeded, or <code>false</code>
40      *         if there was not enough space left for a new menu.
41      */
42     public boolean addMenu(final File directory) {
43         final int newMenuHorizontalIndex = menus.size();
44
45         if (newMenuHorizontalIndex >= VERTICAL_MENUS_COUNT)
46             return false;
47
48         final Menu menu = new Menu(directory);
49         menu.addMenuListener(this);
50
51         final JPanel placeholderPanel = panels.get(newMenuHorizontalIndex);
52         final JPanel menuPanel = menu.getMenuPanel();
53         placeholderPanel.add(menuPanel, BorderLayout.CENTER);
54
55         menus.add(menu);
56         return true;
57     }
58
59     public JPanel buildContentPanel() {
60
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);
65
66         // initialize panels
67         for (int i = 0; i < VERTICAL_MENUS_COUNT; i++) {
68             contentPane.add(Box.createHorizontalStrut(10));
69
70             final JPanel panel = new JPanel(new BorderLayout());
71             panel.setBackground(Color.BLACK);
72
73             Utils.setComponentSize(panel, Menu.SIZE_MENU_PANEL);
74
75             panels.add(panel);
76             contentPane.add(panel);
77         }
78
79         final File directory = instantLauncher.configuration.getRootDirectory();
80         addMenu(directory);
81
82         return contentPane;
83     }
84
85     public int getMenuIndex(final Menu menu) {
86         int i = 0;
87         for (final Menu m : menus) {
88             if (m == menu)
89                 return i;
90             i++;
91         }
92         return -1;
93     }
94
95     @Override
96     public void menuItemSelected(final Menu menu, final File chosenFile) {
97
98         if (chosenFile.isDirectory()) {
99             final int menuIndex = getMenuIndex(menu);
100
101             if (menuIndex >= 0) {
102                 removeMenus(menuIndex + 1);
103                 if (addMenu(chosenFile)) {
104                     contentPane.validate();
105                     contentPane.repaint();
106                 }
107             }
108         }
109
110         if (chosenFile.isFile()) {
111
112             if (chosenFile.canExecute()) {
113                 try {
114                     // Execute a command without arguments
115                     final String command = chosenFile.getAbsolutePath();
116
117                     final String[] c = new String[1];
118                     c[0] = command;
119
120                     Runtime.getRuntime().exec(c);
121                     InstantLauncher.exitProgram();
122                 } catch (final IOException e) {
123                     ExceptionDialog.showException(e);
124                 }
125
126             } else {
127                 final boolean fileOpened = FileAssociationManager.openFile(chosenFile);
128                 if (fileOpened) {
129                     InstantLauncher.exitProgram();
130                 }
131             }
132         }
133     }
134
135     @Override
136     public void menuItemSelectedAlternative(final Menu menu, final File chosenFile) {
137         if (chosenFile.isDirectory()) {
138             // Execute a command without arguments
139             final String directoryPath = chosenFile.getAbsolutePath();
140
141             final String[] c = new String[2];
142             c[0] = "nautilus";
143             c[1] = directoryPath;
144
145             try {
146                 Runtime.getRuntime().exec(c);
147                 InstantLauncher.exitProgram();
148
149             } catch (final IOException e) {
150                 ExceptionDialog.showException(e);
151             }
152         } else {
153             chosenFile.setExecutable(!chosenFile.canExecute());
154         }
155     }
156
157     public void removeMenus(final int fromIndex) {
158
159         for (int i = fromIndex; i < menus.size(); i++) {
160             final JPanel jPanel = panels.get(i);
161             jPanel.removeAll();
162         }
163
164         while (menus.size() > fromIndex) {
165             menus.remove(fromIndex);
166         }
167     }
168
169 }