KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > riotfamily > riot > list > command > core > AbstractCommand


1 /* ***** BEGIN LICENSE BLOCK *****
2  * Version: MPL 1.1
3  * The contents of this file are subject to the Mozilla Public License Version
4  * 1.1 (the "License"); you may not use this file except in compliance with
5  * the License. You may obtain a copy of the License at
6  * http://www.mozilla.org/MPL/
7  *
8  * Software distributed under the License is distributed on an "AS IS" basis,
9  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
10  * for the specific language governing rights and limitations under the
11  * License.
12  *
13  * The Original Code is Riot.
14  *
15  * The Initial Developer of the Original Code is
16  * Neteye GmbH.
17  * Portions created by the Initial Developer are Copyright (C) 2006
18  * the Initial Developer. All Rights Reserved.
19  *
20  * Contributor(s):
21  * Felix Gnass [fgnass at neteye dot de]
22  *
23  * ***** END LICENSE BLOCK ***** */

24 package org.riotfamily.riot.list.command.core;
25
26 import org.apache.commons.logging.Log;
27 import org.apache.commons.logging.LogFactory;
28 import org.riotfamily.common.util.FormatUtils;
29 import org.riotfamily.riot.list.command.Command;
30 import org.riotfamily.riot.list.command.CommandContext;
31 import org.riotfamily.riot.list.command.CommandState;
32 import org.riotfamily.riot.runtime.RiotRuntime;
33 import org.riotfamily.riot.runtime.RiotRuntimeAware;
34 import org.springframework.beans.factory.BeanNameAware;
35
36 /**
37  * Abstract baseclass for commands.
38  */

39 public abstract class AbstractCommand implements Command, BeanNameAware,
40         RiotRuntimeAware {
41
42     private static final String JavaDoc COMMAND_NAME_SUFFIX = "Command";
43
44     private final String JavaDoc COMMAND_MESSAGE_PREFIX = "command.";
45
46     protected Log log = LogFactory.getLog(getClass());
47
48     private String JavaDoc id;
49
50     private boolean showOnForm;
51     
52     private String JavaDoc riotServletPrefix;
53
54     public String JavaDoc getId() {
55         return id;
56     }
57
58     /**
59      * Sets the commandId. If no value is set the bean name will be used
60      * by default.
61      *
62      * @see #setBeanName(String)
63      */

64     public void setId(String JavaDoc id) {
65         this.id = id;
66     }
67
68     /**
69      * Implementation of the
70      * {@link org.springframework.beans.factory.BeanNameAware BeanNameAware}
71      * interface. If no command id is explicitly set, the bean name will be
72      * used instead. Note that if the name ends with the suffix "Command"
73      * it will be removed from the id.
74      */

75     public void setBeanName(String JavaDoc beanName) {
76         if (id == null) {
77             if (beanName.endsWith(COMMAND_NAME_SUFFIX)) {
78                 beanName = beanName.substring(0, beanName.length() -
79                         COMMAND_NAME_SUFFIX.length());
80             }
81             id = beanName;
82         }
83     }
84     
85     /**
86      * Implementation of the {@link RiotRuntimeAware} interface. Allows
87      * subclasses to call {@link #getRiotServletPrefix()}.
88      */

89     public void setRiotRuntime(RiotRuntime runtime) {
90         this.riotServletPrefix = runtime.getServletPrefix();
91     }
92     
93     protected String JavaDoc getRiotServletPrefix() {
94         return riotServletPrefix;
95     }
96
97     /**
98      * Always returns <code>null</code>. Sublasses may override this method
99      * in order to display a confirmation message before the command is
100      * executed.
101      */

102     public String JavaDoc getConfirmationMessage(CommandContext context) {
103         return null;
104     }
105
106     public boolean isShowOnForm() {
107         return this.showOnForm;
108     }
109
110     public void setShowOnForm(boolean showOnForm) {
111         this.showOnForm = showOnForm;
112     }
113
114     /**
115      *
116      */

117     public CommandState getState(CommandContext context) {
118         CommandState state = new CommandState();
119         state.setId(getId());
120         String JavaDoc action = getAction(context);
121         state.setAction(action);
122         state.setEnabled(isEnabled(context, action));
123         state.setLabel(getLabel(context, action));
124         state.setStyleClass(getStyleClass(context, action));
125         state.setItemStyleClass(getItemStyleClass(context, action));
126         return state;
127     }
128
129     /**
130      * Returns the command's id. Subclasses may override this method if the
131      * action depends on the context.
132      */

133     protected String JavaDoc getAction(CommandContext context) {
134         return getId();
135     }
136
137     /**
138      * Returns a label by resolving the message-key
139      * <code>command.<i>labelKeySuffix</i></code>, where <i>labelKeySuffix</i>
140      * is the String returned by {@link #getLabelKeySuffix(CommandContext, String)}.
141      */

142     protected String JavaDoc getLabel(CommandContext context, String JavaDoc action) {
143         String JavaDoc key = getLabelKeySuffix(context, action);
144         return context.getMessageResolver().getMessage(
145                 COMMAND_MESSAGE_PREFIX + key, null,
146                 FormatUtils.camelToTitleCase(key));
147     }
148
149     /**
150      * Returns the command's action. Subclasses may override this method if the
151      * label depends on the context.
152      *
153      * @see #getLabel(CommandContext, String)
154      */

155     protected String JavaDoc getLabelKeySuffix(CommandContext context, String JavaDoc action) {
156         return action;
157     }
158
159     /**
160      * Returns the CSS class that is assigned to command's HTML element and
161      * therefore defines which icon is displayed. The default implementation
162      * delegates the call to {@link #getAction(CommandContext)}.
163      */

164     protected String JavaDoc getStyleClass(CommandContext context, String JavaDoc action) {
165         return getAction(context);
166     }
167
168     /**
169      * Returns a CSS class that is added to the list of class names of the
170      * whole item/row. The default implementation always returns
171      * <code>null</code>. Subclasses may override this method to highlight
172      * a list item depending on the context.
173      */

174     protected String JavaDoc getItemStyleClass(CommandContext context, String JavaDoc action) {
175         return null;
176     }
177
178     /**
179      * Subclasses may inspect the given context to decide whether the
180      * command should be enabled. Commands don't need to check the
181      * {@link org.riotfamily.riot.security.policy.AuthorizationPolicy policy} since
182      * commands will be automatically disabled if the action returned by
183      * {@link #getAction(CommandContext) getAction()} is denied.
184      * The default implementation always returns <code>true</code>.
185      */

186     protected boolean isEnabled(CommandContext context, String JavaDoc action) {
187         return true;
188     }
189
190 }
191
Popular Tags