KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > core > XMLDataObject


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.modules.xml.core;
20
21 import java.beans.*;
22
23 import javax.xml.transform.Source JavaDoc;
24
25 import org.xml.sax.*;
26
27 import org.openide.awt.HtmlBrowser;
28 import org.openide.filesystems.*;
29 import org.openide.loaders.*;
30 import org.openide.util.*;
31 import org.openide.nodes.*;
32 import org.openide.cookies.*;
33 import org.openide.actions.*;
34 import org.openide.windows.CloneableOpenSupport;
35 import org.openide.util.actions.SystemAction;
36
37 import org.netbeans.modules.xml.core.text.TextEditorSupport;
38 import org.netbeans.modules.xml.core.sync.*;
39 import org.netbeans.modules.xml.core.cookies.*;
40 import org.netbeans.modules.xml.core.settings.*;
41
42 import org.netbeans.spi.xml.cookies.*;
43
44 /** Object that provides main functionality for xml document.
45  * Instance holds all synchronization related state information.
46  * This class is final only for performance reasons,
47  * can be unfinaled if desired.
48  *
49  * @author Libor Kramolis
50  */

51 public final class XMLDataObject extends org.openide.loaders.XMLDataObject
52         implements XMLDataObjectLook, PropertyChangeListener {
53
54     /** Serial Version UID */
55     private static final long serialVersionUID = 9153823984913876866L;
56     
57     /** Default XML Mime Type. */
58     public static final String JavaDoc MIME_TYPE = "text/xml"; // NOI18N
59

60     /** Synchronization implementation delegate. */
61     private XMLSyncSupport sync;
62     
63     /** Cookie Manager */
64     private final DataObjectCookieManager cookieManager;
65     
66
67     //
68
// init
69
//
70

71
72     /** Create new XMLDataObject
73      *
74      * @param fo the primary file object
75      * @param loader loader of this data object
76      */

77     public XMLDataObject (final FileObject fo, MultiFileLoader loader) throws DataObjectExistsException {
78         super (fo, loader);
79         
80         CookieSet set = getCookieSet();
81         set.add (cookieManager = new DataObjectCookieManager (this, set));
82
83         sync = new XMLSyncSupport(this);
84
85         TextEditorSupport.TextEditorSupportFactory editorFactory =
86             TextEditorSupport.findEditorSupportFactory (this, MIME_TYPE);
87         editorFactory.registerCookies (set);
88
89         CookieSet.Factory viewCookieFactory = new ViewCookieFactory();
90         set.add (ViewCookie.class, viewCookieFactory);
91
92         InputSource is = DataObjectAdapters.inputSource (this);
93         // add check and validate cookies
94
set.add (new CheckXMLSupport (is));
95         set.add (new ValidateXMLSupport (is));
96         
97         // add TransformableCookie
98
Source JavaDoc source = DataObjectAdapters.source (this);
99         set.add (new TransformableSupport (source));
100         
101         // add Scenario support
102
set.add (new ScenarioSupport (this));
103         
104         new CookieManager (this, set, XMLCookieFactoryCreator.class);
105
106         this.addPropertyChangeListener (this); //??? - strange be aware of firing cycles
107
}
108
109
110     
111 /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
112  * DATAOBJECT stuff
113  * cookie management
114  * node delegate
115  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
116  */

117
118
119     /**
120      */

121     protected Node createNodeDelegate () {
122         if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug ("--> XMLDataObject.createNodeDelegate: this = " + this);
123
124         DataNodeCreator dataNodeCreator = (DataNodeCreator) Lookup.getDefault().lookup (DataNodeCreator.class);
125         Node dataNode = null;
126
127         if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug ("-*- XMLD O .createNodeDelegate: dataNodeCreator = " + dataNodeCreator);
128
129         if ( dataNodeCreator != null ) {
130             dataNode = dataNodeCreator.createDataNode (this);
131         } else {
132             dataNode = new XMLDataNode (this);
133         }
134
135         if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug ("<-- XMLDataObject.createNodeDelegate: dataNode = " + dataNode);
136
137         return dataNode;
138     }
139
140     /**
141      * Get 'semantics' node delegate from superclass.
142      */

143     Node createDefaultNodeDelegate () {
144         return super.createNodeDelegate(); //it is a FilterNode
145
}
146
147
148
149     // it is called by super class constructor
150
protected EditorCookie createEditorCookie () {
151         return null;
152     }
153     
154
155     /** Delegate to super with possible debug messages. */
156     public void setModified (boolean state) {
157         if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug ("XMLDataObject:setModified: state = " + state); // NOI18N
158

159         super.setModified (state);
160     }
161
162
163     /** Delegate to super with possible debug messages. */
164     public org.openide.nodes.Node.Cookie getCookie(Class JavaDoc klass) {
165                 
166         Node.Cookie cake = null;
167         boolean change = false;
168
169         if (SaveCookie.class.equals (klass) ) {
170             if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug ("XMLDataObject::getCookie");//, new RuntimeException ("Save cookie check")); // NOI18N
171
}
172
173         // take lock to prevent deadlock on cookie set that can be called
174
// from other thread during cookie removal
175
synchronized (this) {
176             cake = super.getCookie (klass);
177
178             if ( ( cake == null ) &&
179                  ( CloneableOpenSupport.class == klass ) ) { //!!! HACK -- backward compatibility
180
cake = super.getCookie (OpenCookie.class);
181             }
182         }
183         
184         if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug ("XMLDataOObject::getCookie: class = " + klass + " => " + cake); // NOI18N
185

186         return cake;
187     }
188         
189         
190     public DataObjectCookieManager getCookieManager() {
191         return cookieManager;
192     }
193     
194     
195 /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
196  * UPDATE section
197  * handles updating one representation by changed another one
198  *
199  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
200  */

201
202 // // from XMLDataObjectLook
203
// public void updateTextDocument () {
204
// EditorCookie es = (EditorCookie)getCookie (EditorCookie.class);
205
// if (es != null) {
206
// es.close();
207
// }
208
// }
209

210     /** TREE -> TEXT
211      * Updates document by content of parsed tree based on the
212      * last document version.
213      * Note: the tree is always maximum valid part of document.
214      * It takes parsed tree as primary data model. IT MUST CHANGE
215      * tree must contain an error element.
216      */

217     public synchronized void updateDocument () {
218
219         //!!! to be implemented without dependency on tree
220
Thread.dumpStack();
221 // sync.representationChanged(TreeDocument.class); //!!!
222

223     }
224     
225     public Synchronizator getSyncInterface() {
226         return sync;
227     }
228
229
230     
231
232
233 /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
234  * LISTENERS section
235  * handlers of various listeners attached by this DataObject
236  *
237  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
238  */

239
240     /**
241      * File was externaly modified, detected by OpenIDE DataObject.
242      */

243     public void propertyChange (PropertyChangeEvent e) {
244
245         if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug ("event " + e.getPropertyName()); // NOI18N
246

247         if (org.openide.loaders.XMLDataObject.PROP_DOCUMENT.equals (e.getPropertyName())) {
248
249             // filter out uninteresting events
250
if (e.getOldValue() == e.getNewValue()) return; //e.g. null == null
251

252             sync.representationChanged(FileObject.class);
253         }
254     }
255
256
257     public HelpCtx getHelpCtx() {
258         //return new HelpCtx(XMLDataObject.class);
259
return HelpCtx.DEFAULT_HELP;
260     }
261     
262     //
263
// class XMLDataNode
264
//
265

266     /**
267      *
268      */

269     public static class XMLDataNode extends DataNode {
270
271         /** Create new XMLDataNode. */
272         public XMLDataNode (XMLDataObject obj) {
273             super (obj, Children.LEAF);
274
275             setIconBaseWithExtension ("org/netbeans/modules/xml/core/resources/xmlObject.gif"); // NOI18N
276
setShortDescription (Util.THIS.getString ("PROP_XMLDataNode_description"));
277         }
278
279         public SystemAction getDefaultAction() {
280             CoreSettings settings = CoreSettings.getDefault();
281             SystemAction actions[] = getActions();
282             SystemAction system = SystemAction.get (EditAction.class);
283
284             return settings.getDefaultAction (actions, system);
285         }
286     
287     } // end of class XMLDataNode
288

289
290     //
291
// class ViewCookieFactory
292
//
293

294     /**
295      *
296      */

297     private class ViewCookieFactory implements CookieSet.Factory {
298
299         /** Creates new Cookie */
300         public Node.Cookie createCookie (Class JavaDoc klass) {
301             if (klass == ViewCookie.class) {
302                 return new ViewSupport (XMLDataObject.this.getPrimaryEntry());
303             } else {
304                 return null;
305             }
306         }
307         
308     } // end of class ViewCookieFactory
309

310
311     //
312
// class ViewSupport
313
//
314

315     /**
316      *
317      */

318     private static final class ViewSupport implements ViewCookie {
319
320         /** entry */
321         private MultiDataObject.Entry primary;
322         
323         /** Constructs new ViewSupport */
324         public ViewSupport (MultiDataObject.Entry primary) {
325             this.primary = primary;
326         }
327         
328         /**
329          */

330         public void view () {
331             try {
332                 HtmlBrowser.URLDisplayer.getDefault().showURL(primary.getFile().getURL());
333             } catch (FileStateInvalidException e) {
334             }
335         }
336
337     } // end of class ViewSupport
338

339
340
341     //
342
// interface DataNodeCreator
343
//
344

345     /**
346      *
347      */

348     public static interface DataNodeCreator {
349
350         /**
351          */

352         public DataNode createDataNode (XMLDataObject xmlDO);
353
354     } // end of interface DataNodeCreator
355

356     
357
358     //
359
// interface XMLCookieFactoryCreator
360
//
361

362     /**
363      *
364      */

365     public static interface XMLCookieFactoryCreator extends CookieFactoryCreator {
366         
367     } // end: interface XMLCookieFactoryCreator
368

369 }
370
Popular Tags