|
FeaturesEditor |
|
1 /* 2 * Copyright (c) 1998-2001, The University of Sheffield. 3 * 4 * This file is part of GATE (see http://gate.ac.uk/), and is free 5 * software, licenced under the GNU Library General Public License, 6 * Version 2, June 1991 (in the distribution as file licence.html, 7 * and also available at http://gate.ac.uk/gate/licence.html). 8 * 9 * Valentin Tablan 23/01/2001 10 * 11 * $Id: FeaturesEditor.java,v 1.14 2003/01/14 08:56:56 nasso Exp $ 12 * 13 */ 14 15 package gate.gui; 16 17 import gate.*; 18 import gate.util.*; 19 import gate.swing.*; 20 import gate.creole.*; 21 22 import javax.swing.*; 23 import java.awt.BorderLayout; 24 import java.awt.Component; 25 import java.awt.Color; 26 import java.awt.Dimension; 27 import javax.swing.table.*; 28 import javax.swing.event.*; 29 import java.awt.event.*; 30 31 import java.util.*; 32 33 import gate.creole.AbstractVisualResource; 34 35 public class FeaturesEditor extends AbstractVisualResource{ 36 37 public FeaturesEditor() { 38 initLocalData(); 39 initGuiComponents(); 40 initListeners(); 41 }// FeaturesEditor() 42 43 protected void initLocalData(){ 44 features = Factory.newFeatureMap(); 45 } 46 47 protected void initGuiComponents(){ 48 this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS)); 49 tableModel = new FeaturesTableModel(); 50 table = new XJTable(tableModel); 51 // table.setIntercellSpacing(new Dimension(5,5)); 52 table.setDefaultRenderer(String.class, new ObjectRenderer()); 53 table.setDefaultRenderer(Object.class, new ObjectRenderer()); 54 55 DefaultCellEditor editor = new DefaultCellEditor(new JTextField()); 56 editor.setClickCountToStart(0); 57 table.setDefaultEditor(String.class, editor); 58 table.setDefaultEditor(Object.class, editor); 59 60 JScrollPane scroll = new JScrollPane(table); 61 this.add(scroll, BorderLayout.CENTER); 62 this.add(Box.createVerticalStrut(5)); 63 64 Box box = Box.createHorizontalBox(); 65 newFeatureField = new JTextField(10); 66 newFeatureField.setMaximumSize(new Dimension(Integer.MAX_VALUE, 67 newFeatureField. 68 getPreferredSize().height)); 69 70 Box vBox = Box.createVerticalBox(); 71 vBox.add(new JLabel("New feature name")); 72 vBox.add(newFeatureField); 73 box.add(vBox); 74 box.add(Box.createHorizontalStrut(5)); 75 76 newValueField = new JTextField(10); 77 newValueField.setMaximumSize(new Dimension(Integer.MAX_VALUE, 78 newValueField. 79 getPreferredSize().height)); 80 81 vBox = Box.createVerticalBox(); 82 vBox.add(new JLabel("New feature value")); 83 vBox.add(newValueField); 84 box.add(vBox); 85 box.add(Box.createHorizontalStrut(5)); 86 87 addNewBtn = new JButton("Add feature"); 88 box.add(addNewBtn); 89 box.add(Box.createHorizontalStrut(5)); 90 91 delBtn = new JButton("Delete"); 92 box.add(delBtn); 93 94 this.add(box); 95 this.add(Box.createVerticalGlue()); 96 97 }// protected void initGuiComponents() 98 99 protected void initListeners(){ 100 addNewBtn.addActionListener(new ActionListener() { 101 public void actionPerformed(ActionEvent e) { 102 String name = newFeatureField.getText(); 103 String value = newValueField.getText(); 104 if(name != null){ 105 features.put(name, value); 106 tableModel.fireTableDataChanged(); 107 newFeatureField.setText(""); 108 newValueField.setText(""); 109 } 110 } 111 }); 112 113 delBtn.addActionListener(new ActionListener() { 114 public void actionPerformed(ActionEvent e) { 115 String name = newFeatureField.getText(); 116 String value = newValueField.getText(); 117 if(name != null){ 118 features.remove(name); 119 tableModel.fireTableDataChanged(); 120 newFeatureField.setText(""); 121 newValueField.setText(""); 122 } 123 } 124 }); 125 } 126 127 public void cleanup(){ 128 super.cleanup(); 129 features = null; 130 resource = null; 131 } 132 133 public void setFeatureBearer(FeatureBearer newResource) { 134 if(newResource == null){ 135 resource = null; 136 features = null; 137 }else{ 138 resource = newResource; 139 features = resource.getFeatures(); 140 } 141 tableModel.fireTableDataChanged(); 142 }// public void setFeatureBearer(FeatureBearer newResource) 143 144 public void setTarget(Object target) { 145 if(target == null || target instanceof FeatureBearer){ 146 setFeatureBearer((FeatureBearer)target); 147 }else{ 148 throw new IllegalArgumentException( 149 "FeatureEditors can only be used with FeatureBearer!\n" + 150 target.getClass().toString() + " is not a FeatureBearer!"); 151 } 152 }//public void setResource(Resource resource) 153 154 public void setHandle(Handle handle){ 155 //NOP 156 } 157 158 159 public FeatureBearer getFeatureBearer() { 160 return resource; 161 } 162 163 XJTable table; 164 FeaturesTableModel tableModel; 165 private FeatureBearer resource; 166 FeatureMap features; 167 JTextField newFeatureField; 168 JTextField newValueField; 169 170 JButton addNewBtn; 171 JButton delBtn; 172 173 class FeaturesTableModel extends AbstractTableModel{ 174 public int getColumnCount(){ 175 return 2; 176 } 177 178 public int getRowCount(){ 179 return features == null ? 0 : features.size(); 180 } 181 182 public String getColumnName(int columnIndex){ 183 switch(columnIndex){ 184 case 0: return "Feature"; 185 case 1: return "Value"; 186 default: return "?"; 187 } 188 }//public String getColumnName(int columnIndex) 189 190 public Class getColumnClass(int columnIndex){ 191 switch(columnIndex){ 192 case 0: return String.class; 193 case 1: return Object.class; 194 default: return Object.class; 195 } 196 } 197 198 public boolean isCellEditable(int rowIndex, 199 int columnIndex){ 200 if(features == null) return false; 201 return rowIndex == features.size() 202 || 203 ((!((String)table.getModel().getValueAt(rowIndex, 0)). 204 startsWith("gate.")) 205 ); 206 }// public boolean isCellEditable 207 208 public Object getValueAt(int rowIndex, 209 int columnIndex){ 210 if(features == null) return null; 211 List keys = new ArrayList(features.keySet()); 212 Collections.sort(keys); 213 Object key = keys.get(rowIndex); 214 switch(columnIndex){ 215 case 0:{ 216 return key; 217 } 218 case 1:{ 219 return features.get(key) == null ? "" : features.get(key).toString(); 220 } 221 default:{ 222 return null; 223 } 224 } 225 }// public Object getValueAt 226 227 public void setValueAt(Object aValue, 228 int rowIndex, 229 int columnIndex){ 230 231 if(columnIndex == 0) { 232 //the name of the feature changed 233 //if the name is null or empty the feature will be deleted 234 String oldName = (String)getValueAt(rowIndex, 0); 235 Object oldValue = features.remove(oldName); 236 if(aValue != null && !aValue.equals("")){ 237 features.put(aValue, oldValue); 238 } 239 } else { 240 //the value of a feature changed 241 features.put(getValueAt(rowIndex, 0), aValue); 242 } 243 fireTableDataChanged(); 244 }// public void setValueAt 245 246 }///class FeaturesTableModel extends DefaultTableModel 247 /* 248 class FeaturesTableRenderer extends DefaultTableCellRenderer{ 249 public Component getTableCellRendererComponent(JTable table, 250 Object value, 251 boolean isSelected, 252 boolean hasFocus, 253 int row, 254 int column){ 255 256 super.getTableCellRendererComponent(table, value, false, hasFocus, 257 row, column); 258 setEnabled(table.isCellEditable(row, column)); 259 return this; 260 } 261 262 }// class FeaturesTableRenderer 263 */ 264 }// class FeaturesEditor
|
FeaturesEditor |
|