View Javadoc

1   /*
2   * The contents of this file are subject to the BT "ZEUS" Open Source 
3   * Licence (L77741), Version 1.0 (the "Licence"); you may not use this file 
4   * except in compliance with the Licence. You may obtain a copy of the Licence
5   * from $ZEUS_INSTALL/licence.html or alternatively from
6   * http://www.labs.bt.com/projects/agents/zeus/licence.htm
7   * 
8   * Except as stated in Clause 7 of the Licence, software distributed under the
9   * Licence is distributed WITHOUT WARRANTY OF ANY KIND, either express or 
10  * implied. See the Licence for the specific language governing rights and 
11  * limitations under the Licence.
12  * 
13  * The Original Code is within the package zeus.*.
14  * The Initial Developer of the Original Code is British Telecommunications
15  * public limited company, whose registered office is at 81 Newgate Street, 
16  * London, EC1A 7AJ, England. Portions created by British Telecommunications 
17  * public limited company are Copyright 1996-9. All Rights Reserved.
18  * 
19  * THIS NOTICE MUST BE INCLUDED ON ANY COPY OF THIS FILE
20  */
21  
22  
23  
24  /******************************************************************************
25  * ExpressionCellEditor.java
26  *
27  * The large text field for editing long expressions
28  *****************************************************************************/
29  
30  package zeus.gui.editors;
31  
32  import java.awt.*;
33  import java.awt.event.*;
34  import javax.swing.*;
35  import javax.swing.table.*;
36  import javax.swing.text.*;
37  
38  import zeus.gui.fields.*;
39  import zeus.util.Core;
40  
41  
42  public class ExpressionCellEditor extends DefaultCellEditor
43                              implements ActionListener {
44    
45      protected JButton button = new JButton("");
46      protected int row, column;
47      protected LargeTextField valuefield = new LargeTextField(10,40);
48      protected boolean first = true;
49      protected TableModel model;
50    
51      public ExpressionCellEditor(TableModel model) {
52        super(new JTextField());
53  
54        this.model = model;
55    
56        setClickCountToStart(2);
57    
58        button.setBackground(Color.white);
59        button.setBorderPainted(false);
60        button.addActionListener(this);
61  
62        valuefield.setLineWrap(true);
63        valuefield.addActionListener(this);
64        valuefield.setBackground(Color.green);
65        valuefield.setOpaque(true);
66        valuefield.setSize(400,80);
67        valuefield.setMinimumSize(new Dimension(400,80));
68      }
69  
70      public void actionPerformed(ActionEvent e) {
71        Object src = e.getSource();
72        if ( src == button ) {
73           button.setEnabled(false);
74           Component root = SwingUtilities.getRoot(button);
75           JLayeredPane pane = null;
76           if ( root instanceof JDialog )
77              pane = ((JDialog)root).getLayeredPane();
78           else if ( root instanceof JFrame )
79              pane = ((JFrame)root).getLayeredPane();
80           else
81              Core.ERROR(null,1,this);
82  
83           if ( first ) {
84              pane.add(valuefield,JLayeredPane.PALETTE_LAYER);
85              first = false;
86           }
87           Point pt = SwingUtilities.convertPoint(button,0,0,root);
88           pane.moveToFront(valuefield);
89           valuefield.setVisible(true);
90           valuefield.setLocation(new Point(pt.x-2,pt.y-22));
91           valuefield.grabFocus();
92        }
93        else if ( src == valuefield ) {
94           fireEditingStopped();
95           valuefield.setVisible(false);
96           String value = valuefield.getText();
97           model.setValueAt(value,row,column);
98        }
99      }
100 
101     public Document getDocument() {
102        return valuefield.getDocument();
103     }
104     public void addMouseListener(MouseListener l) {
105        valuefield.addMouseListener(l);
106     }
107     public void removeMouseListener(MouseListener l) {
108        valuefield.removeMouseListener(l);
109     }
110 
111     public Component getTableCellEditorComponent(JTable table, Object value,
112                                                  boolean isSelected,
113                                                  int row, int column) {  
114       this.row = row;
115       this.column = column;
116       String s = "";
117       if ( value != null ) s = (String)value;
118       valuefield.setText(s);
119       valuefield.selectAll();
120       button.setEnabled(true);
121       button.setText(s);
122       return  button;
123     }
124 }