KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jaspersoft > jasperserver > irplugin > gui > inputcontrols > BasicInputControl


1 /*
2  * BasicInputControl.java
3  *
4  * All rights reserved.
5  * Copyright (C) 2005 JasperSoft Corporation
6  *
7  * JasperSoft Corporation
8  * 303 Second Street, Suite 450 North
9  * San Francisco, CA 94107
10  * http://www.jaspersoft.com
11  *
12  *
13  * Created on June 7, 2006, 1:46 PM
14  *
15  */

16
17 package com.jaspersoft.jasperserver.irplugin.gui.inputcontrols;
18
19 import com.jaspersoft.jasperserver.api.metadata.xml.domain.impl.ResourceDescriptor;
20 import com.jaspersoft.jasperserver.irplugin.gui.inputcontrols.ui.BasicInputControlUI;
21 import com.jaspersoft.jasperserver.irplugin.gui.inputcontrols.ui.InputControlUI;
22 import java.text.ParseException JavaDoc;
23 import java.util.regex.Pattern JavaDoc;
24 import javax.swing.JComponent JavaDoc;
25 import java.text.SimpleDateFormat JavaDoc;
26
27 /**
28  *
29  * @author gtoffoli
30  */

31 public class BasicInputControl {
32     
33     /*
34      * This map stores the "recent" list of values for each control name...
35      * As key, the control URI is used...
36      * Each key stores a List.
37      *
38      */

39     public static java.util.Map JavaDoc valueHistories = new java.util.HashMap JavaDoc();
40     
41     protected ResourceDescriptor inputControl = null;
42     private ResourceDescriptor dataType = null;
43     private InputControlUI inputControlUI;
44     
45     /** Creates a new instance of BasicInputControl */
46     public BasicInputControl() {
47          setInputControlUI(new BasicInputControlUI());
48     }
49     
50     public JComponent JavaDoc getUI()
51     {
52         return (JComponent JavaDoc)getInputControlUI();
53     }
54     
55     public Object JavaDoc getValue() throws InputValidationException
56     {
57         return validate();
58     }
59
60     public ResourceDescriptor getInputControl() {
61         return inputControl;
62     }
63
64     public void setInputControl(ResourceDescriptor inputControl)
65     {
66         setInputControl(inputControl, null);
67     }
68             
69     public void setInputControl(ResourceDescriptor inputControl, ResourceDescriptor dataType) {
70         this.inputControl = inputControl;
71         this.dataType = dataType;
72     
73         String JavaDoc label = inputControl.getLabel() + ((inputControl.isMandatory()) ? "*" : "");
74         getInputControlUI().setLabel(label);
75         getInputControlUI().setReadOnly( inputControl.isReadOnly() );
76         
77         java.util.List JavaDoc history = getHistory(inputControl.getUriString());
78         java.util.List JavaDoc revisedHistory = new java.util.ArrayList JavaDoc();
79         if (dataType != null && dataType.getDataType() == dataType.DT_TYPE_DATE)
80         {
81             SimpleDateFormat JavaDoc format = new SimpleDateFormat JavaDoc(it.businesslogic.ireport.gui.MainFrame.getMainInstance().getProperties()
82                                           .getProperty("dateformat", "d/M/y"));
83             for (int i=0; i<history.size(); ++i)
84             {
85                 Object JavaDoc obj = history.get(i);
86                 if (obj instanceof java.util.Date JavaDoc)
87                 {
88                     revisedHistory.add( format.format(obj));
89                 }
90                 else
91                     revisedHistory.add( obj);
92             }
93         }
94         else if (dataType != null && dataType.getDataType() == dataType.DT_TYPE_DATE)
95         {
96             SimpleDateFormat JavaDoc format = new SimpleDateFormat JavaDoc(it.businesslogic.ireport.gui.MainFrame.getMainInstance().getProperties()
97                                           .getProperty("timeformat", "d/M/y H:m:s"));
98             for (int i=0; i<history.size(); ++i)
99             {
100                 Object JavaDoc obj = history.get(i);
101                 if (obj instanceof java.util.Date JavaDoc)
102                 {
103                     revisedHistory.add( format.format(obj));
104                 }
105                 else
106                     revisedHistory.add( obj);
107             }
108         }
109         else
110         {
111             revisedHistory = history;
112         }
113         getInputControlUI().setHistory( revisedHistory );
114         
115         
116         
117     }
118     
119     public void addHistoryValue(String JavaDoc controlUri, Object JavaDoc value)
120     {
121         
122         if (controlUri == null) return;
123         java.util.List JavaDoc list = getHistory(controlUri);
124         if (list.contains( value)) list.remove(value );
125         list.add(0,value);
126     }
127     
128     public java.util.List JavaDoc getHistory(String JavaDoc controlUri)
129     {
130         if (controlUri == null) return null;
131         
132         java.util.List JavaDoc list = null;
133         if ( valueHistories.get(controlUri) == null)
134         {
135             list = new java.util.ArrayList JavaDoc();
136             valueHistories.put(controlUri, list);
137             return list;
138         }
139         
140         return (java.util.List JavaDoc)valueHistories.get(controlUri);
141     }
142
143     public InputControlUI getInputControlUI() {
144         return inputControlUI;
145     }
146
147     public void setInputControlUI(InputControlUI inputControlUI) {
148         this.inputControlUI = inputControlUI;
149     }
150     
151     public Object JavaDoc validate() throws InputValidationException
152     {
153         Object JavaDoc val = getInputControlUI().getValue();
154         if (getInputControl().getControlType() == ResourceDescriptor.IC_TYPE_SINGLE_VALUE)
155         {
156             // Look for the datatype....
157
if (getDataType() != null)
158             {
159                 if (val == null) return null;
160                 
161                 String JavaDoc strVal = ""+val;
162                 
163                 
164                 
165                 switch(getDataType().getDataType())
166         {
167             case ResourceDescriptor.DT_TYPE_TEXT :
168             {
169                 //if (
170
// getDataType().getMaxValue()getMaxLength() != null
171
// && getDataType().getMaxLength().intValue() < (val+"").length()
172
// )
173
//{
174
// wrapper.setErrorMessage(messages.getMessage("fillParameters.error.invalidType", null, Locale.getDefault()));
175
//}
176
//else
177
if (getDataType().getPattern() != null
178                     && getDataType().getPattern().trim().length() > 0
179                     && !Pattern.matches(getDataType().getPattern(), (strVal)))
180                 {
181                     throw new InputValidationException("" + getInputControl().getLabel() + " does not match the pattern (" + getDataType().getPattern() +")");
182                                 }
183                                 val = strVal;
184                 break;
185             }
186             case ResourceDescriptor.DT_TYPE_NUMBER :
187             {
188                 //FIXME take care of input mask
189
try
190                 {
191                                     if (strVal.trim().length() == 0) return null;
192                     val = new java.math.BigDecimal JavaDoc(strVal);
193                 }
194                 catch(NumberFormatException JavaDoc e)
195                 {
196                                     throw new InputValidationException("" + getInputControl().getLabel() + " is not a valid number");
197                 }
198                                 break;
199             }
200             case ResourceDescriptor.DT_TYPE_DATE_TIME :
201                         {
202                                 SimpleDateFormat JavaDoc format = new SimpleDateFormat JavaDoc(it.businesslogic.ireport.gui.MainFrame.getMainInstance().getProperties()
203                                           .getProperty("timeformat", "d/M/y H:m:s"));
204                                 try
205                                 {
206                                         if (strVal.trim().length() == 0) return null;
207                                         val = format.parse(strVal);
208                                 }
209                                 catch (ParseException JavaDoc e)
210                                 {
211                                         throw new InputValidationException("" + getInputControl().getLabel() + " is not a valid datetime in the form " +
212                                                 it.businesslogic.ireport.gui.MainFrame.getMainInstance().getProperties().getProperty("timeformat", "d/M/y H:m:s") );
213                                 }
214                                 break;
215                         }
216                             
217                             
218             case ResourceDescriptor.DT_TYPE_DATE :
219             {
220                 SimpleDateFormat JavaDoc format = new SimpleDateFormat JavaDoc( it.businesslogic.ireport.gui.MainFrame.getMainInstance().getProperties()
221                                       .getProperty("dateformat", "d/M/y") );
222
223                 try
224                 {
225                                         if (strVal.trim().length() == 0) return null;
226                     val = format.parse(strVal);
227                 }
228                 catch (ParseException JavaDoc e)
229                 {
230                     throw new InputValidationException("" + getInputControl().getLabel() + " is not a valid date in the form " +
231                                                 it.businesslogic.ireport.gui.MainFrame.getMainInstance().getProperties().getProperty("dateformat", "d/M/y") );
232                 }
233                                 break;
234             }
235         }
236             }
237         }
238         
239         return val;
240     }
241
242     public ResourceDescriptor getDataType() {
243         return dataType;
244     }
245
246     public void setDataType(ResourceDescriptor dataType) {
247         this.dataType = dataType;
248     }
249 }
250
Popular Tags