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.flow;
042:
043: import org.netbeans.modules.vmd.api.flow.FlowPresenter;
044: import org.netbeans.modules.vmd.api.flow.visual.FlowScene;
045: import org.netbeans.modules.vmd.api.flow.visual.FlowDescriptor;
046: import org.netbeans.modules.vmd.api.model.*;
047:
048: import javax.swing.*;
049: import java.util.Collection;
050: import java.util.HashSet;
051:
052: /**
053: * @author David Kaspar
054: */
055: // TODO - incremental selection update in notifyEventFired method
056: // TODO - model->flow selection bug - ListCD, ListElementCD flow presenters vs. the descriptors (ListCD.elements belongs to each element - not to the list)
057: public class FlowAccessController implements AccessController {
058:
059: private DesignDocument document;
060: private FlowScene scene;
061:
062: private HashSet<FlowPresenter> dirtyPresenters = new HashSet<FlowPresenter>();
063:
064: private volatile long eventID = 0;
065:
066: public FlowAccessController(DesignDocument document) {
067: this .document = document;
068: scene = new FlowScene(document);
069: }
070:
071: public void writeAccess(Runnable runnable) {
072: runnable.run();
073: }
074:
075: public void notifyEventFiring(DesignEvent event) {
076: }
077:
078: public void notifyEventFired(final DesignEvent event) {
079: if (eventID < event.getEventID())
080: eventID = event.getEventID();
081: SwingUtilities.invokeLater(new Runnable() {
082: public void run() {
083: document.getTransactionManager().readAccess(
084: new Runnable() {
085: public void run() {
086: scene
087: .setCurrentEventIDForPreferredNodeLocationProcessing(eventID);
088: resolveDirty();
089: scene
090: .setCurrentEventIDForPreferredNodeLocationProcessing(null);
091:
092: if (!FlowViewController.FLOW_ID
093: .equals(document
094: .getSelectionSourceID())) {
095: HashSet<FlowDescriptor> objects = new HashSet<FlowDescriptor>();
096: for (DesignComponent component : document
097: .getSelectedComponents()) {
098: for (FlowPresenter presenter : component
099: .getPresenters(FlowPresenter.class))
100: objects
101: .addAll(presenter
102: .getFlowDescriptors());
103: }
104: scene.setSelectedObjects(objects);
105: }
106: scene.validate();
107: }
108: });
109: }
110: });
111: }
112:
113: public void notifyComponentsCreated(
114: Collection<DesignComponent> createdComponents) {
115: }
116:
117: public FlowScene getScene() {
118: return scene;
119: }
120:
121: public JComponent getCreateView() {
122: JComponent view = scene.getView();
123: if (view == null)
124: view = scene.createView();
125: return view;
126: }
127:
128: public JComponent createSatelliteView() {
129: return scene.createSatelliteView();
130: }
131:
132: public void addDirtyPresenter(FlowPresenter presenter) {
133: dirtyPresenters.add(presenter);
134: }
135:
136: private void resolveDirty() {
137: for (FlowPresenter presenter : dirtyPresenters)
138: presenter.updateDescriptors();
139: for (FlowPresenter presenter : dirtyPresenters)
140: presenter.resolveRemoveBadge();
141: for (FlowPresenter presenter : dirtyPresenters)
142: presenter.resolveRemoveEdge();
143: for (FlowPresenter presenter : dirtyPresenters)
144: presenter.resolveRemovePin();
145: for (FlowPresenter presenter : dirtyPresenters)
146: presenter.resolveRemoveNode();
147: for (FlowPresenter presenter : dirtyPresenters)
148: presenter.resolveAddNode();
149: for (FlowPresenter presenter : dirtyPresenters)
150: presenter.resolveAddPin();
151: for (FlowPresenter presenter : dirtyPresenters)
152: presenter.resolveAddEdge();
153: for (FlowPresenter presenter : dirtyPresenters)
154: presenter.resolveAddBadge();
155: for (FlowPresenter presenter : dirtyPresenters)
156: presenter.resolveUpdate();
157: dirtyPresenters.clear();
158: scene.resolveOrderInNodeDescriptors();
159: }
160:
161: public static class Factory implements AccessControllerFactory {
162:
163: public AccessController createAccessController(
164: DesignDocument document) {
165: return new FlowAccessController(document);
166: }
167: }
168:
169: }
|