source: trunk/gli/src/org/greenstone/gatherer/cdm/DynamicListModel.java@ 4366

Last change on this file since 4366 was 4366, checked in by kjdon, 21 years ago

re-tabbed the code for java

  • Property svn:keywords set to Author Date Id Revision
File size: 3.7 KB
Line 
1/**
2 *#########################################################################
3 *
4 * A component of the Gatherer application, part of the Greenstone digital
5 * library suite from the New Zealand Digital Library Project at the
6 * University of Waikato, New Zealand.
7 *
8 * <BR><BR>
9 *
10 * Author: John Thompson, Greenstone Digital Library, University of Waikato
11 *
12 * <BR><BR>
13 *
14 * Copyright (C) 1999 New Zealand Digital Library Project
15 *
16 * <BR><BR>
17 *
18 * This program is free software; you can redistribute it and/or modify
19 * it under the terms of the GNU General Public License as published by
20 * the Free Software Foundation; either version 2 of the License, or
21 * (at your option) any later version.
22 *
23 * <BR><BR>
24 *
25 * This program is distributed in the hope that it will be useful,
26 * but WITHOUT ANY WARRANTY; without even the implied warranty of
27 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28 * GNU General Public License for more details.
29 *
30 * <BR><BR>
31 *
32 * You should have received a copy of the GNU General Public License
33 * along with this program; if not, write to the Free Software
34 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
35 *########################################################################
36 */
37
38
39
40
41
42
43/* GPL_HEADER */
44package org.greenstone.gatherer.cdm;
45/**************************************************************************************
46 * Title: Gatherer
47 * Description: The Gatherer: a tool for gathering and enriching a digital collection.
48 * Company: The University of Waikato
49 * Written: 20/05/02
50 * Revised: 03/10/02 - Commented
51 **************************************************************************************/
52import javax.swing.ComboBoxModel;
53import javax.swing.DefaultListModel;
54/** This class extends the functionality of the DefaultListModel to include simple methods for refreshing the contents of this model if one of its elements changes.
55 * @author John Thompson, Greenstone Digital Library, University of Waikato
56 * @version 2.3
57 */
58public class DynamicListModel
59 extends DefaultListModel
60 implements ComboBoxModel {
61
62 private boolean auto_order = false;
63
64 private Object object = null;
65
66 public void addElement(Object element) {
67 if(auto_order) {
68 // Insert the object in its alphabetically correct place.
69 int position = 0;
70 boolean found = false;
71 while(!found && position < size()) {
72 ///ystem.err.print("Compare " + element.toString() + " to " + get(position).toString() + ": ");
73 int order = element.toString().compareTo(get(position).toString());
74 if(order < 0) {
75 add(position, element);
76 found = true;
77 ///ystem.err.println("Greater than. Insert");
78 }
79 else if(order == 0) {
80 found = true;
81 ///ystem.err.println("Equal. End.");
82 }
83 else {
84 position++;
85 ///ystem.err.println("Less than. Carry on.");
86 }
87 }
88 if(!found) {
89 super.addElement(element);
90 ///ystem.err.println("Out of elements. Insert");
91 }
92 }
93 else {
94 super.addElement(element);
95 }
96 }
97
98 public DynamicListModel shallowCopy() {
99 DynamicListModel copy = new DynamicListModel();
100 copy.setAutoOrder(auto_order);
101 for(int i = 0; i < size(); i++) {
102 copy.addElement(get(i));
103 }
104 return copy;
105 }
106
107 public Object getSelectedItem() {
108 ///ystem.err.println("Get item: " + object);
109 return object;
110 }
111 /** Notify all controls that are based on this list model that its contents have changed, and they should repaint themselves. */
112 public void refresh() {
113 fireContentsChanged(this, 0, size());
114 }
115
116 public void setAutoOrder(boolean auto_order) {
117 this.auto_order = auto_order;
118 }
119
120 public void setSelectedItem(Object object) {
121 ///ystem.err.println("Set item: " + object);
122 this.object = object;
123 }
124}
Note: See TracBrowser for help on using the repository browser.