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-2006 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.vmd.structure.document;
042:
043: import org.netbeans.api.visual.action.ActionFactory;
044: import org.netbeans.api.visual.action.SelectProvider;
045: import org.netbeans.api.visual.action.TwoStateHoverProvider;
046: import org.netbeans.api.visual.action.WidgetAction;
047: import org.netbeans.api.visual.model.ObjectScene;
048: import org.netbeans.api.visual.widget.LabelWidget;
049: import org.netbeans.api.visual.widget.Widget;
050: import org.netbeans.modules.vmd.api.model.DesignComponent;
051: import org.netbeans.modules.vmd.api.model.DesignDocument;
052: import org.openide.util.NbBundle;
053:
054: import java.awt.*;
055: import java.util.ArrayList;
056: import java.util.Collections;
057:
058: /**
059: * @author David Kaspar
060: */
061: public class DocumentScene extends ObjectScene {
062:
063: static final Color COLOR_HOVERED = Color.CYAN;
064: static final Color COLOR_SELECTED = Color.YELLOW;
065:
066: private LabelWidget labelWidget;
067: private WidgetAction hoverAction = ActionFactory
068: .createHoverAction(new DocumentHoverProvider());
069: private WidgetAction selectAction = ActionFactory
070: .createSelectAction(new DocumentSelectProvider());
071:
072: private DesignDocument document;
073:
074: public DocumentScene() {
075: setOpaque(false);
076: getActions().addAction(ActionFactory.createZoomAction());
077: getActions().addAction(ActionFactory.createPanAction());
078: getActions().addAction(hoverAction);
079:
080: labelWidget = new LabelWidget(this , NbBundle.getMessage(
081: DocumentScene.class, "MSG_Loading")); // NOI18N
082: labelWidget.setFont(getDefaultFont().deriveFont(Font.BOLD, 24));
083: // labelWidget.setForeground (Color.DARK_GRAY);
084: }
085:
086: public void clear() {
087: ArrayList<Object> objects = new ArrayList<Object>(getObjects());
088: for (Object object : objects) {
089: findWidget(object).removeFromParent();
090: removeObject(object);
091: }
092: if (getChildren().contains(labelWidget))
093: removeChild(labelWidget);
094: }
095:
096: public void setRootNode(DesignDocument document,
097: ComponentWidget widget) {
098: this .document = document;
099: addChild(widget);
100: addObject(document, widget);
101: }
102:
103: public void setupLoadingDocument() {
104: clear();
105: if (getChildren().contains(labelWidget))
106: return;
107: addChild(labelWidget);
108: validate();
109: }
110:
111: public WidgetAction getHoverAction() {
112: return hoverAction;
113: }
114:
115: public WidgetAction getSelectAction() {
116: return selectAction;
117: }
118:
119: private class DocumentHoverProvider implements
120: TwoStateHoverProvider {
121:
122: private Paint background;
123:
124: public void unsetHovering(Widget widget) {
125: widget.setBackground(background);
126: }
127:
128: public void setHovering(Widget widget) {
129: background = widget.getBackground();
130: widget.setBackground(COLOR_HOVERED);
131: }
132: }
133:
134: private class DocumentSelectProvider implements SelectProvider {
135:
136: public boolean isAimingAllowed(Widget widget, Point point,
137: boolean b) {
138: return false;
139: }
140:
141: public boolean isSelectionAllowed(Widget widget, Point point,
142: boolean b) {
143: return true;
144: }
145:
146: public void select(Widget widget, Point localLocation,
147: boolean invertSelection) {
148: Object object = findObject(widget);
149: final DesignComponent component = object != null ? ((ComponentWidget) findWidget(object))
150: .getComponent()
151: : null;
152: final DesignDocument document = DocumentScene.this .document;
153: if (document != null)
154: document.getTransactionManager().writeAccess(
155: new Runnable() {
156: public void run() {
157: if (component != null)
158: document
159: .setSelectedComponents(
160: DocumentEditorView.DOCUMENT_ID,
161: Collections
162: .singleton(component));
163: else {
164: document
165: .setSelectedComponents(
166: DocumentEditorView.DOCUMENT_ID,
167: Collections
168: .<DesignComponent> emptySet());
169: }
170: }
171: });
172: }
173:
174: }
175:
176: }
|