source: trunk/gli/src/org/greenstone/gatherer/gems/AttributeTable.java@ 12658

Last change on this file since 12658 was 12658, checked in by kjdon, 18 years ago

LangDependElementTable and MetadataElementTable have been merged into one class, AttributeTable

  • Property svn:keywords set to Author Date Id Revision
File size: 10.1 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 javax.swing.table.*;
40import javax.swing.JTable;
41import java.awt.event.ActionListener;
42import java.awt.event.ActionEvent;
43import java.util.ArrayList;
44import java.awt.Rectangle;
45import javax.swing.JOptionPane;
46import javax.swing.DefaultCellEditor;
47import javax.swing.JTextField;
48import javax.swing.ListSelectionModel;
49
50import org.greenstone.gatherer.Dictionary;
51
52public class AttributeTable
53 extends JTable
54 implements MetadataElementListener, MetadataSetListener, ActionListener {
55
56 // self reference
57 JTable this_table;
58
59 private ArrayList listeners = new ArrayList();
60 private JTextField metadata_value_text_field = new JTextField();
61 private boolean language_dependent;
62
63 public AttributeTable(boolean lang_dependent){
64 super();
65 setRowHeight(20);
66 language_dependent = lang_dependent;
67
68 metadata_value_text_field.setBorder(null);
69 DefaultCellEditor cellEditor = new DefaultCellEditor(metadata_value_text_field);
70 cellEditor.setClickCountToStart(1);
71 setDefaultEditor(String.class,cellEditor);
72 this_table = this;
73 setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
74 }
75
76 /* not currently used */
77 public void actionPerformed(ActionEvent e){
78 String command = e.getActionCommand();
79
80 if (command.equals(GEMSConstants.ADD_ATTRIBUTE)){
81 addAttribute();
82 return ;
83 }
84
85 if (command.equals(GEMSConstants.DELETE_ATTRIBUTE)){
86 deleteAttribute();
87 }
88
89 }
90
91 public void addNewLanguage(String lang) {
92 AttributeTableModel model = (AttributeTableModel)getModel();
93 model.addNewLanguage(lang);
94 }
95
96 public void metadataElementChanged(MetadataElementEvent mee){
97 MetadataElementModel model = mee.getMetadataElementModel();
98 if (model == null){ //this model has been deleted
99 setModel(new DefaultTableModel());
100 return;
101 }
102 populateAttributeTable(model);
103 }
104
105 public void metadataSetChanged(MetadataSetEvent mse){
106 MetadataSetInfo info = mse.getMetadataSetInfo();
107 if (info == null){
108 setModel(new DefaultTableModel());
109 return;
110 }
111 populateAttributeTable(info);
112 }
113
114 public void addAttributeListener(AttributeListener al){
115 if (!listeners.contains(al)){
116 listeners.add(al);
117 }
118 }
119
120 public void removeAttributeListener(AttributeListener al){
121 listeners.remove(al);
122 }
123
124 private void addAttribute(){
125 AttributeTableModel model = (AttributeTableModel)getModel();
126 model.addNewRow();
127 }
128
129
130 private void deleteAttribute(){
131 int index = getSelectedRow();
132 AttributeTableModel model = (AttributeTableModel)getModel();
133 model.deleteRow(index);
134 }
135
136 private void populateAttributeTable(AttributeContainer container){
137 AttributeTableModel model = new AttributeTableModel(container, language_dependent);
138 setModel(model);
139 TableColumnModel tcm = getColumnModel();
140 if (language_dependent) {
141 tcm.getColumn(0).setPreferredWidth(100);
142 tcm.getColumn(1).setPreferredWidth(50);
143 tcm.getColumn(2).setPreferredWidth(350);
144 }
145 else {
146 tcm.getColumn(0).setPreferredWidth(100);
147 tcm.getColumn(1).setPreferredWidth(400);
148 }
149 }
150
151
152
153 private void notifyListeners(Attribute attr){
154 AttributeEvent ae = new AttributeEvent(attr);
155 for(int i=0;i<listeners.size();i++){
156 AttributeListener al = (AttributeListener) listeners.get(i);
157 al.attributeChanged(ae);
158
159 }
160 }
161
162
163
164 protected class AttributeTableModel extends AbstractTableModel {
165
166 AttributeContainer attribute_container;
167 ArrayList attributes;
168 boolean language_dependent;
169
170 String[] column_names;
171
172 public AttributeTableModel(AttributeContainer container, boolean lang_dependent) {
173 attribute_container = container;
174 language_dependent = lang_dependent;
175 if (language_dependent) {
176 attributes = container.getLanguageDependentAttributes();
177 column_names = GEMSConstants.LANG_DEPENDENT_ATTRIBUTES_COLUMN_NAMES;
178 }
179 else {
180 attributes = container.getAttributes();
181 column_names = GEMSConstants.ATTRIBUTES_COLUMN_NAMES;
182 }
183 }
184
185 public int getRowCount(){
186 return attributes.size();
187 }
188
189 public String getColumnName(int column){
190 return column_names[column];
191 }
192
193 public Class getColumnClass(int columnIndex){
194 return String.class;
195 }
196 public int getColumnCount(){
197 return column_names.length;
198 }
199 public boolean isCellEditable(int row, int col) {
200 Attribute attr = (Attribute)attributes.get(row);
201 if (col == 0 && attr.isRequired()) return false;
202 return true;
203 }
204
205 public Object getValueAt(int row, int column){
206 if (row < attributes.size()){
207 Attribute attr = (Attribute)attributes.get(row);
208 // String name = attr.getName();
209 // if (attr.isRequired()){
210 // name = "<html><body Color=blue>"+name+"</body></html>";
211 // }
212
213 if (column == 0) return attr.getName();
214 if (language_dependent) {
215 if (column == 1) return attr.getLanguage();
216 if (column == 2) return attr.getValue();
217 }
218 else {
219 if (column == 1) return attr.getValue();
220 }
221 }
222 return null;
223 }
224
225 public void setValueAt(Object value, int row, int column){
226 if (row < attributes.size() && attributes.size()>0 ){
227 Attribute attr = (Attribute)attributes.get(row);
228 if (attr.isRequired() && ((String)value).trim().equals("") && !getValueAt(row,column).equals("")) {
229 JOptionPane.showMessageDialog(null,Dictionary.get("GEMS.Attribute_Edition_Error_Message"), Dictionary.get("GEMS.Attribute_Edition_Error"),JOptionPane.ERROR_MESSAGE);
230 return;
231 }
232
233 // TODO: check namespace conflict
234 if (column == 0) attr.setName((String)value);
235 else {
236 if (language_dependent) {
237 if (column == 1) attr.setLanguage((String)value);
238 else if (column == 2) attr.setValue((String)value);
239 }
240 else {
241 if (column == 1) attr.setValue((String)value);
242 }
243 }
244 notifyListeners(attr);
245
246 /// check this stuff
247 if (attribute_container instanceof MetadataElementModel) {
248 if (attr.getName().trim().equals(GEMSConstants.NAME_ATTRIBUTE)) {
249 ((MetadataElementModel)attribute_container).setName((String)value);
250 }
251 } else if (attribute_container instanceof MetadataSetInfo) {
252 ((MetadataSetInfo)attribute_container).infoChanged();
253 }
254 }
255 }
256
257 /** used only for language dependent attribute tables */
258 public void addNewLanguage(String lang)
259 {
260 if (!language_dependent) return;
261 int index = attribute_container.languageExists(lang);
262 if (index >=0){
263 this_table.getSelectionModel().setSelectionInterval(index,index);
264 Rectangle rect = this_table.getCellRect(index, 0, true);
265 this_table.scrollRectToVisible(rect);
266 }
267 else {
268 String [] attrNames = attribute_container.getLanguageDependentAttributeNames();
269 if (attrNames == null){
270 JOptionPane.showMessageDialog(null,Dictionary.get("GEMS.Add_Lang_Depend_Attr_Error_Message"),Dictionary.get("GEMS.Add_Lang_Depend_Attr_Error"),JOptionPane.ERROR_MESSAGE);
271 return ;
272 }
273 for(int i=0;i<attrNames.length;i++){
274 Attribute attr = new Attribute(attrNames[i].trim(),"", true);
275 attr.setLanguage(lang);
276 attributes.add(attr);
277 notifyListeners(attr);
278 }
279 fireTableDataChanged();
280 // first att from new lot
281 int first_new_att_index = attributes.size()-attrNames.length;
282 this_table.editCellAt(first_new_att_index, 2);
283 if (this_table.isEditing()) {
284 this_table.getEditorComponent().requestFocus();
285 }
286
287 this_table.getSelectionModel().setSelectionInterval(attributes.size()-attrNames.length,attributes.size()-1);
288 Rectangle rect = this_table.getCellRect(attributes.size()-1, 0, true);
289 this_table.scrollRectToVisible(rect);
290 }
291 }
292
293 /* not currently used */
294 public void addNewRow(){
295 Attribute attr = new Attribute();
296 attributes.add(attr);
297 fireTableDataChanged();
298 this_table.getSelectionModel().setSelectionInterval(attributes.size()-1,attributes.size()-1);
299 Rectangle rect = this_table.getCellRect(attributes.size()-1, 0, true);
300 this_table.scrollRectToVisible(rect);
301 }
302
303 /* not currently used */
304 public void deleteRow(int index){
305 if (index < attributes.size() && index >=0){
306 Attribute attr = (Attribute)attributes.get(index);
307 if (attr.isRequired()){
308 JOptionPane.showMessageDialog(null,Dictionary.get("GEMS.Attribute_Deletion_Error_Message"), Dictionary.get("GEMS.Attribute_Deletion_Error"),JOptionPane.ERROR_MESSAGE);
309 return;
310 }
311
312 // 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] );
313
314 //if (result !=JOptionPane.OK_OPTION) return;
315 notifyListeners(attr);
316 attributes.remove(index);
317 fireTableDataChanged();
318 }
319
320 }
321
322 }
323}
Note: See TracBrowser for help on using the repository browser.