FileDocCategorySizeDatePackage
EditableDialogCellEditor.javaAPI DocAndroid 1.5 API14547Wed May 06 22:41:10 BST 2009com.android.ide.eclipse.editors.ui

EditableDialogCellEditor

public abstract class EditableDialogCellEditor extends org.eclipse.jface.viewers.DialogCellEditor
Custom DialogCellEditor, replacing the Label with an editable {@link Text} widget.

Also set the button to {@link SWT#FLAT} to make sure it looks good on MacOS X.

Most of the code comes from TextCellEditor.

Fields Summary
private org.eclipse.swt.widgets.Text
text
private org.eclipse.swt.events.ModifyListener
modifyListener
private boolean
isSelection
State information for updating action enablement
private boolean
isDeleteable
private boolean
isSelectable
Constructors Summary
EditableDialogCellEditor(org.eclipse.swt.widgets.Composite parent)

    
      
        super(parent);
    
Methods Summary
private voidcheckDeleteable()
Checks to see if the "deletable" state (can delete/ nothing to delete) has changed and if so fire an enablement changed notification.

        boolean oldIsDeleteable = isDeleteable;
        isDeleteable = isDeleteEnabled();
        if (oldIsDeleteable != isDeleteable) {
            fireEnablementChanged(DELETE);
        }
    
private voidcheckSelectable()
Checks to see if the "selectable" state (can select) has changed and if so fire an enablement changed notification.

        boolean oldIsSelectable = isSelectable;
        isSelectable = isSelectAllEnabled();
        if (oldIsSelectable != isSelectable) {
            fireEnablementChanged(SELECT_ALL);
        }
    
private voidcheckSelection()
Checks to see if the selection state (selection / no selection) has changed and if so fire an enablement changed notification.

        boolean oldIsSelection = isSelection;
        isSelection = text.getSelectionCount() > 0;
        if (oldIsSelection != isSelection) {
            fireEnablementChanged(COPY);
            fireEnablementChanged(CUT);
        }
    
protected org.eclipse.swt.widgets.ButtoncreateButton(org.eclipse.swt.widgets.Composite parent)

        Button result = new Button(parent, SWT.DOWN | SWT.FLAT);
        result.setText("..."); //$NON-NLS-1$
        return result;
    
protected org.eclipse.swt.widgets.ControlcreateContents(org.eclipse.swt.widgets.Composite cell)

        text = new Text(cell, SWT.SINGLE);
        text.addSelectionListener(new SelectionAdapter() {
            @Override
            public void widgetDefaultSelected(SelectionEvent e) {
                handleDefaultSelection(e);
            }
        });
        text.addKeyListener(new KeyAdapter() {
            // hook key pressed - see PR 14201  
            @Override
            public void keyPressed(KeyEvent e) {
                keyReleaseOccured(e);

                // as a result of processing the above call, clients may have
                // disposed this cell editor
                if ((getControl() == null) || getControl().isDisposed()) {
                    return;
                }
                checkSelection(); // see explanation below
                checkDeleteable();
                checkSelectable();
            }
        });
        text.addTraverseListener(new TraverseListener() {
            public void keyTraversed(TraverseEvent e) {
                if (e.detail == SWT.TRAVERSE_ESCAPE
                        || e.detail == SWT.TRAVERSE_RETURN) {
                    e.doit = false;
                }
            }
        });
        // We really want a selection listener but it is not supported so we
        // use a key listener and a mouse listener to know when selection changes
        // may have occurred
        text.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseUp(MouseEvent e) {
                checkSelection();
                checkDeleteable();
                checkSelectable();
            }
        });
        text.addFocusListener(new FocusAdapter() {
            @Override
            public void focusLost(FocusEvent e) {
                EditableDialogCellEditor.this.focusLost();
            }
        });
        text.setFont(cell.getFont());
        text.setBackground(cell.getBackground());
        text.setText("");//$NON-NLS-1$
        text.addModifyListener(getModifyListener());
        return text;
    
protected java.lang.ObjectdoGetValue()
The TextCellEditor implementation of this CellEditor framework method returns the text string.

return
the text string

        return text.getText();
    
protected voiddoSetFocus()

        if (text != null) {
            text.selectAll();
            text.setFocus();
            checkSelection();
            checkDeleteable();
            checkSelectable();
        }
    
protected voideditOccured(org.eclipse.swt.events.ModifyEvent e)
Processes a modify event that occurred in this text cell editor. This framework method performs validation and sets the error message accordingly, and then reports a change via fireEditorValueChanged. Subclasses should call this method at appropriate times. Subclasses may extend or reimplement.

param
e the SWT modify event

        String value = text.getText();
        if (value == null) {
            value = "";//$NON-NLS-1$
        }
        Object typedValue = value;
        boolean oldValidState = isValueValid();
        boolean newValidState = isCorrect(typedValue);

        if (!newValidState) {
            // try to insert the current value into the error message.
            setErrorMessage(MessageFormat.format(getErrorMessage(),
                    new Object[] { value }));
        }
        valueChanged(oldValidState, newValidState);
    
