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:
020: package org.netbeans.modules.bpel.core.multiview;
021:
022: import java.util.ArrayList;
023: import java.util.Collection;
024: import java.util.Collections;
025: import java.util.Comparator;
026: import java.util.List;
027: import java.util.Set;
028:
029: import org.netbeans.core.api.multiview.MultiViewHandler;
030: import org.netbeans.core.api.multiview.MultiViewPerspective;
031: import org.netbeans.core.api.multiview.MultiViews;
032: import org.netbeans.core.spi.multiview.MultiViewDescription;
033: import org.netbeans.core.spi.multiview.MultiViewFactory;
034: import org.netbeans.modules.bpel.core.BPELDataEditorSupport;
035: import org.netbeans.modules.bpel.core.BPELDataObject;
036: import org.netbeans.modules.bpel.core.multiview.spi.BpelMultiviewProvider;
037: import org.openide.loaders.DataObject;
038: import org.openide.util.Lookup;
039: import org.openide.util.Lookup.Result;
040: import org.openide.windows.CloneableTopComponent;
041: import org.openide.windows.TopComponent;
042:
043: /**
044: * @author ads
045: *
046: */
047: public final class BpelMultiViewSupport {
048:
049: // class should not be inst-ed
050: private BpelMultiViewSupport() {
051: super ();
052: }
053:
054: public static CloneableTopComponent createMultiView(
055: final BPELDataObject dataObject) {
056: Collection coll = getMultiviewProviders();
057: MultiViewDescription views[] = new MultiViewDescription[coll
058: .size() + 1];
059:
060: // Put the source element first so that client code can find its
061: // CloneableEditorSupport.Pane implementation.
062: views[0] = new BPELSourceMultiViewElementDesc(dataObject);
063: int i = 0;
064: for (Object provider : coll) {
065: views[++i] = ((BpelMultiviewProvider) provider)
066: .createMultiview(dataObject);
067: }
068:
069: CloneableTopComponent multiview = MultiViewFactory
070: .createCloneableMultiView(views, views[0],
071: new BPELDataEditorSupport.CloseHandler(
072: dataObject));
073: String name = dataObject.getNodeDelegate().getDisplayName();
074: multiview.setDisplayName(name);
075: multiview.setName(name);
076: return multiview;
077: }
078:
079: public static final BpelMultiViewSupport getInstance() {
080: return INSTANCE;
081: }
082:
083: public void requestViewOpen(BPELDataEditorSupport support) {
084:
085: List<TopComponent> associatedTCs = new ArrayList<TopComponent>();
086: DataObject targetDO = support.getDataObject();
087: TopComponent activeTC = TopComponent.getRegistry()
088: .getActivated();
089: if (targetDO == (DataObject) activeTC.getLookup().lookup(
090: DataObject.class)) {
091: associatedTCs.add(activeTC);
092: }
093: Set openTCs = TopComponent.getRegistry().getOpened();
094: for (Object tc : openTCs) {
095: TopComponent topComponent = (TopComponent) tc;
096: if (targetDO == (DataObject) topComponent.getLookup()
097: .lookup(DataObject.class)) {
098: associatedTCs.add(topComponent);
099: }
100: }
101:
102: // Use the first TC in the list that has the desired perspective
103: boolean found = false;
104: for (TopComponent targetTC : associatedTCs) {
105: MultiViewHandler handler = MultiViews
106: .findMultiViewHandler(targetTC);
107: if (handler == null) {
108: continue;
109: }
110: MultiViewPerspective[] p = handler.getPerspectives();
111: for (MultiViewPerspective mvp : p) {
112: if (!mvp.preferredID().equals(
113: BPELSourceMultiViewElementDesc.PREFERED_ID)) {
114: handler.requestActive(mvp);
115: found = true;
116: break;
117: }
118: }
119: if (found) {
120: break;
121: }
122: }
123:
124: }
125:
126: private static Collection getMultiviewProviders() {
127: Result<BpelMultiviewProvider> result = Lookup
128: .getDefault()
129: .lookup(
130: new Lookup.Template(BpelMultiviewProvider.class));
131: Collection<? extends BpelMultiviewProvider> coll = result
132: .allInstances();
133: assert coll != null;
134: List<? extends BpelMultiviewProvider> bpelMvProvs = new ArrayList<BpelMultiviewProvider>(
135: coll);
136:
137: Collections.sort(bpelMvProvs,
138: new Comparator<BpelMultiviewProvider>() {
139:
140: public int compare(BpelMultiviewProvider o1,
141: BpelMultiviewProvider o2) {
142: int o1Weight = o1 == null ? -1 : o1
143: .getSortWeight();
144: int o2Weight = o2 == null ? -1 : o2
145: .getSortWeight();
146:
147: return o1Weight < o2Weight ? -1
148: : o1Weight == o2Weight ? 0 : 1;
149: }
150:
151: });
152: return bpelMvProvs;
153: }
154:
155: private final static BpelMultiViewSupport INSTANCE = new BpelMultiViewSupport();
156:
157: }
|