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.

ProfileListModel.java 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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.profiles;
  23. import java.util.ArrayList;
  24. import java.util.Iterator;
  25. import java.util.List;
  26. import javax.swing.DefaultListModel;
  27. /** Profile list model. */
  28. public class ProfileListModel extends DefaultListModel implements Iterable<Profile> {
  29. /**
  30. * A version number for this class. It should be changed whenever the class
  31. * structure is changed (or anything else that would prevent serialized
  32. * objects being unserialized with the new class).
  33. */
  34. private static final long serialVersionUID = 1;
  35. /** Profile list. */
  36. private final List<Profile> profiles;
  37. /** Creates a new profile list model. */
  38. public ProfileListModel() {
  39. this(new ArrayList<Profile>());
  40. }
  41. /**
  42. * Creates a new profile list model.
  43. *
  44. * @param profiles Profile list to use
  45. */
  46. public ProfileListModel(final List<Profile> profiles) {
  47. this.profiles = profiles;
  48. }
  49. /** {@inheritDoc} */
  50. @Override
  51. public int getSize() {
  52. return profiles.size();
  53. }
  54. /** {@inheritDoc} */
  55. @Override
  56. public Profile getElementAt(int index) {
  57. return profiles.get(index);
  58. }
  59. /**
  60. * Removes the index from the model.
  61. *
  62. * @param index Index to remove
  63. *
  64. * @return the profile that was removed
  65. */
  66. @Override
  67. public Profile remove(int index) {
  68. final Profile returnValue = profiles.remove(index);
  69. fireIntervalRemoved(this, index, index);
  70. return returnValue;
  71. }
  72. /**
  73. * Removes the object from the model.
  74. *
  75. * @param p object to remove from the model
  76. *
  77. * @return true if the object was removed
  78. */
  79. public boolean remove(Profile p) {
  80. final int index = profiles.indexOf(p);
  81. final boolean returnValue = profiles.remove(p);
  82. fireIntervalRemoved(this, index, index);
  83. return returnValue;
  84. }
  85. /**
  86. * Checks if the model is empty.
  87. *
  88. * @return true if the model is empty
  89. */
  90. @Override
  91. public boolean isEmpty() {
  92. return profiles.isEmpty();
  93. }
  94. /**
  95. * Returns the index of the object.
  96. *
  97. * @param p object to find the index of
  98. *
  99. * @return index of the specified object
  100. */
  101. public int indexOf(Profile p) {
  102. return profiles.indexOf(p);
  103. }
  104. /**
  105. * Returns the profile at the index.
  106. *
  107. * @param index index to retrieve
  108. *
  109. * @return the profile that was removed
  110. */
  111. @Override
  112. public Profile get(int index) {
  113. return profiles.get(index);
  114. }
  115. /**
  116. * Returns a list of all profiles
  117. *
  118. * @return Profile list
  119. */
  120. public List<Profile> getProfiles() {
  121. return new ArrayList<Profile>(profiles);
  122. }
  123. /**
  124. *
  125. * Checks if the model contains the profile
  126. *
  127. * @param p profile to check for
  128. *
  129. * @return true if the model contains the profile
  130. */
  131. public boolean contains(Profile p) {
  132. return profiles.contains(p);
  133. }
  134. /**
  135. *
  136. * Checks if the model contains a profile with the specified name.
  137. *
  138. * @param name name to match against
  139. *
  140. * @return true if the model contains a profile with the specified name
  141. */
  142. public boolean contains(String name) {
  143. synchronized (profiles) {
  144. for (Profile profile : profiles) {
  145. if (profile.getName().equals(name)) {
  146. return true;
  147. }
  148. }
  149. return false;
  150. }
  151. }
  152. /**
  153. * Clears the model.
  154. */
  155. @Override
  156. public void clear() {
  157. final int size = profiles.size();
  158. profiles.clear();
  159. fireIntervalRemoved(this, 0, size);
  160. }
  161. /**
  162. * Adds a profile at the index
  163. *
  164. * @param index index to add the profile
  165. * @param element profile to add
  166. */
  167. public void add(int index, Profile element) {
  168. profiles.add(index, element);
  169. fireIntervalAdded(this, index, index);
  170. }
  171. /**
  172. * Adds the profile to the model
  173. *
  174. * @param p profile to add
  175. *
  176. * @return true if the item was added
  177. */
  178. public boolean add(Profile p) {
  179. final boolean returnValue = profiles.add(p);
  180. final int index = profiles.indexOf(p);
  181. fireIntervalAdded(this, index, index);
  182. return returnValue;
  183. }
  184. /**
  185. * Returns an iterator for this model.
  186. *
  187. * @return Iterator for the model
  188. */
  189. @Override
  190. public Iterator<Profile> iterator() {
  191. return profiles.iterator();
  192. }
  193. /** {@inheritDoc} */
  194. @Override
  195. public boolean equals(Object obj) {
  196. return profiles.equals(obj);
  197. }
  198. /** {@inheritDoc} */
  199. @Override
  200. public int hashCode() {
  201. return profiles.hashCode();
  202. }
  203. /** {@inheritDoc} */
  204. @Override
  205. public String toString() {
  206. return profiles.toString();
  207. }
  208. }