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: */
018:
019: package com.jeta.swingbuilder.gui.beanmgr;
020:
021: import java.awt.Component;
022:
023: import com.jeta.forms.gui.common.FormException;
024: import com.jeta.open.i18n.I18N;
025: import com.jeta.open.rules.JETARule;
026: import com.jeta.open.rules.RuleResult;
027: import com.jeta.swingbuilder.gui.components.TSErrorDialog2;
028: import com.jeta.swingbuilder.gui.utils.FormDesignerUtils;
029:
030: /**
031: * Validator for the look and feel info view
032: *
033: * @author Jeff Tassin
034: */
035: public class BeanDefinitionValidator implements JETARule {
036: /**
037: * Validates the look and feel view.
038: *
039: * @param params
040: * a 2 element array that contains a BeanDefinitionView and a
041: * BeanLoader
042: */
043: public RuleResult check(Object[] params) {
044: BeanDefinitionView view = (BeanDefinitionView) params[0];
045: String classname = FormDesignerUtils.fastTrim(view
046: .getText(BeanDefinitionNames.ID_BEAN_CLASS));
047:
048: if (classname.length() == 0)
049: return new RuleResult(I18N
050: .getLocalizedMessage("Invalid class name"));
051:
052: BeanLoader loader = (BeanLoader) params[1];
053: try {
054: Component comp = loader.createBean(classname);
055: } catch (FormException fe) {
056: Exception e = fe;
057: if (fe.getSourceException() != null)
058: e = fe.getSourceException();
059:
060: if (e instanceof ClassNotFoundException)
061: handleException(view, (ClassNotFoundException) e);
062: else if (e instanceof IllegalAccessException)
063: handleException(view, (IllegalAccessException) e);
064: else if (e instanceof ClassCastException)
065: handleException(view, (ClassCastException) e);
066: else if (e instanceof InstantiationException)
067: handleException(view, (InstantiationException) e);
068: else
069: handleException(view, e);
070:
071: return new RuleResult(RuleResult.FAIL_NO_MESSAGE_ID);
072: } catch (Exception e) {
073: handleException(view, e);
074: return new RuleResult(RuleResult.FAIL_NO_MESSAGE_ID);
075: }
076: return RuleResult.SUCCESS;
077: }
078:
079: private void handleException(Component view,
080: ClassNotFoundException e) {
081: String caption = I18N
082: .getLocalizedMessage("Unable to instantiate bean");
083: String msg = "The bean class was not found in the classpath. Make sure you have included all JAR files and paths required by this bean in the classpaths list.";
084: TSErrorDialog2 dlg = TSErrorDialog2.createDialog(view, caption,
085: msg, e);
086: dlg.showCenter();
087: }
088:
089: private void handleException(Component view,
090: IllegalAccessException e) {
091: String caption = I18N
092: .getLocalizedMessage("Unable to instantiate bean");
093: String msg = "An illegal access exception occurred. Please verify that the bean is declared in a public class and that the bean has a public no-arg constructor.";
094: TSErrorDialog2 dlg = TSErrorDialog2.createDialog(view, caption,
095: msg, e);
096: dlg.showCenter();
097: }
098:
099: private void handleException(Component view, ClassCastException e) {
100: String caption = I18N
101: .getLocalizedMessage("Unable to instantiate bean");
102: String msg = "The given bean class does not appear to be a valid Swing component. Please verify that the bean has javax.swing.JComponent has a superclass";
103: TSErrorDialog2 dlg = TSErrorDialog2.createDialog(view, caption,
104: msg, e);
105: dlg.showCenter();
106: }
107:
108: private void handleException(Component view,
109: InstantiationException e) {
110: String caption = I18N
111: .getLocalizedMessage("Unable to instantiate bean");
112: String msg = "Please verify that the bean has a public no-arg constructor.";
113: TSErrorDialog2 dlg = TSErrorDialog2.createDialog(view, caption,
114: msg, e);
115: dlg.showCenter();
116: }
117:
118: private void handleException(Component view, Exception e) {
119: String caption = I18N
120: .getLocalizedMessage("Unable to instantiate bean");
121: String msg = "An unknown exception occurred. The constructor or a member variable in the bean class might be throwing an unchecked exception.";
122: TSErrorDialog2 dlg = TSErrorDialog2.createDialog(view, caption,
123: msg, e);
124: dlg.showCenter();
125: }
126:
127: }
|