소스 검색

Adds basic config options to the UI for the relaybot plugin.

Fixes issue CLIENT-31

Change-Id: I573c3667286859df9e8b53f92c4b105bb87b4c27
Reviewed-on: http://gerrit.dmdirc.com/1552
Automatic-Compile: Greg Holmes <greg@dmdirc.com>
Reviewed-by: Chris Smith <chris@dmdirc.com>
tags/0.6.5
Greg Holmes 13 년 전
부모
커밋
c8c66dd03f

+ 69
- 1
src/com/dmdirc/addons/relaybot/RelayBotPlugin.java 파일 보기

@@ -28,15 +28,24 @@ import com.dmdirc.ServerManager;
28 28
 import com.dmdirc.actions.ActionManager;
29 29
 import com.dmdirc.actions.CoreActionType;
30 30
 import com.dmdirc.actions.interfaces.ActionType;
31
+import com.dmdirc.addons.ui_swing.UIUtilities;
31 32
 import com.dmdirc.config.IdentityManager;
33
+import com.dmdirc.config.prefs.PluginPreferencesCategory;
34
+import com.dmdirc.config.prefs.PreferencesCategory;
35
+import com.dmdirc.config.prefs.PreferencesManager;
36
+import com.dmdirc.config.prefs.PreferencesSetting;
37
+import com.dmdirc.config.prefs.PreferencesType;
32 38
 import com.dmdirc.interfaces.ActionListener;
33 39
 import com.dmdirc.interfaces.ConfigChangeListener;
34 40
 import com.dmdirc.parser.interfaces.ChannelClientInfo;
35 41
 import com.dmdirc.parser.interfaces.Parser;
36 42
 import com.dmdirc.parser.irc.IRCParser;
37 43
 import com.dmdirc.plugins.Plugin;
44
+import com.dmdirc.plugins.PluginManager;
45
+import com.dmdirc.util.ReturnableThread;
38 46
 
39 47
 import java.util.ArrayList;
48
+import java.util.Arrays;
40 49
 import java.util.Date;
41 50
 import java.util.HashMap;
42 51
 import java.util.Map;
@@ -65,6 +74,7 @@ public class RelayBotPlugin extends Plugin implements ActionListener, ConfigChan
65 74
         ActionManager.addListener(this, CoreActionType.SERVER_CONNECTED);
66 75
         ActionManager.addListener(this, CoreActionType.SERVER_DISCONNECTED);
67 76
         ActionManager.addListener(this, CoreActionType.CHANNEL_QUIT);
77
+        IdentityManager.getGlobalConfig().addChangeListener(getDomain(), this);
68 78
 
69 79
         // Add ourself to all currently known channels that we should be
70 80
         // connected with.
@@ -160,7 +170,6 @@ public class RelayBotPlugin extends Plugin implements ActionListener, ConfigChan
160 170
     /** {@inheritDoc} */
161 171
     @Override
162 172
     public void configChanged(final String domain, final String key) {
163
-        if (!domain.equals(getDomain())) { return; }
164 173
         final boolean wasUnset = !(IdentityManager.getGlobalConfig().hasOptionString(domain, key));
165 174
 
166 175
         for (Server server : ServerManager.getServerManager().getServers()) {
@@ -236,4 +245,63 @@ public class RelayBotPlugin extends Plugin implements ActionListener, ConfigChan
236 245
 
237 246
         return null;
238 247
     }
248
+
249
+    /**
250
+     * Reads the relay bot channel data from the config.
251
+     *
252
+     * @return A multi-dimensional array of channel data.
253
+     */
254
+    public String[][] getData() {
255
+        final Map<String, String> settings = IdentityManager.getGlobalConfig()
256
+                .getOptions(getDomain());
257
+        int i = 0;
258
+        for (Map.Entry<String, String> entry : settings.entrySet()) {
259
+            if (entry.getKey().charAt(0) == '#') {
260
+                i++;
261
+            }
262
+        }
263
+        final String[][] data = new String[i][2];
264
+        i = 0;
265
+        for (Map.Entry<String, String> entry : settings.entrySet()) {
266
+            if (entry.getKey().charAt(0) == '#') {
267
+                data[i] = new String[]{entry.getKey(), entry.getValue(), };
268
+                i++;
269
+            }
270
+        }
271
+        return data;
272
+    }
273
+
274
+    /** {@inheritDoc} */
275
+    @Override
276
+    public void showConfig(final PreferencesManager manager) {
277
+        final PreferencesCategory general = new PluginPreferencesCategory(
278
+                getPluginInfo(), "Relay Bot",
279
+                "General configuration for the Relay bot plugin.");
280
+
281
+        final PreferencesCategory colours = new PluginPreferencesCategory(
282
+                getPluginInfo(), "Channels",
283
+                "Identifies where and who the bot is in channels.",
284
+                UIUtilities.invokeAndWait(
285
+                new ReturnableThread<RelayChannelPanel>() {
286
+
287
+            /** {@inheritDoc} */
288
+            @Override
289
+            public void run() {
290
+                setObject(new RelayChannelPanel(PluginManager.getPluginManager()
291
+                        .getPluginInfoByName("ui_swing").getPlugin(),
292
+                        RelayBotPlugin.this));
293
+            }
294
+        }));
295
+        colours.setInline().setInlineAfter();
296
+        general.addSetting(new PreferencesSetting(PreferencesType.BOOLEAN,
297
+                getDomain(), "joinOnDiscover", "Join on discover",
298
+                "Do you want fake clients to join the channel?"));
299
+        general.addSetting(new PreferencesSetting(PreferencesType.BOOLEAN,
300
+                getDomain(), "colourFullName", "Colour full name",
301
+                "Do you want to colour the full name?"));
302
+        manager.getCategory("Plugins").addSubCategory(general);
303
+        general.addSubCategory(colours);
304
+    }
305
+
306
+
239 307
 }

