simplified code
[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 = 7;
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 menuItemHighlighted(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         }
111
112         @Override
113         public void menuItemSelected(final Menu menu, final File chosenFile) {
114                 if (chosenFile.isFile())
115                         if (chosenFile.canExecute())
116                                 executeCommand(chosenFile.getAbsolutePath());
117                         else {
118                                 final boolean fileOpened = FileAssociationManager.openFile(chosenFile);
119                                 if (fileOpened)
120                                         InstantLauncher.exitProgram();
121                         }
122
123                 if (chosenFile.isDirectory()) {
124                         executeCommand("nautilus", "-n", chosenFile.getAbsolutePath());
125                 }
126         }
127
128         private void executeCommand(String... c) {
129                 try {
130                         Runtime.getRuntime().exec(c);
131                         InstantLauncher.exitProgram();
132
133                 } catch (final IOException e) {
134                         new ExceptionDialog(e);
135                 }
136         }
137
138         @Override
139         public void menuItemSelectedAlternative(final Menu menu, final File chosenFile) {
140                 if (chosenFile.isFile())
141                         chosenFile.setExecutable(!chosenFile.canExecute());
142                 else {
143                         executeCommand("gnome-terminal", "--working-directory=" + chosenFile.getAbsolutePath());
144                 }
145         }
146
147         public void removeMenus(final int fromIndex) {
148
149                 for (int i = fromIndex; i < menus.size(); i++) {
150                         final JPanel jPanel = panels.get(i);
151                         jPanel.removeAll();
152                 }
153
154                 while (menus.size() > fromIndex)
155                         menus.remove(fromIndex);
156         }
157
158         @Override
159         public void menuItemSelectedAlternative2(Menu menu, File chosenFile) {
160                 if (chosenFile.isFile()) {
161                         executeCommand("emacs", chosenFile.getAbsolutePath());
162                 }
163         }
164
165 }