package com.dwave.gui; import javax.swing.*; import java.awt.*; import java.awt.event.*; import com.dwave.util.DMath; public class DProgressMonitor extends JDialog implements ActionListener { public static final int PLAIN_TYPE = 0, CANCEL_TYPE = 1; DLabel messageLabel, noteLabel; JProgressBar theBar; DButton cancelButton; JPanel aPanel; int counter = 0; int min; int max; float percent = 0f; volatile boolean cancelled = false; public DProgressMonitor(Frame owner, int min, int max, String title, String text, String note, int type) { super(owner, title, false); this.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE); this.getContentPane().setLayout(new com.dwave.layout.VerticalFlowLayout( com.dwave.layout.VerticalFlowLayout.MIDDLE, 5, 5, true, false)); this.setResizable(false); this.min = min; this.max = max; messageLabel = new DLabel(text); messageLabel.setVisible(true); noteLabel = new DLabel(note); noteLabel.setVisible(true); theBar = new JProgressBar(min, max); theBar.setVisible(true); this.getContentPane().add(messageLabel); this.getContentPane().add(noteLabel); this.getContentPane().add(theBar); if(type == CANCEL_TYPE) { aPanel = new JPanel(new FlowLayout()); cancelButton = new DButton("Cancel"); cancelButton.setVisible(true); cancelButton.addActionListener(this); aPanel.add(cancelButton); this.getContentPane().add(aPanel); this.setSize(300, 150); } else this.setSize(300, 100); this.setLocation((owner.getWidth()-this.getWidth()) /2, (owner.getHeight()-this.getHeight()) / 2); // this.paintComponents(this.getGraphics()); } public void setProgress(int newValue) { if(!cancelled) { theBar.setValue(newValue); float f = DMath.round((float)newValue/max*100F, 1); setNote(f + "% completed"); if(f > percent) { percent = f; this.paintComponents(this.getGraphics()); } } } public boolean isCancelled() { return cancelled; } public void destroy() { this.dispose(); } public void setNote(String newNote) { if(!cancelled) noteLabel.setText(newNote); } public void setMessage(String newMessage) { if(!cancelled) messageLabel.setText(newMessage); } public void actionPerformed(ActionEvent e) { this.setMessage("Operation cancelled by user."); this.setNote("Please wait....."); this.setTitle("Cancelling....."); this.paintComponents(this.getGraphics()); _cancel(); } private void _cancel() { cancelled = true; // this.dispose(); } }