001: /**
002: * Copyright (C) 2005 Jeff Tassin
003: *
004: * This library is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 2.1 of the License, or (at your option) any later version.
008: *
009: * This library is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: */package com.jeta.swingbuilder.gui.beanmgr;
018:
019: import java.util.Collection;
020: import java.util.Iterator;
021: import java.util.LinkedList;
022:
023: import javax.swing.Icon;
024:
025: import com.jeta.forms.beanmgr.BeanManager;
026: import com.jeta.forms.components.border.TitledBorderBottom;
027: import com.jeta.forms.components.border.TitledBorderLabel;
028: import com.jeta.forms.components.border.TitledBorderSide;
029: import com.jeta.forms.components.colors.JETAColorWell;
030: import com.jeta.forms.components.line.HorizontalLineComponent;
031: import com.jeta.forms.components.line.VerticalLineComponent;
032: import com.jeta.forms.gui.beans.JETABeanFactory;
033: import com.jeta.forms.gui.common.FormException;
034: import com.jeta.forms.project.ProjectManager;
035: import com.jeta.open.i18n.I18N;
036: import com.jeta.open.registry.JETARegistry;
037: import com.jeta.swingbuilder.common.ComponentNames;
038: import com.jeta.swingbuilder.gui.utils.FormDesignerUtils;
039: import com.jeta.swingbuilder.interfaces.app.ObjectStore;
040: import com.jeta.swingbuilder.project.DefaultProjectManager;
041: import com.jeta.swingbuilder.resources.Icons;
042: import com.jeta.swingbuilder.store.ImportedBeanInfo;
043: import com.jeta.swingbuilder.store.ImportedBeansModel;
044: import com.jeta.swingbuilder.store.ProjectLevelImportedBeansModel;
045:
046: /**
047: * The bean manager is responsible for managing imported beans in the builder.
048: *
049: * @author Jeff Tassin
050: */
051: public class DefaultBeanManager implements BeanManager {
052: /**
053: * The class loader for the beans
054: */
055: private BeanLoader m_loader;
056:
057: /**
058: * The class loader for the beans
059: */
060: private BeanLoader m_project_loader;
061:
062: /**
063: * The underyling data model
064: */
065: private ImportedBeansModel m_ibm;
066:
067: /**
068: * The underyling data model
069: */
070: private ProjectLevelImportedBeansModel m_plibm;
071:
072: /**
073: * A collection of DefaultBean objects. These are the standard Swing
074: * components supported by the designer.
075: */
076: private LinkedList m_default_beans = new LinkedList();
077:
078: /**
079: * ctor
080: */
081: public DefaultBeanManager() {
082: try {
083: ObjectStore os = (ObjectStore) JETARegistry
084: .lookup(ComponentNames.APPLICATION_STATE_STORE);
085: m_ibm = (ImportedBeansModel) os
086: .load(ImportedBeansModel.COMPONENT_ID);
087: DefaultProjectManager projectMgr = (DefaultProjectManager) JETARegistry
088: .lookup(ProjectManager.COMPONENT_ID);
089: if ((projectMgr != null)
090: && (projectMgr.getProject() != null)) {
091: m_plibm = projectMgr.getProject()
092: .getProjectLevelImportedBeansModel();
093: }
094: } catch (Exception e) {
095: e.printStackTrace();
096: }
097: if (m_ibm == null) {
098: m_ibm = new ImportedBeansModel();
099: }
100: if (m_plibm == null) {
101: m_plibm = new ProjectLevelImportedBeansModel();
102: }
103:
104: registerDefaultBeans();
105: registerBeans(m_ibm, m_plibm);
106: }
107:
108: /**
109: * @return the underlying class loader for loading imported beans. This can
110: * be null
111: */
112: public ClassLoader getClassLoader() throws FormException {
113: return getBeanLoader().getClassLoader();
114: }
115:
116: public BeanLoader getBeanLoader() {
117: if (m_loader == null) {
118: Collection urls = m_ibm.getUrls();
119: m_loader = new BeanLoader(urls);
120: }
121: return m_loader;
122: }
123:
124: private BeanLoader getProjectLevelBeanLoader() {
125: if (m_project_loader == null) {
126: Collection urls = m_plibm.getUrls();
127: m_project_loader = new BeanLoader(urls);
128: }
129: return m_project_loader;
130: }
131:
132: /**
133: * @return the class for the given bean class name
134: */
135: public Class getBeanClass(String beanClassName)
136: throws FormException {
137: Throwable throwable = null;
138: try {
139: Class clazz = null;
140: try {
141: clazz = getBeanLoader().getClass(beanClassName);
142: if (clazz != null) {
143: return clazz;
144: }
145: } catch (Throwable t) {
146: throwable = t;
147: }
148: try {
149: clazz = getProjectLevelBeanLoader().getClass(
150: beanClassName);
151: if (clazz != null) {
152: return clazz;
153: }
154: } catch (Throwable t) {
155: throwable = t;
156: }
157:
158: if (throwable != null)
159: throw throwable;
160:
161: return clazz;
162: } catch (Throwable t) {
163: if (t instanceof FormException)
164: throw (FormException) t;
165: else {
166: t.printStackTrace();
167: throw new FormException(t.getMessage(), null);
168: }
169: }
170: }
171:
172: /**
173: * @return the default beans supported by the application.
174: */
175: public Collection getDefaultBeans() {
176: return m_default_beans;
177: }
178:
179: /**
180: * @return the imported beans in the model
181: */
182: public Collection getImportedBeans() {
183: Collection beans = new LinkedList();
184: if (m_ibm != null)
185: beans.addAll(m_ibm.getImportedBeans());
186: if (m_plibm != null)
187: beans.addAll(m_plibm.getImportedBeans());
188: return beans;
189: }
190:
191: /**
192: * @return the underlying data model
193: */
194: public ImportedBeansModel getModel() {
195: return m_ibm;
196: }
197:
198: /**
199: * Registers all imported beans with the JETABeanFactory
200: */
201: private void registerBeans(ImportedBeansModel ibm,
202: ProjectLevelImportedBeansModel plibm) {
203: try {
204: if ((ibm != null) || (plibm != null)) {
205: JETABeanFactory.clearCustomFactories();
206:
207: if (ibm != null) {
208: Collection beans = ibm.getImportedBeans();
209: Iterator iter = beans.iterator();
210: while (iter.hasNext()) {
211: ImportedBeanInfo bi = (ImportedBeanInfo) iter
212: .next();
213: try {
214: JETABeanFactory.tryRegisterCustomFactory(
215: getBeanLoader().getClass(
216: bi.getBeanName()), bi
217: .isScrollable());
218: } catch (Throwable e) {
219: e.printStackTrace();
220: }
221: }
222: }
223:
224: if (plibm != null) {
225: Collection beans = plibm.getImportedBeans();
226: Iterator iter = beans.iterator();
227: while (iter.hasNext()) {
228: ImportedBeanInfo bi = (ImportedBeanInfo) iter
229: .next();
230: try {
231: JETABeanFactory
232: .tryRegisterCustomFactory(
233: getProjectLevelBeanLoader()
234: .getClass(
235: bi
236: .getBeanName()),
237: bi.isScrollable());
238: } catch (Throwable e) {
239: e.printStackTrace();
240: }
241: }
242: }
243: }
244: } catch (Throwable e) {
245: e.printStackTrace();
246: }
247: }
248:
249: /**
250: * Registers a default bean used by the designer.
251: */
252: private void registerDefaultBean(String description,
253: String className, Icon icon) {
254: DefaultBean db = new DefaultBean(description, className, icon);
255: m_default_beans.add(db);
256: }
257:
258: /**
259: * Loads the default Swing components supported by the application
260: */
261: private void registerDefaultBeans() {
262: registerDefaultBean(I18N.getLocalizedMessage("JLabel"),
263: com.jeta.forms.components.label.JETALabel.class
264: .getName(), FormDesignerUtils
265: .loadImage(Icons.LABEL_16));
266:
267: registerDefaultBean(I18N.getLocalizedMessage("JRadioButton"),
268: "javax.swing.JRadioButton", FormDesignerUtils
269: .loadImage(Icons.RADIO_16));
270:
271: registerDefaultBean(I18N.getLocalizedMessage("JCheckBox"),
272: "javax.swing.JCheckBox", FormDesignerUtils
273: .loadImage(Icons.CHECK_16));
274:
275: registerDefaultBean(I18N.getLocalizedMessage("JButton"),
276: "javax.swing.JButton", FormDesignerUtils
277: .loadImage(Icons.BUTTON_16));
278:
279: registerDefaultBean(I18N.getLocalizedMessage("JToggleButton"),
280: "javax.swing.JToggleButton", FormDesignerUtils
281: .loadImage(Icons.TOGGLE_BUTTON_16));
282:
283: registerDefaultBean(I18N.getLocalizedMessage("JComboBox"),
284: "javax.swing.JComboBox", FormDesignerUtils
285: .loadImage(Icons.COMBO_16));
286:
287: registerDefaultBean(I18N.getLocalizedMessage("JList"),
288: "javax.swing.JList", FormDesignerUtils
289: .loadImage(Icons.LIST_16));
290:
291: registerDefaultBean(I18N.getLocalizedMessage("JTable"),
292: "javax.swing.JTable", FormDesignerUtils
293: .loadImage(Icons.TABLE_16));
294:
295: registerDefaultBean(I18N.getLocalizedMessage("JTree"),
296: "javax.swing.JTree", FormDesignerUtils
297: .loadImage(Icons.TREE_16));
298:
299: registerDefaultBean(I18N.getLocalizedMessage("JProgressBar"),
300: "javax.swing.JProgressBar", FormDesignerUtils
301: .loadImage(Icons.PROGRESS_BAR_16));
302:
303: registerDefaultBean(I18N.getLocalizedMessage("JSlider"),
304: "javax.swing.JSlider", FormDesignerUtils
305: .loadImage(Icons.SLIDER_16));
306:
307: registerDefaultBean(I18N.getLocalizedMessage("JSpinner"),
308: "javax.swing.JSpinner", FormDesignerUtils
309: .loadImage(Icons.SPINNER_16));
310:
311: registerDefaultBean(I18N.getLocalizedMessage("JTextField"),
312: "javax.swing.JTextField", FormDesignerUtils
313: .loadImage(Icons.TEXT_FIELD_16));
314:
315: registerDefaultBean(I18N.getLocalizedMessage("JPasswordField"),
316: "javax.swing.JPasswordField", FormDesignerUtils
317: .loadImage(Icons.PASSWORD_FIELD_16));
318:
319: registerDefaultBean(I18N
320: .getLocalizedMessage("JFormattedTextField"),
321: "javax.swing.JFormattedTextField", FormDesignerUtils
322: .loadImage(Icons.FORMATTED_FIELD_16));
323:
324: registerDefaultBean(I18N.getLocalizedMessage("JTextArea"),
325: "javax.swing.JTextArea", FormDesignerUtils
326: .loadImage(Icons.TEXT_16));
327:
328: registerDefaultBean(I18N.getLocalizedMessage("JEditorPane"),
329: "javax.swing.JEditorPane", FormDesignerUtils
330: .loadImage(Icons.RICH_TEXT_16));
331:
332: registerDefaultBean(I18N.getLocalizedMessage("JTabbedPane"),
333: "javax.swing.JTabbedPane", FormDesignerUtils
334: .loadImage(Icons.TABPANE_16));
335:
336: registerDefaultBean(
337: I18N.getLocalizedMessage("Horizontal Line"),
338: HorizontalLineComponent.class.getName(),
339: FormDesignerUtils.loadImage(Icons.HORIZONTAL_LINE_16));
340:
341: registerDefaultBean(I18N.getLocalizedMessage("Vertical Line"),
342: VerticalLineComponent.class.getName(),
343: FormDesignerUtils.loadImage(Icons.VERTICAL_LINE_16));
344:
345: registerDefaultBean(I18N.getLocalizedMessage("Color Well"),
346: JETAColorWell.class.getName(), FormDesignerUtils
347: .loadImage(Icons.COLOR_WELL_16));
348:
349: registerDefaultBean(I18N.getLocalizedMessage("Image"),
350: "com.jeta.forms.components.image.ImageComponent",
351: FormDesignerUtils.loadImage(Icons.PORTRAIT_16));
352:
353: registerDefaultBean(I18N
354: .getLocalizedMessage("Titled Border Label"),
355: TitledBorderLabel.class.getName(), FormDesignerUtils
356: .loadImage(Icons.TITLE_BORDER_LABEL_16));
357:
358: registerDefaultBean(I18N
359: .getLocalizedMessage("Titled Border Bottom"),
360: TitledBorderBottom.class.getName(), FormDesignerUtils
361: .loadImage(Icons.TITLE_BORDER_BOTTOM_16));
362:
363: registerDefaultBean(I18N
364: .getLocalizedMessage("Titled Border Side"),
365: TitledBorderSide.class.getName(), FormDesignerUtils
366: .loadImage(Icons.TITLE_BORDER_SIDE_16));
367:
368: registerDefaultBean(
369: I18N.getLocalizedMessage("JGoodies Separator"),
370: com.jeta.forms.components.separator.TitledSeparator.class
371: .getName(), FormDesignerUtils
372: .loadImage(Icons.JGOODIES_SEPARATOR_16));
373: }
374:
375: public void setModel(ImportedBeansModel ibm) {
376: if (ibm == null) {
377: m_ibm = new ImportedBeansModel();
378: } else {
379: m_ibm = ibm;
380: }
381: m_loader = null;
382: try {
383: registerBeans(m_ibm, m_plibm);
384:
385: ObjectStore os = (ObjectStore) JETARegistry
386: .lookup(ComponentNames.APPLICATION_STATE_STORE);
387: os.store(ImportedBeansModel.COMPONENT_ID, m_ibm);
388: os.flush(ImportedBeansModel.COMPONENT_ID);
389: } catch (Exception e) {
390: e.printStackTrace();
391: }
392: }
393:
394: public void setModel(ProjectLevelImportedBeansModel plibm) {
395: if (plibm == null) {
396: DefaultProjectManager projectMgr = (DefaultProjectManager) JETARegistry
397: .lookup(ProjectManager.COMPONENT_ID);
398: if ((projectMgr != null)
399: && (projectMgr.getProject() != null)) {
400: m_plibm = projectMgr.getProject()
401: .getProjectLevelImportedBeansModel();
402: } else {
403: m_plibm = new ProjectLevelImportedBeansModel();
404: }
405: } else {
406: m_plibm = plibm;
407: }
408: m_project_loader = null;
409: try {
410: registerBeans(m_ibm, m_plibm);
411: } catch (Exception e) {
412: e.printStackTrace();
413: }
414: }
415:
416: }
|