001 /* 002 * LAPIS lightweight structured text processing system 003 * 004 * Copyright (C) 1998-2002 Carnegie Mellon University, 005 * Copyright (C) 2003 Massachusetts Institute of Technology. 006 * All rights reserved. 007 * 008 * This library is free software; you can redistribute it 009 * and/or modify it under the terms of the GNU General 010 * Public License as published by the Free Software 011 * Foundation, version 2. 012 * 013 * LAPIS homepage: http://graphics.lcs.mit.edu/lapis/ 014 */ 015 package lapisx.progress.swing; 016 017 import java.awt.*; 018 import java.awt.event.*; 019 import javax.swing.*; 020 import lapisx.swing.Win; 021 import lapisx.progress.*; 022 023 public class SwingProgressBar extends JProgressBar implements ProgressListener { 024 025 boolean cancelled = false; 026 JDialog dialog = null; 027 028 public SwingProgressBar () { 029 setStringPainted (true); 030 setBorderPainted (true); 031 } 032 033 public JDialog makeDialog (Component parent, String title, boolean modal) { 034 if (dialog != null) 035 return dialog; 036 037 dialog = new JDialog (Win.findFrameOrMakeFrame (parent), 038 title, modal); 039 Container contentPane = dialog.getContentPane (); 040 041 contentPane.setLayout (new BorderLayout ()); 042 contentPane.add (this); 043 044 Panel bottom = new Panel (); 045 Button cancelButton = new Button ("Cancel"); 046 cancelButton.addActionListener (new ActionListener () { 047 public void actionPerformed (ActionEvent event) { 048 cancel (); 049 } 050 }); 051 bottom.add (cancelButton); 052 contentPane.add (bottom, BorderLayout.SOUTH); 053 054 dialog.pack (); 055 Win.center (dialog, parent); 056 057 return dialog; 058 } 059 060 public void cancel () { 061 cancelled = true; 062 if (dialog != null) 063 dialog.dispose (); 064 } 065 066 public void startProgress (ProgressEvent event) { 067 handle (event); 068 } 069 070 public void makeProgress (ProgressEvent event) { 071 handle (event); 072 } 073 074 public void doneProgress (ProgressEvent event) { 075 handle (event); 076 } 077 078 private void handle (final ProgressEvent event) { 079 if (cancelled) 080 throw new CancelException (); 081 SwingUtilities.invokeLater (new Runnable () { 082 public void run () { 083 setString (event.getLabel ()); 084 setMinimum (event.getMinimum ()); 085 setMaximum (event.getMaximum ()); 086 setValue (event.getValue ()); 087 } 088 }); 089 } 090 091 public static void main (String[] args) throws Exception { 092 SwingProgressBar pb = new SwingProgressBar (); 093 JDialog dlg = pb.makeDialog (null, "Progress", false); 094 dlg.show (); 095 096 BasicProgressGenerator gen = new BasicProgressGenerator (dlg); 097 gen.addProgressListener (pb); 098 gen.fireStartProgress ("Counting to infinity", 1000); 099 Thread.currentThread ().setPriority (4); 100 try { 101 for (int i = 0; dlg != null && i < 20; ++i) { 102 Thread.sleep (500); 103 gen.fireProgress (); 104 } 105 gen.fireDoneProgress (); 106 } catch (CancelException e) { 107 System.err.println (e); 108 } finally { 109 dlg.dispose (); 110 } 111 112 System.exit (0); 113 } 114 115 }