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 import static eu.svjatoslav.instantlauncher.Utils.executeCommand;
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.configurationManager.getConfiguration().getNavigationRootDirectory();
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 File chosenFile) {
108 if (chosenFile.isFile()) {
109 if (chosenFile.canExecute()) {
110 executeCommand(chosenFile.getAbsolutePath());
114 fileOpened = instantLauncher.openFile(chosenFile.getCanonicalFile());
116 InstantLauncher.exitProgram();
118 } catch (IOException e) {
124 if (chosenFile.isDirectory()) {
126 instantLauncher.openDirectory(chosenFile.getCanonicalFile());
127 InstantLauncher.exitProgram();
128 } catch (IOException e) {
135 public void menuItemSelectedAlternative(final File chosenFile) {
136 if (chosenFile.isFile())
137 chosenFile.setExecutable(!chosenFile.canExecute());
139 instantLauncher.openDirectoryInTerminal(chosenFile);
140 InstantLauncher.exitProgram();
144 private void removeMenus(final int fromIndex) {
146 for (int i = fromIndex; i < menus.size(); i++) {
147 final JPanel jPanel = panels.get(i);
151 while (menus.size() > fromIndex) {
152 menus.remove(fromIndex);
157 public void menuItemSelectedAlternative2(File chosenFile) {
158 // TODO: define some middle mouse click functions