001: /*
002: * InterruptibleProgressDialog.java
003: *
004: * Created on 6 June 2006, 01:44
005: *
006: * To change this template, choose Tools | Template Manager
007: * and open the template in the editor.
008: */
009:
010: package org.underworldlabs.swing;
011:
012: import java.awt.Container;
013: import java.awt.Dimension;
014: import java.awt.Frame;
015: import java.awt.GridBagConstraints;
016: import java.awt.GridBagLayout;
017: import java.awt.Insets;
018: import java.awt.event.ActionEvent;
019: import java.awt.event.ActionListener;
020: import javax.swing.BorderFactory;
021: import javax.swing.JButton;
022: import javax.swing.JDialog;
023: import javax.swing.JLabel;
024: import javax.swing.JPanel;
025: import org.underworldlabs.Constants;
026: import org.underworldlabs.swing.util.InterruptibleProcess;
027:
028: /**
029: *
030: * @author Takis Diakoumis
031: * @version $Revision: 1.2 $
032: * @date $Date: 2006/07/15 11:47:49 $
033: */
034: public class InterruptibleProgressDialog extends JDialog implements
035: Runnable, ActionListener {
036:
037: /** The event parent to this object */
038: private InterruptibleProcess process;
039:
040: /** The progress bar widget */
041: private IndeterminateProgressBar progressBar;
042:
043: /** The parent frame of this dialog */
044: private Frame parentFrame;
045:
046: /** The progress bar label text */
047: private String labelText;
048:
049: public InterruptibleProgressDialog(Frame parentFrame, String title,
050: String labelText, InterruptibleProcess process) {
051: super (parentFrame, title, true);
052: this .process = process;
053: this .labelText = labelText;
054: try {
055: jbInit();
056: } catch (Exception e) {
057: e.printStackTrace();
058: }
059: }
060:
061: public void run() {
062: pack();
063: setLocation(GUIUtils.getLocationForDialog(parentFrame,
064: getSize()));
065: progressBar.start();
066: setVisible(true);
067: }
068:
069: private void jbInit() throws Exception {
070:
071: progressBar = new IndeterminateProgressBar();
072: progressBar.setPreferredSize(new Dimension(260, 18));
073:
074: JPanel base = new JPanel(new GridBagLayout());
075:
076: JButton cancelButton = new CancelButton();
077: cancelButton.addActionListener(this );
078:
079: GridBagConstraints gbc = new GridBagConstraints();
080: Insets ins = new Insets(10, 20, 10, 20);
081: gbc.insets = ins;
082: base.add(new JLabel(labelText), gbc);
083: gbc.gridy = 1;
084: gbc.insets.top = 0;
085: base.add(progressBar, gbc);
086: gbc.gridy = 2;
087: gbc.weighty = 1.0;
088: base.add(cancelButton, gbc);
089:
090: base.setBorder(BorderFactory.createEtchedBorder());
091:
092: Container c = this .getContentPane();
093: c.setLayout(new GridBagLayout());
094: c.add(base, new GridBagConstraints(1, 1, 1, 1, 1.0, 1.0,
095: GridBagConstraints.SOUTHEAST, GridBagConstraints.BOTH,
096: new Insets(5, 5, 5, 5), 0, 0));
097: setResizable(false);
098: setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
099: }
100:
101: public void actionPerformed(ActionEvent e) {
102: process.setCancelled(true);
103: process.interrupt();
104: dispose();
105: }
106:
107: public void dispose() {
108: if (progressBar != null) {
109: progressBar.stop();
110: progressBar.cleanup();
111: }
112: super .dispose();
113: }
114:
115: class CancelButton extends JButton {
116:
117: private int DEFAULT_WIDTH = 75;
118: private int DEFAULT_HEIGHT = 30;
119:
120: public CancelButton() {
121: super ("Cancel");
122: setMargin(Constants.EMPTY_INSETS);
123: }
124:
125: public int getWidth() {
126: int width = super .getWidth();
127: if (width < DEFAULT_WIDTH) {
128: return DEFAULT_WIDTH;
129: }
130: return width;
131: }
132:
133: public int getHeight() {
134: int height = super .getHeight();
135: if (height < DEFAULT_HEIGHT) {
136: return DEFAULT_HEIGHT;
137: }
138: return height;
139: }
140:
141: public Dimension getPreferredSize() {
142: return new Dimension(getWidth(), getHeight());
143: }
144: }
145:
146: }
|