浏览代码

Created OsdPolicies Enum

Fixes issue 3630
Change-Id: I3880b3201932d914e11a6cefbcef81f788c0f60d
Reviewed-on: http://gerrit.dmdirc.com/758
Reviewed-by: Chris Smith <chris@dmdirc.com>
Automatic-Compile: Chris Smith <chris@dmdirc.com>
tags/0.6.3
Simon Mott 14 年前
父节点
当前提交
1ecff3587b

+ 16
- 52
src/com/dmdirc/addons/osd/OsdManager.java 查看文件

@@ -44,8 +44,6 @@ public class OsdManager {
44 44
     private final List<OsdWindow> windowList = new ArrayList<OsdWindow>();
45 45
     /** List of messages to be queued. */
46 46
     private final Queue<String> windowQueue = new LinkedList<String>();
47
-    /** The spacing between the windows. */
48
-    private static final int WINDOW_GAP = 5;
49 47
 
50 48
     /**
51 49
      * Create a new OSD Manager.
@@ -85,13 +83,20 @@ public class OsdManager {
85 83
      * Create a new OSD window with "message".
86 84
      * <p>
87 85
      * This method needs to be synchronised to ensure that the window list is
88
-     * not modified in between the invocation of {@link #getYPosition()} and
89
-     * the point at which the {@link OSDWindow} is added to the windowList.
86
+     * not modified in between the invocation of
87
+     * {@link OsdPolicy#getYPosition(com.dmdirc.addons.osd.OsdManager, int)} 
88
+     * and the point at which the {@link OsdWindow} is added to the windowList.
90 89
      *
91
-     * @see #getYPosition()
90
+     * @see OsdPolicy#getYPosition(com.dmdirc.addons.osd.OsdManager, int)
92 91
      * @param message Text to display in the OSD window.
93 92
      */
94 93
     private synchronized void displayWindow(final String message) {
94
+        final OsdPolicy policy = OsdPolicy.valueOf(IdentityManager.
95
+                getGlobalConfig().getOption(plugin.getDomain(), "newbehaviour").
96
+                toUpperCase());
97
+        final int startY = IdentityManager.getGlobalConfig().getOptionInt(
98
+                plugin.getDomain(), "locationY");
99
+
95 100
         windowList.add(UIUtilities.invokeAndWait(
96 101
                 new ReturnableThread<OsdWindow>() {
97 102
 
@@ -100,8 +105,8 @@ public class OsdManager {
100 105
             public void run() {
101 106
                 setObject(new OsdWindow(message, false,
102 107
                         IdentityManager.getGlobalConfig().getOptionInt(
103
-                        plugin.getDomain(), "locationX"), getYPosition(),
104
-                        plugin, OsdManager.this));
108
+                        plugin.getDomain(), "locationX"), policy.getYPosition(
109
+                        OsdManager.this, startY), plugin, OsdManager.this));
105 110
             }
106 111
         }));
107 112
     }
