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:
042: package org.netbeans.modules.tasklist.todo.settings;
043:
044: import java.beans.PropertyChangeListener;
045: import java.beans.PropertyChangeSupport;
046: import java.util.ArrayList;
047: import java.util.Collection;
048: import java.util.Collections;
049: import java.util.HashMap;
050: import java.util.Map;
051: import java.util.StringTokenizer;
052: import java.util.prefs.Preferences;
053: import org.openide.util.NbPreferences;
054:
055: /**
056: *
057: * @author S. Aubrecht
058: */
059: final public class Settings {
060:
061: public static final String PROP_PATTERN_LIST = "patternList"; //NOI18N
062: public static final String PROP_SCAN_COMMENTS_ONLY = "scanCommentsOnly"; //NOI18N
063:
064: private static Settings theInstance;
065: private static final String PATTERN_DELIMITER = "|"; //NOI18N
066:
067: private ArrayList<String> patterns = new ArrayList<String>(10);
068: private Map<String, CommentTags> ext2comments = new HashMap<String, CommentTags>(
069: 10);
070: private Map<String, CommentTags> mime2comments = new HashMap<String, CommentTags>(
071: 10);
072: private boolean scanCommentsOnly = true;
073:
074: private PropertyChangeSupport propertySupport;
075:
076: /** Creates a new instance of Settings */
077: private Settings() {
078: patterns.addAll(decodePatterns(getPreferences().get("patterns", //NOI18N
079: "@todo|TODO|FIXME|XXX|PENDING|<<<<<<<"))); //NOI18N
080:
081: scanCommentsOnly = getPreferences().getBoolean(
082: "scanCommentsOnly", true); //NOI18N
083:
084: ext2comments.put("JAVA", new CommentTags("//", "/*", "*/")); //NOI18N //NOI18N //NOI18N //NOI18N
085: ext2comments.put("C", new CommentTags("//", "/*", "*/")); //NOI18N //NOI18N //NOI18N //NOI18N
086: ext2comments.put("CPP", new CommentTags("//", "/*", "*/")); //NOI18N //NOI18N //NOI18N //NOI18N
087: ext2comments.put("CXX", new CommentTags("//", "/*", "*/")); //NOI18N //NOI18N //NOI18N //NOI18N
088: ext2comments.put("CC", new CommentTags("//", "/*", "*/")); //NOI18N //NOI18N //NOI18N //NOI18N
089: ext2comments.put("H", new CommentTags("//", "/*", "*/")); //NOI18N //NOI18N //NOI18N //NOI18N
090: ext2comments.put("HTML", new CommentTags("<!--", "-->")); //NOI18N //NOI18N //NOI18N
091: ext2comments.put("HTM", new CommentTags("<!--", "-->")); //NOI18N //NOI18N //NOI18N
092: ext2comments.put("XML", new CommentTags("<!--", "-->")); //NOI18N //NOI18N //NOI18N
093: ext2comments.put("JSP", new CommentTags("<%--", "--%>")); //NOI18N //NOI18N //NOI18N
094: ext2comments.put("PROPERTIES", new CommentTags("#")); //NOI18N //NOI18N
095: ext2comments.put("SH", new CommentTags("#")); //NOI18N //NOI18N
096: ext2comments.put("RB", new CommentTags("#")); //NOI18N //NOI18N
097:
098: mime2comments.put("text/x-java", new CommentTags("//", "/*",
099: "*/")); //NOI18N //NOI18N //NOI18N //NOI18N
100: mime2comments.put("text/html", new CommentTags("<!--", "-->")); //NOI18N //NOI18N //NOI18N
101: mime2comments.put("application/x-httpd-eruby", new CommentTags(
102: "<!--", "-->")); //NOI18N //NOI18N //NOI18N
103: mime2comments.put("text/x-ruby", new CommentTags("#")); //NOI18N //NOI18N
104: }
105:
106: public static final Settings getDefault() {
107: if (null == theInstance)
108: theInstance = new Settings();
109: return theInstance;
110: }
111:
112: public Collection<String> getPatterns() {
113: return Collections.unmodifiableCollection(patterns);
114: }
115:
116: public void setPatterns(Collection<String> newPatterns) {
117: patterns.clear();
118: patterns.addAll(newPatterns);
119: getPreferences().put("patterns", encodePatterns(newPatterns)); //NOI18N
120: if (null == propertySupport)
121: propertySupport = new PropertyChangeSupport(this );
122: propertySupport.firePropertyChange(PROP_PATTERN_LIST, null,
123: getPatterns());
124: }
125:
126: public boolean isExtensionSupported(String fileExtension) {
127: return null != ext2comments.get(fileExtension.toUpperCase());
128: }
129:
130: public boolean isMimeTypeSupported(String mimeType) {
131: return null != mime2comments.get(mimeType);
132: }
133:
134: public String getLineComment(String fileExtension, String mime) {
135: CommentTags ct = ext2comments.get(fileExtension.toUpperCase());
136: if (null == ct)
137: ct = mime2comments.get(mime);
138: return null == ct ? null : ct.lineComment;
139: }
140:
141: public String getBlockCommentStart(String fileExtension, String mime) {
142: CommentTags ct = ext2comments.get(fileExtension.toUpperCase());
143: if (null == ct)
144: ct = mime2comments.get(mime);
145: return null == ct ? null : ct.blockCommentStart;
146: }
147:
148: public String getBlockCommentEnd(String fileExtension, String mime) {
149: CommentTags ct = ext2comments.get(fileExtension.toUpperCase());
150: if (null == ct)
151: ct = mime2comments.get(mime);
152: return null == ct ? null : ct.blockCommentEnd;
153: }
154:
155: public boolean isScanCommentsOnly() {
156: return scanCommentsOnly;
157: }
158:
159: public void setScanCommentsOnly(boolean val) {
160: boolean oldVal = scanCommentsOnly;
161: this .scanCommentsOnly = val;
162: getPreferences().putBoolean("scanCommentsOnly", val); //NOI18N
163: if (null == propertySupport)
164: propertySupport = new PropertyChangeSupport(this );
165: propertySupport.firePropertyChange(PROP_SCAN_COMMENTS_ONLY,
166: oldVal, val);
167: }
168:
169: public void addPropertyChangeListener(PropertyChangeListener l) {
170: if (null == propertySupport)
171: propertySupport = new PropertyChangeSupport(this );
172: propertySupport.addPropertyChangeListener(l);
173: }
174:
175: public void removePropertyChangeListener(PropertyChangeListener l) {
176: if (null != propertySupport)
177: propertySupport.removePropertyChangeListener(l);
178: }
179:
180: private Preferences getPreferences() {
181: return NbPreferences.forModule(Settings.class);
182: }
183:
184: private static Collection<String> decodePatterns(
185: String encodedPatterns) {
186: StringTokenizer st = new StringTokenizer(encodedPatterns,
187: PATTERN_DELIMITER, false);
188:
189: Collection<String> patterns = new ArrayList<String>();
190:
191: while (st.hasMoreTokens()) {
192: String im = st.nextToken();
193: patterns.add(im);
194: }
195:
196: return patterns;
197: }
198:
199: private static String encodePatterns(Collection<String> patterns) {
200: StringBuffer sb = new StringBuffer();
201:
202: for (String p : patterns) {
203: sb.append(p);
204: sb.append(PATTERN_DELIMITER);
205: }
206:
207: return sb.toString();
208: }
209:
210: private static class CommentTags {
211: private String lineComment;
212: private String blockCommentStart;
213: private String blockCommentEnd;
214:
215: public CommentTags(String lineComment,
216: String blockCommentStart, String blockCommentEnd) {
217: this .lineComment = lineComment;
218: this .blockCommentStart = blockCommentStart;
219: this .blockCommentEnd = blockCommentEnd;
220: }
221:
222: public CommentTags(String blockCommentStart,
223: String blockCommentEnd) {
224: this .blockCommentStart = blockCommentStart;
225: this .blockCommentEnd = blockCommentEnd;
226: }
227:
228: public CommentTags(String lineComment) {
229: this.lineComment = lineComment;
230: }
231: }
232: }
|