Преглед изворни кода

The now playing plugin now allows you to reorder media sources

git-svn-id: http://svn.dmdirc.com/trunk@2021 00569f92-eb28-0410-84fd-f71c24880f
tags/0.5
Chris Smith пре 16 година
родитељ
комит
0ce7aa7136

+ 7
- 9
src/com/dmdirc/addons/nowplaying/plugin/ConfigPanel.java Прегледај датотеку

@@ -22,7 +22,6 @@
22 22
 
23 23
 package com.dmdirc.addons.nowplaying.plugin;
24 24
 
25
-import com.dmdirc.addons.nowplaying.MediaSource;
26 25
 import com.dmdirc.ui.swing.components.reorderablelist.ReorderableJList;
27 26
 import static com.dmdirc.ui.swing.UIUtilities.SMALL_BORDER;
28 27
 
@@ -51,13 +50,13 @@ public class ConfigPanel extends JPanel {
51 50
     private ReorderableJList list;
52 51
     
53 52
     /** Media sources. */
54
-    private final List<MediaSource> sources;
53
+    private final List<String> sources;
55 54
     
56 55
     /** Creates a new instance of ConfigPanel. */
57
-    public ConfigPanel(List<MediaSource> sources) {
56
+    public ConfigPanel(final List<String> sources) {
58 57
         super();
59 58
         
60
-        this.sources = new LinkedList<MediaSource>(sources);
59
+        this.sources = new LinkedList<String>(sources);
61 60
         
62 61
         initComponents();
63 62
     }
@@ -65,9 +64,8 @@ public class ConfigPanel extends JPanel {
65 64
     /** Initialises the components. */
66 65
     private void initComponents() {
67 66
         list = new ReorderableJList();
68
-        list.setCellRenderer(new MediaSourceListRenderer());
69 67
         
70
-        for (MediaSource source: sources) {
68
+        for (String source : sources) {
71 69
             list.getModel().addElement(source);
72 70
         }
73 71
         
@@ -77,13 +75,13 @@ public class ConfigPanel extends JPanel {
77 75
         add(new JScrollPane(list), BorderLayout.CENTER);
78 76
     }
79 77
     
80
-    public List<MediaSource> getSources() {
81
-        final List<MediaSource> newSources = new LinkedList<MediaSource>();
78
+    public List<String> getSources() {
79
+        final List<String> newSources = new LinkedList<String>();
82 80
         
83 81
         final Enumeration<?> values = list.getModel().elements();
84 82
         
85 83
         while (values.hasMoreElements()) {
86
-            newSources.add((MediaSource) values.nextElement());
84
+            newSources.add((String) values.nextElement());
87 85
         }
88 86
         
89 87
         return newSources;

src/com/dmdirc/addons/nowplaying/plugin/MediaSourceListRenderer.java → src/com/dmdirc/addons/nowplaying/plugin/MediaSourceComparator.java Прегледај датотеку

@@ -24,37 +24,46 @@ package com.dmdirc.addons.nowplaying.plugin;
24 24
 
25 25
 import com.dmdirc.addons.nowplaying.MediaSource;
26 26
 
27
-import java.awt.Component;
28
-
29
-import javax.swing.DefaultListCellRenderer;
30
-import javax.swing.JList;
27
+import java.util.Comparator;
28
+import java.util.List;
31 29
 
32 30
 /**
33
- * Displays Media sources in a list.
31
+ * Sorts media sources according to an ordered list of their names.
32
+ *
33
+ * @author chris
34 34
  */
35
-public final class MediaSourceListRenderer extends DefaultListCellRenderer {
35
+public class MediaSourceComparator implements Comparator<MediaSource> {
36
+    
37
+    /** The order that the sources should be checked. */
38
+    private final List<String> order;
36 39
     
37 40
     /**
38
-     * A version number for this class. It should be changed whenever the class
39
-     * structure is changed (or anything else that would prevent serialized
40
-     * objects being unserialized with the new class).
41
+     * Creates a new instance of MediaSourceComparator.
42
+     * NB: The order list may be altered during comparisons.
43
+     *
44
+     * @param order An ordered list of media source names
41 45
      */
42
-    private static final long serialVersionUID = 1;
43
-    
44
-    /** Creates a new instance of MediaSourceListRenderer. */
45
-    public MediaSourceListRenderer() {
46
-        super();
46
+    public MediaSourceComparator(final List<String> order) {
47
+        this.order = order;
48
+    }
49
+
50
+    /** {@inheritDoc} */
51
+    public int compare(final MediaSource o1, final MediaSource o2) {
52
+        return getPosition(o1) - getPosition(o2);
47 53
     }
48 54
     
49
-    /** @{inheritDoc} */
50
-    public Component getListCellRendererComponent(final JList list,
51
-            final Object value, final int index, final boolean isSelected,
52
-            final boolean cellHasFocus) {
53
-        super.getListCellRendererComponent(list, value, index, isSelected, 
54
-                cellHasFocus);
55
-        setText(((MediaSource) value).getName());
55
+    /**
56
+     * Retrieves the position of the source within the order list.
57
+     * If the source is not present it is appended to the list.
58
+     *
59
+     * @param source The media source to be tested
60
+     */
61
+    private int getPosition(final MediaSource source) {
62
+        if (!order.contains(source.getName())) {
63
+            order.add(source.getName());
64
+        }
56 65
         
57
-        return this;
66
+        return order.indexOf(source.getName());
58 67
     }
59 68
     
60 69
 }

+ 39
- 13
src/com/dmdirc/addons/nowplaying/plugin/NowPlayingPlugin.java Прегледај датотеку

@@ -34,9 +34,9 @@ import com.dmdirc.plugins.Plugin;
34 34
 import com.dmdirc.plugins.PluginManager;
35 35
 import com.dmdirc.ui.interfaces.PreferencesInterface;
36 36
 import com.dmdirc.ui.interfaces.PreferencesPanel;
37
-import java.util.ArrayList;
38 37
 
39
-import java.util.LinkedList;
38
+import java.util.ArrayList;
39
+import java.util.Collections;
40 40
 import java.util.List;
41 41
 import java.util.Properties;
42 42
 
@@ -55,6 +55,9 @@ public class NowPlayingPlugin extends Plugin implements EventPlugin,
55 55
     /** Config panel. */
56 56
     private ConfigPanel configPanel;
57 57
     
58
+    /** The user's preferred order for source usage. */
59
+    private List<String> order;
60
+    
58 61
     public NowPlayingPlugin() {
59 62
         super();
60 63
         
@@ -71,6 +74,8 @@ public class NowPlayingPlugin extends Plugin implements EventPlugin,
71 74
     protected void onActivate() {
72 75
         sources.clear();
73 76
         
77
+        loadSettings();
78
+        
74 79
         for (Plugin target : PluginManager.getPluginManager().getPlugins()) {
75 80
             if (target.isActive()) {
76 81
                 addPlugin(target);
@@ -78,7 +83,6 @@ public class NowPlayingPlugin extends Plugin implements EventPlugin,
78 83
         }
79 84
         
80 85
         command = new NowPlayingCommand(this);
81
-        loadSettings();
82 86
     }
83 87
     
84 88
     /** {@inheritDoc} */
@@ -110,12 +114,15 @@ public class NowPlayingPlugin extends Plugin implements EventPlugin,
110 114
     }
111 115
     
112 116
     /** {@inheritDoc} */
113
-    public boolean isConfigurable() { return true; }
117
+    public boolean isConfigurable() {
118
+        return true;
119
+    }
114 120
     
115 121
     /** {@inheritDoc} */
116 122
     public void showConfig() {
117 123
         final PreferencesPanel preferencesPanel = Main.getUI().getPreferencesPanel(this, "Now playing Plugin - Config");
118
-        configPanel = new ConfigPanel(sources);
124
+        
125
+        configPanel = new ConfigPanel(order);
119 126
         
120 127
         preferencesPanel.addCategory("General", "General options for the plugin.");
121 128
         
@@ -126,8 +133,8 @@ public class NowPlayingPlugin extends Plugin implements EventPlugin,
126 133
     
127 134
     /** {@inheritDoc} */
128 135
     public void configClosed(final Properties properties) {
129
-        //save settings
130
-        sources = configPanel.getSources();
136
+        order = configPanel.getSources();
137
+        
131 138
         saveSettings();
132 139
     }
133 140
     
@@ -138,16 +145,16 @@ public class NowPlayingPlugin extends Plugin implements EventPlugin,
138 145
     
139 146
     /** Saves the plugins settings. */
140 147
     private void saveSettings() {
141
-        final List<String> values = new LinkedList<String>();
142
-        for (final MediaSource source : sources) {
143
-            values.add(source.getName());
144
-        }
145
-        Config.setOption(DOMAIN, "sourceOrder", values);
148
+        Config.setOption(DOMAIN, "sourceOrder", order);
146 149
     }
147 150
     
148 151
     /** Loads the plugins settings. */
149 152
     private void loadSettings() {
150
-        final List<String> values = Config.getOptionList(DOMAIN, "sourceOrder");
153
+        if (Config.hasOption(DOMAIN, "sourceOrder")) {
154
+            order = Config.getOptionList(DOMAIN, "sourceOrder");
155
+        } else {
156
+            order = new ArrayList<String>();
157
+        }
151 158
     }
152 159
     
153 160
     /** {@inheritDoc} */
@@ -169,10 +176,27 @@ public class NowPlayingPlugin extends Plugin implements EventPlugin,
169 176
     private void addPlugin(final Plugin target) {
170 177
         if (target instanceof MediaSource) {
171 178
             sources.add((MediaSource) target);
179
+            addSourceToOrder((MediaSource) target);
172 180
         }
173 181
         
174 182
         if (target instanceof MediaSourceManager) {
175 183
             sources.addAll(((MediaSourceManager) target).getSources());
184
+            
185
+            for (MediaSource source : ((MediaSourceManager) target).getSources()) {
186
+                addSourceToOrder(source);
187
+            }
188
+        }
189
+    }
190
+    
191
+    /**
192
+     * Checks to see if the specified media source needs to be added to our
193
+     * order list, and adds it if neccessary.
194
+     *
195
+     * @param source The media source to be tested
196
+     */
197
+    private void addSourceToOrder(final MediaSource source) {
198
+        if (!order.contains(source.getName())) {
199
+            order.add(source.getName());
176 200
         }
177 201
     }
178 202
     
@@ -219,6 +243,8 @@ public class NowPlayingPlugin extends Plugin implements EventPlugin,
219 243
     public MediaSource getBestSource() {
220 244
         MediaSource paused = null;
221 245
         
246
+        Collections.sort(sources, new MediaSourceComparator(order));
247
+        
222 248
         for (final MediaSource source : sources) {
223 249
             if (source.isRunning()) {
224 250
                 if (source.isPlaying()) {

Loading…
Откажи
Сачувај