1 package eu.svjatoslav.instantlauncher.menu;
3 import eu.svjatoslav.commons.gui.dialog.ExceptionDialog;
4 import eu.svjatoslav.instantlauncher.InstantLauncher;
5 import eu.svjatoslav.instantlauncher.Utils;
10 import java.io.IOException;
11 import java.util.ArrayList;
13 public class MultiLevelMenu implements MenuListener {
15 private static final int VERTICAL_MENUS_COUNT = 7;
16 private static final Dimension CONTENT_PANEL_SIZE = new Dimension(1024, 900);
17 private final InstantLauncher instantLauncher;
18 private final ArrayList<Menu> menus = new ArrayList<>();
20 private final ArrayList<JPanel> panels = new ArrayList<>();
21 private JPanel contentPane;
23 public MultiLevelMenu(final InstantLauncher instantLauncher) {
24 this.instantLauncher = instantLauncher;
28 * Adds new vertical menu.
30 * @return <code>true</code> if operation succeeded, or <code>false</code>
31 * if there was not enough space left for a new menu.
33 private boolean addMenu(final File directory) {
34 final int newMenuHorizontalIndex = menus.size();
36 if (newMenuHorizontalIndex >= VERTICAL_MENUS_COUNT) {
40 final Menu menu = new Menu(directory);
41 menu.addMenuListener(this);
43 final JPanel placeholderPanel = panels.get(newMenuHorizontalIndex);
44 final JPanel menuPanel = menu.getMenuPanel();
45 placeholderPanel.add(menuPanel, BorderLayout.CENTER);
51 public JPanel buildContentPanel() {
53 contentPane = new JPanel();
54 contentPane.setBackground(Color.BLACK);
55 contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.X_AXIS));
56 Utils.setComponentSize(contentPane, CONTENT_PANEL_SIZE);
59 for (int i = 0; i < VERTICAL_MENUS_COUNT; i++) {
60 contentPane.add(Box.createHorizontalStrut(10));
62 final JPanel panel = new JPanel(new BorderLayout());
63 panel.setBackground(Color.BLACK);
65 Utils.setComponentSize(panel, Menu.SIZE_MENU_PANEL);
68 contentPane.add(panel);
71 final File directory = instantLauncher.configurationManager.getNavigationRootDirectory();
77 private int getMenuIndex(final Menu menu) {
79 for (final Menu m : menus) {
89 public void menuItemHighlighted(final Menu menu, final File chosenFile) {
91 if (chosenFile.isDirectory()) {
92 final int menuIndex = getMenuIndex(menu);
95 removeMenus(menuIndex + 1);
96 if (addMenu(chosenFile)) {
97 contentPane.validate();
98 contentPane.repaint();
106 public void menuItemSelected(final Menu menu, final File chosenFile) {
107 if (chosenFile.isFile()) {
108 if (chosenFile.canExecute()) {
109 executeCommand(chosenFile.getAbsolutePath());
113 fileOpened = instantLauncher.openFile(chosenFile.getCanonicalFile());
115 InstantLauncher.exitProgram();
117 } catch (IOException e) {
123 if (chosenFile.isDirectory()) {
125 executeCommand("nautilus", "-w", chosenFile.getCanonicalFile().getAbsolutePath());
126 } catch (IOException e) {
132 private void executeCommand(String... c) {
134 Runtime.getRuntime().exec(c);
135 InstantLauncher.exitProgram();
137 } catch (final IOException e) {
138 new ExceptionDialog(e);
143 public void menuItemSelectedAlternative(final Menu menu, final File chosenFile) {
144 if (chosenFile.isFile()) {
145 chosenFile.setExecutable(!chosenFile.canExecute());
148 executeCommand("gnome-terminal", "--working-directory="
149 + chosenFile.getCanonicalFile().getAbsolutePath());
150 } catch (IOException e) {
156 private void removeMenus(final int fromIndex) {
158 for (int i = fromIndex; i < menus.size(); i++) {
159 final JPanel jPanel = panels.get(i);
163 while (menus.size() > fromIndex) {
164 menus.remove(fromIndex);
169 public void menuItemSelectedAlternative2(Menu menu, File chosenFile) {
170 if (chosenFile.isFile()) {
172 executeCommand("emacs", chosenFile.getCanonicalFile().getAbsolutePath());
173 } catch (IOException e) {