class MacOSXConfiguration {
/**
* Binds <code>homeApplication</code> to Mac OS X application menu.
*/
public static void bindToApplicationMenu(final SweetHome3D homeApplication) {
Application macosxApplication = Application.getApplication();
// Create a default controller for an empty home and disable unrelated actions
final HomeController defaultController =
homeApplication.createHomeFrameController(homeApplication.createHome()).getHomeController();
final HomePane defaultHomeView = (HomePane)defaultController.getView();
for (HomePane.ActionType action : HomePane.ActionType.values()) {
switch (action) {
case ABOUT :
case NEW_HOME :
case OPEN :
case DELETE_RECENT_HOMES :
case HELP :
break;
default :
defaultHomeView.setEnabled(action, false);
}
}
final JMenuBar defaultMenuBar = defaultHomeView.getJMenuBar();
JFrame frame;
try {
if (UIManager.getLookAndFeel().getClass().getName().equals(UIManager.getSystemLookAndFeelClassName())) {
macosxApplication.setDefaultMenuBar(defaultMenuBar);
addWindowMenu(null, defaultMenuBar, homeApplication, true);
}
frame = null;
} catch (NoSuchMethodError ex) {
// Create default frame if setDefaultMenuBar isn't available
frame = new JFrame();
final JFrame defaultFrame = frame;
EventQueue.invokeLater(new Runnable() {
public void run() {
// Create a default undecorated frame out of sight
// and attach the application menu bar of empty view to it
defaultFrame.setLocation(-10, 0);
defaultFrame.setUndecorated(true);
defaultFrame.setVisible(true);
defaultFrame.setJMenuBar(defaultMenuBar);
defaultFrame.setContentPane(defaultHomeView);
addWindowMenu(defaultFrame, defaultMenuBar, homeApplication, true);
}
});
}
final JFrame defaultFrame = frame;
// Add a listener to Mac OS X application that will call
// controller methods of the active frame
macosxApplication.addApplicationListener(new ApplicationAdapter() {
@Override
public void handleQuit(ApplicationEvent ev) {
handleAction(new Runnable() {
public void run() {
defaultController.exit();
}
});
if (homeApplication.getHomes().isEmpty()) {
System.exit(0);
}
}
@Override
public void handleAbout(ApplicationEvent ev) {
handleAction(new Runnable() {
public void run() {
defaultController.about();
}
});
ev.setHandled(true);
}
@Override
public void handlePreferences(ApplicationEvent ev) {
handleAction(new Runnable() {
public void run() {
defaultController.editPreferences();
}
});
}
private void handleAction(Runnable runnable) {
Frame activeFrame = null;
for (Frame frame : Frame.getFrames()) {
if (frame.isActive()) {
activeFrame = frame;
break;
}
}
if (defaultFrame != null) {
// Move default frame to center to display dialogs at center
defaultFrame.setLocationRelativeTo(null);
}
runnable.run();
// Activate previous frame again
if (activeFrame != null) {
activeFrame.toFront();
}
if (defaultFrame != null) {
// Move default frame out of user view
defaultFrame.setLocation(-10, 0);
}
}
@Override
public void handleOpenFile(ApplicationEvent ev) {
// handleOpenFile is called when user opens a document
// associated with a Java Web Start application
// Just call main with -open file arguments as JNLP specifies
homeApplication.run(new String [] {"-open", ev.getFilename()});
}
@Override
public void handleReOpenApplication(ApplicationEvent ev) {
// handleReOpenApplication is called when user launches
// the application when it's already open
homeApplication.run(new String [0]);
}
});
macosxApplication.setEnabledAboutMenu(true);
macosxApplication.setEnabledPreferencesMenu(true);
homeApplication.addHomesListener(new CollectionListener<Home>() {
public void collectionChanged(CollectionEvent<Home> ev) {
if (ev.getType() == CollectionEvent.Type.ADD) {
// Add Mac OS X Window menu on new homes
JFrame homeFrame = homeApplication.getHomeFrame(ev.getItem());
MacOSXConfiguration.addWindowMenu(
homeFrame, homeFrame.getJMenuBar(), homeApplication, false);
}
};
});
// Set application icon if program wasn't launch from bundle
if (!"true".equalsIgnoreCase(System.getProperty("sweethome3d.bundle", "false"))) {
try {
String iconPath = homeApplication.getUserPreferences().getLocalizedString(HomePane.class, "about.icon");
Image icon = ImageIO.read(HomePane.class.getResource(iconPath));
macosxApplication.setDockIconImage(icon);
} catch (NoSuchMethodError ex) {
// Ignore icon change if setDockIconImage isn't available
} catch (IOException ex) {
}
}