Statistics
| Revision:

gvsig-catalog / trunk / appCatalog / src / org / gvsig / catalog / utils / xmltreetable / XMLTreeTableCellEditor.java @ 6

History | View | Annotate | Download (6.52 KB)

1

    
2
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
3
 *
4
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 2
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
19
 *
20
 * For more information, contact:
21
 *
22
 *  Generalitat Valenciana
23
 *   Conselleria d'Infraestructures i Transport
24
 *   Av. Blasco Ib??ez, 50
25
 *   46010 VALENCIA
26
 *   SPAIN
27
 *
28
 *      +34 963862235
29
 *   gvsig@gva.es
30
 *      www.gvsig.gva.es
31
 *
32
 *    or
33
 *
34
 *   IVER T.I. S.A
35
 *   Salamanca 50
36
 *   46005 Valencia
37
 *   Spain
38
 *
39
 *   +34 963163400
40
 *   dac@iver.es
41
 */
42
package org.gvsig.catalog.utils.xmltreetable;
43
import java.awt.Component;
44
import java.awt.event.MouseEvent;
45
import java.io.Serializable;
46
import java.util.EventObject;
47

    
48
import javax.swing.JComponent;
49
import javax.swing.JTable;
50
import javax.swing.JTree;
51
import javax.swing.SwingUtilities;
52
import javax.swing.event.CellEditorListener;
53
import javax.swing.event.ChangeEvent;
54
import javax.swing.event.EventListenerList;
55
import javax.swing.table.TableCellEditor;
56
import javax.swing.tree.TreeCellEditor;
57

    
58
/**
59
 * DOCUMENT ME!
60
 * 
61
 * 
62
 * @author $author$
63
 */
