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