1 /*******************************************************************************
2 * Copyright (c) 2005, 2011 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
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package org.eclipse.jdt.internal.ui.dialogs;
13 import org.eclipse.swt.SWT;
14 import org.eclipse.swt.events.FocusAdapter;
15 import org.eclipse.swt.events.FocusEvent;
16 import org.eclipse.swt.events.KeyAdapter;
17 import org.eclipse.swt.events.KeyEvent;
18 import org.eclipse.swt.events.ModifyEvent;
19 import org.eclipse.swt.events.ModifyListener;
20 import org.eclipse.swt.events.MouseAdapter;
21 import org.eclipse.swt.events.MouseEvent;
22 import org.eclipse.swt.events.SelectionAdapter;
23 import org.eclipse.swt.events.SelectionEvent;
24 import org.eclipse.swt.events.TraverseEvent;
25 import org.eclipse.swt.events.TraverseListener;
26 import org.eclipse.swt.widgets.Composite;
27 import org.eclipse.swt.widgets.Control;
28 import org.eclipse.swt.widgets.Text;
30 import org.eclipse.core.runtime.Assert;
32 import org.eclipse.jface.contentassist.SubjectControlContentAssistant;
33 import org.eclipse.jface.viewers.CellEditor;
34 import org.eclipse.jface.viewers.IStructuredSelection;
35 import org.eclipse.jface.viewers.TableViewer;
37 import org.eclipse.ui.contentassist.ContentAssistHandler;
39 import org.eclipse.jdt.internal.corext.util.Messages;
41 import org.eclipse.jdt.internal.ui.refactoring.ChangeParametersControl;
42 import org.eclipse.jdt.internal.ui.refactoring.contentassist.ControlContentAssistHelper;
43 import org.eclipse.jdt.internal.ui.refactoring.contentassist.JavaTypeCompletionProcessor;
46 * <code>TableTextCellEditor</code> is a copy of TextCellEditor, with the
50 * <li> modify events are sent out as the text is changed, and not only after
51 * editing is done </li>
53 * <li>a content assistant is supported</li>
55 * <li>the user can go to the next/previous row with up and down keys</li>
58 public class TableTextCellEditor extends CellEditor {
59 public interface IActivationListener {
60 public void activate();
63 private final TableViewer fTableViewer;
64 private final int fColumn;
65 private final String fProperty;
67 * The editor's value on activation. This value is reset to the
68 * cell when the editor is left via ESC key.
70 String fOriginalValue;
71 SubjectControlContentAssistant fContentAssistant;
72 private IActivationListener fActivationListener;
76 private boolean isSelection = false;
77 private boolean isDeleteable = false;
78 private boolean isSelectable = false;
80 private static final int defaultStyle = SWT.SINGLE;
81 private ModifyListener fModifyListener;
83 public TableTextCellEditor(TableViewer tableViewer, int column) {
84 super(tableViewer.getTable(), defaultStyle);
85 fTableViewer= tableViewer;
87 fProperty= (String) tableViewer.getColumnProperties()[column];
91 public void activate() {
93 if (fActivationListener != null)
94 fActivationListener.activate();
95 fOriginalValue= text.getText();
98 private void fireModifyEvent(Object newValue) {
99 fTableViewer.getCellModifier().modify(
100 ((IStructuredSelection) fTableViewer.getSelection()).getFirstElement(),
101 fProperty, newValue);
105 protected void focusLost() {
106 if (fContentAssistant != null && fContentAssistant.hasProposalPopupFocus()) {
107 // skip focus lost if it went to the content assist popup
113 public void setContentAssistant(SubjectControlContentAssistant assistant) {
114 fContentAssistant= assistant;
117 public void setActivationListener(IActivationListener listener) {
118 fActivationListener= listener;
121 public Text getText() {
125 protected void checkDeleteable() {
126 boolean oldIsDeleteable = isDeleteable;
127 isDeleteable = isDeleteEnabled();
128 if (oldIsDeleteable != isDeleteable) {
129 fireEnablementChanged(DELETE);
133 protected void checkSelectable() {
134 boolean oldIsSelectable = isSelectable;
135 isSelectable = isSelectAllEnabled();
136 if (oldIsSelectable != isSelectable) {
137 fireEnablementChanged(SELECT_ALL);
141 protected void checkSelection() {
142 boolean oldIsSelection = isSelection;
143 isSelection = text.getSelectionCount() > 0;
144 if (oldIsSelection != isSelection) {
145 fireEnablementChanged(COPY);
146 fireEnablementChanged(CUT);
150 private ModifyListener getModifyListener() {
151 if (fModifyListener == null) {
152 fModifyListener = new ModifyListener() {
153 public void modifyText(ModifyEvent e) {
158 return fModifyListener;
162 * Method declared on CellEditor.
165 protected Control createControl(Composite parent) {
166 text= new Text(parent, getStyle());
167 text.addSelectionListener(new SelectionAdapter() {
169 public void widgetDefaultSelected(SelectionEvent e) {
170 handleDefaultSelection(e);
173 text.addKeyListener(new KeyAdapter() {
175 public void keyPressed(KeyEvent e) {
176 // support switching rows while editing:
177 if (e.stateMask == SWT.MOD1 || e.stateMask == SWT.MOD2) {
178 if (e.keyCode == SWT.ARROW_UP || e.keyCode == SWT.ARROW_DOWN) {
179 // allow starting multi-selection even if in edit mode
186 if (e.stateMask != SWT.NONE)
190 case SWT.ARROW_DOWN :
192 int nextRow= fTableViewer.getTable().getSelectionIndex() + 1;
193 if (nextRow >= fTableViewer.getTable().getItemCount())
200 int prevRow= fTableViewer.getTable().getSelectionIndex() - 1;
213 private void editRow(int row) {
214 fTableViewer.getTable().setSelection(row);
215 IStructuredSelection newSelection= (IStructuredSelection) fTableViewer.getSelection();
216 if (newSelection.size() == 1)
217 fTableViewer.editElement(newSelection.getFirstElement(), fColumn);
220 text.addKeyListener(new KeyAdapter() {
221 // hook key pressed - see PR 14201
223 public void keyPressed(KeyEvent e) {
224 keyReleaseOccured(e);
226 // as a result of processing the above call, clients may have
227 // disposed this cell editor
228 if ((getControl() == null) || getControl().isDisposed())
230 checkSelection(); // see explaination below
235 text.addTraverseListener(new TraverseListener() {
236 public void keyTraversed(TraverseEvent e) {
237 if (e.detail == SWT.TRAVERSE_ESCAPE
238 || e.detail == SWT.TRAVERSE_RETURN) {
243 // We really want a selection listener but it is not supported so we
244 // use a key listener and a mouse listener to know when selection changes
246 text.addMouseListener(new MouseAdapter() {
248 public void mouseUp(MouseEvent e) {
254 text.addFocusListener(new FocusAdapter() {
256 public void focusLost(FocusEvent e) {
257 e.display.asyncExec(new Runnable() {
259 // without the asyncExec, focus has not had a chance to go to the content assist proposals
260 TableTextCellEditor.this.focusLost();
265 text.setFont(parent.getFont());
266 text.setBackground(parent.getBackground());
267 text.setText("");//$NON-NLS-1$
268 text.addModifyListener(getModifyListener());
274 protected void fireCancelEditor() {
275 /* bug 58540: change signature refactoring interaction: validate as you type [refactoring] */
276 text.setText(fOriginalValue);
277 super.fireApplyEditorValue();
281 * The <code>TextCellEditor</code> implementation of
282 * this <code>CellEditor</code> framework method returns
285 * @return the text string
288 protected Object doGetValue() {
289 return text.getText();
293 protected void doSetFocus() {
304 * The <code>TextCellEditor2</code> implementation of
305 * this <code>CellEditor</code> framework method accepts
306 * a text string (type <code>String</code>).
308 * @param value a text string (type <code>String</code>)
311 protected void doSetValue(Object value) {
312 Assert.isTrue(text != null && (value instanceof String));
313 text.removeModifyListener(getModifyListener());
314 text.setText((String) value);
315 text.addModifyListener(getModifyListener());
319 * Processes a modify event that occurred in this text cell editor.
320 * This framework method performs validation and sets the error message
321 * accordingly, and then reports a change via <code>fireEditorValueChanged</code>.
322 * Subclasses should call this method at appropriate times. Subclasses
323 * may extend or reimplement.
325 * @param e the SWT modify event
327 protected void editOccured(ModifyEvent e) {
328 String value = text.getText();
329 boolean oldValidState = isValueValid();
330 boolean newValidState = isCorrect(value);
331 if (!newValidState) {
332 // try to insert the current value into the error message.
333 setErrorMessage(Messages.format(getErrorMessage(),
334 new Object[] { value }));
336 valueChanged(oldValidState, newValidState);
337 fireModifyEvent(text.getText()); // update model on-the-fly
341 public LayoutData getLayoutData() {
342 return new LayoutData();
345 protected void handleDefaultSelection(SelectionEvent event) {
346 // same with enter-key handling code in keyReleaseOccured(e);
347 fireApplyEditorValue();
352 public boolean isCopyEnabled() {
353 if (text == null || text.isDisposed())
355 return text.getSelectionCount() > 0;
359 public boolean isCutEnabled() {
360 if (text == null || text.isDisposed())
362 return text.getSelectionCount() > 0;
366 public boolean isDeleteEnabled() {
367 if (text == null || text.isDisposed())
369 return text.getSelectionCount() > 0
370 || text.getCaretPosition() < text.getCharCount();
374 public boolean isPasteEnabled() {
375 if (text == null || text.isDisposed())
381 public boolean isSelectAllEnabled() {
382 if (text == null || text.isDisposed())
384 return text.getCharCount() > 0;
388 protected void keyReleaseOccured(KeyEvent keyEvent) {
389 if (keyEvent.character == '\r') { // Return key
390 // Enter is handled in handleDefaultSelection.
391 // Do not apply the editor value in response to an Enter key event
392 // since this can be received from the IME when the intent is -not-
393 // to apply the value.
394 // See bug 39074 [CellEditors] [DBCS] canna input mode fires bogus event from Text Control
396 // An exception is made for Ctrl+Enter for multi-line texts, since
397 // a default selection event is not sent in this case.
398 if (text != null && !text.isDisposed()
399 && (text.getStyle() & SWT.MULTI) != 0) {
400 if ((keyEvent.stateMask & SWT.CTRL) != 0) {
401 super.keyReleaseOccured(keyEvent);
406 super.keyReleaseOccured(keyEvent);
410 public void performCopy() {
415 public void performCut() {
423 public void performDelete() {
424 if (text.getSelectionCount() > 0)
425 // remove the contents of the current selection
426 text.insert(""); //$NON-NLS-1$
428 // remove the next character
429 int pos = text.getCaretPosition();
430 if (pos < text.getCharCount()) {
431 text.setSelection(pos, pos + 1);
432 text.insert(""); //$NON-NLS-1$
441 public void performPaste() {
449 public void performSelectAll() {
456 * @see org.eclipse.jface.viewers.CellEditor#dependsOnExternalFocusListener()
459 protected boolean dependsOnExternalFocusListener() {
463 public void generated_8020750380894542173(final TableViewer tableViewer, JavaTypeCompletionProcessor superInterfaceCompletionProcessor) {
464 SubjectControlContentAssistant contentAssistant= ControlContentAssistHelper.createJavaContentAssistant(superInterfaceCompletionProcessor);
465 Text cellEditorText= getText();
466 ContentAssistHandler.createHandlerForText(cellEditorText, contentAssistant);
467 TextFieldNavigationHandler.install(cellEditorText);
468 setContentAssistant(contentAssistant);
470 tableViewer.setCellEditors(new CellEditor[] { this });
473 public void generated_3875397303026142273(final int editorColumn, final ChangeParametersControl changeparameterscontrol) {
474 getText().addTraverseListener(new TraverseListener() {
475 public void keyTraversed(TraverseEvent e) {
477 case SWT.TRAVERSE_TAB_NEXT :
478 changeparameterscontrol.editColumnOrNextPossible(changeparameterscontrol.nextColumn(editorColumn));
479 e.detail= SWT.TRAVERSE_NONE;
482 case SWT.TRAVERSE_TAB_PREVIOUS :
483 changeparameterscontrol.editColumnOrPrevPossible(changeparameterscontrol.prevColumn(editorColumn));
484 e.detail= SWT.TRAVERSE_NONE;
492 TextFieldNavigationHandler.install(getText());