private org.eclipse.swt.events.ModifyListenergetModifyListener()
Return the modify listener.

        if (modifyListener == null) {
            modifyListener = new ModifyListener() {
                public void modifyText(ModifyEvent e) {
                    editOccured(e);
                }
            };
        }
        return modifyListener;
    
protected voidhandleDefaultSelection(org.eclipse.swt.events.SelectionEvent event)
Handles a default selection event from the text control by applying the editor value and deactivating this cell editor.

param
event the selection event
since
3.0

        // same with enter-key handling code in keyReleaseOccured(e);
        fireApplyEditorValue();
        deactivate();
    
public booleanisCopyEnabled()
The TextCellEditor implementation of this CellEditor method returns true if the current selection is not empty.

        if (text == null || text.isDisposed()) {
            return false;
        }
        return text.getSelectionCount() > 0;
    
public booleanisCutEnabled()
The TextCellEditor implementation of this CellEditor method returns true if the current selection is not empty.

        if (text == null || text.isDisposed()) {
            return false;
        }
        return text.getSelectionCount() > 0;
    
public booleanisDeleteEnabled()
The TextCellEditor implementation of this CellEditor method returns true if there is a selection or if the caret is not positioned at the end of the text.

        if (text == null || text.isDisposed()) {
            return false;
        }
        return text.getSelectionCount() > 0
                || text.getCaretPosition() < text.getCharCount();
    
public booleanisPasteEnabled()
The TextCellEditor implementation of this CellEditor method always returns true.

        if (text == null || text.isDisposed()) {
            return false;
        }
        return true;
    
public booleanisSaveAllEnabled()
Check if save all is enabled

return
true if it is

        if (text == null || text.isDisposed()) {
            return false;
        }
        return true;
    
public booleanisSelectAllEnabled()
Returns true if this cell editor is able to perform the select all action.

This default implementation always returns false.

Subclasses may override

return
true if select all is possible, false otherwise

        if (text == null || text.isDisposed()) {
            return false;
        }
        return text.getCharCount() > 0;
    
protected voidkeyReleaseOccured(org.eclipse.swt.events.KeyEvent keyEvent)
Processes a key release event that occurred in this cell editor.

The TextCellEditor implementation of this framework method ignores when the RETURN key is pressed since this is handled in handleDefaultSelection. An exception is made for Ctrl+Enter for multi-line texts, since a default selection event is not sent in this case.

param
keyEvent the key event

        if (keyEvent.character == '\r") { // Return key
            // Enter is handled in handleDefaultSelection.
            // Do not apply the editor value in response to an Enter key event
            // since this can be received from the IME when the intent is -not-
            // to apply the value.  
            // See bug 39074 [CellEditors] [DBCS] canna input mode fires bogus event from Text Control
            //
            // An exception is made for Ctrl+Enter for multi-line texts, since
            // a default selection event is not sent in this case. 
            if (text != null && !text.isDisposed()
                    && (text.getStyle() & SWT.MULTI) != 0) {
                if ((keyEvent.stateMask & SWT.CTRL) != 0) {
                    super.keyReleaseOccured(keyEvent);
                }
            }
            return;
        }
        super.keyReleaseOccured(keyEvent);
    
public voidperformCopy()
The TextCellEditor implementation of this CellEditor method copies the current selection to the clipboard.

        text.copy();
    
public voidperformCut()
The TextCellEditor implementation of this CellEditor method cuts the current selection to the clipboard.

        text.cut();
        checkSelection();
        checkDeleteable();
        checkSelectable();
    
public voidperformDelete()
The TextCellEditor implementation of this CellEditor method deletes the current selection or, if there is no selection, the character next character from the current position.

        if (text.getSelectionCount() > 0) {
            // remove the contents of the current selection
            text.insert(""); //$NON-NLS-1$
        } else {
            // remove the next character
            int pos = text.getCaretPosition();
            if (pos < text.getCharCount()) {
                text.setSelection(pos, pos + 1);
                text.insert(""); //$NON-NLS-1$
            }
        }
        checkSelection();
        checkDeleteable();
        checkSelectable();
    
public voidperformPaste()
The TextCellEditor implementation of this CellEditor method pastes the the clipboard contents over the current selection.

        text.paste();
        checkSelection();
        checkDeleteable();
        checkSelectable();
    
public voidperformSelectAll()
The TextCellEditor implementation of this CellEditor method selects all of the current text.

        text.selectAll();
        checkSelection();
        checkDeleteable();
    
protected voidupdateContents(java.lang.Object value)

        Assert.isTrue(text != null && (value == null || (value instanceof String)));
        if (value != null) {
            text.removeModifyListener(getModifyListener());
            text.setText((String) value);
            text.addModifyListener(getModifyListener());
        }