Skip to content

Commit

Permalink
separated main menu from frame; fixed look and feel menu bar.
Browse files Browse the repository at this point in the history
  • Loading branch information
i-make-robots committed Jun 4, 2024
1 parent 6ca3701 commit f1efeb1
Show file tree
Hide file tree
Showing 5 changed files with 225 additions and 164 deletions.
4 changes: 3 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>com.marginallyclever</groupId>
<artifactId>RobotOverlord</artifactId>
<version>2.112.1</version>
<version>2.113.0</version>
<name>Robot Overlord</name>
<description>A friendly 3D user interface for controlling robots.</description>
<url>https://www.marginallyclever.com/</url>
Expand Down Expand Up @@ -530,6 +530,7 @@
<version>0.3.12</version>
</dependency>

<!-- dockable windows -->
<dependency>
<groupId>io.github.andrewauclair</groupId>
<artifactId>modern-docking-api</artifactId>
Expand All @@ -545,6 +546,7 @@
<artifactId>modern-docking-ui</artifactId>
<version>0.11.1</version>
</dependency>

<!-- https://mvnrepository.com/artifact/com.formdev/flatlaf -->
<dependency>
<groupId>com.formdev</groupId>
Expand Down
22 changes: 22 additions & 0 deletions src/main/java/com/marginallyclever/ro3/RO3.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.marginallyclever.ro3;

import com.formdev.flatlaf.FlatLaf;
import com.formdev.flatlaf.FlatLightLaf;
import com.marginallyclever.ro3.apps.RO3Frame;

import javax.swing.*;
Expand All @@ -10,11 +12,31 @@
* You can find the friendly user manual at <a href="mcr.dozuki.com">mcr.dozuki.com</a>.
*/
public class RO3 {
private static final org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(RO3.class);

public static void main(String[] args) {
Registry.start();
setLookAndFeel();

if(!GraphicsEnvironment.isHeadless()) {
SwingUtilities.invokeLater(() -> (new RO3Frame()).setVisible(true));
}
}

private static void setLookAndFeel() {
logger.info("Setting look and feel...");
FlatLaf.registerCustomDefaultsSource( "com.marginallyclever.ro3" );
//FlatLaf.registerCustomDefaultsSource("docking");
try {
UIManager.setLookAndFeel(new FlatLightLaf());
// option 2: UIManager.setLookAndFeel(new FlatDarkLaf());
} catch (Exception ignored) {
logger.warn("failed to set flat look and feel. falling back to default native look and feel");
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception ex) {
logger.warn("failed to set native look and feel.", ex);
}
}
}
}
163 changes: 163 additions & 0 deletions src/main/java/com/marginallyclever/ro3/apps/MainMenu.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
package com.marginallyclever.ro3.apps;

import ModernDocking.app.DockableMenuItem;
import com.marginallyclever.ro3.apps.actions.*;
import com.marginallyclever.ro3.apps.dialogs.AppSettingsDialog;
import com.marginallyclever.ro3.apps.shared.PersistentJFileChooser;

import javax.swing.*;
import java.awt.*;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.awt.event.WindowEvent;
import java.util.List;
import java.util.Locale;
import java.util.Objects;
import java.util.prefs.Preferences;

