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.jdt.internal.ui.preferences;
011:
012: import java.util.StringTokenizer;
013:
014: import org.eclipse.jface.preference.IPreferenceStore;
015:
016: import org.eclipse.jdt.core.IJavaProject;
017:
018: import org.eclipse.jdt.ui.PreferenceConstants;
019:
020: import org.eclipse.jdt.internal.corext.codemanipulation.CodeGenerationSettings;
021: import org.eclipse.jdt.internal.corext.util.CodeFormatterUtil;
022:
023: public class JavaPreferencesSettings {
024:
025: public static CodeGenerationSettings getCodeGenerationSettings(
026: IJavaProject project) {
027: CodeGenerationSettings res = new CodeGenerationSettings();
028: res.createComments = Boolean.valueOf(
029: PreferenceConstants.getPreference(
030: PreferenceConstants.CODEGEN_ADD_COMMENTS,
031: project)).booleanValue();
032: res.useKeywordThis = Boolean.valueOf(
033: PreferenceConstants.getPreference(
034: PreferenceConstants.CODEGEN_KEYWORD_THIS,
035: project)).booleanValue();
036: res.overrideAnnotation = Boolean
037: .valueOf(
038: PreferenceConstants
039: .getPreference(
040: PreferenceConstants.CODEGEN_USE_OVERRIDE_ANNOTATION,
041: project)).booleanValue();
042: res.importOrder = getImportOrderPreference(project);
043: res.importThreshold = getImportNumberThreshold(project);
044: res.staticImportThreshold = getStaticImportNumberThreshold(project);
045: res.importIgnoreLowercase = Boolean.valueOf(
046: PreferenceConstants.getPreference(
047: PreferenceConstants.ORGIMPORTS_IGNORELOWERCASE,
048: project)).booleanValue();
049: res.tabWidth = CodeFormatterUtil.getTabWidth(project);
050: res.indentWidth = CodeFormatterUtil.getIndentWidth(project);
051: return res;
052: }
053:
054: /**
055: * @deprecated Use getCodeGenerationSettings(IJavaProject) instead
056: */
057: public static CodeGenerationSettings getCodeGenerationSettings() {
058: return getCodeGenerationSettings(null);
059: }
060:
061: public static int getImportNumberThreshold(IJavaProject project) {
062: String thresholdStr = PreferenceConstants.getPreference(
063: PreferenceConstants.ORGIMPORTS_ONDEMANDTHRESHOLD,
064: project);
065: try {
066: int threshold = Integer.parseInt(thresholdStr);
067: if (threshold < 0) {
068: threshold = Integer.MAX_VALUE;
069: }
070: return threshold;
071: } catch (NumberFormatException e) {
072: return Integer.MAX_VALUE;
073: }
074: }
075:
076: public static int getStaticImportNumberThreshold(
077: IJavaProject project) {
078: String thresholdStr = PreferenceConstants.getPreference(
079: PreferenceConstants.ORGIMPORTS_ONDEMANDTHRESHOLD,
080: project);
081: try {
082: int threshold = Integer.parseInt(thresholdStr);
083: if (threshold < 0) {
084: threshold = Integer.MAX_VALUE;
085: }
086: return threshold;
087: } catch (NumberFormatException e) {
088: return Integer.MAX_VALUE;
089: }
090: }
091:
092: public static String[] getImportOrderPreference(IJavaProject project) {
093: String str = PreferenceConstants.getPreference(
094: PreferenceConstants.ORGIMPORTS_IMPORTORDER, project);
095: if (str != null) {
096: return unpackList(str, ";"); //$NON-NLS-1$
097: }
098: return new String[0];
099: }
100:
101: /**
102: * @deprecated Use getImportNumberThreshold(IJavaProject) instead
103: */
104: public static int getImportNumberThreshold(IPreferenceStore prefs) {
105: int threshold = prefs
106: .getInt(PreferenceConstants.ORGIMPORTS_ONDEMANDTHRESHOLD);
107: if (threshold < 0) {
108: threshold = Integer.MAX_VALUE;
109: }
110: return threshold;
111: }
112:
113: /**
114: * @deprecated Use getImportOrderPreference(IJavaProject) instead
115: */
116: public static String[] getImportOrderPreference(
117: IPreferenceStore prefs) {
118: String str = prefs
119: .getString(PreferenceConstants.ORGIMPORTS_IMPORTORDER);
120: if (str != null) {
121: return unpackList(str, ";"); //$NON-NLS-1$
122: }
123: return new String[0];
124: }
125:
126: private static String[] unpackList(String str, String separator) {
127: StringTokenizer tok = new StringTokenizer(str, separator);
128: int nTokens = tok.countTokens();
129: String[] res = new String[nTokens];
130: for (int i = 0; i < nTokens; i++) {
131: res[i] = tok.nextToken().trim();
132: }
133: return res;
134: }
135:
136: }
|