|
@@ -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
|
+}
|