KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jface > viewers > TextCellEditor


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11
12 package org.eclipse.jface.viewers;
13
14 import java.text.MessageFormat JavaDoc; // Not using ICU to support standalone JFace scenario
15

16 import org.eclipse.core.runtime.Assert;
17 import org.eclipse.swt.SWT;
18 import org.eclipse.swt.events.FocusAdapter;
19 import org.eclipse.swt.events.FocusEvent;
20 import org.eclipse.swt.events.KeyAdapter;
21 import org.eclipse.swt.events.KeyEvent;
22 import org.eclipse.swt.events.ModifyEvent;
23 import org.eclipse.swt.events.ModifyListener;
24 import org.eclipse.swt.events.MouseAdapter;
25 import org.eclipse.swt.events.MouseEvent;
26 import org.eclipse.swt.events.SelectionAdapter;
27 import org.eclipse.swt.events.SelectionEvent;
28 import org.eclipse.swt.events.TraverseEvent;
29 import org.eclipse.swt.events.TraverseListener;
30 import org.eclipse.swt.widgets.Composite;
31 import org.eclipse.swt.widgets.Control;
32 import org.eclipse.swt.widgets.Text;
33
34 /**
35  * A cell editor that manages a text entry field.
36  * The cell editor's value is the text string itself.
37  * <p>
38  * This class may be instantiated; it is not intended to be subclassed.
39  * </p>
40  */

