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.mercurial;
043:
044: import java.io.File;
045: import java.io.IOException;
046: import java.util.logging.Level;
047: import java.util.List;
048: import java.net.URL;
049: import org.openide.awt.HtmlBrowser;
050: import org.openide.util.RequestProcessor;
051: import org.openide.windows.IOProvider;
052: import org.openide.windows.InputOutput;
053: import org.openide.windows.OutputEvent;
054: import org.openide.windows.OutputWriter;
055: import org.openide.windows.OutputListener;
056:
057: /**
058: *
059: * @author Tomas Stupka
060: */
061: public class OutputLogger {
062:
063: private InputOutput log;
064: private boolean ignoreCommand = false;
065: private String repositoryRootString;
066: private static final RequestProcessor rp = new RequestProcessor(
067: "MercurialOutput", 1);
068: public static final int MAX_LINES_TO_PRINT = 500;
069:
070: private static final String MSG_TOO_MANY_LINES = "The number of output lines is greater than 500; see message log for complete output";
071:
072: public static OutputLogger getLogger(String repositoryRoot) {
073: if (repositoryRoot != null) {
074: return new OutputLogger(repositoryRoot);
075: } else {
076: return new NullLogger();
077: }
078: }
079:
080: private OutputLogger(String repositoryRoot) {
081: repositoryRootString = repositoryRoot;
082: log = IOProvider.getDefault()
083: .getIO(repositoryRootString, false);
084: }
085:
086: private OutputLogger() {
087: }
088:
089: public void closeLog() {
090: rp.post(new Runnable() {
091: public void run() {
092: log.getOut().flush();
093: log.getOut().close();
094: log.getErr().flush();
095: log.getErr().close();
096: }
097: });
098: }
099:
100: public void flushLog() {
101: rp.post(new Runnable() {
102: public void run() {
103: log.getOut().flush();
104: log.getErr().flush();
105: }
106: });
107: }
108:
109: /**
110: * Print contents of list to OutputLogger's tab
111: *
112: * @param list to print out
113: *
114: */
115: public void output(final List<String> list) {
116: if (list.isEmpty())
117: return;
118:
119: rp.post(new Runnable() {
120: public void run() {
121: log.select();
122: OutputWriter out = log.getOut();
123:
124: int lines = list.size();
125: if (lines > MAX_LINES_TO_PRINT) {
126: out.println(list.get(1));
127: out.println(list.get(2));
128: out.println(list.get(3));
129: out.println("...");
130: out.println(list.get(list.size() - 1));
131: out.println(MSG_TOO_MANY_LINES);
132: for (String s : list) {
133: Mercurial.LOG.log(Level.WARNING, s);
134: }
135: } else {
136: for (String s : list) {
137: out.println(s);
138: }
139: }
140: out.flush();
141: }
142: });
143: }
144:
145: /**
146: * Print msg to OutputLogger's tab
147: *
148: * @param String msg to print out
149: *
150: */
151: public void output(final String msg) {
152: if (msg == null)
153: return;
154:
155: rp.post(new Runnable() {
156: public void run() {
157: log.select();
158:
159: log.getOut().println(msg);
160: log.getOut().flush();
161: }
162: });
163: }
164:
165: /**
166: * Print msg to OutputLogger's tab in Red
167: *
168: * @param String msg to print out
169: *
170: */
171: public void outputInRed(final String msg) {
172: if (msg == null)
173: return;
174:
175: rp.post(new Runnable() {
176: public void run() {
177: log.select();
178: log.getErr().println(msg);
179: log.getErr().flush();
180: }
181: });
182: }
183:
184: /**
185: * Print URL to OutputLogger's tab as an active Hyperlink
186: *
187: * @param String sURL to print out
188: *
189: */
190: public void outputLink(final String sURL) {
191: if (sURL == null)
192: return;
193:
194: rp.post(new Runnable() {
195: public void run() {
196: log.select();
197: try {
198: OutputWriter out = log.getOut();
199:
200: OutputListener listener = new OutputListener() {
201: public void outputLineAction(OutputEvent ev) {
202: try {
203: HtmlBrowser.URLDisplayer.getDefault()
204: .showURL(new URL(sURL));
205: } catch (IOException ex) {
206: // Ignore
207: }
208: }
209:
210: public void outputLineSelected(OutputEvent ev) {
211: }
212:
213: public void outputLineCleared(OutputEvent ev) {
214: }
215: };
216: out.println(sURL, listener, true);
217: out.flush();
218: } catch (IOException ex) {
219: // Ignore
220: }
221: }
222: });
223: }
224:
225: /**
226: * Select and Clear OutputLogger's tab
227: *
228: * @param list to print out
229: *
230: */
231: public void clearOutput() {
232: rp.post(new Runnable() {
233: public void run() {
234: log.select();
235: OutputWriter out = log.getOut();
236:
237: try {
238: out.reset();
239: } catch (IOException ex) {
240: // Ignore Exception
241: }
242: out.flush();
243: }
244: });
245: }
246:
247: private static class NullLogger extends OutputLogger {
248:
249: public void closeLog() {
250: }
251:
252: public void flushLog() {
253: }
254:
255: public void output(List<String> list) {
256: }
257:
258: public void output(String msg) {
259: }
260:
261: public void outputInRed(String msg) {
262: }
263:
264: public void outputLink(final String sURL) {
265: }
266:
267: public void clearOutput() {
268: }
269: }
270:
271: }
|