001: package net.sourceforge.squirrel_sql.client.gui;
002:
003: /*
004: * Copyright (C) 2007 Rob Manning
005: * manningr@user.sourceforge.net
006: *
007: * This library is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU Lesser General Public
009: * License as published by the Free Software Foundation; either
010: * version 2.1 of the License, or (at your option) any later version.
011: *
012: * This library is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this library; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
020: */
021: import java.awt.Dialog;
022: import java.awt.Dimension;
023: import java.awt.Frame;
024: import java.awt.GridBagConstraints;
025: import java.awt.GridBagLayout;
026: import java.awt.Insets;
027: import java.awt.Window;
028:
029: import javax.swing.BorderFactory;
030: import javax.swing.JDialog;
031: import javax.swing.JFrame;
032: import javax.swing.JLabel;
033: import javax.swing.JPanel;
034: import javax.swing.JProgressBar;
035:
036: import net.sourceforge.squirrel_sql.client.ApplicationArguments;
037: import net.sourceforge.squirrel_sql.fw.gui.GUIUtils;
038: import net.sourceforge.squirrel_sql.fw.sql.ProgressCallBack;
039: import net.sourceforge.squirrel_sql.fw.util.StringManager;
040: import net.sourceforge.squirrel_sql.fw.util.StringManagerFactory;
041: import net.sourceforge.squirrel_sql.fw.util.log.ILogger;
042: import net.sourceforge.squirrel_sql.fw.util.log.LoggerController;
043:
044: /**
045: * A simple class that can be used to show the user a dialog to indicate the
046: * progress of some task using the ProgressCallBack interface. Since certain
047: * classes in fw module interact with the database and certain operations can
048: * take quite a long time, letting the user know how it's going is nice.
049: * However, fw module classes don't (and shouldn't) know anything about the UI
050: * as this is the responsibility of the app module classes. So, this class
051: * can be passed in by app classes to certain fw long-running methods to
052: * bridge the gap and provide feedback to the user.
053: *
054: * @author manningr
055: */
056: public class ProgessCallBackDialog extends JDialog implements
057: ProgressCallBack {
058:
059: /** Logger for this class. */
060: private final static ILogger s_log = LoggerController
061: .createLogger(ProgessCallBackDialog.class);
062:
063: /** Internationalized strings for this class */
064: private static final StringManager s_stringMgr = StringManagerFactory
065: .getStringManager(ProgessCallBackDialog.class);
066:
067: static interface i18n {
068: //i18n[ProgressCallBackDialog.defaultLoadingPrefix=Loading:]
069: String DEFAULT_LOADING_PREFIX = s_stringMgr
070: .getString("ProgressCallBackDialog.defaultLoadingPrefix");
071:
072: //i18n[ProgressCallBackDialog.initialLoadingPrefix=Loading...]
073: String INITIAL_LOADING_PREFIX = s_stringMgr
074: .getString("ProgressCallBackDialog.initialLoadingPrefix");
075: }
076:
077: private int itemCount = 0;
078:
079: private JProgressBar progressBar = null;
080:
081: private JLabel statusLabel = null;
082:
083: private String _loadingPrefix = i18n.DEFAULT_LOADING_PREFIX;
084:
085: public ProgessCallBackDialog(Dialog owner, String title,
086: int totalItems) {
087: super (owner, title);
088: init(totalItems);
089: }
090:
091: public ProgessCallBackDialog(Frame owner, String title,
092: int totalItems) {
093: super (owner, title);
094: setLocationRelativeTo(owner);
095: init(totalItems);
096: }
097:
098: private void init(int totalItems) {
099: itemCount = totalItems;
100: final Window owner = super .getOwner();
101: final ProgessCallBackDialog dialog = this ;
102:
103: GUIUtils.processOnSwingEventThread(new Runnable() {
104: public void run() {
105: createGUI();
106: setLocationRelativeTo(owner);
107: dialog.setVisible(true);
108: }
109: }, true);
110:
111: }
112:
113: public void setTotalItems(int totalItems) {
114: itemCount = totalItems;
115: progressBar.setMaximum(totalItems);
116: }
117:
118: /**
119: * Sets the text that is displayed before each thing being loaded. By
120: * default this is the string "Loading:".
121: * @param loadingPrefix
122: */
123: public void setLoadingPrefix(String loadingPrefix) {
124: if (loadingPrefix != null) {
125: _loadingPrefix = loadingPrefix;
126: }
127: }
128:
129: /* (non-Javadoc)
130: * @see net.sourceforge.squirrel_sql.fw.sql.ProgressCallBack#currentlyLoading(java.lang.String)
131: */
132: public void currentlyLoading(final String simpleName) {
133: final StringBuilder statusText = new StringBuilder();
134: statusText.append(_loadingPrefix);
135: statusText.append(" ");
136: statusText.append(simpleName);
137: try {
138: GUIUtils.processOnSwingEventThread(new Runnable() {
139: public void run() {
140: statusLabel.setText(statusText.toString());
141: progressBar.setValue(progressBar.getValue() + 1);
142: if (finishedLoading()) {
143: ProgessCallBackDialog.this .setVisible(false);
144: return;
145: }
146: }
147: });
148: } catch (Exception e) {
149: s_log.error("Unexpected exception: " + e.getMessage(), e);
150: }
151: }
152:
153: public boolean finishedLoading() {
154: return progressBar.getValue() >= itemCount - 1;
155: }
156:
157: private void createGUI() {
158: JPanel dialogPanel = new JPanel(new GridBagLayout());
159: dialogPanel.setBorder(BorderFactory.createEmptyBorder(0, 10, 0,
160: 10));
161: GridBagConstraints c;
162:
163: c = new GridBagConstraints();
164: c.gridx = 0;
165: c.gridy = 0;
166: c.fill = GridBagConstraints.HORIZONTAL;
167: c.anchor = GridBagConstraints.WEST;
168:
169: statusLabel = new JLabel(i18n.INITIAL_LOADING_PREFIX);
170: dialogPanel.add(statusLabel, c);
171:
172: progressBar = new JProgressBar(0, itemCount);
173: c = new GridBagConstraints();
174: c.gridx = 0;
175: c.gridy = 1;
176: c.anchor = GridBagConstraints.WEST;
177: c.fill = GridBagConstraints.HORIZONTAL;
178: c.insets = new Insets(0, 0, 10, 0);
179: c.weightx = 1.0;
180:
181: dialogPanel.add(progressBar, c);
182: super .getContentPane().add(dialogPanel);
183: super .pack();
184: super .setSize(new Dimension(400, 100));
185: }
186:
187: /**
188: * @param args
189: */
190: public static void main(String[] args) throws Exception {
191: ApplicationArguments.initialize(new String[] {});
192: String[] tables = new String[] { "table_a", "table_b",
193: "table_c", "table_d", "table_e", };
194: JFrame parent = new JFrame();
195: GUIUtils.centerWithinScreen(parent);
196: parent.setSize(new Dimension(200, 200));
197: parent.setVisible(true);
198: ProgessCallBackDialog dialog = new ProgessCallBackDialog(
199: parent, "test", 5);
200:
201: dialog.setVisible(true);
202: for (int i = 0; i < 5; i++) {
203: dialog.currentlyLoading(tables[i]);
204: Thread.sleep(1000);
205: }
206: System.exit(0);
207: }
208:
209: }
|