Search in sources :

Example 1 with PropertyHolder

use of henplus.property.PropertyHolder in project henplus by neurolabs.

the class AbstractPropertyCommand method execute.

/**
     * execute the command given.
     */
@Override
public int execute(final SQLSession currentSession, final String cmd, final String param) {
    final StringTokenizer st = new StringTokenizer(param);
    final int argc = st.countTokens();
    if (cmd.startsWith("re")) {
        // 'reset-property'
        if (argc == 1) {
            final String name = st.nextToken();
            PropertyHolder holder;
            holder = getRegistry().getPropertyMap().get(name);
            if (holder == null) {
                return EXEC_FAILED;
            }
            final String defaultValue = holder.getDefaultValue();
            try {
                holder.setValue(defaultValue);
            } catch (final Exception e) {
                HenPlus.msg().println("setting to default '" + defaultValue + "' failed.");
                return EXEC_FAILED;
            }
            return SUCCESS;
        }
        return SYNTAX_ERROR;
    } else {
        /*
             * no args. show available properties
             */
        if (argc == 0) {
            PROP_META[0].resetWidth();
            PROP_META[1].resetWidth();
            final TableRenderer table = new TableRenderer(PROP_META, HenPlus.out());
            for (Map.Entry<String, PropertyHolder> entry : getRegistry().getPropertyMap().entrySet()) {
                final Column[] row = new Column[3];
                final PropertyHolder holder = entry.getValue();
                row[0] = new Column(entry.getKey());
                row[1] = new Column(holder.getValue());
                row[2] = new Column(holder.getShortDescription());
                table.addRow(row);
            }
            table.closeTable();
            return SUCCESS;
        } else if (argc == 1) {
            /*
                 * one arg: show help
                 */
            final String name = st.nextToken();
            PropertyHolder holder;
            holder = getRegistry().getPropertyMap().get(name);
            if (holder == null) {
                return EXEC_FAILED;
            }
            printDescription(name, holder);
            return SUCCESS;
        } else if (argc >= 2) {
            /*
                 * more than one arg
                 */
            final String varname = (String) st.nextElement();
            int pos = 0;
            final int paramLength = param.length();
            // skip whitespace after 'set'
            while (pos < paramLength && Character.isWhitespace(param.charAt(pos))) {
                ++pos;
            }
            // skip non-whitespace after 'set ': variable name
            while (pos < paramLength && !Character.isWhitespace(param.charAt(pos))) {
                ++pos;
            }
            // skip whitespace before vlue..
            while (pos < paramLength && Character.isWhitespace(param.charAt(pos))) {
                ++pos;
            }
            String value = param.substring(pos);
            if (value.startsWith("\"") && value.endsWith("\"")) {
                value = value.substring(1, value.length() - 1);
            } else if (value.startsWith("\'") && value.endsWith("\'")) {
                value = value.substring(1, value.length() - 1);
            }
            try {
                getRegistry().setProperty(varname, value);
            } catch (final Exception e) {
                HenPlus.msg().println(e.getMessage());
                return EXEC_FAILED;
            }
            return SUCCESS;
        }
    }
    return SUCCESS;
}
Also used : TableRenderer(henplus.view.TableRenderer) StringTokenizer(java.util.StringTokenizer) PropertyHolder(henplus.property.PropertyHolder) Column(henplus.view.Column) Map(java.util.Map)

Example 2 with PropertyHolder

use of henplus.property.PropertyHolder in project henplus by neurolabs.

the class PropertyRegistry method setProperty.

/**
     * sets the Property to the given value. This throws an Exception, if the PropertyHolder vetoes this attempt or if there is
     * simply no Property bound to the given name.
     * 
     * @param name
     *            the name the property is bound to.
     * @param value
     *            the new value of the property to be set.
     * @throws Exception
     *             , if the property does not exist or throws an Exception to veto the new value.
     */
public void setProperty(final String name, final String value) throws Exception {
    final PropertyHolder holder = _namedProperties.get(name);
    if (holder == null) {
        throw new IllegalArgumentException("unknown property '" + name + "'");
    }
    holder.setValue(value);
}
Also used : PropertyHolder(henplus.property.PropertyHolder)

Example 3 with PropertyHolder

use of henplus.property.PropertyHolder in project henplus by neurolabs.

the class AbstractPropertyCommand method complete.

/**
     * complete property names.
     */
@Override
public Iterator<String> complete(final CommandDispatcher disp, final String partialCommand, final String lastWord) {
    final StringTokenizer st = new StringTokenizer(partialCommand);
    final String cmd = st.nextToken();
    final int argc = st.countTokens();
    if (argc > ("".equals(lastWord) ? 0 : 1)) {
        /* one arg given */
        if (getSetCommand().equals(cmd)) {
            final String name = st.nextToken();
            PropertyHolder holder;
            holder = getRegistry().getPropertyMap().get(name);
            if (holder == null) {
                return null;
            }
            return holder.completeValue(lastWord);
        }
        return null;
    }
    return new SortedMatchIterator(lastWord, getRegistry().getPropertyMap());
}
Also used : StringTokenizer(java.util.StringTokenizer) PropertyHolder(henplus.property.PropertyHolder) SortedMatchIterator(henplus.view.util.SortedMatchIterator)

Example 4 with PropertyHolder

use of henplus.property.PropertyHolder in project henplus by neurolabs.

the class PropertyCommand method shutdown.

@Override
public void shutdown() {
    final Map<String, String> writeMap = new HashMap<String, String>();
    for (Map.Entry<String, PropertyHolder> entry : _registry.getPropertyMap().entrySet()) {
        final PropertyHolder holder = entry.getValue();
        writeMap.put(entry.getKey(), holder.getValue());
    }
    _config.storeProperties(writeMap, true, "user properties");
}
Also used : PropertyHolder(henplus.property.PropertyHolder) HashMap(java.util.HashMap) Map(java.util.Map) HashMap(java.util.HashMap)

Aggregations

PropertyHolder (henplus.property.PropertyHolder)4 Map (java.util.Map)2 StringTokenizer (java.util.StringTokenizer)2 Column (henplus.view.Column)1 TableRenderer (henplus.view.TableRenderer)1 SortedMatchIterator (henplus.view.util.SortedMatchIterator)1 HashMap (java.util.HashMap)1