Skip to content

Commit

Permalink
refactor: component actions out of main window [macata #94] (#95)
Browse files Browse the repository at this point in the history
  • Loading branch information
DazedNConfused- authored Nov 29, 2024
1 parent 7233d9a commit 5d084cf
Show file tree
Hide file tree
Showing 8 changed files with 1,218 additions and 741 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

<sonar.organization>dazednconfused</sonar.organization>
<sonar.host.url>https://sonarcloud.io</sonar.host.url>
<sonar.cpd.exclusions>**/MainWindow.java</sonar.cpd.exclusions>
<sonar.cpd.exclusions>**/gui/**</sonar.cpd.exclusions>
</properties>

<profiles>
Expand Down
118 changes: 118 additions & 0 deletions src/main/java/com/dazednconfused/catalauncher/gui/LauncherMenuBar.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
package com.dazednconfused.catalauncher.gui;

import com.dazednconfused.catalauncher.configuration.ConfigurationManager;
import com.dazednconfused.catalauncher.helper.LogLevelManager;

import io.vavr.control.Try;

import java.awt.Component;
import java.awt.event.ActionListener;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;

import javax.swing.JCheckBoxMenuItem;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.KeyStroke;

import lombok.Getter;

import org.apache.log4j.Level;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* This class comprises all actions related to the Launcher's Menu Bar and its associated GUI elements.
* */
public class LauncherMenuBar {

private static final Logger LOGGER = LoggerFactory.getLogger(LauncherMenuBar.class);

@Getter
private final JMenuBar menuBar;

private final JMenu helpMenu;
private final JMenu developerToolsMenu;

private final JMenuItem showConsoleLogMenuItem;
private final JCheckBoxMenuItem debugModeCheckBoxMenuItem;
private final JMenuItem aboutMenuItem;

/**
* Public constructor.
* */
public LauncherMenuBar(Component parent) {

LOGGER.trace("Building menu bar...");

// main menu bar ---
this.menuBar = new JMenuBar();

// help menu ---
this.helpMenu = new JMenu("Help");
this.helpMenu.setMnemonic(KeyEvent.VK_H);
this.menuBar.add(helpMenu);

// developer tools submenu --
this.developerToolsMenu = new JMenu("Developer Tools");
this.helpMenu.add(developerToolsMenu);

// show console log button -
this.showConsoleLogMenuItem = new JMenuItem("Show console log");
this.showConsoleLogMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C, InputEvent.ALT_DOWN_MASK));
this.showConsoleLogMenuItem.addActionListener(LauncherMenuBar.onShowConsoleButtonClicked(parent));
this.developerToolsMenu.add(this.showConsoleLogMenuItem);

// debug mode checkbox -
this.debugModeCheckBoxMenuItem = new JCheckBoxMenuItem("Debug mode");
this.debugModeCheckBoxMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_D, InputEvent.ALT_DOWN_MASK));
this.debugModeCheckBoxMenuItem.setState(ConfigurationManager.getInstance().isDebug());
this.debugModeCheckBoxMenuItem.addActionListener(LauncherMenuBar.onDebugModeButtonClicked(this.debugModeCheckBoxMenuItem));
this.developerToolsMenu.add(this.debugModeCheckBoxMenuItem);

// separator --
helpMenu.addSeparator();

// about button --
this.aboutMenuItem = new JMenuItem("About");
this.aboutMenuItem.setMnemonic(KeyEvent.VK_T);
this.aboutMenuItem.addActionListener(LauncherMenuBar.onAboutButtonClicked(parent));
this.helpMenu.add(this.aboutMenuItem);
}

/**
* The action to be performed on {@link #showConsoleLogMenuItem}'s click.
* */
private static ActionListener onShowConsoleButtonClicked(Component parent) {
return e -> {
LOGGER.trace("Show console button clicked");
Try.of(ConsoleLogReader::new)
.andThen(consoleLogReader -> consoleLogReader.packCenterAndShow(parent))
.onFailure(throwable -> LOGGER.error("There was an error while ConsoleLogReader window: [{}]", throwable.getMessage()));
};
}

/**
* The action to be performed on {@link #debugModeCheckBoxMenuItem}'s click.
* */
private static ActionListener onDebugModeButtonClicked(JCheckBoxMenuItem debugMode) {
return e -> {
LOGGER.trace("Debug mode checkbox clicked. Enabled: [{}]", debugMode.getState());
ConfigurationManager.getInstance().setDebug(debugMode.getState());
LogLevelManager.changeGlobalLogLevelTo(debugMode.getState() ? Level.TRACE : Level.INFO);
};
}

/**
* The action to be performed on {@link #aboutMenuItem}'s click.
* */
private static ActionListener onAboutButtonClicked(Component parent) {
return e -> {
LOGGER.trace("About button clicked");

VersionManagerWindow versionManagerWindow = new VersionManagerWindow();
versionManagerWindow.packCenterAndShow(parent);
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@
<properties/>
<border type="none"/>
<children>
<component id="f5a0c" class="javax.swing.JButton" binding="openFinderButton" default-binding="true">
<component id="f5a0c" class="javax.swing.JButton" binding="openExecutableFinderButton" default-binding="true">
<constraints>
<grid row="0" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
Expand Down
Loading

0 comments on commit 5d084cf

Please sign in to comment.