KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > web > core > jsploader > JspServletDataObject


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
20 package org.netbeans.modules.web.core.jsploader;
21
22 import java.beans.PropertyChangeEvent JavaDoc;
23 import java.beans.PropertyChangeListener JavaDoc;
24 import java.beans.PropertyChangeSupport JavaDoc;
25 import java.io.IOException JavaDoc;
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 /** Dataobject representing a servlet generated from a JSP page
39 *
40 * @author Petr Jiricka
41 */

42
43 public final class JspServletDataObject extends MultiDataObject {
44
45     public static final String JavaDoc EA_ORIGIN_JSP_PAGE = "NetBeansAttrOriginJspPage"; // NOI18N
46

47     private transient ServletEditorCookie servletEditor;
48     
49     public JspServletDataObject(FileObject pf, MultiFileLoader loader) throws DataObjectExistsException {
50         super(pf, loader);
51     }
52     
53     public @Override JavaDoc Node createNodeDelegate() {
54         return JavaDataSupport.createJavaNode(getPrimaryFile());
55     }
56
57     public @Override JavaDoc Cookie getCookie(Class JavaDoc 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     /** Get the name of the data object.
71     * Uses the name of the source JSP
72     * @return the name
73     */

74     public String JavaDoc getName () {
75         DataObject jsp = getSourceJspPage();
76         if (jsp == null)
77             return super.getName();
78         int markIndex = getPrimaryFile().getName().lastIndexOf(JspServletDataLoader.JSP_MARK);
79         String JavaDoc fileIndex = (markIndex == -1) ? "" : getPrimaryFile().getName().substring(
80                                markIndex + JspServletDataLoader.JSP_MARK.length());
81         if (fileIndex.startsWith("_")) // NOI18N
82
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 JavaDoc[] {fileIndex, jsp.getPrimaryFile().getName()});
88         }
89     }
90
91     /** Sets the source JSP page for this servlet */
92     public void setSourceJspPage(DataObject jspPage) throws IOException JavaDoc {
93         setSourceJspPage(getPrimaryFile(), jspPage);
94         firePropertyChange(PROP_COOKIE, null, null);
95     }
96     
97     public static void setSourceJspPage(FileObject generatedServlet, DataObject jspPage) throws IOException JavaDoc {
98         generatedServlet.setAttribute(EA_ORIGIN_JSP_PAGE, jspPage.getPrimaryFile());
99     }
100
101     /** Returns the source JSP page for this servlet */
102     public DataObject getSourceJspPage() {
103         Object JavaDoc 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) {//nothing to do
110
}
111             }
112         }
113         return null;
114     }
115     
116     private static class ServletEditorCookie implements EditorCookie.Observable, PropertyChangeListener JavaDoc {
117         
118         private EditorCookie original;
119         private JspServletDataObject servlet;
120         private EditorCookie currentEditor;
121         private PropertyChangeSupport JavaDoc pcs;
122         
123         public ServletEditorCookie(EditorCookie original, JspServletDataObject servlet) {
124             this.original = original;
125             this.servlet = servlet;
126             pcs = new PropertyChangeSupport JavaDoc(this);
127         }
128
129         private EditorCookie currentEditorCookie() {
130             EditorCookie newCurrent = computeCurrentEditorCookie();
131             if (currentEditor != newCurrent) {
132                 // re-register a property change listener to the new editor
133
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                 // remember the new editor
140
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         // implementation of EditorCookie
158
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 JavaDoc openDocument() throws java.io.IOException JavaDoc {
175             return currentEditorCookie().openDocument();
176         }
177
178         public javax.swing.text.StyledDocument JavaDoc getDocument() {
179             return currentEditorCookie().getDocument();
180         }
181
182         public void saveDocument() throws java.io.IOException JavaDoc {
183             currentEditorCookie().saveDocument();
184         }
185
186         public boolean isModified() {
187             return currentEditorCookie().isModified();
188         }
189
190         public javax.swing.JEditorPane JavaDoc[] getOpenedPanes() {
191             return currentEditorCookie().getOpenedPanes();
192         }
193
194         // implementation of EditorSupport.Observable
195

196         public void addPropertyChangeListener(PropertyChangeListener JavaDoc l) {
197             pcs.addPropertyChangeListener(l);
198         }
199         
200         public void removePropertyChangeListener(PropertyChangeListener JavaDoc l) {
201             pcs.removePropertyChangeListener(l);
202         }
203         
204         // implementation of PropertyChangeListener
205

206         public void propertyChange(PropertyChangeEvent JavaDoc evt) {
207             pcs.firePropertyChange(evt);
208         }
209         
210     }
211
212 }
213
214
Popular Tags