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.languages;
043:
044: import java.awt.Point;
045: import java.lang.ref.WeakReference;
046: import java.util.Collection;
047: import java.util.HashMap;
048: import java.util.Iterator;
049: import java.util.Map;
050: import java.util.logging.Level;
051: import java.util.logging.Logger;
052: import javax.swing.SwingUtilities;
053: import javax.swing.text.Document;
054: import org.netbeans.api.lexer.TokenHierarchy;
055: import org.netbeans.api.lexer.TokenSequence;
056:
057: import org.openide.DialogDisplayer;
058: import org.openide.NotifyDescriptor.Message;
059: import org.openide.util.RequestProcessor;
060:
061: /**
062: *
063: * @author Jan Jancura
064: */
065: public class Utils {
066:
067: private static Logger logger = Logger
068: .getLogger("org.netbeans.modules.languages");
069:
070: public static void notify(String message) {
071: logger.log(Level.WARNING, message);
072: }
073:
074: public static void message(final String message) {
075: SwingUtilities.invokeLater(new Runnable() {
076: public void run() {
077: DialogDisplayer.getDefault().notify(
078: new Message(message));
079: }
080: });
081: }
082:
083: public static void notify(Exception exception) {
084: logger.log(Level.WARNING, null, exception);
085: }
086:
087: public static void notify(String message, Exception exception) {
088: logger.log(Level.WARNING, message, exception);
089: }
090:
091: private static Map<String, WeakReference> collections;
092:
093: public static void startTest(String name, Collection c) {
094: if (collections == null) {
095: // init
096: collections = new HashMap<String, WeakReference>();
097: start();
098: }
099: collections.put(name, new WeakReference<Collection>(c));
100: }
101:
102: public static void startTest(String name, Map m) {
103: if (collections == null) {
104: // init
105: collections = new HashMap<String, WeakReference>();
106: start();
107: }
108: collections.put(name, new WeakReference<Map>(m));
109: }
110:
111: private static void start() {
112: RequestProcessor.getDefault().post(new Runnable() {
113: public void run() {
114: Map<String, WeakReference> cs = new HashMap<String, WeakReference>(
115: collections);
116: Iterator<String> it = cs.keySet().iterator();
117: while (it.hasNext()) {
118: String name = it.next();
119: Object o = cs.get(name).get();
120: if (o == null)
121: collections.remove(name);
122: else
123: System.out.println(":" + name + " " + size(o));
124: }
125: start();
126: }
127: }, 5000);
128: }
129:
130: private static int size(Object o) {
131: if (o instanceof Collection) {
132: Collection c = (Collection) o;
133: int s = c.size();
134: Iterator it = c.iterator();
135: while (it.hasNext()) {
136: Object item = it.next();
137: if (item instanceof Collection || item instanceof Map)
138: s += size(item);
139: }
140: return s;
141: }
142: Map m = (Map) o;
143: int s = m.size();
144: Iterator it = m.keySet().iterator();
145: while (it.hasNext()) {
146: Object key = it.next();
147: if (key instanceof Collection || key instanceof Map)
148: s += size(key);
149: Object value = m.get(key);
150: if (value instanceof Collection || value instanceof Map)
151: s += size(value);
152: }
153: return s;
154: }
155:
156: public static Point findPosition(String text, int offset) {
157: int current = 0;
158: int next = text.indexOf('\n', current);
159: int lineNumber = 1;
160: while (next >= 0) {
161: if (next > offset)
162: return new Point(lineNumber, offset - current + 1);
163: lineNumber++;
164: current = next + 1;
165: next = text.indexOf('\n', current);
166: }
167: throw new ArrayIndexOutOfBoundsException();
168: }
169:
170: public static TokenSequence getTokenSequence(Document document,
171: int offset) {
172: TokenHierarchy tokenHierarchy = TokenHierarchy.get(document);
173: if (tokenHierarchy == null)
174: return null;
175: TokenSequence tokenSequence = tokenHierarchy.tokenSequence();
176: if (tokenSequence == null)
177: return null;
178: while (true) {
179: tokenSequence.move(offset);
180: if (!tokenSequence.moveNext())
181: return tokenSequence;
182: TokenSequence tokenSequence2 = tokenSequence.embedded();
183: if (tokenSequence2 == null)
184: return tokenSequence;
185: tokenSequence = tokenSequence2;
186: }
187: }
188: }
|