001: /*
002: * @(#)DialogOutputStream.java 1.2 04/12/06
003: *
004: * Copyright (c) 1997-2003 Sun Microsystems, Inc. All Rights Reserved.
005: *
006: * See the file "LICENSE.txt" for information on usage and redistribution
007: * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
008: */
009: package org.pnuts.awt;
010:
011: import java.awt.Button;
012: import java.awt.Color;
013: import java.awt.Dialog;
014: import java.awt.Dimension;
015: import java.awt.FlowLayout;
016: import java.awt.Frame;
017: import java.awt.Panel;
018: import java.awt.Point;
019: import java.awt.TextArea;
020: import java.awt.Toolkit;
021: import java.awt.event.ActionEvent;
022: import java.awt.event.ActionListener;
023: import java.awt.event.KeyEvent;
024: import java.awt.event.KeyListener;
025: import java.io.ByteArrayOutputStream;
026:
027: /**
028: * Output stream to a Dialog. The flush() method brings up the dialog.
029: */
030: public class DialogOutputStream extends ByteArrayOutputStream implements
031: ActionListener, KeyListener {
032:
033: Frame parent;
034:
035: Dialog dialog;
036:
037: TextArea textArea;
038:
039: Button button;
040:
041: int len = 0;
042:
043: private static FlowLayout flowLayout = new FlowLayout();
044:
045: static int screen_width, screen_height;
046: static {
047: Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
048: screen_width = dim.width;
049: screen_height = dim.height;
050: }
051:
052: public DialogOutputStream(Frame parent) {
053: this (parent, 32);
054: }
055:
056: public DialogOutputStream(Frame parent, int size) {
057: this (parent, size, false);
058: }
059:
060: public DialogOutputStream(Frame parent, int size, boolean modal) {
061: super (size);
062: this .parent = parent;
063: dialog = new Dialog(parent, "Error", modal);
064: textArea = new TextArea("", 0, 0, TextArea.SCROLLBARS_BOTH);
065: textArea.setEditable(false);
066: textArea.setSize(screen_width * 3 / 4, screen_height / 2);
067: textArea.setBackground(Color.white);
068: textArea.addKeyListener(this );
069: dialog.add("Center", textArea);
070:
071: button = new Button("OK");
072: button.addActionListener(this );
073:
074: Panel panel = new Panel();
075: panel.setLayout(flowLayout);
076: panel.add(button);
077: dialog.add("South", panel);
078: dialog.setSize(screen_width * 3 / 4, screen_height / 2);
079: }
080:
081: public void keyTyped(KeyEvent e) {
082: if (e.getKeyChar() == KeyEvent.VK_ENTER) {
083: dialog.setVisible(false);
084: reset();
085: textArea.setText("");
086: }
087: }
088:
089: public void keyPressed(KeyEvent e) {
090: }
091:
092: public void keyReleased(KeyEvent e) {
093: }
094:
095: public void actionPerformed(ActionEvent e) {
096: dialog.setVisible(false);
097: reset();
098: textArea.setText("");
099: dialog.getParent().requestFocus();
100: }
101:
102: public Dimension getSize() {
103: return dialog.getSize();
104: }
105:
106: public void setSize(int w, int h) {
107: dialog.setSize(w, h);
108: }
109:
110: public void toFront() {
111: dialog.toFront();
112: }
113:
114: public synchronized void write(int b) {
115: this .len++;
116: }
117:
118: public synchronized void write(byte b[], int off, int len) {
119: super .write(b, off, len);
120: this .len += len;
121: }
122:
123: public synchronized void flush() {
124: if (len < 1) {
125: return;
126: }
127: textArea.setText(new String(toByteArray()));
128: Point loc = parent.getLocation();
129: Dimension sz1 = parent.getSize();
130: Dimension sz2 = dialog.getSize();
131: int x = loc.x + (sz1.width - sz2.width) / 2;
132: int y = loc.y + (sz1.height - sz2.height) / 2;
133: if (x < 0 || y < 0) {
134: Dimension d = dialog.getToolkit().getScreenSize();
135: x = (d.width - sz2.width) / 2;
136: y = (d.height - sz2.height) / 2;
137: }
138: dialog.setLocation(x, y);
139:
140: dialog.setVisible(true);
141: dialog.toFront();
142: len = 0;
143: }
144: }
|