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.widget.Widget;
044: import org.netbeans.api.visual.action.WidgetAction;
045: import org.netbeans.api.visual.action.SelectProvider;
046:
047: import java.awt.event.MouseEvent;
048: import java.awt.event.KeyEvent;
049: import java.awt.*;
050:
051: /**
052: * @author David Kaspar
053: */
054: public final class SelectAction extends WidgetAction.LockedAdapter {
055:
056: private boolean aiming = false;
057: private Widget aimedWidget = null;
058: private boolean invertSelection;
059: private SelectProvider provider;
060:
061: public SelectAction(SelectProvider provider) {
062: this .provider = provider;
063: }
064:
065: protected boolean isLocked() {
066: return aiming;
067: }
068:
069: public State mousePressed(Widget widget, WidgetMouseEvent event) {
070: if (isLocked())
071: return State.createLocked(widget, this );
072: if (event.getButton() == MouseEvent.BUTTON1
073: || event.getButton() == MouseEvent.BUTTON2) {
074: invertSelection = (event.getModifiersEx() & MouseEvent.CTRL_DOWN_MASK) != 0;
075: Point localLocation = event.getPoint();
076: if (provider.isSelectionAllowed(widget, localLocation,
077: invertSelection)) {
078: aiming = provider.isAimingAllowed(widget,
079: localLocation, invertSelection);
080: if (aiming) {
081: updateState(widget, localLocation);
082: return State.createLocked(widget, this );
083: } else {
084: provider.select(widget, localLocation,
085: invertSelection);
086: return State.CHAIN_ONLY;
087: }
088: }
089: }
090: return State.REJECTED;
091: }
092:
093: public State mouseReleased(Widget widget, WidgetMouseEvent event) {
094: if (aiming) {
095: Point point = event.getPoint();
096: updateState(widget, point);
097: if (aimedWidget != null)
098: provider.select(widget, point, invertSelection);
099: updateState(null, null);
100: aiming = false;
101: return State.CONSUMED;
102: }
103: return super .mouseReleased(widget, event);
104: }
105:
106: private void updateState(Widget widget, Point localLocation) {
107: if (widget != null && !widget.isHitAt(localLocation))
108: widget = null;
109: if (widget == aimedWidget)
110: return;
111: if (aimedWidget != null)
112: aimedWidget.setState(aimedWidget.getState()
113: .deriveWidgetAimed(false));
114: aimedWidget = widget;
115: if (aimedWidget != null)
116: aimedWidget.setState(aimedWidget.getState()
117: .deriveWidgetAimed(true));
118: }
119:
120: public State keyTyped(Widget widget, WidgetKeyEvent event) {
121: if (!aiming && event.getKeyChar() == KeyEvent.VK_SPACE) {
122: provider
123: .select(
124: widget,
125: null,
126: (event.getModifiersEx() & MouseEvent.CTRL_DOWN_MASK) != 0);
127: return State.CONSUMED;
128: }
129: return State.REJECTED;
130: }
131:
132: }
|