001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2008 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.java.hints.errors;
041:
042: import com.sun.source.tree.Tree.Kind;
043: import com.sun.source.util.TreePath;
044: import java.util.EnumMap;
045: import java.util.EnumSet;
046: import java.util.List;
047: import java.util.Map;
048: import java.util.Set;
049: import java.util.prefs.Preferences;
050: import javax.swing.JComponent;
051: import org.netbeans.api.java.source.CompilationInfo;
052: import org.netbeans.modules.java.hints.spi.AbstractHint;
053: import org.netbeans.spi.editor.hints.ErrorDescription;
054: import org.openide.filesystems.FileObject;
055: import org.openide.util.NbBundle;
056:
057: /**
058: *
059: * @author Jan Lahoda
060: */
061: public class ErrorFixesFakeHint extends AbstractHint {
062:
063: private FixKind kind;
064:
065: private ErrorFixesFakeHint(FixKind kind) {
066: super (true, false, null);
067: this .kind = kind;
068: }
069:
070: @Override
071: public String getDescription() {
072: return NbBundle.getMessage(ErrorFixesFakeHint.class,
073: "DESC_ErrorFixesFakeHint" + kind.name());
074: }
075:
076: public Set<Kind> getTreeKinds() {
077: return EnumSet.noneOf(Kind.class);
078: }
079:
080: public List<ErrorDescription> run(CompilationInfo compilationInfo,
081: TreePath treePath) {
082: return null;//should not be called
083: }
084:
085: public String getId() {
086: return ErrorFixesFakeHint.class.getName() + kind.name();
087: }
088:
089: public String getDisplayName() {
090: return NbBundle.getMessage(ErrorFixesFakeHint.class,
091: "DN_ErrorFixesFakeHint" + kind.name());
092: }
093:
094: public void cancel() {
095: }
096:
097: @Override
098: public JComponent getCustomizer(Preferences node) {
099: switch (kind) {
100: case CREATE_LOCAL_VARIABLE:
101: return new LocalVariableFixCustomizer(node);
102: case SURROUND_WITH_TRY_CATCH:
103: return new SurroundWithTryCatchLog(node);
104: }
105: return super .getCustomizer(node);
106: }
107:
108: public static enum FixKind {
109: SURROUND_WITH_TRY_CATCH, CREATE_LOCAL_VARIABLE;
110: }
111:
112: private static Map<FixKind, ErrorFixesFakeHint> kind2Hint = new EnumMap<FixKind, ErrorFixesFakeHint>(
113: FixKind.class);
114:
115: private static synchronized ErrorFixesFakeHint getHint(FixKind kind) {
116: ErrorFixesFakeHint h = kind2Hint.get(kind);
117:
118: if (h == null) {
119: kind2Hint.put(kind, h = new ErrorFixesFakeHint(kind));
120: }
121:
122: return h;
123: }
124:
125: public static boolean enabled(FixKind kind) {
126: return getHint(kind).isEnabled();
127: }
128:
129: public static boolean isCreateLocalVariableInPlace() {
130: return getHint(FixKind.CREATE_LOCAL_VARIABLE).getPreferences(
131: null).getBoolean(LOCAL_VARIABLES_INPLACE, true);
132: }
133:
134: public static void setCreateLocalVariableInPlace(boolean v) {
135: setCreateLocalVariableInPlace(getHint(
136: FixKind.CREATE_LOCAL_VARIABLE).getPreferences(null), v);
137: }
138:
139: public static void setCreateLocalVariableInPlace(Preferences p,
140: boolean v) {
141: p.putBoolean(LOCAL_VARIABLES_INPLACE, v);
142: }
143:
144: public static boolean isUseExceptions() {
145: return getHint(FixKind.SURROUND_WITH_TRY_CATCH).getPreferences(
146: null).getBoolean(SURROUND_USE_EXCEPTIONS, true);
147: }
148:
149: public static void setUseExceptions(boolean v) {
150: setUseExceptions(getHint(FixKind.SURROUND_WITH_TRY_CATCH)
151: .getPreferences(null), v);
152: }
153:
154: public static void setUseExceptions(Preferences p, boolean v) {
155: p.putBoolean(SURROUND_USE_EXCEPTIONS, v);
156: }
157:
158: public static boolean isUseLogger() {
159: return getHint(FixKind.SURROUND_WITH_TRY_CATCH).getPreferences(
160: null).getBoolean(SURROUND_USE_JAVA_LOGGER, true);
161: }
162:
163: public static void setUseLogger(boolean v) {
164: setUseLogger(getHint(FixKind.SURROUND_WITH_TRY_CATCH)
165: .getPreferences(null), v);
166: }
167:
168: public static void setUseLogger(Preferences p, boolean v) {
169: p.putBoolean(SURROUND_USE_JAVA_LOGGER, v);
170: }
171:
172: public static final String LOCAL_VARIABLES_INPLACE = "create-local-variables-in-place";
173: public static final String SURROUND_USE_EXCEPTIONS = "surround-try-catch-org-openide-util-Exceptions";
174: public static final String SURROUND_USE_JAVA_LOGGER = "surround-try-catch-java-util-logging-Logger";
175:
176: public static ErrorFixesFakeHint create(FileObject file) {
177: if (file.getName().endsWith("surround")) {
178: return getHint(FixKind.SURROUND_WITH_TRY_CATCH);
179: }
180: if (file.getName().endsWith("local")) {
181: return getHint(FixKind.CREATE_LOCAL_VARIABLE);
182: }
183:
184: throw new IllegalArgumentException();
185: }
186: }
|