1 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 40 public abstract class AbstractCommand implements Command, BeanNameAware { 41 42 private static final String COMMAND_NAME_SUFFIX = "Command"; 43 44 private final String COMMAND_MESSAGE_PREFIX = "command."; 45 46 protected Log log = LogFactory.getLog(getClass()); 47 48 private String id; 49 50 private boolean showOnForm; 51 52 public String getId() { 53 return id; 54 } 55 56 62 public void setId(String id) { 63 this.id = id; 64 } 65 66 73 public void setBeanName(String 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 88 public String getConfirmationMessage(CommandContext context) { 89 return null; 90 } 91 92 97 public String getLabel(CommandContext context) { 98 String key = getLabelKeySuffix(context); 99 return context.getMessageResolver().getMessage( 100 COMMAND_MESSAGE_PREFIX + key, null, 101 FormatUtils.camelToTitleCase(key)); 102 } 103 104 110 protected String getLabelKeySuffix(CommandContext context) { 111 return getId(); 112 } 113 114 118 public String getAction(CommandContext context) { 119 return getId(); 120 } 121 122 126 public String getStyleClass(CommandContext context) { 127 return getAction(context); 128 } 129 130 136 public String getItemStyleClass(CommandContext context) { 137 return null; 138 } 139 140 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 161 protected Object loadParent(CommandContext context) { 162 String 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 |