You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

URLHandlerTableModel.java 6.4KB

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