KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sslexplorer > core > forms > CoreForm


1 /*
2  * SSL-Explorer
3  *
4  * Copyright (C) 2003-2006 3SP LTD. All Rights Reserved
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2 of
9  * the License, or (at your option) any later version.
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */

19             
20 package com.sslexplorer.core.forms;
21
22 import org.apache.struts.action.ActionForm;
23 import org.apache.struts.action.ActionMapping;
24
25 import com.sslexplorer.security.Constants;
26
27 /**
28  * Extension to the standard struts {@link org.apache.struts.action.ActionForm}
29  * that adds some additional attributes used by pretty much all of
30  * SSL-Explorer's forms.
31  * <p>
32  * One such attribute is the <i>Referer</i> attribute. This should be set when
33  * entering a form for the first in some kind of flow (edit screen for example).
34  * When the form is finished with (e.g. when the user commits the entered
35  * details or cancels the form) the browser is forwarded to the stored referer.
36  * This allows us to return to the page the flow was called from.
37  * <p>
38  * Another command attribute is the <i>Action Target</i>. This is used by all
39  * <i>Dispatch Actions</i> to store action withing the dispatch action to
40  * invoke on the next execution.
41  * <p>
42  * <i>Editing</i> is another common attribute to be used to denote the form is
43  * editing an entity as opposed to creating it.
44  * <p>
45  *
46  * @author Brett Smith <a HREF="mailto: brett@3sp.com">&lt;brett@3sp.com&gt;</a>
47  */

48 public class CoreForm extends ActionForm {
49
50     // Private instance variables
51

52     protected String JavaDoc referer;
53     private String JavaDoc actionTarget;
54     protected boolean editing;
55
56     /**
57      * Constructor
58      */

59     public CoreForm() {
60         super();
61     }
62
63     /**
64      * Get if the resource is being edited
65      *
66      * @return resource being edited
67      */

68     public boolean getEditing() {
69         return editing;
70     }
71
72     /**
73      * Set the form as creating an entity. This method has no signature to
74      * prevent the value from being able to be changed by posting a parameter
75      */

76     public void setCreating() {
77         this.editing = false;
78     }
79
80     /**
81      * Set the form as creating an entity. This method has no signature to
82      * prevent the value from being able to be changed by posting a parameter
83      */

84     public void setEditing() {
85         this.editing = true;
86         this.actionTarget = "edit";
87     }
88
89     /**
90      * Helps with a work around for character encoding on IE. All JSP for forms
91      * should include the hidden <i>_charset_</i> attribute using the struts
92      * <stronghtml:hidden</strong> tag. This will be expanded to 'utf-8'
93      * forcing all form submissions to be in UTF-8 encoding.
94      *
95      * @return character set for form submission encoding
96      */

97     public String JavaDoc get_charset_() {
98         return System.getProperty("sslexplorer.encoding", "UTF-8");
99     }
100
101     /**
102      * Set action target, used when form is handled by a <i>Dispatch Action</i>
103      * to store the dispatch method to invoke the next time the action is
104      * executed.
105      *
106      * @param actionTarget action target for dispatch action
107      */

108     public void setActionTarget(String JavaDoc actionTarget) {
109         this.actionTarget = actionTarget;
110     }
111
112     /**
113      * Get action target, used when form is handled by a <i>Dispatch Action</i>
114      * to store the dispatch method to invoke the next time the action is
115      * executed.
116      *
117      * @return action target for dispatch action
118      */

119     public String JavaDoc getActionTarget() {
120         return actionTarget;
121     }
122
123     /**
124      * Set the referer. This should be set when entering a form for the first in
125      * some kind of flow (edit screen for example). When the form is finished
126      * with (e.g. when the user commits the entered details or cancels the form)
127      * the browser is forwarded to the stored referer. This allows us to return
128      * to the page the flow was called from.
129      *
130      * @param referer referer
131      */

132     public void setReferer(String JavaDoc referer) {
133         this.referer = referer;
134     }
135
136
137     /**
138      * Get the referer. This should be set when entering a form for the first in
139      * some kind of flow (edit screen for example). When the form is finished
140      * with (e.g. when the user commits the entered details or cancels the form)
141      * the browser is forwarded to the stored referer. This allows us to return
142      * to the page the flow was called from.
143      *
144      * @return referer
145      */

146     public String JavaDoc getReferer() {
147         return referer;
148     }
149
150     /* (non-Javadoc)
151      * @see org.apache.struts.action.ActionForm#reset(org.apache.struts.action.ActionMapping, javax.servlet.http.HttpServletRequest)
152      */

153     public void reset(ActionMapping mapping, javax.servlet.http.HttpServletRequest JavaDoc request) {
154         referer = null;
155         actionTarget = null;
156         request.setAttribute(Constants.REQ_ATTR_ACTION_MAPPING, mapping);
157         request.setAttribute(Constants.REQ_ATTR_FORM, this);
158     }
159
160     /**
161      * Get if the user is canceling this form. This may be used in the
162      * validate() method to prevent validation upon cancel.
163      *
164      * @return cancelling
165      */

166     public boolean isCancelling() {
167         return getActionTarget().equals("cancel");
168     }
169     
170     /**
171      * @param value
172      * @return true if the value is either null or zero length when trimmed
173      */

174     public static boolean isEmpty(String JavaDoc value) {
175         return value == null || value.trim().length() == 0;
176     }
177
178     /**
179      * Get if the user is commiting this form. This may be used in the
180      * validate() method to prevent validation upon cancel.
181      *
182      * @return commit
183      */

184     public boolean isCommiting() {
185         return "commit".equals(getActionTarget());
186     }
187 }
Popular Tags