KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > tools > common > beans > BasicIasBean


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 /*
25  * BaicIasBean.java
26  *
27  * Created on April 4, 2002, 9:47 AM
28  */

29
30 package com.sun.enterprise.tools.common.beans;
31
32 import java.util.ResourceBundle JavaDoc;
33
34 import java.beans.PropertyVetoException JavaDoc;
35 import java.beans.VetoableChangeSupport JavaDoc;
36 import java.beans.VetoableChangeListener JavaDoc;
37 import java.beans.PropertyChangeSupport JavaDoc;
38 import java.beans.PropertyChangeListener JavaDoc;
39 import java.beans.PropertyChangeEvent JavaDoc;
40 import org.netbeans.modules.schema2beans.BaseBean;
41 /**
42  *
43  * @author vkraemer
44  */

45 public abstract class BasicIasBean {
46
47     protected static VetoableChangeListener JavaDoc greaterThanNegOne =
48         new PositiveValueListener();
49     protected static VetoableChangeListener JavaDoc notNull =
50         new NotEmptyValueListener();
51
52     private PropertyChangeSupport JavaDoc pcs;
53     private VetoableChangeSupport JavaDoc vcs;
54
55     protected static ResourceBundle JavaDoc bundle =
56         ResourceBundle.getBundle("com.sun.enterprise.tools.common.beans.Bundle"); //NOI18N
57

58
59     /** Creates a new instance of BaicIasBean */
60     protected BasicIasBean() {
61     }
62     
63     public PropertyChangeSupport JavaDoc getPCS() {
64         if (null == pcs)
65             pcs = new PropertyChangeSupport JavaDoc(this);
66         return pcs;
67     }
68     
69     public VetoableChangeSupport JavaDoc getVCS() {
70         if (null == vcs)
71             vcs = new VetoableChangeSupport JavaDoc(this);
72         return vcs;
73     }
74
75      protected void doAttrSetProcessing(BaseBean bean, String JavaDoc newVal, String JavaDoc attrName, String JavaDoc propName)
76         throws PropertyVetoException JavaDoc {
77         String JavaDoc oldName = bean.getAttributeValue(attrName); //NOI18N
78
fireMyVetoableChange(propName, oldName, newVal);
79         bean.setAttributeValue(attrName,newVal); //NOI18N
80
fireMyPropertyChange(propName, oldName, newVal);
81     }
82      
83      protected void doElementSetProcessing(BaseBean bean, String JavaDoc newVal, String JavaDoc subElement, String JavaDoc propName)
84         throws PropertyVetoException JavaDoc {
85         String JavaDoc oldName = (String JavaDoc) bean.getValue(subElement);
86         fireMyVetoableChange(propName, oldName, newVal);
87         bean.setValue(subElement,newVal);
88         fireMyPropertyChange(propName, oldName, newVal);
89     }
90      
91      protected void doAttrSetProcessing(BaseBean bean, int newVal, String JavaDoc attrName, String JavaDoc propName)
92         throws PropertyVetoException JavaDoc {
93         int oldVal = Integer.parseInt(bean.getAttributeValue(attrName)); //NOI18N
94
fireMyVetoableChange(propName, oldVal, newVal);
95         bean.setAttributeValue(attrName, ""+newVal); //NOI18N
96
fireMyPropertyChange(propName, oldVal, newVal);
97     }
98
99      protected void fireMyVetoableChange(String JavaDoc name, Object JavaDoc oldV, Object JavaDoc newV) throws PropertyVetoException JavaDoc {
100         if (null == vcs) {
101             vcs = new VetoableChangeSupport JavaDoc(this);
102         }
103         vcs.fireVetoableChange(name,oldV,newV);
104     }
105     
106     protected void fireMyVetoableChange(String JavaDoc name, int oldV, int newV) throws PropertyVetoException JavaDoc {
107         if (null == vcs) {
108             vcs = new VetoableChangeSupport JavaDoc(this);
109         }
110         vcs.fireVetoableChange(name,oldV,newV);
111     }
112     
113     protected void fireMyPropertyChange(String JavaDoc name, Object JavaDoc oldV, Object JavaDoc newV) { // throws PropertyVetoException {
114
if (null == pcs) {
115             pcs = new PropertyChangeSupport JavaDoc(this);
116         }
117         pcs.firePropertyChange(name,oldV,newV);
118     }
119     
120     protected void fireMyPropertyChange(String JavaDoc name, int oldV, int newV) { // throws PropertyVetoException {
121
if (null == pcs) {
122             pcs = new PropertyChangeSupport JavaDoc(this);
123         }
124         pcs.firePropertyChange(name,oldV,newV);
125     }
126         
127     /** Add a property listener to this bean.
128      * @param pcl PropertyChangeListener to add
129      */

130    public void addPropertyChangeListener(PropertyChangeListener JavaDoc pcl) {
131        if (null == pcs)
132            pcs = new PropertyChangeSupport JavaDoc(this);
133        pcs.addPropertyChangeListener(pcl);
134    }
135
136    /** Remove this listener.
137     * @param pcl Listener to remove.
138     */

139    public void removePropertyChangeListener(PropertyChangeListener JavaDoc pcl) {
140        if (null != pcs)
141            pcs.removePropertyChangeListener(pcl);
142    }
143
144     /** Add a Vetoable listener to this bean.
145      * @param pcl VetoableChangeListener to add
146      */

147    public void addVetoableChangeListener(VetoableChangeListener JavaDoc pcl) {
148        if (null == vcs)
149            vcs = new VetoableChangeSupport JavaDoc(this);
150        vcs.addVetoableChangeListener(pcl);
151    }
152
153    /** Remove this listener.
154     * @param pcl Listener to remove.
155     */

156    public void removeVetoableChangeListener(VetoableChangeListener JavaDoc pcl) {
157        if (null != vcs)
158            vcs.removeVetoableChangeListener(pcl);
159    }
160
161    /////////////////////
162

163    public abstract void outTo(java.io.OutputStream JavaDoc os) throws java.io.IOException JavaDoc;
164
165    static class PositiveValueListener implements VetoableChangeListener JavaDoc {
166         public void vetoableChange(PropertyChangeEvent JavaDoc pce) throws PropertyVetoException JavaDoc {
167             try {
168                 Integer JavaDoc valI= (Integer JavaDoc) pce.getNewValue();
169                 int val = valI.intValue();
170                 if (0 > val) {
171                     String JavaDoc messFormat = bundle.getString("ERROR_MUST_BE_POSITIVE");
172                     String JavaDoc mess = java.text.MessageFormat.format(messFormat, new Object JavaDoc[] { pce.getPropertyName() });
173                     throw new PropertyVetoException JavaDoc(mess, pce);
174                 }
175             }
176             catch (Throwable JavaDoc t) {
177                 throw new PropertyVetoException JavaDoc(t.getLocalizedMessage(), pce);
178             }
179         }
180     }
181     
182    static class NotEmptyValueListener implements VetoableChangeListener JavaDoc {
183         public void vetoableChange(PropertyChangeEvent JavaDoc pce) throws PropertyVetoException JavaDoc {
184             try {
185                 String JavaDoc val= (String JavaDoc) pce.getNewValue();
186                 //int val = valI.intValue();
187
if (null == val || val.trim().length() == 0) {
188                     String JavaDoc messFormat = bundle.getString("ERROR_MUST_HAVE_VALUE");
189                     String JavaDoc mess = java.text.MessageFormat.format(messFormat, new Object JavaDoc[] { pce.getPropertyName() });
190                     throw new PropertyVetoException JavaDoc(mess, pce);
191                 }
192             }
193             catch (Throwable JavaDoc t) {
194                 throw new PropertyVetoException JavaDoc(t.getLocalizedMessage(), pce);
195             }
196         }
197     }
198    
199 }
200
Popular Tags