001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041: package org.netbeans.modules.vmd.io.editor;
042:
043: import org.netbeans.core.spi.multiview.CloseOperationState;
044: import org.netbeans.core.spi.multiview.MultiViewElement;
045: import org.netbeans.core.spi.multiview.MultiViewElementCallback;
046: import org.netbeans.core.spi.multiview.MultiViewFactory;
047: import org.netbeans.modules.vmd.api.io.DataEditorView;
048: import org.netbeans.modules.vmd.api.io.DataObjectContext;
049: import org.netbeans.modules.vmd.api.io.providers.IOSupport;
050: import org.openide.awt.UndoRedo;
051: import org.openide.util.Lookup;
052: import org.openide.util.lookup.Lookups;
053: import org.openide.util.lookup.ProxyLookup;
054:
055: import javax.swing.*;
056: import java.io.IOException;
057: import java.io.Serializable;
058: import java.util.ArrayList;
059:
060: /**
061: * @author David Kaspar
062: */
063: public class EditorViewElement implements MultiViewElement,
064: Serializable {
065:
066: private static final long serialVersionUID = -1;
067:
068: private static final String CLOSING_ID = "ID_JAVA_CLOSING"; // NOI18N
069:
070: private DataObjectContext context;
071: private DataEditorView view;
072: private transient DataEditorView.Kind kind;
073: private transient Lookup lookup;
074: private transient EditorTopComponent topComponent;
075: private transient MultiViewElementCallback callback;
076:
077: public EditorViewElement() {
078: }
079:
080: public EditorViewElement(DataObjectContext context,
081: DataEditorView view) {
082: this .context = context;
083: this .view = view;
084: init();
085: }
086:
087: private void init() {
088: kind = view.getKind();
089: ArrayList<Object> lookupObjects = DataEditorViewLookupFactoryRegistry
090: .getLookupObjects(context, view);
091: ArrayList<Lookup> lookups = DataEditorViewLookupFactoryRegistry
092: .getLookups(context, view);
093: lookupObjects.add(view);
094: lookups.add(Lookups.fixed(lookupObjects.toArray()));
095: lookup = new ProxyLookup(lookups.toArray(new Lookup[lookups
096: .size()]));
097: IOSupport.getDocumentSerializer(context.getDataObject())
098: .startLoadingDocument();
099: }
100:
101: public JComponent getVisualRepresentation() {
102: if (topComponent == null) {
103: JComponent visualRepresentation = view
104: .getVisualRepresentation();
105: if (visualRepresentation != null) {
106: topComponent = kind == DataEditorView.Kind.CODE ? new CodeEditorTopComponent(
107: context, lookup, visualRepresentation)
108: : new EditorTopComponent(context, lookup,
109: visualRepresentation);
110: }
111: }
112: return topComponent;
113: }
114:
115: public JComponent getToolbarRepresentation() {
116: return view.getToolbarRepresentation();
117: }
118:
119: public Action[] getActions() {
120: return callback != null ? callback.createDefaultActions()
121: : new Action[0];
122: }
123:
124: public Lookup getLookup() {
125: getVisualRepresentation();
126: return topComponent.getLookup();
127: }
128:
129: public void componentOpened() {
130: view.componentOpened();
131: }
132:
133: public void componentClosed() {
134: view.componentClosed();
135: }
136:
137: public void componentShowing() {
138: view.componentShowing();
139: }
140:
141: public void componentHidden() {
142: view.componentHidden();
143: }
144:
145: public void componentActivated() {
146: IOSupport.notifyDataEditorViewActivated(view);
147: view.componentActivated();
148: }
149:
150: public void componentDeactivated() {
151: view.componentDeactivated();
152: }
153:
154: public UndoRedo getUndoRedo() {
155: UndoRedo undoRedo = view.getUndoRedo();
156: if (undoRedo != null)
157: return undoRedo;
158: if (kind != DataEditorView.Kind.MODEL)
159: return null;
160: return IOSupport.getDocumentSerializer(context.getDataObject())
161: .getUndoRedoManager();
162: }
163:
164: public void setMultiViewCallback(MultiViewElementCallback callback) {
165: this .callback = callback;
166: IOSupport.getDataObjectInteface(context.getDataObject())
167: .setMVTC(callback.getTopComponent());
168: }
169:
170: public CloseOperationState canCloseElement() {
171: return MultiViewFactory.createUnsafeCloseState(CLOSING_ID,
172: null, null);
173: }
174:
175: private void writeObject(java.io.ObjectOutputStream out)
176: throws IOException {
177: out.writeObject(context);
178: out.writeObject(view);
179: }
180:
181: private void readObject(java.io.ObjectInputStream in)
182: throws IOException, ClassNotFoundException {
183: Object object = in.readObject();
184: if (!(object instanceof DataObjectContext))
185: throw new ClassNotFoundException(
186: "DataObjectContext expected but not found"); // NOI18N
187: context = (DataObjectContext) object;
188: object = in.readObject();
189: if (!(object instanceof DataEditorView))
190: throw new ClassNotFoundException(
191: "DataEditorView expected but not found"); // NOI18N
192: view = (DataEditorView) object;
193: init();
194: }
195:
196: }
|