1 package eu.svjatoslav.instantlauncher.menu;
3 import eu.svjatoslav.instantlauncher.InstantLauncher;
4 import eu.svjatoslav.instantlauncher.Utils;
9 import java.io.IOException;
10 import java.util.ArrayList;
12 public class MultiLevelMenu implements MenuListener {
14 private static final int VERTICAL_MENUS_COUNT = 7;
15 private static final Dimension CONTENT_PANEL_SIZE = new Dimension(1024, 900);
16 private final InstantLauncher instantLauncher;
17 private final ArrayList<Menu> menus = new ArrayList<>();
19 private final ArrayList<JPanel> panels = new ArrayList<>();
20 private JPanel contentPane;
22 public MultiLevelMenu(final InstantLauncher instantLauncher) {
23 this.instantLauncher = instantLauncher;
27 * Adds new vertical menu.
29 * @return <code>true</code> if operation succeeded, or <code>false</code>
30 * if there was not enough space left for a new menu.
32 private boolean addMenu(final File directory) {
33 final int newMenuHorizontalIndex = menus.size();
35 if (newMenuHorizontalIndex >= VERTICAL_MENUS_COUNT) {
39 final Menu menu = new Menu(directory);
40 menu.addMenuListener(this);
42 final JPanel placeholderPanel = panels.get(newMenuHorizontalIndex);
43 final JPanel menuPanel = menu.getMenuPanel();
44 placeholderPanel.add(menuPanel, BorderLayout.CENTER);
50 public JPanel buildContentPanel() {
52 contentPane = new JPanel();
53 contentPane.setBackground(Color.BLACK);
54 contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.X_AXIS));
55 Utils.setComponentSize(contentPane, CONTENT_PANEL_SIZE);
58 for (int i = 0; i < VERTICAL_MENUS_COUNT; i++) {
59 contentPane.add(Box.createHorizontalStrut(10));
61 final JPanel panel = new JPanel(new BorderLayout());
62 panel.setBackground(Color.BLACK);
64 Utils.setComponentSize(panel, Menu.SIZE_MENU_PANEL);
67 contentPane.add(panel);
70 final File directory = instantLauncher.configurationManager.getConfiguration().getNavigationRootDirectory();
76 private int getMenuIndex(final Menu menu) {
78 for (final Menu m : menus) {
88 public void menuItemHighlighted(final Menu menu, final File chosenFile) {
90 if (chosenFile.isDirectory()) {
91 final int menuIndex = getMenuIndex(menu);
94 removeMenus(menuIndex + 1);
95 if (addMenu(chosenFile)) {
96 contentPane.validate();
97 contentPane.repaint();
105 public void menuItemSelected(final File chosenFile) {
106 if (chosenFile.isFile()) {
107 if (chosenFile.canExecute()) {
108 instantLauncher.executeCommand(chosenFile.getAbsolutePath());
112 fileOpened = instantLauncher.openFile(chosenFile.getCanonicalFile());
114 InstantLauncher.exitProgram();
116 } catch (IOException e) {
122 if (chosenFile.isDirectory()) {
124 instantLauncher.openDirectory(chosenFile.getCanonicalFile());
125 } catch (IOException e) {
132 public void menuItemSelectedAlternative(final File chosenFile) {
133 if (chosenFile.isFile())
134 chosenFile.setExecutable(!chosenFile.canExecute());
137 instantLauncher.executeCommand("gnome-terminal", "--working-directory="
138 + chosenFile.getCanonicalFile().getAbsolutePath());
139 } catch (IOException e) {
145 private void removeMenus(final int fromIndex) {
147 for (int i = fromIndex; i < menus.size(); i++) {
148 final JPanel jPanel = panels.get(i);
152 while (menus.size() > fromIndex) {
153 menus.remove(fromIndex);
158 public void menuItemSelectedAlternative2(File chosenFile) {
159 if (chosenFile.isFile()) {
161 instantLauncher.executeCommand("emacs", chosenFile.getCanonicalFile().getAbsolutePath());
162 } catch (IOException e) {