1 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 ; 27 import java.io.Writer ; 28 import java.io.StringWriter ; 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 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 50 public BookDataObject (FileObject pf, BookDataLoader loader) throws DataObjectExistsException { 51 super (pf, loader); 52 modelSynchronizer = new ModelSynchronizer(this); 53 org.xml.sax.InputSource 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 ex) { 61 System.out.println("ex="+ex); 62 } 63 } 64 68 private void parseDocument() throws IOException { 69 if (book==null) { 70 book = getBook(); 71 } else { 72 java.io.InputStream is = getEditorSupport().getInputStream(); 73 Book newBook = null; 74 try { 75 newBook = Book.createGraph(is); 76 } catch (RuntimeException 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 { 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 getIcon() { 108 return org.openide.util.Utilities.loadImage("org/netbeans/modules/xml/multiview/resources/xmlObject.gif"); } 110 111 public String preferredID() { 112 return "book_multiview_design"+String.valueOf(type); 113 } 114 } 115 116 119 public void showElement(Object element) { 120 Object target=null; 121 if (element instanceof Chapter) { 122 openView(0); 123 target=element; 124 } 125 if (target!=null) { 126 final Object key=target; 127 org.netbeans.modules.xml.multiview.Utils.runInAwtDispatchThread(new Runnable () { 128 public void run() { 129 getActiveMultiViewElement0().getSectionView().openPanel(key); 130 } 131 }); 132 } 133 } 134 135 protected String getPrefixMark() { 136 return null; 137 } 138 139 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 model, FileLock lock, boolean modify) { 160 if (model == null) { 161 return; 162 } 163 try { 164 Writer out = new StringWriter (); 165 ((Book) model).write(out); 166 out.close(); 167 getDataCache().setData(lock, out.toString(), modify); 168 } catch (IOException 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 getModel() { 176 try { 177 return getBook(); 178 } catch (IOException 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 e) { 188 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e); 189 } 190 } 191 } 192 } 193 | Popular Tags |