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.WidgetAction;
045: import org.netbeans.modules.visual.util.GeomUtil;
046: import org.netbeans.api.visual.widget.Scene;
047: import org.netbeans.api.visual.widget.Widget;
048:
049: import javax.swing.*;
050: import java.awt.*;
051: import java.awt.event.MouseEvent;
052: import java.awt.event.KeyEvent;
053: import java.util.EnumSet;
054:
055: /**
056: * @author David Kaspar
057: */
058: public final class InplaceEditorAction<C extends JComponent> extends
059: WidgetAction.LockedAdapter implements
060: InplaceEditorProvider.EditorController {
061:
062: private InplaceEditorProvider<C> provider;
063:
064: private C editor = null;
065: private Widget widget = null;
066: private Rectangle rectangle = null;
067:
068: public InplaceEditorAction(InplaceEditorProvider<C> provider) {
069: this .provider = provider;
070: }
071:
072: protected boolean isLocked() {
073: return editor != null;
074: }
075:
076: public State mouseClicked(Widget widget, WidgetMouseEvent event) {
077: if (event.getButton() == MouseEvent.BUTTON1
078: && event.getClickCount() == 2) {
079: if (openEditor(widget))
080: return State.createLocked(widget, this );
081: }
082: return State.REJECTED;
083: }
084:
085: public State mousePressed(Widget widget, WidgetMouseEvent event) {
086: if (editor != null)
087: closeEditor(true);
088: return State.REJECTED;
089: }
090:
091: public State mouseReleased(Widget widget,
092: WidgetAction.WidgetMouseEvent event) {
093: if (editor != null)
094: closeEditor(true);
095: return State.REJECTED;
096: }
097:
098: public State keyPressed(Widget widget, WidgetKeyEvent event) {
099: if (event.getKeyChar() == KeyEvent.VK_ENTER)
100: if (openEditor(widget))
101: return State.createLocked(widget, this );
102: return State.REJECTED;
103: }
104:
105: public final boolean isEditorVisible() {
106: return editor != null;
107: }
108:
109: public final boolean openEditor(Widget widget) {
110: if (editor != null)
111: return false;
112:
113: Scene scene = widget.getScene();
114: JComponent component = scene.getView();
115: if (component == null)
116: return false;
117:
118: editor = provider.createEditorComponent(this , widget);
119: if (editor == null)
120: return false;
121: this .widget = widget;
122:
123: component.add(editor);
124: provider.notifyOpened(this , widget, editor);
125:
126: Rectangle rectangle = widget.getScene().convertSceneToView(
127: widget.convertLocalToScene(widget.getBounds()));
128:
129: Point center = GeomUtil.center(rectangle);
130: Dimension size = editor.getMinimumSize();
131: if (rectangle.width > size.width)
132: size.width = rectangle.width;
133: if (rectangle.height > size.height)
134: size.height = rectangle.height;
135: int x = center.x - size.width / 2;
136: int y = center.y - size.height / 2;
137:
138: rectangle = new Rectangle(x, y, size.width, size.height);
139: updateRectangleToFitToView(rectangle);
140:
141: Rectangle r = provider.getInitialEditorComponentBounds(this ,
142: widget, editor, rectangle);
143: this .rectangle = r != null ? r : rectangle;
144:
145: editor.setBounds(x, y, size.width, size.height);
146: notifyEditorComponentBoundsChanged();
147: editor.requestFocusInWindow();
148:
149: return true;
150: }
151:
152: private void updateRectangleToFitToView(Rectangle rectangle) {
153: JComponent component = widget.getScene().getView();
154: if (rectangle.x + rectangle.width > component.getWidth())
155: rectangle.x = component.getWidth() - rectangle.width;
156: if (rectangle.y + rectangle.height > component.getHeight())
157: rectangle.y = component.getHeight() - rectangle.height;
158: if (rectangle.x < 0)
159: rectangle.x = 0;
160: if (rectangle.y < 0)
161: rectangle.y = 0;
162: }
163:
164: public final void closeEditor(boolean commit) {
165: if (editor == null)
166: return;
167: Container parent = editor.getParent();
168: Rectangle bounds = parent != null ? editor.getBounds() : null;
169: provider.notifyClosing(this , widget, editor, commit);
170: boolean hasFocus = editor.hasFocus();
171: if (bounds != null) {
172: parent.remove(editor);
173: parent.repaint(bounds.x, bounds.y, bounds.width,
174: bounds.height);
175: }
176: editor = null;
177: widget = null;
178: rectangle = null;
179: if (hasFocus)
180: if (parent != null)
181: parent.requestFocusInWindow();
182: }
183:
184: public void notifyEditorComponentBoundsChanged() {
185: EnumSet<InplaceEditorProvider.ExpansionDirection> directions = provider
186: .getExpansionDirections(this , widget, editor);
187: if (directions == null)
188: directions = EnumSet
189: .noneOf(InplaceEditorProvider.ExpansionDirection.class);
190: Rectangle rectangle = this .rectangle;
191: Dimension size = editor.getPreferredSize();
192: Dimension minimumSize = editor.getMinimumSize();
193: if (minimumSize != null) {
194: if (size.width < minimumSize.width)
195: size.width = minimumSize.width;
196: if (size.height < minimumSize.height)
197: size.height = minimumSize.height;
198: }
199:
200: int heightDiff = rectangle.height - size.height;
201: int widthDiff = rectangle.width - size.width;
202:
203: boolean top = directions
204: .contains(InplaceEditorProvider.ExpansionDirection.TOP);
205: boolean bottom = directions
206: .contains(InplaceEditorProvider.ExpansionDirection.BOTTOM);
207:
208: if (top) {
209: if (bottom) {
210: rectangle.y += heightDiff / 2;
211: rectangle.height = size.height;
212: } else {
213: rectangle.y += heightDiff;
214: rectangle.height = size.height;
215: }
216: } else {
217: if (bottom) {
218: rectangle.height = size.height;
219: } else {
220: }
221: }
222:
223: boolean left = directions
224: .contains(InplaceEditorProvider.ExpansionDirection.LEFT);
225: boolean right = directions
226: .contains(InplaceEditorProvider.ExpansionDirection.RIGHT);
227:
228: if (left) {
229: if (right) {
230: rectangle.x += widthDiff / 2;
231: rectangle.width = size.width;
232: } else {
233: rectangle.x += widthDiff;
234: rectangle.width = size.width;
235: }
236: } else {
237: if (right) {
238: rectangle.width = size.width;
239: } else {
240: }
241: }
242:
243: updateRectangleToFitToView(rectangle);
244:
245: editor.setBounds(rectangle);
246: editor.repaint();
247: }
248:
249: }
|