File Source: ChannelListModesPane.java
/*
P/P * Method: com.dmdirc.addons.ui_swing.dialogs.channelsetting.ChannelListModesPane__static_init
*/
1 /*
2 * Copyright (c) 2006-2009 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.ui_swing.dialogs.channelsetting;
24
25 import com.dmdirc.addons.ui_swing.components.renderers.ListModeCellRenderer;
26 import com.dmdirc.addons.ui_swing.components.renderers.ExtendedListModeCellRenderer;
27 import com.dmdirc.addons.ui_swing.UIUtilities;
28 import com.dmdirc.Channel;
29 import com.dmdirc.config.IdentityManager;
30 import com.dmdirc.interfaces.ConfigChangeListener;
31 import com.dmdirc.parser.irc.ChannelListModeItem;
32 import com.dmdirc.util.MapList;
33
34 import java.awt.Insets;
35 import java.awt.event.ActionEvent;
36 import java.awt.event.ActionListener;
37 import java.util.ArrayList;
38 import java.util.Enumeration;
39 import java.util.HashMap;
40 import java.util.List;
41 import java.util.Map;
42 import java.util.Map.Entry;
43
44 import javax.swing.BorderFactory;
45 import javax.swing.DefaultComboBoxModel;
46 import javax.swing.DefaultListModel;
47 import javax.swing.JButton;
48 import javax.swing.JCheckBox;
49 import javax.swing.JComboBox;
50 import javax.swing.JLabel;
51 import javax.swing.JList;
52 import javax.swing.JOptionPane;
53 import javax.swing.JPanel;
54 import javax.swing.JScrollPane;
55 import javax.swing.ListCellRenderer;
56 import javax.swing.event.ListSelectionEvent;
57 import javax.swing.event.ListSelectionListener;
58
59 import net.miginfocom.swing.MigLayout;
60
61 /** List modes panel. */
62 public final class ChannelListModesPane extends JPanel implements ActionListener,
63 ListSelectionListener, ConfigChangeListener {
64
65 /**
66 * A version number for this class. It should be changed whenever the class
67 * structure is changed (or anything else that would prevent serialized
68 * objects being unserialized with the new class).
69 */
70 private static final long serialVersionUID = 5;
71 /** Channel. */
72 private final Channel channel;
73 /** Combox box used to switch between list modes. */
74 private final JComboBox listModesMenu;
75 /** Arraylist of jpanels containing the listmodes. */
76 private final List<JList> listModesPanels;
77 /** JPanel used to show listmodespanels in. */
78 private final JScrollPane listModesPanel;
79 /** Add list mode button. */
80 private final JButton addListModeButton;
81 /** Remove list mode button. */
82 private final JButton removeListModeButton;
83 /** list modes available on this server. */
84 private final char[] listModesArray;
85 /** Modes on creation. */
86 private final MapList<Character, ChannelListModeItem> existingListItems;
87 /** Mode count label. */
88 private final JLabel modeCount;
89 /** Cell renderer. */
90 private ListCellRenderer renderer;
91 /** Extended info toggle. */
92 private final JCheckBox toggle;
93
94 /**
95 * Creates a new instance of ChannelListModePane.
96 *
97 * @param channel Parent channel
98 */
99 public ChannelListModesPane(final Channel channel) {
/*
P/P * Method: void com.dmdirc.addons.ui_swing.dialogs.channelsetting.ChannelListModesPane(Channel)
*
* Preconditions:
* channel != null
*
* Presumptions:
* com.dmdirc.Channel:getServer(...)@113 != null
* com.dmdirc.Server:getParser(...)@113 != null
* com.dmdirc.config.IdentityManager:getGlobalConfig(...)@105 != null
* com.dmdirc.config.IdentityManager:getGlobalConfig(...)@123 != null
* com.dmdirc.parser.irc.IRCParser:getListChanModes(...)@113 != null
* ...
*
* Postconditions:
* this.addListModeButton == &new JButton(ChannelListModesPane#8)
* this.channel == channel
* this.channel != null
* this.existingListItems == &new MapList(ChannelListModesPane#5)
* this.listModesArray != null
* this.listModesMenu == &new JComboBox(ChannelListModesPane#6)
* this.listModesPanel == &new JScrollPane(ChannelListModesPane#3)
* this.listModesPanels == &new ArrayList(ChannelListModesPane#4)
* this.modeCount == &new JLabel(ChannelListModesPane#10)
* this.removeListModeButton == &new JButton(ChannelListModesPane#9)
* ...
*
* Test Vectors:
* com.dmdirc.config.ConfigManager:getOptionBool(...)@105: {0}, {1}
*/
100 super();
101
102 this.setOpaque(UIUtilities.getTabbedPaneOpaque());
103 this.channel = channel;
104
105 if (IdentityManager.getGlobalConfig().getOptionBool("general",
106 "extendedListModes")) {
107 renderer = new ExtendedListModeCellRenderer();
108 } else {
109 renderer = new ListModeCellRenderer();
110 }
111 listModesPanel = new JScrollPane();
112 listModesPanels = new ArrayList<JList>();
113 listModesArray =
114 channel.getServer().getParser().getListChanModes().toCharArray();
115 existingListItems =
116 new MapList<Character, ChannelListModeItem>();
117 listModesMenu =
118 new JComboBox(new DefaultComboBoxModel());
119 addListModeButton = new JButton("Add");
120 removeListModeButton = new JButton("Remove");
121 removeListModeButton.setEnabled(false);
122 modeCount = new JLabel();
123 toggle = new JCheckBox("Show extended information",
124 IdentityManager.getGlobalConfig().getOptionBool("general",
125 "extendedListModes"));
126 toggle.setOpaque(UIUtilities.getTabbedPaneOpaque());
127 toggle.setMargin(new Insets(0, 0, 0, 0));
128 toggle.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));
129
130 initListModesPanel();
131 initListeners();
132
133 setVisible(true);
134 }
135
136 /** Updates the panel. */
137 public void update() {
/*
P/P * Method: void update()
*
* Preconditions:
* this.existingListItems != null
* this.listModesArray != null
* this.listModesArray.length <= 232-1
* this.listModesPanel != null
* (soft) this.channel != null
* (soft) init'ed(this.listModesArray[...])
* (soft) this.listModesPanels != null
*
* Presumptions:
* com.dmdirc.Channel:getChannelInfo(...)@149 != null
* com.dmdirc.Channel:getChannelInfo(...)@152 != null
* com.dmdirc.parser.irc.ChannelInfo:getListModeParam(...)@152 != null
* java.util.List:get(...)@154 != null
* javax.swing.JList:getModel(...)@154 != null
*
* Test Vectors:
* java.util.Iterator:hasNext(...)@158: {0}, {1}
* javax.swing.JScrollPane:isVisible(...)@138: {0}, {1}
*/
138 final boolean visible = listModesPanel.isVisible();
139
140 if (visible) {
141 listModesPanel.setVisible(false);
142 }
143
144 existingListItems.clear();
145
146 for (int i = 0; i < listModesArray.length;
147 i++) {
148 final char mode = listModesArray[i];
149 existingListItems.add(mode,
150 new ArrayList<ChannelListModeItem>(channel.getChannelInfo().
151 getListModeParam(mode)));
152 final List<ChannelListModeItem> listItems =
153 channel.getChannelInfo().getListModeParam(mode);
154 final DefaultListModel model =
155 (DefaultListModel) listModesPanels.get(i).getModel();
156
157 model.removeAllElements();
158 for (ChannelListModeItem listItem : listItems) {
159 model.addElement(listItem);
160 }
161 }
162
163 if (visible) {
164 listModesPanel.setVisible(true);
165 }
166 }
167
168 /** Updates the list mode menu. */
169 private void updateMenu() {
/*
P/P * Method: void updateMenu()
*
* Preconditions:
* this.addListModeButton != null
* this.listModesArray != null
* this.listModesArray.length <= 232-1
* this.listModesMenu != null
* (soft) this.channel != null
* (soft) init'ed(this.listModesArray[...])
* (soft) this.listModesPanel != null
* (soft) this.listModesPanels != null
* (soft) this.modeCount != null
* (soft) init'ed(this.renderer)
*
* Presumptions:
* com.dmdirc.Channel:getConfigManager(...)@183 != null
* com.dmdirc.Channel:getConfigManager(...)@185 != null
* com.dmdirc.config.IdentityManager:getGlobalConfig(...)@183 != null
* javax.swing.JComboBox:getModel(...)@179 != null
*
* Test Vectors:
* this.listModesArray.length: {1..232-1}, {0}
* com.dmdirc.config.ConfigManager:getOptionBool(...)@183: {0}, {1}
* com.dmdirc.config.ConfigManager:hasOptionString(...)@183: {0}, {1}
* java.util.List:isEmpty(...)@199: {0}, {1}
*/
170 if (listModesArray.length == 0) {
171 listModesMenu.setEnabled(false);
172 addListModeButton.setEnabled(false);
173 return;
174 } else {
175 listModesMenu.setEnabled(true);
176 addListModeButton.setEnabled(true);
177 }
178
179 final DefaultComboBoxModel model =
180 (DefaultComboBoxModel) listModesMenu.getModel();
181 for (char mode : listModesArray) {
182 String modeText = mode + " list";
183 if (IdentityManager.getGlobalConfig().getOptionBool("server", "friendlymodes") &&
184 channel.getConfigManager().hasOptionString("server", "mode" + mode)) {
185 modeText =
186 channel.getConfigManager().
187 getOption("server", "mode" + mode) + " list";
188 }
189 model.addElement(modeText);
190
191 final JList list =
192 new JList(new DefaultListModel());
193 list.setCellRenderer(renderer);
194 list.setVisibleRowCount(8);
195 list.addListSelectionListener(this);
196
197 listModesPanels.add(list);
198 }
199 if (listModesPanels.isEmpty()) {
200 listModesPanel.setViewportView(new JPanel());
201 } else {
202 listModesPanel.setViewportView(listModesPanels.get(0));
203 }
204 updateModeCount();
205 listModesPanel.setVisible(true);
206 }
207
208 /** Initialises the list modes panel. */
209 private void initListModesPanel() {
/*
P/P * Method: void initListModesPanel()
*
* Preconditions:
* this.addListModeButton != null
* this.existingListItems != null
* this.listModesArray != null
* this.listModesMenu != null
* (soft) this.channel != null
* (soft) this.listModesArray.length in {1..232-1}
* (soft) init'ed(this.listModesArray[...])
* (soft) this.listModesPanel != null
* (soft) this.listModesPanels != null
* (soft) this.modeCount != null
* ...
*/
210 updateMenu();
211
212 setLayout(new MigLayout("fill, wrap 1"));
213
214 add(listModesMenu, "growx, pushx");
215 add(listModesPanel, "grow, push");
216 add(modeCount, "split 2, growx, pushx");
217 add(toggle, "alignx center");
218 add(addListModeButton, "split 2, growx, pushx");
219 add(removeListModeButton, "growx, pushx");
220
221 update();
222 updateModeCount();
223 }
224
225 /** Initialises listeners for this dialog. */
226 private void initListeners() {
/*
P/P * Method: void initListeners()
*
* Preconditions:
* this.addListModeButton != null
* this.listModesMenu != null
* this.removeListModeButton != null
* this.toggle != null
*
* Presumptions:
* com.dmdirc.config.IdentityManager:getGlobalConfig(...)@231 != null
*/
227 addListModeButton.addActionListener(this);
228 removeListModeButton.addActionListener(this);
229 listModesMenu.addActionListener(this);
230 toggle.addActionListener(this);
231 IdentityManager.getGlobalConfig().addChangeListener("general",
232 "extendedListModes", this);
233
234 }
235
236 /** Sends the list modes to the server. */
237 public void save() {
/*
P/P * Method: void save()
*
* Preconditions:
* this.channel != null
* this.listModesArray != null
* this.listModesArray.length <= 232-1
* this.toggle != null
* (soft) this.existingListItems != null
* (soft) init'ed(this.listModesArray[...])
* (soft) this.listModesPanels != null
*
* Presumptions:
* com.dmdirc.Channel:getChannelInfo(...)@266 != null
* com.dmdirc.Channel:getChannelInfo(...)@273 != null
* com.dmdirc.Channel:getChannelInfo(...)@277 != null
* com.dmdirc.config.IdentityManager:getConfigIdentity(...)@279 != null
* com.dmdirc.util.MapList:get(...)@248 != null
* ...
*
* Test Vectors:
* java.util.Enumeration:hasMoreElements(...)@255: {0}, {1}
* java.util.Iterator:hasNext(...)@251: {0}, {1}
* java.util.Iterator:hasNext(...)@262: {0}, {1}
* java.util.Iterator:hasNext(...)@272: {0}, {1}
* java.util.Map:containsKey(...)@263: {0}, {1}
*/
238 final Map<ChannelListModeItem, Character> currentModes =
239 new HashMap<ChannelListModeItem, Character>();
240 final Map<ChannelListModeItem, Character> newModes =
241 new HashMap<ChannelListModeItem, Character>();
242
243 for (int i = 0; i < listModesArray.length;
244 i++) {
245 final char mode = listModesArray[i];
246 final Enumeration<?> values =
247 ((DefaultListModel) listModesPanels.get(i).getModel()).elements();
248 final List<ChannelListModeItem> listItems =
249 existingListItems.get(mode);
250
251 for (ChannelListModeItem listItem : listItems) {
252 currentModes.put(listItem, mode);
253 }
254
255 while (values.hasMoreElements()) {
256 final ChannelListModeItem value =
257 (ChannelListModeItem) values.nextElement();
258 newModes.put(value, mode);
259 }
260 }
261
262 for (Entry<ChannelListModeItem, Character> entry : newModes.entrySet()) {
263 if (currentModes.containsKey(entry.getKey())) {
264 currentModes.remove(entry.getKey());
265 } else {
266 channel.getChannelInfo().
267 alterMode(true, entry.getValue(),
268 entry.getKey().getItem());
269 }
270 }
271
272 for (Entry<ChannelListModeItem, Character> entry : currentModes.entrySet()) {
273 channel.getChannelInfo().
274 alterMode(false, entry.getValue(), entry.getKey().getItem());
275 }
276
277 channel.getChannelInfo().sendModes();
278
279 IdentityManager.getConfigIdentity().setOption("general",
280 "extendedListModes", toggle.isSelected());
281 }
282
283 /** Adds a list mode. */
284 private void addListMode() {
/*
P/P * Method: void addListMode()
*
* Preconditions:
* this.channel != null
* this.listModesArray != null
* this.listModesArray.length >= 1
* this.listModesMenu != null
* this.listModesPanels != null
* this.modeCount != null
* (soft) init'ed(this.listModesArray[...])
*
* Presumptions:
* com.dmdirc.Channel:getConfigManager(...)@288 != null
* com.dmdirc.Channel:getConfigManager(...)@290 != null
* java.util.List:get(...)@296 != null
* javax.swing.JComboBox:getSelectedIndex(...)@285 >= 0
* javax.swing.JComboBox:getSelectedIndex(...)@285 < this.listModesArray.length
* ...
*
* Test Vectors:
* com.dmdirc.config.ConfigManager:hasOptionString(...)@288: {0}, {1}
* java.lang.String:isEmpty(...)@295: {0}, {1}
* java.lang.String:isEmpty(...)@295: {1}, {0}
* javax.swing.JOptionPane:showInputDialog(...)@293: Addr_Set{null}, Inverse{null}
*/
285 final int selectedIndex = listModesMenu.getSelectedIndex();
286 String modeText = String.valueOf(listModesArray[selectedIndex]);
287 String modeMask;
288 if (channel.getConfigManager().hasOptionString("server", "mode"
289 + listModesArray[selectedIndex])) {
290 modeText = channel.getConfigManager().
291 getOption("server", "mode" + listModesArray[selectedIndex]);
292 }
293 modeMask = JOptionPane.showInputDialog(listModesPanel,
294 "Please enter the hostmask for the new " + modeText);
295 if (modeMask != null && (!modeMask.isEmpty() || !modeMask.isEmpty())) {
296 final DefaultListModel model =
297 (DefaultListModel) listModesPanels.get(selectedIndex).
298 getModel();
299 model.addElement(new ChannelListModeItem(modeMask, "",
300 System.currentTimeMillis() / 1000));
301 }
302 updateModeCount();
303 }
304
305 /** Removes a list mode. */
306 private void removeListMode() {
/*
P/P * Method: void removeListMode()
*
* Preconditions:
* this.listModesMenu != null
* this.listModesPanels != null
* this.modeCount != null
* (soft) this.channel != null
* (soft) this.listModesArray != null
* (soft) this.listModesArray.length >= 1
* (soft) init'ed(this.listModesArray[...])
*
* Presumptions:
* arr$.length@309 <= 232-1
* java.util.List:get(...)@308 != null
* javax.swing.JList:getModel(...)@310 != null
* javax.swing.JList:getSelectedValues(...)@309 != null
*/
307 final int selectedIndex = listModesMenu.getSelectedIndex();
308 final JList list = listModesPanels.get(selectedIndex);
309 for (Object mode : list.getSelectedValues()) {
310 ((DefaultListModel) list.getModel()).removeElement(mode);
311 }
312 updateModeCount();
313 }
314
315 /**
316 * {@inheritDoc}
317 *
318 * @param event Action event
319 */
320 @Override
321 public void actionPerformed(final ActionEvent event) {
/*
P/P * Method: void actionPerformed(ActionEvent)
*
* Preconditions:
* event != null
* this.listModesMenu != null
* (soft) this.addListModeButton != null
* (soft) this.channel != null
* (soft) this.listModesArray != null
* (soft) this.listModesArray.length >= 1
* (soft) init'ed(this.listModesArray[...])
* (soft) this.listModesPanel != null
* (soft) this.listModesPanels != null
* (soft) this.modeCount != null
* ...
*
* Presumptions:
* java.util.Iterator:next(...)@337 != null
*
* Postconditions:
* this.renderer == One-of{old this.renderer, &new ExtendedListModeCellRenderer(actionPerformed#1), &new ListModeCellRenderer(actionPerformed#2)}
* new ExtendedListModeCellRenderer(actionPerformed#1) num objects <= 1
* new ListModeCellRenderer(actionPerformed#2) num objects <= 1
*
* Test Vectors:
* java.lang.Object:equals(...)@322: {0}, {1}
* java.lang.Object:equals(...)@327: {0}, {1}
* java.lang.Object:equals(...)@329: {0}, {1}
* java.lang.Object:equals(...)@331: {0}, {1}
* java.util.Iterator:hasNext(...)@337: {0}, {1}
* javax.swing.JCheckBox:isSelected(...)@332: {0}, {1}
*/
322 if (listModesMenu.equals(event.getSource())) {
323 final int selectedIndex = listModesMenu.getSelectedIndex();
324 listModesPanel.setVisible(false);
325 listModesPanel.setViewportView(listModesPanels.get(selectedIndex));
326 listModesPanel.setVisible(true);
327 } else if (addListModeButton.equals(event.getSource())) {
328 addListMode();
329 } else if (removeListModeButton.equals(event.getSource())) {
330 removeListMode();
331 } else if (toggle.equals(event.getSource())) {
332 if (toggle.isSelected()) {
333 renderer = new ExtendedListModeCellRenderer();
334 } else {
335 renderer = new ListModeCellRenderer();
336 }
337 for (JList list : listModesPanels) {
338 list.setCellRenderer(renderer);
339 }
340 }
341 }
342
343 /**
344 * {@inheritDoc}
345 *
346 * @param event List selection event
347 */
348 @Override
349 public void valueChanged(final ListSelectionEvent event) {
/*
P/P * Method: void valueChanged(ListSelectionEvent)
*
* Preconditions:
* event != null
* (soft) this.removeListModeButton != null
*
* Presumptions:
* javax.swing.event.ListSelectionEvent:getSource(...)@351 != null
*
* Test Vectors:
* javax.swing.JList:getSelectedIndex(...)@351: {-231..-2, 0..232-1}, {-1}
* javax.swing.event.ListSelectionEvent:getValueIsAdjusting(...)@350: {1}, {0}
*/
350 if (!event.getValueIsAdjusting()) {
351 final int selected =
352 ((JList) event.getSource()).getSelectedIndex();
353 if (selected == -1) {
354 removeListModeButton.setEnabled(false);
355 } else {
356 removeListModeButton.setEnabled(true);
357 }
358 }
359 }
360
361 /** Updates the mode count label. */
362 private void updateModeCount() {
/*
P/P * Method: void updateModeCount()
*
* Preconditions:
* this.listModesPanels != null
* this.modeCount != null
* (soft) this.channel != null
* (soft) this.listModesArray != null
* (soft) this.listModesArray.length >= 1
* (soft) init'ed(this.listModesArray[...])
* (soft) this.listModesMenu != null
*
* Presumptions:
* com.dmdirc.Channel:getServer(...)@370 != null
* com.dmdirc.Server:getParser(...)@370 != null
* java.util.List:get(...)@369 != null
* javax.swing.JComboBox:getSelectedIndex(...)@368 >= 0
* javax.swing.JComboBox:getSelectedIndex(...)@368 < this.listModesArray.length
* ...
*
* Test Vectors:
* com.dmdirc.parser.irc.IRCParser:getMaxListModes(...)@370: {-231..-2, 0..232-1}, {-1}
* java.util.List:isEmpty(...)@363: {0}, {1}
*/
363 if (listModesPanels.isEmpty()) {
364 modeCount.setText(null);
365 return;
366 }
367
368 final int selected = listModesMenu.getSelectedIndex();
369 final int current = listModesPanels.get(selected).getModel().getSize();
370 final int maxModes = channel.getServer().getParser().
371 getMaxListModes(listModesArray[selected]);
372
373 if (maxModes == -1) {
374 modeCount.setText(current + " mode" + (current == 1 ? "" : "s") + " set");
375 } else {
376 modeCount.setText(current + " mode" + (current == 1 ? "" : "s")
377 + " set (maximum of " + maxModes + ")");
378 }
379 }
380
381 /** {@inheritDoc} */
382 @Override
383 public void configChanged(final String domain, final String key) {
/*
P/P * Method: void configChanged(String, String)
*
* Preconditions:
* this.listModesPanels != null
*
* Presumptions:
* com.dmdirc.config.IdentityManager:getGlobalConfig(...)@384 != null
* java.util.Iterator:next(...)@390 != null
*
* Postconditions:
* this.renderer in Addr_Set{&new ExtendedListModeCellRenderer(configChanged#2),&new ListModeCellRenderer(configChanged#1)}
* new ExtendedListModeCellRenderer(configChanged#2) num objects <= 1
* new ListModeCellRenderer(configChanged#1) num objects <= 1
*
* Test Vectors:
* com.dmdirc.config.ConfigManager:getOptionBool(...)@384: {0}, {1}
* java.util.Iterator:hasNext(...)@390: {0}, {1}
*/
384 if (IdentityManager.getGlobalConfig().getOptionBool("general",
385 "extendedListModes")) {
386 renderer = new ListModeCellRenderer();
387 } else {
388 renderer = new ExtendedListModeCellRenderer();
389 }
390 for (JList list : listModesPanels) {
391 list.setCellRenderer(renderer);
392 }
393 }
394 }
SofCheck Inspector Build Version : 2.17854
| ChannelListModesPane.java |
2009-Jun-25 01:54:24 |
| ChannelListModesPane.class |
2009-Sep-02 17:04:16 |