001: /*
002: * This is free software, licensed under the Gnu Public License (GPL)
003: * get a copy from <http://www.gnu.org/licenses/gpl.html>
004: * $Id: AbstractPropertyCommand.java,v 1.4 2005/06/18 04:58:13 hzeller Exp $
005: * author: Henner Zeller <H.Zeller@acm.org>
006: */
007: package henplus.commands.properties;
008:
009: import henplus.HenPlus;
010: import henplus.AbstractCommand;
011: import henplus.CommandDispatcher;
012: import henplus.PropertyRegistry;
013: import henplus.SQLSession;
014: import henplus.property.PropertyHolder;
015: import henplus.view.Column;
016: import henplus.view.ColumnMetaData;
017: import henplus.view.TableRenderer;
018: import henplus.view.util.SortedMatchIterator;
019:
020: import java.util.Iterator;
021: import java.util.Map;
022: import java.util.StringTokenizer;
023:
024: /**
025: * The command, that allows to set properties. This abstract
026: * command is used for the session specific and global properties.
027: */
028: public abstract class AbstractPropertyCommand extends AbstractCommand {
029: private final static ColumnMetaData[] PROP_META;
030:
031: static {
032: PROP_META = new ColumnMetaData[3];
033: PROP_META[0] = new ColumnMetaData("Name");
034: PROP_META[1] = new ColumnMetaData("Value");
035: PROP_META[2] = new ColumnMetaData("Description");
036: }
037:
038: /**
039: * returns the command-strings this command can handle.
040: */
041: public String[] getCommandList() {
042: final String setCmd = getSetCommand();
043: return new String[] { setCmd, "re" + setCmd };
044: }
045:
046: /**
047: * returns the name of the command this command reacts on.
048: */
049: protected abstract String getSetCommand();
050:
051: /**
052: * the PropertyRegistry associcaed with the current
053: */
054: protected abstract PropertyRegistry getRegistry();
055:
056: /**
057: * execute the command given.
058: */
059: public int execute(SQLSession currentSession, String cmd,
060: String param) {
061: StringTokenizer st = new StringTokenizer(param);
062: int argc = st.countTokens();
063:
064: if (cmd.startsWith("re")) { // 'reset-property'
065: if (argc == 1) {
066: String name = st.nextToken();
067: PropertyHolder holder;
068: holder = (PropertyHolder) (getRegistry()
069: .getPropertyMap().get(name));
070: if (holder == null) {
071: return EXEC_FAILED;
072: }
073: String defaultValue = holder.getDefaultValue();
074: try {
075: holder.setValue(defaultValue);
076: } catch (Exception e) {
077: HenPlus.msg().println(
078: "setting to default '" + defaultValue
079: + "' failed.");
080: return EXEC_FAILED;
081: }
082: return SUCCESS;
083: }
084: return SYNTAX_ERROR;
085: } else {
086: /*
087: * no args. show available properties
088: */
089: if (argc == 0) {
090: PROP_META[0].resetWidth();
091: PROP_META[1].resetWidth();
092: TableRenderer table = new TableRenderer(PROP_META,
093: HenPlus.out());
094: Iterator propIt = (getRegistry().getPropertyMap()
095: .entrySet().iterator());
096: while (propIt.hasNext()) {
097: Map.Entry entry = (Map.Entry) propIt.next();
098: Column[] row = new Column[3];
099: PropertyHolder holder = (PropertyHolder) entry
100: .getValue();
101: row[0] = new Column((String) entry.getKey());
102: row[1] = new Column(holder.getValue());
103: row[2] = new Column(holder.getShortDescription());
104: table.addRow(row);
105: }
106: table.closeTable();
107: return SUCCESS;
108: }
109:
110: /*
111: * one arg: show help
112: */
113: else if (argc == 1) {
114: String name = st.nextToken();
115: PropertyHolder holder;
116: holder = (PropertyHolder) (getRegistry()
117: .getPropertyMap().get(name));
118: if (holder == null) {
119: return EXEC_FAILED;
120: }
121: printDescription(name, holder);
122: return SUCCESS;
123: }
124:
125: /*
126: * more than one arg
127: */
128: else if (argc >= 2) {
129: String varname = (String) st.nextElement();
130: int pos = 0;
131: int paramLength = param.length();
132: // skip whitespace after 'set'
133: while (pos < paramLength
134: && Character.isWhitespace(param.charAt(pos))) {
135: ++pos;
136: }
137: // skip non-whitespace after 'set ': variable name
138: while (pos < paramLength
139: && !Character.isWhitespace(param.charAt(pos))) {
140: ++pos;
141: }
142: // skip whitespace before vlue..
143: while (pos < paramLength
144: && Character.isWhitespace(param.charAt(pos))) {
145: ++pos;
146: }
147: String value = param.substring(pos);
148: if (value.startsWith("\"") && value.endsWith("\"")) {
149: value = value.substring(1, value.length() - 1);
150: } else if (value.startsWith("\'")
151: && value.endsWith("\'")) {
152: value = value.substring(1, value.length() - 1);
153: }
154:
155: try {
156: getRegistry().setProperty(varname, value);
157: } catch (Exception e) {
158: HenPlus.msg().println(e.getMessage());
159: return EXEC_FAILED;
160: }
161: return SUCCESS;
162: }
163: }
164: return SUCCESS;
165: }
166:
167: private void printDescription(String propName, PropertyHolder prop) {
168:
169: if (prop.getShortDescription() != null) {
170: HenPlus.msg().attributeBold();
171: HenPlus.msg().println("PROPERTY");
172: HenPlus.msg().attributeReset();
173: HenPlus.msg().println(
174: "\t" + propName + " : "
175: + prop.getShortDescription());
176: HenPlus.msg().println();
177: }
178:
179: String desc = prop.getLongDescription();
180: if (desc != null) {
181: HenPlus.msg().attributeBold();
182: HenPlus.msg().println("DESCRIPTION");
183: HenPlus.msg().attributeReset();
184: HenPlus.msg().println(desc);
185: } else {
186: HenPlus.msg().println(
187: "no detailed help for '" + propName + "'");
188: }
189: }
190:
191: /**
192: * complete property names.
193: */
194: public Iterator complete(CommandDispatcher disp,
195: String partialCommand, final String lastWord) {
196: StringTokenizer st = new StringTokenizer(partialCommand);
197: String cmd = st.nextToken();
198: int argc = st.countTokens();
199:
200: if (argc > ("".equals(lastWord) ? 0 : 1)) { /* one arg given */
201: if (getSetCommand().equals(cmd)) {
202: String name = st.nextToken();
203: PropertyHolder holder;
204: holder = (PropertyHolder) (getRegistry()
205: .getPropertyMap().get(name));
206: if (holder == null) {
207: return null;
208: }
209: return holder.completeValue(lastWord);
210: }
211: return null;
212: }
213:
214: return new SortedMatchIterator(lastWord, getRegistry()
215: .getPropertyMap());
216: }
217:
218: protected abstract String getHelpHeader();
219:
220: /**
221: * return a descriptive string.
222: */
223: public String getShortDescription() {
224: return "set " + getHelpHeader() + " properties";
225: }
226:
227: public String getSynopsis(String cmd) {
228: if (cmd.startsWith("re")) {
229: return cmd + " <propery-name>";
230: }
231: return cmd + " [<property-name> [<value>]]";
232: }
233:
234: public String getLongDescription(String cmd) {
235: String dsc = null;
236: if (cmd.startsWith("re")) {
237: dsc = "\tReset the given " + getHelpHeader()
238: + " property\n" + "\tto its default value";
239: } else {
240: dsc = "\tWithout parameters, show available "
241: + getHelpHeader()
242: + "\n"
243: + "\tproperties and their settings.\n\n"
244: + "\tWith only the property name given as parameter,\n"
245: + "\tshow the long help associated with that property.\n\n"
246: + "\tIs the property name followed by a value, the property is\n"
247: + "\tset to that value.";
248: }
249: return dsc;
250: }
251: }
252:
253: /*
254: * Local variables:
255: * c-basic-offset: 4
256: * compile-command: "ant -emacs -find build.xml"
257: * End:
258: */
|