Changed license to CC0
[instantlauncher.git] / src / main / java / eu / svjatoslav / instantlauncher / menu / MultiLevelMenu.java
1 /*
2  * Instantlauncher. Author: Svjatoslav Agejenko, svjatoslav@svjatoslav.eu
3  * This project is released under Creative Commons Zero (CC0) license.
4  */
5 package eu.svjatoslav.instantlauncher.menu;
6
7 import eu.svjatoslav.instantlauncher.InstantLauncher;
8 import eu.svjatoslav.instantlauncher.Utils;
9
10 import javax.swing.*;
11 import java.awt.*;
12 import java.io.File;
13 import java.io.IOException;
14 import java.util.ArrayList;
15
16 import static eu.svjatoslav.instantlauncher.Utils.executeCommand;
17
18 public class MultiLevelMenu implements MenuListener {
19
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<>();
24
25     private final ArrayList<JPanel> panels = new ArrayList<>();
26     private JPanel contentPane;
27
28     public MultiLevelMenu(final InstantLauncher instantLauncher) {
29         this.instantLauncher = instantLauncher;
30     }
31
32     /**
33      * Adds new vertical menu.
34      *
35      * @return <code>true</code> if operation succeeded, or <code>false</code>
36      * if there was not enough space left for a new menu.
37      */
38     private boolean addMenu(final File directory) {
39         final int newMenuHorizontalIndex = menus.size();
40
41         if (newMenuHorizontalIndex >= VERTICAL_MENUS_COUNT) {
42             return false;
43         }
44
45         final Menu menu = new Menu(directory);
46         menu.addMenuListener(this);
47
48         final JPanel placeholderPanel = panels.get(newMenuHorizontalIndex);
49         final JPanel menuPanel = menu.getMenuPanel();
50         placeholderPanel.add(menuPanel, BorderLayout.CENTER);
51
52         menus.add(menu);
53         return true;
54     }
55
56     public JPanel buildContentPanel() {
57
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);
62
63         // initialize panels
64         for (int i = 0; i < VERTICAL_MENUS_COUNT; i++) {
65             contentPane.add(Box.createHorizontalStrut(10));
66
67             final JPanel panel = new JPanel(new BorderLayout());
68             panel.setBackground(Color.BLACK);
69
70             Utils.setComponentSize(panel, Menu.SIZE_MENU_PANEL);
71
72             panels.add(panel);
73             contentPane.add(panel);
74         }
75
76         final File directory = instantLauncher.configurationManager.getConfiguration().getNavigationRootDirectory();
77         addMenu(directory);
78
79         return contentPane;
80     }
81
82     private int getMenuIndex(final Menu menu) {
83         int i = 0;
84         for (final Menu m : menus) {
85             if (m == menu) {
86                 return i;
87             }
88             i++;
89         }
90         return -1;
91     }
92
93     @Override
94     public void menuItemHighlighted(final Menu menu, final File chosenFile) {
95
96         if (chosenFile.isDirectory()) {
97             final int menuIndex = getMenuIndex(menu);
98
99             if (menuIndex >= 0) {
100                 removeMenus(menuIndex + 1);
101                 if (addMenu(chosenFile)) {
102                     contentPane.validate();
103                     contentPane.repaint();
104                 }
105             }
106         }
107
108     }
109
110     @Override
111     public void menuItemSelected(final File chosenFile) {
112         if (chosenFile.isFile()) {
113             if (chosenFile.canExecute()) {
114                 executeCommand(chosenFile.getAbsolutePath());
115                 InstantLauncher.exitProgram();
116             } else {
117                 boolean fileOpened;
118                 try {
119                     fileOpened = instantLauncher.openFile(chosenFile.getCanonicalFile());
120                     if (fileOpened)
121                         InstantLauncher.exitProgram();
122
123                 } catch (IOException e) {
124                     e.printStackTrace();
125                 }
126             }
127         }
128
129         if (chosenFile.isDirectory()) {
130             try {
131                 instantLauncher.openDirectory(chosenFile.getCanonicalFile());
132                 InstantLauncher.exitProgram();
133             } catch (IOException e) {
134                 e.printStackTrace();
135             }
136         }
137     }
138
139     @Override
140     public void menuItemSelectedAlternative(final File chosenFile) {
141         if (chosenFile.isFile())
142             chosenFile.setExecutable(!chosenFile.canExecute());
143         else {
144             instantLauncher.openDirectoryInTerminal(chosenFile);
145             InstantLauncher.exitProgram();
146         }
147     }
148
149     private void removeMenus(final int fromIndex) {
150
151         for (int i = fromIndex; i < menus.size(); i++) {
152             final JPanel jPanel = panels.get(i);
153             jPanel.removeAll();
154         }
155
156         while (menus.size() > fromIndex) {
157             menus.remove(fromIndex);
158         }
159     }
160
161     @Override
162     public void menuItemSelectedAlternative2(File chosenFile) {
163         // TODO: define some middle mouse click functions
164     }
165
166
167 }