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