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.xslt.core.multiview;
021:
022: import java.util.ArrayList;
023: import java.util.List;
024: import java.util.Set;
025:
026: import org.netbeans.core.api.multiview.MultiViewHandler;
027: import org.netbeans.core.api.multiview.MultiViewPerspective;
028: import org.netbeans.core.api.multiview.MultiViews;
029: import org.netbeans.core.spi.multiview.MultiViewDescription;
030: import org.netbeans.core.spi.multiview.MultiViewFactory;
031: import org.netbeans.modules.xslt.core.XSLTDataEditorSupport;
032: import org.netbeans.modules.xslt.core.XSLTDataObject;
033: import org.netbeans.modules.xslt.core.multiview.source.XSLTSourceMultiViewElementDesc;
034: import org.netbeans.modules.xslt.core.multiview.mapper.MapperMultiViewElementDesc;
035: import org.openide.loaders.DataObject;
036: import org.openide.windows.CloneableTopComponent;
037: import org.openide.windows.TopComponent;
038:
039: /**
040: *
041: * @author Vitaly Bychkov
042: * @version 1.0
043: */
044: public class XsltMultiViewSupport {
045: private final static XsltMultiViewSupport INSTANCE = new XsltMultiViewSupport();
046:
047: private XsltMultiViewSupport() {
048: }
049:
050: public static final XsltMultiViewSupport getInstance() {
051: return INSTANCE;
052: }
053:
054: public static CloneableTopComponent createMultiView(
055: final XSLTDataObject dataObject) {
056: MultiViewDescription views[] = {
057: new XSLTSourceMultiViewElementDesc(dataObject),
058: new MapperMultiViewElementDesc(dataObject) };
059:
060: CloneableTopComponent multiview = MultiViewFactory
061: .createCloneableMultiView(views,
062: // source view invokes cloneableEditorSupport initialization (add listeners ...)
063: views[0],
064: new XSLTDataEditorSupport.CloseHandler(
065: dataObject));
066: String name = dataObject.getNodeDelegate().getDisplayName();
067: multiview.setDisplayName(name);
068: multiview.setName(name);
069: return multiview;
070: }
071:
072: public void requestViewOpen(XSLTDataEditorSupport support) {
073:
074: List<TopComponent> associatedTCs = new ArrayList<TopComponent>();
075: DataObject targetDO = support.getDataObject();
076: TopComponent activeTC = TopComponent.getRegistry()
077: .getActivated();
078: if (targetDO == (DataObject) activeTC.getLookup().lookup(
079: DataObject.class)) {
080: associatedTCs.add(activeTC);
081: }
082: Set openTCs = TopComponent.getRegistry().getOpened();
083: for (Object tc : openTCs) {
084: TopComponent topComponent = (TopComponent) tc;
085: if (targetDO == (DataObject) topComponent.getLookup()
086: .lookup(DataObject.class)) {
087: associatedTCs.add(topComponent);
088: }
089: }
090:
091: // Use the first TC in the list that has the desired perspective
092: boolean found = false;
093: for (TopComponent targetTC : associatedTCs) {
094: MultiViewHandler handler = MultiViews
095: .findMultiViewHandler(targetTC);
096: if (handler == null) {
097: continue;
098: }
099: MultiViewPerspective[] p = handler.getPerspectives();
100: for (MultiViewPerspective mvp : p) {
101: if (!mvp.preferredID().equals(
102: XSLTSourceMultiViewElementDesc.PREFERED_ID)) {
103: handler.requestActive(mvp);
104: found = true;
105: break;
106: }
107: }
108: if (found) {
109: break;
110: }
111: }
112:
113: }
114: }
|