View Javadoc

1   /*
2    * Copyright (c) 2001 - 2005 ivata limited.
3    * All rights reserved.
4    * -----------------------------------------------------------------------------
5    * ivata masks may be redistributed under the GNU General Public
6    * License as published by the Free Software Foundation;
7    * version 2 of the License.
8    *
9    * These programs are free software; you can redistribute them and/or
10   * modify them under the terms of the GNU General Public License
11   * as published by the Free Software Foundation; version 2 of the License.
12   *
13   * These programs are distributed in the hope that they will be useful,
14   * but WITHOUT ANY WARRANTY; without even the implied warranty of
15   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16   *
17   * See the GNU General Public License in the file LICENSE.txt for more
18   * details.
19   *
20   * If you would like a copy of the GNU General Public License write to
21   *
22   * Free Software Foundation, Inc.
23   * 59 Temple Place - Suite 330
24   * Boston, MA 02111-1307, USA.
25   *
26   *
27   * To arrange commercial support and licensing, contact ivata at
28   *                  http://www.ivata.com/contact.jsp
29   * -----------------------------------------------------------------------------
30   * $Log: MaskImpl.java,v $
31   * Revision 1.5  2005/04/09 18:04:14  colinmacleod
32   * Changed copyright text to GPL v2 explicitly.
33   *
34   * Revision 1.4  2005/01/19 12:39:39  colinmacleod
35   * Changed Id --> Name.
36   *
37   * Revision 1.3  2005/01/06 22:13:21  colinmacleod
38   * Moved up a version number.
39   * Changed copyright notices to 2005.
40   * Updated the documentation:
41   *   - started working on multiproject:site docu.
42   *   - changed the logo.
43   * Added checkstyle and fixed LOADS of style issues.
44   * Added separate thirdparty subproject.
45   * Added struts (in web), util and webgui (in webtheme) from ivata op.
46   *
47   * Revision 1.2  2004/12/30 20:15:14  colinmacleod
48   * Moved first and last fields up to Group from Mask.
49   *
50   * Revision 1.1  2004/12/29 20:07:06  colinmacleod
51   * Renamed subproject masks to mask.
52   *
53   * Revision 1.1.1.1  2004/05/16 20:40:31  colinmacleod
54   * Ready for 0.1 release
55   * -----------------------------------------------------------------------------
56   */
57  package com.ivata.mask;
58  import java.beans.PropertyDescriptor;
59  import java.util.Collections;
60  import java.util.List;
61  import java.util.Set;
62  import java.util.Vector;
63  
64  import org.apache.commons.beanutils.PropertyUtils;
65  import com.ivata.mask.field.Field;
66  import com.ivata.mask.group.Group;
67  import com.ivata.mask.group.GroupImpl;
68  /***
69   * <p>
70   * In the Masks display configuration, a mask extends a group to define which
71   * fields come first and last, or the fields which should appear explicitly.
72   * </p>
73   *
74   * <p>
75   * Don't try to instantiate this class - use the mask factory instead.
76   * </p>
77   *
78   * @author Colin MacLeod
79   * @since ivata masks 0.1 (2004-05-15) <a
80   *        href='mailto:colin.macleod@ivata.com'>colin.macleod@ivata.com </a>
81   */
82  public class MaskImpl extends GroupImpl implements Mask {
83      /***
84       * The class of the dependent value objects which will be associated with
85       * this mask.
86       */
87      private Class dOClass;
88      /***
89       * Refer to {@link Mask#getIncludePaths}.
90       */
91      private List includePaths = new Vector();
92      /***
93       * Create a mask with the specified parent.
94       *
95       * @param dOClassParam
96       *            The class of the data object displayed in this mask.
97       * @param parent
98       *            Group which contains this mask, or parent mask this mask
99       *            extends.
100      * @param nameParam
101      *            Name of the mask. This is used in combination with the class
102      *            name to build the identifier for each mask uniquely.
103      */
104     public MaskImpl(final Class dOClassParam,
105             final Group parent, final String nameParam) {
106         super(nameParam, parent);
107         this.dOClass = dOClassParam;
108     }
109     /***
110      * Refer to {@link Mask#getIncludePaths}.
111      * @param includePathParam The include path to add to the list.
112      */
113     public final void addIncludePath(final String includePathParam) {
114         this.includePaths.add(includePathParam);
115     }
116     /***
117      * Get the dependent object class associated with this mask.
118      *
119      * @return The class of the data object displayed in this mask.
120      */
121     public final Class getDOClass() {
122         return dOClass;
123     }
124     /***
125      * Get all the fields which should be displayed/hidden (not excluded) in
126      * this mask, in order.
127      *
128      * @return All field names apart from those explicitly excluded.
129      */
130     public final List getFields() {
131         Set allExcludedFieldNames = getAllExcludedFieldNames();
132         List firstFieldNames = getAllFirstFieldNames();
133         List lastFieldNames = getAllLastFieldNames();
134         PropertyDescriptor[] propertyDescriptors = PropertyUtils
135                 .getPropertyDescriptors(dOClass);
136         List maskFields = new java.util.ArrayList();
137         // go thro' all the first field names and see if any match this class
138         for (int i = 0; i < firstFieldNames.size(); ++i) {
139             String firstFieldId = (String) firstFieldNames.get(i);
140             for (int j = 0; j < propertyDescriptors.length; j++) {
141                 String name = propertyDescriptors[j].getName();
142                 if (name.equals(firstFieldId)) {
143                     // only add field names which are _not_ on the exclude list
144                     // explicitly
145                     if (!allExcludedFieldNames.contains(name)) {
146                         Field field = getField(name);
147                         if (field != null) {
148                             maskFields.add(field);
149                         }
150                     }
151                     break;
152                 }
153             }
154         }
155         // get all the fields which are in this class, and not specified as
156         // either first or last.
157         for (int i = 0; i < propertyDescriptors.length; i++) {
158             String name = propertyDescriptors[i].getName();
159             // make sure this field is not excluded
160             if (!firstFieldNames.contains(name)
161                     && !allExcludedFieldNames.contains(name)
162                     && !lastFieldNames.contains(name)) {
163                 Field field = getField(name);
164                 if (field != null) {
165                     maskFields.add(field);
166                 }
167             }
168         }
169         // these fields should come at the end, in the order specified
170         for (int i = 0; i < lastFieldNames.size(); ++i) {
171             String lastFieldId = (String) lastFieldNames.get(i);
172             for (int j = 0; j < propertyDescriptors.length; j++) {
173                 String name = propertyDescriptors[j].getName();
174                 if (name.equals(lastFieldId)) {
175                     // only add field names which are _not_ on the exclude list
176                     if (!firstFieldNames.contains(name)
177                             && !allExcludedFieldNames.contains(name)) {
178                         Field field = getField(name);
179                         if (field != null) {
180                             maskFields.add(field);
181                         }
182                     }
183                     break;
184                 }
185             }
186         }
187         return maskFields;
188     }
189     /***
190      * Refer to {@link Mask#getIncludePaths}.
191      * @return Returns all include paths.
192      */
193     public List getIncludePaths() {
194         return Collections.unmodifiableList(includePaths);
195     }
196 }