Sfoglia il codice sorgente

Issue 465: OSD respond live to config changes

git-svn-id: http://svn.dmdirc.com/trunk@2290 00569f92-eb28-0410-84fd-f71c24880f
tags/0.5.1
Gregory Holmes 16 anni fa
parent
commit
89e7cf5f91

+ 1
- 1
src/com/dmdirc/Main.java Vedi File

@@ -64,7 +64,7 @@ public final class Main {
64 64
      * A revision number for actions and core plugins. If this is increased,
65 65
      * users will be prompted to re-extract them.
66 66
      */
67
-    private static final int ADDON_REVISION = 3;
67
+    private static final int ADDON_REVISION = 4;
68 68
     
69 69
     /**
70 70
      * The UI to use for the client.

+ 38
- 15
src/com/dmdirc/addons/osdplugin/OsdPlugin.java Vedi File

@@ -24,11 +24,13 @@ package com.dmdirc.addons.osdplugin;
24 24
 
25 25
 import com.dmdirc.Main;
26 26
 import com.dmdirc.commandparser.CommandManager;
27
+import com.dmdirc.config.ConfigManager;
27 28
 import com.dmdirc.config.Identity;
28 29
 import com.dmdirc.config.IdentityManager;
29 30
 import com.dmdirc.plugins.Plugin;
30 31
 import com.dmdirc.ui.interfaces.PreferencesInterface;
31 32
 import com.dmdirc.ui.interfaces.PreferencesPanel;
33
+import com.dmdirc.ui.swing.components.ColourChooser;
32 34
 
33 35
 import java.awt.BorderLayout;
34 36
 import java.util.Properties;
@@ -55,6 +57,12 @@ public final class OsdPlugin extends Plugin implements PreferencesInterface {
55 57
     /** Font size spinner. */
56 58
     private JSpinner spinner;
57 59
     
60
+    /** Background colour chooser. */
61
+    private ColourChooser bgColour;
62
+    
63
+    /** Background colour chooser. */
64
+    private ColourChooser fgColour;
65
+    
58 66
     /**
59 67
      * Creates a new instance of OsdPlugin.
60 68
      */
@@ -96,31 +104,46 @@ public final class OsdPlugin extends Plugin implements PreferencesInterface {
96 104
     
97 105
     /** {@inheritDoc}. */
98 106
     public void showConfig() {
99
-        final PreferencesPanel preferencesPanel = Main.getUI().getPreferencesPanel(this, "OSD Plugin - Config");
100
-        final JPanel panel = new JPanel();
107
+        final ConfigManager config = IdentityManager.getGlobalConfig();
108
+        final PreferencesPanel preferencesPanel = Main.getUI().
109
+                getPreferencesPanel(this, "OSD Plugin - Config");
110
+        final JPanel spinnerPanel = new JPanel(new BorderLayout());
111
+        final JPanel bgColourPanel = new JPanel(new BorderLayout());
112
+        final JPanel fgColourPanel = new JPanel(new BorderLayout());
101 113
         spinner = new JSpinner(new SpinnerNumberModel());
114
+        bgColour = new ColourChooser();
115
+        fgColour = new ColourChooser();
102 116
         
103
-        spinner.setValue(IdentityManager.getGlobalConfig().getOptionInt(MY_DOMAIN, "fontSize", 20));
117
+        spinner.setValue(config.getOptionInt(MY_DOMAIN, "fontSize", 20));
118
+        bgColour.setColour(config.getOption(MY_DOMAIN, "bgcolour", "2222aa"));
119
+        fgColour.setColour(config.getOption(MY_DOMAIN, "fgcolour", "ffffff"));
104 120
         
105
-        panel.setLayout(new BorderLayout());
106
-        panel.add(spinner);
121
+        spinnerPanel.add(spinner);
122
+        bgColourPanel.add(bgColour);
123
+        fgColourPanel.add(fgColour);
107 124
         
108 125
         preferencesPanel.addCategory("General", "General configuration for OSD plugin.");
109 126
         
110 127
         preferencesPanel.addPanelOption("General", "fontsize", "Font size: ",
111
-                "Changes the font size of the OSD", panel);
112
-        preferencesPanel.addColourOption("General", "bgcolour",
128
+                "Changes the font size of the OSD", spinnerPanel);
129
+        preferencesPanel.addPanelOption("General", "bgcolour",
113 130
                 "Background Colour: ", "Background colour for the OSD",
114
-                IdentityManager.getGlobalConfig().getOption(MY_DOMAIN, "bgcolour", "2222aa"), true, true);
115
-        preferencesPanel.addColourOption("General", "fgcolour",
116
-                "Foreground Colour: ", "Foreground colour for the OSD",
117
-                IdentityManager.getGlobalConfig().getOption(MY_DOMAIN, "fgcolour", "ffffff"), true, true);
131
+                bgColourPanel);
132
+        preferencesPanel.addPanelOption("General", "fgcolour",
133
+                "Foreground Colour: ", "Foreground colour for the OSD", 
134
+                fgColourPanel);
118 135
         preferencesPanel.addSpinnerOption("General", "timeout", "Timeout: ",
119 136
                 "Length of times in seconds before the OSD window times out",
120 137
                 IdentityManager.getGlobalConfig().getOptionInt(MY_DOMAIN, "timeout", 15));
121 138
         
122 139
         osdWindow = new OsdWindow("Please drag this OSD to position", true);
123 140
         
141
+        spinner.addChangeListener(osdWindow);
142
+        bgColour.addActionListener(osdWindow);
143
+        bgColour.setActionCommand("backgroundColour");
144
+        fgColour.addActionListener(osdWindow);
145
+        fgColour.setActionCommand("foregroundColour");
146
+        
124 147
         preferencesPanel.display();
125 148
     }
126 149
     
@@ -131,11 +154,11 @@ public final class OsdPlugin extends Plugin implements PreferencesInterface {
131 154
         if (spinner != null) {
132 155
             config.setOption(MY_DOMAIN, "fontSize", String.valueOf(spinner.getValue()));
133 156
         }
134
-        if (properties.getProperty("fgcolour") != null) {
135
-            config.setOption(MY_DOMAIN, "fgcolour", properties.getProperty("fgcolour"));
157
+        if (fgColour != null) {
158
+            config.setOption(MY_DOMAIN, "fgcolour", fgColour.getColour());
136 159
         }
137
-        if (properties.getProperty("bgcolour") != null) {
138
-            config.setOption(MY_DOMAIN, "bgcolour", properties.getProperty("bgcolour"));
160
+        if (bgColour != null) {
161
+            config.setOption(MY_DOMAIN, "bgcolour", bgColour.getColour());
139 162
         }
140 163
         if (properties.getProperty("timeout") != null) {
141 164
             config.setOption(MY_DOMAIN, "timeout", properties.getProperty("timeout"));

+ 37
- 3
src/com/dmdirc/addons/osdplugin/OsdWindow.java Vedi File

@@ -24,12 +24,16 @@ package com.dmdirc.addons.osdplugin;
24 24
 
25 25
 import com.dmdirc.Main;
26 26
 import com.dmdirc.config.IdentityManager;
27
+import com.dmdirc.ui.messages.ColourManager;
27 28
 import com.dmdirc.ui.swing.MainFrame;
28 29
 import static com.dmdirc.ui.swing.UIUtilities.LARGE_BORDER;
30
+import com.dmdirc.ui.swing.components.ColourChooser;
29 31
 
30 32
 import java.awt.BorderLayout;
31 33
 import java.awt.Color;
32 34
 import java.awt.Dimension;
35
+import java.awt.event.ActionEvent;
36
+import java.awt.event.ActionListener;
33 37
 import java.awt.event.MouseEvent;
34 38
 import java.awt.event.MouseListener;
35 39
 import java.awt.event.MouseMotionListener;
@@ -39,9 +43,13 @@ import java.util.TimerTask;
39 43
 import javax.swing.JDialog;
40 44
 import javax.swing.JLabel;
41 45
 import javax.swing.JPanel;
46
+import javax.swing.JSpinner;
47
+import javax.swing.SpinnerNumberModel;
42 48
 import javax.swing.SwingConstants;
43 49
 import javax.swing.WindowConstants;
44 50
 import javax.swing.border.LineBorder;
51
+import javax.swing.event.ChangeEvent;
52
+import javax.swing.event.ChangeListener;
45 53
 
46 54
 /**
47 55
  * The OSD Window is an always-on-top window designed to convey information
@@ -49,7 +57,7 @@ import javax.swing.border.LineBorder;
49 57
  * @author chris
50 58
  */
51 59
 public final class OsdWindow extends JDialog implements MouseListener,
52
-        MouseMotionListener {
60
+        MouseMotionListener, ChangeListener, ActionListener {
53 61
     
54 62
     /**
55 63
      * A version number for this class. It should be changed whenever the class
@@ -57,6 +65,12 @@ public final class OsdWindow extends JDialog implements MouseListener,
57 65
      * objects being unserialized with the new class).
58 66
      */
59 67
     private static final long serialVersionUID = 2;
68
+    
69
+    /** OSD Label. */
70
+    private final JLabel label;
71
+    
72
+    /** OSD Panel. */
73
+    private final JPanel panel;
60 74
 
61 75
     /**
62 76
      * Creates a new instance of OsdWindow.
@@ -80,7 +94,7 @@ public final class OsdWindow extends JDialog implements MouseListener,
80 94
         setLocation(IdentityManager.getGlobalConfig().getOptionInt("plugin-OSD", "locationX", 20),
81 95
                 IdentityManager.getGlobalConfig().getOptionInt("plugin-OSD", "locationY", 20));
82 96
         
83
-        final JPanel panel = new JPanel();
97
+        panel = new JPanel();
84 98
         panel.setBorder(new LineBorder(Color.BLACK));
85 99
         panel.setBackground(IdentityManager.getGlobalConfig().getOptionColour("plugin-OSD",
86 100
                 "bgcolour", Color.decode("#2222aa")));
@@ -88,7 +102,7 @@ public final class OsdWindow extends JDialog implements MouseListener,
88 102
         setContentPane(panel);
89 103
         setLayout(new BorderLayout());
90 104
         
91
-        final JLabel label = new JLabel(text);
105
+        label = new JLabel(text);
92 106
         label.setForeground(IdentityManager.getGlobalConfig().getOptionColour("plugin-OSD",
93 107
                 "fgcolour", Color.decode("#ffffff")));
94 108
         label.setFont(label.getFont().deriveFont(
@@ -145,5 +159,25 @@ public final class OsdWindow extends JDialog implements MouseListener,
145 159
     public void mouseMoved(final MouseEvent e) {
146 160
         // Do nothing
147 161
     }
162
+
163
+    /** {@inheritDoc} */
164
+    public void stateChanged(final ChangeEvent e) {
165
+        final float size = (((SpinnerNumberModel) ((JSpinner) e.getSource()).
166
+                getModel()).getNumber()).floatValue();
167
+        label.setFont(label.getFont().deriveFont(size));
168
+        setSize(new Dimension(500, (int) size + LARGE_BORDER));
169
+    }
170
+
171
+    /** {@inheritDoc} */
172
+    public void actionPerformed(final ActionEvent e) {
173
+        if ("backgroundColour".equals(e.getActionCommand())) {
174
+            panel.setBackground(ColourManager.parseColour(
175
+                    ((ColourChooser) e.getSource()).getColour()));
176
+        }
177
+        if ("foregroundColour".equals(e.getActionCommand())) {
178
+            label.setForeground(ColourManager.parseColour(
179
+                    ((ColourChooser) e.getSource()).getColour()));
180
+        }
181
+    }
148 182
     
149 183
 }

+ 59
- 3
src/com/dmdirc/ui/swing/components/ColourChooser.java Vedi File

@@ -35,6 +35,7 @@ import java.awt.event.ActionListener;
35 35
 import javax.swing.BorderFactory;
36 36
 import javax.swing.JButton;
37 37
 import javax.swing.JPanel;
38
+import javax.swing.event.EventListenerList;
38 39
 
39 40
 /**
40 41
  * Colour chooser widget.
@@ -66,6 +67,12 @@ public final class ColourChooser extends JPanel implements ActionListener {
66 67
     /** The value of this component. */
67 68
     private String value;
68 69
     
70
+    /** Event listeners. */
71
+    private EventListenerList listeners;
72
+    
73
+    /** Action command. */
74
+    private String command;
75
+    
69 76
     /** Creates a new instance of ColourChooser. */
70 77
     public ColourChooser() {
71 78
         this("ffffff", true, true);
@@ -84,6 +91,8 @@ public final class ColourChooser extends JPanel implements ActionListener {
84 91
         showIRC = ircColours;
85 92
         showHex = hexColours;
86 93
         value = initialColour;
94
+        listeners = new EventListenerList();
95
+        command = "";
87 96
         
88 97
         editButton = new JButton("Edit");
89 98
         editButton.setMargin(new Insets(0, 2, 0, 2));
@@ -94,10 +103,10 @@ public final class ColourChooser extends JPanel implements ActionListener {
94 103
         previewPanel.setPreferredSize(new Dimension(40, 10));
95 104
         previewPanel.setBorder(BorderFactory.createLineBorder(Color.DARK_GRAY));
96 105
         
97
-        this.setLayout(new BorderLayout(SMALL_BORDER, SMALL_BORDER));
106
+        setLayout(new BorderLayout(SMALL_BORDER, SMALL_BORDER));
98 107
         
99
-        this.add(editButton, BorderLayout.LINE_END);
100
-        this.add(previewPanel, BorderLayout.CENTER);
108
+        add(editButton, BorderLayout.LINE_END);
109
+        add(previewPanel, BorderLayout.CENTER);
101 110
         
102 111
         updateColour(initialColour);
103 112
     }
@@ -150,7 +159,54 @@ public final class ColourChooser extends JPanel implements ActionListener {
150 159
         } else {
151 160
             value = e.getActionCommand();
152 161
             updateColour(e.getActionCommand());
162
+            fireActionPerformed();
153 163
             cpd.dispose();
154 164
         }
155 165
     }
166
+    
167
+    /**
168
+     * Sets this colour choosers action command.
169
+     *
170
+     * @param command New action command
171
+     */
172
+    public void setActionCommand(final String command) {
173
+        this.command = command;
174
+    }
175
+    
176
+    /**
177
+     * Adds a ActionListener to the listener list.
178
+     *
179
+     * @param listener Listener to add
180
+     */
181
+    public void addActionListener(final ActionListener listener) {
182
+        synchronized (listeners) {
183
+            if (listener == null) {
184
+                return;
185
+            }
186
+            listeners.add(ActionListener.class, listener);
187
+        }
188
+    }
189
+    
190
+    /**
191
+     * Removes a ActionListener from the listener list.
192
+     *
193
+     * @param listener Listener to remove
194
+     */
195
+    public void removeActionListener(final ActionListener listener) {
196
+        listeners.remove(ActionListener.class, listener);
197
+    }
198
+    
199
+    /**
200
+     * Fires the action performed method on all listeners.
201
+     */
202
+    protected void fireActionPerformed() {
203
+        final Object[] listenerList = listeners.getListenerList();
204
+        for (int i = 0; i < listenerList.length; i += 2) {
205
+            if (listenerList[i] == ActionListener.class) {
206
+                ((ActionListener) listenerList[i + 1]).actionPerformed(
207
+                        new ActionEvent((Object) this, 
208
+                        ActionEvent.ACTION_PERFORMED, command));
209
+            }
210
+        }
211
+    }
156 212
 }

Loading…
Annulla
Salva