001: package org.jsqltool.gui;
002:
003: import java.awt.*;
004: import javax.swing.*;
005: import org.jsqltool.utils.Options;
006:
007: /**
008: * <p>Title: JSqlTool Project</p>
009: * <p>Description: Dialog used to view a progress bar to indicate a work in progress.
010: * </p>
011: * <p>Copyright: Copyright (C) 2006 Mauro Carniel</p>
012: *
013: * <p> This file is part of JSqlTool project.
014: * This library is free software; you can redistribute it and/or
015: * modify it under the terms of the (LGPL) Lesser General Public
016: * License as published by the Free Software Foundation;
017: *
018: * GNU LESSER GENERAL PUBLIC LICENSE
019: * Version 2.1, February 1999
020: *
021: * This library is distributed in the hope that it will be useful,
022: * but WITHOUT ANY WARRANTY; without even the implied warranty of
023: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
024: * Library General Public License for more details.
025: *
026: * You should have received a copy of the GNU Library General Public
027: * License along with this library; if not, write to the Free
028: * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
029: *
030: * The author may be contacted at:
031: * maurocarniel@tin.it</p>
032: *
033: * @author Mauro Carniel
034: * @version 1.0
035: */
036: public class ProgressDialog extends JDialog {
037: JPanel panel1 = new JPanel();
038: JProgressBar progressBar = new JProgressBar();
039: GridBagLayout gridBagLayout1 = new GridBagLayout();
040:
041: /** flag used to start progress */
042: private boolean goOn = false;
043:
044: /** thread used to start progress bar */
045: private ProgressThread thread = new ProgressThread();
046:
047: /** true to increment progress bar, false to decrement it */
048: private boolean inc = true;
049:
050: /** unique instance of the class */
051: private static ProgressDialog instance = null;
052:
053: /** flag used to disable progress dialog closing */
054: private boolean canClose = true;
055:
056: /**
057: * @return unique instance of the class
058: */
059: public static ProgressDialog getInstance() {
060: if (instance == null)
061: instance = new ProgressDialog();
062: return instance;
063: }
064:
065: /**
066: * Private constructor, called by the static initilizer.
067: */
068: private ProgressDialog() {
069: super (MainFrame.getInstance(), false);
070: setSize(340, 70);
071: Dimension screenSize = Toolkit.getDefaultToolkit()
072: .getScreenSize();
073: setLocation((int) screenSize.getWidth() / 2 - 170,
074: (int) screenSize.getHeight() / 2 - 30);
075: setDefaultCloseOperation(JDialog.HIDE_ON_CLOSE);
076: progressBar.setMinimum(0);
077: progressBar.setMaximum(20);
078: try {
079: jbInit();
080: } catch (Exception ex) {
081: }
082: }
083:
084: /**
085: * Method used to start the progress.
086: */
087: public void startProgress() {
088: if (!goOn) {
089: setVisible(true);
090: goOn = true;
091: thread.start();
092: }
093:
094: }
095:
096: /**
097: * Method used to start the progress and disable progress closing by means of stopProgress.
098: */
099: public void startProgressNoClose() {
100: if (!goOn) {
101: canClose = false;
102: setVisible(true);
103: goOn = true;
104: thread.start();
105: }
106:
107: }
108:
109: /**
110: * Method used to stop the progress.
111: */
112: public void stopProgress() {
113: if (!canClose)
114: return;
115: if (goOn) {
116: goOn = false;
117: setVisible(false);
118: }
119: }
120:
121: /**
122: * Method used to force progress stopping.
123: */
124: public void forceStopProgress() {
125: canClose = true;
126: if (goOn) {
127: goOn = false;
128: setVisible(false);
129: }
130: }
131:
132: private void jbInit() throws Exception {
133: panel1.setLayout(gridBagLayout1);
134: progressBar.setPreferredSize(new Dimension(250, 25));
135: this .setTitle(Options.getInstance().getResource(
136: "work in progress..."));
137: getContentPane().add(panel1);
138: panel1.add(progressBar, new GridBagConstraints(0, 0, 1, 1, 0.0,
139: 0.0, GridBagConstraints.CENTER,
140: GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
141: }
142:
143: class ProgressThread extends Thread {
144:
145: public void run() {
146: while (goOn) {
147: if (progressBar.getValue() == progressBar.getMaximum())
148: inc = false;
149: else if (progressBar.getValue() == progressBar
150: .getMinimum())
151: inc = true;
152: progressBar.setValue(progressBar.getValue()
153: + (inc ? 1 : -1));
154: try {
155: sleep(100);
156: } catch (InterruptedException ex) {
157: }
158: }
159: }
160:
161: }
162:
163: }
|