KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > riotfamily > riot > list > command > support > 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.support;
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.editor.EditorDefinitionUtils;
30 import org.riotfamily.riot.editor.ListDefinition;
31 import org.riotfamily.riot.list.command.Command;
32 import org.riotfamily.riot.list.command.CommandContext;
33 import org.riotfamily.riot.list.command.CommandState;
34 import org.springframework.beans.factory.BeanNameAware;
35
36 /**
37  * Abstract base class for old-style (pre 6.5) commands.
38  * @deprecated
39  */

40 public abstract class AbstractCommand implements Command, BeanNameAware {
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     public String JavaDoc getId() {
53         return id;
54     }
55     
56     /**
57      * Sets the commandId. If no value is set the bean name will be used
58      * by default.
59      *
60      * @see #setBeanName(String)
61      */

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

73     public void setBeanName(String JavaDoc beanName) {
74         if (id == null) {
75             if (beanName.endsWith(COMMAND_NAME_SUFFIX)) {
76                 beanName = beanName.substring(0, beanName.length() -
77                         COMMAND_NAME_SUFFIX.length());
78             }
79             id = beanName;
80         }
81     }
82     
83     /**
84      * Always returns <code>null</code>. Sublasses may override this method
85      * in order to display a confirmation message before the command is
86      * executed.
87      */

88     public String JavaDoc getConfirmationMessage(CommandContext context) {
89         return null;
90     }
91
92     /**
93      * Returns a label by resolving the message-key
94      * <code>command.<i>labelKeySuffix</i></code>, where <i>labelKeySuffix</i>
95      * is the String returned by {@link #getLabelKeySuffix(CommandContext)}.
96      */

97     public String JavaDoc getLabel(CommandContext context) {
98         String JavaDoc key = getLabelKeySuffix(context);
99         return context.getMessageResolver().getMessage(
100                 COMMAND_MESSAGE_PREFIX + key, null,
101                 FormatUtils.camelToTitleCase(key));
102     }
103     
104     /**
105      * Returns the command's id. Subclasses may override this method if the
106      * label depends on the context.
107      *
108      * @see #getLabel(CommandContext)
109      */

110     protected String JavaDoc getLabelKeySuffix(CommandContext context) {
111         return getId();
112     }
113     
114     /**
115      * Returns the command's id. Subclasses may override this method if the
116      * action depends on the context.
117      */

118     public String JavaDoc getAction(CommandContext context) {
119         return getId();
120     }
121     
122     /**
123      * Returns the command's CSS class. The default implementation delegates
124      * the call to {@link #getAction(CommandContext)}.
125      */

126     public String JavaDoc getStyleClass(CommandContext context) {
127         return getAction(context);
128     }
129     
130     /**
131      * Always returns <code>true</code>. Subclasses may override this method
132      * to highlight a list item depending on the context.
133      *
134      * @since 6.5
135      */

136     public String JavaDoc getItemStyleClass(CommandContext context) {
137         return null;
138     }
139
140     /**
141      * Always returns <code>true</code>. Subclasses may override this method
142      * to disable the command depending on the context.
143      */

144     public boolean isEnabled(CommandContext context) {
145         return true;
146     }
147
148     public boolean isShowOnForm() {
149         return this.showOnForm;
150     }
151
152     public void setShowOnForm(boolean showOnForm) {
153         this.showOnForm = showOnForm;
154     }
155
156     /**
157      * Convenience method that can be used by subclasses to load to parent
158      * object.
159      * @since 6.5
160      */

161     protected Object JavaDoc loadParent(CommandContext context) {
162         String JavaDoc parentId = context.getParentId();
163         if (parentId != null) {
164             ListDefinition listDef = context.getListDefinition();
165             return EditorDefinitionUtils.loadParent(listDef, parentId);
166         }
167         return null;
168     }
169     
170     public CommandState getState(CommandContext context) {
171         CommandState state = new CommandState();
172         state.setId(getId());
173         state.setAction(getAction(context));
174         state.setLabel(getLabel(context));
175         state.setEnabled(isEnabled(context));
176         state.setStyleClass(getStyleClass(context));
177         state.setItemStyleClass(getItemStyleClass(context));
178         return state;
179     }
180     
181 }
182
Popular Tags