Setting the Look and Feel only if not changed by the user
"bbergeson" <[email protected]>
| Newsgroups | gmane.comp.java.netbeans.modules.openide.devel |
|---|---|
| Message-ID | <[email protected]> |
I came up with a solution that seems to be working well for me. I prefer that the users rely on the Options panel to change the Look and Feel. I created the following class and then I call it in my installer class passing in the default L&F I want to use in that application. I also pass in the Dark Nimbus L&F as an extra L&F to support.
Code:
public class LookAndFeelUtility {
// force the user to use the constructor that requires a default look and feel.
private LookAndFeelUtility() {
defaultLookAndFeel = null;
extraLookAndFeels = new HashSet<>();
}
/**
* Creates an instance of the LookAndFeelUtility
*
* @param defaultLookAndFeel this is the default look and feel that will be
* used if this is the first installation and initial launch of the application.
*/
public LookAndFeelUtility(LookAndFeel defaultLookAndFeel) {
this.defaultLookAndFeel = defaultLookAndFeel;
extraLookAndFeels = new HashSet<>();
}
public void addExtraLookAndFeel(LookAndFeel extraLookAndFeel) {
extraLookAndFeels.add(extraLookAndFeel);
}
private final HashSet<LookAndFeel> extraLookAndFeels;
private final LookAndFeel defaultLookAndFeel;
private Preferences getPreferences() {
return NbPreferences.root().node("laf");
}
/**
* This will set the look and feel according to what was saved by the application
* from the previous execution; otherwise it will use the default look and feel
* passed to the constructor.
*/
public void setLookAndFeel() {
LookAndFeel lookAndFeel = null;
Preferences prefs = getPreferences();
boolean firstTime = prefs.getBoolean("first_launch", true); // used to know if this is the first launch after initial installation of the application.
String lookAndFeelClassStr = prefs.get("laf", "na");
if (firstTime) {
prefs.put("laf", defaultLookAndFeel.getClass().getTypeName());
lookAndFeel = defaultLookAndFeel;
prefs.putBoolean("first_time", false);
} else {
// this following look and feels are included in my NetBeans RCP apps by default.
switch (lookAndFeelClassStr) {
case "javax.swing.plaf.metal.MetalLookAndFeel": // Metal
lookAndFeel = new MetalLookAndFeel();
break;
case "javax.swing.plaf.nimbus.NimbusLookAndFeel": // Nimbus
lookAndFeel = new NimbusLookAndFeel();
break;
case "com.sun.java.swing.plaf.motif.MotifLookAndFeel": // CDE/Motif
lookAndFeel = new MotifLookAndFeel();
break;
case "com.sun.java.swing.plaf.windows.WindowsLookAndFeel": // Windows
lookAndFeel = new WindowsLookAndFeel();
break;
case "com.sun.java.swing.plaf.windows.WindowsClassicLookAndFeel": // Windows Classic
lookAndFeel = new WindowsClassicLookAndFeel();
break;
case "org.netbeans.swing.laf.dark.DarkMetalLookAndFeel": // Dark Metal
lookAndFeel = new DarkMetalLookAndFeel();
break;
default: // if it didn't match one of the defaults then check the list of extra look and feels.
for (LookAndFeel laf : extraLookAndFeels) {
if (laf.getClass().getTypeName().equals(lookAndFeelClassStr)) {
lookAndFeel = laf;
break;
}
}
// couldn't find a match so use the default provided to the constructor.
if (lookAndFeel == null) {
prefs.put("laf", defaultLookAndFeel.getClass().getTypeName());
lookAndFeel = defaultLookAndFeel;
}
break;
}
}
setLookAndFeel(lookAndFeel);
}
/**
* This will set the look and feel to the value provided. If look and feel provided is null, then the default provided to the constructor will be used.
*/
public void setLookAndFeel(LookAndFeel lookAndFeel) {
LookAndFeel tmpLookAndFeel = lookAndFeel;
if (tmpLookAndFeel == null) {
tmpLookAndFeel = defaultLookAndFeel;
}
Preferences prefs = getPreferences();
for (UIManager.LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
if ("Dark Nimbus".equals(info.getName())) {
prefs.putBoolean("dark.themes.installed", true); //NOI18N
break;
}
}
try {
// don't use the method that requires a class name because it tries to get the class loader from the system but the class loader is not know within this validate method yet. Also, I don't use installLookAndFeel because that causes the L&F to show up twice in the L&F comboBox in the appearance panel and I get strange behavior sometimes.
UIManager.setLookAndFeel(tmpLookAndFeel);
} catch (UnsupportedLookAndFeelException ex) {
Exceptions.printStackTrace(ex);
}
}
}
Hope this helps someone else.