64
public class XMLTreeTableCellEditor implements Serializable, TableCellEditor, TreeCellEditor {
65
/**
66
 * 
67
 * 
68
 */
69
    protected EventListenerList listenerList = new EventListenerList();
70
/**
71
 * 
72
 * 
73
 */
74
    protected transient ChangeEvent changeEvent = null;
75
/**
76
 * 
77
 * 
78
 */
79
    protected JComponent editorComponent = null;
80
/**
81
 * 
82
 * 
83
 */
84
    protected JComponent container = null;
85
// Can be tree or table
86

    
87
/**
88
 * DOCUMENT ME!
89
 * 
90
 * 
91
 * @return DOCUMENT ME!
92
 */
93
    public Component getComponent() {        
94
        return editorComponent;
95
    } 
96

    
97
/**
98
 * DOCUMENT ME!
99
 * 
100
 * 
101
 * @return DOCUMENT ME!
102
 */
103
    public Object getCellEditorValue() {        
104
        return editorComponent;
105
    } 
106

    
107
/**
108
 * DOCUMENT ME!
109
 * 
110
 * 
111
 * @return DOCUMENT ME!
112
 * @param anEvent DOCUMENT ME!
113
 */
114
    public boolean isCellEditable(EventObject anEvent) {        
115
        return true;
116
    } 
117

    
118
/**
119
 * DOCUMENT ME!
120
 * 
121
 * 
122
 * @return DOCUMENT ME!
123
 * @param anEvent DOCUMENT ME!
124
 */
125
    public boolean shouldSelectCell(EventObject anEvent) {        
126
        if ((editorComponent != null) && anEvent instanceof MouseEvent &&
127
                (((MouseEvent) anEvent).getID() == MouseEvent.MOUSE_PRESSED)) {
128
            Component dispatchComponent = SwingUtilities.getDeepestComponentAt(editorComponent,
129
                    3, 3);
130
            MouseEvent e = (MouseEvent) anEvent;
131
            MouseEvent e2 = new MouseEvent(dispatchComponent,
132
                    MouseEvent.MOUSE_RELEASED, e.getWhen() + 100000,
133
                    e.getModifiers(), 3, 3, e.getClickCount(),
134
                    e.isPopupTrigger());
135
            dispatchComponent.dispatchEvent(e2);
136
            e2 = new MouseEvent(dispatchComponent, MouseEvent.MOUSE_CLICKED,
137
                    e.getWhen() + 100001, e.getModifiers(), 3, 3, 1,
138
                    e.isPopupTrigger());
139
            dispatchComponent.dispatchEvent(e2);
140
        }
141
        return false;
142
    } 
143

    
144
/**
145
 * DOCUMENT ME!
146
 * 
147
 * 
148
 * @return DOCUMENT ME!
149
 */
150
    public boolean stopCellEditing() {        
151
        fireEditingStopped();
152
        return true;
153
    } 
154

    
155
/**
156
 * DOCUMENT ME!
157
 * 
158
 */
159
    public void cancelCellEditing() {        
160
        fireEditingCanceled();
161
    } 
162

    
163
/**
164
 * DOCUMENT ME!
165
 * 
166
 * 
167
 * @param l DOCUMENT ME!
168
 */
169
    public void addCellEditorListener(CellEditorListener l) {        
170
        listenerList.add(CellEditorListener.class, l);
171
    } 
172

    
173
/**
174
 * DOCUMENT ME!
175
 * 
176
 * 
177
 * @param l DOCUMENT ME!
178
 */
179
    public void removeCellEditorListener(CellEditorListener l) {        
180
        listenerList.remove(CellEditorListener.class, l);
181
    } 
182

    
183
/**
184
 * DOCUMENT ME!
185
 * 
186
 */
187
    protected void fireEditingStopped() {        
188
        Object[] listeners = listenerList.getListenerList();
189
        // Process the listeners last to first, notifying
190
        // those that are interested in this event
191
        for (int i = listeners.length - 2; i >= 0; i -= 2) {
192
            if (listeners[i] == CellEditorListener.class) {
193
                // Lazily create the event:
194
                if (changeEvent == null) {
195
                    changeEvent = new ChangeEvent(this);
196
                }
197
                ((CellEditorListener) listeners[i + 1]).editingStopped(changeEvent);
198
            }
199
        }
200
    } 
201

    
202
/**
203
 * DOCUMENT ME!
204
 * 
205
 */
206
    protected void fireEditingCanceled() {        
207
        // Guaranteed to return a non-null array
208
        Object[] listeners = listenerList.getListenerList();
209
        // Process the listeners last to first, notifying
210
        // those that are interested in this event
211
        for (int i = listeners.length - 2; i >= 0; i -= 2) {
212
            if (listeners[i] == CellEditorListener.class) {
213
                // Lazily create the event:
214
                if (changeEvent == null) {
215
                    changeEvent = new ChangeEvent(this);
216
                }
217
                ((CellEditorListener) listeners[i + 1]).editingCanceled(changeEvent);
218
            }
219
        }
220
    } 
221
// implements javax.swing.tree.TreeCellEditor
222

    
223
/**
224
 * 
225
 * 
226
 * 
227
 * @return 
228
 * @param tree 
229
 * @param value 
230
 * @param isSelected 
231
 * @param expanded 
232
 * @param leaf 
233
 * @param row 
234
 */
235
    public Component getTreeCellEditorComponent(JTree tree, Object value, boolean isSelected, boolean expanded, boolean leaf, int row) {        
236
        tree.convertValueToText(value, isSelected,
237
                expanded, leaf, row, false);
238
        editorComponent = (JComponent) value;
239
        container = tree;
240
        return editorComponent;
241
    } 
242
// implements javax.swing.table.TableCellEditor
243

    
244
/**
245
 * 
246
 * 
247
 * 
248
 * @return 
249
 * @param table 
250
 * @param value 
251
 * @param isSelected 
252
 * @param row 
253
 * @param column 
254
 */
255
    public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {        
256
        editorComponent = (JComponent) value;
257
        container = table;
258
        return editorComponent;
259
    } 
260
 }
261
// End of class JComponentCellEditor