1 19 20 package org.netbeans.modules.xml.catalog; 21 22 import java.beans.IntrospectionException ; 23 import java.io.File ; 24 import java.io.IOException ; 25 import java.net.MalformedURLException ; 26 import java.net.URL ; 27 import javax.swing.Action ; 28 import org.netbeans.modules.xml.catalog.lib.URLEnvironment; 29 import org.netbeans.modules.xml.catalog.spi.CatalogReader; 30 import org.netbeans.modules.xml.catalog.spi.CatalogWriter; 31 import org.netbeans.modules.xml.catalog.user.UserXMLCatalog; 32 import org.openide.ErrorManager; 33 import org.openide.NotifyDescriptor; 34 import org.openide.actions.DeleteAction; 35 import org.openide.actions.EditAction; 36 import org.openide.actions.PropertiesAction; 37 import org.openide.actions.ViewAction; 38 import org.openide.cookies.EditCookie; 39 import org.openide.cookies.ViewCookie; 40 import org.openide.filesystems.FileObject; 41 import org.openide.filesystems.FileUtil; 42 import org.openide.loaders.DataObject; 43 import org.openide.nodes.BeanNode; 44 import org.openide.nodes.Node; 45 import org.openide.text.CloneableEditor; 46 import org.openide.text.CloneableEditorSupport; 47 import org.openide.text.CloneableEditorSupport.Env; 48 import org.openide.util.HelpCtx; 49 import org.openide.util.actions.SystemAction; 50 51 57 final class CatalogEntryNode extends BeanNode implements EditCookie { 58 59 private transient ViewCookie view; 61 private boolean isCatalogWriter; 62 private CatalogReader catalogReader; 63 64 65 public CatalogEntryNode(CatalogEntry entry) throws IntrospectionException { 66 super(entry); 67 catalogReader = entry.getCatalog(); 69 if (catalogReader instanceof CatalogWriter) { 70 isCatalogWriter = true; 71 } 72 } 73 74 public javax.swing.Action getPreferredAction() { 75 if (isCatalogWriter) 76 return SystemAction.get(EditAction.class); 77 else 78 return SystemAction.get(ViewAction.class); 79 } 80 81 public void edit() { 82 UserXMLCatalog catalog = (UserXMLCatalog)getCatalogReader(); 83 try { 84 java.net.URI uri = new java.net.URI (getSystemID()); 85 File file = new File (uri); 86 FileObject fo = FileUtil.toFileObject(file); 87 boolean editPossible=false; 88 if (fo!=null) { 89 DataObject obj = DataObject.find(fo); 90 EditCookie editCookie = (EditCookie)obj.getCookie(EditCookie.class); 91 if (editCookie!=null) { 92 editPossible=true; 93 editCookie.edit(); 94 } 95 } 96 if (!editPossible) 97 org.openide.DialogDisplayer.getDefault().notify( 98 new NotifyDescriptor.Message( 99 Util.THIS.getString("MSG_CannotOpenURI",getSystemID()), NotifyDescriptor.INFORMATION_MESSAGE)); 101 } catch (Throwable ex) { 102 ErrorManager.getDefault().notify(ex); 103 } 104 } 105 106 private CatalogReader getCatalogReader() { 107 return catalogReader; 108 } 109 110 public Action [] getActions(boolean context) { 111 if (isCatalogWriter) 112 return new Action [] { 113 SystemAction.get(EditAction.class), 114 SystemAction.get(DeleteAction.class), 115 null, 116 SystemAction.get(PropertiesAction.class) 117 }; 118 else 119 return new Action [] { 120 SystemAction.get(ViewAction.class), 121 null, 122 SystemAction.get(PropertiesAction.class) 123 }; 124 } 125 126 130 public Node.Cookie getCookie(Class clazz) { 131 132 if (ViewCookie.class.equals(clazz)) { 133 134 try { 135 String sys = getSystemID(); 136 if (sys == null) return null; 137 138 if (view == null) { 139 URL url = new URL (sys); 140 ViewEnv env = new ViewEnv(url); 141 view = new ViewCookieImpl(env); 142 } 143 return view; 144 145 } catch (MalformedURLException ex) { 146 ErrorManager emgr = ErrorManager.getDefault(); 147 emgr.notify(ErrorManager.INFORMATIONAL, ex); 148 return null; 149 } catch (IOException ex) { 150 ErrorManager emgr = ErrorManager.getDefault(); 151 emgr.notify(ErrorManager.INFORMATIONAL, ex); 152 return null; 153 } 154 155 } else { 156 return super.getCookie(clazz); 157 } 158 } 159 160 161 public HelpCtx getHelpCtx() { 162 return HelpCtx.DEFAULT_HELP; 164 } 165 166 private String getPublicID() { 167 return ((CatalogEntry)getBean()).getPublicID(); 168 } 169 170 private String getSystemID() { 171 return ((CatalogEntry)getBean()).getSystemID(); 172 } 173 174 public String getShortDescription() { 175 return getSystemID(); 176 } 177 178 public void destroy() throws IOException { 179 super.destroy(); 180 if (isCatalogWriter) { 181 CatalogWriter catalogWriter = (CatalogWriter)((CatalogEntry)getBean()).getCatalog(); 182 catalogWriter.registerCatalogEntry(getPublicID(),null); 183 } 184 } 185 186 187 191 private class ViewCookieImpl extends CloneableEditorSupport implements ViewCookie { 192 193 ViewCookieImpl(Env env) { 194 super(env); 195 } 196 197 protected String messageName() { 198 return Util.THIS.getString("MSG_opened_entity", getPublicID()); } 200 201 protected String messageSave() { 202 return Util.THIS.getString ("MSG_ENTITY_SAVE", getPublicID()); } 204 205 protected java.lang.String messageToolTip() { 206 return Util.THIS.getString ("MSG_ENTITY_TOOLTIP", getSystemID()); } 208 209 protected java.lang.String messageOpening() { 210 return Util.THIS.getString ("MSG_ENTITY_OPENING", getPublicID()); } 212 213 protected java.lang.String messageOpened() { 214 return Util.THIS.getString ("MSG_ENTITY_OPENED", getPublicID()); } 216 217 protected CloneableEditor createCloneableEditor() { 219 CloneableEditor editor = super.createCloneableEditor(); 220 editor.setActivatedNodes(new Node[] {CatalogEntryNode.this}); 221 return editor; 222 } 223 224 227 private Object writeReplace() { 228 return null; 229 } 230 231 } 232 233 234 236 239 private class ViewEnv extends URLEnvironment { 240 241 242 private static final long serialVersionUID =-5031004511063404433L; 243 244 ViewEnv (URL url) { 245 super(url); 246 } 247 248 public org.openide.windows.CloneableOpenSupport findCloneableOpenSupport() { 249 return (ViewCookieImpl) CatalogEntryNode.this.getCookie(ViewCookieImpl.class); 250 } 251 } 252 253 } 254 | Popular Tags |