public class Menu {
+ public static class ButtonMouseListener implements MouseListener {
+
+ JButton parentButton;
+ Menu parentMenu;
+ File file;
+
+ public ButtonMouseListener(final JButton button, final Menu parentMenu, final File file) {
+ parentButton = button;
+ this.parentMenu = parentMenu;
+ this.file = file;
+ }
+
+ @Override
+ public void mouseClicked(final MouseEvent e) {
+ try {
+
+ if (e.getButton() == 1)
+ parentMenu.menuItemSelected(file);
+ if (e.getButton() == 3)
+ parentMenu.menuItemSelectedAlternative(file);
+ } catch (final Exception ex) {
+ new ExceptionDialog(ex);
+ }
+ }
+
+ @Override
+ public void mouseEntered(final MouseEvent e) {
+ try {
+ if (parentButton != parentMenu.getSelectedButton()) {
+
+ if (parentMenu.getSelectedButton() != null)
+ parentMenu.getSelectedButton().setBackground(getFileColor(parentMenu.getSelectedFile(), false));
+
+ parentMenu.setSelected(parentButton, file);
+ parentButton.setBackground(getFileColor(file, true));
+
+ parentMenu.menuItemHighlighted(file);
+ }
+ } catch (final Exception ex) {
+ new ExceptionDialog(ex);
+ }
+ }
+
+ @Override
+ public void mouseExited(final MouseEvent e) {
+ }
+
+ @Override
+ public void mousePressed(final MouseEvent e) {
+ }
+
+ @Override
+ public void mouseReleased(final MouseEvent e) {
+ }
+
+ }
+
public static final Color BUTTON_BG_DIRECTORY = new Color(0, 0, 60);
- public static final Color BUTTON_BG_DIRECTORY_SELECTED = new Color(0, 0, 100);
+ public static final Color BUTTON_BG_DIRECTORY_SELECTED = new Color(0, 0, 100);
public static final Color BUTTON_BG_LINK = new Color(60, 60, 0);
- public static final Color BUTTON_BG_LINK_SELECTED = new Color(100, 100, 0);
+ public static final Color BUTTON_BG_LINK_SELECTED = new Color(100, 100, 0);
public static final Color BUTTON_BG_EXECUTABLE_FILE = new Color(60, 0, 0);
- public static final Color BUTTON_BG_EXECUTABLE_FILE_SELECTED = new Color(100, 0, 0);
+ public static final Color BUTTON_BG_EXECUTABLE_FILE_SELECTED = new Color(100, 0, 0);
public static final Color BUTTON_BG_NONEXECUTABLE_FILE = new Color(0, 60, 0);
- public static final Color BUTTON_BG_NONEXECUTABLE_FILE_SELECTED = new Color(0, 100, 0);
+ public static final Color BUTTON_BG_NONEXECUTABLE_FILE_SELECTED = new Color(0, 100, 0);
public static final Dimension SIZE_BUTTON = new Dimension(200, 30);
+
public static final Dimension SIZE_MENU_PANEL = new Dimension(210, 900);
+ /**
+ * <pre>
+ * Assign specific colors to:
+ * Directory
+ * Executable file
+ * Non executable file
+ * </pre>
+ */
+ public static Color getFileColor(final File file, final boolean selected) {
+ if (file.isDirectory())
+ if (selected)
+ return BUTTON_BG_DIRECTORY_SELECTED;
+ else
+ return BUTTON_BG_DIRECTORY;
+
+ if (file.isFile())
+ if (file.canExecute()) {
+ if (selected)
+ return BUTTON_BG_EXECUTABLE_FILE_SELECTED;
+ else
+ return BUTTON_BG_EXECUTABLE_FILE;
+ } else if (selected)
+ return BUTTON_BG_NONEXECUTABLE_FILE_SELECTED;
+ else
+ return BUTTON_BG_NONEXECUTABLE_FILE;
+
+ if (selected)
+ return BUTTON_BG_LINK_SELECTED;
+ else
+ return BUTTON_BG_LINK;
+ }
+
private JPanel menuPanel;
public JButton selectedButton;
- public File selectedFile;
+
+ public File selectedFile;;
private final Set<MenuListener> menuListeners = new HashSet<MenuListener>();
public Menu(final File directory) {
buildContentPanel(directory);
- };
+ }
public void addMenuListener(final MenuListener menuListener) {
menuListeners.add(menuListener);
public JButton buildButton(final File file) {
JButton button;
- if (file.isDirectory()) {
+ if (file.isDirectory())
button = new JButton("[ " + file.getName() + " ]");
- } else {
+ else
button = new JButton(file.getName());
- }
button.setBackground(getFileColor(file, false));
button.setForeground(new Color(255, 255, 255));
public int compare(final File f1, final File f2) {
// directories come before files
- if (f1.isFile() && f2.isDirectory()) {
+ if (f1.isFile() && f2.isDirectory())
return 1;
- }
- if (f2.isFile() && f1.isDirectory()) {
+ if (f2.isFile() && f1.isDirectory())
return -1;
- }
// order by file name
return f1.getName().toUpperCase().compareTo(f2.getName().toUpperCase());
});
// add files to the panel
- for (final File file : listFiles) {
+ for (final File file : listFiles)
// ignore hidden files
if (!file.getName().startsWith(".")) {
menuPanel.add(Box.createVerticalStrut(10));
menuPanel.add(buildButton(file));
}
- }
}
public JPanel getMenuPanel() {
return selectedFile;
}
- public void notifyAlternativeEventListeners(final File chosenFile) {
- for (final MenuListener menuListener : menuListeners) {
- menuListener.menuItemSelectedAlternative(this, chosenFile);
- }
+ public void menuItemHighlighted(final File chosenFile) {
+ for (final MenuListener menuListener : menuListeners)
+ menuListener.menuItemHighlighted(this, chosenFile);
}
- public void notifyEventListeners(final File chosenFile) {
- for (final MenuListener menuListener : menuListeners) {
+ public void menuItemSelected(final File chosenFile) {
+ for (final MenuListener menuListener : menuListeners)
menuListener.menuItemSelected(this, chosenFile);
- }
+ }
+
+ public void menuItemSelectedAlternative(final File chosenFile) {
+ for (final MenuListener menuListener : menuListeners)
+ menuListener.menuItemSelectedAlternative(this, chosenFile);
}
public void setSelected(final JButton selectedButton, final File selectedFile) {
this.selectedFile = selectedFile;
}
- /**
- * <pre>
- * Assign specific colors to:
- * Directory
- * Executable file
- * Non executable file
- * </pre>
- */
- public static Color getFileColor(final File file, final boolean selected) {
- if (file.isDirectory()) {
- if (selected) {
- return BUTTON_BG_DIRECTORY_SELECTED;
- } else {
- return BUTTON_BG_DIRECTORY;
- }
- }
-
- if (file.isFile()) {
- if (file.canExecute()) {
- if (selected) {
- return BUTTON_BG_EXECUTABLE_FILE_SELECTED;
- } else {
- return BUTTON_BG_EXECUTABLE_FILE;
- }
- } else {
- if (selected) {
- return BUTTON_BG_NONEXECUTABLE_FILE_SELECTED;
- } else {
- return BUTTON_BG_NONEXECUTABLE_FILE;
- }
- }
- }
-
- if (selected) {
- return BUTTON_BG_LINK_SELECTED;
- } else {
- return BUTTON_BG_LINK;
- }
- }
-
- public static class ButtonMouseListener implements MouseListener {
-
- JButton parentButton;
- Menu parentMenu;
- File file;
-
- public ButtonMouseListener(final JButton button, final Menu parentMenu, final File file) {
- parentButton = button;
- this.parentMenu = parentMenu;
- this.file = file;
- }
-
- @Override
- public void mouseClicked(final MouseEvent e) {
- try {
-
- if (e.getButton() == 1) {
- parentMenu.notifyEventListeners(file);
- }
- if (e.getButton() == 3) {
- parentMenu.notifyAlternativeEventListeners(file);
- }
- } catch (final Exception ex) {
- new ExceptionDialog(ex);
- }
- }
-
- @Override
- public void mouseEntered(final MouseEvent e) {
- try {
- if (parentButton != parentMenu.getSelectedButton()) {
-
- if (parentMenu.getSelectedButton() != null) {
- parentMenu.getSelectedButton().setBackground(getFileColor(parentMenu.getSelectedFile(), false));
- }
-
- parentMenu.setSelected(parentButton, file);
- parentButton.setBackground(getFileColor(file, true));
-
- if (file.isDirectory()) {
- parentMenu.notifyEventListeners(file);
- }
- }
- } catch (final Exception ex) {
- new ExceptionDialog(ex);
- }
- }
-
- @Override
- public void mouseExited(final MouseEvent e) {
- }
-
- @Override
- public void mousePressed(final MouseEvent e) {
- }
-
- @Override
- public void mouseReleased(final MouseEvent e) {
- }
-
- }
-
}
\ No newline at end of file
}
@Override
- public void menuItemSelected(final Menu menu, final File chosenFile) {
+ public void menuItemHighlighted(final Menu menu, final File chosenFile) {
if (chosenFile.isDirectory()) {
final int menuIndex = getMenuIndex(menu);
}
}
- if (chosenFile.isFile()) {
+ }
- if (chosenFile.canExecute()) {
+ @Override
+ public void menuItemSelected(final Menu menu, final File chosenFile) {
+ if (chosenFile.isFile())
+ if (chosenFile.canExecute())
try {
// Execute a command without arguments
final String command = chosenFile.getAbsolutePath();
} catch (final IOException e) {
new ExceptionDialog(e);
}
-
- } else {
+ else {
final boolean fileOpened = FileAssociationManager.openFile(chosenFile);
- if (fileOpened) {
+ if (fileOpened)
InstantLauncher.exitProgram();
- }
}
- }
- }
- @Override
- public void menuItemSelectedAlternative(final Menu menu, final File chosenFile) {
if (chosenFile.isDirectory()) {
// Execute a command without arguments
final String directoryPath = chosenFile.getAbsolutePath();
} catch (final IOException e) {
new ExceptionDialog(e);
}
- } else {
+ }
+ }
+
+ @Override
+ public void menuItemSelectedAlternative(final Menu menu, final File chosenFile) {
+ 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();
+
+ try {
+ Runtime.getRuntime().exec(c);
+ InstantLauncher.exitProgram();
+
+ } catch (final IOException e) {
+ new ExceptionDialog(e);
+ }
}
}
jPanel.removeAll();
}
- while (menus.size() > fromIndex) {
+ while (menus.size() > fromIndex)
menus.remove(fromIndex);
- }
}
}