001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.pde.ui.templates;
011:
012: /**
013: * The base class of all the template options. Options have unique name and a
014: * value that can be changed. The value of the option is automatically available
015: * to the template files - can be accessed by substitution (e.g. $value_name$)
016: * or as part of conditional code generation (e.g. if value_name).
017: *
018: * @since 2.0
019: */
020: public abstract class TemplateOption extends TemplateField {
021: private String name;
022: private Object value;
023: private boolean enabled = true;
024: private boolean required;
025:
026: /**
027: * Creates a new option for the provided template section.
028: *
029: * @param section
030: * the parent template section
031: * @param name
032: * the unique name of this option
033: * @param label
034: * presentable label of this option
035: */
036: public TemplateOption(BaseOptionTemplateSection section,
037: String name, String label) {
038: super (section, label);
039: this .name = name;
040: }
041:
042: /**
043: * Returns the unique name of this option
044: *
045: * @return option name
046: */
047: public String getName() {
048: return name;
049: }
050:
051: /**
052: * Changes the unique name of this option
053: *
054: * @param name
055: * the new option name
056: */
057: public void setName(String name) {
058: this .name = name;
059: }
060:
061: /**
062: * Returns the value of this option.
063: *
064: * @return the current value
065: */
066: public Object getValue() {
067: return value;
068: }
069:
070: /**
071: * Returns whether this option is currently empty. The actual semantics of
072: * the result depends on the implementing option.
073: *
074: * @return <samp>true </samp> if option is empty, </samp> false otherwise.
075: */
076: public boolean isEmpty() {
077: return false;
078: }
079:
080: /**
081: * Marks this option as required. Required options must be set by the user.
082: * An option that is empty and is marked required will be flagged as an
083: * error in the wizard.
084: *
085: * @param required
086: * the new value of the property
087: * @see #isEmpty
088: */
089: public void setRequired(boolean required) {
090: this .required = required;
091: }
092:
093: /**
094: * Returns whether this option is required (cannot be empty)
095: *
096: * @return <samp>true </samp> if this option is required, <samp>false
097: * </samp> otherwise.
098: */
099: public boolean isRequired() {
100: return required;
101: }
102:
103: /**
104: * Sets the new value of this option.
105: *
106: * @param value
107: * the new value
108: */
109: public void setValue(Object value) {
110: this .value = value;
111: }
112:
113: /**
114: * Returns whether this option is enabled. The actual presentation of
115: * enabled state depends on the implementing option.
116: *
117: * @return <samp>true </samp> if option is enabled and can be modified.
118: */
119: public boolean isEnabled() {
120: return enabled;
121: }
122:
123: /**
124: * Sets the enabled state of this option. The action presentation of the
125: * enabled state depends on the implementing option.
126: *
127: * @param enabled
128: * the new enabled state
129: */
130: public void setEnabled(boolean enabled) {
131: this .enabled = enabled;
132: }
133:
134: /**
135: * Returns the label of this option that can be presented in the messages to
136: * the user. The default implementation trims the 'label' property from
137: * mnemonics and from the trailing column.
138: */
139: public String getMessageLabel() {
140: String label = getLabel();
141: StringBuffer buf = new StringBuffer();
142: for (int i = 0; i < label.length(); i++) {
143: char c = label.charAt(i);
144: if (c == '(' && i < label.length() - 1) {
145: char c2 = label.charAt(i + 1);
146: if (c2 == '&') {
147: // DBCS mnemonic sequence "(&<char>)"
148: // It is OK to truncate the label
149: // at this point
150: break;
151: }
152: }
153: if (c != '&' && c != ':')
154: buf.append(c);
155: }
156: return buf.toString();
157: }
158: }
|