source: other-projects/FileTransfer-WebSocketPair/testGXTWithGreenstone/src/org/greenstone/gatherer/gems/AttributeTable.java@ 33053

Last change on this file since 33053 was 33053, checked in by ak19, 5 years ago

I still had some stuff of Nathan Kelly's (FileTransfer-WebSocketPair) sitting on my USB. Had already commited the Themes folder at the time, 2 years back. Not sure if he wanted this additional folder commited. But I didn't want to delete it and decided it will be better off on SVN. When we use his project, if we find we didn't need this test folder, we can remove it from svn then.

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