1 /*******************************************************************************
2  *  Copyright (c) 2000, 2011 IBM Corporation and others.
3  *
4  *  This program and the accompanying materials
5  *  are made available under the terms of the Eclipse Public License 2.0
6  *  which accompanies this distribution, and is available at
7  *  https://www.eclipse.org/legal/epl-2.0/
8  *
9  *  SPDX-License-Identifier: EPL-2.0
10  *
11  *  Contributors:
12  *     IBM Corporation - initial API and implementation
13  *******************************************************************************/
14 package org.eclipse.pde.ui.templates;
15 
16 /**
17  * The base class of all the template options. Options have unique name and a
18  * value that can be changed. The value of the option is automatically available
19  * to the template files - can be accessed by substitution (e.g. $value_name$)
20  * or as part of conditional code generation (e.g. if value_name).
21  *
22  * @since 2.0
23  */
24 public abstract class TemplateOption extends TemplateField {
25 	private String name;
26 	private Object value;
27 	private boolean enabled = true;
28 	private boolean required;
29 
30 	/**
31 	 * Creates a new option for the provided template section.
32 	 *
33 	 * @param section
34 	 *            the parent template section
35 	 * @param name
36 	 *            the unique name of this option
37 	 * @param label
38 	 *            presentable label of this option
39 	 */
TemplateOption(BaseOptionTemplateSection section, String name, String label)40 	public TemplateOption(BaseOptionTemplateSection section, String name, String label) {
41 		super(section, label);
42 		this.name = name;
43 	}
44 
45 	/**
46 	 * Returns the unique name of this option
47 	 *
48 	 * @return option name
49 	 */
getName()50 	public String getName() {
51 		return name;
52 	}
53 
54 	/**
55 	 * Changes the unique name of this option
56 	 *
57 	 * @param name
58 	 *            the new option name
59 	 */
setName(String name)60 	public void setName(String name) {
61 		this.name = name;
62 	}
63 
64 	/**
65 	 * Returns the value of this option.
66 	 *
67 	 * @return the current value
68 	 */
getValue()69 	public Object getValue() {
70 		return value;
71 	}
72 
73 	/**
74 	 * Returns whether this option is currently empty. The actual semantics of
75 	 * the result depends on the implementing option.
76 	 *
77 	 * @return <samp>true</samp> if option is empty, <samp>false</samp>
78 	 *         otherwise.
79 	 */
isEmpty()80 	public boolean isEmpty() {
81 		return false;
82 	}
83 
84 	/**
85 	 * Marks this option as required. Required options must be set by the user.
86 	 * An option that is empty and is marked required will be flagged as an
87 	 * error in the wizard.
88 	 *
89 	 * @param required
90 	 *            the new value of the property
91 	 * @see #isEmpty
92 	 */
setRequired(boolean required)93 	public void setRequired(boolean required) {
94 		this.required = required;
95 	}
96 
97 	/**
98 	 * Returns whether this option is required (cannot be empty)
99 	 *
100 	 * @return <samp>true </samp> if this option is required, <samp>false
101 	 *         </samp> otherwise.
102 	 */
isRequired()103 	public boolean isRequired() {
104 		return required;
105 	}
106 
107 	/**
108 	 * Sets the new value of this option.
109 	 *
110 	 * @param value
111 	 *            the new value
112 	 */
setValue(Object value)113 	public void setValue(Object value) {
114 		this.value = value;
115 	}
116 
117 	/**
118 	 * Returns whether this option is enabled. The actual presentation of
119 	 * enabled state depends on the implementing option.
120 	 *
121 	 * @return <samp>true </samp> if option is enabled and can be modified.
122 	 */
isEnabled()123 	public boolean isEnabled() {
124 		return enabled;
125 	}
126 
127 	/**
128 	 * Sets the enabled state of this option. The action presentation of the
129 	 * enabled state depends on the implementing option.
130 	 *
131 	 * @param enabled
132 	 *            the new enabled state
133 	 */
setEnabled(boolean enabled)134 	public void setEnabled(boolean enabled) {
135 		this.enabled = enabled;
136 	}
137 
138 	/**
139 	 * Returns the label of this option that can be presented in the messages to
140 	 * the user. The default implementation trims the 'label' property from
141 	 * mnemonics and from the trailing column.
142 	 *
143 	 * @return the label to show to the user
144 	 */
getMessageLabel()145 	public String getMessageLabel() {
146 		String label = getLabel();
147 		StringBuilder buf = new StringBuilder();
148 		for (int i = 0; i < label.length(); i++) {
149 			char c = label.charAt(i);
150 			if (c == '(' && i < label.length() - 1) {
151 				char c2 = label.charAt(i + 1);
152 				if (c2 == '&') {
153 					// DBCS mnemonic sequence "(&<char>)"
154 					// It is OK to truncate the label
155 					// at this point
156 					break;
157 				}
158 			}
159 			if (c != '&' && c != ':')
160 				buf.append(c);
161 		}
162 		return buf.toString();
163 	}
164 }
165