001: /*
002: * Copyright 2006-2007 Wolfgang S. Kechel
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.hecl.midp20.lcdui;
018:
019: import org.hecl.ClassCommand;
020: import org.hecl.ClassCommandInfo;
021: import org.hecl.HeclException;
022: import org.hecl.Interp;
023: import org.hecl.ObjectThing;
024: import org.hecl.Properties;
025: import org.hecl.Thing;
026:
027: public abstract class OptionCmd implements ClassCommand,
028: org.hecl.Command {
029: protected OptionCmd() {
030: }
031:
032: public Object setInstanceProperties(Interp ip, Object target,
033: Properties p) throws HeclException {
034: if (p != null) {
035: Thing optargs[] = p.getProps();
036: configure(ip, target, optargs, 0, optargs.length);
037: }
038: return target;
039: }
040:
041: public Thing method(Interp ip, ClassCommandInfo context,
042: Thing[] argv) throws HeclException {
043: if (argv.length > 1) {
044: String subcmd = argv[1].toString().toLowerCase();
045: Object target = ObjectThing.get(argv[0]);
046:
047: //System.out.println("OptionCmd::method("+target+", subcmd="+subcmd);
048:
049: if (subcmd.equals(WidgetInfo.NCGET)) {
050: if (argv.length != 3) {
051: throw HeclException.createWrongNumArgsException(
052: argv, 2, "option");
053: }
054: return cget(ip, target, argv[2].toString());
055: } else if (subcmd.equals(WidgetInfo.NCONF)
056: || subcmd.equals(WidgetInfo.NCONFIGURE)) {
057: configure(ip, target, argv, 2, argv.length - 2);
058: return null;
059: }
060: return handlecmd(ip, target, subcmd, argv, 2);
061: }
062: throw HeclException.createWrongNumArgsException(argv, 2,
063: "Object method [arg...]");
064: }
065:
066: public void configure(Interp ip, Object target, Thing[] argv,
067: int start, int n) throws HeclException {
068: if (n < 0 || n % 2 != 0) {
069: throw new HeclException("configure needs name-value pairs");
070: }
071: // deal with option/value pairs
072: for (int i = start; n > 0; n -= 2, i += 2) {
073: cset(ip, target, argv[i].toString().toLowerCase(),
074: argv[i + 1]);
075: }
076: }
077:
078: private static HeclException optex(String optname) {
079: return new HeclException("Unknown option '" + optname + "'");
080: }
081:
082: private static HeclException itemoptex(String optname)
083: throws HeclException {
084: return new HeclException("Unknown item option '" + optname
085: + "'");
086: }
087:
088: protected Thing cget(Interp ip, Object target, String optname)
089: throws HeclException {
090: throw optex(optname);
091: }
092:
093: protected void cset(Interp ip, Object target, String optname,
094: Thing optval) throws HeclException {
095: throw optex(optname);
096: }
097:
098: protected Thing itemcget(Interp ip, Object target, int itemno,
099: String optname) throws HeclException {
100: throw itemoptex("item cget " + optname);
101: }
102:
103: protected void itemcset(Interp ip, Object target, int itemno,
104: String optname, Thing optval) throws HeclException {
105: throw itemoptex("item cset " + optname);
106: }
107:
108: protected Thing handlecmd(Interp ip, Object target, String subcmd,
109: Thing[] argv, int startat) throws HeclException {
110: throw new HeclException("invalid subcommand '" + subcmd + "'");
111: }
112: }
113:
114: // Variables:
115: // mode:java
116: // coding:utf-8
117: // End:
|