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: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041: package org.netbeans.modules.diff;
042:
043: import org.openide.util.NbPreferences;
044: import org.openide.util.Lookup;
045: import org.openide.filesystems.FileSystem;
046: import org.openide.filesystems.FileObject;
047: import org.openide.loaders.DataFolder;
048: import org.openide.loaders.DataObject;
049: import org.openide.loaders.InstanceDataObject;
050: import org.netbeans.spi.diff.DiffProvider;
051: import org.netbeans.modules.diff.builtin.provider.BuiltInDiffProvider;
052: import org.netbeans.modules.diff.cmdline.CmdlineDiffProvider;
053:
054: import java.util.prefs.Preferences;
055: import java.util.*;
056: import java.awt.Color;
057:
058: /**
059: * Module settings for Diff module.
060: *
061: * @author Maros Sandor
062: */
063: public class DiffModuleConfig {
064:
065: public static final String PREF_EXTERNAL_DIFF_COMMAND = "externalDiffCommand"; // NOI18N
066:
067: private static final String PREF_IGNORE_WHITESPACE = "ignoreWhitespace"; // NOI18N
068: private static final String PREF_USE_INTERNAL_DIFF = "useInternalDiff"; // NOI18N
069: private static final String PREF_ADDED_COLOR = "addedColor"; // NOI18N
070: private static final String PREF_CHANGED_COLOR = "changedColor"; // NOI18N
071: private static final String PREF_DELETED_COLOR = "deletedColor"; // NOI18N
072:
073: private static final DiffModuleConfig INSTANCE = new DiffModuleConfig();
074:
075: private final Color defaultAddedColor = new Color(180, 255, 180);
076: private final Color defaultChangedColor = new Color(160, 200, 255);
077: private final Color defaultDeletedColor = new Color(255, 160, 180);
078:
079: public static DiffModuleConfig getDefault() {
080: return INSTANCE;
081: }
082:
083: private DiffModuleConfig() {
084: }
085:
086: public Color getAddedColor() {
087: return getColor(PREF_ADDED_COLOR, defaultAddedColor);
088: }
089:
090: public Color getChangedColor() {
091: return getColor(PREF_CHANGED_COLOR, defaultChangedColor);
092: }
093:
094: public Color getDeletedColor() {
095: return getColor(PREF_DELETED_COLOR, defaultDeletedColor);
096: }
097:
098: public void setChangedColor(Color changedColor) {
099: putColor(PREF_CHANGED_COLOR, changedColor);
100: }
101:
102: public void setAddedColor(Color addedColor) {
103: putColor(PREF_ADDED_COLOR, addedColor);
104: }
105:
106: public void setDeletedColor(Color deletedColor) {
107: putColor(PREF_DELETED_COLOR, deletedColor);
108: }
109:
110: private void putColor(String key, Color color) {
111: getPreferences().putInt(key, color.getRGB());
112: }
113:
114: private Color getColor(String key, Color defaultColor) {
115: int rgb = getPreferences().getInt(key, defaultColor.getRGB());
116: return new Color(rgb);
117: }
118:
119: public DiffProvider getDefaultDiffProvider() {
120: DiffProvider provider = Lookup.getDefault().lookup(
121: DiffProvider.class);
122: if (provider instanceof BuiltInDiffProvider) {
123: ((BuiltInDiffProvider) provider)
124: .setTrimLines(isIgnoreWhitespace());
125: } else if (provider instanceof CmdlineDiffProvider) {
126: ((CmdlineDiffProvider) provider)
127: .setDiffCommand(getDiffCommand());
128: }
129: return provider;
130: }
131:
132: private String getDiffCommand() {
133: return getPreferences().get(PREF_EXTERNAL_DIFF_COMMAND,
134: "diff {0} {1}");
135: }
136:
137: public void setIgnoreWhitespace(boolean ignoreWhitespace) {
138: getPreferences().putBoolean(PREF_IGNORE_WHITESPACE,
139: ignoreWhitespace);
140: getBuiltinProvider().setTrimLines(ignoreWhitespace);
141: }
142:
143: private BuiltInDiffProvider getBuiltinProvider() {
144: Collection<? extends DiffProvider> diffs = Lookup.getDefault()
145: .lookupAll(DiffProvider.class);
146: for (DiffProvider diff : diffs) {
147: if (diff instanceof BuiltInDiffProvider) {
148: return (BuiltInDiffProvider) diff;
149: }
150: }
151: throw new IllegalStateException("No builtin diff provider");
152: }
153:
154: public boolean isIgnoreWhitespace() {
155: return getPreferences()
156: .getBoolean(PREF_IGNORE_WHITESPACE, true);
157: }
158:
159: public void setUseInteralDiff(boolean useInternal) {
160: getPreferences()
161: .putBoolean(PREF_USE_INTERNAL_DIFF, useInternal);
162: Collection<? extends DiffProvider> diffs = Lookup.getDefault()
163: .lookupAll(DiffProvider.class);
164: if (useInternal) {
165: setDefaultProvider(getBuiltinProvider());
166: } else {
167: for (DiffProvider diff : diffs) {
168: if (diff instanceof CmdlineDiffProvider) {
169: setDefaultProvider(diff);
170: break;
171: }
172: }
173: }
174: }
175:
176: public boolean isUseInteralDiff() {
177: return getPreferences()
178: .getBoolean(PREF_USE_INTERNAL_DIFF, true);
179: }
180:
181: private void setDefaultProvider(DiffProvider ds) {
182: // TODO: for compatibility with legacy diff component, think of better way
183: FileSystem dfs = org.openide.filesystems.Repository
184: .getDefault().getDefaultFileSystem();
185: FileObject services = dfs
186: .findResource("Services/DiffProviders");
187: DataFolder df = DataFolder.findFolder(services);
188: DataObject[] children = df.getChildren();
189: for (int i = 0; i < children.length; i++) {
190: if (children[i] instanceof InstanceDataObject) {
191: InstanceDataObject ido = (InstanceDataObject) children[i];
192: if (ido.instanceOf(ds.getClass())) {
193: try {
194: if (ds.equals(ido.instanceCreate())) {
195: df.setOrder(new DataObject[] { ido });
196: break;
197: }
198: } catch (java.io.IOException ioex) {
199: } catch (ClassNotFoundException cnfex) {
200: }
201: }
202: }
203: }
204: }
205:
206: // properties ~~~~~~~~~~~~~~~~~~~~~~~~~
207:
208: public Preferences getPreferences() {
209: return NbPreferences.forModule(DiffModuleConfig.class);
210: }
211:
212: }
|