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: If you wish your version of this file to be governed by only the CDDL
023: or only the GPL Version 2, indicate your decision by adding
024: "[Contributor] elects to include this software in this distribution
025: under the [CDDL or GPL Version 2] license." If you do not indicate a
026: single choice of license, a recipient has the option to distribute
027: your version of this file under either the CDDL, the GPL Version 2 or
028: to extend the choice of license to its licensees as provided above.
029: However, if you add GPL Version 2 code and therefore, elected the GPL
030: Version 2 license, then the option applies only if the new code is
031: made subject to such option by the copyright holder.
032: */
033: package org.netbeans.modules.etl.ui;
034:
035: import java.io.IOException;
036: import java.util.LinkedList;
037: import java.util.List;
038: import java.util.concurrent.atomic.AtomicBoolean;
039: import java.util.concurrent.atomic.AtomicReference;
040:
041: import org.openide.util.Lookup;
042: import org.openide.util.lookup.Lookups;
043: import org.openide.util.lookup.ProxyLookup;
044: import org.netbeans.modules.etl.model.ETLDefinition;
045: import org.netbeans.modules.etl.ui.model.impl.ETLCollaborationModel;
046: import org.netbeans.modules.etl.ui.view.ETLCollaborationTopPanel;
047: import org.netbeans.spi.xml.cookies.CheckXMLSupport;
048: import org.netbeans.spi.xml.cookies.DataObjectAdapters;
049: import org.openide.ErrorManager;
050: import org.openide.WizardDescriptor;
051: import org.openide.cookies.SaveCookie;
052: import org.openide.filesystems.FileObject;
053: import org.openide.loaders.DataFolder;
054: import org.openide.loaders.DataObject;
055: import org.openide.loaders.DataObjectExistsException;
056: import org.openide.loaders.MultiDataObject;
057: import org.openide.loaders.MultiFileLoader;
058: import org.openide.nodes.CookieSet;
059: import org.openide.nodes.Node;
060: import org.openide.util.HelpCtx;
061: import org.xml.sax.InputSource;
062:
063: import com.sun.org.apache.xerces.internal.util.XMLChar;
064:
065: /**
066: * Represents a ETL file.
067: *
068: * @author radval
069: */
070: public class ETLDataObject extends MultiDataObject {
071:
072: public ETLDataObject(FileObject fObj, MultiFileLoader loader)
073: throws DataObjectExistsException {
074: super (fObj, loader);
075: CookieSet set = getCookieSet();
076:
077: editorSupport = new ETLEditorSupport(this );
078: // editor support defines MIME type understood by EditorKits registry
079: set.add(editorSupport);
080:
081: // Add check and validate cookies
082: InputSource is = DataObjectAdapters.inputSource(this );
083: set.add(new CheckXMLSupport(is));
084: try {
085: mModel = new ETLCollaborationModel(this );
086: } catch (Exception ex) {
087: ErrorManager.getDefault().notify(ex);
088: }
089: // add validate action here
090: }
091:
092: @Override
093: protected Node createNodeDelegate() {
094: if (this .mNode == null) {
095: this .mNode = new ETLNode(this );
096: }
097: return this .mNode;
098: }
099:
100: /**
101: * subclasses should look updateServices() and additionalInitialLookup()
102: */
103: @Override
104: public final Lookup getLookup() {
105: if (myLookup.get() == null) {
106:
107: Lookup lookup;
108: List<Lookup> list = new LinkedList<Lookup>();
109:
110: list.add(Lookups.fixed(new Object[] { this }));
111: lookup = new ProxyLookup(list.toArray(new Lookup[list
112: .size()]));
113: myLookup.compareAndSet(null, lookup);
114: isLookupInit.compareAndSet(false, true);
115: }
116: return myLookup.get();
117: }
118:
119: @Override
120: public HelpCtx getHelpCtx() {
121: return HelpCtx.DEFAULT_HELP;
122: // If you add context help, change to:
123: // return new HelpCtx (MyDataObject.class);
124: }
125:
126: @Override
127: protected FileObject handleRename(String name) throws IOException {
128: ETLEditorSupport weSupport = getETLEditorSupport();
129: String oldName = this .getName();
130: weSupport.updateTitles();
131: //Do we need to change the name attribute of the ETL def?
132:
133: //logic to keep the status of the save badge (*) intact.
134: boolean modified = weSupport.getEnv().isModified();
135: if (modified) {
136: weSupport.getEnv().unmarkModified();
137: }
138: FileObject fo = super .handleRename(name);
139: if (modified) {
140: weSupport.getEnv().markModified();
141: }
142: firePropertyChange(DataObject.PROP_NAME, oldName, name);
143: return fo;
144: }
145:
146: @Override
147: protected void handleDelete() throws IOException {
148: //this is work around when file is modified in editor and
149: //editor has lock
150: getETLEditorSupport().getEnv().unmarkModified();
151:
152: super .handleDelete();
153: }
154:
155: @Override
156: protected DataObject handleCreateFromTemplate(DataFolder df,
157: String name) throws IOException {
158: ETLDataObject dataObject = (ETLDataObject) super
159: .handleCreateFromTemplate(df, name);
160: String doName = dataObject.getName();
161: //make sure the the name is a valid NMTOKEN.
162: if (!XMLChar.isValidNmtoken(doName)) {
163: return dataObject;
164: }
165:
166: SaveCookie sCookie = (SaveCookie) dataObject
167: .getCookie(SaveCookie.class);
168: if (sCookie != null) {
169: sCookie.save();
170: }
171: return dataObject;
172: }
173:
174: public void initialize(WizardDescriptor descriptor) {
175: try {
176: DataObjectHelper dHelper = new DataObjectHelper(this );
177: dHelper.initializeETLDataObject(descriptor, this ,
178: getETLEditorSupport());
179: // the first time data object is created, it cannot be got through lookup.
180: // find a better way to do this.
181: DataObjectProvider.activeDataObject = this ;
182: } catch (Exception ex) {
183: ErrorManager.getDefault().notify(ex);
184: }
185: }
186:
187: @Override
188: public void setModified(boolean modified) {
189: super .setModified(modified);
190: if (modified) {
191: getCookieSet().add(getSaveCookie());
192: } else {
193: getCookieSet().remove(getSaveCookie());
194: }
195: }
196:
197: private SaveCookie getSaveCookie() {
198: return new SaveCookie() {
199:
200: public void save() throws IOException {
201: getETLEditorSupport().synchDocument();
202: getETLEditorSupport().saveDocument();
203: }
204:
205: @Override
206: public int hashCode() {
207: return getClass().hashCode();
208: }
209:
210: @Override
211: public boolean equals(Object other) {
212: return getClass().equals(other.getClass());
213: }
214: };
215: }
216:
217: public ETLEditorSupport getETLEditorSupport() {
218: return editorSupport;
219: }
220:
221: public ETLDefinition getETLDefinition() throws Exception {
222: return this .mModel.getETLDefinition();
223: }
224:
225: public ETLCollaborationModel getModel() {
226: return this .mModel;
227: }
228:
229: public ETLCollaborationTopPanel getETLEditorTopPanel()
230: throws Exception {
231: if (this .topPanel == null) {
232: this .topPanel = new ETLCollaborationTopPanel(this );
233: }
234: return this .topPanel;
235: }
236:
237: private static final long serialVersionUID = 6338889116068357651L;
238: private transient ETLEditorSupport editorSupport;
239: private transient ETLCollaborationTopPanel topPanel;
240: private transient ETLCollaborationModel mModel;
241: private transient AtomicReference<Lookup> myLookup = new AtomicReference<Lookup>();
242: private transient AtomicBoolean isLookupInit = new AtomicBoolean(
243: false);
244: private Node mNode = null;
245: public static final String ETL_ICON = "org/netbeans/modules/etl/ui/resources/images/ETLDefinition.png";
246: }
|