001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041: package org.netbeans.modules.visual.action;
042:
043: import org.netbeans.api.visual.action.InplaceEditorProvider;
044: import org.netbeans.api.visual.action.TextFieldInplaceEditor;
045: import org.netbeans.api.visual.widget.Scene;
046: import org.netbeans.api.visual.widget.Widget;
047:
048: import javax.swing.*;
049: import javax.swing.event.DocumentListener;
050: import javax.swing.event.DocumentEvent;
051: import java.awt.*;
052: import java.awt.event.*;
053: import java.util.EnumSet;
054:
055: /**
056: * @author David Kaspar
057: */
058: public final class TextFieldInplaceEditorProvider implements
059: InplaceEditorProvider<JTextField> {
060:
061: private TextFieldInplaceEditor editor;
062: private EnumSet<InplaceEditorProvider.ExpansionDirection> expansionDirections;
063:
064: private KeyListener keyListener;
065: private FocusListener focusListener;
066: private DocumentListener documentListener;
067:
068: public TextFieldInplaceEditorProvider(
069: TextFieldInplaceEditor editor,
070: EnumSet<InplaceEditorProvider.ExpansionDirection> expansionDirections) {
071: this .editor = editor;
072: this .expansionDirections = expansionDirections;
073: }
074:
075: public JTextField createEditorComponent(
076: EditorController controller, Widget widget) {
077: if (!editor.isEnabled(widget))
078: return null;
079: JTextField field = new JTextField(editor.getText(widget));
080: field.selectAll();
081: Scene scene = widget.getScene();
082: double zoomFactor = scene.getZoomFactor();
083: if (zoomFactor > 1.0) {
084: Font font = scene.getDefaultFont();
085: font = font
086: .deriveFont((float) (font.getSize2D() * zoomFactor));
087: field.setFont(font);
088: }
089: return field;
090: }
091:
092: public void notifyOpened(final EditorController controller,
093: Widget widget, JTextField editor) {
094: editor.setMinimumSize(new Dimension(64, 19));
095: keyListener = new KeyAdapter() {
096: public void keyPressed(KeyEvent e) {
097: switch (e.getKeyChar()) {
098: case KeyEvent.VK_ESCAPE:
099: e.consume();
100: controller.closeEditor(false);
101: break;
102: case KeyEvent.VK_ENTER:
103: e.consume();
104: controller.closeEditor(true);
105: break;
106: }
107: }
108: };
109: focusListener = new FocusAdapter() {
110: public void focusLost(FocusEvent e) {
111: controller.closeEditor(true);
112: }
113: };
114: documentListener = new DocumentListener() {
115: public void insertUpdate(DocumentEvent e) {
116: controller.notifyEditorComponentBoundsChanged();
117: }
118:
119: public void removeUpdate(DocumentEvent e) {
120: controller.notifyEditorComponentBoundsChanged();
121: }
122:
123: public void changedUpdate(DocumentEvent e) {
124: controller.notifyEditorComponentBoundsChanged();
125: }
126: };
127: editor.addKeyListener(keyListener);
128: editor.addFocusListener(focusListener);
129: editor.getDocument().addDocumentListener(documentListener);
130: editor.selectAll();
131: }
132:
133: public void notifyClosing(EditorController controller,
134: Widget widget, JTextField editor, boolean commit) {
135: editor.getDocument().removeDocumentListener(documentListener);
136: editor.removeFocusListener(focusListener);
137: editor.removeKeyListener(keyListener);
138: if (commit) {
139: this .editor.setText(widget, editor.getText());
140: if (widget != null)
141: widget.getScene().validate();
142: }
143: }
144:
145: public Rectangle getInitialEditorComponentBounds(
146: EditorController controller, Widget widget,
147: JTextField editor, Rectangle viewBounds) {
148: return null;
149: }
150:
151: public EnumSet<ExpansionDirection> getExpansionDirections(
152: EditorController controller, Widget widget,
153: JTextField editor) {
154: return expansionDirections;
155: }
156:
157: }
|