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.navigator;
020:
021: import java.awt.Component;
022: import javax.swing.JComboBox;
023: import javax.swing.SwingUtilities;
024: import org.netbeans.core.api.multiview.MultiViewHandler;
025: import org.netbeans.core.api.multiview.MultiViewPerspective;
026: import org.netbeans.core.api.multiview.MultiViews;
027: import org.netbeans.modules.bpel.editors.multiview.DesignerMultiViewElementDesc;
028: import org.openide.windows.TopComponent;
029: import org.openide.windows.WindowManager;
030:
031: /**
032: *
033: * @author Vitaly Bychkov
034: * @version 1.0
035: *
036: * it's utility class to controll bpelNavigator
037: *
038: */
039: public class BpelNavigatorController {
040: public static final String BPEL_SOURCE_MVPID = "bpelsource"; // NOI18N
041: public static final String BPEL_DESIGNER_MVPID = DesignerMultiViewElementDesc.PREFERRED_ID;
042:
043: private BpelNavigatorController() {
044: }
045:
046: public static TopComponent getNavigatorTC() {
047: return WindowManager.getDefault().findTopComponent(
048: "navigatorTC"); // NOI18N
049: }
050:
051: /**
052: * get current active editor panel in case it's bpel multyview editor then
053: * switch accordingly bpelNavigator.
054: */
055: public static void switchNavigatorPanel() {
056: String mvpId = getMVEditorActivePanelPrefferedId();
057: if (mvpId == null) {
058: return;
059: }
060:
061: if (mvpId.equals(BPEL_DESIGNER_MVPID)) {
062: switchNavigatorPanel(true);
063: }
064: /* next use case should be supported
065: * editor switches from diagramm to source => navigator doesn't switch state
066: else if (mvpId.equals(BPEL_SOURCE_MVPID)) {
067: switchNavigatorPanel(false);
068: }*/
069: }
070:
071: public static void switchNavigatorPanel(final boolean isLogicalPanel) {
072: SwingUtilities.invokeLater(new Runnable() {
073: public void run() {
074: TopComponent navigatorTC = getNavigatorTC();
075: if (navigatorTC == null) {
076: return;
077: }
078:
079: JComboBox navComboBox = getNavigatorComboBox(navigatorTC);
080: if (navComboBox == null) {
081: return;
082: }
083:
084: Object selectedNavPanel = navComboBox.getSelectedItem();
085: if ((isLogicalPanel && !(BpelNavigatorPanel.getUName()
086: .equals(selectedNavPanel)))
087: || (!isLogicalPanel && BpelNavigatorPanel
088: .getUName().equals(selectedNavPanel))) {
089: int numPanels = navComboBox.getItemCount();
090: if (numPanels > 1) {
091: navComboBox.setSelectedIndex(navComboBox
092: .getSelectedIndex() == 0 ? 1 : 0);
093: }
094: }
095: }
096: });
097:
098: }
099:
100: private static JComboBox getNavigatorComboBox(
101: TopComponent navigatorTC) {
102: assert navigatorTC != null;
103: JComboBox comboBox = null;
104: Component[] components = navigatorTC.getComponents();
105: for (Component elem : components) {
106: if (elem instanceof JComboBox) {
107: comboBox = (JComboBox) elem;
108: break;
109: }
110: }
111: return comboBox;
112: }
113:
114: private static String getMVEditorActivePanelPrefferedId() {
115: TopComponent activeTC = WindowManager.getDefault()
116: .getRegistry().getActivated();
117: MultiViewHandler mvh = MultiViews
118: .findMultiViewHandler(activeTC);
119: if (mvh == null) {
120: return null;
121: }
122:
123: MultiViewPerspective mvp = mvh.getSelectedPerspective();
124: if (mvp != null) {
125: return mvp.preferredID();
126: }
127:
128: return null;
129: }
130: }
|