| 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.11 2001/10/22 16:50:55 cursu 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
    90      this.add(box);
    91      this.add(Box.createVerticalGlue());
    92
    93    }// protected void initGuiComponents()
    94
    95    protected void initListeners(){
    96      addNewBtn.addActionListener(new ActionListener() {
    97        public void actionPerformed(ActionEvent e) {
    98          String name = newFeatureField.getText();
    99          String value = newValueField.getText();
    100         if(name != null){
    101           features.put(name, value);
    102           tableModel.fireTableDataChanged();
    103           newFeatureField.setText("");
    104           newValueField.setText("");
    105         }
    106       }
    107     });
    108   }
    109
    110   public void setFeatureBearer(FeatureBearer newResource) {
    111     if(newResource == null){
    112       resource = null;
    113       features = null;
    114     }else{
    115       resource = newResource;
    116       features = resource.getFeatures();
    117     }
    118     tableModel.fireTableDataChanged();
    119   }// public void setFeatureBearer(FeatureBearer newResource)
    120
    121   public void setTarget(Object target) {
    122     if(target == null || target instanceof FeatureBearer){
    123       setFeatureBearer((FeatureBearer)target);
    124     }else{
    125       throw new IllegalArgumentException(
    126         "FeatureEditors can only be used with FeatureBearer!\n" +
    127         target.getClass().toString() + " is not a FeatureBearer!");
    128     }
    129   }//public void setResource(Resource resource)
    130
    131   public void setHandle(Handle handle){
    132     //NOP
    133   }
    134
    135
    136   public FeatureBearer getFeatureBearer() {
    137     return resource;
    138   }
    139
    140   XJTable table;
    141   FeaturesTableModel tableModel;
    142   private FeatureBearer resource;
    143   FeatureMap features;
    144   JTextField newFeatureField;
    145   JTextField newValueField;
    146
    147   JButton addNewBtn;
    148
    149   class FeaturesTableModel extends AbstractTableModel{
    150     public int getColumnCount(){
    151       return 2;
    152     }
    153
    154     public int getRowCount(){
    155       return features == null ? 0 : features.size();
    156     }
    157
    158     public String getColumnName(int columnIndex){
    159       switch(columnIndex){
    160         case 0: return "Feature";
    161         case 1: return "Value";
    162         default: return "?";
    163       }
    164     }//public String getColumnName(int columnIndex)
    165
    166     public Class getColumnClass(int columnIndex){
    167       switch(columnIndex){
    168         case 0: return String.class;
    169         case 1: return Object.class;
    170         default: return Object.class;
    171       }
    172     }
    173
    174     public boolean isCellEditable(int rowIndex,
    175                               int columnIndex){
    176       if(features == null) return false;
    177       return rowIndex == features.size()
    178              ||
    179              ((!((String)table.getModel().getValueAt(rowIndex, 0)).
    180               startsWith("gate."))
    181              );
    182     }// public boolean isCellEditable
    183
    184     public Object getValueAt(int rowIndex,
    185                          int columnIndex){
    186       if(features == null) return null;
    187       List keys = new ArrayList(features.keySet());
    188       Collections.sort(keys);
    189       Object key = keys.get(rowIndex);
    190       switch(columnIndex){
    191         case 0:{
    192           return key;
    193         }
    194         case 1:{
    195           return features.get(key) == null ? "" : features.get(key).toString();
    196         }
    197         default:{
    198           return null;
    199         }
    200       }
    201     }// public Object getValueAt
    202
    203     public void setValueAt(Object aValue,
    204                        int rowIndex,
    205                        int columnIndex){
    206
    207       if(columnIndex == 0) {
    208         //the name of the feature changed
    209         //if the name is null or empty the feature will be deleted
    210         String oldName = (String)getValueAt(rowIndex, 0);
    211         Object oldValue = features.remove(oldName);
    212         if(aValue != null && !aValue.equals("")){
    213           features.put(aValue, oldValue);
    214         }
    215       } else {
    216         //the value of a feature changed
    217         features.put(getValueAt(rowIndex, 0), aValue);
    218       }
    219       fireTableDataChanged();
    220     }// public void setValueAt
    221
    222   }///class FeaturesTableModel extends DefaultTableModel
    223 /*
    224   class FeaturesTableRenderer extends DefaultTableCellRenderer{
    225     public Component getTableCellRendererComponent(JTable table,
    226                                                    Object value,
    227                                                    boolean isSelected,
    228                                                    boolean hasFocus,
    229                                                    int row,
    230                                                    int column){
    231
    232       super.getTableCellRendererComponent(table, value, false, hasFocus,
    233                                           row, column);
    234       setEnabled(table.isCellEditable(row, column));
    235       return this;
    236     }
    237
    238   }// class FeaturesTableRenderer
    239 */
    240 }// class FeaturesEditor

| FeaturesEditor |