2 * Instantlauncher. Author: Svjatoslav Agejenko, svjatoslav@svjatoslav.eu
3 * This project is released under Creative Commons Zero (CC0) license.
5 package eu.svjatoslav.instantlauncher.menu;
7 import eu.svjatoslav.instantlauncher.InstantLauncher;
8 import eu.svjatoslav.instantlauncher.Utils;
13 import java.io.IOException;
14 import java.util.ArrayList;
16 import static eu.svjatoslav.instantlauncher.Utils.executeCommand;
18 public class MultiLevelMenu implements MenuListener {
20 private static final int VERTICAL_MENUS_COUNT = 7;
21 private static final Dimension CONTENT_PANEL_SIZE = new Dimension(1024, 900);
22 private final InstantLauncher instantLauncher;
23 private final ArrayList<Menu> menus = new ArrayList<>();
25 private final ArrayList<JPanel> panels = new ArrayList<>();
26 private JPanel contentPane;
28 public MultiLevelMenu(final InstantLauncher instantLauncher) {
29 this.instantLauncher = instantLauncher;
33 * Adds new vertical menu.
35 * @return <code>true</code> if operation succeeded, or <code>false</code>
36 * if there was not enough space left for a new menu.
38 private boolean addMenu(final File directory) {
39 final int newMenuHorizontalIndex = menus.size();
41 if (newMenuHorizontalIndex >= VERTICAL_MENUS_COUNT) {
45 final Menu menu = new Menu(directory);
46 menu.addMenuListener(this);
48 final JPanel placeholderPanel = panels.get(newMenuHorizontalIndex);
49 final JPanel menuPanel = menu.getMenuPanel();
50 placeholderPanel.add(menuPanel, BorderLayout.CENTER);
56 public JPanel buildContentPanel() {
58 contentPane = new JPanel();
59 contentPane.setBackground(Color.BLACK);
60 contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.X_AXIS));
61 Utils.setComponentSize(contentPane, CONTENT_PANEL_SIZE);
64 for (int i = 0; i < VERTICAL_MENUS_COUNT; i++) {
65 contentPane.add(Box.createHorizontalStrut(10));
67 final JPanel panel = new JPanel(new BorderLayout());
68 panel.setBackground(Color.BLACK);
70 Utils.setComponentSize(panel, Menu.SIZE_MENU_PANEL);
73 contentPane.add(panel);
76 final File directory = instantLauncher.configurationManager.getConfiguration().getNavigationRootDirectory();
82 private int getMenuIndex(final Menu menu) {
84 for (final Menu m : menus) {
94 public void menuItemHighlighted(final Menu menu, final File chosenFile) {
96 if (chosenFile.isDirectory()) {
97 final int menuIndex = getMenuIndex(menu);
100 removeMenus(menuIndex + 1);
101 if (addMenu(chosenFile)) {
102 contentPane.validate();
103 contentPane.repaint();
111 public void menuItemSelected(final File chosenFile) {
112 if (chosenFile.isFile()) {
113 if (chosenFile.canExecute()) {
114 executeCommand(chosenFile.getAbsolutePath());
115 InstantLauncher.exitProgram();
119 fileOpened = instantLauncher.openFile(chosenFile.getCanonicalFile());
121 InstantLauncher.exitProgram();
123 } catch (IOException e) {
129 if (chosenFile.isDirectory()) {
131 instantLauncher.openDirectory(chosenFile.getCanonicalFile());
132 InstantLauncher.exitProgram();
133 } catch (IOException e) {
140 public void menuItemSelectedAlternative(final File chosenFile) {
141 if (chosenFile.isFile())
142 chosenFile.setExecutable(!chosenFile.canExecute());
144 instantLauncher.openDirectoryInTerminal(chosenFile);
145 InstantLauncher.exitProgram();
149 private void removeMenus(final int fromIndex) {
151 for (int i = fromIndex; i < menus.size(); i++) {
152 final JPanel jPanel = panels.get(i);
156 while (menus.size() > fromIndex) {
157 menus.remove(fromIndex);
162 public void menuItemSelectedAlternative2(File chosenFile) {
163 // TODO: define some middle mouse click functions