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-2006 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:
042: package org.netbeans.modules.subversion;
043:
044: import java.util.regex.Pattern;
045: import java.util.*;
046: import java.util.prefs.Preferences;
047: import org.netbeans.modules.subversion.options.AnnotationExpression;
048: import org.netbeans.modules.subversion.ui.repository.RepositoryConnection;
049: import org.openide.util.NbPreferences;
050: import org.netbeans.modules.versioning.util.TableSorter;
051: import org.netbeans.modules.versioning.util.Utils;
052:
053: /**
054: * Stores Subversion module configuration.
055: *
056: * @author Maros Sandor
057: */
058: public class SvnModuleConfig {
059:
060: public static final String PROP_IGNORED_FILEPATTERNS = "ignoredFilePatterns"; // NOI18N
061: public static final String PROP_COMMIT_EXCLUSIONS = "commitExclusions"; // NOI18N
062: public static final String PROP_DEFAULT_VALUES = "defaultValues"; // NOI18N
063: public static final String KEY_EXECUTABLE_BINARY = "svnExecBinary"; // NOI18N
064: public static final String KEY_ANNOTATION_FORMAT = "annotationFormat"; // NOI18N
065: public static final String SAVE_PASSWORD = "savePassword"; // NOI18N
066:
067: private static final String RECENT_URL = "repository.recentURL"; // NOI18N
068: private static final String SHOW_CHECKOUT_COMPLETED = "checkoutCompleted.showCheckoutCompleted"; // NOI18N
069:
070: private static final String URL_EXP = "annotator.urlExp"; // NOI18N
071: private static final String ANNOTATION_EXP = "annotator.annotationExp"; // NOI18N
072:
073: public static final String TEXT_ANNOTATIONS_FORMAT_DEFAULT = "{DEFAULT}"; // NOI18N
074:
075: private static final SvnModuleConfig INSTANCE = new SvnModuleConfig();
076:
077: private Map<String, String[]> urlCredentials;
078:
079: public static SvnModuleConfig getDefault() {
080: return INSTANCE;
081: }
082:
083: private Set<String> exclusions;
084:
085: // properties ~~~~~~~~~~~~~~~~~~~~~~~~~
086:
087: public Preferences getPreferences() {
088: return NbPreferences.forModule(SvnModuleConfig.class);
089: }
090:
091: public boolean getShowCheckoutCompleted() {
092: return getPreferences().getBoolean(SHOW_CHECKOUT_COMPLETED,
093: true);
094: }
095:
096: public Pattern[] getIgnoredFilePatterns() {
097: return getDefaultFilePatterns();
098: }
099:
100: public boolean isExcludedFromCommit(String path) {
101: return getCommitExclusions().contains(path);
102: }
103:
104: /**
105: * @param paths collection of paths, of File.getAbsolutePath()
106: */
107: public void addExclusionPaths(Collection<String> paths) {
108: Set<String> exclusions = getCommitExclusions();
109: if (exclusions.addAll(paths)) {
110: Utils.put(getPreferences(), PROP_COMMIT_EXCLUSIONS,
111: new ArrayList<String>(exclusions));
112: }
113: }
114:
115: /**
116: * @param paths collection of paths, File.getAbsolutePath()
117: */
118: public void removeExclusionPaths(Collection<String> paths) {
119: Set<String> exclusions = getCommitExclusions();
120: if (exclusions.removeAll(paths)) {
121: Utils.put(getPreferences(), PROP_COMMIT_EXCLUSIONS,
122: new ArrayList<String>(exclusions));
123: }
124: }
125:
126: public String getExecutableBinaryPath() {
127: return (String) getPreferences().get(KEY_EXECUTABLE_BINARY, "");
128: }
129:
130: public void setExecutableBinaryPath(String path) {
131: getPreferences().put(KEY_EXECUTABLE_BINARY, path);
132: }
133:
134: public String getAnnotationFormat() {
135: return (String) getPreferences().get(KEY_ANNOTATION_FORMAT,
136: getDefaultAnnotationFormat());
137: }
138:
139: public String getDefaultAnnotationFormat() {
140: return "[{" + Annotator.ANNOTATION_STATUS + "} {"
141: + Annotator.ANNOTATION_FOLDER + "}]";
142: }
143:
144: public void setAnnotationFormat(String annotationFormat) {
145: getPreferences().put(KEY_ANNOTATION_FORMAT, annotationFormat);
146: }
147:
148: public void setShowCheckoutCompleted(boolean bl) {
149: getPreferences().putBoolean(SHOW_CHECKOUT_COMPLETED, bl);
150: }
151:
152: public boolean getSavePassword() {
153: return getPreferences().getBoolean(SAVE_PASSWORD, true);
154: }
155:
156: public void setSavePassword(boolean bl) {
157: getPreferences().putBoolean(SAVE_PASSWORD, bl);
158: }
159:
160: public RepositoryConnection getRepositoryConnection(String url) {
161: List<RepositoryConnection> rcs = getRecentUrls();
162: for (Iterator<RepositoryConnection> it = rcs.iterator(); it
163: .hasNext();) {
164: RepositoryConnection rc = it.next();
165: if (url.equals(rc.getUrl())) {
166: return rc;
167: }
168: }
169: return null;
170: }
171:
172: public void insertRecentUrl(RepositoryConnection rc) {
173: Preferences prefs = getPreferences();
174:
175: List<String> urlValues = Utils.getStringList(prefs, RECENT_URL);
176: for (Iterator<String> it = urlValues.iterator(); it.hasNext();) {
177: String rcOldString = it.next();
178: RepositoryConnection rcOld = RepositoryConnection
179: .parse(rcOldString);
180: if (rcOld.equals(rc)) {
181: Utils.removeFromArray(prefs, RECENT_URL, rcOldString);
182: }
183: }
184: handleCredentials(rc);
185: Utils.insert(prefs, RECENT_URL, RepositoryConnection
186: .getString(rc), -1);
187: }
188:
189: public void setRecentUrls(List<RepositoryConnection> recentUrls) {
190: List<String> urls = new ArrayList<String>(recentUrls.size());
191:
192: int idx = 0;
193: for (Iterator<RepositoryConnection> it = recentUrls.iterator(); it
194: .hasNext();) {
195: idx++;
196: RepositoryConnection rc = it.next();
197: handleCredentials(rc);
198: urls.add(RepositoryConnection.getString(rc));
199: }
200: Preferences prefs = getPreferences();
201: Utils.put(prefs, RECENT_URL, urls);
202: }
203:
204: public List<RepositoryConnection> getRecentUrls() {
205: Preferences prefs = getPreferences();
206: List<String> urls = Utils.getStringList(prefs, RECENT_URL);
207: List<RepositoryConnection> ret = new ArrayList<RepositoryConnection>(
208: urls.size());
209: for (Iterator<String> it = urls.iterator(); it.hasNext();) {
210: RepositoryConnection rc = RepositoryConnection.parse(it
211: .next());
212: if (getUrlCredentials().containsKey(rc.getUrl())) {
213: String[] creds = getUrlCredentials().get(rc.getUrl());
214: rc = new RepositoryConnection(rc.getUrl(), creds[0],
215: creds[1], rc.getExternalCommand(), rc
216: .getSavePassword());
217: }
218: ret.add(rc);
219: }
220: return ret;
221: }
222:
223: public void setAnnotationExpresions(List<AnnotationExpression> exps) {
224: List<String> urlExp = new ArrayList<String>(exps.size());
225: List<String> annotationExp = new ArrayList<String>(exps.size());
226:
227: int idx = 0;
228: for (Iterator<AnnotationExpression> it = exps.iterator(); it
229: .hasNext();) {
230: idx++;
231: AnnotationExpression exp = it.next();
232: urlExp.add(exp.getUrlExp());
233: annotationExp.add(exp.getAnnotationExp());
234: }
235:
236: Preferences prefs = getPreferences();
237: Utils.put(prefs, URL_EXP, urlExp);
238: Utils.put(prefs, ANNOTATION_EXP, annotationExp);
239: }
240:
241: public List<AnnotationExpression> getAnnotationExpresions() {
242: Preferences prefs = getPreferences();
243: List<String> urlExp = Utils.getStringList(prefs, URL_EXP);
244: List<String> annotationExp = Utils.getStringList(prefs,
245: ANNOTATION_EXP);
246:
247: List<AnnotationExpression> ret = new ArrayList<AnnotationExpression>(
248: urlExp.size());
249: for (int i = 0; i < urlExp.size(); i++) {
250: ret.add(new AnnotationExpression(urlExp.get(i),
251: annotationExp.get(i)));
252: }
253: if (ret.size() < 1) {
254: ret = getDefaultAnnotationExpresions();
255: }
256: return ret;
257: }
258:
259: public List<AnnotationExpression> getDefaultAnnotationExpresions() {
260: List<AnnotationExpression> ret = new ArrayList<AnnotationExpression>(
261: 1);
262: ret.add(new AnnotationExpression(".*/(branches|tags)/(.+?)/.*",
263: "\\2"));
264: return ret;
265: }
266:
267: // TODO: persist state
268:
269: private TableSorter importTableSorter;
270: private TableSorter commitTableSorter;
271:
272: public TableSorter getImportTableSorter() {
273: return importTableSorter;
274: }
275:
276: public void setImportTableSorter(TableSorter sorter) {
277: importTableSorter = sorter;
278: }
279:
280: public TableSorter getCommitTableSorter() {
281: return commitTableSorter;
282: }
283:
284: public void setCommitTableSorter(TableSorter sorter) {
285: commitTableSorter = sorter;
286: }
287:
288: // private methods ~~~~~~~~~~~~~~~~~~
289:
290: private synchronized Set<String> getCommitExclusions() {
291: if (exclusions == null) {
292: exclusions = new HashSet<String>(Utils.getStringList(
293: getPreferences(), PROP_COMMIT_EXCLUSIONS));
294: }
295: return exclusions;
296: }
297:
298: private static Pattern[] getDefaultFilePatterns() {
299: return new Pattern[] { Pattern.compile("cvslog\\..*"), // NOI18N
300: Pattern.compile("\\.make\\.state"), // NOI18N
301: Pattern.compile("\\.nse_depinfo"), // NOI18N
302: Pattern.compile(".*~"), // NOI18N
303: Pattern.compile("#.*"), // NOI18N
304: Pattern.compile("\\.#.*"), // NOI18N
305: Pattern.compile(",.*"), // NOI18N
306: Pattern.compile("_\\$.*"), // NOI18N
307: Pattern.compile(".*\\$"), // NOI18N
308: Pattern.compile(".*\\.old"), // NOI18N
309: Pattern.compile(".*\\.bak"), // NOI18N
310: Pattern.compile(".*\\.BAK"), // NOI18N
311: Pattern.compile(".*\\.orig"), // NOI18N
312: Pattern.compile(".*\\.rej"), // NOI18N
313: Pattern.compile(".*\\.del-.*"), // NOI18N
314: Pattern.compile(".*\\.a"), // NOI18N
315: Pattern.compile(".*\\.olb"), // NOI18N
316: Pattern.compile(".*\\.o"), // NOI18N
317: Pattern.compile(".*\\.obj"), // NOI18N
318: Pattern.compile(".*\\.so"), // NOI18N
319: Pattern.compile(".*\\.exe"), // NOI18N
320: Pattern.compile(".*\\.Z"), // NOI18N
321: Pattern.compile(".*\\.elc"), // NOI18N
322: Pattern.compile(".*\\.ln"), // NOI18N
323: };
324: }
325:
326: private void handleCredentials(RepositoryConnection rc) {
327: if (!rc.getSavePassword()) {
328: getUrlCredentials()
329: .put(
330: rc.getUrl(),
331: new String[] { rc.getUsername(),
332: rc.getPassword() });
333: } else {
334: getUrlCredentials().remove(rc.getUrl());
335: }
336: }
337:
338: private Map<String, String[]> getUrlCredentials() {
339: if (urlCredentials == null) {
340: urlCredentials = new HashMap<String, String[]>();
341: }
342: return urlCredentials;
343: }
344:
345: }
|