public class MainMenu extends JMenuBar {
private final RO3Frame frame;
private final JFileChooser fileChooser;
private boolean isMacOS = false;
private int SHORTCUT_CTRL = InputEvent.CTRL_DOWN_MASK;
private int SHORTCUT_ALT = InputEvent.ALT_DOWN_MASK;

public MainMenu(RO3Frame frame) {
super();
this.frame = frame;

fileChooser = new PersistentJFileChooser();
fileChooser.setFileFilter(RO3Frame.FILE_FILTER);

setSystemLookAndFeelForMacos();
add(buildFileMenu());
add(buildEditMenu());
add(buildWindowsMenu());
add(buildHelpMenu());
updateUI();
}

private void setSystemLookAndFeelForMacos() {
String os = System.getProperty("os.name").toLowerCase(Locale.ENGLISH);
if ((os.contains("mac")) || (os.contains("darwin"))) {
isMacOS=true;
System.setProperty("apple.laf.useScreenMenuBar", "true");
SHORTCUT_CTRL = InputEvent.META_DOWN_MASK;
SHORTCUT_ALT = InputEvent.META_DOWN_MASK;
}
}

private JMenu buildFileMenu() {
// "load", "load recent", and "new" enable/disable save.
var loadRecentMenu = new RecentFilesMenu(Preferences.userNodeForPackage(LoadScene.class));
var save = new SaveScene(loadRecentMenu);
save.setEnabled(false);
var load = new LoadScene(loadRecentMenu,null,fileChooser);
load.setSaveScene(save);
loadRecentMenu.setSaveScene(save);
var saveAs = new SaveAsScene(loadRecentMenu,fileChooser);
saveAs.setSaveScene(save);

JMenu menuFile = new JMenu("File");
menuFile.add(new JMenuItem(new NewScene(save)));
menuFile.add(new JSeparator());
menuFile.add(new JMenuItem(load));
menuFile.add(loadRecentMenu);
menuFile.add(new JMenuItem(new ImportScene(fileChooser)));
menuFile.add(new JMenuItem(save));
menuFile.add(new JMenuItem(saveAs));
menuFile.add(new JMenuItem(new ExportScene(fileChooser)));

menuFile.add(new JSeparator());
var settingsMenu = new JMenuItem("Settings");
settingsMenu.setIcon(new ImageIcon(Objects.requireNonNull(getClass().getResource(
"/com/marginallyclever/ro3/apps/actions/icons8-settings-16.png"))));
menuFile.add(settingsMenu);

settingsMenu.addActionListener( e -> frame.runAppSettingsDialog() );

if(!isMacOS) {
menuFile.add(new JSeparator());
menuFile.add(new JMenuItem(new AbstractAction("Quit") {
{
putValue(Action.NAME, "Quit");
putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_Q, SHORTCUT_CTRL));
putValue(Action.SMALL_ICON, new ImageIcon(Objects.requireNonNull(getClass().getResource("icons8-stop-16.png"))));
putValue(Action.SHORT_DESCRIPTION, "Quit the application.");
}

@Override
public void actionPerformed(java.awt.event.ActionEvent e) {
if (frame.confirmClose()) {
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.dispatchEvent(new WindowEvent(frame, WindowEvent.WINDOW_CLOSING));
}
}
}));
}

return menuFile;
}

private JMenu buildWindowsMenu() {
JMenu menuWindows = new JMenu("Windows");
// add each panel to the windows menu with a checkbox if the current panel is visible.
int index=0;
for(DockingPanel w : frame.getDockingPanels()) {
DockableMenuItem item = new DockableMenuItem(w.getPersistentID(),w.getTabText());
menuWindows.add(item);
item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F1 + index, InputEvent.SHIFT_DOWN_MASK));
index++;
}

menuWindows.add(new JSeparator());
menuWindows.add(new JMenuItem(new AbstractAction() {
{
putValue(Action.NAME, "Reset default layout");
// no accelerator key.
putValue(Action.SMALL_ICON, new ImageIcon(Objects.requireNonNull(getClass().getResource("icons8-reset-16.png"))));
putValue(Action.SHORT_DESCRIPTION, "Reset the layout to the default.");
}

@Override
public void actionPerformed(java.awt.event.ActionEvent e) {
frame.resetDefaultLayout();
}
}));
return menuWindows;
}

private Component buildEditMenu() {
JMenu menu = new JMenu("Edit");
menu.add(new JMenuItem(UndoSystem.getCommandUndo()));
menu.add(new JMenuItem(UndoSystem.getCommandRedo()));
//menu.add(new JSeparator());
return menu;
}

private Component buildHelpMenu() {
JMenu menuHelp = new JMenu("Help");
var openManual = new BrowseURLAction("https://mcr.dozuki.com/c/Robot_Overlord_3");
openManual.putValue(Action.NAME, "Read the friendly manual");
openManual.putValue(Action.SMALL_ICON, new ImageIcon(Objects.requireNonNull(getClass().getResource("icons8-open-book-16.png"))));
openManual.putValue(Action.SHORT_DESCRIPTION, "Read the friendly manual. It has pictures and everything!");
menuHelp.add(new JMenuItem(openManual));

var visitForum = new BrowseURLAction("https://discord.gg/VQ82jNvDBP");
visitForum.putValue(Action.NAME, "Visit Forums");
visitForum.putValue(Action.SMALL_ICON, new ImageIcon(Objects.requireNonNull(getClass().getResource("icons8-discord-16.png"))));
visitForum.putValue(Action.SHORT_DESCRIPTION, "Join us on Discord!");
menuHelp.add(new JMenuItem(visitForum));

var visitIssues = new BrowseURLAction("https://github.com/MarginallyClever/Robot-Overlord-App/issues");
visitIssues.putValue(Action.NAME, "Report an Issue");
visitIssues.putValue(Action.SMALL_ICON, new ImageIcon(Objects.requireNonNull(getClass().getResource("icons8-bug-16.png"))));
visitIssues.putValue(Action.SHORT_DESCRIPTION, "Report an issue on GitHub");
menuHelp.add(new JMenuItem(visitIssues));

menuHelp.add(new JMenuItem(new CheckForUpdateAction()));

return menuHelp;
}

}
Loading

0 comments on commit f1efeb1

Please sign in to comment.