001: package net.sourceforge.squirrel_sql.plugins.dataimport.gui;
002:
003: /*
004: * Copyright (C) 2007 Thorsten Mürell
005: *
006: * This program is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU General Public License
008: * as published by the Free Software Foundation; either version 2
009: * of the License, or any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: * GNU General Public License for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * along with this program; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
019: */
020:
021: import java.awt.BorderLayout;
022: import java.awt.FlowLayout;
023: import java.awt.Frame;
024: import java.awt.GridBagConstraints;
025: import java.awt.GridBagLayout;
026: import java.awt.Insets;
027: import java.awt.event.ActionListener;
028:
029: import javax.swing.BorderFactory;
030: import javax.swing.JButton;
031: import javax.swing.JDialog;
032: import javax.swing.JLabel;
033: import javax.swing.JPanel;
034: import javax.swing.JProgressBar;
035: import javax.swing.SwingUtilities;
036:
037: import net.sourceforge.squirrel_sql.fw.util.StringManager;
038: import net.sourceforge.squirrel_sql.fw.util.StringManagerFactory;
039: import net.sourceforge.squirrel_sql.fw.util.log.ILogger;
040: import net.sourceforge.squirrel_sql.fw.util.log.LoggerController;
041:
042: /**
043: * This class shows a progress dialog while the file is imported.
044: *
045: * @author Thorsten Mürell
046: */
047: public class ProgressBarDialog {
048:
049: private static JProgressBar progressBar = null;
050: private static JLabel message = null;
051: private static JButton cancelButton = null;
052:
053: private static JDialog dialog = null;
054:
055: /** Internationalized strings for this class */
056: private static final StringManager stringMgr = StringManagerFactory
057: .getStringManager(ProgressBarDialog.class);
058:
059: /** Logger for this class. */
060: private final static ILogger log = LoggerController
061: .createLogger(ProgressBarDialog.class);
062:
063: /**
064: * Returns the dialog
065: *
066: * @param owner The owner
067: * @param title The title for the dialog
068: * @param modal If this should be a modal dialog
069: * @param listener The listener for actions
070: * @return The dialog to show
071: */
072: public static JDialog getDialog(final Frame owner,
073: final String title, final boolean modal,
074: final ActionListener listener) {
075: if (SwingUtilities.isEventDispatchThread()) {
076: _getDialog(owner, title, modal, listener);
077: } else {
078: try {
079: SwingUtilities.invokeAndWait(new Runnable() {
080: public void run() {
081: _getDialog(owner, title, modal, listener);
082: }
083: });
084: } catch (Exception e) {
085: //i18n[ProgressBarDialog.error.getdialog=getDialog: unable to invokeAndWait for dialog]
086: log
087: .error(
088: stringMgr
089: .getString("ProgressBarDialog.error.getdialog"),
090: e);
091: }
092: }
093:
094: return dialog;
095: }
096:
097: private static void _getDialog(Frame owner, String title,
098: boolean modal, ActionListener listener) {
099: dialog = new JDialog(owner, title, modal);
100: dialog.getContentPane().setLayout(new BorderLayout());
101: dialog.getContentPane().add(buildPanel(), BorderLayout.CENTER);
102: dialog.getContentPane().add(buildButtonPanel(),
103: BorderLayout.SOUTH);
104: dialog.setSize(250, 135);
105: dialog.setLocationRelativeTo(owner);
106: cancelButton.addActionListener(listener);
107: dialog.setVisible(true);
108: }
109:
110: private static JPanel buildPanel() {
111: JPanel dataPanel = new JPanel();
112: dataPanel.setBorder(BorderFactory.createEmptyBorder(0, 10, 0,
113: 10));
114: GridBagLayout gl = new GridBagLayout();
115: dataPanel.setLayout(gl);
116: GridBagConstraints c;
117:
118: c = new GridBagConstraints();
119: c.gridx = 0;
120: c.gridy = 0;
121: c.fill = GridBagConstraints.HORIZONTAL;
122: c.anchor = GridBagConstraints.WEST;
123: // i18n[ProgressBarDialog.insertingRecordsLabel=Copying records]
124: String topLabelText = stringMgr
125: .getString("ProgressBarDialog.insertingRecordsLabel");
126: message = new JLabel(topLabelText);
127: dataPanel.add(message, c);
128:
129: c = new GridBagConstraints();
130: c.gridx = 0;
131: c.gridy = 1;
132: c.anchor = GridBagConstraints.WEST;
133: c.fill = GridBagConstraints.HORIZONTAL;
134: c.insets = new Insets(0, 0, 10, 0);
135: c.weightx = 1.0;
136: progressBar = new JProgressBar(0, 10);
137: dataPanel.add(progressBar, c);
138:
139: return dataPanel;
140: }
141:
142: private static JPanel buildButtonPanel() {
143: JPanel buttonPanel = new JPanel(new FlowLayout());
144: // i18n[ProgressBarDialog.cancelButtonLabel=Cancel]
145: String buttonText = stringMgr
146: .getString("ProgressBarDialog.cancelButtonLabel");
147: cancelButton = new JButton(buttonText);
148: buttonPanel.add(cancelButton);
149: return buttonPanel;
150: }
151:
152: /**
153: * Sets the message for the progress bar dialog
154: *
155: * @param msg The message to show
156: */
157: public static void setMessage(final String msg) {
158: if (SwingUtilities.isEventDispatchThread()) {
159: message.setText(msg);
160: } else {
161: SwingUtilities.invokeLater(new Runnable() {
162: public void run() {
163: message.setText(msg);
164: }
165: });
166: }
167: }
168:
169: /**
170: * Sets the Dialog to indeterminate.
171: *
172: * Use this, when you do not know the number
173: * of items to process.
174: */
175: public static void setIndeterminate() {
176: if (SwingUtilities.isEventDispatchThread()) {
177: progressBar.setIndeterminate(true);
178: } else {
179: SwingUtilities.invokeLater(new Runnable() {
180: public void run() {
181: progressBar.setIndeterminate(true);
182: }
183: });
184: }
185: }
186:
187: /**
188: * Sets the minimum and maximum values for the dialog.
189: *
190: * @param min The minimum value
191: * @param max The maximum value
192: */
193: public static void setBarMinMax(final int min, final int max) {
194: if (progressBar.getMinimum() == min
195: && progressBar.getMaximum() == max) {
196: return;
197: }
198: if (SwingUtilities.isEventDispatchThread()) {
199: progressBar.setMinimum(min);
200: progressBar.setMaximum(max);
201: } else {
202: SwingUtilities.invokeLater(new Runnable() {
203: public void run() {
204: progressBar.setMinimum(min);
205: progressBar.setMaximum(max);
206: }
207: });
208: }
209: }
210:
211: /**
212: * Sets the value for the progress bar
213: *
214: * @param value The current value
215: */
216: public static void setBarValue(final int value) {
217: if (SwingUtilities.isEventDispatchThread()) {
218: progressBar.setValue(value);
219: } else {
220: SwingUtilities.invokeLater(new Runnable() {
221: public void run() {
222: progressBar.setValue(value);
223: }
224: });
225: }
226: }
227:
228: /**
229: * Increments the progress bar by the given value
230: *
231: * @param value The value to increment the bar
232: */
233: public static void incrementBar(final int value) {
234: final int newValue = progressBar.getValue() + value;
235: if (SwingUtilities.isEventDispatchThread()) {
236: progressBar.setValue(newValue);
237: } else {
238: SwingUtilities.invokeLater(new Runnable() {
239: public void run() {
240: progressBar.setValue(newValue);
241: }
242: });
243: }
244: }
245:
246: /**
247: * Sets the visibiility of the progress dialog
248: *
249: * @param visible a boolean value indicating whether or not to make the
250: * dialog visible.
251: */
252: public static void setVisible(final boolean visible) {
253: if (dialog == null) {
254: return;
255: }
256: if (dialog.isVisible() != visible) {
257: if (SwingUtilities.isEventDispatchThread()) {
258: dialog.setVisible(visible);
259: } else {
260: SwingUtilities.invokeLater(new Runnable() {
261: public void run() {
262: dialog.setVisible(visible);
263: }
264: });
265: }
266: }
267: }
268:
269: /**
270: * Closes the progress dialog.
271: */
272: public static void dispose() {
273: if (dialog == null) {
274: return;
275: }
276: if (SwingUtilities.isEventDispatchThread()) {
277: dialog.dispose();
278: } else {
279: SwingUtilities.invokeLater(new Runnable() {
280: public void run() {
281: dialog.dispose();
282: }
283: });
284: }
285: }
286: }
|