@@ -113,8 +118,9 @@ public class OsdManager {
113 118
      * @param window The window that we are destroying.
114 119
      */
115 120
     public synchronized void closeWindow(final OsdWindow window) {
116
-        final String policy = IdentityManager.getGlobalConfig().getOption(
117
-                plugin.getDomain(), "newbehaviour");
121
+        final OsdPolicy policy = OsdPolicy.valueOf(IdentityManager.
122
+                getGlobalConfig().getOption(plugin.getDomain(), "newbehaviour").
123
+                toUpperCase());
118 124
 
119 125
         int oldY = window.getDesiredY();
120 126
         final int closedIndex = windowList.indexOf(window);
@@ -136,13 +142,11 @@ public class OsdManager {
136 142
         final List<OsdWindow> newList = getWindowList();
137 143
         for (OsdWindow otherWindow : newList.subList(closedIndex, newList.size())) {
138 144
             final int currentY = otherWindow.getDesiredY();
139
-
140
-            if ("down".equals(policy) || "up".equals(policy)) {
145
+            if (policy.changesPosition()) {
141 146
                 otherWindow.setDesiredLocation(otherWindow.getDesiredX(), oldY);
142 147
                 oldY = currentY;
143 148
             }
144 149
         }
145
-
146 150
         displayWindows();
147 151
     }
148 152
 
@@ -172,44 +176,4 @@ public class OsdManager {
172 176
     public int getWindowCount() {
173 177
         return windowList.size();
174 178
     }
175
-
176
-    /**
177
-     * Get the Y position for the next window.
178
-     * <p>
179
-     * In order to ensure that windows are displayed at the correct position,
180
-     * the calling party MUST ensure that the window list is not altered between
181
-     * this method's invocation and the time at which the window is displayed.
182
-     * If the window list is altered, multiple windows may appear on top of
183
-     * each other instead of stacking correctly, or there may be gaps in up/down
184
-     * policy layouts.
185
-     *
186
-     * @return the Y position for the next window.
187
-     */
188
-    private int getYPosition() {
189
-        final String policy = IdentityManager.getGlobalConfig().getOption(
190
-                plugin.getDomain(), "newbehaviour");
191
-        int y = IdentityManager.getGlobalConfig().getOptionInt(plugin.getDomain(),
192
-                "locationY");
193
-
194
-        if ("down".equals(policy)) {
195
-            // Place our new window below old windows
196
-            for (OsdWindow window : getWindowList()) {
197
-                if (window.isVisible()) {
198
-                    y = Math.max(y, window.getY() + window.getHeight() + WINDOW_GAP);
199
-                }
200
-            }
201
-        } else if ("up".equals(policy)) {
202
-            // Place our new window above old windows
203
-            for (OsdWindow window : getWindowList()) {
204
-                if (window.isVisible()) {
205
-                    y = Math.min(y, window.getY() - window.getHeight() - WINDOW_GAP);
206
-                }
207
-            }
208
-        } else if ("close".equals(policy)) {
209
-            // Close existing windows and use their place
210
-            closeAll();
211
-        }
212
-
213
-        return y;
214
-    }
215 179
 }

+ 5
- 4
src/com/dmdirc/addons/osd/OsdPlugin.java 查看文件

@@ -126,10 +126,11 @@ public final class OsdPlugin extends Plugin implements CategoryChangeListener,
126 126
         category.addSetting(maxWindowsSetting);
127 127
         
128 128
         final Map<String, String> posOptions = new HashMap<String, String>();
129
-        posOptions.put("down", "Place new windows below old ones");
130
-        posOptions.put("up", "Place new windows above old ones");
131
-        posOptions.put("close", "Close existing windows");
132
-        posOptions.put("ontop", "Place new windows on top of existing window");
129
+
130
+        //Populate policy MULTICHOICE
131
+        for (OsdPolicy policy : OsdPolicy.values()) {
132
+            posOptions.put(policy.name(), policy.getDescription());
133
+        }
133 134
         
134 135
         category.addSetting(new PreferencesSetting(getDomain(), "newbehaviour",
135 136
                 "New window policy", "What to do when an OSD Window "

+ 132
- 0
src/com/dmdirc/addons/osd/OsdPolicy.java 查看文件

@@ -0,0 +1,132 @@
1
+/*
2
+ * Copyright (c) 2006-2010 Chris Smith, Shane Mc Cormack, Gregory Holmes,
3
+ * Simon Mott
4
+ *
5
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ * of this software and associated documentation files (the "Software"), to deal
7
+ * in the Software without restriction, including without limitation the rights
8
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ * copies of the Software, and to permit persons to whom the Software is
10
+ * furnished to do so, subject to the following conditions:
11
+ *
12
+ * The above copyright notice and this permission notice shall be included in
13
+ * all copies or substantial portions of the Software.
14
+ *
15
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ * SOFTWARE.
22
+ */
23
+package com.dmdirc.addons.osd;
24
+
25
+/**
26
+ * Enumerates OSD Policies.
27
+ *
28
+ * @author simon
29
+ * @since 0.6.3
30
+ */
31
+public enum OsdPolicy {
32
+
33
+    /** Spawn new windows below old ones. */
34
+    DOWN ("Place new windows below old ones", true) {
35
+        /** {@inheritDoc} */
36
+        @Override
37
+        public int getYPosition(final OsdManager osdManager, final int startY) {
38
+            int y = startY;
39
+            for (OsdWindow window : osdManager.getWindowList()) {
40
+                if (window.isVisible()) {
41
+                    y = Math.max(y, window.getY() + window.getHeight() + WINDOW_GAP);
42
+                }
43
+            }
44
+            return y;
45
+        }
46
+    },
47
+    /** Spawn new windows above old ones. */
48
+    UP ("Place new windows above old ones", true) {
49
+        /** {@inheritDoc} */
50
+        @Override
51
+        public int getYPosition(final OsdManager osdManager, final int startY) {
52
+            int y = startY;
53
+            for (OsdWindow window : osdManager.getWindowList()) {
54
+                if (window.isVisible()) {
55
+                    y = Math.min(y, window.getY() - window.getHeight() - WINDOW_GAP);
56
+                }
57
+            }
58
+            return y;
59
+        }
60
+    },
61
+    /** Close old OSD windows and display the new windows. */
62
+    CLOSE ("Close existing windows", false) {
63
+        /** {@inheritDoc} */
64
+        @Override
65
+        public int getYPosition(final OsdManager osdManager, final int startY) {
66
+            osdManager.closeAll();
67
+            return startY;
68
+        }
69
+    },
70
+    /** Place new windows on top of old windows. */
71
+    ONTOP ("Place new windows on top of existing windows", false) {
72
+        /** {@inheritDoc} */
73
+        @Override
74
+        public int getYPosition(final OsdManager osdManager, final int startY) {
75
+            return startY;
76
+        }
77
+    };
78
+
79
+    /** The spacing between the windows. */
80
+    private static final int WINDOW_GAP = 5;
81
+
82
+    /** Description of policy. */
83
+    private final String description;
84
+
85
+    /** Does policy need to change window location */
86
+    private final boolean changesPosition;
87
+
88
+    /**
89
+     * Creates a new instance of OsdPolicies.
90
+     *
91
+     * @param description Description of the behaviour of the enum value
92
+     */
93
+    OsdPolicy(final String description, final boolean changesPosition) {
94
+        this.description = description;
95
+        this.changesPosition = changesPosition;
96
+    }
97
+
98
+    /**
99
+     * Calculates the Y position for new windows according to this policy.
100
+     * <p>
101
+     * In order to ensure that windows are displayed at the correct position,
102
+     * the calling party MUST ensure that the window list is not altered between
103
+     * this method's invocation and the time at which the window is displayed.
104
+     * If the window list is altered, multiple windows may appear on top of
105
+     * each other instead of stacking correctly, or there may be gaps in up/down
106
+     * policy layouts.
107
+     *
108
+     * @param osdManager OSD Manager we are using
109
+     * @param startY Value of startY from the main config
110
+     *
111
+     * @return returns Y Value to use for new windows
112
+     */
113
+    public abstract int getYPosition(final OsdManager osdManager, final int startY);
114
+
115
+    /**
116
+     * See if this behaviour changes window position
117
+     *
118
+     * @return true or false depending on window behaviour
119
+     */
120
+    public boolean changesPosition() {
121
+        return changesPosition;
122
+    };
123
+
124
+    /**
125
+     * Return a description of what each policy does.
126
+     *
127
+     * @return Description of policies behaviour
128
+     */
129
+    public String getDescription() {
130
+        return description;
131
+    }
132
+}

正在加载...
取消
保存