001: /*
002: * Sun Public License Notice
003: *
004: * The contents of this file are subject to the Sun Public License
005: * Version 1.0 (the "License"). You may not use this file except in
006: * compliance with the License. A copy of the License is available at
007: * http://www.sun.com/
008: *
009: * The Original Code is NetBeans. The Initial Developer of the Original
010: * Code is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
011: * Microsystems, Inc. All Rights Reserved.
012: If you wish your version of this file to be governed by only the CDDL
013: or only the GPL Version 2, indicate your decision by adding
014: "[Contributor] elects to include this software in this distribution
015: under the [CDDL or GPL Version 2] license." If you do not indicate a
016: single choice of license, a recipient has the option to distribute
017: your version of this file under either the CDDL, the GPL Version 2 or
018: to extend the choice of license to its licensees as provided above.
019: However, if you add GPL Version 2 code and therefore, elected the GPL
020: Version 2 license, then the option applies only if the new code is
021: made subject to such option by the copyright holder.
022:
023: If you wish your version of this file to be governed by only the CDDL
024: or only the GPL Version 2, indicate your decision by adding
025: "[Contributor] elects to include this software in this distribution
026: under the [CDDL or GPL Version 2] license." If you do not indicate a
027: single choice of license, a recipient has the option to distribute
028: your version of this file under either the CDDL, the GPL Version 2 or
029: to extend the choice of license to its licensees as provided above.
030: However, if you add GPL Version 2 code and therefore, elected the GPL
031: Version 2 license, then the option applies only if the new code is
032: made subject to such option by the copyright holder.
033: */
034:
035: /*
036: * ETLSourceMultiViewElement.java
037: *
038: * Created on October 13, 2005, 2:36 PM
039: *
040: * To change this template, choose Tools | Template Manager
041: * and open the template in the editor.
042: */
043:
044: package org.netbeans.modules.etl.ui;
045:
046: import java.io.IOException;
047: import java.io.ObjectInput;
048: import java.io.ObjectOutput;
049:
050: import java.util.ArrayList;
051: import javax.swing.Action;
052: import javax.swing.JComponent;
053: import javax.swing.JSeparator;
054: import javax.swing.text.Document;
055:
056: import org.netbeans.core.spi.multiview.CloseOperationState;
057: import org.netbeans.core.spi.multiview.MultiViewElement;
058: import org.netbeans.core.spi.multiview.MultiViewElementCallback;
059: import org.netbeans.core.spi.multiview.MultiViewFactory;
060: import org.openide.actions.FileSystemAction;
061: import org.openide.awt.UndoRedo;
062: import org.openide.cookies.SaveCookie;
063: import org.openide.nodes.Node;
064: import org.openide.text.CloneableEditor;
065: import org.openide.text.NbDocument;
066: import org.openide.util.Lookup;
067: import org.openide.util.lookup.Lookups;
068: import org.openide.util.lookup.ProxyLookup;
069:
070: /**
071: *
072: * @author Jeri Lockhart
073: */
074:
075: public class ETLSourceMultiViewElement extends CloneableEditor
076: implements MultiViewElement {
077:
078: static final long serialVersionUID = 4403502726950453345L;
079:
080: transient private JComponent toolbar;
081: transient private MultiViewElementCallback multiViewObserver;
082: private ETLDataObject etlDataObject;
083:
084: // Do NOT remove. Only for externalization //
085: public ETLSourceMultiViewElement() {
086: super ();
087: }
088:
089: // Creates new editor //
090: public ETLSourceMultiViewElement(ETLDataObject etlDataObject) {
091: super (etlDataObject.getETLEditorSupport());
092: this .etlDataObject = etlDataObject;
093: initialize();
094:
095: setActivatedNodes(new Node[] { etlDataObject.getNodeDelegate() });
096: }
097:
098: private void initialize() {
099: /**
100: * only thing which works to make the XmlNav show for this MVElement see
101: * (http://www.netbeans.org/issues/show_bug.cgi?id=67257)
102: */
103: associateLookup(new ProxyLookup(new Lookup[] { Lookups
104: .fixed(new Object[] { getActionMap(), etlDataObject,
105: etlDataObject.getNodeDelegate() }) }));
106: }
107:
108: public JComponent getToolbarRepresentation() {
109: Document doc = getEditorPane().getDocument();
110: if (doc instanceof NbDocument.CustomToolbar) {
111: if (toolbar == null) {
112: toolbar = ((NbDocument.CustomToolbar) doc)
113: .createToolbar(getEditorPane());
114: }
115: return toolbar;
116: }
117: return null;
118: }
119:
120: public JComponent getVisualRepresentation() {
121: return this ;
122: }
123:
124: public void setMultiViewCallback(MultiViewElementCallback callback) {
125: multiViewObserver = callback;
126: ETLEditorSupport editor = etlDataObject.getETLEditorSupport();
127: editor.setTopComponent(callback.getTopComponent());
128: }
129:
130: @Override
131: public void requestVisible() {
132: if (multiViewObserver != null)
133: multiViewObserver.requestVisible();
134: else
135: super .requestVisible();
136: }
137:
138: @Override
139: public void requestActive() {
140: if (multiViewObserver != null)
141: multiViewObserver.requestActive();
142: else
143: super .requestActive();
144: }
145:
146: @Override
147: protected boolean closeLast() {
148: return true;
149: }
150:
151: @Override
152: public org.openide.awt.UndoRedo getUndoRedo() {
153: ETLEditorSupport editor = etlDataObject.getETLEditorSupport();
154: return editor.getUndoManager();
155: }
156:
157: public CloseOperationState canCloseElement() {
158: // if this is not the last cloned xml editor component, closing is OK
159: if (!ETLEditorSupport.isLastView(multiViewObserver
160: .getTopComponent()))
161: return CloseOperationState.STATE_OK;
162:
163: // return a placeholder state - to be sure our CloseHandler is called
164: return MultiViewFactory.createUnsafeCloseState(
165: "ID_TEXT_CLOSING", // dummy ID // NOI18N
166: MultiViewFactory.NOOP_CLOSE_ACTION,
167: MultiViewFactory.NOOP_CLOSE_ACTION);
168: }
169:
170: // org.openide.text.CloneableEditor - default PERSISTENCE_ONLY_OPENED
171: @Override
172: public int getPersistenceType() {
173: return PERSISTENCE_ONLY_OPENED;
174: }
175:
176: @Override
177: public void componentDeactivated() {
178: SaveCookie cookie = (SaveCookie) etlDataObject
179: .getCookie(SaveCookie.class);
180: if (cookie != null) {
181: etlDataObject.getETLEditorSupport().syncModel();
182: }
183: super .componentDeactivated();
184: // When the user is switching from the source view sync the text
185: // with the etl model (treat it as a focus change).
186:
187: }
188:
189: @Override
190: public void componentActivated() {
191: super .componentActivated();
192: setActivatedNodes(new Node[0]);
193: setActivatedNodes(new Node[] { etlDataObject.getNodeDelegate() });
194: DataObjectProvider.activeDataObject = etlDataObject;
195: }
196:
197: @Override
198: public void componentClosed() {
199: super .canClose(null, true);
200: super .componentClosed();
201: }
202:
203: @Override
204: public void componentShowing() {
205: super .componentShowing();
206: ETLEditorSupport editor = etlDataObject.getETLEditorSupport();
207: SaveCookie cookie = (SaveCookie) etlDataObject
208: .getCookie(SaveCookie.class);
209: if (cookie != null) {
210: editor.synchDocument();
211: }
212: UndoRedo.Manager undoRedo = editor.getUndoManager();
213: Document document = editor.getDocument();
214: document.addUndoableEditListener(undoRedo);
215: DataObjectProvider.activeDataObject = etlDataObject;
216: }
217:
218: @Override
219: public void componentHidden() {
220: super .componentHidden();
221: // When the user is switching from the source view sync the text
222: // with the wsdl model (treat it as a focus change).
223: etlDataObject.getETLEditorSupport().syncModel();
224: }
225:
226: @Override
227: public void componentOpened() {
228: super .componentOpened();
229: DataObjectProvider.activeDataObject = etlDataObject;
230: }
231:
232: @Override
233: public void writeExternal(ObjectOutput out) throws IOException {
234: super .writeExternal(out);
235: out.writeObject(etlDataObject);
236: }
237:
238: @Override
239: public void readExternal(ObjectInput in) throws IOException,
240: ClassNotFoundException {
241: super .readExternal(in);
242: Object firstObject = in.readObject();
243: if (firstObject instanceof ETLDataObject) {
244: etlDataObject = (ETLDataObject) firstObject;
245: }
246: }
247:
248: @Override
249: public Action[] getActions() {
250: ArrayList<Action> actionsList = new ArrayList<Action>();
251: for (Action action : super .getActions()) {
252: //FileSystemAction gets added from addFromLayers().commenting this will make Local History option appear twice
253: if (!(action instanceof FileSystemAction))
254: actionsList.add(action);
255: }
256: actionsList.add(addFromLayers());
257: Action[] actions = new Action[actionsList.size()];
258: actionsList.toArray(actions);
259: return actions;
260: }
261:
262: private Action addFromLayers() {
263: Action action = null;
264: Lookup look = Lookups.forPath("Projects/Actions");
265: for (Object next : look.lookupAll(Object.class)) {
266: if (next instanceof Action) {
267: action = (Action) next;
268: } else if (next instanceof JSeparator) {
269: action = null;
270: }
271: }
272: return action;
273: }
274: }
|