From: Svjatoslav Agejenko Date: Mon, 1 Aug 2016 19:43:34 +0000 (+0300) Subject: code cleanup X-Git-Url: http://www2.svjatoslav.eu/gitweb/?a=commitdiff_plain;h=8e31839bb326186944811ac3e54a3037ffa75106;p=instantlauncher.git code cleanup --- diff --git a/instantlauncher.iml b/instantlauncher.iml index 20c81c0..df9054d 100644 --- a/instantlauncher.iml +++ b/instantlauncher.iml @@ -1,6 +1,6 @@ - + @@ -10,7 +10,7 @@ - + \ No newline at end of file diff --git a/pom.xml b/pom.xml index 96479f5..89ee842 100644 --- a/pom.xml +++ b/pom.xml @@ -1,7 +1,7 @@ + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xmlns="http://maven.apache.org/POM/4.0.0" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 eu.svjatoslav instantlauncher @@ -14,7 +14,7 @@ eu.svjatoslav svjatoslavcommons - 1.0 + 1.4 @@ -31,8 +31,8 @@ maven-compiler-plugin 2.3.1 - 1.6 - 1.6 + 1.8 + 1.8 true UTF-8 diff --git a/run.sh b/run.sh deleted file mode 100755 index 1726ba2..0000000 --- a/run.sh +++ /dev/null @@ -1,5 +0,0 @@ -cd ~/workspace/instantlauncher - -# For faster subsequent startup we use offline mode -mvn -o exec:java -Dexec.mainClass="eu.svjatoslav.instantlauncher.Main" - diff --git a/src/main/java/eu/svjatoslav/instantlauncher/CommandlineHandler.java b/src/main/java/eu/svjatoslav/instantlauncher/CommandlineHandler.java deleted file mode 100755 index 2b71669..0000000 --- a/src/main/java/eu/svjatoslav/instantlauncher/CommandlineHandler.java +++ /dev/null @@ -1,28 +0,0 @@ -package eu.svjatoslav.instantlauncher; - -import eu.svjatoslav.commons.commandline.parameterparser.Parameter; -import eu.svjatoslav.commons.commandline.parameterparser.Parser; - -public class CommandlineHandler { - - Parameter helpParameter = new Parameter("Show commandline optons help", "-h", "--help"); - private final Parser parser; - - public CommandlineHandler() { - - parser = new Parser(); - parser.addParameter(helpParameter); - - } - - public void parseCommandlineArguments(final String[] args) { - - if (!parser.parse(args)) - return; - - if (helpParameter.isParameterSpecified()) { - parser.showHelp(); - System.exit(0); - } - } -} diff --git a/src/main/java/eu/svjatoslav/instantlauncher/Configuration.java b/src/main/java/eu/svjatoslav/instantlauncher/Configuration.java index 28f2f82..94f9de2 100755 --- a/src/main/java/eu/svjatoslav/instantlauncher/Configuration.java +++ b/src/main/java/eu/svjatoslav/instantlauncher/Configuration.java @@ -2,7 +2,6 @@ package eu.svjatoslav.instantlauncher; import java.io.File; import java.io.FileInputStream; -import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.util.Properties; @@ -11,15 +10,15 @@ public class Configuration { private static final String KEY_ROOT_DIRECTORY = "KEY_ROOT_DIRECTORY"; private static final String CONFIG_FILE_NAME = ".instantlauncher"; - Properties properties = new Properties(); + private final Properties properties = new Properties(); - boolean propertiesChanged = false; + private boolean propertiesChanged = false; - public Configuration() throws FileNotFoundException, IOException { + public Configuration() throws IOException { initialize(); } - public File getPropertiesFile() { + private File getPropertiesFile() { return new File(System.getProperty("user.home") + "/" + CONFIG_FILE_NAME); } @@ -35,7 +34,7 @@ public class Configuration { } - public void initialize() throws FileNotFoundException, IOException { + private void initialize() throws IOException { loadIfFileExists(); @@ -46,7 +45,7 @@ public class Configuration { } } - public void loadIfFileExists() throws FileNotFoundException, IOException { + private void loadIfFileExists() throws IOException { final File propertiesFile = getPropertiesFile(); if (propertiesFile.exists()) { @@ -56,11 +55,11 @@ public class Configuration { } } - public void saveFile() throws FileNotFoundException, IOException { + private void saveFile() throws IOException { properties.store(new FileOutputStream(getPropertiesFile()), "Instantlauncher configuration file."); } - public void validatePropertiesFile() { + private void validatePropertiesFile() { getRootDirectory(); } diff --git a/src/main/java/eu/svjatoslav/instantlauncher/FileAssociationManager.java b/src/main/java/eu/svjatoslav/instantlauncher/FileAssociationManager.java index 51d900f..a4323ce 100755 --- a/src/main/java/eu/svjatoslav/instantlauncher/FileAssociationManager.java +++ b/src/main/java/eu/svjatoslav/instantlauncher/FileAssociationManager.java @@ -1,118 +1,116 @@ package eu.svjatoslav.instantlauncher; +import eu.svjatoslav.commons.gui.dialog.ExceptionDialog; + import java.io.File; import java.io.IOException; import java.util.HashMap; -import eu.svjatoslav.commons.gui.dialog.ExceptionDialog; - public class FileAssociationManager { - private static final String FILE_INDICATOR = ""; + private static final String FILE_INDICATOR = ""; - private static final String[] TEXT_EDITOR_PATTERN = new String[] { "emc", FILE_INDICATOR }; + private static final String[] TEXT_EDITOR_PATTERN = new String[]{"emc", FILE_INDICATOR}; - private static final String DOCUMENT_EDITOR = "lowriter"; + private static final String DOCUMENT_EDITOR = "lowriter"; - private static final String SPREADSHEET_EDITOR = "localc"; + private static final String SPREADSHEET_EDITOR = "localc"; - private static final String MUSIC_PLAYER = "clementine"; + private static final String MUSIC_PLAYER = "clementine"; - public static HashMap extensionToApplication = new HashMap(); + private static final HashMap extensionToApplication = new HashMap<>(); - private static boolean isLogFile(final String fileName) { - if (fileName.endsWith(".out")) - return true; - if (fileName.endsWith(".log")) - return true; - if (fileName.contains(".log.")) - return true; + public FileAssociationManager() { - return false; - } + extensionToApplication.put("txt", TEXT_EDITOR_PATTERN); + extensionToApplication.put("xml", TEXT_EDITOR_PATTERN); + extensionToApplication.put("org", TEXT_EDITOR_PATTERN); + extensionToApplication.put("properties", TEXT_EDITOR_PATTERN); + extensionToApplication.put("ini", TEXT_EDITOR_PATTERN); + extensionToApplication.put("", TEXT_EDITOR_PATTERN); - /** - * @return true if file was opened. false if - * unknown file type. - */ - public static boolean openFile(final File file) { + extensionToApplication.put("jpg", new String[]{"eog", FILE_INDICATOR}); + extensionToApplication.put("jpeg", new String[]{"eog", FILE_INDICATOR}); + extensionToApplication.put("png", new String[]{"eog", FILE_INDICATOR}); - if (isLogFile(file.getName())) { - runOpeningApplication(file, new String[] { "glogg", FILE_INDICATOR }); - return true; - } + extensionToApplication.put("pdf", new String[]{"evince", FILE_INDICATOR}); - final String fileExtension = Utils.getFileExtension(file); + extensionToApplication.put("mp3", new String[]{"banshee", FILE_INDICATOR}); + extensionToApplication.put("ogg", new String[]{"banshee", FILE_INDICATOR}); - if (extensionToApplication.containsKey(fileExtension)) { - final String[] commands = extensionToApplication.get(fileExtension); - runOpeningApplication(file, commands); - return true; - } else - return false; + extensionToApplication.put("avi", new String[]{"vlc", FILE_INDICATOR}); + extensionToApplication.put("mkv", new String[]{"vlc", FILE_INDICATOR}); + extensionToApplication.put("ogv", new String[]{"vlc", FILE_INDICATOR}); + extensionToApplication.put("mp4", new String[]{"vlc", FILE_INDICATOR}); + extensionToApplication.put("flv", new String[]{"vlc", FILE_INDICATOR}); + extensionToApplication.put("wmv", new String[]{"vlc", FILE_INDICATOR}); - } + extensionToApplication.put("flac", new String[]{MUSIC_PLAYER, FILE_INDICATOR}); + extensionToApplication.put("mp3", new String[]{MUSIC_PLAYER, FILE_INDICATOR}); + extensionToApplication.put("ogg", new String[]{MUSIC_PLAYER, FILE_INDICATOR}); + extensionToApplication.put("wav", new String[]{MUSIC_PLAYER, FILE_INDICATOR}); - private static void runOpeningApplication(final File file, final String[] commands) { - for (int i = 0; i < commands.length; i++) - if (commands[i].equals(FILE_INDICATOR)) - commands[i] = file.getAbsolutePath(); + extensionToApplication.put("ods", new String[]{SPREADSHEET_EDITOR, FILE_INDICATOR}); + extensionToApplication.put("xls", new String[]{SPREADSHEET_EDITOR, FILE_INDICATOR}); + extensionToApplication.put("xlsx", new String[]{SPREADSHEET_EDITOR, FILE_INDICATOR}); - try { - Runtime.getRuntime().exec(commands); - InstantLauncher.exitProgram(); + extensionToApplication.put("docx", new String[]{DOCUMENT_EDITOR, FILE_INDICATOR}); + extensionToApplication.put("doc", new String[]{DOCUMENT_EDITOR, FILE_INDICATOR}); - } catch (final IOException e) { - new ExceptionDialog(e); - } - } + extensionToApplication.put("desktop", new String[]{"run-desktop", FILE_INDICATOR}); - public FileAssociationManager() { + extensionToApplication.put("log", new String[]{"glogg", FILE_INDICATOR}); + extensionToApplication.put("out", new String[]{"glogg", FILE_INDICATOR}); - extensionToApplication.put("txt", TEXT_EDITOR_PATTERN); - extensionToApplication.put("xml", TEXT_EDITOR_PATTERN); - extensionToApplication.put("org", TEXT_EDITOR_PATTERN); - extensionToApplication.put("properties", TEXT_EDITOR_PATTERN); - extensionToApplication.put("ini", TEXT_EDITOR_PATTERN); - extensionToApplication.put("", TEXT_EDITOR_PATTERN); + extensionToApplication.put("html", new String[]{"firefox", FILE_INDICATOR}); - extensionToApplication.put("jpg", new String[] { "eog", FILE_INDICATOR }); - extensionToApplication.put("jpeg", new String[] { "eog", FILE_INDICATOR }); - extensionToApplication.put("png", new String[] { "eog", FILE_INDICATOR }); + extensionToApplication.put("htm", new String[]{"firefox", FILE_INDICATOR}); - extensionToApplication.put("pdf", new String[] { "evince", FILE_INDICATOR }); + } - extensionToApplication.put("mp3", new String[] { "banshee", FILE_INDICATOR }); - extensionToApplication.put("ogg", new String[] { "banshee", FILE_INDICATOR }); + private static boolean isLogFile(final String fileName) { + if (fileName.endsWith(".out")) + return true; + if (fileName.endsWith(".log")) + return true; + return fileName.contains(".log."); - extensionToApplication.put("avi", new String[] { "vlc", FILE_INDICATOR }); - extensionToApplication.put("mkv", new String[] { "vlc", FILE_INDICATOR }); - extensionToApplication.put("ogv", new String[] { "vlc", FILE_INDICATOR }); - extensionToApplication.put("mp4", new String[] { "vlc", FILE_INDICATOR }); - extensionToApplication.put("flv", new String[] { "vlc", FILE_INDICATOR }); - extensionToApplication.put("wmv", new String[] { "vlc", FILE_INDICATOR }); + } - extensionToApplication.put("flac", new String[] { MUSIC_PLAYER, FILE_INDICATOR }); - extensionToApplication.put("mp3", new String[] { MUSIC_PLAYER, FILE_INDICATOR }); - extensionToApplication.put("ogg", new String[] { MUSIC_PLAYER, FILE_INDICATOR }); - extensionToApplication.put("wav", new String[] { MUSIC_PLAYER, FILE_INDICATOR }); + /** + * @return true if file was opened. false if + * unknown file type. + */ + public static boolean openFile(final File file) { - extensionToApplication.put("ods", new String[] { SPREADSHEET_EDITOR, FILE_INDICATOR }); - extensionToApplication.put("xls", new String[] { SPREADSHEET_EDITOR, FILE_INDICATOR }); - extensionToApplication.put("xlsx", new String[] { SPREADSHEET_EDITOR, FILE_INDICATOR }); + if (isLogFile(file.getName())) { + runOpeningApplication(file, new String[]{"glogg", FILE_INDICATOR}); + return true; + } - extensionToApplication.put("docx", new String[] { DOCUMENT_EDITOR, FILE_INDICATOR }); - extensionToApplication.put("doc", new String[] { DOCUMENT_EDITOR, FILE_INDICATOR }); + final String fileExtension = Utils.getFileExtension(file); - extensionToApplication.put("desktop", new String[] { "run-desktop", FILE_INDICATOR }); + if (extensionToApplication.containsKey(fileExtension)) { + final String[] commands = extensionToApplication.get(fileExtension); + runOpeningApplication(file, commands); + return true; + } else + return false; - extensionToApplication.put("log", new String[] { "glogg", FILE_INDICATOR }); - extensionToApplication.put("out", new String[] { "glogg", FILE_INDICATOR }); + } - extensionToApplication.put("html", new String[] { "firefox", FILE_INDICATOR }); + private static void runOpeningApplication(final File file, final String[] commands) { + for (int i = 0; i < commands.length; i++) + if (commands[i].equals(FILE_INDICATOR)) + commands[i] = file.getAbsolutePath(); - extensionToApplication.put("htm", new String[] { "firefox", FILE_INDICATOR }); + try { + Runtime.getRuntime().exec(commands); + InstantLauncher.exitProgram(); - } + } catch (final IOException e) { + new ExceptionDialog(e); + } + } } diff --git a/src/main/java/eu/svjatoslav/instantlauncher/InstantLauncher.java b/src/main/java/eu/svjatoslav/instantlauncher/InstantLauncher.java index b13b585..c68c70e 100755 --- a/src/main/java/eu/svjatoslav/instantlauncher/InstantLauncher.java +++ b/src/main/java/eu/svjatoslav/instantlauncher/InstantLauncher.java @@ -1,28 +1,25 @@ package eu.svjatoslav.instantlauncher; -import java.io.FileNotFoundException; -import java.io.IOException; - import org.apache.log4j.Logger; +import java.io.IOException; + public class InstantLauncher { + private static final Logger LOGGER = Logger.getLogger(InstantLauncher.class); public Configuration configuration; - FileAssociationManager associationManager = new FileAssociationManager(); - public static final Logger LOGGER = Logger.getLogger(InstantLauncher.class); + public static void exitProgram() { + LOGGER.info("Closing InstantLauncher"); + System.exit(0); + } - public void run() throws FileNotFoundException, IOException { + public void run() throws IOException { configuration = new Configuration(); final MainFrame mainFrame = new MainFrame(this); mainFrame.show(); } - public static void exitProgram() { - LOGGER.info("Closing InstantLauncher"); - System.exit(0); - } - } diff --git a/src/main/java/eu/svjatoslav/instantlauncher/Main.java b/src/main/java/eu/svjatoslav/instantlauncher/Main.java index 47f1cba..e743390 100755 --- a/src/main/java/eu/svjatoslav/instantlauncher/Main.java +++ b/src/main/java/eu/svjatoslav/instantlauncher/Main.java @@ -2,17 +2,12 @@ package eu.svjatoslav.instantlauncher; import eu.svjatoslav.commons.gui.dialog.ExceptionDialog; -public class Main { +class Main { public static void main(final String[] args) { try { - - final CommandlineHandler commandlineHandler = new CommandlineHandler(); - commandlineHandler.parseCommandlineArguments(args); - - final InstantLauncher instantLauncher = new InstantLauncher(); - instantLauncher.run(); + new InstantLauncher().run(); } catch (final Exception e) { new ExceptionDialog(e); } diff --git a/src/main/java/eu/svjatoslav/instantlauncher/MainFrame.java b/src/main/java/eu/svjatoslav/instantlauncher/MainFrame.java index cdd351c..3ec2966 100755 --- a/src/main/java/eu/svjatoslav/instantlauncher/MainFrame.java +++ b/src/main/java/eu/svjatoslav/instantlauncher/MainFrame.java @@ -1,23 +1,18 @@ package eu.svjatoslav.instantlauncher; -import java.awt.BorderLayout; -import java.awt.Dimension; -import java.awt.event.WindowEvent; -import java.awt.event.WindowListener; - -import javax.swing.JFrame; - import eu.svjatoslav.instantlauncher.menu.MultiLevelMenu; -public class MainFrame { - - private JFrame frame; +import javax.swing.*; +import java.awt.*; +import java.awt.event.WindowEvent; +import java.awt.event.WindowListener; - private boolean isShowing; +class MainFrame { public static final Dimension CONTENT_PANEL_SIZE = new Dimension(1100, 850); - private final InstantLauncher instantLauncher; + private JFrame frame; + private boolean isShowing; public MainFrame(final InstantLauncher instantLauncher) { this.instantLauncher = instantLauncher; @@ -51,7 +46,7 @@ public class MainFrame { } - public static class FrameWindowListener implements WindowListener { + private static class FrameWindowListener implements WindowListener { @Override public void windowActivated(final WindowEvent e) { diff --git a/src/main/java/eu/svjatoslav/instantlauncher/Utils.java b/src/main/java/eu/svjatoslav/instantlauncher/Utils.java index b82f623..f382e7c 100755 --- a/src/main/java/eu/svjatoslav/instantlauncher/Utils.java +++ b/src/main/java/eu/svjatoslav/instantlauncher/Utils.java @@ -1,10 +1,9 @@ package eu.svjatoslav.instantlauncher; -import java.awt.Dimension; +import javax.swing.*; +import java.awt.*; import java.io.File; -import javax.swing.JComponent; - public class Utils { public static String getFileExtension(File file) { @@ -23,7 +22,7 @@ public class Utils { return fileExtension; } - public static final void setComponentSize(JComponent component, Dimension size) { + public static void setComponentSize(JComponent component, Dimension size) { component.setMinimumSize(size); component.setMaximumSize(size); component.setSize(size); diff --git a/src/main/java/eu/svjatoslav/instantlauncher/menu/Menu.java b/src/main/java/eu/svjatoslav/instantlauncher/menu/Menu.java index 916f8ef..2201868 100755 --- a/src/main/java/eu/svjatoslav/instantlauncher/menu/Menu.java +++ b/src/main/java/eu/svjatoslav/instantlauncher/menu/Menu.java @@ -1,244 +1,229 @@ package eu.svjatoslav.instantlauncher.menu; -import java.awt.Color; -import java.awt.Dimension; +import eu.svjatoslav.commons.gui.dialog.ExceptionDialog; +import eu.svjatoslav.instantlauncher.Utils; + +import javax.swing.*; +import javax.swing.border.EmptyBorder; +import java.awt.*; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.io.File; import java.util.Arrays; -import java.util.Comparator; import java.util.HashSet; import java.util.Set; -import javax.swing.Box; -import javax.swing.BoxLayout; -import javax.swing.JButton; -import javax.swing.JPanel; -import javax.swing.SwingConstants; -import javax.swing.border.EmptyBorder; - -import eu.svjatoslav.commons.gui.dialog.ExceptionDialog; -import eu.svjatoslav.instantlauncher.Utils; - -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() == 2) - parentMenu.menuItemSelectedAlternative2(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_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_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_NONEXECUTABLE_FILE = new Color(0, 60, 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); - - /** - *
-	 * Assign specific colors to:
-	 *    Directory
-	 *    Executable file
-	 *    Non executable file
-	 * 
- */ - 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;; - - private final Set menuListeners = new HashSet(); - - 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()) - button = new JButton("[ " + file.getName() + " ]"); - else - button = new JButton(file.getName()); - - button.setBackground(getFileColor(file, false)); - button.setForeground(new Color(255, 255, 255)); - - button.setBorder(new EmptyBorder(5, 10, 5, 10)); - - final Menu.ButtonMouseListener mouseListener = new ButtonMouseListener(button, this, file); - button.addMouseListener(mouseListener); - - button.setHorizontalTextPosition(SwingConstants.CENTER); - - // set button size - Utils.setComponentSize(button, SIZE_BUTTON); - - return button; - } - - private void buildContentPanel(final File directory) { - menuPanel = new JPanel(); - - Utils.setComponentSize(menuPanel, SIZE_MENU_PANEL); - - final BoxLayout layout = new BoxLayout(menuPanel, BoxLayout.Y_AXIS); - menuPanel.setLayout(layout); - menuPanel.setBackground(Color.BLACK); - - final File[] listFiles = directory.listFiles(); - - // Sort files by name - Arrays.sort(listFiles, new Comparator() { - @Override - public int compare(final File f1, final File f2) { - - // directories come before files - if (f1.isFile() && f2.isDirectory()) - return 1; - 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) - // ignore hidden files - if (!file.getName().startsWith(".")) { - menuPanel.add(Box.createVerticalStrut(10)); - menuPanel.add(buildButton(file)); - } - } - - public JPanel getMenuPanel() { - return menuPanel; - } - - public JButton getSelectedButton() { - return selectedButton; - } - - public File getSelectedFile() { - return selectedFile; - } - - public void menuItemHighlighted(final File chosenFile) { - for (final MenuListener menuListener : menuListeners) - menuListener.menuItemHighlighted(this, chosenFile); - } - - 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 menuItemSelectedAlternative2(final File chosenFile) { - for (final MenuListener menuListener : menuListeners) - menuListener.menuItemSelectedAlternative2(this, chosenFile); - } - - public void setSelected(final JButton selectedButton, final File selectedFile) { - this.selectedButton = selectedButton; - this.selectedFile = selectedFile; - } +class Menu { + + public static final Dimension SIZE_MENU_PANEL = new Dimension(210, 900); + private static final Color BUTTON_BG_DIRECTORY = new Color(0, 0, 60); + + private static final Color BUTTON_BG_DIRECTORY_SELECTED = new Color(0, 0, 100); + private static final Color BUTTON_BG_LINK = new Color(60, 60, 0); + + private static final Color BUTTON_BG_LINK_SELECTED = new Color(100, 100, 0); + private static final Color BUTTON_BG_EXECUTABLE_FILE = new Color(60, 0, 0); + + private static final Color BUTTON_BG_EXECUTABLE_FILE_SELECTED = new Color(100, 0, 0); + private static final Color BUTTON_BG_NONEXECUTABLE_FILE = new Color(0, 60, 0); + + private static final Color BUTTON_BG_NONEXECUTABLE_FILE_SELECTED = new Color(0, 100, 0); + private static final Dimension SIZE_BUTTON = new Dimension(200, 30); + private final Set menuListeners = new HashSet<>(); + private JPanel menuPanel; + private JButton selectedButton; + private File selectedFile; + + public Menu(final File directory) { + buildContentPanel(directory); + } + + /** + *
+     * Assign specific colors to:
+     *    Directory
+     *    Executable file
+     *    Non executable file
+     * 
+ */ + private 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 void addMenuListener(final MenuListener menuListener) { + menuListeners.add(menuListener); + } + + private JButton buildButton(final File file) { + JButton button; + if (file.isDirectory()) + button = new JButton("[ " + file.getName() + " ]"); + else + button = new JButton(file.getName()); + + button.setBackground(getFileColor(file, false)); + button.setForeground(new Color(255, 255, 255)); + + button.setBorder(new EmptyBorder(5, 10, 5, 10)); + + final Menu.ButtonMouseListener mouseListener = new ButtonMouseListener(button, this, file); + button.addMouseListener(mouseListener); + + button.setHorizontalTextPosition(SwingConstants.CENTER); + + // set button size + Utils.setComponentSize(button, SIZE_BUTTON); + + return button; + } + + private void buildContentPanel(final File directory) { + menuPanel = new JPanel(); + + Utils.setComponentSize(menuPanel, SIZE_MENU_PANEL); + + final BoxLayout layout = new BoxLayout(menuPanel, BoxLayout.Y_AXIS); + menuPanel.setLayout(layout); + menuPanel.setBackground(Color.BLACK); + + final File[] listFiles = directory.listFiles(); + + // Sort files by name + Arrays.sort(listFiles, (f1, f2) -> { + + // directories come before files + if (f1.isFile() && f2.isDirectory()) + return 1; + 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) + // ignore hidden files + if (!file.getName().startsWith(".")) { + menuPanel.add(Box.createVerticalStrut(10)); + menuPanel.add(buildButton(file)); + } + } + + public JPanel getMenuPanel() { + return menuPanel; + } + + private JButton getSelectedButton() { + return selectedButton; + } + + private File getSelectedFile() { + return selectedFile; + } + + private void menuItemHighlighted(final File chosenFile) { + for (final MenuListener menuListener : menuListeners) + menuListener.menuItemHighlighted(this, chosenFile); + } + + private void menuItemSelected(final File chosenFile) { + for (final MenuListener menuListener : menuListeners) + menuListener.menuItemSelected(this, chosenFile); + } + + private void menuItemSelectedAlternative(final File chosenFile) { + for (final MenuListener menuListener : menuListeners) + menuListener.menuItemSelectedAlternative(this, chosenFile); + } + + private void menuItemSelectedAlternative2(final File chosenFile) { + for (final MenuListener menuListener : menuListeners) + menuListener.menuItemSelectedAlternative2(this, chosenFile); + } + + private void setSelected(final JButton selectedButton, final File selectedFile) { + this.selectedButton = selectedButton; + this.selectedFile = selectedFile; + } + + public static class ButtonMouseListener implements MouseListener { + + final JButton parentButton; + final Menu parentMenu; + final 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() == 2) + parentMenu.menuItemSelectedAlternative2(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) { + } + + } } \ No newline at end of file diff --git a/src/main/java/eu/svjatoslav/instantlauncher/menu/MenuListener.java b/src/main/java/eu/svjatoslav/instantlauncher/menu/MenuListener.java index 82e1a57..19f59d6 100755 --- a/src/main/java/eu/svjatoslav/instantlauncher/menu/MenuListener.java +++ b/src/main/java/eu/svjatoslav/instantlauncher/menu/MenuListener.java @@ -1,25 +1,25 @@ package eu.svjatoslav.instantlauncher.menu; -public interface MenuListener { +interface MenuListener { - /** - * mouse over - */ - public void menuItemHighlighted(Menu menu, java.io.File chosenFile); + /** + * mouse over + */ + void menuItemHighlighted(Menu menu, java.io.File chosenFile); - /** - * left click - */ - public void menuItemSelected(Menu menu, java.io.File chosenFile); + /** + * left click + */ + void menuItemSelected(Menu menu, java.io.File chosenFile); - /** - * right click - */ - public void menuItemSelectedAlternative(Menu menu, java.io.File chosenFile); + /** + * right click + */ + void menuItemSelectedAlternative(Menu menu, java.io.File chosenFile); - /** - * middle click - */ - public void menuItemSelectedAlternative2(Menu menu, java.io.File chosenFile); + /** + * middle click + */ + void menuItemSelectedAlternative2(Menu menu, java.io.File chosenFile); } diff --git a/src/main/java/eu/svjatoslav/instantlauncher/menu/MultiLevelMenu.java b/src/main/java/eu/svjatoslav/instantlauncher/menu/MultiLevelMenu.java index 017872f..10d6f3f 100755 --- a/src/main/java/eu/svjatoslav/instantlauncher/menu/MultiLevelMenu.java +++ b/src/main/java/eu/svjatoslav/instantlauncher/menu/MultiLevelMenu.java @@ -1,188 +1,180 @@ 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; - - public static final Dimension CONTENT_PANEL_SIZE = new Dimension(1024, 900); - - ArrayList menus = new ArrayList(); - - ArrayList panels = new ArrayList(); - JPanel contentPane; - - public MultiLevelMenu(final InstantLauncher instantLauncher) { - this.instantLauncher = instantLauncher; - } - - /** - * Adds new vertical menu. - * - * @return true if operation succeeded, or false - * if there was not enough space left for a new menu. - */ - public boolean addMenu(final File directory) { - final int newMenuHorizontalIndex = menus.size(); - - if (newMenuHorizontalIndex >= VERTICAL_MENUS_COUNT) { - return false; - } - - final Menu menu = new Menu(directory); - menu.addMenuListener(this); - - final JPanel placeholderPanel = panels.get(newMenuHorizontalIndex); - final JPanel menuPanel = menu.getMenuPanel(); - placeholderPanel.add(menuPanel, BorderLayout.CENTER); - - menus.add(menu); - return true; - } - - public JPanel buildContentPanel() { - - contentPane = new JPanel(); - contentPane.setBackground(Color.BLACK); - contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.X_AXIS)); - Utils.setComponentSize(contentPane, CONTENT_PANEL_SIZE); - - // initialize panels - for (int i = 0; i < VERTICAL_MENUS_COUNT; i++) { - contentPane.add(Box.createHorizontalStrut(10)); - - final JPanel panel = new JPanel(new BorderLayout()); - panel.setBackground(Color.BLACK); - - Utils.setComponentSize(panel, Menu.SIZE_MENU_PANEL); - - panels.add(panel); - contentPane.add(panel); - } - - final File directory = instantLauncher.configuration.getRootDirectory(); - addMenu(directory); - - return contentPane; - } - - public int getMenuIndex(final Menu menu) { - int i = 0; - for (final Menu m : menus) { - if (m == menu) { - return i; - } - i++; - } - return -1; - } - - @Override - public void menuItemHighlighted(final Menu menu, final File chosenFile) { - - if (chosenFile.isDirectory()) { - final int menuIndex = getMenuIndex(menu); - - if (menuIndex >= 0) { - removeMenus(menuIndex + 1); - if (addMenu(chosenFile)) { - contentPane.validate(); - contentPane.repaint(); - } - } - } - - } - - @Override - public void menuItemSelected(final Menu menu, final File chosenFile) { - if (chosenFile.isFile()) { - if (chosenFile.canExecute()) { - executeCommand(chosenFile.getAbsolutePath()); - } else { - boolean fileOpened; - try { - fileOpened = FileAssociationManager.openFile(chosenFile.getCanonicalFile()); - if (fileOpened) { - InstantLauncher.exitProgram(); - } - } catch (IOException e) { - e.printStackTrace(); - } - } - } - - if (chosenFile.isDirectory()) { - try { - 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()) { - chosenFile.setExecutable(!chosenFile.canExecute()); - } else { - try { - executeCommand("gnome-terminal", "--working-directory=" - + chosenFile.getCanonicalFile().getAbsolutePath()); - } catch (IOException e) { - e.printStackTrace(); - } - } - } - - public 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) { - 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(); - } - } - } + 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 menus = new ArrayList<>(); + + private final ArrayList panels = new ArrayList<>(); + private JPanel contentPane; + + public MultiLevelMenu(final InstantLauncher instantLauncher) { + this.instantLauncher = instantLauncher; + } + + /** + * Adds new vertical menu. + * + * @return true if operation succeeded, or false + * if there was not enough space left for a new menu. + */ + private boolean addMenu(final File directory) { + final int newMenuHorizontalIndex = menus.size(); + + if (newMenuHorizontalIndex >= VERTICAL_MENUS_COUNT) { + return false; + } + + final Menu menu = new Menu(directory); + menu.addMenuListener(this); + + final JPanel placeholderPanel = panels.get(newMenuHorizontalIndex); + final JPanel menuPanel = menu.getMenuPanel(); + placeholderPanel.add(menuPanel, BorderLayout.CENTER); + + menus.add(menu); + return true; + } + + public JPanel buildContentPanel() { + + contentPane = new JPanel(); + contentPane.setBackground(Color.BLACK); + contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.X_AXIS)); + Utils.setComponentSize(contentPane, CONTENT_PANEL_SIZE); + + // initialize panels + for (int i = 0; i < VERTICAL_MENUS_COUNT; i++) { + contentPane.add(Box.createHorizontalStrut(10)); + + final JPanel panel = new JPanel(new BorderLayout()); + panel.setBackground(Color.BLACK); + + Utils.setComponentSize(panel, Menu.SIZE_MENU_PANEL); + + panels.add(panel); + contentPane.add(panel); + } + + final File directory = instantLauncher.configuration.getRootDirectory(); + addMenu(directory); + + return contentPane; + } + + private int getMenuIndex(final Menu menu) { + int i = 0; + for (final Menu m : menus) { + if (m == menu) { + return i; + } + i++; + } + return -1; + } + + @Override + public void menuItemHighlighted(final Menu menu, final File chosenFile) { + + if (chosenFile.isDirectory()) { + final int menuIndex = getMenuIndex(menu); + + if (menuIndex >= 0) { + removeMenus(menuIndex + 1); + if (addMenu(chosenFile)) { + contentPane.validate(); + contentPane.repaint(); + } + } + } + + } + + @Override + public void menuItemSelected(final Menu menu, final File chosenFile) { + if (chosenFile.isFile()) { + if (chosenFile.canExecute()) { + executeCommand(chosenFile.getAbsolutePath()); + } else { + boolean fileOpened; + try { + fileOpened = FileAssociationManager.openFile(chosenFile.getCanonicalFile()); + if (fileOpened) { + InstantLauncher.exitProgram(); + } + } catch (IOException e) { + e.printStackTrace(); + } + } + } + + if (chosenFile.isDirectory()) { + try { + 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()) { + chosenFile.setExecutable(!chosenFile.canExecute()); + } else { + try { + executeCommand("gnome-terminal", "--working-directory=" + + chosenFile.getCanonicalFile().getAbsolutePath()); + } catch (IOException e) { + e.printStackTrace(); + } + } + } + + 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) { + 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(); + } + } + } } diff --git a/src/main/resources/log4j.xml b/src/main/resources/log4j.xml index b7e0f6c..8bb9ee1 100644 --- a/src/main/resources/log4j.xml +++ b/src/main/resources/log4j.xml @@ -1,16 +1,16 @@ - - - + + + - - - + + + - - - + + + \ No newline at end of file