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: package org.netbeans.modules.sql.framework.ui.output;
042:
043: import org.netbeans.modules.sql.framework.ui.view.*;
044: import java.awt.BorderLayout;
045: import java.awt.Color;
046: import java.awt.Font;
047: import java.awt.Insets;
048: import java.awt.Rectangle;
049:
050: import java.awt.event.ActionEvent;
051: import java.awt.event.ActionListener;
052: import java.net.URL;
053: import javax.swing.BorderFactory;
054: import javax.swing.ImageIcon;
055: import javax.swing.JButton;
056: import javax.swing.JScrollPane;
057: import javax.swing.JTextArea;
058: import javax.swing.SwingUtilities;
059:
060: import net.java.hulp.i18n.Logger;
061: import org.netbeans.modules.etl.logger.Localizer;
062: import org.netbeans.modules.etl.logger.LogUtil;
063: import org.openide.windows.TopComponent;
064:
065: /**
066: * Scrollable view for text output. Useful for log traces, etc.
067: *
068: * @author Ritesh Adval
069: */
070: public class SQLLogView extends TopComponent implements IMessageView,
071: ETLOutputPanel {
072:
073: private JTextArea textArea;
074: private JButton refreshButton;
075: private ActionListener aListener;
076: private JButton[] btn = new JButton[1];
077: private static transient final Logger mLogger = LogUtil
078: .getLogger(SQLLogView.class.getName());
079: private static transient final Localizer mLoc = Localizer.get();
080:
081: /** Creates a new instance of SQLLogView */
082: public SQLLogView() {
083: String nbBundle1 = mLoc.t("PRSR001: Execution Log");
084: String viewLabel = Localizer.parse(nbBundle1);
085: this .setName(viewLabel);
086: this .setLayout(new BorderLayout());
087: setBorder(BorderFactory.createEmptyBorder());
088:
089: //add refresh button
090: URL url = getClass()
091: .getResource(
092: "/org/netbeans/modules/sql/framework/ui/resources/images/rerun.png");
093: refreshButton = new JButton(new ImageIcon(url));
094: String nbBundle2 = mLoc.t("PRSR001: Refresh Log");
095: refreshButton.setToolTipText(Localizer.parse(nbBundle2));
096: refreshButton.getAccessibleContext().setAccessibleName(
097: viewLabel);
098: refreshButton.addActionListener(aListener);
099: btn[0] = refreshButton;
100:
101: ActionListener aListener = new ActionListener() {
102:
103: public void actionPerformed(ActionEvent e) {
104: Object src = e.getSource();
105: if (src.equals(refreshButton)) {
106: validate();
107: }
108: }
109: };
110: textArea = new JTextArea();
111: textArea.setBackground(Color.white);
112: textArea.setBorder(BorderFactory.createEmptyBorder());
113: textArea.setEditable(false);
114: textArea.setWrapStyleWord(true);
115: textArea.setLineWrap(true);
116: textArea.setMargin(new Insets(5, 5, 5, 5));
117: textArea.setFont(new Font("monospaced", Font.PLAIN, 12));
118: textArea.setText("");
119:
120: JScrollPane sPane = new JScrollPane(textArea);
121: this .add(sPane, BorderLayout.CENTER);
122: }
123:
124: /**
125: * Appends given String to the text area.
126: *
127: * @param logMessage message to be appended.
128: * @see org.netbeans.modules.sql.framework.ui.view.IMessageView#appendToView
129: */
130: public void appendToView(String logMessage) {
131: append(logMessage);
132: }
133:
134: /**
135: * @see org.netbeans.modules.sql.framework.ui.view.IMessageView#clearView()
136: */
137: public synchronized void clearView() {
138: textArea.setText("");
139: adjustViewport();
140: }
141:
142: /**
143: * Refreshes view with the given string.
144: *
145: * @param newStr String to refresh with
146: */
147: public synchronized void refreshView(String newStr) {
148: textArea.setText(newStr);
149: adjustViewport();
150: }
151:
152: /**
153: * Adjusts viewport to show data at bottom of screen.
154: */
155: private void adjustViewport() {
156: // setCaretPosition() and scrollRectToVisible() are not thread-safe,
157: // so make sure they are executed on the event dispatch thread.
158: if (!SwingUtilities.isEventDispatchThread()) {
159: SwingUtilities.invokeLater(new Runnable() {
160:
161: public void run() {
162: adjustViewport();
163: }
164: });
165: } else {
166: textArea.setCaretPosition(textArea.getText().length());
167: textArea.scrollRectToVisible(new Rectangle(0, Math.max(0,
168: textArea.getHeight() - 2), 1, 1));
169: }
170: }
171:
172: /**
173: * Appends given string to the text area, and adjusts viewport if necessary to ensure
174: * its visibility.
175: *
176: * @param msg String to be appended to text area.
177: */
178: private synchronized void append(String msg) {
179: textArea.append(msg);
180: adjustViewport();
181: }
182:
183: public JButton[] getVerticalToolBar() {
184: return btn;
185: }
186: }
|