1 19 20 package org.netbeans.modules.web.core.jsploader; 21 22 import java.beans.PropertyChangeEvent ; 23 import java.beans.PropertyChangeListener ; 24 import java.beans.PropertyChangeSupport ; 25 import java.io.IOException ; 26 27 import org.openide.cookies.*; 28 import org.openide.filesystems.*; 29 import org.openide.loaders.*; 30 import org.openide.nodes.Node; 31 import org.openide.nodes.Node.Cookie; 32 import org.openide.util.Task; 33 import org.openide.text.Line; 34 import org.openide.util.NbBundle; 35 36 import org.netbeans.api.java.loaders.JavaDataSupport; 37 38 42 43 public final class JspServletDataObject extends MultiDataObject { 44 45 public static final String EA_ORIGIN_JSP_PAGE = "NetBeansAttrOriginJspPage"; 47 private transient ServletEditorCookie servletEditor; 48 49 public JspServletDataObject(FileObject pf, MultiFileLoader loader) throws DataObjectExistsException { 50 super(pf, loader); 51 } 52 53 public @Override Node createNodeDelegate() { 54 return JavaDataSupport.createJavaNode(getPrimaryFile()); 55 } 56 57 public @Override Cookie getCookie(Class type) { 58 if (type.isAssignableFrom(ServletEditorCookie.class)) { 59 if (servletEditor == null) { 60 EditorCookie ed = (EditorCookie) super.getCookie(EditorCookie.class); 61 if (ed != null) 62 servletEditor = new ServletEditorCookie(ed, this); 63 } 64 if (servletEditor != null) 65 return servletEditor; 66 } 67 return super.getCookie(type); 68 } 69 70 74 public String getName () { 75 DataObject jsp = getSourceJspPage(); 76 if (jsp == null) 77 return super.getName(); 78 int markIndex = getPrimaryFile().getName().lastIndexOf(JspServletDataLoader.JSP_MARK); 79 String fileIndex = (markIndex == -1) ? "" : getPrimaryFile().getName().substring( 80 markIndex + JspServletDataLoader.JSP_MARK.length()); 81 if (fileIndex.startsWith("_")) fileIndex = fileIndex.substring(1); 83 if ("".equals(fileIndex)) { 84 return NbBundle.getMessage(JspServletDataObject.class, "LBL_ServletDisplayNameNoNumber", jsp.getPrimaryFile().getName()); 85 } 86 else { 87 return NbBundle.getMessage(JspServletDataObject.class, "LBL_ServletDisplayName", new Object [] {fileIndex, jsp.getPrimaryFile().getName()}); 88 } 89 } 90 91 92 public void setSourceJspPage(DataObject jspPage) throws IOException { 93 setSourceJspPage(getPrimaryFile(), jspPage); 94 firePropertyChange(PROP_COOKIE, null, null); 95 } 96 97 public static void setSourceJspPage(FileObject generatedServlet, DataObject jspPage) throws IOException { 98 generatedServlet.setAttribute(EA_ORIGIN_JSP_PAGE, jspPage.getPrimaryFile()); 99 } 100 101 102 public DataObject getSourceJspPage() { 103 Object obj = getPrimaryFile().getAttribute(EA_ORIGIN_JSP_PAGE); 104 if (obj instanceof DataObject) return (DataObject)obj; 105 if (obj instanceof FileObject) { 106 if (((FileObject)obj).isValid()) { 107 try { 108 return DataObject.find((FileObject)obj); 109 } catch (DataObjectNotFoundException e) { } 111 } 112 } 113 return null; 114 } 115 116 private static class ServletEditorCookie implements EditorCookie.Observable, PropertyChangeListener { 117 118 private EditorCookie original; 119 private JspServletDataObject servlet; 120 private EditorCookie currentEditor; 121 private PropertyChangeSupport pcs; 122 123 public ServletEditorCookie(EditorCookie original, JspServletDataObject servlet) { 124 this.original = original; 125 this.servlet = servlet; 126 pcs = new PropertyChangeSupport (this); 127 } 128 129 private EditorCookie currentEditorCookie() { 130 EditorCookie newCurrent = computeCurrentEditorCookie(); 131 if (currentEditor != newCurrent) { 132 if ((currentEditor != null) && (currentEditor instanceof EditorCookie.Observable)) { 134 ((EditorCookie.Observable)currentEditor).removePropertyChangeListener(this); 135 } 136 if ((newCurrent != null) && (newCurrent instanceof EditorCookie.Observable)) { 137 ((EditorCookie.Observable)newCurrent).addPropertyChangeListener(this); 138 } 139 currentEditor = newCurrent; 141 } 142 return currentEditor; 143 } 144 145 private EditorCookie computeCurrentEditorCookie() { 146 DataObject jsp = servlet.getSourceJspPage(); 147 if ((jsp != null) && (jsp instanceof JspDataObject)) { 148 if (((JspDataObject)jsp).getServletDataObject() == servlet) { 149 EditorCookie newCookie = ((JspDataObject) jsp).getServletEditor(); 150 if (newCookie != null) 151 return newCookie; 152 } 153 } 154 return original; 155 } 156 157 public Line.Set getLineSet() { 159 return currentEditorCookie().getLineSet(); 160 } 161 162 public void open() { 163 currentEditorCookie().open(); 164 } 165 166 public boolean close() { 167 return currentEditorCookie().close(); 168 } 169 170 public Task prepareDocument() { 171 return currentEditorCookie().prepareDocument(); 172 } 173 174 public javax.swing.text.StyledDocument openDocument() throws java.io.IOException { 175 return currentEditorCookie().openDocument(); 176 } 177 178 public javax.swing.text.StyledDocument getDocument() { 179 return currentEditorCookie().getDocument(); 180 } 181 182 public void saveDocument() throws java.io.IOException { 183 currentEditorCookie().saveDocument(); 184 } 185 186 public boolean isModified() { 187 return currentEditorCookie().isModified(); 188 } 189 190 public javax.swing.JEditorPane [] getOpenedPanes() { 191 return currentEditorCookie().getOpenedPanes(); 192 } 193 194 196 public void addPropertyChangeListener(PropertyChangeListener l) { 197 pcs.addPropertyChangeListener(l); 198 } 199 200 public void removePropertyChangeListener(PropertyChangeListener l) { 201 pcs.removePropertyChangeListener(l); 202 } 203 204 206 public void propertyChange(PropertyChangeEvent evt) { 207 pcs.firePropertyChange(evt); 208 } 209 210 } 211 212 } 213 214 | Popular Tags |