You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@logging.apache.org by rm...@apache.org on 2022/06/20 14:45:41 UTC
[logging-chainsaw] 02/04: Work on converting settings to commons configuration
This is an automated email from the ASF dual-hosted git repository.
rmiddleton pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/logging-chainsaw.git
commit b2c8a61e7782edd8f85c2a653ef179be1d6f0dd4
Author: Robert Middleton <ro...@rm5248.com>
AuthorDate: Sun Jun 19 18:39:18 2022 -0400
Work on converting settings to commons configuration
---
pom.xml | 10 +
.../chainsaw/ApplicationPreferenceModelPanel.java | 6 +-
.../log4j/chainsaw/ChainsawToolBarAndMenus.java | 70 +++--
.../java/org/apache/log4j/chainsaw/LogPanel.java | 44 +--
.../log4j/chainsaw/LogPanelPreferenceModel.java | 2 +-
src/main/java/org/apache/log4j/chainsaw/LogUI.java | 330 ++++++++-----------
.../log4j/chainsaw/TableColorizingRenderer.java | 17 +-
.../apache/log4j/chainsaw/color/ColorPanel.java | 24 +-
.../apache/log4j/chainsaw/color/RuleColorizer.java | 18 +-
.../log4j/chainsaw/prefs/ProfileManager.java | 6 +-
.../log4j/chainsaw/prefs/SettingsManager.java | 350 +++++----------------
11 files changed, 324 insertions(+), 553 deletions(-)
diff --git a/pom.xml b/pom.xml
index 55fae10..22fadea 100644
--- a/pom.xml
+++ b/pom.xml
@@ -210,6 +210,16 @@
<artifactId>genson</artifactId>
<version>1.6</version>
</dependency>
+ <dependency>
+ <groupId>org.apache.commons</groupId>
+ <artifactId>commons-configuration2</artifactId>
+ <version>2.7</version>
+ </dependency>
+ <dependency>
+ <groupId>commons-beanutils</groupId>
+ <artifactId>commons-beanutils</artifactId>
+ <version>1.9.4</version>
+ </dependency>
</dependencies>
<reporting>
diff --git a/src/main/java/org/apache/log4j/chainsaw/ApplicationPreferenceModelPanel.java b/src/main/java/org/apache/log4j/chainsaw/ApplicationPreferenceModelPanel.java
index 535aff3..9678a08 100644
--- a/src/main/java/org/apache/log4j/chainsaw/ApplicationPreferenceModelPanel.java
+++ b/src/main/java/org/apache/log4j/chainsaw/ApplicationPreferenceModelPanel.java
@@ -349,7 +349,7 @@ public class ApplicationPreferenceModelPanel extends AbstractPreferencePanel {
private void initComponents() {
setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
- configurationURL = new JComboBox<>(new DefaultComboBoxModel<>(committedPreferenceModel.getConfigurationURLs()));
+ configurationURL = new JComboBox(); //new JComboBox<>(new DefaultComboBoxModel<>(committedPreferenceModel.getConfigurationURLs()));
configurationURL.setEditable(true);
configurationURL.setPrototypeDisplayValue("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
configurationURL.setPreferredSize(new Dimension(375, 13));
@@ -447,8 +447,8 @@ public class ApplicationPreferenceModelPanel extends AbstractPreferencePanel {
return true;
}
});
- String configToDisplay = committedPreferenceModel.getBypassConfigurationURL() != null ? committedPreferenceModel.getBypassConfigurationURL() : committedPreferenceModel.getConfigurationURL();
- configurationURL.setSelectedItem(configToDisplay);
+// String configToDisplay = committedPreferenceModel.getBypassConfigurationURL() != null ? committedPreferenceModel.getBypassConfigurationURL() : committedPreferenceModel.getConfigurationURL();
+// configurationURL.setSelectedItem(configToDisplay);
}
public void browseForConfiguration() {
diff --git a/src/main/java/org/apache/log4j/chainsaw/ChainsawToolBarAndMenus.java b/src/main/java/org/apache/log4j/chainsaw/ChainsawToolBarAndMenus.java
index e23ea02..7fbeff3 100644
--- a/src/main/java/org/apache/log4j/chainsaw/ChainsawToolBarAndMenus.java
+++ b/src/main/java/org/apache/log4j/chainsaw/ChainsawToolBarAndMenus.java
@@ -32,6 +32,8 @@ import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
+import org.apache.commons.configuration2.AbstractConfiguration;
+import org.apache.log4j.chainsaw.prefs.SettingsManager;
/**
@@ -130,20 +132,20 @@ class ChainsawToolBarAndMenus implements ChangeListener {
toggleLogTreeAction, toggleScrollToBottomAction, changeModelAction,
};
- logui.getApplicationPreferenceModel().addPropertyChangeListener(
- "statusBar",
- evt -> {
- boolean value = (Boolean) evt.getNewValue();
- toggleStatusBarCheck.setSelected(value);
- });
-
- logui.getApplicationPreferenceModel().addPropertyChangeListener(
- "receivers",
- evt -> {
- boolean value = (Boolean) evt.getNewValue();
- showReceiversButton.setSelected(value);
- toggleShowReceiversCheck.setSelected(value);
- });
+// logui.getApplicationPreferenceModel().addPropertyChangeListener(
+// "statusBar",
+// evt -> {
+// boolean value = (Boolean) evt.getNewValue();
+// toggleStatusBarCheck.setSelected(value);
+// });
+//
+// logui.getApplicationPreferenceModel().addPropertyChangeListener(
+// "receivers",
+// evt -> {
+// boolean value = (Boolean) evt.getNewValue();
+// showReceiversButton.setSelected(value);
+// toggleShowReceiversCheck.setSelected(value);
+// });
}
/**
@@ -385,17 +387,18 @@ class ChainsawToolBarAndMenus implements ChangeListener {
viewMenu.setMnemonic('V');
+ AbstractConfiguration configuration = SettingsManager.getInstance().getGlobalConfiguration();
+
final JCheckBoxMenuItem showToolbarCheck =
new JCheckBoxMenuItem(toggleToolbarAction);
- showToolbarCheck.setSelected(
- logui.getApplicationPreferenceModel().isToolbar());
-
- logui.getApplicationPreferenceModel().addPropertyChangeListener(
- "toolbar",
- evt -> {
- boolean value = (Boolean) evt.getNewValue();
- showToolbarCheck.setSelected(value);
- });
+ showToolbarCheck.setSelected(configuration.getBoolean("toolbar", true));
+
+// logui.getApplicationPreferenceModel().addPropertyChangeListener(
+// "toolbar",
+// evt -> {
+// boolean value = (Boolean) evt.getNewValue();
+// showToolbarCheck.setSelected(value);
+// });
menuShowWelcome.setAction(toggleWelcomeVisibleAction);
@@ -436,8 +439,8 @@ class ChainsawToolBarAndMenus implements ChangeListener {
final Action toggleStatusBarAction =
new AbstractAction("Show Status bar") {
public void actionPerformed(ActionEvent arg0) {
- logui.getApplicationPreferenceModel().setStatusBar(
- toggleStatusBarCheck.isSelected());
+// logui.getApplicationPreferenceModel().setStatusBar(
+// toggleStatusBarCheck.isSelected());
}
};
@@ -445,7 +448,7 @@ class ChainsawToolBarAndMenus implements ChangeListener {
Action.MNEMONIC_KEY, KeyEvent.VK_B);
toggleStatusBarCheck.setAction(toggleStatusBarAction);
toggleStatusBarCheck.setSelected(
- logui.getApplicationPreferenceModel().isStatusBar());
+ configuration.getBoolean("statusBar", true));
activeTabMenu.add(pause);
activeTabMenu.add(toggleCyclicMenuItem);
@@ -621,8 +624,8 @@ class ChainsawToolBarAndMenus implements ChangeListener {
final Action action =
new AbstractAction("Show Receivers") {
public void actionPerformed(ActionEvent arg0) {
- logui.getApplicationPreferenceModel().setReceivers(
- !logui.getApplicationPreferenceModel().isReceivers());
+// logui.getApplicationPreferenceModel().setReceivers(
+// !logui.getApplicationPreferenceModel().isReceivers());
}
};
@@ -669,8 +672,8 @@ class ChainsawToolBarAndMenus implements ChangeListener {
final Action action =
new AbstractAction("Show Toolbar") {
public void actionPerformed(ActionEvent e) {
- logui.getApplicationPreferenceModel().setToolbar(
- !logui.getApplicationPreferenceModel().isToolbar());
+// logui.getApplicationPreferenceModel().setToolbar(
+// !logui.getApplicationPreferenceModel().isToolbar());
}
};
@@ -780,9 +783,12 @@ class ChainsawToolBarAndMenus implements ChangeListener {
}
private void scanState() {
+ AbstractConfiguration configuration = SettingsManager.getInstance().getGlobalConfiguration();
+ boolean showReceiversByDefault = configuration.getBoolean("showReceivers", false);
+
toggleStatusBarCheck.setSelected(logui.isStatusBarVisible());
toggleShowReceiversCheck.setSelected(
- logui.getApplicationPreferenceModel().isReceivers());
+ showReceiversByDefault);
logTreePaneButton.setSelected(logui.isLogTreePanelVisible());
LogPanel panel = logui.getCurrentLogPanel();
@@ -794,7 +800,7 @@ class ChainsawToolBarAndMenus implements ChangeListener {
toggleDetailMenuItem.setSelected(false);
}
showReceiversButton.setSelected(
- logui.getApplicationPreferenceModel().isReceivers());
+ showReceiversByDefault);
menuShowWelcome.setSelected(logui.getTabbedPane().containsWelcomePanel());
/**
diff --git a/src/main/java/org/apache/log4j/chainsaw/LogPanel.java b/src/main/java/org/apache/log4j/chainsaw/LogPanel.java
index 348fdbf..b2cb46c 100644
--- a/src/main/java/org/apache/log4j/chainsaw/LogPanel.java
+++ b/src/main/java/org/apache/log4j/chainsaw/LogPanel.java
@@ -528,27 +528,27 @@ public class LogPanel extends DockablePanel implements Profileable, ChainsawEven
menuItemToggleDetails.setSelected(value);
});
- applicationPreferenceModel.addPropertyChangeListener("searchColor", new PropertyChangeListener() {
- public void propertyChange(PropertyChangeEvent evt) {
- if (table != null) {
- table.repaint();
- }
- if (searchTable != null) {
- searchTable.repaint();
- }
- }
- });
-
- applicationPreferenceModel.addPropertyChangeListener("alternatingColor", new PropertyChangeListener() {
- public void propertyChange(PropertyChangeEvent evt) {
- if (table != null) {
- table.repaint();
- }
- if (searchTable != null) {
- searchTable.repaint();
- }
- }
- });
+// applicationPreferenceModel.addPropertyChangeListener("searchColor", new PropertyChangeListener() {
+// public void propertyChange(PropertyChangeEvent evt) {
+// if (table != null) {
+// table.repaint();
+// }
+// if (searchTable != null) {
+// searchTable.repaint();
+// }
+// }
+// });
+//
+// applicationPreferenceModel.addPropertyChangeListener("alternatingColor", new PropertyChangeListener() {
+// public void propertyChange(PropertyChangeEvent evt) {
+// if (table != null) {
+// table.repaint();
+// }
+// if (searchTable != null) {
+// searchTable.repaint();
+// }
+// }
+// });
/*
*End of preferenceModel listeners
@@ -2133,7 +2133,7 @@ public class LogPanel extends DockablePanel implements Profileable, ChainsawEven
}
//no need to URL encode the identifier
- colorizer.saveColorSettings(identifier);
+// colorizer.saveColorSettings(identifier);
}
private XStream buildXStreamForLogPanelPreference() {
diff --git a/src/main/java/org/apache/log4j/chainsaw/LogPanelPreferenceModel.java b/src/main/java/org/apache/log4j/chainsaw/LogPanelPreferenceModel.java
index c0844c2..f563ad9 100644
--- a/src/main/java/org/apache/log4j/chainsaw/LogPanelPreferenceModel.java
+++ b/src/main/java/org/apache/log4j/chainsaw/LogPanelPreferenceModel.java
@@ -43,7 +43,7 @@ public class LogPanelPreferenceModel implements Serializable {
static {
Collection list = new ArrayList();
- Properties properties = SettingsManager.getInstance().getDefaultSettings();
+ Properties properties = new Properties(); //SettingsManager.getInstance().getDefaultSettings();
for (Map.Entry<Object, Object> objectObjectEntry : properties.entrySet()) {
Map.Entry<Object, Object> entry = objectObjectEntry;
diff --git a/src/main/java/org/apache/log4j/chainsaw/LogUI.java b/src/main/java/org/apache/log4j/chainsaw/LogUI.java
index 806e1de..1283669 100644
--- a/src/main/java/org/apache/log4j/chainsaw/LogUI.java
+++ b/src/main/java/org/apache/log4j/chainsaw/LogUI.java
@@ -60,6 +60,7 @@ import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
+import org.apache.commons.configuration2.AbstractConfiguration;
import org.apache.log4j.chainsaw.logevents.ChainsawLoggingEvent;
import org.apache.log4j.chainsaw.zeroconf.ZeroConfPlugin;
import org.apache.logging.log4j.core.LoggerContext;
@@ -82,7 +83,7 @@ import org.w3c.dom.Element;
* @author Scott Deboy <sdeboy@apache.org>
* @author Paul Smith <psmith@apache.org>
*/
-public class LogUI extends JFrame implements ChainsawViewer, SettingsListener {
+public class LogUI extends JFrame {
private static final String MAIN_WINDOW_HEIGHT = "main.window.height";
private static final String MAIN_WINDOW_WIDTH = "main.window.width";
private static final String MAIN_WINDOW_Y = "main.window.y";
@@ -206,26 +207,20 @@ public class LogUI extends JFrame implements ChainsawViewer, SettingsListener {
System.setProperty("apple.laf.useScreenMenuBar", "true");
}
- final ApplicationPreferenceModel model = new ApplicationPreferenceModel();
-
- SettingsManager.getInstance().configure(new ApplicationPreferenceModelSaver(model));
- //if a configuration URL param was provided, set the configuration URL field to null
- if (configurationURLAppArg != null) {
- model.setBypassConfigurationURL(configurationURLAppArg);
- }
+ AbstractConfiguration configuration = SettingsManager.getInstance().getGlobalConfiguration();
EventQueue.invokeLater(() -> {
- String lookAndFeelClassName = model.getLookAndFeelClassName();
+ String lookAndFeelClassName = configuration.getString("lookAndFeelClassName");
if (lookAndFeelClassName == null || lookAndFeelClassName.trim().equals("")) {
String osName = System.getProperty("os.name");
if (osName.toLowerCase(Locale.ENGLISH).startsWith("mac")) {
//no need to assign look and feel
} else if (osName.toLowerCase(Locale.ENGLISH).startsWith("windows")) {
lookAndFeelClassName = "com.sun.java.swing.plaf.windows.WindowsLookAndFeel";
- model.setLookAndFeelClassName(lookAndFeelClassName);
+ configuration.setProperty("lookAndFeelClassName",lookAndFeelClassName);
} else if (osName.toLowerCase(Locale.ENGLISH).startsWith("linux")) {
lookAndFeelClassName = "com.sun.java.swing.plaf.gtk.GTKLookAndFeel";
- model.setLookAndFeelClassName(lookAndFeelClassName);
+ configuration.setProperty("lookAndFeelClassName",lookAndFeelClassName);
}
}
@@ -238,7 +233,7 @@ public class LogUI extends JFrame implements ChainsawViewer, SettingsListener {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}catch(Exception ex){}
}
- createChainsawGUI(model, null);
+ createChainsawGUI(null);
});
}
@@ -250,10 +245,10 @@ public class LogUI extends JFrame implements ChainsawViewer, SettingsListener {
* @param model
* @param newShutdownAction DOCUMENT ME!
*/
- public static void createChainsawGUI(
- ApplicationPreferenceModel model, Action newShutdownAction) {
+ public static void createChainsawGUI(Action newShutdownAction) {
+ AbstractConfiguration config = SettingsManager.getInstance().getGlobalConfiguration();
- if (model.isOkToRemoveSecurityManager()) {
+ if (config.getBoolean("okToRemoveSecurityManager", false)) {
// statusBar.setMessage("User has authorised removal of Java Security Manager via preferences");
System.setSecurityManager(null);
// this SHOULD set the Policy/Permission stuff for any
@@ -273,12 +268,11 @@ public class LogUI extends JFrame implements ChainsawViewer, SettingsListener {
}
final LogUI logUI = new LogUI();
- logUI.applicationPreferenceModel = model;
- if (model.isShowSplash()) {
+ if (config.getBoolean("slowSplash", true)) {
showSplash(logUI);
}
- logUI.cyclicBufferSize = model.getCyclicBufferSize();
+ logUI.cyclicBufferSize = config.getInt("cyclicBufferSize", 50000);
final LoggerContext ctx = (LoggerContext) org.apache.logging.log4j.LogManager.getContext(false);
logUI.chainsawAppender = ctx.getConfiguration().getAppender("chainsaw");
@@ -297,46 +291,46 @@ public class LogUI extends JFrame implements ChainsawViewer, SettingsListener {
logger.error("Uncaught exception in thread " + t, e);
});
- String config = configurationURLAppArg;
- if (config != null) {
- logger.info("Command-line configuration arg provided (overriding auto-configuration URL) - using: " + config);
- } else {
- config = model.getConfigurationURL();
- }
-
- if (config != null && (!config.trim().equals(""))) {
- config = config.trim();
- try {
- URL configURL = new URL(config);
- logger.info("Using '" + config + "' for auto-configuration");
-// logUI.loadConfigurationUsingPluginClassLoader(configURL);
- } catch (MalformedURLException e) {
- logger.error("Initial configuration - failed to convert config string to url", e);
- } catch (IOException e) {
- logger.error("Unable to access auto-configuration URL: " + config);
- }
- }
+// String config = configurationURLAppArg;
+// if (config != null) {
+// logger.info("Command-line configuration arg provided (overriding auto-configuration URL) - using: " + config);
+// } else {
+// config = model.getConfigurationURL();
+// }
+
+// if (config != null && (!config.trim().equals(""))) {
+// config = config.trim();
+// try {
+// URL configURL = new URL(config);
+// logger.info("Using '" + config + "' for auto-configuration");
+//// logUI.loadConfigurationUsingPluginClassLoader(configURL);
+// } catch (MalformedURLException e) {
+// logger.error("Initial configuration - failed to convert config string to url", e);
+// } catch (IOException e) {
+// logger.error("Unable to access auto-configuration URL: " + config);
+// }
+// }
//register a listener to load the configuration when it changes (avoid having to restart Chainsaw when applying a new configuration)
//this doesn't remove receivers from receivers panel, it just triggers DOMConfigurator.configure.
- model.addPropertyChangeListener("configurationURL", evt -> {
- String newConfiguration = evt.getNewValue().toString();
- if (newConfiguration != null && !(newConfiguration.trim().equals(""))) {
- newConfiguration = newConfiguration.trim();
- try {
- logger.info("loading updated configuration: " + newConfiguration);
- URL newConfigurationURL = new URL(newConfiguration);
- File file = new File(newConfigurationURL.toURI());
- if (file.exists()) {
-// logUI.loadConfigurationUsingPluginClassLoader(newConfigurationURL);
- } else {
- logger.info("Updated configuration but file does not exist");
- }
- } catch (MalformedURLException | URISyntaxException e) {
- logger.error("Updated configuration - failed to convert config string to URL", e);
- }
- }
- });
+// model.addPropertyChangeListener("configurationURL", evt -> {
+// String newConfiguration = evt.getNewValue().toString();
+// if (newConfiguration != null && !(newConfiguration.trim().equals(""))) {
+// newConfiguration = newConfiguration.trim();
+// try {
+// logger.info("loading updated configuration: " + newConfiguration);
+// URL newConfigurationURL = new URL(newConfiguration);
+// File file = new File(newConfigurationURL.toURI());
+// if (file.exists()) {
+//// logUI.loadConfigurationUsingPluginClassLoader(newConfigurationURL);
+// } else {
+// logger.info("Updated configuration but file does not exist");
+// }
+// } catch (MalformedURLException | URISyntaxException e) {
+// logger.error("Updated configuration - failed to convert config string to URL", e);
+// }
+// }
+// });
EventQueue.invokeLater(logUI::activateViewer);
EventQueue.invokeLater(logUI::buildChainsawLogPanel);
@@ -355,41 +349,6 @@ public class LogUI extends JFrame implements ChainsawViewer, SettingsListener {
}
}
- /**
- * Allow Chainsaw v2 to be ran in-process (configured as a ChainsawAppender)
- * NOTE: Closing Chainsaw will NOT stop the application generating the events.
- *
- * @param appender
- */
- public void activateViewer(ChainsawAppender appender) {
-
- if (OSXIntegration.IS_OSX) {
- System.setProperty("apple.laf.useScreenMenuBar", "true");
- }
-
- final ApplicationPreferenceModel model = new ApplicationPreferenceModel();
- SettingsManager.getInstance().configure(new ApplicationPreferenceModelSaver(model));
-
- cyclicBufferSize = model.getCyclicBufferSize();
-
- setShutdownAction(
- new AbstractAction() {
- public void actionPerformed(ActionEvent e) {
- }
- });
-
- applicationPreferenceModel = new ApplicationPreferenceModel();
-
- SettingsManager.getInstance().configure(new ApplicationPreferenceModelSaver(model));
-
- EventQueue.invokeLater(() -> {
-// loadLookAndFeelUsingPluginClassLoader(model.getLookAndFeelClassName());
- createChainsawGUI(model, null);
- getApplicationPreferenceModel().apply(model);
- activateViewer();
- });
- }
-
/**
* Initialises the menu's and toolbars, but does not actually create any of
* the main panel components.
@@ -405,8 +364,8 @@ public class LogUI extends JFrame implements ChainsawViewer, SettingsListener {
setJMenuBar(getToolBarAndMenus().getMenubar());
setTabbedPane(new ChainsawTabbedPane());
- getSettingsManager().addSettingsListener(getTabbedPane());
- getSettingsManager().configure(getTabbedPane());
+// getSettingsManager().addSettingsListener(getTabbedPane());
+// getSettingsManager().configure(getTabbedPane());
/**
* This adds Drag & Drop capability to Chainsaw
@@ -458,10 +417,10 @@ public class LogUI extends JFrame implements ChainsawViewer, SettingsListener {
private void setupReceiverPanel() {
receiversPanel = new ReceiversPanel(this, statusBar);
- receiversPanel.addPropertyChangeListener(
- "visible",
- evt -> getApplicationPreferenceModel().setReceivers(
- (Boolean) evt.getNewValue()));
+// receiversPanel.addPropertyChangeListener(
+// "visible",
+// evt -> getApplicationPreferenceModel().setReceivers(
+// (Boolean) evt.getNewValue()));
}
/**
@@ -529,11 +488,12 @@ public class LogUI extends JFrame implements ChainsawViewer, SettingsListener {
*
* @param event DOCUMENT ME!
*/
- public void loadSettings(LoadSettingsEvent event) {
+ private void loadSettings() {
+ AbstractConfiguration config = SettingsManager.getInstance().getGlobalConfiguration();
setLocation(
- event.asInt(LogUI.MAIN_WINDOW_X), event.asInt(LogUI.MAIN_WINDOW_Y));
- int width = event.asInt(LogUI.MAIN_WINDOW_WIDTH);
- int height = event.asInt(LogUI.MAIN_WINDOW_HEIGHT);
+ config.getInt(LogUI.MAIN_WINDOW_X, 0), config.getInt(LogUI.MAIN_WINDOW_Y, 0));
+ int width = config.getInt(LogUI.MAIN_WINDOW_WIDTH, -1);
+ int height = config.getInt(LogUI.MAIN_WINDOW_HEIGHT, -1);
if (width == -1 && height == -1) {
width = Toolkit.getDefaultToolkit().getScreenSize().width;
height = Toolkit.getDefaultToolkit().getScreenSize().height;
@@ -547,27 +507,6 @@ public class LogUI extends JFrame implements ChainsawViewer, SettingsListener {
RuleColorizer colorizer = new RuleColorizer();
colorizer.loadColorSettings(ChainsawConstants.DEFAULT_COLOR_RULE_NAME);
allColorizers.put(ChainsawConstants.DEFAULT_COLOR_RULE_NAME, colorizer);
- if (event.getSetting("SavedConfig.logFormat") != null) {
- receiverConfigurationPanel.getModel().setLogFormat(event.getSetting("SavedConfig.logFormat"));
- }
- }
-
- /**
- * Ensures the location/size of the main window is stored with the settings
- *
- * @param event DOCUMENT ME!
- */
- public void saveSettings(SaveSettingsEvent event) {
- event.saveSetting(LogUI.MAIN_WINDOW_X, (int) getLocation().getX());
- event.saveSetting(LogUI.MAIN_WINDOW_Y, (int) getLocation().getY());
-
- event.saveSetting(LogUI.MAIN_WINDOW_WIDTH, getWidth());
- event.saveSetting(LogUI.MAIN_WINDOW_HEIGHT, getHeight());
- RuleColorizer colorizer = allColorizers.get(ChainsawConstants.DEFAULT_COLOR_RULE_NAME);
- colorizer.saveColorSettings(ChainsawConstants.DEFAULT_COLOR_RULE_NAME);
- if (receiverConfigurationPanel.getModel().getLogFormat() != null) {
- event.saveSetting("SavedConfig.logFormat", receiverConfigurationPanel.getModel().getLogFormat());
- }
}
public void buildChainsawLogPanel(){
@@ -582,6 +521,7 @@ public class LogUI extends JFrame implements ChainsawViewer, SettingsListener {
*/
public void activateViewer() {
initGUI();
+ loadSettings();
initPrefModelListeners();
@@ -822,22 +762,22 @@ public class LogUI extends JFrame implements ChainsawViewer, SettingsListener {
// statusBar.setDataRate(dataRate);
// });
- getSettingsManager().addSettingsListener(this);
- getSettingsManager().addSettingsListener(MRUFileListPreferenceSaver.getInstance());
- getSettingsManager().addSettingsListener(receiversPanel);
- try {
- //if an uncaught exception is thrown, allow the UI to continue to load
- getSettingsManager().loadSettings();
- } catch (Exception e) {
- e.printStackTrace();
- }
+// getSettingsManager().addSettingsListener(this);
+// getSettingsManager().addSettingsListener(MRUFileListPreferenceSaver.getInstance());
+// getSettingsManager().addSettingsListener(receiversPanel);
+// try {
+// //if an uncaught exception is thrown, allow the UI to continue to load
+// getSettingsManager().loadSettings();
+// } catch (Exception e) {
+// e.printStackTrace();
+// }
//app preferences have already been loaded (and configuration url possibly set to blank if being overridden)
//but we need a listener so the settings will be saved on exit (added after loadsettings was called)
- getSettingsManager().addSettingsListener(new ApplicationPreferenceModelSaver(applicationPreferenceModel));
+// getSettingsManager().addSettingsListener(new ApplicationPreferenceModelSaver(applicationPreferenceModel));
setVisible(true);
- if (applicationPreferenceModel.isReceivers()) {
+ if (sm.getGlobalConfiguration().getBoolean("showReceivers", false)) {
showReceiverPanel();
} else {
hideReceiverPanel();
@@ -852,7 +792,7 @@ public class LogUI extends JFrame implements ChainsawViewer, SettingsListener {
if (
noReceiversDefined
- && applicationPreferenceModel.isShowNoReceiverWarning()) {
+ && sm.getGlobalConfiguration().getBoolean("showNoReceiverWarning", true)) {
SwingHelper.invokeOnEDT(this::showReceiverConfigurationPanel);
}
@@ -995,10 +935,10 @@ public class LogUI extends JFrame implements ChainsawViewer, SettingsListener {
* hide those tabs out of currently loaded tabs..
*/
- if (!getTabbedPane().tabSetting.isWelcome()) {
+ if (!sm.getGlobalConfiguration().getBoolean("displayWelcomeTab", true)) {
displayPanel(ChainsawTabbedPane.WELCOME_TAB, false);
}
- if (!getTabbedPane().tabSetting.isZeroconf()) {
+ if (!sm.getGlobalConfiguration().getBoolean("displayZeroconfTab", true)) {
displayPanel(ChainsawTabbedPane.ZEROCONF, false);
}
tbms.stateChange();
@@ -1039,13 +979,13 @@ public class LogUI extends JFrame implements ChainsawViewer, SettingsListener {
// evt -> handler.setIdentifierExpression(evt.getNewValue().toString()));
// handler.setIdentifierExpression(applicationPreferenceModel.getIdentifierExpression());
-
- applicationPreferenceModel.addPropertyChangeListener(
- "toolTipDisplayMillis",
- evt -> ToolTipManager.sharedInstance().setDismissDelay(
- (Integer) evt.getNewValue()));
+ int tooltipDisplayMillis = sm.getGlobalConfiguration().getInt("tooltipDisplayMillis", 4000);
+// applicationPreferenceModel.addPropertyChangeListener(
+// "toolTipDisplayMillis",
+// evt -> ToolTipManager.sharedInstance().setDismissDelay(
+// (Integer) evt.getNewValue()));
ToolTipManager.sharedInstance().setDismissDelay(
- applicationPreferenceModel.getToolTipDisplayMillis());
+ tooltipDisplayMillis);
// applicationPreferenceModel.addPropertyChangeListener(
// "responsiveness",
@@ -1055,57 +995,58 @@ public class LogUI extends JFrame implements ChainsawViewer, SettingsListener {
// });
// handler.setQueueInterval((applicationPreferenceModel.getResponsiveness() * 1000) - 750);
- applicationPreferenceModel.addPropertyChangeListener(
- "tabPlacement",
- evt -> SwingUtilities.invokeLater(
- () -> {
- int placement = (Integer) evt.getNewValue();
-
- switch (placement) {
- case SwingConstants.TOP:
- case SwingConstants.BOTTOM:
- tabbedPane.setTabPlacement(placement);
-
- break;
-
- default:
- break;
- }
- }));
-
- applicationPreferenceModel.addPropertyChangeListener(
- "statusBar",
- evt -> {
- boolean value = (Boolean) evt.getNewValue();
- setStatusBarVisible(value);
- });
- setStatusBarVisible(applicationPreferenceModel.isStatusBar());
-
- applicationPreferenceModel.addPropertyChangeListener(
- "receivers",
- evt -> {
- boolean value = (Boolean) evt.getNewValue();
-
- if (value) {
- showReceiverPanel();
- } else {
- hideReceiverPanel();
- }
- });
-// if (applicationPreferenceModel.isReceivers()) {
-// showReceiverPanel();
-// } else {
-// hideReceiverPanel();
-// }
-
-
- applicationPreferenceModel.addPropertyChangeListener(
- "toolbar",
- evt -> {
- boolean value = (Boolean) evt.getNewValue();
- toolbar.setVisible(value);
- });
- toolbar.setVisible(applicationPreferenceModel.isToolbar());
+// applicationPreferenceModel.addPropertyChangeListener(
+// "tabPlacement",
+// evt -> SwingUtilities.invokeLater(
+// () -> {
+// int placement = (Integer) evt.getNewValue();
+//
+// switch (placement) {
+// case SwingConstants.TOP:
+// case SwingConstants.BOTTOM:
+// tabbedPane.setTabPlacement(placement);
+//
+// break;
+//
+// default:
+// break;
+// }
+// }));
+//
+// applicationPreferenceModel.addPropertyChangeListener(
+// "statusBar",
+// evt -> {
+// boolean value = (Boolean) evt.getNewValue();
+// setStatusBarVisible(value);
+// });
+// setStatusBarVisible(applicationPreferenceModel.isStatusBar());
+//
+// applicationPreferenceModel.addPropertyChangeListener(
+// "receivers",
+// evt -> {
+// boolean value = (Boolean) evt.getNewValue();
+//
+// if (value) {
+// showReceiverPanel();
+// } else {
+// hideReceiverPanel();
+// }
+// });
+//// if (applicationPreferenceModel.isReceivers()) {
+//// showReceiverPanel();
+//// } else {
+//// hideReceiverPanel();
+//// }
+//
+//
+// applicationPreferenceModel.addPropertyChangeListener(
+// "toolbar",
+// evt -> {
+// boolean value = (Boolean) evt.getNewValue();
+// toolbar.setVisible(value);
+// });
+ boolean showToolbar = sm.getGlobalConfiguration().getBoolean("toolbar", true);
+ toolbar.setVisible(showToolbar);
}
@@ -1292,7 +1233,7 @@ public class LogUI extends JFrame implements ChainsawViewer, SettingsListener {
* Exits the application, ensuring Settings are saved.
*/
public boolean exit() {
- getSettingsManager().saveSettings();
+ getSettingsManager().saveAllSettings();
return shutdown();
}
@@ -1663,9 +1604,6 @@ public class LogUI extends JFrame implements ChainsawViewer, SettingsListener {
throws IllegalArgumentException {
final LogPanel thisPanel = new LogPanel(getStatusBar(), ident, cyclicBufferSize, allColorizers, applicationPreferenceModel);
- getSettingsManager().addSettingsListener(thisPanel);
- getSettingsManager().configure(thisPanel);
-
if( !customExpression && rx != null ){
thisPanel.setReceiver(rx);
}
diff --git a/src/main/java/org/apache/log4j/chainsaw/TableColorizingRenderer.java b/src/main/java/org/apache/log4j/chainsaw/TableColorizingRenderer.java
index aa110f5..964d6c7 100644
--- a/src/main/java/org/apache/log4j/chainsaw/TableColorizingRenderer.java
+++ b/src/main/java/org/apache/log4j/chainsaw/TableColorizingRenderer.java
@@ -39,6 +39,8 @@ import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.TimeZone;
+import org.apache.commons.configuration2.AbstractConfiguration;
+import org.apache.log4j.chainsaw.prefs.SettingsManager;
/**
@@ -402,14 +404,15 @@ public class TableColorizingRenderer extends DefaultTableCellRenderer {
Color background;
Color foreground;
Rule loggerRule = colorizer.getLoggerRule();
+ AbstractConfiguration configuration = SettingsManager.getInstance().getGlobalConfiguration();
//use logger colors in table instead of event colors if event passes logger rule
if (loggerRule != null && loggerRule.evaluate(loggingEventWrapper.getLoggingEvent(), null)) {
- background = applicationPreferenceModel.getSearchBackgroundColor();
- foreground = applicationPreferenceModel.getSearchForegroundColor();
+ background = configuration.get(Color.class, "searchBackgroundColor", ChainsawConstants.FIND_LOGGER_BACKGROUND);
+ foreground = configuration.get(Color.class, "searchForegroundColor", ChainsawConstants.FIND_LOGGER_FOREGROUND);
} else {
- if (colorizeSearch && !applicationPreferenceModel.isBypassSearchColors()) {
- background = loggingEventWrapper.isSearchMatch() ? applicationPreferenceModel.getSearchBackgroundColor() : loggingEventWrapper.getBackground();
- foreground = loggingEventWrapper.isSearchMatch() ? applicationPreferenceModel.getSearchForegroundColor() : loggingEventWrapper.getForeground();
+ if (colorizeSearch && !configuration.getBoolean("bypassSearchColors", false)) {
+ background = loggingEventWrapper.isSearchMatch() ? configuration.get(Color.class, "searchBackgroundColor", ChainsawConstants.FIND_LOGGER_BACKGROUND) : loggingEventWrapper.getBackground();
+ foreground = loggingEventWrapper.isSearchMatch() ? configuration.get(Color.class, "searchForegroundColor", ChainsawConstants.FIND_LOGGER_FOREGROUND) : loggingEventWrapper.getForeground();
} else {
background = loggingEventWrapper.getBackground();
foreground = loggingEventWrapper.getForeground();
@@ -420,8 +423,8 @@ public class TableColorizingRenderer extends DefaultTableCellRenderer {
* Colourize background based on row striping if the event still has default foreground and background color
*/
if (background.equals(ChainsawConstants.COLOR_DEFAULT_BACKGROUND) && foreground.equals(ChainsawConstants.COLOR_DEFAULT_FOREGROUND) && (row % 2) != 0) {
- background = applicationPreferenceModel.getAlternatingColorBackgroundColor();
- foreground = applicationPreferenceModel.getAlternatingColorForegroundColor();
+ background = configuration.get(Color.class, "alternatingColorBackground", ChainsawConstants.COLOR_ODD_ROW_BACKGROUND);
+ foreground = configuration.get(Color.class, "alternatingColorForeground", ChainsawConstants.COLOR_ODD_ROW_FOREGROUND);
}
component.setBackground(background);
diff --git a/src/main/java/org/apache/log4j/chainsaw/color/ColorPanel.java b/src/main/java/org/apache/log4j/chainsaw/color/ColorPanel.java
index 20de124..136c874 100644
--- a/src/main/java/org/apache/log4j/chainsaw/color/ColorPanel.java
+++ b/src/main/java/org/apache/log4j/chainsaw/color/ColorPanel.java
@@ -42,6 +42,8 @@ import java.util.List;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableCellEditor;
import javax.swing.table.TableModel;
+import org.apache.commons.configuration2.AbstractConfiguration;
+import org.apache.log4j.chainsaw.prefs.SettingsManager;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
@@ -90,6 +92,8 @@ public class ColorPanel extends JPanel {
final Map<String, RuleColorizer> allLogPanelColorizers, final ApplicationPreferenceModel applicationPreferenceModel) {
super(new BorderLayout());
+ AbstractConfiguration configuration = SettingsManager.getInstance().getGlobalConfiguration();
+
this.currentLogPanelColorizer = currentLogPanelColorizer;
this.colorizer = currentLogPanelColorizer;
this.filterModel = filterModel;
@@ -132,15 +136,15 @@ public class ColorPanel extends JPanel {
//searchtable contains only a single-entry vector containing a two-item vector (foreground, background)
searchDataVector = new Vector<>();
searchDataVectorEntry = new Vector<>();
- searchDataVectorEntry.add(applicationPreferenceModel.getSearchBackgroundColor());
- searchDataVectorEntry.add(applicationPreferenceModel.getSearchForegroundColor());
+ searchDataVectorEntry.add(configuration.get(Color.class, "searchBackgroundColor", ChainsawConstants.FIND_LOGGER_BACKGROUND));
+ searchDataVectorEntry.add(configuration.get(Color.class, "searchForegroundColor", ChainsawConstants.FIND_LOGGER_FOREGROUND));
searchDataVector.add(searchDataVectorEntry);
searchTableModel.setDataVector(searchDataVector, searchColumns);
alternatingColorDataVector = new Vector<>();
alternatingColorDataVectorEntry = new Vector<>();
- alternatingColorDataVectorEntry.add(applicationPreferenceModel.getAlternatingColorBackgroundColor());
- alternatingColorDataVectorEntry.add(applicationPreferenceModel.getAlternatingColorForegroundColor());
+ alternatingColorDataVectorEntry.add(configuration.get(Color.class, "alternatingColorBackground", ChainsawConstants.COLOR_ODD_ROW_BACKGROUND));
+ alternatingColorDataVectorEntry.add(configuration.get(Color.class, "alternatingColorForeground", ChainsawConstants.COLOR_ODD_ROW_FOREGROUND));
alternatingColorDataVector.add(alternatingColorDataVectorEntry);
alternatingColorTableModel.setDataVector(alternatingColorDataVector, alternatingColorColumns);
@@ -189,7 +193,7 @@ public class ColorPanel extends JPanel {
southPanel.add(Box.createVerticalStrut(5));
JPanel searchAndAlternatingColorPanel = buildSearchAndAlternatingColorPanel();
JPanel bypassSearchColorsPanel = buildBypassSearchColorsPanel();
- bypassSearchColorsCheckBox.setSelected(applicationPreferenceModel.isBypassSearchColors());
+ bypassSearchColorsCheckBox.setSelected(configuration.getBoolean("bypassSearchColors", false));
JPanel globalLabelPanel = new JPanel();
globalLabelPanel.setLayout(new BoxLayout(globalLabelPanel, BoxLayout.X_AXIS));
@@ -267,11 +271,13 @@ public class ColorPanel extends JPanel {
logPanelColorizersModel.addElement(entry.getKey());
}
}
+
+ AbstractConfiguration configuration = SettingsManager.getInstance().getGlobalConfiguration();
//update search and alternating colors, since they may have changed from another color panel
- searchDataVectorEntry.set(0, applicationPreferenceModel.getSearchBackgroundColor());
- searchDataVectorEntry.set(1, applicationPreferenceModel.getSearchForegroundColor());
- alternatingColorDataVectorEntry.set(0, applicationPreferenceModel.getAlternatingColorBackgroundColor());
- alternatingColorDataVectorEntry.set(1, applicationPreferenceModel.getAlternatingColorForegroundColor());
+ searchDataVectorEntry.set(0, configuration.get(Color.class, "searchBackgroundColor", ChainsawConstants.FIND_LOGGER_BACKGROUND));
+ searchDataVectorEntry.set(1, configuration.get(Color.class, "searchForegroundColor", ChainsawConstants.FIND_LOGGER_FOREGROUND));
+ alternatingColorDataVectorEntry.set(0, configuration.get(Color.class, "alternatingColorBackground", ChainsawConstants.COLOR_ODD_ROW_BACKGROUND));
+ alternatingColorDataVectorEntry.set(1, configuration.get(Color.class, "alternatingColorForeground", ChainsawConstants.COLOR_ODD_ROW_FOREGROUND));
}
public JPanel buildBypassSearchColorsPanel() {
diff --git a/src/main/java/org/apache/log4j/chainsaw/color/RuleColorizer.java b/src/main/java/org/apache/log4j/chainsaw/color/RuleColorizer.java
index 5626e89..cf982ec 100644
--- a/src/main/java/org/apache/log4j/chainsaw/color/RuleColorizer.java
+++ b/src/main/java/org/apache/log4j/chainsaw/color/RuleColorizer.java
@@ -30,6 +30,7 @@ import java.io.*;
import java.net.URLEncoder;
import java.util.*;
import java.util.List;
+import org.apache.commons.configuration2.AbstractConfiguration;
import org.apache.log4j.chainsaw.logevents.ChainsawLoggingEvent;
@@ -236,12 +237,19 @@ public class RuleColorizer implements Colorizer {
}
/**
- * Load panel color settings if they exist - otherwise, load default color settings
+ * Load panel color settings if they exist from the given configuration - otherwise, load default color settings
*/
- public void loadColorSettings(String name) {
- if (!doLoadColorSettings(name)) {
- doLoadColorSettings(ChainsawConstants.DEFAULT_COLOR_RULE_NAME);
- }
+ public void loadColorSettings(String name){}
+
+ public void loadColorSettings(AbstractConfiguration configuration) {
+ // When we save/load the rule, we really need to load a map of rules
+ // There's no real good way to do this, so we will do this the somewhat Qt-ish
+ // way and have a key that first tells us how many entries we need to read,
+ // and read the entries based off of their number
+// configuration.getInt( "colors.", 0)
+// if (!doLoadColorSettings(name)) {
+// doLoadColorSettings(ChainsawConstants.DEFAULT_COLOR_RULE_NAME);
+// }
}
private boolean doLoadColorSettings(String name) {
diff --git a/src/main/java/org/apache/log4j/chainsaw/prefs/ProfileManager.java b/src/main/java/org/apache/log4j/chainsaw/prefs/ProfileManager.java
index 0bfaa9f..0a01c3e 100644
--- a/src/main/java/org/apache/log4j/chainsaw/prefs/ProfileManager.java
+++ b/src/main/java/org/apache/log4j/chainsaw/prefs/ProfileManager.java
@@ -30,9 +30,9 @@ public class ProfileManager {
}
public void configure(Profileable p) {
- Properties props = new Properties(SettingsManager.getInstance().getDefaultSettings());
- LoadSettingsEvent event = new LoadSettingsEvent(this, props);
- p.loadSettings(event);
+// Properties props = new Properties(SettingsManager.getInstance().getDefaultSettings());
+// LoadSettingsEvent event = new LoadSettingsEvent(this, props);
+// p.loadSettings(event);
}
public void configure(Profileable p, String profileName) {
diff --git a/src/main/java/org/apache/log4j/chainsaw/prefs/SettingsManager.java b/src/main/java/org/apache/log4j/chainsaw/prefs/SettingsManager.java
index efbb373..a4a6357 100644
--- a/src/main/java/org/apache/log4j/chainsaw/prefs/SettingsManager.java
+++ b/src/main/java/org/apache/log4j/chainsaw/prefs/SettingsManager.java
@@ -20,7 +20,18 @@ import javax.swing.event.EventListenerList;
import java.io.*;
import java.net.URLEncoder;
import java.util.EventListener;
+import java.util.HashMap;
+import java.util.Map;
import java.util.Properties;
+import org.apache.commons.configuration2.AbstractConfiguration;
+import org.apache.commons.configuration2.PropertiesConfiguration;
+import org.apache.commons.configuration2.builder.BasicConfigurationBuilder;
+import org.apache.commons.configuration2.builder.FileBasedConfigurationBuilder;
+import org.apache.commons.configuration2.builder.fluent.Parameters;
+import org.apache.commons.configuration2.convert.DefaultListDelimiterHandler;
+import org.apache.commons.configuration2.ex.ConfigurationException;
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.Logger;
/**
@@ -33,318 +44,107 @@ import java.util.Properties;
public final class SettingsManager {
private static final SettingsManager instance = new SettingsManager();
private static final String GLOBAL_SETTINGS_FILE_NAME = "chainsaw.settings.properties";
- private static final String HEADER = "Chainsaws Settings Files";
- private EventListenerList listenerList = new EventListenerList();
- private Properties defaultProperties = new Properties();
+ private static final Logger logger = LogManager.getLogger();
+
+ private PropertiesConfiguration m_configuration;
+ private FileBasedConfigurationBuilder<PropertiesConfiguration> m_builder;
+ private Map<String,FileBasedConfigurationBuilder<PropertiesConfiguration>> m_tabSettings;
/**
* Initialises the SettingsManager by loading the default Properties from
* a resource
*/
private SettingsManager() {
- // load the default properties as a Resource
- InputStream is = null;
-
- try {
- is = this.getClass().getClassLoader()
- .getResource("org/apache/log4j/chainsaw/prefs/default.properties")
- .openStream();
- defaultProperties.load(is);
-
- // defaultProperties.list(System.out);
- is.close();
- } catch (IOException e) {
- e.printStackTrace();
- } finally {
- if (is != null) {
- try {
- is.close();
- } catch (Exception e) {
- }
- }
- }
- }
-
- /**
- * Returns the singleton instance of the SettingsManager
- *
- * @return settings manager
- */
- public static SettingsManager getInstance() {
- return instance;
- }
-
- /**
- * Registers the listener with the manager
- *
- * @param listener
- */
- public void addSettingsListener(SettingsListener listener) {
- listenerList.add(SettingsListener.class, listener);
- }
+ m_tabSettings = new HashMap<>();
+ Parameters params = new Parameters();
+ File f = new File(getSettingsDirectory(), GLOBAL_SETTINGS_FILE_NAME);
- /**
- * Requests that the settings be loaded, all listeners will be notified of
- * this call, and configure themselves according to the values found in the
- * loaded settings
- */
- public void loadSettings() {
- /*
- * Ok, note we ensure we have a .chainsaw directory in the users
- * home folder, and create a chainsaw.settings.properties file..
- */
File settingsDir = getSettingsDirectory();
if (!settingsDir.exists()) {
settingsDir.mkdir();
}
- loadGlobalSettings();
- loadProfileableSettings();
- }
-
- /**
- *
- */
- private void loadProfileableSettings() {
- EventListener[] listeners = listenerList.getListeners(SettingsListener.class);
-
- for (EventListener listener : listeners) {
- SettingsListener settingsListener = (SettingsListener) listener;
-
- if (settingsListener instanceof Profileable) {
- Profileable p = (Profileable) settingsListener;
- loadProfileble(p);
- }
+ m_builder =
+ new FileBasedConfigurationBuilder<PropertiesConfiguration>(
+ PropertiesConfiguration.class)
+ .configure(params.fileBased()
+ .setListDelimiterHandler(
+ new DefaultListDelimiterHandler(','))
+ .setFile(f)
+ );
+
+
+ try{
+ PropertiesConfiguration config = m_builder.getConfiguration();
+ m_configuration = config;
+ }catch( ConfigurationException ex ){
+ logger.error( "Can't load properties??" );
}
}
- private void loadProfileble(Profileable p) {
- LoadSettingsEvent event = createProfilebleEvent(p);
- p.loadSettings(event);
- }
-
- private LoadSettingsEvent createProfilebleEvent(Profileable p) {
- Properties loadedProperties = new Properties();
- loadedProperties.putAll(getDefaultSettings());
- loadedProperties.putAll(loadProperties(p));
-
-
- LoadSettingsEvent event = new LoadSettingsEvent(this, loadedProperties);
-
- return event;
- }
-
/**
- * @param p
- * @return
+ * Returns the singleton instance of the SettingsManager
+ *
+ * @return settings manager
*/
- private Properties loadProperties(Profileable p) {
- Properties properties = new Properties(defaultProperties);
- InputStream is = null;
-
- File f = new File(getSettingsDirectory(),
- URLEncoder.encode(p.getNamespace() + ".properties"));
-
- if (!f.exists()) {
- f = new File(getSettingsDirectory(),
- p.getNamespace() + ".properties");
- }
-
- if (f.exists()) {
- try {
- is = new BufferedInputStream(new FileInputStream(f));
-
- Properties toLoad = new Properties();
- toLoad.load(is);
- properties.putAll(toLoad);
- } catch (IOException ioe) {
- ioe.printStackTrace();
- } finally {
- if (is != null) {
- try {
- is.close();
- } catch (IOException e1) {
- e1.printStackTrace();
- }
- }
- }
- }
-
- return properties;
- }
-
- private void loadGlobalSettings() {
- EventListener[] listeners = listenerList.getListeners(SettingsListener.class);
- LoadSettingsEvent event = null;
-
- for (EventListener listener : listeners) {
- SettingsListener settingsListener = (SettingsListener) listener;
-
- if (event == null) {
- Properties loadedProperties = loadGlobalProperties();
-
- // loadedProperties.list(System.out);
- event = new LoadSettingsEvent(this, loadedProperties);
- }
-
- settingsListener.loadSettings(event);
- }
+ public static SettingsManager getInstance() {
+ return instance;
}
- /**
- * Creates a SaveSettingsEvent and calls all the SettingsListeners
- * to populate the properties with configuration information
- */
- public void saveSettings() {
- /*
- * Ok, note we ensure we have a .chainsaw directory in the users
- * home folder, and create a chainsaw.settings.properties file..
- */
- File settingsDir = getSettingsDirectory();
-
- if (!settingsDir.exists()) {
- settingsDir.mkdir();
- }
-
- saveGlobalSettings(settingsDir);
- saveProfileableSetting(settingsDir);
+ public AbstractConfiguration getGlobalConfiguration(){
+ return m_configuration;
}
- /**
- * Looks up all the Profileable's that have been registered
- * and creates a new event for each of them, and ensures that they
- * are saved within a separate external store
- *
- * @param settingsDir
- */
- private void saveProfileableSetting(File settingsDir) {
- EventListener[] listeners = listenerList.getListeners(SettingsListener.class);
- SaveSettingsEvent event;
-
- for (EventListener listener : listeners) {
- SettingsListener settingsListener = (SettingsListener) listener;
-
- if (settingsListener instanceof Profileable) {
- Profileable profileable = (Profileable) settingsListener;
- event = new SaveSettingsEvent(this, getSettingsDirectory());
+ public AbstractConfiguration getSettingsForReceiverTab(String identifier){
- profileable.saveSettings(event);
+ PropertiesConfiguration configuration = null;
- OutputStream os = null;
-
- try {
- os = new BufferedOutputStream(new FileOutputStream(
- new File(settingsDir,
- URLEncoder.encode(profileable.getNamespace()) + ".properties")));
- event.getProperties().store(os, HEADER);
- } catch (Exception e) {
- e.printStackTrace();
- } finally {
- if (os != null) {
- try {
- os.close();
- } catch (IOException e1) {
- e1.printStackTrace();
- }
- }
- }
- }
+ if( m_tabSettings.containsKey( identifier ) ){
+ try{
+ return m_tabSettings.get( identifier ).getConfiguration();
+ }catch( ConfigurationException ex ){}
}
- }
-
- private void saveGlobalSettings(File settingsDir) {
- EventListener[] listeners = listenerList.getListeners(SettingsListener.class);
- SaveSettingsEvent event = null;
-
- for (EventListener listener : listeners) {
- SettingsListener settingsListener = (SettingsListener) listener;
-
- if (!(settingsListener instanceof Profileable)) {
- if (event == null) {
- event = new SaveSettingsEvent(this, getSettingsDirectory());
- }
- settingsListener.saveSettings(event);
- }
- }
+ // Either we don't contain the key, or we got an exception. Regardless,
+ // create a new configuration that we can use
+ Parameters params = new Parameters();
+ File f = new File(getSettingsDirectory(), identifier + "-receiver.properties");
+ FileBasedConfigurationBuilder<PropertiesConfiguration> builder =
+ new FileBasedConfigurationBuilder<PropertiesConfiguration>(
+ PropertiesConfiguration.class)
+ .configure(params.fileBased()
+ .setListDelimiterHandler(
+ new DefaultListDelimiterHandler(','))
+ .setFile(f)
+ );
- OutputStream os = null;
+ m_tabSettings.put( identifier, builder );
- try {
- os = new BufferedOutputStream(new FileOutputStream(
- new File(settingsDir, GLOBAL_SETTINGS_FILE_NAME)));
- event.getProperties().store(os, HEADER);
- } catch (Exception e) {
- e.printStackTrace();
- } finally {
- if (os != null) {
- try {
- os.close();
- } catch (IOException e1) {
- e1.printStackTrace();
- }
- }
- }
+ try{
+ return builder.getConfiguration();
+ }catch( ConfigurationException ex ){}
+
+ return null;
}
public File getSettingsDirectory() {
return new File(System.getProperty("user.home"), ".chainsaw");
}
- public void configure(SettingsListener listener) {
- if (listener instanceof Profileable) {
- loadProfileble((Profileable) listener);
- } else {
- Properties loadedProperties = loadGlobalProperties();
- LoadSettingsEvent event = new LoadSettingsEvent(this,
- loadedProperties);
- listener.loadSettings(event);
+ public void saveAllSettings(){
+ try{
+ m_builder.save();
+ }catch( ConfigurationException ex ){
+ logger.error( "Unable to save global settings: {}", ex );
}
- }
-
- /**
- * Returns the current Properties settings for this user
- * by merging the default Properties with the ones we find in their directory.
- *
- * @return
- */
- private Properties loadGlobalProperties() {
- Properties properties = new Properties(defaultProperties);
- InputStream is = null;
-
- File f = new File(getSettingsDirectory(), GLOBAL_SETTINGS_FILE_NAME);
-
- if (f.exists()) {
- try {
- is = new BufferedInputStream(new FileInputStream(f));
- Properties toLoad = new Properties();
- toLoad.load(is);
- properties.putAll(toLoad);
- } catch (IOException ioe) {
- ioe.printStackTrace();
- } finally {
- if (is != null) {
- try {
- is.close();
- } catch (IOException e1) {
- e1.printStackTrace();
- }
- }
+ for( String key : m_tabSettings.keySet() ){
+ try{
+ m_tabSettings.get(key).save();
+ }catch( ConfigurationException ex ){
+ logger.error( "Unable to save settings for {}", key );
}
}
-
- return properties;
- }
-
- /**
- * Returns the loaded default settings, which can be used by
- * other classes within this package.
- *
- * @return Properties defaults
- */
- public Properties getDefaultSettings() {
- return defaultProperties;
}
}