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 BracePlacementProperty extends
053: PropertySupport.ReadWrite<CodeStyle.BracePlacement> {
054:
055: private final CodeStyle.Language language;
056: private final String optionID;
057: private PreviewPreferences preferences;
058: private CodeStyle.BracePlacement state;
059: private PropertyEditor editor;
060:
061: public BracePlacementProperty(CodeStyle.Language language,
062: PreviewPreferences preferences, String optionID) {
063: super (optionID, CodeStyle.BracePlacement.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.BracePlacement.valueOf(getPreferences().get(
078: optionID, getDefault().name()));
079: }
080:
081: private CodeStyle.BracePlacement getDefault() {
082: return CodeStyle.BracePlacement.valueOf((String) EditorOptions
083: .getDefault(getPreferences().getLanguage(),
084: getPreferences().getStyleId(), optionID));
085: }
086:
087: private PreviewPreferences getPreferences() {
088: return preferences;
089: }
090:
091: @Override
092: public String getHtmlDisplayName() {
093: if (!isDefaultValue()) {
094: return "<b>" + getDisplayName(); // NOI18N
095: }
096: return null;
097: }
098:
099: public CodeStyle.BracePlacement getValue() {
100: return state;
101: }
102:
103: public void setValue(CodeStyle.BracePlacement v) {
104: state = v;
105: getPreferences().put(optionID, state.name());
106: }
107:
108: @Override
109: public void restoreDefaultValue() {
110: setValue(getDefault());
111: }
112:
113: @Override
114: public boolean supportsDefaultValue() {
115: return true;
116: }
117:
118: @Override
119: public boolean isDefaultValue() {
120: return getDefault().equals(getValue());
121: }
122:
123: @Override
124: public PropertyEditor getPropertyEditor() {
125: if (editor == null) {
126: editor = new BracePlacementEditor();
127: }
128: return editor;
129: }
130:
131: private static class BracePlacementEditor extends
132: PropertyEditorSupport {
133: @Override
134: public String[] getTags() {
135: try {
136: CodeStyle.BracePlacement[] values = CodeStyle.BracePlacement
137: .values();
138: String[] tags = new String[values.length];
139: for (int i = 0; i < values.length; i++) {
140: tags[i] = values[i].toString();
141: }
142: return tags;
143: } catch (Exception x) {
144: throw new AssertionError(x);
145: }
146: }
147:
148: @Override
149: public String getAsText() {
150: Object o = getValue();
151: return o != null ? o.toString() : ""; // NOI18N
152: }
153:
154: @Override
155: public void setAsText(String text) {
156: if (text.length() > 0) {
157: CodeStyle.BracePlacement[] values = CodeStyle.BracePlacement
158: .values();
159: for (int i = 0; i < values.length; i++) {
160: if (values[i].toString().equals(text)) {
161: setValue(values[i]);
162: return;
163: }
164: }
165: }
166: setValue(null);
167: }
168:
169: @Override
170: public String getJavaInitializationString() {
171: CodeStyle.BracePlacement e = (CodeStyle.BracePlacement) getValue();
172: return e != null ? CodeStyle.BracePlacement.class.getName()
173: .replace('$', '.')
174: + '.' + e.name() : "null"; // NOI18N
175: }
176: }
177: }
|