Skip to content

Commit d1d7f13

Browse files
committed
A more robust "universal" Main on mac
1 parent 10dfac4 commit d1d7f13

6 files changed

Lines changed: 26 additions & 21 deletions

File tree

modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/Application.java

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,9 @@ public class Application {
6464

6565
private static final Logger LOGGER = LoggerFactory.getLogger(Application.class);
6666

67-
public static void launch(String[] args, UIInitializer platformInitializer) {
67+
public static void launch(String[] args, UIInitializer platformInit) {
68+
69+
platformInit.beforeSwingLaunch();
6870

6971
CliArgs cli = CliArgs.parse(args);
7072

@@ -80,11 +82,11 @@ public static void launch(String[] args, UIInitializer platformInitializer) {
8082
new ModelerModule());
8183

8284
SwingUtilities.invokeLater(() ->
83-
new Application(injector, platformInitializer, cli).launch(cli.initialProject()));
85+
new Application(injector, platformInit, cli).launch(cli.initialProject()));
8486
}
8587

8688
private final Injector injector;
87-
private final UIInitializer platformInitializer;
89+
private final UIInitializer platformInit;
8890
private final ModelerClassLoader classLoader;
8991
private final PreferencesRepository preferencesRepository;
9092
private final ProjectValidator projectValidator;
@@ -95,9 +97,9 @@ public static void launch(String[] args, UIInitializer platformInitializer) {
9597
private CayenneUndoManager undoManager;
9698
private DBConnectors dbConnectors;
9799

98-
public Application(Injector injector, UIInitializer platformInitializer, CliArgs cli) {
100+
public Application(Injector injector, UIInitializer platformInit, CliArgs cli) {
99101
this.injector = injector;
100-
this.platformInitializer = platformInitializer;
102+
this.platformInit = platformInit;
101103
this.cli = cli;
102104

103105
this.classLoader = new ModelerClassLoader();
@@ -134,8 +136,8 @@ public UpgradeService getUpgradeService() {
134136
return injector.getInstance(UpgradeService.class);
135137
}
136138

137-
public UIInitializer getPlatformInitializer() {
138-
return platformInitializer;
139+
public UIInitializer getPlatformInit() {
140+
return platformInit;
139141
}
140142

141143
public ConfigurationNodeParentGetter getConfigurationNodeParentGetter() {
@@ -171,7 +173,7 @@ public CliArgs getCli() {
171173
}
172174

173175
public void launch(File initialProject) {
174-
this.platformInitializer.initLookAndFeel();
176+
175177
this.actionManager = new GlobalActions(
176178
this,
177179
injector.getInstance(ConfigurationNameMapper.class),
@@ -197,6 +199,8 @@ public void launch(File initialProject) {
197199

198200
this.undoManager = new CayenneUndoManager(this);
199201
this.frame = new MainFrame(this);
202+
this.platformInit.afterFrameCreated(this);
203+
200204

201205
// open up
202206
frame.onStartup();

modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/platform/UIInitializer.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,22 +20,22 @@
2020

2121
import org.apache.cayenne.modeler.Application;
2222

23-
import javax.swing.JFrame;
24-
2523
/**
2624
* A base callback for platform-specific Modeler initialization.
2725
*/
2826
public interface UIInitializer {
2927

3028
/**
31-
* Initializes application look and feel.
29+
* Sets platform-specific system properties and initializes the application look and feel.
30+
* Called before AWT/Swing is touched, so implementations may set properties that AWT reads
31+
* only during its own init (e.g. {@code apple.awt.application.name}) before installing the L&F.
3232
*/
33-
default void initLookAndFeel() {
33+
default void beforeSwingLaunch() {
3434
}
3535

3636
/**
3737
* Updates default frame menus according to the platform specifics.
3838
*/
39-
default void setupMenus(Application app, JFrame frame) {
39+
default void afterFrameCreated(Application app) {
4040
}
4141
}

modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/platform/generic/GenericUIInitializer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public class GenericUIInitializer implements UIInitializer {
3939
private static final String DEFAULT_THEME_NAME = "Sky Bluer";
4040

4141
@Override
42-
public void initLookAndFeel() {
42+
public void beforeSwingLaunch() {
4343

4444
PlasticTheme theme = findTheme();
4545

modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/platform/mac/MacUIInitializer.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,10 @@
3535
public class MacUIInitializer implements UIInitializer {
3636

3737
@Override
38-
public void initLookAndFeel() {
38+
public void beforeSwingLaunch() {
39+
// must be set before Aqua L&F initializes — it reads this property during init
40+
System.setProperty("apple.awt.application.name", "CayenneModeler");
41+
3942
// override some default styles and colors, assuming that Aqua theme will be used
4043

4144
Color lightGrey = new Color(0xEEEEEE);
@@ -89,10 +92,10 @@ public void paintBorder(Component c, Graphics g, int x, int y, int width, int he
8992
}
9093

9194
@Override
92-
public void setupMenus(Application app, JFrame frame) {
95+
public void afterFrameCreated(Application app) {
9396

9497
// set additional look and feel for the window
95-
frame.getRootPane().putClientProperty("apple.awt.brushMetalLook", Boolean.TRUE);
98+
app.getFrame().getRootPane().putClientProperty("apple.awt.brushMetalLook", Boolean.TRUE);
9699

97100
// Only relocate About/Preferences/Quit to the macOS application menu when the
98101
// screen menu bar is actually in use. Otherwise the JMenuBar stays inside the
@@ -119,7 +122,7 @@ public void setupMenus(Application app, JFrame frame) {
119122
removeActions.add(globalActions.getAction(AboutAction.class));
120123
removeActions.add(globalActions.getAction(ConfigurePreferencesAction.class));
121124

122-
JMenuBar menuBar = frame.getJMenuBar();
125+
JMenuBar menuBar = app.getFrame().getJMenuBar();
123126
for (Component c : menuBar.getComponents()) {
124127
if (c instanceof JMenu menu) {
125128

modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/platform/win/WinUIInitializer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public class WinUIInitializer implements UIInitializer {
3232
private static final Logger LOGGER = LoggerFactory.getLogger(WinUIInitializer.class);
3333

3434
@Override
35-
public void initLookAndFeel() {
35+
public void beforeSwingLaunch() {
3636
try {
3737
UIManager.setLookAndFeel(WindowsLookAndFeel.class.getName());
3838
// override some default styles and colors

modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/ui/MainFrame.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,6 @@ public MainFrame(Application app) {
135135

136136
setProjectView(null);
137137

138-
this.app.getPlatformInitializer().setupMenus(this.app, this);
139-
140138
this.session = new ProjectSession(this.app);
141139
this.session.addDirtyListener((wasDirty, isDirty) -> {
142140
if (isDirty) {

0 commit comments

Comments
 (0)