001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.beans.beaninfo;
043:
044: import org.netbeans.api.editor.guards.GuardedSectionManager;
045: import org.netbeans.api.editor.guards.InteriorSection;
046: import org.netbeans.api.editor.guards.SimpleSection;
047: import org.netbeans.modules.beans.GenerateBeanException;
048: import org.openide.filesystems.FileObject;
049: import org.openide.filesystems.Repository;
050: import org.openide.loaders.DataObject;
051: import org.openide.loaders.DataFolder;
052: import org.openide.loaders.DataObjectNotFoundException;
053:
054: /**
055: * Finds or creates BeanInfo source elemnet for the class.
056: * It can regenerate the source if there are the guarded blocks.
057: * @author Petr Hrebejk
058: */
059:
060: public final class BeanInfoSource extends Object {
061:
062: private static final String BEANINFO_NAME_EXT = "BeanInfo"; // NOI18N
063:
064: private static final String DESCRIPTOR_SECTION = "BeanDescriptor"; // NOI18N
065: private static final String PROPERTIES_SECTION = "Properties"; // NOI18N
066: private static final String EVENTSETS_SECTION = "Events"; // NOI18N
067: private static final String ICONS_SECTION = "Icons"; // NOI18N
068: private static final String IDX_SECTION = "Idx"; // NOI18N
069: private static final String METHODS_SECTION = "Methods"; // NOI18N
070: private static final String SUPERCLASS_SECTION = "Superclass"; // NOI18N
071:
072: private DataObject javaDataObject;
073: private DataObject biDataObject = null;
074: private BIEditorSupport javaEditor = null;
075:
076: /** Creates new BeanInfoSource */
077: public BeanInfoSource(FileObject javafile)
078: throws GenerateBeanException {
079:
080: findBeanInfo(javafile);
081: }
082:
083: /** Returns wether the bean info exists or not */
084: boolean exists() {
085: return biDataObject != null;
086: }
087:
088: /** Checks wether the bean info object has Guarded sections i.e.
089: * was created from netbeans template.
090: */
091: boolean isNbBeanInfo() {
092:
093: GuardedSectionManager guards = null;
094: if (!exists()
095: || javaEditor == null
096: || null == (guards = javaEditor
097: .getGuardedSectionManager())) {
098: return false;
099: }
100:
101: //JavaEditor.InteriorSection dis = javaEditor.findInteriorSection( DESCRIPTOR_SECTION );
102: InteriorSection pis = guards
103: .findInteriorSection(PROPERTIES_SECTION);
104: InteriorSection eis = guards
105: .findInteriorSection(EVENTSETS_SECTION);
106: // JavaEditor.InteriorSection mis = javaEditor.findInteriorSection( METHODS_SECTION );
107: //JavaEditor.SimpleSection iss = javaEditor.findSimpleSection( ICONS_SECTION );
108: SimpleSection dss = guards.findSimpleSection(IDX_SECTION);
109:
110: //return ( pis != null && eis != null && iss != null && dss != null);
111: return (pis != null && eis != null && dss != null);
112: }
113:
114: boolean hasIconInfo() {
115: GuardedSectionManager guards = javaEditor
116: .getGuardedSectionManager();
117: if (guards == null) {
118: return false;
119: }
120: SimpleSection iss = guards.findSimpleSection(ICONS_SECTION);
121: return (iss != null);
122: }
123:
124: /** Checks wether the bean descriptor object has Guarded sections i.e.
125: * was created from new netbeans template.
126: */
127: boolean isNbBeanInfoDescriptor() {
128:
129: GuardedSectionManager guards = null;
130: if (!exists()
131: || javaEditor == null
132: || null == (guards = javaEditor
133: .getGuardedSectionManager())) {
134: return false;
135: }
136: InteriorSection dis = guards
137: .findInteriorSection(DESCRIPTOR_SECTION);
138: return (dis != null);
139: }
140:
141: /** Checks wether the bean info object has Guarded sections for superclass i.e.
142: * was created from new netbeans template.
143: */
144: boolean isNbSuperclass() {
145:
146: GuardedSectionManager guards = null;
147: if (!exists()
148: || javaEditor == null
149: || null == (guards = javaEditor
150: .getGuardedSectionManager())) {
151: return false;
152: }
153: InteriorSection dis = guards
154: .findInteriorSection(SUPERCLASS_SECTION);
155: return (dis != null);
156: }
157:
158: /** Finds the bean info for classElement asspciated with this
159: object */
160: void findBeanInfo(FileObject javafile) throws GenerateBeanException {
161:
162: javaEditor = null;
163: try {
164: this .javaDataObject = DataObject.find(javafile);
165: FileObject parent = javafile.getParent();
166: FileObject bifile = parent.getFileObject(javafile.getName()
167: + BEANINFO_NAME_EXT, "java"); // NOI18N
168: if (bifile != null) {
169: biDataObject = DataObject.find(bifile);
170: javaEditor = biDataObject.getLookup().lookup(
171: BIEditorSupport.class);
172: }
173: } catch (DataObjectNotFoundException ex) {
174: throw new GenerateBeanException();
175: // Do nothing if no data object is found
176: }
177: }
178:
179: /** Deletes the BeanInfo */
180: void delete() throws java.io.IOException {
181: biDataObject.delete();
182: }
183:
184: /** Creates beanInfo data object */
185: void createFromTemplate(boolean iconBlock) {
186: FileObject foTemplates = Repository.getDefault()
187: .getDefaultFileSystem().findResource("Templates"); //NOI18N ;
188: if (foTemplates == null) {
189: return;
190: }
191:
192: FileObject foClassTemplates = foTemplates
193: .getFileObject("Beans"); // NOI18N
194: if (foClassTemplates == null) {
195: return;
196: }
197:
198: FileObject foBiTemplate = null;
199:
200: if (iconBlock) {
201: foBiTemplate = foClassTemplates.getFileObject("BeanInfo",
202: "java"); // NOI18N
203: } else {
204: foBiTemplate = foClassTemplates.getFileObject(
205: "NoIconBeanInfo", "java"); // NOI18N
206: }
207:
208: if (foBiTemplate == null) {
209: return;
210: }
211:
212: try {
213: DataObject doBiTemplate = DataObject.find(foBiTemplate);
214: DataFolder folder = this .javaDataObject.getFolder();
215: biDataObject = doBiTemplate.createFromTemplate(folder,
216: this .javaDataObject.getName() + BEANINFO_NAME_EXT);
217: javaEditor = biDataObject.getLookup().lookup(
218: BIEditorSupport.class);
219: } catch (org.openide.loaders.DataObjectNotFoundException e) {
220: //System.out.println ( e );
221: // Do nothing if no data object is found
222: } catch (java.io.IOException e) {
223: //System.out.println ( e );
224: // Do nothing if no data object is found
225: }
226: }
227:
228: /** If the bean info is available returns the bean info data object */
229: DataObject getDataObject() {
230: return biDataObject;
231: }
232:
233: DataObject getSourceDataObject() {
234: return javaDataObject;
235: }
236:
237: /** opens the source */
238: void open() {
239: javaEditor.open();
240: }
241:
242: /** Sets the header and bottom of properties section */
243: void setDescriptorSection(String header, String bottom) {
244: setInteriorSection(DESCRIPTOR_SECTION, header, bottom);
245: }
246:
247: /** Gets the header of properties setion */
248: String getDescriptorSection() {
249: GuardedSectionManager guards = javaEditor
250: .getGuardedSectionManager();
251: InteriorSection is = guards
252: .findInteriorSection(DESCRIPTOR_SECTION);
253:
254: if (is != null) {
255: return is.getText();
256: } else
257: return null;
258:
259: }
260:
261: /** Sets the header and bottom of properties section */
262: void setPropertiesSection(String header, String bottom) {
263: setInteriorSection(PROPERTIES_SECTION, header, bottom);
264: }
265:
266: /** Gets the header of properties setion */
267: String getPropertiesSection() {
268: return getInteriorSection(PROPERTIES_SECTION);
269: }
270:
271: /** Sets the header and bottom of methods section */
272: void setMethodsSection(String header, String bottom) {
273: setInteriorSection(METHODS_SECTION, header, bottom);
274: }
275:
276: /** Gets the header of properties setion */
277: String getMethodsSection() {
278: return getInteriorSection(METHODS_SECTION);
279: }
280:
281: /** Sets the header and bottom of event sets section */
282: void setEventSetsSection(String header, String bottom) {
283: setInteriorSection(EVENTSETS_SECTION, header, bottom);
284: }
285:
286: /** Gets the header of properties setion */
287: String getEventSetsSection() {
288: return getInteriorSection(EVENTSETS_SECTION);
289: }
290:
291: /** Gets the header of properties setion */
292: String getIconsSection() {
293: return getSimpleSection(ICONS_SECTION);
294: }
295:
296: /** Sets the header of properties setion */
297: void setIconsSection(String text) {
298: setSimpleSection(ICONS_SECTION, text);
299: }
300:
301: /** Gets the header of properties setion */
302: String getDefaultIdxSection() {
303: return getSimpleSection(IDX_SECTION);
304: }
305:
306: /** Sets the header of properties setion */
307: void setDefaultIdxSection(String text) {
308: setSimpleSection(IDX_SECTION, text);
309: }
310:
311: /** Sets the header and bottom of properties section */
312: void setSuperclassSection(String header, String bottom) {
313: setInteriorSection(SUPERCLASS_SECTION, header, bottom);
314: }
315:
316: /** Gets the header of properties setion */
317: String getSuperclassSection() {
318: return getInteriorSection(SUPERCLASS_SECTION);
319: }
320:
321: private void setInteriorSection(String section, String header,
322: String bottom) {
323: GuardedSectionManager guards = javaEditor
324: .getGuardedSectionManager();
325: if (guards == null) {
326: return;
327: }
328: InteriorSection is = guards.findInteriorSection(section);
329:
330: if (is != null) {
331: is.setHeader(header);
332: is.setFooter(bottom);
333: }
334: }
335:
336: private String getInteriorSection(String section) {
337: GuardedSectionManager guards = javaEditor
338: .getGuardedSectionManager();
339: if (guards == null) {
340: return null;
341: }
342: InteriorSection is = guards.findInteriorSection(section);
343: return is == null ? null : is.getText();
344: }
345:
346: private void setSimpleSection(String section, String text) {
347: GuardedSectionManager guards = javaEditor
348: .getGuardedSectionManager();
349: if (guards == null) {
350: return;
351: }
352: SimpleSection ss = guards.findSimpleSection(section);
353:
354: if (ss != null) {
355: ss.setText(text);
356: }
357: }
358:
359: private String getSimpleSection(String section) {
360: GuardedSectionManager guards = javaEditor
361: .getGuardedSectionManager();
362: if (guards == null) {
363: return null;
364: }
365: SimpleSection ss = guards.findSimpleSection(section);
366: return ss == null ? null : ss.getText();
367: }
368:
369: /*
370: void regenerateMethods() {
371: JavaEditor.InteriorSection is = javaEditor.findInteriorSection( "Events" );
372:
373: if ( is != null ) {
374: is.setHeader( BeanInfoGenerator.generateMethods( classElement.getName().getName(), methods ) );
375: is.setBottom( BeanInfoGenerator.generateMethodsBottom( methods ) );
376: }
377: }
378: */
379:
380: }
|