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