source: trunk/gli/src/org/greenstone/gatherer/gems/LangDependElementTable.java@ 12564

Last change on this file since 12564 was 12564, checked in by shaoqun, 18 years ago

add new gems

  • Property svn:keywords set to Author Date Id Revision
File size: 12.2 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: Shaoqun Wu, Greenstone Digital Library, University of Waikato
11 *
12 * <BR><BR>
13 *
14 * Copyright (C) 2006 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 */
37package org.greenstone.gatherer.gems;
38
39import org.w3c.dom.Element;
40
41import javax.swing.table.*;
42import javax.swing.JTable;
43
44import java.awt.event.ActionListener;
45import java.awt.event.ActionEvent;
46import java.util.ArrayList;
47import java.util.HashMap;
48import java.util.Iterator;
49import java.awt.Rectangle;
50
51import javax.swing.JOptionPane;
52
53import org.greenstone.gatherer.Dictionary;
54
55/**
56 * @author Shaoqun Wu, Greenstone Digital Library, University of Waikato
57 * @version 2.4
58 */
59public class LangDependElementTable
60 extends JTable implements MetadataElementListener, MetadataSetListener, ActionListener {
61
62 int DISPLAY_INFO = 0;
63
64 int DISPLAY_ELEMENT = 1;
65
66 int state = -1;
67
68 JTable table;
69
70 private ArrayList listeners = new ArrayList();
71
72 public LangDependElementTable(){
73 super();
74 setRowHeight(20);
75 setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
76 table = this;
77 }
78
79
80 public void actionPerformed(ActionEvent e){
81 String command = e.getActionCommand();
82
83 if (command.equals(GEMSConstants.DELETE_ATTRIBUTE)){
84 deleteAttribute();
85 }
86
87 }
88
89 public void languageSelected(String lang){
90 if (state == DISPLAY_ELEMENT){
91 MetadataElementTableModel model = (MetadataElementTableModel)getModel();
92 model.addNewLanguage(lang);
93 }
94 else{
95 MetadataSetInfoTableModel model = (MetadataSetInfoTableModel)getModel();
96 model.addNewLanguage(lang);
97 }
98 }
99
100
101
102 private void deleteAttribute(){
103 int index = getSelectedRow();
104
105 if (state == DISPLAY_ELEMENT){
106 MetadataElementTableModel model = (MetadataElementTableModel)getModel();
107 model.deleteRow(index);
108 }
109 else{
110
111 MetadataSetInfoTableModel model = (MetadataSetInfoTableModel)getModel();
112 model.deleteRow(index);
113 }
114 }
115
116 public void metadataElementChanged(MetadataElementEvent mee){
117 state = DISPLAY_ELEMENT;
118 MetadataElementModel model = mee.getMetadataElementModel();
119 if (model == null){ //this model has been deleted
120 setModel(new DefaultTableModel());
121 return;
122 }
123 populateMetadataElementTable(model);
124 }
125
126 public void metadataSetChanged(MetadataSetEvent mse){
127 state = DISPLAY_INFO;
128 MetadataSetInfo info = mse.getMetadataSetInfo();
129 if (info == null){
130 setModel(new DefaultTableModel());
131 return;
132 }
133 populateMetadataElementTable(info);
134 }
135
136
137
138 private void populateMetadataElementTable(MetadataElementModel model){
139 MetadataElementTableModel table_model = new MetadataElementTableModel(model);
140 setModel(table_model);
141 getColumnModel().getColumn(0).setPreferredWidth(100);
142 getColumnModel().getColumn(1).setPreferredWidth(50);
143 getColumnModel().getColumn(2).setPreferredWidth(390);
144 }
145
146
147
148 private void populateMetadataElementTable(MetadataSetInfo info){
149 MetadataSetInfoTableModel model = new MetadataSetInfoTableModel(info);
150 setModel(model);
151 getColumnModel().getColumn(0).setPreferredWidth(100);
152 getColumnModel().getColumn(1).setPreferredWidth(50);
153 getColumnModel().getColumn(2).setPreferredWidth(390);
154 }
155
156
157 public void addAttributeListener(AttributeListener al){
158 if (!listeners.contains(al)){
159 listeners.add(al);
160 }
161 }
162
163 public void removeAttributeListener(AttributeListener al){
164 listeners.remove(al);
165 }
166
167 private void notifyListeners(Attribute attr){
168 AttributeEvent ae = new AttributeEvent(attr);
169 for(int i=0;i<listeners.size();i++){
170 AttributeListener al = (AttributeListener) listeners.get(i);
171 al.attributeChanged(ae);
172
173 }
174 }
175
176
177 class MetadataElementTableModel extends AbstractTableModel{
178 MetadataElementModel element_model;
179 ArrayList attributes;
180
181 String[] columnNames= new String[]{"name","lang","value"};
182
183
184 public MetadataElementTableModel(MetadataElementModel model){
185 element_model = model;
186 attributes = element_model.getLangDependAttributes();
187 }
188
189 public int getRowCount(){
190 return attributes.size();
191 }
192
193 public String getColumnName(int column){
194 return columnNames[column];
195 }
196
197 public Class getColumnClass(int columnIndex){
198 return String.class;
199 }
200
201 public boolean isCellEditable(int row, int col) {
202 return true;
203 }
204
205 public int getColumnCount(){
206 return columnNames.length;
207 }
208
209 public Object getValueAt(int row, int column){
210 if (row < attributes.size()){
211 Attribute attr = (Attribute)attributes.get(row);
212
213 if (column == 0) return attr.getName();
214 if (column == 1) return attr.getLanguage();
215 if (column == 2) return attr.getValue();
216 }
217
218
219 return null;
220 }
221
222 public void setValueAt(Object value, int row, int column){
223 if (row < attributes.size()){
224 Attribute attr = (Attribute)attributes.get(row);
225 if (attr.isRequired() && ((String)value).trim().equals("") && !getValueAt(row,column).equals("")) {
226 JOptionPane.showMessageDialog(null,Dictionary.get("GEMS.Attribute_Edition_Error_Message"), Dictionary.get("GEMS.Attribute_Edition_Error"),JOptionPane.ERROR_MESSAGE);
227 return;
228 }
229
230 if (column == 0) attr.setName((String)value);
231 if (column == 1) attr.setLanguage((String)value);
232 if (column == 2) attr.setValue((String)value);
233 notifyListeners(attr);
234 }
235 }
236
237 public void addNewLanguage(String lang){
238 int index =element_model.languageExist(lang);
239 if (index > 0){
240 table.getSelectionModel().setSelectionInterval(index,index);
241 Rectangle rect = table.getCellRect(index, 0, true);
242 table.scrollRectToVisible(rect);
243 }
244 else{
245 String[] attrNames = element_model.getLangDependAttrNames();
246
247 if (attrNames == null){
248 JOptionPane.showMessageDialog(null,Dictionary.get("GEMS.Add_Lang_Depend_Attr_Error_Message"),Dictionary.get("GEMS.Add_Lang_Depend_Attr_Error"),JOptionPane.ERROR_MESSAGE);
249 return ;
250 }
251 for(int i=0;i<attrNames.length;i++){
252 Attribute attr = new Attribute(attrNames[i].trim(),"");
253 attr.setLanguage(lang);
254 attributes.add(attr);
255 notifyListeners(attr);
256 }
257 fireTableDataChanged();
258 table.getSelectionModel().setSelectionInterval(attributes.size()-attrNames.length,attributes.size()-1);
259 Rectangle rect = table.getCellRect(attributes.size()-1, 0, true);
260 table.scrollRectToVisible(rect);
261 }
262 }
263
264
265 public void deleteRow(int index){
266 if (index < attributes.size() && index >=0){
267 Attribute attr = (Attribute)attributes.get(index);
268
269 if (attr.isRequired()){
270 JOptionPane.showMessageDialog(null,Dictionary.get("GEMS.Attribute_Deletion_Error_Message"), Dictionary.get("GEMS.Attribute_Deletion_Error"),JOptionPane.ERROR_MESSAGE);
271 return;
272 }
273
274 int result = JOptionPane.showOptionDialog(null, Dictionary.get("GEMS.Confirm_Removal", Dictionary.get("GEMS.Attribute") + " " + Dictionary.get("GEMS.Cannot_Undo")), Dictionary.get("GEMS.Confirm_Removal_Title"), JOptionPane.DEFAULT_OPTION, JOptionPane.QUESTION_MESSAGE,null,GEMSConstants.DIALOG_OPTIONS,GEMSConstants.DIALOG_OPTIONS[0]);
275
276 if (result !=JOptionPane.OK_OPTION) return;
277 notifyListeners(attr);
278 attributes.remove(index);
279 fireTableDataChanged();
280 }
281
282 }
283
284
285 }
286
287
288 class MetadataSetInfoTableModel extends AbstractTableModel{
289 MetadataSetInfo metadata_info;
290 ArrayList attributes;
291
292 String[] columnNames= new String[]{"name","lang","value"};
293
294
295 public MetadataSetInfoTableModel(MetadataSetInfo info){
296 metadata_info = info;
297 attributes = info.getLangDependAttributes();
298 }
299
300 public int getRowCount(){
301 return attributes.size();
302 }
303
304 public String getColumnName(int column){
305 return columnNames[column];
306 }
307
308 public Class getColumnClass(int columnIndex){
309 return String.class;
310 }
311
312 public boolean isCellEditable(int row, int col) {
313 return (col !=0 && col != 1);
314 }
315
316 public int getColumnCount(){
317 return columnNames.length;
318 }
319
320 public Object getValueAt(int row, int column){
321 if (row < attributes.size()){
322 Attribute attr = (Attribute)attributes.get(row);
323 String name = attr.getName();
324 if (attr.isRequired()){
325 name = "<html><body Color=blue>"+name+"</body></html>";
326 }
327
328 if (column ==0 )return name;
329 if (column == 1) return attr.getLanguage();
330 if (column == 2) return attr.getValue();
331 }
332
333 return null;
334 }
335
336 public void setValueAt(Object value, int row, int column){
337 if (row < attributes.size()){
338 Attribute attr = (Attribute)attributes.get(row);
339 if (attr.isRequired() && ((String)value).trim().equals("")&& !getValueAt(row,column).equals("")) {
340 JOptionPane.showMessageDialog(null,Dictionary.get("GEMS.Attribute_Edition_Error_Message"), Dictionary.get("GEMS.Attribute_Edition_Error"),JOptionPane.ERROR_MESSAGE);
341 return;
342 }
343
344 if (column == 0) attr.setName((String)value);
345 if (column == 1) attr.setLanguage((String)value);
346 if (column == 2) attr.setValue((String)value);
347 notifyListeners(attr);
348
349 if (attr.getName().trim().equals(GEMSConstants.NAME_ATTRIBUTE)
350 && attr.getLanguage().trim().equals(metadata_info.getCurrentLanguage())
351 )
352 {
353 metadata_info.infoChanged();
354 }
355 }
356
357 }
358
359
360 public void addNewLanguage(String lang){
361 int index = metadata_info.languageExist(lang);
362 if (index > 0){
363 table.getSelectionModel().setSelectionInterval(index,index);
364 Rectangle rect = table.getCellRect(index, 0, true);
365 table.scrollRectToVisible(rect);
366 }
367 else{
368 String[] attrNames = GEMSConstants.LANG_DEPEND_ATTR_NAMES;
369
370 for(int i=0;i<attrNames.length;i++){
371 Attribute attr = new Attribute(attrNames[i],"");
372 attr.setLanguage(lang);
373 attributes.add(attr);
374 notifyListeners(attr);
375 }
376 fireTableDataChanged();
377 table.getSelectionModel().setSelectionInterval(attributes.size()-attrNames.length,attributes.size()-1);
378 Rectangle rect = table.getCellRect(attributes.size()-1, 0, true);
379 table.scrollRectToVisible(rect);
380 }
381 }
382
383 public void deleteRow(int index){
384
385 if (index < attributes.size() && index >=0){
386 Attribute attr = (Attribute)attributes.get(index);
387
388 if (attr.isRequired()){
389 JOptionPane.showMessageDialog(null,Dictionary.get("GEMS.Attribute_Deletion_Error_Message"), Dictionary.get("GEMS.Attribute_Deletion_Error"),JOptionPane.ERROR_MESSAGE);
390 return;
391 }
392
393 int result = JOptionPane.showOptionDialog(null, Dictionary.get("GEMS.Confirm_Removal", Dictionary.get("GEMS.Attribute") + " " + Dictionary.get("GEMS.Cannot_Undo")), Dictionary.get("GEMS.Confirm_Removal_Title"), JOptionPane.DEFAULT_OPTION, JOptionPane.QUESTION_MESSAGE,null, GEMSConstants.DIALOG_OPTIONS,GEMSConstants.DIALOG_OPTIONS[0]);
394
395 if (result !=JOptionPane.OK_OPTION) return;
396
397 notifyListeners(attr);
398 attributes.remove(index);
399 fireTableDataChanged();
400 return;
401 }
402
403 }
404
405 }
406
407}
408
409
410
411
Note: See TracBrowser for help on using the repository browser.