1 package eu.svjatoslav.instantlauncher.menu;
3 import eu.svjatoslav.commons.gui.dialog.ExceptionDialog;
4 import eu.svjatoslav.instantlauncher.FileAssociationManager;
5 import eu.svjatoslav.instantlauncher.InstantLauncher;
6 import eu.svjatoslav.instantlauncher.Utils;
11 import java.io.IOException;
12 import java.util.ArrayList;
14 public class MultiLevelMenu implements MenuListener {
16 private static final int VERTICAL_MENUS_COUNT = 7;
17 private static final Dimension CONTENT_PANEL_SIZE = new Dimension(1024, 900);
18 private final InstantLauncher instantLauncher;
19 private final ArrayList<Menu> menus = new ArrayList<>();
21 private final ArrayList<JPanel> panels = new ArrayList<>();
22 private JPanel contentPane;
24 public MultiLevelMenu(final InstantLauncher instantLauncher) {
25 this.instantLauncher = instantLauncher;
29 * Adds new vertical menu.
31 * @return <code>true</code> if operation succeeded, or <code>false</code>
32 * if there was not enough space left for a new menu.
34 private boolean addMenu(final File directory) {
35 final int newMenuHorizontalIndex = menus.size();
37 if (newMenuHorizontalIndex >= VERTICAL_MENUS_COUNT) {
41 final Menu menu = new Menu(directory);
42 menu.addMenuListener(this);
44 final JPanel placeholderPanel = panels.get(newMenuHorizontalIndex);
45 final JPanel menuPanel = menu.getMenuPanel();
46 placeholderPanel.add(menuPanel, BorderLayout.CENTER);
52 public JPanel buildContentPanel() {
54 contentPane = new JPanel();
55 contentPane.setBackground(Color.BLACK);
56 contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.X_AXIS));
57 Utils.setComponentSize(contentPane, CONTENT_PANEL_SIZE);
60 for (int i = 0; i < VERTICAL_MENUS_COUNT; i++) {
61 contentPane.add(Box.createHorizontalStrut(10));
63 final JPanel panel = new JPanel(new BorderLayout());
64 panel.setBackground(Color.BLACK);
66 Utils.setComponentSize(panel, Menu.SIZE_MENU_PANEL);
69 contentPane.add(panel);
72 final File directory = instantLauncher.configuration.getRootDirectory();
78 private int getMenuIndex(final Menu menu) {
80 for (final Menu m : menus) {
90 public void menuItemHighlighted(final Menu menu, final File chosenFile) {
92 if (chosenFile.isDirectory()) {
93 final int menuIndex = getMenuIndex(menu);
96 removeMenus(menuIndex + 1);
97 if (addMenu(chosenFile)) {
98 contentPane.validate();
99 contentPane.repaint();
107 public void menuItemSelected(final Menu menu, final File chosenFile) {
108 if (chosenFile.isFile()) {
109 if (chosenFile.canExecute()) {
110 executeCommand(chosenFile.getAbsolutePath());
114 fileOpened = FileAssociationManager.openFile(chosenFile.getCanonicalFile());
116 InstantLauncher.exitProgram();
118 } catch (IOException e) {
124 if (chosenFile.isDirectory()) {
126 executeCommand("nautilus", "-w", chosenFile.getCanonicalFile().getAbsolutePath());
127 } catch (IOException e) {
133 private void executeCommand(String... c) {
135 Runtime.getRuntime().exec(c);
136 InstantLauncher.exitProgram();
138 } catch (final IOException e) {
139 new ExceptionDialog(e);
144 public void menuItemSelectedAlternative(final Menu menu, final File chosenFile) {
145 if (chosenFile.isFile()) {
146 chosenFile.setExecutable(!chosenFile.canExecute());
149 executeCommand("gnome-terminal", "--working-directory="
150 + chosenFile.getCanonicalFile().getAbsolutePath());
151 } catch (IOException e) {
157 private void removeMenus(final int fromIndex) {
159 for (int i = fromIndex; i < menus.size(); i++) {
160 final JPanel jPanel = panels.get(i);
164 while (menus.size() > fromIndex) {
165 menus.remove(fromIndex);
170 public void menuItemSelectedAlternative2(Menu menu, File chosenFile) {
171 if (chosenFile.isFile()) {
173 executeCommand("emacs", chosenFile.getCanonicalFile().getAbsolutePath());
174 } catch (IOException e) {