Rework colors to load from config file
diff --git a/src/main/java/org/apache/log4j/chainsaw/ApplicationPreferenceModelPanel.java b/src/main/java/org/apache/log4j/chainsaw/ApplicationPreferenceModelPanel.java
index 45e38db..d88167c 100644
--- a/src/main/java/org/apache/log4j/chainsaw/ApplicationPreferenceModelPanel.java
+++ b/src/main/java/org/apache/log4j/chainsaw/ApplicationPreferenceModelPanel.java
@@ -447,6 +447,7 @@
sliderLabelMap.put(3, new JLabel(" Medium "));
sliderLabelMap.put(4, new JLabel(" Slow "));
+
confirmExit.setSelected(m_globalConfiguration.getBoolean("confirmExit", Boolean.TRUE));
showSplash.setSelected(m_globalConfiguration.getBoolean("showSplash", Boolean.TRUE));
toolTipDisplayMillis.setText(m_globalConfiguration.getInt( "toolTipDisplayMillis", 1000 ) + "");
diff --git a/src/main/java/org/apache/log4j/chainsaw/LogPanel.java b/src/main/java/org/apache/log4j/chainsaw/LogPanel.java
index 3b4c28b..df76989 100644
--- a/src/main/java/org/apache/log4j/chainsaw/LogPanel.java
+++ b/src/main/java/org/apache/log4j/chainsaw/LogPanel.java
@@ -143,7 +143,8 @@
private ApplicationPreferenceModel applicationPreferenceModel;
private final LogPanelPreferencePanel logPanelPreferencesPanel;
private final FilterModel filterModel = new FilterModel();
- private final RuleColorizer colorizer = new RuleColorizer();
+ private RuleColorizer colorizer;
+ private final RuleColorizer m_globalColorizer;
private final RuleMediator tableRuleMediator = new RuleMediator(false);
private final RuleMediator searchRuleMediator = new RuleMediator(true);
private final EventDetailLayout detailLayout = new EventDetailLayout();
@@ -183,6 +184,7 @@
private boolean isDetailPanelVisible;
private ChainsawReceiver m_receiver;
private AbstractConfiguration m_configuration;
+ private Map<String, RuleColorizer> m_allColorizers;
/**
* Creates a new LogPanel object. If a LogPanel with this identifier has
@@ -195,14 +197,18 @@
final String identifier,
int cyclicBufferSize,
Map<String, RuleColorizer> allColorizers,
- final ApplicationPreferenceModel applicationPreferenceModel) {
+ final ApplicationPreferenceModel applicationPreferenceModel,
+ RuleColorizer globalRuleColorizer) {
this.identifier = identifier;
this.statusBar = statusBar;
this.applicationPreferenceModel = applicationPreferenceModel;
this.logPanelPreferencesPanel = new LogPanelPreferencePanel(preferenceModel, applicationPreferenceModel);
+ this.colorizer = globalRuleColorizer;
+ this.m_globalColorizer = globalRuleColorizer;
+ this.m_allColorizers = allColorizers;
logger.debug("creating logpanel for {}", identifier);
- m_configuration = SettingsManager.getInstance().getSettingsForReceiverTab(identifier);
+ m_configuration = SettingsManager.getInstance().getCombinedSettingsForRecevierTab(identifier);
setLayout(new BorderLayout());
@@ -709,7 +715,7 @@
((ImageIcon) ChainsawIcons.ICON_PREFERENCES).getImage());
allColorizers.put(identifier, colorizer);
- colorPanel = new ColorPanel(colorizer, filterModel, allColorizers, applicationPreferenceModel);
+ colorPanel = new ColorPanel(m_globalColorizer, filterModel, allColorizers, this);
colorFrame.getContentPane().add(colorPanel);
@@ -1499,7 +1505,7 @@
Color c = JColorChooser.showDialog(getRootPane(), "Choose a color", Color.red);
if (c != null) {
String expression = columnNameKeywordMap.get(colName).toString() + " " + operator + " '" + value + "'";
- colorizer.addRule(ChainsawConstants.DEFAULT_COLOR_RULE_NAME, new ColorRule(expression,
+ colorizer.addRule(new ColorRule(expression,
ExpressionRule.getRule(expression), c, ChainsawConstants.COLOR_DEFAULT_FOREGROUND));
}
}
@@ -2063,7 +2069,15 @@
}
}
- colorizer.loadColorSettings(config);
+ AbstractConfiguration configuration = SettingsManager.getInstance().getSettingsForReceiverTab(identifier);
+
+ if( configuration.getBoolean( "color.rules.default", true ) ){
+ colorizer = m_globalColorizer;
+ }else{
+ setRuleColorizer(new RuleColorizer());
+ colorizer.setConfiguration(configuration);
+ colorizer.loadColorSettings();
+ }
// if (xmlFile.exists()) {
@@ -2251,6 +2265,29 @@
return stream;
}
+ public void setRuleColorizer(RuleColorizer newRuleColor){
+ if( newRuleColor == colorizer ){
+ return;
+ }
+
+ if( colorizer == m_globalColorizer ){
+ // Current colorizer is the global one, so set our new colorizer
+ colorizer = newRuleColor;
+ AbstractConfiguration configuration = SettingsManager.getInstance().getSettingsForReceiverTab(identifier);
+
+ colorizer.setConfiguration(configuration);
+ colorizer.setUseDefaultSettings(false);
+ m_allColorizers.put( identifier, colorizer );
+ }else{
+ m_allColorizers.remove( identifier );
+ colorizer = newRuleColor;
+ }
+ }
+
+ public RuleColorizer getCurrentRuleColorizer(){
+ return colorizer;
+ }
+
/**
* Display the panel preferences frame
*/
diff --git a/src/main/java/org/apache/log4j/chainsaw/LogUI.java b/src/main/java/org/apache/log4j/chainsaw/LogUI.java
index d7382fb..ff90d58 100644
--- a/src/main/java/org/apache/log4j/chainsaw/LogUI.java
+++ b/src/main/java/org/apache/log4j/chainsaw/LogUI.java
@@ -145,6 +145,7 @@
//map of tab names to rulecolorizers
private Map<String, RuleColorizer> allColorizers = new HashMap<>();
+ private RuleColorizer globalRuleColorizer = new RuleColorizer(true);
private ReceiverConfigurationPanel receiverConfigurationPanel = new ReceiverConfigurationPanel();
/**
@@ -155,6 +156,9 @@
super("Chainsaw");
setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
+ globalRuleColorizer.setConfiguration(SettingsManager.getInstance().getGlobalConfiguration());
+ globalRuleColorizer.loadColorSettings();
+
if (ChainsawIcons.WINDOW_ICON != null) {
setIconImage(new ImageIcon(ChainsawIcons.WINDOW_ICON).getImage());
}
@@ -249,6 +253,7 @@
public static void createChainsawGUI(Action newShutdownAction) {
AbstractConfiguration config = SettingsManager.getInstance().getGlobalConfiguration();
+
if (config.getBoolean("okToRemoveSecurityManager", false)) {
// statusBar.setMessage("User has authorised removal of Java Security Manager via preferences");
System.setSecurityManager(null);
@@ -506,7 +511,6 @@
getToolBarAndMenus().stateChange();
RuleColorizer colorizer = new RuleColorizer();
- colorizer.loadColorSettings(ChainsawConstants.DEFAULT_COLOR_RULE_NAME);
allColorizers.put(ChainsawConstants.DEFAULT_COLOR_RULE_NAME, colorizer);
}
@@ -1627,7 +1631,7 @@
private void buildLogPanel(
boolean customExpression, final String ident, final List<ChainsawLoggingEvent> events, final ChainsawReceiver rx)
throws IllegalArgumentException {
- final LogPanel thisPanel = new LogPanel(getStatusBar(), ident, cyclicBufferSize, allColorizers, applicationPreferenceModel);
+ final LogPanel thisPanel = new LogPanel(getStatusBar(), ident, cyclicBufferSize, allColorizers, applicationPreferenceModel, globalRuleColorizer);
if( !customExpression && rx != null ){
thisPanel.setReceiver(rx);
diff --git a/src/main/java/org/apache/log4j/chainsaw/LoggerNameTreePanel.java b/src/main/java/org/apache/log4j/chainsaw/LoggerNameTreePanel.java
index ff798d2..26a1cae 100644
--- a/src/main/java/org/apache/log4j/chainsaw/LoggerNameTreePanel.java
+++ b/src/main/java/org/apache/log4j/chainsaw/LoggerNameTreePanel.java
@@ -870,7 +870,7 @@
Color c = JColorChooser.showDialog(getRootPane(), "Choose a color", Color.red);
if (c != null) {
String expression = "logger like '^" + selectedLogger + ".*'";
- colorizer.addRule(ChainsawConstants.DEFAULT_COLOR_RULE_NAME, new ColorRule(expression,
+ colorizer.addRule(new ColorRule(expression,
ExpressionRule.getRule(expression), c, ChainsawConstants.COLOR_DEFAULT_FOREGROUND));
}
}
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 136c874..9c6484e 100644
--- a/src/main/java/org/apache/log4j/chainsaw/color/ColorPanel.java
+++ b/src/main/java/org/apache/log4j/chainsaw/color/ColorPanel.java
@@ -43,6 +43,7 @@
import javax.swing.table.TableCellEditor;
import javax.swing.table.TableModel;
import org.apache.commons.configuration2.AbstractConfiguration;
+import org.apache.log4j.chainsaw.LogPanel;
import org.apache.log4j.chainsaw.prefs.SettingsManager;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
@@ -61,7 +62,7 @@
private static final String DEFAULT_STATUS = "<html>Double click a rule field to edit the rule</html>";
private final String currentRuleSet = "Default";
- private RuleColorizer colorizer;
+// private RuleColorizer colorizer;
private JPanel rulesPanel;
private FilterModel filterModel;
private RulesTableModel rulesTableModel;
@@ -73,34 +74,38 @@
private final String noTab = "None";
private DefaultComboBoxModel logPanelColorizersModel;
private Map<String, RuleColorizer> allLogPanelColorizers;
- private RuleColorizer currentLogPanelColorizer;
+// private RuleColorizer currentLogPanelColorizer;
private JTable searchTable;
private DefaultTableModel searchTableModel;
private Vector<String> searchColumns;
private Vector<Vector<Color>> searchDataVector;
private Vector<Color> searchDataVectorEntry;
+ private RuleColorizer m_globalColorizer;
private JTable alternatingColorTable;
private DefaultTableModel alternatingColorTableModel;
private Vector<String> alternatingColorColumns;
private Vector<Vector<Color>> alternatingColorDataVector;
private Vector<Color> alternatingColorDataVectorEntry;
- private ApplicationPreferenceModel applicationPreferenceModel;
private JCheckBox bypassSearchColorsCheckBox;
+ private JCheckBox m_globalRulesCheckbox;
+ private LogPanel m_parentLogPanel;
+ private JLabel m_rulesLabel;
- public ColorPanel(final RuleColorizer currentLogPanelColorizer, final FilterModel filterModel,
- final Map<String, RuleColorizer> allLogPanelColorizers, final ApplicationPreferenceModel applicationPreferenceModel) {
+ public ColorPanel(final RuleColorizer globalColorizer,
+ final FilterModel filterModel,
+ final Map<String, RuleColorizer> allLogPanelColorizers,
+ final LogPanel parent) {
super(new BorderLayout());
AbstractConfiguration configuration = SettingsManager.getInstance().getGlobalConfiguration();
- this.currentLogPanelColorizer = currentLogPanelColorizer;
- this.colorizer = currentLogPanelColorizer;
this.filterModel = filterModel;
this.allLogPanelColorizers = allLogPanelColorizers;
- this.applicationPreferenceModel = applicationPreferenceModel;
+ this.m_globalColorizer = globalColorizer;
+ this.m_parentLogPanel = parent;
- currentLogPanelColorizer.addPropertyChangeListener(
+ m_parentLogPanel.getCurrentRuleColorizer().addPropertyChangeListener(
"colorrule",
evt -> updateColors());
@@ -234,8 +239,13 @@
Object selectedItem = loadPanelColorizersComboBox.getSelectedItem();
if (selectedItem != null) {
RuleColorizer sourceColorizer = allLogPanelColorizers.get(selectedItem.toString());
- colorizer.setRules(sourceColorizer.getRules());
+ RuleColorizer newColorizer = new RuleColorizer();
+ newColorizer.setRules(sourceColorizer.getRules());
+ parent.setRuleColorizer(newColorizer);
updateColors();
+ m_parentLogPanel.getCurrentRuleColorizer().addPropertyChangeListener(
+ "colorrule",
+ evt -> updateColors());
}
}
};
@@ -265,6 +275,7 @@
if (logPanelColorizersModel.getIndexOf(noTab) == -1) {
logPanelColorizersModel.addElement(noTab);
}
+ RuleColorizer currentLogPanelColorizer = m_parentLogPanel.getCurrentRuleColorizer();
for (Object o : allLogPanelColorizers.entrySet()) {
Map.Entry entry = (Map.Entry) o;
if (!entry.getValue().equals(currentLogPanelColorizer) && (logPanelColorizersModel.getIndexOf(entry.getKey()) == -1)) {
@@ -319,7 +330,7 @@
public void updateColors() {
rulesTableModel.clear();
- rulesTableModel.addDefaultRules();
+ rulesTableModel.addColorRules();
rulesTableModel.fireTableDataChanged();
}
@@ -329,8 +340,8 @@
rulesTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
rulesTable.setColumnSelectionAllowed(false);
- Vector backgroundColors = colorizer.getDefaultColors();
- Vector foregroundColors = colorizer.getDefaultColors();
+ Vector backgroundColors = new Vector(RuleColorizer.getDefaultColors());
+ Vector foregroundColors = new Vector(RuleColorizer.getDefaultColors());
backgroundColors.add("Browse...");
foregroundColors.add("Browse...");
@@ -367,8 +378,8 @@
thisTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
thisTable.setColumnSelectionAllowed(false);
- Vector backgroundColors = colorizer.getDefaultColors();
- Vector foregroundColors = colorizer.getDefaultColors();
+ Vector backgroundColors = new Vector(RuleColorizer.getDefaultColors());
+ Vector foregroundColors = new Vector(RuleColorizer.getDefaultColors());
backgroundColors.add("Browse...");
foregroundColors.add("Browse...");
@@ -445,10 +456,7 @@
statusBar.setText(DEFAULT_STATUS);
//only update rules if there were no errors
- Map map = new HashMap();
- List<ColorRule> newRules = new ArrayList<>(rulesTableModel.rules());
- map.put(ruleSet, newRules);
- applyingColorizer.setRules(map);
+ applyingColorizer.setRules(rulesTableModel.rules());
} else {
statusBar.setText("Errors - see expression tooltip (color filters won't be active until errors are resolved)");
@@ -462,24 +470,24 @@
panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS));
panel.add(Box.createHorizontalGlue());
- JButton saveAsDefaultButton = new JButton(" Save as default ");
-
- saveAsDefaultButton.addActionListener(
- new AbstractAction() {
- public void actionPerformed(ActionEvent evt) {
- RuleColorizer defaultColorizer = allLogPanelColorizers.get(ChainsawConstants.DEFAULT_COLOR_RULE_NAME);
- applyRules(currentRuleSet, defaultColorizer);
- }
- });
-
- panel.add(saveAsDefaultButton);
+// JButton saveAsDefaultButton = new JButton(" Save as default ");
+//
+// saveAsDefaultButton.addActionListener(
+// new AbstractAction() {
+// public void actionPerformed(ActionEvent evt) {
+// RuleColorizer defaultColorizer = allLogPanelColorizers.get(ChainsawConstants.DEFAULT_COLOR_RULE_NAME);
+// applyRules(currentRuleSet, defaultColorizer);
+// }
+// });
+//
+// panel.add(saveAsDefaultButton);
JButton applyButton = new JButton(" Apply ");
applyButton.addActionListener(
new AbstractAction() {
public void actionPerformed(ActionEvent evt) {
- applyRules(currentRuleSet, colorizer);
+ applyRules(currentRuleSet, m_parentLogPanel.getCurrentRuleColorizer());
saveSearchColors();
saveAlternatingColors();
saveBypassFlag();
@@ -505,19 +513,20 @@
private void saveSearchColors() {
Vector thisVector = (Vector) searchTableModel.getDataVector().get(0);
- applicationPreferenceModel.setSearchBackgroundColor((Color) thisVector.get(0));
- applicationPreferenceModel.setSearchForegroundColor((Color) thisVector.get(1));
+ AbstractConfiguration globalConfig = SettingsManager.getInstance().getGlobalConfiguration();
+ globalConfig.setProperty("searchBackgroundColor", RuleColorizer.colorToRGBString((Color) thisVector.get(0)));
+ globalConfig.setProperty("searchForegroundColor", RuleColorizer.colorToRGBString((Color) thisVector.get(1)));
}
private void saveAlternatingColors() {
Vector thisVector = (Vector) alternatingColorTableModel.getDataVector().get(0);
- applicationPreferenceModel.setAlternatingBackgroundColor((Color) thisVector.get(0));
+// applicationPreferenceModel.setAlternatingBackgroundColor((Color) thisVector.get(0));
Color alternatingColorForegroundColor = (Color) thisVector.get(1);
- applicationPreferenceModel.setAlternatingForegroundColor(alternatingColorForegroundColor);
+// applicationPreferenceModel.setAlternatingForegroundColor(alternatingColorForegroundColor);
}
private void saveBypassFlag() {
- applicationPreferenceModel.setBypassSearchColors(bypassSearchColorsCheckBox.isSelected());
+// applicationPreferenceModel.setBypassSearchColors(bypassSearchColorsCheckBox.isSelected());
}
JPanel buildUpDownPanel() {
@@ -596,9 +605,34 @@
panel.add(Box.createVerticalStrut(10));
- JLabel rulesLabel = new JLabel("Rules:");
+ m_rulesLabel = new JLabel("Rules:");
+ m_globalRulesCheckbox = new JCheckBox("Use global rules");
- panel.add(rulesLabel);
+ if( m_globalColorizer == m_parentLogPanel.getCurrentRuleColorizer() ){
+ m_globalRulesCheckbox.setSelected(true);
+ m_rulesLabel.setText("Global rules:");
+ }
+
+ m_globalRulesCheckbox.addActionListener(
+ new AbstractAction() {
+ @Override
+ public void actionPerformed(ActionEvent ae) {
+ if( m_globalRulesCheckbox.isSelected() ){
+ m_parentLogPanel.setRuleColorizer( m_globalColorizer );
+ m_rulesLabel.setText( "Global rules:" );
+ }else{
+ m_parentLogPanel.setRuleColorizer(new RuleColorizer());
+ m_rulesLabel.setText("Rules:");
+ }
+ updateColors();
+ m_parentLogPanel.getCurrentRuleColorizer().addPropertyChangeListener(
+ "colorrule",
+ evt -> updateColors());
+ }
+ });
+
+ panel.add(m_globalRulesCheckbox);
+ panel.add(m_rulesLabel);
JPanel buttonPanel = new JPanel(new GridLayout(0, 2));
buttonPanel.setAlignmentX(Component.LEFT_ALIGNMENT);
@@ -851,7 +885,7 @@
RulesTableModel(){
m_data = new ArrayList<>();
- addDefaultRules();
+ addColorRules();
}
List<ColorRule> rules(){
@@ -890,16 +924,8 @@
}
}
- void addDefaultRules(){
- Map map = colorizer.getRules();
- for (Object o1 : map.entrySet()) {
- Map.Entry entry = (Map.Entry) o1;
- logger.debug( "entry: {}", o1 );
- //update ruleset list
- if (entry.getKey().equals(currentRuleSet)) {
- m_data.addAll( (List<ColorRule>)entry.getValue() );
- }
- }
+ void addColorRules(){
+ m_data.addAll( m_parentLogPanel.getCurrentRuleColorizer().getRules() );
}
@Override
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 aa197cf..44e17bb 100644
--- a/src/main/java/org/apache/log4j/chainsaw/color/RuleColorizer.java
+++ b/src/main/java/org/apache/log4j/chainsaw/color/RuleColorizer.java
@@ -31,7 +31,10 @@
import java.util.*;
import java.util.List;
import org.apache.commons.configuration2.AbstractConfiguration;
+import org.apache.commons.configuration2.DataConfiguration;
import org.apache.log4j.chainsaw.logevents.ChainsawLoggingEvent;
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.Logger;
/**
@@ -41,25 +44,28 @@
* @author Scott Deboy <sdeboy@apache.org>
*/
public class RuleColorizer implements Colorizer {
- private Map<String,List<ColorRule>> rules;
+ private static final Logger logger = LogManager.getLogger();
+
+ private List<ColorRule> rules;
private final PropertyChangeSupport colorChangeSupport =
new PropertyChangeSupport(this);
- private Map<String,List<ColorRule>> defaultRules = new HashMap<>();
- private String currentRuleSet = ChainsawConstants.DEFAULT_COLOR_RULE_NAME;
+ private List<ColorRule> defaultRules = new ArrayList<>();
private Rule findRule;
private Rule loggerRule;
+ private AbstractConfiguration m_configuration;
+ private boolean m_isGlobal;
private static final String COLORS_EXTENSION = ".colors";
- private final Color WARN_DEFAULT_COLOR = new Color(255, 255, 153);
- private final Color FATAL_OR_ERROR_DEFAULT_COLOR = new Color(255, 153, 153);
- private final Color MARKER_DEFAULT_COLOR = new Color(153, 255, 153);
+ private static final Color WARN_DEFAULT_COLOR = new Color(255, 255, 153);
+ private static final Color FATAL_OR_ERROR_DEFAULT_COLOR = new Color(255, 153, 153);
+ private static final Color MARKER_DEFAULT_COLOR = new Color(153, 255, 153);
- private final String DEFAULT_WARN_EXPRESSION = "level == WARN";
- private final String DEFAULT_FATAL_ERROR_EXCEPTION_EXPRESSION = "level == FATAL || level == ERROR || exception exists";
- private final String DEFAULT_MARKER_EXPRESSION = "prop.marker exists";
+ private static final String DEFAULT_WARN_EXPRESSION = "level == WARN";
+ private static final String DEFAULT_FATAL_ERROR_EXCEPTION_EXPRESSION = "level == FATAL || level == ERROR || exception exists";
+ private static final String DEFAULT_MARKER_EXPRESSION = "prop.marker exists";
- public RuleColorizer() {
+ public static List<ColorRule> defaultRules(){
List<ColorRule> rulesList = new ArrayList<>();
String expression = DEFAULT_FATAL_ERROR_EXCEPTION_EXPRESSION;
@@ -79,8 +85,17 @@
expression, ExpressionRule.getRule(expression), MARKER_DEFAULT_COLOR,
Color.black));
- defaultRules.put(currentRuleSet, rulesList);
- setRules(defaultRules);
+ return rulesList;
+ }
+
+ public RuleColorizer() {
+ this.rules = defaultRules();
+ m_isGlobal = false;
+ }
+
+ public RuleColorizer(boolean isGlobal){
+ this.rules = defaultRules();
+ m_isGlobal = isGlobal;
}
public void setLoggerRule(Rule loggerRule) {
@@ -101,74 +116,43 @@
return loggerRule;
}
- public void setRules(Map<String,List<ColorRule>> rules) {
- this.rules = rules;
+ public void setRules(List<ColorRule> rules) {
+ this.rules.clear();
+ this.rules.addAll(rules);
colorChangeSupport.firePropertyChange("colorrule", false, true);
+
+ saveColorSettings();
}
- public Map<String,List<ColorRule>> getRules() {
+ public List<ColorRule> getRules() {
return rules;
}
- public List<ColorRule> getCurrentRules() {
- return rules.get(currentRuleSet);
- }
-
- public void addRules(Map<String,List<ColorRule>> newRules) {
-
- for (Map.Entry<String,List<ColorRule>> entry : newRules.entrySet()) {
-
- if (rules.containsKey(entry.getKey())) {
- rules.get(entry.getKey()).addAll( entry.getValue() );
- } else {
- rules.put(entry.getKey(), entry.getValue());
- }
- }
+ public void addRules(List<ColorRule> newRules) {
+ rules.addAll( newRules );
colorChangeSupport.firePropertyChange("colorrule", false, true);
+
+ saveColorSettings();
}
- public void addRule(String ruleSetName, ColorRule rule) {
- if (rules.containsKey(ruleSetName)) {
- rules.get(ruleSetName).add(rule);
- } else {
- List<ColorRule> list = new ArrayList<>();
- list.add(rule);
- rules.put(ruleSetName, list);
- }
+ public void addRule(ColorRule rule) {
+ rules.add(rule);
colorChangeSupport.firePropertyChange("colorrule", false, true);
+
+ saveColorSettings();
}
- public void removeRule(String ruleSetName, String expression) {
- if (rules.containsKey(ruleSetName)) {
- List<ColorRule> list = rules.get(ruleSetName);
-
- for (int i = 0; i < list.size(); i++) {
- ColorRule rule = list.get(i);
-
- if (rule.getExpression().equals(expression)) {
- list.remove(rule);
-
- return;
- }
- }
- }
- }
-
- public void setCurrentRuleSet(String ruleSetName) {
- currentRuleSet = ruleSetName;
+ public void removeRule(String expression) {
+ rules.removeIf( x -> x.getExpression().equals( expression ) );
}
public Color getBackgroundColor(ChainsawLoggingEvent event) {
- if (rules.containsKey(currentRuleSet)) {
- List<ColorRule> list = rules.get(currentRuleSet);
+ for (ColorRule rule : rules) {
- for (ColorRule rule : list) {
-
- if ((rule.getBackgroundColor() != null) && (rule.evaluate(event, null))) {
- return rule.getBackgroundColor();
- }
+ if ((rule.getBackgroundColor() != null) && (rule.evaluate(event, null))) {
+ return rule.getBackgroundColor();
}
}
@@ -176,14 +160,10 @@
}
public Color getForegroundColor(ChainsawLoggingEvent event) {
- if (rules.containsKey(currentRuleSet)) {
- List<ColorRule> list = rules.get(currentRuleSet);
+ for (ColorRule rule : rules) {
- for (ColorRule rule : list) {
-
- if ((rule.getForegroundColor() != null) && (rule.evaluate(event, null))) {
- return rule.getForegroundColor();
- }
+ if ((rule.getForegroundColor() != null) && (rule.evaluate(event, null))) {
+ return rule.getForegroundColor();
}
}
@@ -207,65 +187,94 @@
colorChangeSupport.addPropertyChangeListener(propertyName, listener);
}
-
- /**
- * Save panel color settings
- */
- public void saveColorSettings(String name) {
- ObjectOutputStream o = null;
- try {
- File f = new File(SettingsManager.getInstance().getSettingsDirectory(), URLEncoder.encode(name + COLORS_EXTENSION));
-
- o = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream(f)));
-
- o.writeObject(getRules());
- o.flush();
- } catch (IOException ioe) {
- ioe.printStackTrace();
- } finally {
- try {
- if (o != null) {
- o.close();
- }
- } catch (IOException ioe) {
- ioe.printStackTrace();
- }
- }
+ public void setConfiguration(AbstractConfiguration configuration){
+ m_configuration = configuration;
}
- /**
- * Load panel color settings if they exist from the given configuration - otherwise, load default color settings
- */
- public void loadColorSettings(String name){}
+ public void setUseDefaultSettings(boolean useDefaultSettings){
+ if( m_configuration == SettingsManager.getInstance().getGlobalConfiguration() ){
+ return;
+ }
+
+ m_configuration.setProperty( "color.rules.default", useDefaultSettings );
+ }
+
+ public static String colorToRGBString( Color c ){
+ return String.format( "#%02x%02x%02x",
+ c.getRed(),
+ c.getGreen(),
+ c.getBlue());
+ }
+
+ private void saveColorSettings(){
+ if( m_configuration == null ){
+ return;
+ }
+
+ DataConfiguration data = new DataConfiguration(m_configuration);
+
+ if( !m_isGlobal && m_configuration.getBoolean( "color.rules.default", true ) ){
+ // No need to save, using the default rules
+ return;
+ }
+
+ for( int x = 0; x < 32; x++ ){
+ String baseConfigKey = "color.rules(" + x + ")";
+
+ if( rules.size() <= x ){
+ break;
+ }
+
+ ColorRule rule = rules.get(x);
+
+ logger.debug( "Saving rule {}. Expression: {}", x, rule.getExpression() );
+
+ m_configuration.setProperty( baseConfigKey + ".expression", rule.getExpression() );
+ String bgColorString = colorToRGBString(rule.getBackgroundColor());
+ String fgColorString = colorToRGBString(rule.getForegroundColor());
+ data.setProperty( baseConfigKey + ".backgroundColor", bgColorString );
+ data.setProperty( baseConfigKey + ".foregroundColor", fgColorString );
+ }
+
+ logger.debug( "all keys for {}:", m_configuration );
+ java.util.Iterator<String> s = m_configuration.getKeys();
+ while( s.hasNext() ){
+ logger.debug( "found key: {}", s.next() );
+ }
+ }
- public void loadColorSettings(AbstractConfiguration configuration) {
+ public void loadColorSettings() {
// 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
// dumb way and just load up to 32 color rules, since that seems like
// a good number
- // This needs to be done for each rule set, so we first need to get a
- // list of the rule sets
- String[] ruleSets = configuration.getStringArray( "color.rulesets" );
- if( ruleSets.length == 0 ){
- // Default rule set is already defined
- return;
+ List<ColorRule> newRules = new ArrayList<>();
+ AbstractConfiguration configuration = m_configuration;
+
+ DataConfiguration data = new DataConfiguration(configuration);
+
+ for( int x = 0; x < 32; x++ ){
+ String baseConfigKey = "color.rules(" + x + ")";
+ String expression;
+ Color backgroundColor;
+ Color foregroundColor;
+
+ expression = configuration.getString( baseConfigKey + ".expression" );
+ backgroundColor = data.getColor( baseConfigKey + ".backgroundColor" );
+ foregroundColor = data.getColor( baseConfigKey + ".foregroundColor" );
+
+ if( expression == null ||
+ backgroundColor == null ||
+ foregroundColor == null ){
+ continue;
+ }
+
+ Rule simpleRule = ExpressionRule.getRule(expression);
+ ColorRule rule = new ColorRule( expression, simpleRule, backgroundColor, foregroundColor );
+ newRules.add( rule );
}
- /*
- ColorRule(final String expression,
- final Rule rule,
- final Color backgroundColor,
- final Color foregroundColor)
- */
-// Map<String,List<ColorRule>> rules = new HashMap<>();
-// for( String ruleSet : ruleSets ){
-// String[] rulesForRuleSet = configuration.getStringArray( "color.rules." + ruleSet );
-// for( String individualRule : rulesForRuleSet ){
-//
-// }
-// }
-//
-// setRules(rules);
+ setRules(newRules);
}
private boolean doLoadColorSettings(String name) {
@@ -280,7 +289,7 @@
new BufferedInputStream(new FileInputStream(f)));
Map map = (Map) s.readObject();
- setRules(map);
+// setRules(map);
} catch (EOFException eof) { //end of file - ignore..
} catch (IOException ioe) {
ioe.printStackTrace();
@@ -301,8 +310,8 @@
return f.exists();
}
- public Vector getDefaultColors() {
- Vector vec = new Vector();
+ public static List<Color> getDefaultColors() {
+ List<Color> vec = new ArrayList<>();
vec.add(Color.white);
vec.add(Color.black);
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 6fbe25a..48d903d 100644
--- a/src/main/java/org/apache/log4j/chainsaw/prefs/SettingsManager.java
+++ b/src/main/java/org/apache/log4j/chainsaw/prefs/SettingsManager.java
@@ -52,7 +52,7 @@
private class TabSettingsData{
FileBasedConfigurationBuilder<PropertiesConfiguration> file;
- CombinedConfiguration combined;
+ AbstractConfiguration tabSettings;
}
private PropertiesConfiguration m_configuration;
@@ -74,14 +74,19 @@
settingsDir.mkdir();
}
- URL defaultPrefs = this.getClass().getClassLoader()
+ FileBasedBuilderParameters fileParams = params.fileBased();
+ if( f.exists() ){
+ fileParams.setFile(f);
+ }else{
+ URL defaultPrefs = this.getClass().getClassLoader()
.getResource("org/apache/log4j/chainsaw/prefs/default.properties");
+ fileParams.setURL(defaultPrefs);
+ }
m_builder =
new FileBasedConfigurationBuilder<PropertiesConfiguration>(
PropertiesConfiguration.class)
- .configure(params.fileBased()
- .setURL(defaultPrefs)
+ .configure(fileParams
.setListDelimiterHandler(new DefaultListDelimiterHandler(','))
);
@@ -118,14 +123,21 @@
return m_configuration;
}
- public AbstractConfiguration getSettingsForReceiverTab(String identifier){
- if( m_tabSettings.containsKey( identifier ) ){
- return m_tabSettings.get( identifier ).combined;
- }
-
+ public AbstractConfiguration getCombinedSettingsForRecevierTab(String identifier){
// Override combiner: nodes in the first structure take precedence over the second
CombinedConfiguration combinedConfig = new CombinedConfiguration(new OverrideCombiner());
+ combinedConfig.addConfiguration(getSettingsForReceiverTab(identifier));
+ combinedConfig.addConfiguration(getGlobalConfiguration());
+
+ return combinedConfig;
+ }
+
+ public AbstractConfiguration getSettingsForReceiverTab(String identifier){
+ if( m_tabSettings.containsKey( identifier ) ){
+ return m_tabSettings.get( identifier ).tabSettings;
+ }
+
PropertiesConfiguration configuration = null;
// Either we don't contain the key, or we got an exception. Regardless,
@@ -154,14 +166,11 @@
AbstractConfiguration config = builder.getConfiguration();
builder.getFileHandler().setFile(f);
+ data.tabSettings = config;
- combinedConfig.addConfiguration(config);
- combinedConfig.addConfiguration(getGlobalConfiguration());
-
- data.combined = combinedConfig;
m_tabSettings.put( identifier, data );
- return combinedConfig;
+ return config;
}catch( ConfigurationException ex ){}
return null;
@@ -188,6 +197,9 @@
for( String key : m_tabSettings.keySet() ){
try{
+ logger.debug( "Saving {}({}) to {}", key,
+ m_tabSettings.get(key).tabSettings,
+ m_tabSettings.get(key).file.getFileHandler().getURL() );
m_tabSettings.get(key).file.save();
}catch( ConfigurationException ex ){
logger.error( "Unable to save settings for {}", key );
diff --git a/src/main/resources/org/apache/log4j/chainsaw/prefs/default.properties b/src/main/resources/org/apache/log4j/chainsaw/prefs/default.properties
index 41d3a03..1c4ec4d 100644
--- a/src/main/resources/org/apache/log4j/chainsaw/prefs/default.properties
+++ b/src/main/resources/org/apache/log4j/chainsaw/prefs/default.properties
@@ -50,3 +50,14 @@
highlightSearchMatchText=true
wrapMessage=true
searchResultsVisible=true
+
+# Default color settings
+color.rules(0).expression = level == FATAL || level == ERROR || exception exists
+color.rules(0).backgroundColor = #ff9999
+color.rules(0).foregroundColor = #000000
+color.rules(1).expression = level == WARN
+color.rules(1).backgroundColor = #ffff99
+color.rules(1).foregroundColor = #000000
+color.rules(2).expression = prop.marker exists
+color.rules(2).backgroundColor = #99ff99
+color.rules(2).foregroundColor = #000000
diff --git a/src/main/resources/org/apache/log4j/chainsaw/prefs/logpanel.properties b/src/main/resources/org/apache/log4j/chainsaw/prefs/logpanel.properties
index 2c284e5..d02d4e3 100644
--- a/src/main/resources/org/apache/log4j/chainsaw/prefs/logpanel.properties
+++ b/src/main/resources/org/apache/log4j/chainsaw/prefs/logpanel.properties
@@ -35,4 +35,3 @@
logpanel.hiddenLoggers=
logpanel.hiddenExpression=
logpanel.alwaysDisplayExpression=
-