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.tmap.navigator;
021:
022: import java.util.Collection;
023: import java.util.concurrent.atomic.AtomicReference;
024: import javax.swing.JComponent;
025: import org.netbeans.modules.xslt.tmap.TMapDataObject;
026: import org.netbeans.modules.xslt.tmap.model.api.TMapModel;
027: import org.netbeans.spi.navigator.NavigatorPanel;
028: import org.netbeans.spi.navigator.NavigatorPanelWithUndo;
029: import org.openide.awt.UndoRedo;
030: import org.openide.loaders.DataObject;
031: import org.openide.util.Lookup;
032: import org.openide.util.LookupEvent;
033: import org.openide.util.LookupListener;
034: import org.openide.util.NbBundle;
035:
036: /**
037: *
038: * @author Vitaly Bychkov
039: * @version 1.0
040: */
041: public class TMapLogicalNavigatorPanel implements NavigatorPanel/*WithUndo*/{
042:
043: private AtomicReference<UndoRedo.Manager> myUndoRedoRef = new AtomicReference<UndoRedo.Manager>();
044:
045: /** holds UI of this panel. */
046: private JComponent myComponent;
047: /** current context to work on. */
048: private Lookup.Result<DataObject> myContext;
049:
050: private TMapModel myModel;
051:
052: private static String NAV_PANEL_NAME = NbBundle.getMessage(
053: TMapLogicalNavigatorPanel.class, "LBL_TMAP_LOGICAL_VIEW"); // NOI18N
054:
055: public static String getUName() {
056: return NAV_PANEL_NAME;
057: }
058:
059: private final LookupListener mySelectionListener = new LookupListener() {
060: public void resultChanged(LookupEvent ev) {
061: setNewContent();
062: }
063: };
064:
065: public TMapLogicalNavigatorPanel() {
066: }
067:
068: /** {@inheritDoc} */
069: public String getDisplayName() {
070: return NAV_PANEL_NAME;
071: }
072:
073: /** {@inheritDoc} */
074: public String getDisplayHint() {
075: return NbBundle.getMessage(TMapLogicalNavigatorPanel.class,
076: "LBL_TMAP_LOGICAL_VIEW_TOOLTIP"); // NOI18N
077: }
078:
079: /** {@inheritDoc} */
080: public JComponent getComponent() {
081: if (myComponent == null) {
082: myComponent = new TMapLogicalPanel();
083: }
084: return myComponent;
085: }
086:
087: /** {@inheritDoc} */
088: public void panelActivated(Lookup context) {
089: myContext = context
090: .lookup(new Lookup.Template(DataObject.class));
091: assert myContext != null;
092: DataObject curDataObject = getTMapDataObject();
093: myContext.addLookupListener(mySelectionListener);
094: mySelectionListener.resultChanged(null);
095: }
096:
097: /** {@inheritDoc} */
098: public void panelDeactivated() {
099: if (myContext != null) {
100: myContext.removeLookupListener(mySelectionListener);
101: myContext = null;
102: }
103: }
104:
105: /** {@inheritDoc} */
106: public Lookup getLookup() {
107: return null;
108: }
109:
110: /** {@inheritDoc} */
111: // public UndoRedo getUndoRedo() {
112: // if (myUndoRedoRef.get() == null) {
113: // myUndoRedoRef.compareAndSet(null, createUndoRedo());
114: // }
115: // return myUndoRedoRef.get();
116: // }
117: //
118: // private UndoRedo.Manager createUndoRedo() {
119: // UndoRedo.Manager undoRedo = null;
120: // TMapDataObject dObj = getTMapDataObject();
121: // if (dObj != null) {
122: // undoRedo = dObj.getEditorSupport().getUndoManager();
123: // }
124: //
125: // return undoRedo;
126: // }
127: private TMapDataObject getTMapDataObject() {
128: TMapDataObject dObj = null;
129: Collection<? extends DataObject> dObjs = myContext
130: .allInstances();
131: if (dObjs != null && dObjs.size() == 1) {
132: DataObject tmpDObj = dObjs.iterator().next();
133: if (tmpDObj.getClass() == TMapDataObject.class) {
134: dObj = (TMapDataObject) tmpDObj;
135: }
136: }
137:
138: return dObj;
139: }
140:
141: private TMapModel getModel(DataObject dOBj) {
142: assert dOBj != null;
143: Lookup lookup = dOBj.getLookup();
144: return lookup != null ? lookup.lookup(TMapModel.class) : null;
145: }
146:
147: private void setNewContent() {
148: final DataObject curDataObject = getTMapDataObject();
149:
150: TMapModel tMapModel = null;
151: if (curDataObject != null) {
152: tMapModel = getModel(curDataObject);
153: }
154:
155: if (tMapModel != null && myModel != tMapModel) {
156: myModel = tMapModel;
157: ((TMapLogicalPanel) getComponent()).navigate(curDataObject
158: .getLookup(), myModel);
159:
160: }
161:
162: }
163:
164: }
|