File Source: URLHandlerTableModel.java
/*
P/P * Method: com.dmdirc.addons.ui_swing.dialogs.prefs.URLHandlerTableModel__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.prefs;
24
25 import com.dmdirc.config.IdentityManager;
26
27 import java.net.URI;
28 import java.util.ArrayList;
29 import java.util.HashMap;
30 import java.util.List;
31 import java.util.Map;
32
33 import javax.swing.table.AbstractTableModel;
34
35 /**
36 * URL Handler table model.
37 */
38 public class URLHandlerTableModel extends AbstractTableModel {
39
40 /**
41 * A version number for this class. It should be changed whenever the class
42 * structure is changed (or anything else that would prevent serialized
43 * objects being unserialized with the new class).
44 */
45 private static final long serialVersionUID = 3;
46 /** Data list. */
47 private List<URI> uris;
48 /** Handlers list. */
49 private List<String> handlers;
50
51 /**
52 * Instantiates a new table model.
53 */
54 public URLHandlerTableModel() {
/*
P/P * Method: void com.dmdirc.addons.ui_swing.dialogs.prefs.URLHandlerTableModel()
*
* Postconditions:
* this.handlers == &new ArrayList(URLHandlerTableModel#2)
* this.uris == &new ArrayList(URLHandlerTableModel#1)
* new ArrayList(URLHandlerTableModel#1) num objects == 1
* new ArrayList(URLHandlerTableModel#2) num objects == 1
*/
55 this(new ArrayList<URI>(), new ArrayList<String>());
56 }
57
58 /**
59 * Instantiates a new table model.
60 *
61 * @param uris URIs to show
62 * @param handlers Handlers to show
63 */
64 public URLHandlerTableModel(final List<URI> uris,
/*
P/P * Method: void com.dmdirc.addons.ui_swing.dialogs.prefs.URLHandlerTableModel(List, List)
*
* Postconditions:
* this.handlers == handlers
* init'ed(this.handlers)
* this.uris == uris
* init'ed(this.uris)
*/
65 final List<String> handlers) {
66 this.uris = uris;
67 this.handlers = handlers;
68 }
69
70 /** {@inheritDoc} */
71 @Override
72 public int getRowCount() {
/*
P/P * Method: int getRowCount()
*
* Preconditions:
* this.uris != null
*
* Postconditions:
* init'ed(return_value)
*/
73 return uris.size();
74 }
75
76 /** {@inheritDoc} */
77 @Override
78 public int getColumnCount() {
/*
P/P * Method: int getColumnCount()
*
* Postconditions:
* return_value == 2
*/
79 return 2;
80 }
81
82 /** {@inheritDoc} */
83 @Override
84 public String getColumnName(final int columnIndex) {
/*
P/P * Method: String getColumnName(int)
*
* Preconditions:
* columnIndex in {0,1}
*
* Postconditions:
* return_value in Addr_Set{&"Protocol",&"Handler"}
*
* Test Vectors:
* columnIndex: {0}, {1}
*/
85 switch (columnIndex) {
86 case 0:
87 return "Protocol";
88 case 1:
89 return "Handler";
90 default:
91 throw new IllegalArgumentException("Unknown column: " +
92 columnIndex);
93 }
94 }
95
96 /** {@inheritDoc} */
97 @Override
98 public Class<?> getColumnClass(final int columnIndex) {
/*
P/P * Method: Class getColumnClass(int)
*
* Preconditions:
* columnIndex in {0,1}
*
* Test Vectors:
* columnIndex: {0}, {1}
*/
99 switch (columnIndex) {
100 case 0:
101 return URI.class;
102 case 1:
103 return String.class;
104 default:
105 throw new IllegalArgumentException("Unknown column: " +
106 columnIndex);
107 }
108 }
109
110 /** {@inheritDoc} */
111 @Override
112 public Object getValueAt(int rowIndex, int columnIndex) {
/*
P/P * Method: Object getValueAt(int, int)
*
* Preconditions:
* columnIndex in {0,1}
* rowIndex in {0..232-2}
* this.uris != null
* (soft) this.handlers != null
*
* Presumptions:
* java.util.List:size(...)@113 >= 1
* java.util.List:size(...)@113 - rowIndex in {1..232-1}
*
* Postconditions:
* init'ed(return_value)
*
* Test Vectors:
* columnIndex: {0}, {1}
*/
113 if (uris.size() <= rowIndex) {
114 throw new IndexOutOfBoundsException(rowIndex + " >= " +
115 uris.size());
116 }
117 if (rowIndex < 0) {
118 throw new IllegalArgumentException("Must specify a positive integer");
119 }
120 switch (columnIndex) {
121 case 0:
122 return uris.get(rowIndex);
123 case 1:
124 return handlers.get(rowIndex);
125 default:
126 throw new IllegalArgumentException("Unknown column: " +
127 columnIndex);
128 }
129 }
130
131 /** {@inheritDoc} */
132 @Override
133 public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
/*
P/P * Method: void setValueAt(Object, int, int)
*
* Preconditions:
* columnIndex in {0,1}
* rowIndex in {0..232-2}
* this.uris != null
* (soft) this.handlers != null
*
* Presumptions:
* java.lang.String:instanceof(...)@149 == 1
* java.net.URI:instanceof(...)@143 == 1
* java.util.List:size(...)@134 >= 1
* java.util.List:size(...)@134 - rowIndex in {1..232-1}
*
* Test Vectors:
* columnIndex: {0}, {1}
*/
134 if (uris.size() <= rowIndex) {
135 throw new IndexOutOfBoundsException(rowIndex + " >= " +
136 uris.size());
137 }
138 if (rowIndex < 0) {
139 throw new IllegalArgumentException("Must specify a positive integer");
140 }
141 switch (columnIndex) {
142 case 0:
143 if (!(aValue instanceof URI)) {
144 throw new IllegalArgumentException("Value must be a URI");
145 }
146 uris.set(rowIndex, (URI) aValue);
147 break;
148 case 1:
149 if (!(aValue instanceof String)) {
150 throw new IllegalArgumentException("Value must be a String");
151 }
152 handlers.set(rowIndex, (String) aValue);
153 break;
154 default:
155 throw new IllegalArgumentException("Unknown column: " +
156 columnIndex);
157 }
158 fireTableCellUpdated(rowIndex, columnIndex);
159 }
160
161 /**
162 * Adds a URI to the model.
163 *
164 * @param uri URI to add
165 */
166 public void addURI(final URI uri) {
167 final String handler;
/*
P/P * Method: void addURI(URI)
*
* Preconditions:
* this.handlers != null
* this.uris != null
* uri != null
*
* Presumptions:
* com.dmdirc.config.IdentityManager:getGlobalConfig(...)@168 != null
* com.dmdirc.config.IdentityManager:getGlobalConfig(...)@169 != null
* java.util.List:size(...)@175 >= -231+1
*
* Test Vectors:
* com.dmdirc.config.ConfigManager:hasOptionString(...)@168: {0}, {1}
*/
168 if (IdentityManager.getGlobalConfig().hasOptionString("protocol", uri.getScheme())) {
169 handler = IdentityManager.getGlobalConfig().getOption("protocol", uri.getScheme());
170 } else {
171 handler = "";
172 }
173 uris.add(uri);
174 handlers.add(handler);
175 fireTableRowsInserted(uris.size() - 1, uris.size() - 1);
176 }
177
178 /**
179 * Removes a URI to the model.
180 *
181 * @param uri URI to remove
182 */
183 public void removeURI(final URI uri) {
/*
P/P * Method: void removeURI(URI)
*
* Preconditions:
* this.uris != null
* (soft) this.handlers != null
*/
184 removeURI(uris.indexOf(uri));
185 }
186
187 /**
188 * Removes a URI to the model.
189 *
190 * @param index Index of the URI to remove
191 */
192 public void removeURI(final int index) {
/*
P/P * Method: void removeURI(int)
*
* Preconditions:
* (soft) this.handlers != null
* (soft) this.uris != null
*
* Test Vectors:
* index: {-1}, {-231..-2, 0..232-1}
*/
193 if (index != -1) {
194 uris.remove(index);
195 handlers.remove(index);
196 fireTableRowsDeleted(index, index);
197 }
198 }
199
200 /**
201 * Returns a map of the URL handlers in this model.
202 *
203 * @return URL Handler map
204 */
205 public Map<URI, String> getURLHandlers() {
/*
P/P * Method: Map getURLHandlers()
*
* Preconditions:
* this.uris != null
* (soft) this.handlers != null
*
* Postconditions:
* return_value == &new HashMap(getURLHandlers#1)
* new HashMap(getURLHandlers#1) num objects == 1
*/
206 final Map<URI, String> urlHandlers = new HashMap<URI, String>();
207
208 for (int i = 0; i < uris.size(); i++) {
209 urlHandlers.put(uris.get(i), handlers.get(i));
210 }
211
212 return urlHandlers;
213 }
214 }
SofCheck Inspector Build Version : 2.17854
| URLHandlerTableModel.java |
2009-Jun-25 01:54:24 |
| URLHandlerTableModel.class |
2009-Sep-02 17:04:16 |