Selaa lähdekoodia

Prefs stuff. Issue 444.

git-svn-id: http://svn.dmdirc.com/trunk@3272 00569f92-eb28-0410-84fd-f71c24880f
tags/0.6
Chris Smith 16 vuotta sitten
vanhempi
commit
286286d7bd

+ 45
- 0
src/com/dmdirc/config/prefs/CategoryChangeListener.java Näytä tiedosto

@@ -0,0 +1,45 @@
1
+/*
2
+ * Copyright (c) 2006-2008 Chris Smith, Shane Mc Cormack, Gregory Holmes
3
+ *
4
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
5
+ * of this software and associated documentation files (the "Software"), to deal
6
+ * in the Software without restriction, including without limitation the rights
7
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ * copies of the Software, and to permit persons to whom the Software is
9
+ * furnished to do so, subject to the following conditions:
10
+ *
11
+ * The above copyright notice and this permission notice shall be included in
12
+ * all copies or substantial portions of the Software.
13
+ *
14
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20
+ * SOFTWARE.
21
+ */
22
+package com.dmdirc.config.prefs;
23
+
24
+/**
25
+ * Defines methods that should be implemented by objects who wish to be
26
+ * notified about category changes.
27
+ * 
28
+ * @author chris
29
+ */
30
+public interface CategoryChangeListener {
31
+
32
+    /**
33
+     * Called when a category has been selected.
34
+     * 
35
+     * @param category The category that was selected
36
+     */
37
+    void CategorySelected(final PreferencesCategory category);
38
+    
39
+    /**
40
+     * Called when a category has been deselected.
41
+     * 
42
+     * @param category The category that was deselected
43
+     */
44
+    void CategoryDeselected(final PreferencesCategory category);
45
+}

+ 41
- 0
src/com/dmdirc/config/prefs/PreferencesCategory.java Näytä tiedosto

@@ -21,6 +21,8 @@
21 21
  */
22 22
 package com.dmdirc.config.prefs;
23 23
 
24
+import com.dmdirc.util.ListenerList;
25
+
24 26
 import java.util.ArrayList;
25 27
 import java.util.List;
26 28
 
@@ -47,6 +49,9 @@ public class PreferencesCategory {
47 49
     
48 50
     /** The replacement object to use for this category. */
49 51
     private final PreferencesInterface object;
52
+    
53
+    /** A list of listeners who are interested in this category. */
54
+    private final ListenerList listeners = new ListenerList();
50 55
 
51 56
     /**
52 57
      * Creates a new preferences category that contains settings.
@@ -150,5 +155,41 @@ public class PreferencesCategory {
150 155
     public PreferencesInterface getObject() {
151 156
         return object;
152 157
     }
158
+    
159
+    /**
160
+     * Registers a change listener for this category.
161
+     * 
162
+     * @param listener The listener to be added
163
+     */
164
+    public void addChangeListener(final CategoryChangeListener listener) {
165
+        listeners.add(CategoryChangeListener.class, listener);
166
+    }
167
+    
168
+    /**
169
+     * Removes a change listener from this category.
170
+     * 
171
+     * @param listener The listener to be added
172
+     */
173
+    public void removeChangeListener(final CategoryChangeListener listener) {
174
+        listeners.remove(CategoryChangeListener.class, listener);
175
+    }
176
+    
177
+    /**
178
+     * Informs all registered listeners that this category has been selected.
179
+     */
180
+    public void fireCategorySelected() {
181
+        for (CategoryChangeListener listener : listeners.get(CategoryChangeListener.class)) {
182
+            listener.CategorySelected(this);
183
+        }
184
+    }
185
+    
186
+    /**
187
+     * Informs all registered listeners that this category has been deselected.
188
+     */
189
+    public void fireCategoryDeselected() {
190
+        for (CategoryChangeListener listener : listeners.get(CategoryChangeListener.class)) {
191
+            listener.CategorySelected(this);
192
+        }
193
+    }    
153 194
 
154 195
 }

+ 7
- 6
src/com/dmdirc/ui/swing/dialogs/prefs/SwingPreferencesDialog.java Näytä tiedosto

@@ -32,6 +32,7 @@ import com.dmdirc.config.prefs.PreferencesSetting;
32 32
 import com.dmdirc.config.prefs.validator.NumericalValidator;
33 33
 import com.dmdirc.ui.swing.MainFrame;
34 34
 import com.dmdirc.ui.swing.components.validating.ValidatingJTextField;
35
+import com.dmdirc.util.DoubleMap;
35 36
 import static com.dmdirc.ui.swing.UIUtilities.LARGE_BORDER;
36 37
 import static com.dmdirc.ui.swing.UIUtilities.SMALL_BORDER;
37 38
 import static com.dmdirc.ui.swing.UIUtilities.layoutGrid;
@@ -95,7 +96,7 @@ public final class SwingPreferencesDialog extends StandardDialog implements
95 96
     private final Map<PreferencesSetting, JComponent> components;
96 97
 
97 98
     /** Categories in the dialog. */
98
-    private final Map<PreferencesCategory, JPanel> categories;
99
+    private final DoubleMap<PreferencesCategory, JPanel> categories;
99 100
 
