1 19 20 package org.netbeans.modules.xml.schema.core; 21 22 import java.io.IOException ; 23 import javax.swing.Action ; 24 import javax.xml.transform.Source ; 25 import org.netbeans.modules.xml.refactoring.CannotRefactorException; 26 import org.netbeans.modules.xml.refactoring.FileRenameRequest; 27 import org.netbeans.modules.xml.refactoring.RefactoringManager; 28 import org.netbeans.modules.xml.refactoring.ui.ModelProvider; 29 import org.netbeans.modules.xml.schema.core.actions.SchemaViewOpenAction; 31 import org.netbeans.modules.xml.schema.core.multiview.SchemaMultiViewSupport; 32 import org.netbeans.modules.xml.schema.model.SchemaModel; 33 import org.netbeans.spi.xml.cookies.CheckXMLSupport; 34 import org.netbeans.spi.xml.cookies.DataObjectAdapters; 35 import org.netbeans.spi.xml.cookies.TransformableSupport; 36 import org.openide.DialogDisplayer; 37 import org.openide.NotifyDescriptor; 38 import org.openide.filesystems.FileObject; 39 import org.openide.cookies.SaveCookie; 40 import org.openide.loaders.DataFolder; 41 import org.openide.loaders.DataNode; 42 import org.openide.loaders.DataObjectExistsException; 43 import org.openide.loaders.MultiDataObject; 44 import org.openide.loaders.UniFileLoader; 45 import org.openide.nodes.Children; 46 import org.openide.nodes.CookieSet; 47 import org.openide.nodes.Node; 48 import org.openide.util.NbBundle; 49 import org.openide.util.HelpCtx; 50 import org.openide.util.actions.SystemAction; 51 import org.xml.sax.InputSource ; 52 53 59 public final class SchemaDataObject extends MultiDataObject { 60 61 67 public SchemaDataObject(FileObject obj, UniFileLoader loader) throws 68 DataObjectExistsException { 69 super(obj, loader); 70 71 CookieSet set = getCookieSet(); 72 schemaEditorSupport = new SchemaEditorSupport(this); 74 set.add(schemaEditorSupport); 75 InputSource is = DataObjectAdapters.inputSource(this); 77 set.add(new CheckXMLSupport(is)); 78 Source source = DataObjectAdapters.source(this); 80 set.add(new TransformableSupport(source)); 81 set.add(new SchemaValidateXMLCookie(this)); 82 set.add(new SchemaMultiViewSupport(this)); 84 } 85 86 91 public SchemaEditorSupport getSchemaEditorSupport() { 92 return schemaEditorSupport; 93 } 94 95 @Override 96 public void handleDelete() throws IOException { 97 if (isModified()) { 98 setModified(false); 99 } 100 getSchemaEditorSupport().getEnv().unmarkModified(); 101 super.handleDelete(); 102 } 103 104 protected Node createNodeDelegate() { 105 SchemaNode n = new SchemaNode(this); 106 n.setIconBaseWithExtension(SCHEMA_ICON_BASE_WITH_EXT); 107 n.setShortDescription(NbBundle.getMessage(SchemaDataObject.class, 108 "LBL_SchemaNode_desc")); 109 110 return n; 111 } 112 113 static class SchemaNode extends DataNode implements ModelProvider { 114 public SchemaNode(SchemaDataObject dobj) { 115 super(dobj, Children.LEAF); 116 } 117 118 public Action getPreferredAction() { 119 return SystemAction.get(SchemaViewOpenAction.class); 120 } 121 122 public void setName(String name, boolean rename) { 123 if (! rename || name != null && name.equals(this.getDataObject().getName())) { 124 return; 125 } 126 127 SchemaModel model = getModel(); 128 FileRenameRequest request = new FileRenameRequest(model, name); 129 try { 130 RefactoringManager.getInstance().execute(request, true); 131 } catch(CannotRefactorException ex) { 132 } catch(IOException ex) { 134 String msg = NbBundle.getMessage(SchemaDataObject.class, "MSG_UnableToRename", ex.getMessage()); 135 NotifyDescriptor nd = new NotifyDescriptor.Message( 136 msg, NotifyDescriptor.ERROR_MESSAGE); 137 DialogDisplayer.getDefault().notify(nd); 138 } 139 } 140 141 public SchemaModel getModel() { 142 try { 143 SchemaDataObject dobj = (SchemaDataObject) getDataObject(); 144 return dobj.getSchemaEditorSupport().getModel(); 145 } catch(IOException ex) { 146 String msg = NbBundle.getMessage(SchemaDataObject.class, "MSG_UnableToLoadSchema", ex.getMessage()); 147 NotifyDescriptor nd = new NotifyDescriptor.Message( 148 msg, NotifyDescriptor.ERROR_MESSAGE); 149 DialogDisplayer.getDefault().notify(nd); 150 } 151 return null; 152 } 153 } 154 155 @Override 156 public void setModified(boolean modified) { 157 super.setModified(modified); 158 if (modified) { 159 getCookieSet().add(getSaveCookie()); 160 } else { 161 getCookieSet().remove(getSaveCookie()); 162 } 163 } 164 165 private SaveCookie getSaveCookie() { 166 return new SaveCookie() { 167 public void save() throws IOException { 168 getSchemaEditorSupport().saveDocument(); 169 } 170 171 @Override 172 public int hashCode() { 173 return getClass().hashCode(); 174 } 175 176 @Override 177 public boolean equals(Object other) { 178 return other != null && getClass().equals(other.getClass()); 179 } 180 }; 181 } 182 183 @Override 184 public HelpCtx getHelpCtx() { 185 return HelpCtx.DEFAULT_HELP; 186 } 187 188 protected FileObject handleMove(DataFolder df) throws IOException { 189 if(isModified()) { 191 SaveCookie sCookie = (SaveCookie) this.getCookie(SaveCookie.class); 192 if(sCookie != null) { 193 sCookie.save(); 194 } 195 } 196 return super.handleMove(df); 197 } 198 199 203 public static final String SCHEMA_ICON_BASE_WITH_EXT = 204 "org/netbeans/modules/xml/schema/core/resources/Schema_File.png"; private static final long serialVersionUID = -8229569186860053169L; 206 207 208 private transient SchemaEditorSupport schemaEditorSupport; 212 } 213 | Popular Tags |