KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > multiview > test > BookDataObject


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.multiview.test;
20
21 import org.netbeans.modules.xml.multiview.*;
22 import org.openide.filesystems.*;
23 import org.openide.loaders.*;
24 import org.openide.ErrorManager;
25
26 import java.io.IOException JavaDoc;
27 import java.io.Writer JavaDoc;
28 import java.io.StringWriter JavaDoc;
29
30 import org.netbeans.api.xml.cookies.ValidateXMLCookie;
31 import org.netbeans.api.xml.cookies.CheckXMLCookie;
32 import org.netbeans.spi.xml.cookies.*;
33
34 import org.netbeans.modules.xml.multiview.test.bookmodel.*;
35 import org.netbeans.modules.schema2beans.Schema2BeansException;
36
37 /**
38  *
39  * @author mkuchtiak
40  */

41 public class BookDataObject extends XmlMultiViewDataObject {
42
43     private ModelSynchronizer modelSynchronizer;
44
45     private static final int TYPE_TOOLBAR = 0;
46     private static final int TYPE_TREEPANEL = 1;
47     Book book;
48
49     /** Creates a new instance of BookDataObject */
50     public BookDataObject (FileObject pf, BookDataLoader loader) throws DataObjectExistsException {
51         super (pf, loader);
52         modelSynchronizer = new ModelSynchronizer(this);
53         org.xml.sax.InputSource JavaDoc in = DataObjectAdapters.inputSource(this);
54         CheckXMLCookie checkCookie = new CheckXMLSupport(in);
55         getCookieSet().add(checkCookie);
56         ValidateXMLCookie validateCookie = new ValidateXMLSupport(in);
57         getCookieSet().add(validateCookie);
58         try {
59             parseDocument();
60         } catch (IOException JavaDoc ex) {
61             System.out.println("ex="+ex);
62         }
63     }
64     /**
65      *
66      * @throws IOException
67      */

68     private void parseDocument() throws IOException JavaDoc {
69         if (book==null) {
70             book = getBook();
71         } else {
72             java.io.InputStream JavaDoc is = getEditorSupport().getInputStream();
73             Book newBook = null;
74             try {
75                 newBook = Book.createGraph(is);
76             } catch (RuntimeException JavaDoc ex) {
77                 System.out.println("runtime error "+ex);
78             }
79             if (newBook!=null) {
80                 book.merge(newBook, org.netbeans.modules.schema2beans.BaseBean.MERGE_UPDATE);
81             }
82         }
83     }
84
85     public Book getBook() throws IOException JavaDoc {
86         if (book==null) book = Book.createGraph(FileUtil.toFile(getPrimaryFile()));
87         return book;
88     }
89
90     protected DesignMultiViewDesc[] getMultiViewDesc() {
91         return new DesignMultiViewDesc[]{new DesignView(this,TYPE_TOOLBAR),new DesignView(this,TYPE_TREEPANEL)};
92     }
93
94     private static class DesignView extends DesignMultiViewDesc {
95         private int type;
96         DesignView(BookDataObject dObj, int type) {
97             super(dObj, "Design"+String.valueOf(type));
98             this.type=type;
99         }
100
101         public org.netbeans.core.spi.multiview.MultiViewElement createElement() {
102             BookDataObject dObj = (BookDataObject)getDataObject();
103             if (type==TYPE_TOOLBAR) return new BookToolBarMVElement(dObj);
104             else return new BookTreePanelMVElement(dObj);
105         }
106
107         public java.awt.Image JavaDoc getIcon() {
108             return org.openide.util.Utilities.loadImage("org/netbeans/modules/xml/multiview/resources/xmlObject.gif"); //NOI18N
109
}
110
111         public String JavaDoc preferredID() {
112             return "book_multiview_design"+String.valueOf(type);
113         }
114     }
115
116     /** Enable to focus specific object in Multiview Editor
117      * The default implementation opens the XML View
118      */

119     public void showElement(Object JavaDoc element) {
120         Object JavaDoc target=null;
121         if (element instanceof Chapter) {
122             openView(0);
123             target=element;
124         }
125         if (target!=null) {
126             final Object JavaDoc key=target;
127             org.netbeans.modules.xml.multiview.Utils.runInAwtDispatchThread(new Runnable JavaDoc() {
128                 public void run() {
129                     getActiveMultiViewElement0().getSectionView().openPanel(key);
130                 }
131             });
132         }
133     }
134
135     protected String JavaDoc getPrefixMark() {
136         return null;
137     }
138
139     /** Enable to get active MultiViewElement object
140      */

141     public ToolBarMultiViewElement getActiveMultiViewElement0() {
142         return (ToolBarMultiViewElement)super.getActiveMultiViewElement();
143     }
144
145     public void modelUpdatedFromUI() {
146         modelSynchronizer.requestUpdateData();
147     }
148
149     private class ModelSynchronizer extends XmlMultiViewDataSynchronizer {
150
151         public ModelSynchronizer(XmlMultiViewDataObject dataObject) {
152             super(dataObject, 500);
153         }
154
155         protected boolean mayUpdateData(boolean allowDialog) {
156             return true;
157         }
158
159         protected void updateDataFromModel(Object JavaDoc model, FileLock lock, boolean modify) {
160             if (model == null) {
161                 return;
162             }
163             try {
164                 Writer JavaDoc out = new StringWriter JavaDoc();
165                 ((Book) model).write(out);
166                 out.close();
167                 getDataCache().setData(lock, out.toString(), modify);
168             } catch (IOException JavaDoc e) {
169                 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e);
170             } catch (Schema2BeansException e) {
171                 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e);
172             }
173         }
174
175         protected Object JavaDoc getModel() {
176             try {
177                 return getBook();
178             } catch (IOException JavaDoc e) {
179                 ErrorManager.getDefault().notify(org.openide.ErrorManager.INFORMATIONAL, e);
180                 return null;
181             }
182         }
183
184         protected void reloadModelFromData() {
185             try {
186                 parseDocument();
187             } catch (IOException JavaDoc e) {
188                 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e);
189             }
190         }
191     }
192 }
193
Popular Tags