package eu.svjatoslav.instantlauncher.menu;
-import java.awt.BorderLayout;
-import java.awt.Color;
-import java.awt.Dimension;
-import java.io.File;
-import java.io.IOException;
-import java.util.ArrayList;
-
-import javax.swing.Box;
-import javax.swing.BoxLayout;
-import javax.swing.JPanel;
-
import eu.svjatoslav.commons.gui.dialog.ExceptionDialog;
-import eu.svjatoslav.instantlauncher.FileAssociationManager;
import eu.svjatoslav.instantlauncher.InstantLauncher;
import eu.svjatoslav.instantlauncher.Utils;
-public class MultiLevelMenu implements MenuListener {
+import javax.swing.*;
+import java.awt.*;
+import java.io.File;
+import java.io.IOException;
+import java.util.ArrayList;
- private final InstantLauncher instantLauncher;
+public class MultiLevelMenu implements MenuListener {
private static final int VERTICAL_MENUS_COUNT = 7;
+ private static final Dimension CONTENT_PANEL_SIZE = new Dimension(1024, 900);
+ private final InstantLauncher instantLauncher;
+ private final ArrayList<Menu> menus = new ArrayList<>();
- public static final Dimension CONTENT_PANEL_SIZE = new Dimension(1024, 900);
-
- ArrayList<Menu> menus = new ArrayList<Menu>();
-
- ArrayList<JPanel> panels = new ArrayList<JPanel>();
- JPanel contentPane;
+ private final ArrayList<JPanel> panels = new ArrayList<>();
+ private JPanel contentPane;
public MultiLevelMenu(final InstantLauncher instantLauncher) {
this.instantLauncher = instantLauncher;
/**
* Adds new vertical menu.
- *
+ *
* @return <code>true</code> if operation succeeded, or <code>false</code>
- * if there was not enough space left for a new menu.
+ * if there was not enough space left for a new menu.
*/
- public boolean addMenu(final File directory) {
+ private boolean addMenu(final File directory) {
final int newMenuHorizontalIndex = menus.size();
- if (newMenuHorizontalIndex >= VERTICAL_MENUS_COUNT)
+ if (newMenuHorizontalIndex >= VERTICAL_MENUS_COUNT) {
return false;
+ }
final Menu menu = new Menu(directory);
menu.addMenuListener(this);
contentPane.add(panel);
}
- final File directory = instantLauncher.configuration.getRootDirectory();
+ final File directory = instantLauncher.configurationManager.getNavigationRootDirectory();
addMenu(directory);
return contentPane;
}
- public int getMenuIndex(final Menu menu) {
+ private int getMenuIndex(final Menu menu) {
int i = 0;
for (final Menu m : menus) {
- if (m == menu)
+ if (m == menu) {
return i;
+ }
i++;
}
return -1;
@Override
public void menuItemSelected(final Menu menu, final File chosenFile) {
- if (chosenFile.isFile())
- if (chosenFile.canExecute())
+ if (chosenFile.isFile()) {
+ if (chosenFile.canExecute()) {
+ executeCommand(chosenFile.getAbsolutePath());
+ } else {
+ boolean fileOpened;
try {
- // Execute a command without arguments
- final String command = chosenFile.getAbsolutePath();
-
- final String[] c = new String[1];
- c[0] = command;
-
- Runtime.getRuntime().exec(c);
- InstantLauncher.exitProgram();
- } catch (final IOException e) {
- new ExceptionDialog(e);
+ fileOpened = instantLauncher.openFile(chosenFile.getCanonicalFile());
+ if (fileOpened) {
+ InstantLauncher.exitProgram();
+ }
+ } catch (IOException e) {
+ e.printStackTrace();
}
- else {
- final boolean fileOpened = FileAssociationManager.openFile(chosenFile);
- if (fileOpened)
- InstantLauncher.exitProgram();
}
+ }
if (chosenFile.isDirectory()) {
- // Execute a command without arguments
- final String directoryPath = chosenFile.getAbsolutePath();
-
- final String[] c = new String[2];
- c[0] = "nautilus";
- c[1] = directoryPath;
-
try {
- Runtime.getRuntime().exec(c);
- InstantLauncher.exitProgram();
-
- } catch (final IOException e) {
- new ExceptionDialog(e);
+ executeCommand("nautilus", "-w", chosenFile.getCanonicalFile().getAbsolutePath());
+ } catch (IOException e) {
+ e.printStackTrace();
}
}
}
+ private void executeCommand(String... c) {
+ try {
+ Runtime.getRuntime().exec(c);
+ InstantLauncher.exitProgram();
+
+ } catch (final IOException e) {
+ new ExceptionDialog(e);
+ }
+ }
+
@Override
public void menuItemSelectedAlternative(final Menu menu, final File chosenFile) {
- if (chosenFile.isFile())
+ if (chosenFile.isFile()) {
chosenFile.setExecutable(!chosenFile.canExecute());
- else {
- // Execute a command without arguments
- final String[] c = new String[2];
-
- c[0] = "gnome-terminal";
- c[1] = "--working-directory=" + chosenFile.getAbsolutePath();
-
+ } else {
try {
- Runtime.getRuntime().exec(c);
- InstantLauncher.exitProgram();
-
- } catch (final IOException e) {
- new ExceptionDialog(e);
+ executeCommand("gnome-terminal", "--working-directory="
+ + chosenFile.getCanonicalFile().getAbsolutePath());
+ } catch (IOException e) {
+ e.printStackTrace();
}
}
}
- public void removeMenus(final int fromIndex) {
+ private void removeMenus(final int fromIndex) {
for (int i = fromIndex; i < menus.size(); i++) {
final JPanel jPanel = panels.get(i);
jPanel.removeAll();
}
- while (menus.size() > fromIndex)
+ while (menus.size() > fromIndex) {
menus.remove(fromIndex);
+ }
}
+ @Override
+ public void menuItemSelectedAlternative2(Menu menu, File chosenFile) {
+ if (chosenFile.isFile()) {
+ try {
+ executeCommand("emacs", chosenFile.getCanonicalFile().getAbsolutePath());
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ }
+ }
+
+
}