View Javadoc

1   /* ===================================================================
2    * Copyright 2002-04 SRI International.
3    * Released under the MOZILLA PUBLIC LICENSE Version 1.1
4    * which can be obtained at http://www.mozilla.org/MPL/MPL-1.1.html
5    * This software is distributed on an "AS IS"
6    * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
7    * See the License for the specific language governing rights and
8    * limitations under the License.
9    * =================================================================== */
10  package com.sri.emo.wizard.defaults;
11  
12  import com.jcorporate.expresso.core.controller.ErrorCollection;
13  import com.jcorporate.expresso.core.dbobj.ValidValue;
14  import com.sri.emo.wizard.PageMetadata;
15  import com.sri.emo.wizard.WizardException;
16  import com.sri.emo.wizard.WizardPage;
17  
18  import java.io.Serializable;
19  
20  
21  /***
22   * Wizard Page implementation for Emo.  By default will store one string
23   * per page, although derived classes will often store maps, lists, and sets.
24   *
25   * @author Michael Rimov
26   */
27  public class EmoWizardPage implements WizardPage, Serializable {
28      /***
29  	 * 
30  	 */
31  	private static final long serialVersionUID = 1L;
32  
33  	/***
34       * The Page Id as Integer.
35       */
36      private final Integer id;
37  
38      /***
39       * The Page Metadata.
40       */
41      private final PageMetadata metadata;
42  
43      /***
44       * The Page Data.
45       */
46      private Serializable data;
47  
48      /***
49       * The page menus.
50       */
51      private ValidValue[] menu;
52  
53  
54      /***
55       * Sets any errors for the current page.
56       */
57      private ErrorCollection errors = null;
58  
59      /***
60       * Constructor that takes a PageMetadata object and a page definition
61       * object to build itself.
62       *
63       * @param newId        the page id.
64       * @param pageMetadata the metadata for the page
65       * @param optionalMenu The menu to pick from (may be null).
66       */
67      public EmoWizardPage(Integer newId, PageMetadata pageMetadata, ValidValue[] optionalMenu) {
68          id = newId;
69          metadata = pageMetadata;
70          menu = optionalMenu;
71      }
72  
73      public Serializable getId() {
74          return id;
75      }
76  
77      public Serializable getData() {
78          return data;
79      }
80  
81  
82      public String getPrintableData() {
83          if (data == null) {
84              return null;
85          }
86  
87          if (menu != null) {
88              for (int i = 0; i < menu.length; i++) {
89                  ValidValue oneVV = menu[i];
90                  if (data.equals(oneVV.getKey())) {
91                      return oneVV.getDescription();
92                  }
93              }
94  
95              throw new IllegalStateException(
96                      "Menu is specified, but there is no matching data found for: " + data.toString());
97          } else {
98              return data.toString();
99          }
100     }
101 
102 
103     public PageMetadata getMetadata() {
104         return metadata;
105     }
106 
107     public void setData(Serializable newData) throws WizardException {
108         data = newData;
109     }
110 
111     /***
112      * Override to performs field comparison.
113      * <p>{@inheritDoc}</p>
114      *
115      * @param parm1 The other object to compare.
116      * @return true if all fields are equal
117      */
118     public boolean equals(Object parm1) {
119         EmoWizardPage other = (EmoWizardPage) parm1;
120 
121         //
122         //Translation:  Checks that one isn't null and the other isn't.
123         //Then if both aren't null checks for equality.  Yes, I did want
124         //to know if I could do it in one line of code :)
125         //
126         if (!(!(data == null ^ other.data == null) &&
127                 ((data != null && data.equals(other.data))
128                         || data == null))) {
129             return false;
130         }
131 
132         boolean returnValue = true;
133         returnValue &= id.equals(other.id);
134         returnValue &= metadata.equals(other.metadata);
135 
136         return returnValue;
137     }
138 
139     /***
140      * Override of hashcode.  Hashes the id.
141      * <p>{@inheritDoc}</p>
142      *
143      * @return integer
144      */
145     public int hashCode() {
146         return id.hashCode();
147     }
148 
149     /***
150      * Override of toString(), returns the Id.
151      * <p>{@inheritDoc}</p>
152      *
153      * @return String
154      */
155     public String toString() {
156         return EmoWizardPage.class.getName() + ": id=" + id;
157     }
158 
159 
160     /***
161      * Retrieve the picklist value for the map.
162      *
163      * @return ValidValue[]
164      */
165     public ValidValue[] getMenu() {
166         return menu;
167     }
168 
169     /***
170      * Retrieve an error collection (if any) that is associated with this page.
171      * May return null if there are no errors.
172      *
173      * @return ErrorCollection
174      */
175     public ErrorCollection getPageErrors() {
176         return this.errors;
177     }
178 
179     /***
180      * Clear all errors associated with the page.
181      */
182     public void removeErrors() {
183         errors = null;
184     }
185 
186 
187     /***
188      * Adds an error to be associated with the current page.
189      *
190      * @param message String
191      */
192     public synchronized void addError(String message) {
193         if (errors == null) {
194             errors = new ErrorCollection();
195         }
196 
197         errors.addError(message);
198     }
199 
200 }