001: /*
002: * The contents of this file are subject to the terms of the Common Development
003: * and Distribution License (the License). You may not use this file except in
004: * compliance with the License.
005: *
006: * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
007: * or http://www.netbeans.org/cddl.txt.
008: *
009: * When distributing Covered Code, include this CDDL Header Notice in each file
010: * and include the License file at http://www.netbeans.org/cddl.txt.
011: * If applicable, add the following below the CDDL Header, with the fields
012: * enclosed by brackets [] replaced by your own identifying information:
013: * "Portions Copyrighted [year] [name of copyright owner]"
014: *
015: * The Original Software is NetBeans. The Initial Developer of the Original
016: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
017: * Microsystems, Inc. All Rights Reserved.
018: */
019: package org.netbeans.modules.bpel.design.decoration;
020:
021: import java.awt.Component;
022: import java.util.ArrayList;
023: import java.util.Collection;
024: import java.util.HashMap;
025: import java.util.HashSet;
026: import java.util.List;
027: import java.util.Set;
028: import javax.swing.SwingUtilities;
029: import org.netbeans.modules.bpel.design.DesignView;
030: import org.netbeans.modules.bpel.design.decoration.components.DecorationComponent;
031: import org.netbeans.modules.bpel.design.decoration.components.ZoomableDecorationComponent;
032: import org.netbeans.modules.bpel.design.model.patterns.CompositePattern;
033: import org.netbeans.modules.bpel.design.model.patterns.Pattern;
034: import org.netbeans.modules.bpel.model.api.BpelEntity;
035: import org.netbeans.modules.xml.xam.Model;
036:
037: public class DecorationManager {
038:
039: private final Object cacheKey = new Object();
040: public static final Decoration emptyDecoration = new Decoration();
041: private ArrayList<DecorationProvider> providersList = new ArrayList<DecorationProvider>();
042: private DesignView designView;
043:
044: private DecorationUpdater decorationUpdater = new DecorationUpdater();
045:
046: public DecorationManager(DesignView designView) {
047: this .designView = designView;
048: }
049:
050: /**
051: * Calculates the superset of states from all providers
052: **/
053: public Decoration getDecoration(Pattern p) {
054: BpelEntity entity = p.getOMReference();
055:
056: Decoration result = null;
057:
058: if (entity != null) {
059: result = (Decoration) entity.getCookie(cacheKey);
060: }
061:
062: return (result != null) ? result : emptyDecoration;
063: }
064:
065: public List<DecorationProvider> getProviders() {
066: return providersList;
067: }
068:
069: public void attachProvider(DecorationProvider provider) {
070: providersList.add(provider);
071: /*
072: * trigger re-calculation of all decorations to add
073: * of decorations reported by new provider
074: */
075: }
076:
077: /**
078: * Notify all providers, that view is about to close.
079: * Providers can override this method to release resouces, unsubscribe from listeners, etc
080: **/
081: public void release() {
082: for (DecorationProvider p : providersList) {
083: p.release();
084: }
085: }
086:
087: /**
088: * listenenr method to be called by providers when they need to inform us about
089: * changes in element decorations.
090: * @entity - entity to update decorations or null, to update decorations for all entitites
091: **/
092: public void decorationChanged() {
093: decorationUpdater.start();
094: }
095:
096: private void updateResult(BpelEntity entity) {
097: Decoration newDecoration = new Decoration();
098:
099: for (DecorationProvider provider : providersList) {
100: Decoration d = provider.getDecoration(entity);
101: if (d != null && d != emptyDecoration) {
102: newDecoration.combineWith(d);
103: }
104: }
105: entity.setCookie(cacheKey, newDecoration);
106:
107: for (BpelEntity e : entity.getChildren()) {
108: updateResult(e);
109: }
110: }
111:
112: private class DecorationUpdater implements Runnable {
113: private boolean activated = false;
114:
115: public void start() {
116: activated = true;
117: SwingUtilities.invokeLater(this );
118: }
119:
120: public void run() {
121: if (!activated) {
122: return;
123: }
124:
125: if (designView.getModel() == null) {
126: return;
127: }
128: if (designView.getBPELModel().getState() != Model.State.VALID) {
129: return;
130: }
131:
132: //re-query all providers to get updated decorations
133: updateResult(designView.getBPELModel().getProcess());
134:
135: //add/remove decoration components to awt container
136: new ComponentDecorationsUpdater(designView).update();
137: //repaint view
138: designView.revalidate();
139: designView.repaint();
140: designView.getRightStripe().repaint();
141: activated = false;
142: }
143: };
144: }
|