1 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 39 public abstract class AbstractCommand implements Command, BeanNameAware, 40 RiotRuntimeAware { 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 private String riotServletPrefix; 53 54 public String getId() { 55 return id; 56 } 57 58 64 public void setId(String id) { 65 this.id = id; 66 } 67 68 75 public void setBeanName(String 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 89 public void setRiotRuntime(RiotRuntime runtime) { 90 this.riotServletPrefix = runtime.getServletPrefix(); 91 } 92 93 protected String getRiotServletPrefix() { 94 return riotServletPrefix; 95 } 96 97 102 public String 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 117 public CommandState getState(CommandContext context) { 118 CommandState state = new CommandState(); 119 state.setId(getId()); 120 String 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 133 protected String getAction(CommandContext context) { 134 return getId(); 135 } 136 137 142 protected String getLabel(CommandContext context, String action) { 143 String key = getLabelKeySuffix(context, action); 144 return context.getMessageResolver().getMessage( 145 COMMAND_MESSAGE_PREFIX + key, null, 146 FormatUtils.camelToTitleCase(key)); 147 } 148 149 155 protected String getLabelKeySuffix(CommandContext context, String action) { 156 return action; 157 } 158 159 164 protected String getStyleClass(CommandContext context, String action) { 165 return getAction(context); 166 } 167 168 174 protected String getItemStyleClass(CommandContext context, String action) { 175 return null; 176 } 177 178 186 protected boolean isEnabled(CommandContext context, String action) { 187 return true; 188 } 189 190 } 191 | Popular Tags |