File Source: ActionTableModel.java
/*
P/P * Method: com.dmdirc.addons.ui_swing.dialogs.actionsmanager.ActionTableModel__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.actionsmanager;
24
25 import com.dmdirc.actions.Action;
26 import com.dmdirc.actions.ActionGroup;
27 import com.dmdirc.actions.interfaces.ActionType;
28
29 import java.util.ArrayList;
30 import java.util.List;
31
32 import javax.swing.table.AbstractTableModel;
33
34 /**
35 * Action table model.
36 */
37 public class ActionTableModel extends AbstractTableModel {
38
39 /**
40 * A version number for this class. It should be changed whenever the class
41 * structure is changed (or anything else that would prevent serialized
42 * objects being unserialized with the new class).
43 */
44 private static final long serialVersionUID = 1;
45 /** Action list. */
46 private List<Action> actions;
47
48 /**
49 * Instantiates a new table model.
50 */
51 public ActionTableModel() {
/*
P/P * Method: void com.dmdirc.addons.ui_swing.dialogs.actionsmanager.ActionTableModel()
*
* Postconditions:
* this.actions == &new ArrayList(ActionTableModel#1)
* new ArrayList(ActionTableModel#1) num objects == 1
* new ArrayList(ActionTableModel#1) num objects == 0
*/
52 this(new ArrayList<Action>());
53 }
54
55 /**
56 * Instantiates a new table model.
57 *
58 * @param actions Actions to show
59 */
60 public ActionTableModel(final List<Action> actions) {
/*
P/P * Method: void com.dmdirc.addons.ui_swing.dialogs.actionsmanager.ActionTableModel(List)
*
* Postconditions:
* this.actions == One-of{&new ArrayList(ActionTableModel#1), actions}
* this.actions != null
* new ArrayList(ActionTableModel#1) num objects <= 1
*
* Test Vectors:
* actions: Inverse{null}, Addr_Set{null}
*/
61 super();
62
63 if (actions == null) {
64 this.actions = new ArrayList<Action>();
65 } else {
66 this.actions = actions;
67 }
68 }
69
70 /** {@inheritDoc} */
71 @Override
72 public int getRowCount() {
/*
P/P * Method: int getRowCount()
*
* Preconditions:
* this.actions != null
*
* Postconditions:
* init'ed(return_value)
*/
73 synchronized (actions) {
74 return actions.size();
75 }
76 }
77
78 /** {@inheritDoc} */
79 @Override
80 public int getColumnCount() {
/*
P/P * Method: int getColumnCount()
*
* Postconditions:
* return_value == 3
*/
81 return 3;
82 }
83
84 /** {@inheritDoc} */
85 @Override
86 public String getColumnName(final int columnIndex) {
/*
P/P * Method: String getColumnName(int)
*
* Preconditions:
* columnIndex in {0..2}
*
* Postconditions:
* return_value in Addr_Set{&"Name",&"Trigger",&"Response"}
*
* Test Vectors:
* columnIndex: {0}, {1}, {2}
*/
87 switch (columnIndex) {
88 case 0:
89 return "Name";
90 case 1:
91 return "Trigger";
92 case 2:
93 return "Response";
94 default:
95 throw new IllegalArgumentException("Unknown column: " +
96 columnIndex);
97 }
98 }
99
100 /** {@inheritDoc} */
101 @Override
102 public Class<?> getColumnClass(final int columnIndex) {
/*
P/P * Method: Class getColumnClass(int)
*
* Preconditions:
* columnIndex in {0..2}
*
* Test Vectors:
* columnIndex: {0}, {1}, {2}
*/
103 switch (columnIndex) {
104 case 0:
105 return String.class;
106 case 1:
107 return ActionType.class;
108 case 2:
109 return String[].class;
110 default:
111 throw new IllegalArgumentException("Unknown column: " +
112 columnIndex);
113 }
114 }
115
116 /** {@inheritDoc} */
117 @Override
118 public Object getValueAt(final int rowIndex, final int columnIndex) {
/*
P/P * Method: Object getValueAt(int, int)
*
* Preconditions:
* columnIndex in {0..2}
* this.actions != null
*
* Presumptions:
* com.dmdirc.actions.Action:getTriggers(...).length@124 >= 1
* com.dmdirc.actions.Action:getTriggers(...)@124 != null
* java.util.List:get(...)@122 != null
* java.util.List:get(...)@124 != null
* java.util.List:get(...)@126 != null
*
* Postconditions:
* init'ed(return_value)
*
* Test Vectors:
* columnIndex: {0}, {1}, {2}
*/
119 synchronized (actions) {
120 switch (columnIndex) {
121 case 0:
122 return actions.get(rowIndex).getName();
123 case 1:
124 return actions.get(rowIndex).getTriggers()[0];
125 case 2:
126 return actions.get(rowIndex).getResponse();
127 default:
128 throw new IllegalArgumentException("Unknown column: " +
129 columnIndex);
130 }
131 }
132 }
133
134 /**
135 * Returns the action at the specified row.
136 *
137 * @param rowIndex Row index
138 *
139 * @return Action
140 */
141 public Action getAction(final int rowIndex) {
/*
P/P * Method: Action getAction(int)
*
* Preconditions:
* this.actions != null
*
* Postconditions:
* init'ed(return_value)
*/
142 synchronized (actions) {
143 return actions.get(rowIndex);
144 }
145 }
146
147 /**
148 * Returns the row index of the specified index.
149 *
150 * @param action Action to get
151 *
152 * @return Action row index or -1 if not found.
153 */
154 public int getAction(final Action action) {
/*
P/P * Method: int getAction(Action)
*
* Preconditions:
* this.actions != null
*
* Postconditions:
* init'ed(return_value)
*/
155 synchronized (actions) {
156 return actions.indexOf(action);
157 }
158 }
159
160 /**
161 * Replaces the model data with the specified action group.
162 *
163 * @param group New Action group
164 */
165 public void setActionGroup(final ActionGroup group) {
/*
P/P * Method: void setActionGroup(ActionGroup)
*
* Preconditions:
* init'ed(this.actions)
*
* Postconditions:
* init'ed(this.actions)
* new ArrayList(setActionGroup#1) num objects <= 1
*
* Test Vectors:
* group: Inverse{null}, Addr_Set{null}
*/
166 synchronized (actions) {
167 if (group == null) {
168 actions = new ArrayList<Action>();
169 } else {
170 actions = group.getActions();
171 }
172 fireTableDataChanged();
173 }
174 }
175
176 /**
177 * Adds an action to the model.
178 *
179 * @param action Action to add
180 */
181 public void add(final Action action) {
/*
P/P * Method: void add(Action)
*
* Preconditions:
* (soft) this.actions != null
*
* Presumptions:
* java.util.List:size(...)@187 >= -231+1
*
* Test Vectors:
* action: Inverse{null}, Addr_Set{null}
*/
182 synchronized (actions) {
183 if (action == null) {
184 return;
185 }
186 actions.add(action);
187 fireTableRowsInserted(actions.size() - 1, actions.size() - 1);
188 }
189 }
190
191 /**
192 * Removes an action from the model.
193 *
194 * @param action Action to remove
195 */
196 public void remove(final Action action) {
/*
P/P * Method: void remove(Action)
*
* Preconditions:
* (soft) this.actions != null
*
* Test Vectors:
* action: Inverse{null}, Addr_Set{null}
*/
197 if (action == null) {
198 return;
199 }
200 remove(actions.indexOf(action));
201 }
202
203 /**
204 * Removes an action from the model.
205 *
206 * @param index Index of the action to remove
207 */
208 public void remove(final int index) {
/*
P/P * Method: void remove(int)
*
* Preconditions:
* (soft) this.actions != null
*/
209 synchronized (actions) {
210 if (index != -1) {
211 actions.remove(index);
212 fireTableRowsDeleted(index, index);
213 }
214 }
215 }
216
217 /**
218 * Checks if this model contains the specified action.
219 *
220 * @param action Action to check for
221 *
222 * @return true if the action exists
223 */
224 public boolean contains(final Action action) {
/*
P/P * Method: bool contains(Action)
*
* Preconditions:
* this.actions != null
*
* Postconditions:
* init'ed(return_value)
*/
225 synchronized (actions) {
226 return actions.contains(action);
227 }
228 }
229
230 public int findAction(final String name) {
/*
P/P * Method: int findAction(String)
*
* Preconditions:
* this.actions != null
*
* Presumptions:
* com.dmdirc.actions.Action:getName(...)@234 != null
* java.util.Iterator:next(...)@233 != null
*
* Postconditions:
* init'ed(return_value)
*
* Test Vectors:
* java.lang.String:equals(...)@234: {0}, {1}
* java.util.Iterator:hasNext(...)@233: {1}, {0}
*/
231 int location = -1;
232 synchronized (actions) {
233 for(Action action : actions) {
234 if (action.getName().equals(name)) {
235 location = actions.indexOf(action);
236 }
237 }
238 }
239 return location;
240 }
241 }
SofCheck Inspector Build Version : 2.17854
| ActionTableModel.java |
2009-Jun-25 01:54:24 |
| ActionTableModel.class |
2009-Sep-02 17:04:16 |