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.xslt.core;
020:
021: import java.awt.event.ActionEvent;
022: import java.io.IOException;
023: import java.util.LinkedList;
024: import java.util.List;
025: import java.util.concurrent.atomic.AtomicBoolean;
026: import java.util.concurrent.atomic.AtomicReference;
027: import javax.swing.AbstractAction;
028: import javax.swing.Action;
029: import javax.xml.transform.Source;
030: import org.netbeans.modules.xslt.core.multiview.XsltDesignViewOpenAction;
031: import org.netbeans.modules.xslt.core.multiview.XsltMultiViewSupport;
032: import org.netbeans.modules.xslt.mapper.model.MapperContext;
033: import org.netbeans.modules.xslt.model.XslModel;
034: import org.netbeans.spi.xml.cookies.CheckXMLSupport;
035: import org.netbeans.spi.xml.cookies.DataObjectAdapters;
036: import org.netbeans.spi.xml.cookies.TransformableSupport;
037: import org.openide.cookies.SaveCookie;
038: import org.openide.filesystems.FileObject;
039: import org.openide.loaders.DataNode;
040: import org.openide.loaders.DataObjectExistsException;
041: import org.openide.loaders.MultiDataObject;
042: import org.openide.loaders.MultiFileLoader;
043: import org.openide.nodes.Children;
044: import org.openide.nodes.CookieSet;
045: import org.openide.nodes.Node;
046: import org.openide.util.Lookup;
047: import org.openide.util.actions.SystemAction;
048: import org.openide.util.lookup.AbstractLookup;
049: import org.openide.util.lookup.InstanceContent;
050: import org.openide.util.lookup.Lookups;
051: import org.openide.util.lookup.ProxyLookup;
052: import org.xml.sax.InputSource;
053:
054: /**
055: *
056: * @author Vitaly Bychkov
057: * @version 1.0
058: */
059: public class XSLTDataObject extends MultiDataObject {
060:
061: private static final long serialVersionUID = 1L;
062:
063: // private transient final DataObjectCookieManager cookieManager;
064: private static final String FILE_DESC = "LBL_FileNode_desc"; // NOI18N
065: private transient AtomicReference<Lookup> myLookup = new AtomicReference<Lookup>();
066: private transient AtomicBoolean isLookupInit = new AtomicBoolean(
067: false);
068: private XSLTDataEditorSupport myDataEditorSupport;
069: private transient AtomicReference<InstanceContent> myServices = new AtomicReference<InstanceContent>();
070:
071: public XSLTDataObject(final FileObject pf,
072: final MultiFileLoader loader)
073: throws DataObjectExistsException {
074: super (pf, loader);
075: myDataEditorSupport = new XSLTDataEditorSupport(this );
076:
077: CookieSet cookies = getCookieSet();
078: cookies.add(getEditorSupport());
079:
080: // add check and validate cookies
081: InputSource is = DataObjectAdapters.inputSource(this );
082: cookies.add(new CheckXMLSupport(is));
083: cookies.add(new ValidateXSLSupport(is));
084:
085: Source source = DataObjectAdapters.source(this );
086: cookies.add(new TransformableSupport(source));
087: // cookies.add((Node.Cookie) DataEditorSupport.create(this, getPrimaryEntry(), cookies));
088: }
089:
090: public Lookup getLookup() {
091: Lookup lookup;
092: List<Lookup> list = new LinkedList<Lookup>();
093: //TODO m
094: if (myLookup.get() == null) {
095:
096: list.add(Lookups.fixed(new Object[] { super .getLookup(),
097: this }));
098:
099: //
100: // add lazy initialization elements
101: InstanceContent.Convertor<Class, Object> conv = new InstanceContent.Convertor<Class, Object>() {
102: // TODO a
103: // private AtomicReference<XSLTValidationController> valControllerRef =
104: // new AtomicReference<XSLTValidationController>();
105:
106: public Object convert(Class obj) {
107: if (obj == XslModel.class) {
108: return getEditorSupport().getXslModel();
109: }
110: //
111: if (obj == MapperContext.class) {
112: return getEditorSupport().getMapperContext();
113: }
114: //
115: if (obj == XSLTDataEditorSupport.class) {
116: return getEditorSupport();
117: }
118: //
119: // if (obj == XSLTValidationController.class) {
120: // valControllerRef.compareAndSet(null,
121: // new XSLTValidationController(getEditorSupport().getXslModel()));
122: // return valControllerRef.get();
123: // }
124: return null;
125: }
126:
127: public Class type(Class obj) {
128: return obj;
129: }
130:
131: public String id(Class obj) {
132: return obj.toString();
133: }
134:
135: public String displayName(Class obj) {
136: return obj.getName();
137: }
138: };
139:
140: list.add(Lookups.fixed(new Class[] { XslModel.class,
141: MapperContext.class, XSLTDataEditorSupport.class
142: /*, XSLTValidationController.class*/}, conv));
143: //
144:
145: //
146: // WARNING
147: // CANNOT add Lookups.singleton(getNodeDelegate()) or will stack
148: // overflow
149: // WARNING
150: //
151:
152: /*
153: * Services are used for push/pop SaveCookie in lookup. This allow to work
154: * "Save" action on diagram.
155: */
156: myServices.compareAndSet(null, new InstanceContent());
157: myServices.get().add(new Empty()); // FIX for #IZ78702
158: list.add(Lookups.fixed(myServices.get()));
159:
160: lookup = new ProxyLookup(list.toArray(new Lookup[list
161: .size()]));
162:
163: // Lookup is now available from this Lookup.Provider but only from this
164: // same thread which has the lock on Lookup
165: //
166: myLookup.compareAndSet(null, lookup);
167: isLookupInit.compareAndSet(false, true);
168: }
169: return myLookup.get();
170: }
171:
172: @Override
173: public void setModified(boolean modified) {
174: super .setModified(modified);
175: if (modified) {
176: getCookieSet().add(getSaveCookie());
177: if (isLookupInit.get()) {
178: myServices.get().add(getSaveCookie());
179: }
180: } else {
181: getCookieSet().remove(getSaveCookie());
182: if (isLookupInit.get()) {
183: myServices.get().remove(getSaveCookie());
184: }
185: }
186: }
187:
188: @Override
189: protected Node createNodeDelegate() {
190: return new XSLTDataNode(this , getEditorSupport());
191: }
192:
193: public XSLTDataEditorSupport getEditorSupport() {
194: return myDataEditorSupport;
195: }
196:
197: private SaveCookie getSaveCookie() {
198: return new SaveCookie() {
199:
200: public void save() throws IOException {
201: getEditorSupport().saveDocument();
202: }
203:
204: @Override
205: public int hashCode() {
206: return getClass().hashCode();
207: }
208:
209: @Override
210: public boolean equals(Object other) {
211: return other != null
212: && getClass().equals(other.getClass());
213: }
214: };
215: }
216:
217: private static class XSLTDataNode extends DataNode {
218: XSLTDataEditorSupport myEditorSupport;
219:
220: public XSLTDataNode(XSLTDataObject obj,
221: XSLTDataEditorSupport support) {
222: super (obj, Children.LEAF);
223: setIconBaseWithExtension(XSLTDataLoaderBeanInfo.PATH_TO_IMAGE);
224: myEditorSupport = support;
225: }
226:
227: @Override
228: public Action getPreferredAction() {
229: return SystemAction.get(XsltDesignViewOpenAction.class);
230: // return new AbstractAction() {
231: // private static final long serialVersionUID = 1L;
232: // public void actionPerformed(ActionEvent e) {
233: // // Fix for #81066
234: // if ( myEditorSupport.getOpenedPanes()==null ||
235: // myEditorSupport.getOpenedPanes().length==0 )
236: // {
237: // myEditorSupport.open();
238: // XsltMultiViewSupport support =
239: // XsltMultiViewSupport.getInstance();
240: // support.requestViewOpen(myEditorSupport);
241: // } else {
242: // myEditorSupport.open();
243: // }
244: // }
245: // };
246: }
247:
248: }
249:
250: private static class Empty {
251:
252: }
253:
254: }
|