100 101
     /** Custom panels, not to be laid out automatically. */
101 102
     private final List<JPanel> panels;
@@ -123,7 +124,7 @@ public final class SwingPreferencesDialog extends StandardDialog implements
123 124
         
124 125
         manager = new PreferencesManager();
125 126
 
126
-        categories = new HashMap<PreferencesCategory, JPanel>();
127
+        categories = new DoubleMap<PreferencesCategory, JPanel>();
127 128
         components = new HashMap<PreferencesSetting, JComponent>();
128 129
 
129 130
         panels = new ArrayList<JPanel>();
@@ -231,10 +232,10 @@ public final class SwingPreferencesDialog extends StandardDialog implements
231 232
         final JLabel label = getLabel(setting);
232 233
         final JComponent option = getComponent(setting);
233 234
 
234
-        ((JPanel) categories.get(category).getComponent(1)).add(label);
235
+        ((JPanel) categories.getValue(category).getComponent(1)).add(label);
235 236
 
236 237
         label.setLabelFor(option);
237
-        ((JPanel) categories.get(category).getComponent(1)).add(option);
238
+        ((JPanel) categories.getValue(category).getComponent(1)).add(option);
238 239
     }
239 240
 
240 241
     /**
@@ -421,7 +422,7 @@ public final class SwingPreferencesDialog extends StandardDialog implements
421 422
             }
422 423
 
423 424
             panels.add((JPanel) category.getObject());
424
-            categories.get(category).add((JPanel) category.getObject(), 1);
425
+            categories.getValue(category).add((JPanel) category.getObject(), 1);
425 426
 
426 427
             return;
427 428
         }
@@ -494,7 +495,7 @@ public final class SwingPreferencesDialog extends StandardDialog implements
494 495
 
495 496
     /** {@inheritDoc} */
496 497
     public void display() {
497
-        for (JPanel panel : categories.values()) {
498
+        for (JPanel panel : categories.valueSet()) {
498 499
             if (!panels.contains(panel.getComponent(1))) {
499 500
                 layoutGrid((JPanel) panel.getComponent(1), ((JPanel) panel
500 501
                         .getComponent(1)).getComponentCount() / 2, 2, SMALL_BORDER,

+ 97
- 0
src/com/dmdirc/util/DoubleMap.java Näytä tiedosto

@@ -0,0 +1,97 @@
1
+/*
2
+ * Copyright (c) 2006-2008 Chris Smith, Shane Mc Cormack, Gregory Holmes
3
+ *
4
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
5
+ * of this software and associated documentation files (the "Software"), to deal
6
+ * in the Software without restriction, including without limitation the rights
7
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ * copies of the Software, and to permit persons to whom the Software is
9
+ * furnished to do so, subject to the following conditions:
10
+ *
11
+ * The above copyright notice and this permission notice shall be included in
12
+ * all copies or substantial portions of the Software.
13
+ *
14
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20
+ * SOFTWARE.
21
+ */
22
+package com.dmdirc.util;
23
+
24
+import java.util.ArrayList;
25
+import java.util.HashSet;
26
+import java.util.List;
27
+import java.util.Set;
28
+
29
+/**
30
+ * An object that maps keys to values, and values back to keys. Currently
31
+ * does no checking for duplicates. Does not allow null values.
32
+ * 
33
+ * @param <A> The first type of data to be mapped
34
+ * @param <B> The second type of data to be mapped 
35
+ * @author chris
36
+ */
37
+public class DoubleMap<A,B> {
38
+    
39
+    /** The keys in this map. */
40
+    protected final List<A> keys = new ArrayList<A>();
41
+    /** The values in this map. */
42
+    protected final List<B> values = new ArrayList<B>();
43
+    
44
+    /**
45
+     * Adds the specified pair to this map.
46
+     * 
47
+     * @param key The key for the map
48
+     * @param value The value for the map
49
+     */
50
+    public void put(final A key, final B value) {
51
+        if (key == null || value == null) {
52
+            throw new NullPointerException();
53
+        }
54
+        
55
+        keys.add(key);
56
+        values.add(value);
57
+    }
58
+    
59
+    /**
60
+     * Retrieves the value associated with the specified key.
61
+     * 
62
+     * @param key The key to search for 
63
+     * @return The value of the specified key
64
+     */
65
+    public B getValue(final A key) {
66
+        return values.get(keys.indexOf(key));
67
+    }
68
+    
69
+    /**
70
+     * Retrieves the key associated with the specified value.
71
+     * 
72
+     * @param value The value to search for
73
+     * @return The key of the specified value
74
+     */
75
+    public A getKey(final B value) {
76
+        return keys.get(values.indexOf(value));
77
+    }
78
+    
79
+    /**
80
+     * Retrieves the set of keys in this double map.
81
+     * 
82
+     * @return This map's key set
83
+     */
84
+    public Set<A> keySet() {
85
+        return new HashSet<A>(keys);
86
+    }
87
+    
88
+    /**
89
+     * Retrieves the set of values in this double map.
90
+     * 
91
+     * @return This map's value set
92
+     */
93
+    public Set<B> valueSet() {
94
+        return new HashSet<B>(values);
95
+    }
96
+
97
+}

Loading…
Peruuta
Tallenna