001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 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.source.usages;
041:
042: import java.io.IOException;
043: import java.net.URL;
044: import java.util.HashSet;
045: import java.util.Map;
046: import java.util.Set;
047: import java.util.WeakHashMap;
048: import javax.swing.event.ChangeEvent;
049: import javax.swing.event.ChangeListener;
050: import org.openide.util.Exceptions;
051: import org.openide.util.WeakSet;
052:
053: /**
054: *
055: * @author Jan Lahoda
056: */
057: public class ExecutableFilesIndex {
058:
059: public static final ExecutableFilesIndex DEFAULT = new ExecutableFilesIndex();
060:
061: private URL lastLoadURL;
062: private Set<String> mainSources;
063:
064: public synchronized boolean isMainClass(URL root, URL source) {
065: ensureLoad(root);
066:
067: return mainSources.contains(source.toExternalForm());
068: }
069:
070: public synchronized void setMainClass(URL root, URL source,
071: boolean value) {
072: ensureLoad(root);
073:
074: String ext = source.toExternalForm();
075: boolean changed;
076:
077: if (value) {
078: changed = mainSources.add(ext);
079: } else {
080: changed = mainSources.remove(ext);
081: }
082:
083: if (changed) {
084: save(root);
085:
086: Set<ChangeListener> ls = file2Listener.get(ext);
087:
088: if (ls != null) {
089: ChangeEvent e = null;
090:
091: for (ChangeListener l : ls) {
092: if (e == null)
093: e = new ChangeEvent(source);
094:
095: l.stateChanged(e);
096: }
097: }
098: }
099: }
100:
101: private Map<ChangeListener, String> listener2File = new WeakHashMap<ChangeListener, String>();
102: private Map<String, Set<ChangeListener>> file2Listener = new WeakHashMap<String, Set<ChangeListener>>();
103:
104: public synchronized void addChangeListener(URL source,
105: ChangeListener l) {
106: String ext = source.toExternalForm();
107:
108: listener2File.put(l, ext);
109:
110: Set<ChangeListener> ls = file2Listener.get(ext);
111:
112: if (ls == null) {
113: file2Listener.put(ext, ls = new WeakSet<ChangeListener>());
114: }
115:
116: ls.add(l);
117:
118: file2Listener.put(ext, ls);
119: }
120:
121: private void ensureLoad(URL root) {
122: if (lastLoadURL != null && lastLoadURL.equals(root)) {
123: return;
124: }
125:
126: try {
127: mainSources = unwrap(RepositoryUpdater.getAttribute(root,
128: "executable-files", "")); //NOI18N
129: } catch (IOException ex) {
130: Exceptions.printStackTrace(ex);
131: mainSources = new HashSet<String>();
132: } finally {
133: lastLoadURL = root;
134: }
135: }
136:
137: private void save(URL root) {
138: try {
139: RepositoryUpdater.setAttribute(root, "executable-files",
140: wrap(mainSources)); //NOI18N
141: } catch (IOException ex) {
142: Exceptions.printStackTrace(ex);
143: }
144: }
145:
146: static Set<String> unwrap(String value) {
147: if (value.length() == 0) {
148: return new HashSet<String>();
149: }
150:
151: String[] executableFiles = value.split("::"); //NOI18N
152:
153: Set<String> result = new HashSet<String>();
154:
155: for (String file : executableFiles) {
156: file = file.replaceAll("\\\\d", ":"); //NOI18N
157: file = file.replaceAll("\\\\\\\\", "\\\\"); //NOI18N
158:
159: result.add(file);
160: }
161:
162: return result;
163: }
164:
165: static String wrap(Set<String> values) {
166: StringBuilder attribute = new StringBuilder();
167: boolean first = true;
168:
169: for (String s : values) {
170: if (!first) {
171: attribute.append("::"); //NOI18N
172: }
173: s = s.replaceAll("\\\\", "\\\\\\\\"); //NOI18N
174: s = s.replaceAll(":", "\\\\d"); //NOI18N
175:
176: attribute.append(s);
177:
178: first = false;
179: }
180:
181: return attribute.toString();
182: }
183: }
|