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