Switched to YAML config format.
[instantlauncher.git] / src / main / java / eu / svjatoslav / instantlauncher / menu / MultiLevelMenu.java
1 package eu.svjatoslav.instantlauncher.menu;
2
3 import eu.svjatoslav.commons.gui.dialog.ExceptionDialog;
4 import eu.svjatoslav.instantlauncher.FileAssociationManager;
5 import eu.svjatoslav.instantlauncher.InstantLauncher;
6 import eu.svjatoslav.instantlauncher.Utils;
7
8 import javax.swing.*;
9 import java.awt.*;
10 import java.io.File;
11 import java.io.IOException;
12 import java.util.ArrayList;
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.configuration.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 Menu menu, final File chosenFile) {
108         if (chosenFile.isFile()) {
109             if (chosenFile.canExecute()) {
110                 executeCommand(chosenFile.getAbsolutePath());
111             } else {
112                 boolean fileOpened;
113                 try {
114                     fileOpened = FileAssociationManager.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                 executeCommand("nautilus", "-w", chosenFile.getCanonicalFile().getAbsolutePath());
127             } catch (IOException e) {
128                 e.printStackTrace();
129             }
130         }
131     }
132
133     private void executeCommand(String... c) {
134         try {
135             Runtime.getRuntime().exec(c);
136             InstantLauncher.exitProgram();
137
138         } catch (final IOException e) {
139             new ExceptionDialog(e);
140         }
141     }
142
143     @Override
144     public void menuItemSelectedAlternative(final Menu menu, final File chosenFile) {
145         if (chosenFile.isFile()) {
146             chosenFile.setExecutable(!chosenFile.canExecute());
147         } else {
148             try {
149                 executeCommand("gnome-terminal", "--working-directory="
150                         + chosenFile.getCanonicalFile().getAbsolutePath());
151             } catch (IOException e) {
152                 e.printStackTrace();
153             }
154         }
155     }
156
157     private void removeMenus(final int fromIndex) {
158
159         for (int i = fromIndex; i < menus.size(); i++) {
160             final JPanel jPanel = panels.get(i);
161             jPanel.removeAll();
162         }
163
164         while (menus.size() > fromIndex) {
165             menus.remove(fromIndex);
166         }
167     }
168
169     @Override
170     public void menuItemSelectedAlternative2(Menu menu, File chosenFile) {
171         if (chosenFile.isFile()) {
172             try {
173                 executeCommand("emacs", chosenFile.getCanonicalFile().getAbsolutePath());
174             } catch (IOException e) {
175                 e.printStackTrace();
176             }
177         }
178     }
179
180 }