+ 210
- 0
src/com/dmdirc/addons/relaybot/RelayChannelPanel.java 파일 보기

@@ -0,0 +1,210 @@
1
+/*
2
+ * Copyright (c) 2006-2010 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
+
23
+package com.dmdirc.addons.relaybot;
24
+
25
+import com.dmdirc.addons.ui_swing.SwingController;
26
+import com.dmdirc.addons.ui_swing.dialogs.StandardInputDialog;
27
+import com.dmdirc.config.IdentityManager;
28
+import com.dmdirc.config.prefs.PreferencesInterface;
29
+import com.dmdirc.plugins.Plugin;
30
+import com.dmdirc.plugins.PluginManager;
31
+import java.awt.Dialog.ModalityType;
32
+
33
+import java.awt.event.ActionEvent;
34
+import java.awt.event.ActionListener;
35
+import java.lang.reflect.InvocationTargetException;
36
+import java.lang.reflect.Method;
37
+import java.util.ArrayList;
38
+import java.util.List;
39
+
40
+import javax.swing.JButton;
41
+import javax.swing.JPanel;
42
+import javax.swing.JScrollPane;
43
+import javax.swing.JTable;
44
+import javax.swing.event.ListSelectionEvent;
45
+import javax.swing.event.ListSelectionListener;
46
+import javax.swing.table.DefaultTableModel;
47
+
48
+import net.miginfocom.swing.MigLayout;
49
+
50
+/**
51
+ * Panel used for the relay bot channel and nickname settings in the plugin's
52
+ * config dialog.
53
+ */
54
+public class RelayChannelPanel extends JPanel implements ActionListener,
55
+        PreferencesInterface, ListSelectionListener {
56
+
57
+    /**
58
+     * A version number for this class. It should be changed whenever the class
59
+     * structure is changed (or anything else that would prevent serialized
60
+     * objects being unserialized with the new class).
61
+     */
62
+    private static final long serialVersionUID = 1;
63
+    /** The table used for displaying the options. */
64
+    private final JTable table;
65
+    /** The plugin we're associated with. */
66
+    private final transient RelayBotPlugin plugin;
67
+    /** The table headings. */
68
+    private static final String[] HEADERS = {"Channel", "Nickname", };
69
+
70
+    /** Delete button. */
71
+    private final JButton deleteButton;
72
+
73
+    /**
74
+     * Creates a new instance of NickColourPanel.
75
+     *
76
+     * @param controller The UI controller that owns this panel
77
+     * @param plugin The plugin that owns this panel
78
+     */
79
+    public RelayChannelPanel(final Plugin controller,
80
+            final RelayBotPlugin plugin) {
81
+        super();
82
+
83
+        this.plugin = plugin;
84
+
85
+        final Object[][] data = plugin.getData();
86
+
87
+        table = new JTable(new DefaultTableModel(data, HEADERS));
88
+
89
+        final JScrollPane scrollPane = new JScrollPane(table);
90
+
91
+        table.getSelectionModel().addListSelectionListener(this);
92
+        table.setFillsViewportHeight(true);
93
+        int height = 100;
94
+        try {
95
+            final Method getPrefsDialog = controller.getClass()
96
+                    .getDeclaredMethod("getPrefsDialog", (Class<?>) null);
97
+            final Object prefsDialog = getPrefsDialog.invoke(controller,
98
+                    (Class<?>) null);
99
+            final Method getPanelHeight = prefsDialog.getClass()
100
+                    .getDeclaredMethod("getPanelHeight", (Class<?>) null);
101
+            final Object panelHeight = getPanelHeight.invoke(prefsDialog,
102
+                    (Class<?>) null);
103
+            height = (Integer) panelHeight;
104
+        } catch (IllegalAccessException ex) {
105
+            height = 100;
106
+        } catch (IllegalArgumentException ex) {
107
+            height = 100;
108
+        } catch (InvocationTargetException ex) {
109
+            height = 100;
110
+        } catch (NoSuchMethodException ex) {
111
+            height = 100;
112
+        } catch (SecurityException ex) {
113
+            height = 100;
114
+        }
115
+        setLayout(new MigLayout("ins 0, fillx, hmax " + height));
116
+        add(scrollPane, "grow, push, wrap, spanx");
117
+
118
+        final JButton addButton = new JButton("Add");
119
+        addButton.addActionListener(this);
120
+        add(addButton, "sg button, growx, pushx");
121
+
122
+        deleteButton = new JButton("Delete");
123
+        deleteButton.addActionListener(this);
124
+        add(deleteButton, "sg button, growx, pushx");
125
+
126
+        deleteButton.setEnabled(false);
127
+    }
128
+
129
+    /**
130
+     * {@inheritDoc}
131
+     *
132
+     * @param e Action event
133
+     */
134
+    @Override
135
+    public void actionPerformed(final ActionEvent e) {
136
+        final DefaultTableModel model = ((DefaultTableModel) table.getModel());
137
+
138
+        if (e.getActionCommand().equals("Add")) {
139
+            addRow("#changeme", "changeme");
140
+        } else if (e.getActionCommand().equals("Delete")) {
141
+            final int row = table.getSelectedRow();
142
+            if (table.isEditing()) {
143
+                table.getCellEditor().stopCellEditing();
144
+            }
145
+            if (row > -1) {
146
+                model.removeRow(row);
147
+            }
148
+        }
149
+    }
150
+
151
+    /**
152
+     * Removes a row from the table.
153
+     *
154
+     * @param row The row to be removed
155
+     */
156
+    public void removeRow(final int row) {
157
+        ((DefaultTableModel) table.getModel()).removeRow(row);
158
+    }
159
+
160
+    /**
161
+     * Adds a row to the table.
162
+     *
163
+     * @param channel The channel setting
164
+     * @param nickname The nickname setting
165
+     */
166
+    public void addRow(final String channel, final String nickname) {
167
+        final DefaultTableModel model = ((DefaultTableModel) table.getModel());
168
+        model.addRow(new Object[]{channel, nickname, });
169
+    }
170
+
171
+    /**
172
+     * Retrieves the current data in use by this panel.
173
+     *
174
+     * @return This panel's current data.
175
+     */
176
+    public List<String[]> getData() {
177
+        final List<String[]> res = new ArrayList<String[]>();
178
+        final DefaultTableModel model = ((DefaultTableModel) table.getModel());
179
+
180
+        for (int i = 0; i < model.getRowCount(); i++) {
181
+            res.add(new String[]{(String) model.getValueAt(i, 0),
182
+            (String) model.getValueAt(i, 1), });
183
+        }
184
+
185
+        return res;
186
+    }
187
+
188
+    /** {@inheritDoc} */
189
+    @Override
190
+    public void save() {
191
+        // Remove all old config entries
192
+        for (String[] parts : plugin.getData()) {
193
+           IdentityManager.getConfigIdentity().unsetOption(plugin.getDomain(),
194
+                   parts[0]);
195
+        }
196
+
197
+        // And write the new ones
198
+        for (String[] row : getData()) {
199
+            IdentityManager.getConfigIdentity().setOption(plugin.getDomain(),
200
+                    row[0], row[1]);
201
+        }
202
+    }
203
+
204
+    /** {@inheritDoc} */
205
+    @Override
206
+    public void valueChanged(final ListSelectionEvent e) {
207
+        deleteButton.setEnabled(table.getSelectedRow() > -1
208
+                && table.getModel().getRowCount() > 0);
209
+    }
210
+}

+ 3
- 0
src/com/dmdirc/addons/relaybot/plugin.config 파일 보기

@@ -23,6 +23,9 @@ updates:
23 23
 requires:
24 24
   parent=irc
25 25
 
26
+required-services:
27
+  swing ui
28
+
26 29
 defaults:
27 30
   colourFullName=true
28 31
   joinOnDiscover=false

Loading…
취소
저장