source: trunk/gli/src/org/greenstone/gatherer/gui/messages/MessageListModel.java@ 4293

Last change on this file since 4293 was 4293, checked in by jmt12, 21 years ago

Initial revision

  • Property svn:keywords set to Author Date Id Revision
File size: 5.2 KB
Line 
1package org.greenstone.gatherer.gui.messages;
2/**
3 *#########################################################################
4 *
5 * A component of the Gatherer application, part of the Greenstone digital
6 * library suite from the New Zealand Digital Library Project at the
7 * University of Waikato, New Zealand.
8 *
9 * <BR><BR>
10 *
11 * Author: John Thompson, Greenstone Digital Library, University of Waikato
12 *
13 * <BR><BR>
14 *
15 * Copyright (C) 1999 New Zealand Digital Library Project
16 *
17 * <BR><BR>
18 *
19 * This program is free software; you can redistribute it and/or modify
20 * it under the terms of the GNU General Public License as published by
21 * the Free Software Foundation; either version 2 of the License, or
22 * (at your option) any later version.
23 *
24 * <BR><BR>
25 *
26 * This program is distributed in the hope that it will be useful,
27 * but WITHOUT ANY WARRANTY; without even the implied warranty of
28 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
29 * GNU General Public License for more details.
30 *
31 * <BR><BR>
32 *
33 * You should have received a copy of the GNU General Public License
34 * along with this program; if not, write to the Free Software
35 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
36 *########################################################################
37 */
38import java.util.Hashtable;
39import java.util.Vector;
40/**
41 * @author John Thompson, Greenstone Digital Library, University of Waikato
42 * @version 1.1
43 */
44import javax.swing.AbstractListModel;
45import org.greenstone.gatherer.Gatherer;
46import org.greenstone.gatherer.Message;
47import org.greenstone.gatherer.util.Utility;
48class MessageListModel
49 extends AbstractListModel {
50
51 Hashtable mappings;
52
53 int current_index;
54
55 int current_message;
56
57 int sizes[][] = new int[6][4];
58
59 Vector data;
60
61 // Contructor
62 public MessageListModel() {
63 current_index = 0;
64 current_message = 0;
65 data = new Vector();
66 mappings = new Hashtable();
67 for(int i = 0; i < 6; i++) {
68 for(int j = 0; j < 4; j++) {
69 sizes[i][j] = 0;
70 }
71 }
72 }
73
74 // Add a new message in the correct place
75 public synchronized void add(Message message) {
76 // Add the element to the model
77 data.add(message);
78 // Increment the count for that type of message
79 sizes[message.source][message.level]++;
80 // An announce to the world that I have changed!
81 fireContentsChanged(this, 0, getSize() - 1);
82 }
83
84 // Return the size of the list
85 public int getSize() {
86 int size = 0;
87 // Each each source...
88 for(int i = 0; i < 6; i++) {
89 // and each level...
90 for(int j = 0; j < 4; j++) {
91 // If both this source and level are active, add its size
92 /** @todo reenabled
93 * if(gatherer.config.source[i] && gatherer.config.level[j]) {
94 * size = size + sizes[i][j];
95 * }
96 */
97 }
98 }
99 return size;
100 }
101
102 public int getTotalSize() {
103 int size = 0;
104 // Each each source...
105 for(int i = 0; i < 6; i++) {
106 // and each level...
107 for(int j = 0; j < 4; j++) {
108 size = size + sizes[i][j];
109 }
110 }
111 return size;
112 }
113
114 public String getSizeStr() {
115 return "" + getSize() + "";
116 }
117
118 public String getTotalSizeStr() {
119 return "" + getTotalSize() + "";
120 }
121
122 // Return a message from the list
123 public Object getElementAt( int index ) {
124 Integer index_int = new Integer(index);
125 // Maybe we've already found this message.
126 if(mappings.containsKey(index_int)) {
127 current_message = ((Integer)mappings.get(index_int)).intValue();
128 }
129 // Otherwise we're going to have to search for it.
130 else {
131 while(index != current_index) {
132 // Asking for an index lower than the one we're at.
133 if(index < current_index) {
134 Message temp;
135 /*
136 do {
137 current_message--;
138 temp = (Message)data.get(current_message);
139 } while(!(gatherer.config.source[temp.source] &&
140 gatherer.config.level[temp.level]));
141 // We are now at a new visible message.
142 */
143 current_index--;
144 }
145 else {
146 Message temp;
147 /*
148 do {
149 current_message++;
150 temp = (Message)data.get(current_message);
151 } while(!(gatherer.config.source[temp.source] &&
152 gatherer.config.level[temp.level]));
153 // We are now at a new visible message.
154 current_index++;
155 */
156 }
157 }
158 // Now that we've found the blighter, add a mapping so its quicker
159 // next time.
160 mappings.put(new Integer(index),new Integer(current_message));
161 }
162 return ((Message)data.get(current_message)).toString();
163 }
164
165 public void refresh() {
166 if(getSize() > 0) {
167 current_index = 0;
168 // Find the first message that meets the requirements. Its index 0 y'know.
169 current_message = -1;
170 Message temp;
171 /*
172 do {
173 current_message++;
174 temp = (Message)data.get(current_message);
175 } while(!(gatherer.config.source[temp.source] &&
176 gatherer.config.level[temp.level]));
177 */
178 // Reset mappings.
179 mappings.clear();
180 mappings.put(new Integer(current_index), new Integer(current_message));
181 // Let everyone who cares know we've changed.
182 fireContentsChanged(this, 0, getSize() - 1);
183 }
184 // Else don't bother doning anything on an empty list!
185 }
186}
187
188
189
190
Note: See TracBrowser for help on using the repository browser.