Source Code Cross Referenced for AbstractPropertyCommand.java in  » Database-Client » Henplus » henplus » commands » properties » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Database Client » Henplus » henplus.commands.properties 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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:         */
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.