001: /*BEGIN_COPYRIGHT_BLOCK
002: *
003: * Copyright (c) 2001-2007, JavaPLT group at Rice University (javaplt@rice.edu)
004: * All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions are met:
008: * * Redistributions of source code must retain the above copyright
009: * notice, this list of conditions and the following disclaimer.
010: * * Redistributions in binary form must reproduce the above copyright
011: * notice, this list of conditions and the following disclaimer in the
012: * documentation and/or other materials provided with the distribution.
013: * * Neither the names of DrJava, the JavaPLT group, Rice University, nor the
014: * names of its contributors may be used to endorse or promote products
015: * derived from this software without specific prior written permission.
016: *
017: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
018: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
019: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
020: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
021: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
022: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
023: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
024: * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
025: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
026: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
027: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
028: *
029: * This software is Open Source Initiative approved Open Source Software.
030: * Open Source Initative Approved is a trademark of the Open Source Initiative.
031: *
032: * This file is part of DrJava. Download the current version of this project
033: * from http://www.drjava.org/ or http://sourceforge.net/projects/drjava/
034: *
035: * END_COPYRIGHT_BLOCK*/
036:
037: package edu.rice.cs.util.swing;
038:
039: import edu.rice.cs.drjava.ui.MainFrame;
040: import java.awt.*;
041: import java.awt.event.*;
042: import javax.swing.*;
043: import javax.swing.border.*;
044: import java.io.Serializable;
045:
046: /**
047: * Manages a JDialog with a scrollable text area and a button panel.
048: * @version $Id: ScrollableDialog.java 4255 2007-08-28 19:17:37Z mgricken $
049: */
050: public class ScrollableDialog implements Serializable {
051: /** Default width for all ScrollableDialogs. */
052: public static final int DEFAULT_WIDTH = 500;
053: /** Default height for all ScrollableDialogs. */
054: public static final int DEFAULT_HEIGHT = 400;
055: /** JDialog managed by this component. */
056: protected JDialog _dialog;
057: /** JTextArea contained in a scroll pane in this dialog. */
058: protected JTextArea _textArea;
059: /** Panel of buttons at the bottom of this dialog. */
060: protected JPanel _buttonPanel;
061:
062: /**
063: * Creates a new ScrollableDialog with the default width and height.
064: * @param parent Parent frame for this dialog
065: * @param title Title for this dialog
066: * @param header Message to display at the top of this dialog
067: * @param text Text to insert into the scrollable JTextArea
068: */
069: public ScrollableDialog(JFrame parent, String title, String header,
070: String text) {
071: this (parent, title, header, text, DEFAULT_WIDTH, DEFAULT_HEIGHT);
072: }
073:
074: /**
075: * Creates a new ScrollableDialog.
076: * @param parent Parent frame for this dialog
077: * @param title Title for this dialog
078: * @param header Message to display at the top of this dialog
079: * @param text Text to insert into the scrollable JTextArea
080: * @param width Width for this dialog
081: * @param height Height for this dialog
082: */
083: public ScrollableDialog(JFrame parent, String title, String header,
084: String text, int width, int height) {
085: _dialog = new JDialog(parent, title, true);
086: Container content = _dialog.getContentPane();
087:
088: content.setLayout(new BorderLayout());
089:
090: // Create the text area
091: _textArea = new JTextArea();
092: _textArea.setEditable(false);
093: _textArea.setText(text);
094:
095: // Arrange the dialog
096: _dialog.setSize(width, height);
097:
098: // Add components
099: JScrollPane textScroll = new BorderlessScrollPane(_textArea,
100: JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
101: JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
102: JPanel scrollWrapper = new JPanel(new BorderLayout(0, 5));
103: scrollWrapper.setBorder(new EmptyBorder(5, 5, 0, 5));
104: scrollWrapper.add(new JLabel(header), BorderLayout.NORTH);
105: scrollWrapper.add(textScroll, BorderLayout.CENTER);
106: JPanel bottomPanel = new JPanel(new BorderLayout());
107: _buttonPanel = new JPanel(new GridLayout(1, 0, 5, 5));
108: bottomPanel.add(_buttonPanel, BorderLayout.EAST);
109: bottomPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
110: _addButtons();
111:
112: content.add(scrollWrapper, BorderLayout.CENTER);
113: content.add(bottomPanel, BorderLayout.SOUTH);
114:
115: // This method is deprecated. There are alternatives, but it is
116: // probably best to let defer to the standard focus-management
117: // policy rather than trying to customize it.
118: //_textArea.requestDefaultFocus();
119: }
120:
121: /**
122: * Adds buttons to this dialog's button panel.
123: * Subclasses can override this to add different buttons.
124: */
125: protected void _addButtons() {
126: _buttonPanel.add(new JButton(_okAction));
127: }
128:
129: /** A default "OK" action which disposes this dialog when invoked.
130: */
131: private Action _okAction = new AbstractAction("OK") {
132: public void actionPerformed(ActionEvent e) {
133: _dialog.dispose();
134: }
135: };
136:
137: /**
138: * Sets the font for the text area in this dialog.
139: * @param f New font for the text
140: */
141: public void setTextFont(Font f) {
142: _textArea.setFont(f);
143: }
144:
145: /** Shows this dialog. */
146: public void show() {
147: MainFrame.setPopupLoc(_dialog, _dialog.getOwner());
148: _dialog.setVisible(true);
149: }
150: }
|