001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * If you wish your version of this file to be governed by only the CDDL
025: * or only the GPL Version 2, indicate your decision by adding
026: * "[Contributor] elects to include this software in this distribution
027: * under the [CDDL or GPL Version 2] license." If you do not indicate a
028: * single choice of license, a recipient has the option to distribute
029: * your version of this file under either the CDDL, the GPL Version 2 or
030: * to extend the choice of license to its licensees as provided above.
031: * However, if you add GPL Version 2 code and therefore, elected the GPL
032: * Version 2 license, then the option applies only if the new code is
033: * made subject to such option by the copyright holder.
034: *
035: * Contributor(s):
036: *
037: * Portions Copyrighted 2008 Sun Microsystems, Inc.
038: */
039:
040: package org.netbeans.modules.cnd.editor.options;
041:
042: import java.beans.PropertyEditor;
043: import java.beans.PropertyEditorSupport;
044: import org.netbeans.modules.cnd.editor.api.CodeStyle;
045: import org.openide.nodes.PropertySupport;
046: import org.openide.util.NbBundle;
047:
048: /**
049: *
050: * @author Alexander Simon
051: */
052: public class PreprocessorIndentProperty extends
053: PropertySupport.ReadWrite<CodeStyle.PreprocessorIndent> {
054:
055: private final CodeStyle.Language language;
056: private final String optionID;
057: private PreviewPreferences preferences;
058: private CodeStyle.PreprocessorIndent state;
059: private PropertyEditor editor;
060:
061: public PreprocessorIndentProperty(CodeStyle.Language language,
062: PreviewPreferences preferences, String optionID) {
063: super (optionID, CodeStyle.PreprocessorIndent.class,
064: getString("LBL_" + optionID), getString("HINT_"
065: + optionID)); // NOI18N
066: this .language = language;
067: this .optionID = optionID;
068: this .preferences = preferences;
069: init();
070: }
071:
072: private static String getString(String key) {
073: return NbBundle.getMessage(BracePlacementProperty.class, key);
074: }
075:
076: private void init() {
077: state = CodeStyle.PreprocessorIndent.valueOf(getPreferences()
078: .get(optionID, getDefault().name()));
079: }
080:
081: private CodeStyle.PreprocessorIndent getDefault() {
082: return CodeStyle.PreprocessorIndent
083: .valueOf((String) EditorOptions.getDefault(
084: getPreferences().getLanguage(),
085: getPreferences().getStyleId(), optionID));
086: }
087:
088: private PreviewPreferences getPreferences() {
089: return preferences;
090: }
091:
092: @Override
093: public String getHtmlDisplayName() {
094: if (!isDefaultValue()) {
095: return "<b>" + getDisplayName(); // NOI18N
096: }
097: return null;
098: }
099:
100: public CodeStyle.PreprocessorIndent getValue() {
101: return state;
102: }
103:
104: public void setValue(CodeStyle.PreprocessorIndent v) {
105: state = v;
106: getPreferences().put(optionID, state.name());
107: }
108:
109: @Override
110: public void restoreDefaultValue() {
111: setValue(getDefault());
112: }
113:
114: @Override
115: public boolean supportsDefaultValue() {
116: return true;
117: }
118:
119: @Override
120: public boolean isDefaultValue() {
121: return getDefault().equals(getValue());
122: }
123:
124: @Override
125: public PropertyEditor getPropertyEditor() {
126: if (editor == null) {
127: editor = new PreprocessorIndentEditor();
128: }
129: return editor;
130: }
131:
132: private static class PreprocessorIndentEditor extends
133: PropertyEditorSupport {
134: @Override
135: public String[] getTags() {
136: try {
137: CodeStyle.PreprocessorIndent[] values = CodeStyle.PreprocessorIndent
138: .values();
139: String[] tags = new String[values.length];
140: for (int i = 0; i < values.length; i++) {
141: tags[i] = values[i].toString();
142: }
143: return tags;
144: } catch (Exception x) {
145: throw new AssertionError(x);
146: }
147: }
148:
149: @Override
150: public String getAsText() {
151: Object o = getValue();
152: return o != null ? o.toString() : ""; // NOI18N
153: }
154:
155: @Override
156: public void setAsText(String text) {
157: if (text.length() > 0) {
158: CodeStyle.PreprocessorIndent[] values = CodeStyle.PreprocessorIndent
159: .values();
160: for (int i = 0; i < values.length; i++) {
161: if (values[i].toString().equals(text)) {
162: setValue(values[i]);
163: return;
164: }
165: }
166: }
167: setValue(null);
168: }
169:
170: @Override
171: public String getJavaInitializationString() {
172: CodeStyle.PreprocessorIndent e = (CodeStyle.PreprocessorIndent) getValue();
173: return e != null ? CodeStyle.PreprocessorIndent.class
174: .getName().replace('$', '.')
175: + '.' + e.name() : "null"; // NOI18N
176: }
177: }
178: }
|