41 public class TextCellEditor extends CellEditor {
42
43     /**
44      * The text control; initially <code>null</code>.
45      */

46     protected Text text;
47
48     private ModifyListener modifyListener;
49
50     /**
51      * State information for updating action enablement
52      */

53     private boolean isSelection = false;
54
55     private boolean isDeleteable = false;
56
57     private boolean isSelectable = false;
58
59     /**
60      * Default TextCellEditor style
61      * specify no borders on text widget as cell outline in table already
62      * provides the look of a border.
63      */

64     private static final int defaultStyle = SWT.SINGLE;
65
66     /**
67      * Creates a new text string cell editor with no control
68      * The cell editor value is the string itself, which is initially the empty
69      * string. Initially, the cell editor has no cell validator.
70      *
71      * @since 2.1
72      */

73     public TextCellEditor() {
74         setStyle(defaultStyle);
75     }
76
77     /**
78      * Creates a new text string cell editor parented under the given control.
79      * The cell editor value is the string itself, which is initially the empty string.
80      * Initially, the cell editor has no cell validator.
81      *
82      * @param parent the parent control
83      */

84     public TextCellEditor(Composite parent) {
85         this(parent, defaultStyle);
86     }
87
88     /**
89      * Creates a new text string cell editor parented under the given control.
90      * The cell editor value is the string itself, which is initially the empty string.
91      * Initially, the cell editor has no cell validator.
92      *
93      * @param parent the parent control
94      * @param style the style bits
95      * @since 2.1
96      */

97     public TextCellEditor(Composite parent, int style) {
98         super(parent, style);
99     }
100
101     /**
102      * Checks to see if the "deletable" state (can delete/
103      * nothing to delete) has changed and if so fire an
104      * enablement changed notification.
105      */

106     private void checkDeleteable() {
107         boolean oldIsDeleteable = isDeleteable;
108         isDeleteable = isDeleteEnabled();
109         if (oldIsDeleteable != isDeleteable) {
110             fireEnablementChanged(DELETE);
111         }
112     }
113
114     /**
115      * Checks to see if the "selectable" state (can select)
116      * has changed and if so fire an enablement changed notification.
117      */

118     private void checkSelectable() {
119         boolean oldIsSelectable = isSelectable;
120         isSelectable = isSelectAllEnabled();
121         if (oldIsSelectable != isSelectable) {
122             fireEnablementChanged(SELECT_ALL);
123         }
124     }
125
126     /**
127      * Checks to see if the selection state (selection /
128      * no selection) has changed and if so fire an
129      * enablement changed notification.
130      */

131     private void checkSelection() {
132         boolean oldIsSelection = isSelection;
133         isSelection = text.getSelectionCount() > 0;
134         if (oldIsSelection != isSelection) {
135             fireEnablementChanged(COPY);
136             fireEnablementChanged(CUT);
137         }
138     }
139
140     /* (non-Javadoc)
141      * Method declared on CellEditor.
142      */

143     protected Control createControl(Composite parent) {
144         text = new Text(parent, getStyle());
145         text.addSelectionListener(new SelectionAdapter() {
146             public void widgetDefaultSelected(SelectionEvent e) {
147                 handleDefaultSelection(e);
148             }
149         });
150         text.addKeyListener(new KeyAdapter() {
151             // hook key pressed - see PR 14201
152
public void keyPressed(KeyEvent e) {
153                 keyReleaseOccured(e);
154
155                 // as a result of processing the above call, clients may have
156
// disposed this cell editor
157
if ((getControl() == null) || getControl().isDisposed()) {
158                     return;
159                 }
160                 checkSelection(); // see explanation below
161
checkDeleteable();
162                 checkSelectable();
163             }
164         });
165         text.addTraverseListener(new TraverseListener() {
166             public void keyTraversed(TraverseEvent e) {
167                 if (e.detail == SWT.TRAVERSE_ESCAPE
168                         || e.detail == SWT.TRAVERSE_RETURN) {
169                     e.doit = false;
170                 }
171             }
172         });
173         // We really want a selection listener but it is not supported so we
174
// use a key listener and a mouse listener to know when selection changes
175
// may have occurred
176
text.addMouseListener(new MouseAdapter() {
177             public void mouseUp(MouseEvent e) {
178                 checkSelection();
179                 checkDeleteable();
180                 checkSelectable();
181             }
182         });
183         text.addFocusListener(new FocusAdapter() {
184             public void focusLost(FocusEvent e) {
185                 TextCellEditor.this.focusLost();
186             }
187         });
188         text.setFont(parent.getFont());
189         text.setBackground(parent.getBackground());
190         text.setText("");//$NON-NLS-1$
191
text.addModifyListener(getModifyListener());
192         return text;
193     }
194
195     /**
196      * The <code>TextCellEditor</code> implementation of
197      * this <code>CellEditor</code> framework method returns
198      * the text string.
199      *
200      * @return the text string
201      */

202     protected Object JavaDoc doGetValue() {
203         return text.getText();
204     }
205
206     /* (non-Javadoc)
207      * Method declared on CellEditor.
208      */

209     protected void doSetFocus() {
210         if (text != null) {
211             text.selectAll();
212             text.setFocus();
213             checkSelection();
214             checkDeleteable();
215             checkSelectable();
216         }
217     }
218
219     /**
220      * The <code>TextCellEditor</code> implementation of
221      * this <code>CellEditor</code> framework method accepts
222      * a text string (type <code>String</code>).
223      *
224      * @param value a text string (type <code>String</code>)
225      */

226     protected void doSetValue(Object JavaDoc value) {
227         Assert.isTrue(text != null && (value instanceof String JavaDoc));
228         text.removeModifyListener(getModifyListener());
229         text.setText((String JavaDoc) value);
230         text.addModifyListener(getModifyListener());
231     }
232
233     /**
234      * Processes a modify event that occurred in this text cell editor.
235      * This framework method performs validation and sets the error message
236      * accordingly, and then reports a change via <code>fireEditorValueChanged</code>.
237      * Subclasses should call this method at appropriate times. Subclasses
238      * may extend or reimplement.
239      *
240      * @param e the SWT modify event
241      */

242     protected void editOccured(ModifyEvent e) {
243         String JavaDoc value = text.getText();
244         if (value == null) {
245             value = "";//$NON-NLS-1$
246
}
247         Object JavaDoc typedValue = value;
248         boolean oldValidState = isValueValid();
249         boolean newValidState = isCorrect(typedValue);
250         if (typedValue == null && newValidState) {
251             Assert.isTrue(false,
252                     "Validator isn't limiting the cell editor's type range");//$NON-NLS-1$
253
}
254         if (!newValidState) {
255             // try to insert the current value into the error message.
256
setErrorMessage(MessageFormat.format(getErrorMessage(),
257                     new Object JavaDoc[] { value }));
258         }
259         valueChanged(oldValidState, newValidState);
260     }
261
262     /**
263      * Since a text editor field is scrollable we don't
264      * set a minimumSize.
265      */

266     public LayoutData getLayoutData() {
267         return new LayoutData();
268     }
269
270     /**
271      * Return the modify listener.
272      */

273     private ModifyListener getModifyListener() {
274         if (modifyListener == null) {
275             modifyListener = new ModifyListener() {
276                 public void modifyText(ModifyEvent e) {
277                     editOccured(e);
278                 }
279             };
280         }
281         return modifyListener;
282     }
283
284     /**
285      * Handles a default selection event from the text control by applying the editor
286      * value and deactivating this cell editor.
287      *
288      * @param event the selection event
289      *
290      * @since 3.0
291      */

292     protected void handleDefaultSelection(SelectionEvent event) {
293         // same with enter-key handling code in keyReleaseOccured(e);
294
fireApplyEditorValue();
295         deactivate();
296     }
297
298     /**
299      * The <code>TextCellEditor</code> implementation of this
300      * <code>CellEditor</code> method returns <code>true</code> if
301      * the current selection is not empty.
302      */

303     public boolean isCopyEnabled() {
304         if (text == null || text.isDisposed()) {
305             return false;
306         }
307         return text.getSelectionCount() > 0;
308     }
309
310     /**
311      * The <code>TextCellEditor</code> implementation of this
312      * <code>CellEditor</code> method returns <code>true</code> if
313      * the current selection is not empty.
314      */

315     public boolean isCutEnabled() {
316         if (text == null || text.isDisposed()) {
317             return false;
318         }
319         return text.getSelectionCount() > 0;
320     }
321
322     /**
323      * The <code>TextCellEditor</code> implementation of this
324      * <code>CellEditor</code> method returns <code>true</code>
325      * if there is a selection or if the caret is not positioned
326      * at the end of the text.
327      */

328     public boolean isDeleteEnabled() {
329         if (text == null || text.isDisposed()) {
330             return false;
331         }
332         return text.getSelectionCount() > 0
333                 || text.getCaretPosition() < text.getCharCount();
334     }
335
336     /**
337      * The <code>TextCellEditor</code> implementation of this
338      * <code>CellEditor</code> method always returns <code>true</code>.
339      */

340     public boolean isPasteEnabled() {
341         if (text == null || text.isDisposed()) {
342             return false;
343         }
344         return true;
345     }
346
347     /**
348      * Check if save all is enabled
349      * @return true if it is
350      */

351     public boolean isSaveAllEnabled() {
352         if (text == null || text.isDisposed()) {
353             return false;
354         }
355         return true;
356     }
357
358     /**
359      * Returns <code>true</code> if this cell editor is
360      * able to perform the select all action.
361      * <p>
362      * This default implementation always returns
363      * <code>false</code>.
364      * </p>
365      * <p>
366      * Subclasses may override
367      * </p>
368      * @return <code>true</code> if select all is possible,
369      * <code>false</code> otherwise
370      */

371     public boolean isSelectAllEnabled() {
372         if (text == null || text.isDisposed()) {
373             return false;
374         }
375         return text.getCharCount() > 0;
376     }
377
378     /**
379      * Processes a key release event that occurred in this cell editor.
380      * <p>
381      * The <code>TextCellEditor</code> implementation of this framework method
382      * ignores when the RETURN key is pressed since this is handled in
383      * <code>handleDefaultSelection</code>.
384      * An exception is made for Ctrl+Enter for multi-line texts, since
385      * a default selection event is not sent in this case.
386      * </p>
387      *
388      * @param keyEvent the key event
389      */

390     protected void keyReleaseOccured(KeyEvent keyEvent) {
391         if (keyEvent.character == '\r') { // Return key
392
// Enter is handled in handleDefaultSelection.
393
// Do not apply the editor value in response to an Enter key event
394
// since this can be received from the IME when the intent is -not-
395
// to apply the value.
396
// See bug 39074 [CellEditors] [DBCS] canna input mode fires bogus event from Text Control
397
//
398
// An exception is made for Ctrl+Enter for multi-line texts, since
399
// a default selection event is not sent in this case.
400
if (text != null && !text.isDisposed()
401                     && (text.getStyle() & SWT.MULTI) != 0) {
402                 if ((keyEvent.stateMask & SWT.CTRL) != 0) {
403                     super.keyReleaseOccured(keyEvent);
404                 }
405             }
406             return;
407         }
408         super.keyReleaseOccured(keyEvent);
409     }
410
411     /**
412      * The <code>TextCellEditor</code> implementation of this
413      * <code>CellEditor</code> method copies the
414      * current selection to the clipboard.
415      */

416     public void performCopy() {
417         text.copy();
418     }
419
420     /**
421      * The <code>TextCellEditor</code> implementation of this
422      * <code>CellEditor</code> method cuts the
423      * current selection to the clipboard.
424      */

425     public void performCut() {
426         text.cut();
427         checkSelection();
428         checkDeleteable();
429         checkSelectable();
430     }
431
432     /**
433      * The <code>TextCellEditor</code> implementation of this
434      * <code>CellEditor</code> method deletes the
435      * current selection or, if there is no selection,
436      * the character next character from the current position.
437      */

438     public void performDelete() {
439         if (text.getSelectionCount() > 0) {
440             // remove the contents of the current selection
441
text.insert(""); //$NON-NLS-1$
442
} else {
443             // remove the next character
444
int pos = text.getCaretPosition();
445             if (pos < text.getCharCount()) {
446                 text.setSelection(pos, pos + 1);
447                 text.insert(""); //$NON-NLS-1$
448
}
449         }
450         checkSelection();
451         checkDeleteable();
452         checkSelectable();
453     }
454
455     /**
456      * The <code>TextCellEditor</code> implementation of this
457      * <code>CellEditor</code> method pastes the
458      * the clipboard contents over the current selection.
459      */

460     public void performPaste() {
461         text.paste();
462         checkSelection();
463         checkDeleteable();
464         checkSelectable();
465     }
466
467     /**
468      * The <code>TextCellEditor</code> implementation of this
469      * <code>CellEditor</code> method selects all of the
470      * current text.
471      */

472     public void performSelectAll() {
473         text.selectAll();
474         checkSelection();
475         checkDeleteable();
476     }
477
478     boolean dependsOnExternalFocusListener() {
479         return getClass() != TextCellEditor.class;
480     }
481 }
482
Popular Tags