📄 textcelleditor.java
字号:
/******************************************************************************* * Copyright (c) 2000, 2006 IBM Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * * Contributors: * IBM Corporation - initial API and implementation *******************************************************************************/package org.eclipse.jface.viewers;import java.text.MessageFormat; // Not using ICU to support standalone JFace scenarioimport org.eclipse.jface.util.Assert;import org.eclipse.swt.SWT;import org.eclipse.swt.events.FocusAdapter;import org.eclipse.swt.events.FocusEvent;import org.eclipse.swt.events.KeyAdapter;import org.eclipse.swt.events.KeyEvent;import org.eclipse.swt.events.ModifyEvent;import org.eclipse.swt.events.ModifyListener;import org.eclipse.swt.events.MouseAdapter;import org.eclipse.swt.events.MouseEvent;import org.eclipse.swt.events.SelectionAdapter;import org.eclipse.swt.events.SelectionEvent;import org.eclipse.swt.events.TraverseEvent;import org.eclipse.swt.events.TraverseListener;import org.eclipse.swt.widgets.Composite;import org.eclipse.swt.widgets.Control;import org.eclipse.swt.widgets.Text;/** * A cell editor that manages a text entry field. * The cell editor's value is the text string itself. * <p> * This class may be instantiated; it is not intended to be subclassed. * </p> */public class TextCellEditor extends CellEditor { /** * The text control; initially <code>null</code>. */ protected Text text; private ModifyListener modifyListener; /** * State information for updating action enablement */ private boolean isSelection = false; private boolean isDeleteable = false; private boolean isSelectable = false; /** * Default TextCellEditor style * specify no borders on text widget as cell outline in table already * provides the look of a border. */ private static final int defaultStyle = SWT.SINGLE; /** * Creates a new text string cell editor with no control * The cell editor value is the string itself, which is initially the empty * string. Initially, the cell editor has no cell validator. * * @since 2.1 */ public TextCellEditor() { setStyle(defaultStyle); } /** * Creates a new text string cell editor parented under the given control. * The cell editor value is the string itself, which is initially the empty string. * Initially, the cell editor has no cell validator. * * @param parent the parent control */ public TextCellEditor(Composite parent) { this(parent, defaultStyle); } /** * Creates a new text string cell editor parented under the given control. * The cell editor value is the string itself, which is initially the empty string. * Initially, the cell editor has no cell validator. * * @param parent the parent control * @param style the style bits * @since 2.1 */ public TextCellEditor(Composite parent, int style) { super(parent, style); } /** * Checks to see if the "deleteable" state (can delete/ * nothing to delete) has changed and if so fire an * enablement changed notification. */ private void checkDeleteable() { boolean oldIsDeleteable = isDeleteable; isDeleteable = isDeleteEnabled(); if (oldIsDeleteable != isDeleteable) { fireEnablementChanged(DELETE); } } /** * Checks to see if the "selectable" state (can select) * has changed and if so fire an enablement changed notification. */ private void checkSelectable() { boolean oldIsSelectable = isSelectable; isSelectable = isSelectAllEnabled(); if (oldIsSelectable != isSelectable) { fireEnablementChanged(SELECT_ALL); } } /** * Checks to see if the selection state (selection / * no selection) has changed and if so fire an * enablement changed notification. */ private void checkSelection() { boolean oldIsSelection = isSelection; isSelection = text.getSelectionCount() > 0; if (oldIsSelection != isSelection) { fireEnablementChanged(COPY); fireEnablementChanged(CUT); } } /* (non-Javadoc) * Method declared on CellEditor. */ protected Control createControl(Composite parent) { text = new Text(parent, getStyle()); text.addSelectionListener(new SelectionAdapter() { public void widgetDefaultSelected(SelectionEvent e) { handleDefaultSelection(e); } }); text.addKeyListener(new KeyAdapter() { // hook key pressed - see PR 14201 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 explaination 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 occured text.addMouseListener(new MouseAdapter() { public void mouseUp(MouseEvent e) { checkSelection(); checkDeleteable(); checkSelectable(); } }); text.addFocusListener(new FocusAdapter() { public void focusLost(FocusEvent e) { TextCellEditor.this.focusLost(); } }); text.setFont(parent.getFont()); text.setBackground(parent.getBackground()); text.setText("");//$NON-NLS-1$ text.addModifyListener(getModifyListener()); return text; } /** * The <code>TextCellEditor</code> implementation of * this <code>CellEditor</code> framework method returns * the text string. * * @return the text string */ protected Object doGetValue() { return text.getText(); } /* (non-Javadoc) * Method declared on CellEditor. */ protected void doSetFocus() { if (text != null) { text.selectAll(); text.setFocus(); checkSelection(); checkDeleteable(); checkSelectable(); } } /** * The <code>TextCellEditor</code> implementation of * this <code>CellEditor</code> framework method accepts * a text string (type <code>String</code>). * * @param value a text string (type <code>String</code>) */ protected void doSetValue(Object value) { Assert.isTrue(text != null && (value instanceof String)); text.removeModifyListener(getModifyListener()); text.setText((String) value); text.addModifyListener(getModifyListener()); } /** * 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 <code>fireEditorValueChanged</code>. * Subclasses should call this method at appropriate times. Subclasses * may extend or reimplement. *
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -