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.timers;
043:
044: import java.text.MessageFormat;
045: import java.util.MissingResourceException;
046: import java.util.logging.Handler;
047: import java.util.logging.Level;
048: import java.util.logging.LogRecord;
049: import java.util.logging.Logger;
050: import javax.swing.event.ChangeEvent;
051: import javax.swing.event.ChangeListener;
052: import javax.swing.text.Document;
053: import org.netbeans.editor.Registry;
054: import org.openide.filesystems.FileObject;
055: import org.openide.loaders.DataObject;
056: import org.openide.modules.ModuleInstall;
057:
058: /**
059: *
060: * @author nenik
061: */
062: public class Install extends ModuleInstall {
063: static Logger logger;
064: private static Handler timers = new TimerHandler();
065: private static ChangeListener docTracker = new ActivatedDocumentListener();
066:
067: private static String INSTANCES = "Important instances";
068:
069: public void restored() {
070: Logger log = Logger.getLogger("TIMER"); // NOI18N
071: log.setUseParentHandlers(false);
072: log.setLevel(Level.FINE);
073: log.addHandler(timers);
074:
075: Registry.addChangeListener(docTracker);
076: }
077:
078: private static class TimerHandler extends Handler {
079: TimerHandler() {
080: }
081:
082: public void publish(LogRecord rec) {
083: String message = rec.getMessage();
084: if (rec.getResourceBundle() != null) {
085: try {
086: message = rec.getResourceBundle().getString(
087: rec.getMessage());
088: if (rec.getParameters() != null) {
089: message = MessageFormat.format(message, rec
090: .getParameters());
091: }
092: } catch (MissingResourceException ex) {
093: Logger.getAnonymousLogger().log(Level.INFO, null,
094: ex);
095: }
096: }
097:
098: Object[] args = rec.getParameters();
099: if (args == null || args[0] == null)
100: return;
101:
102: if (args.length == 1) { // simplified instance logging
103: TimesCollectorPeer.getDefault().reportReference(
104: INSTANCES, rec.getMessage(), message, args[0]);
105: return;
106: }
107:
108: if (args.length < 2) {
109: return;
110: }
111:
112: Object key = args[0];
113:
114: if (args[1] instanceof Number) { // time
115: TimesCollectorPeer.getDefault().reportTime(key,
116: rec.getMessage(), message,
117: ((Number) args[1]).longValue());
118: } else if (args[1] instanceof Boolean) { // start/stop logic
119: // XXX - start/stop support
120: } else {
121: String txt = message.startsWith("[M]") ? message
122: : "[M] " + message;
123: TimesCollectorPeer.getDefault().reportReference(key,
124: rec.getMessage(), txt, args[1]);
125: }
126: }
127:
128: public void flush() {
129: }
130:
131: public void close() throws SecurityException {
132: }
133: }
134:
135: /**
136: *
137: * @author Jan Lahoda
138: */
139: private static class ActivatedDocumentListener implements
140: ChangeListener {
141: ActivatedDocumentListener() {
142: }
143:
144: public synchronized void stateChanged(ChangeEvent e) {
145: Document active = Registry.getMostActiveDocument();
146: if (active == null)
147: return;
148:
149: Object sourceProperty = active
150: .getProperty(Document.StreamDescriptionProperty);
151: if (!(sourceProperty instanceof DataObject))
152: return;
153:
154: FileObject activeFile = ((DataObject) sourceProperty)
155: .getPrimaryFile();
156: TimesCollectorPeer.getDefault().select(activeFile);
157: }
158: }
159:
160: }
|