right click on directory opens it in terminal
[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                 try {
117                     // Execute a command without arguments
118                     final String command = chosenFile.getAbsolutePath();
119
120                     final String[] c = new String[1];
121                     c[0] = command;
122
123                     Runtime.getRuntime().exec(c);
124                     InstantLauncher.exitProgram();
125                 } catch (final IOException e) {
126                     new ExceptionDialog(e);
127                 }
128             else {
129                 final boolean fileOpened = FileAssociationManager.openFile(chosenFile);
130                 if (fileOpened)
131                     InstantLauncher.exitProgram();
132             }
133
134         if (chosenFile.isDirectory()) {
135             // Execute a command without arguments
136             final String directoryPath = chosenFile.getAbsolutePath();
137
138             final String[] c = new String[2];
139             c[0] = "nautilus";
140             c[1] = directoryPath;
141
142             try {
143                 Runtime.getRuntime().exec(c);
144                 InstantLauncher.exitProgram();
145
146             } catch (final IOException e) {
147                 new ExceptionDialog(e);
148             }
149         }
150     }
151
152     @Override
153     public void menuItemSelectedAlternative(final Menu menu, final File chosenFile) {
154         if (chosenFile.isFile())
155             chosenFile.setExecutable(!chosenFile.canExecute());
156         else {
157             // Execute a command without arguments
158             final String[] c = new String[2];
159
160             c[0] = "gnome-terminal";
161             c[1] = "--working-directory=" + chosenFile.getAbsolutePath();
162
163             try {
164                 Runtime.getRuntime().exec(c);
165                 InstantLauncher.exitProgram();
166
167             } catch (final IOException e) {
168                 new ExceptionDialog(e);
169             }
170         }
171     }
172
173     public void removeMenus(final int fromIndex) {
174
175         for (int i = fromIndex; i < menus.size(); i++) {
176             final JPanel jPanel = panels.get(i);
177             jPanel.removeAll();
178         }
179
180         while (menus.size() > fromIndex)
181             menus.remove(fromIndex);
182     }
183
184 }