001: /*
002: * Gruntspud
003: *
004: * Copyright (C) 2002 Brett Smith.
005: *
006: * Written by: Brett Smith <t_magicthize@users.sourceforge.net>
007: *
008: * This program is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public
009: * License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
010: * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more details.
012: *
013: * You should have received a copy of the GNU Library General Public License along with this program; if not, write to the Free
014: * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
015: */
016:
017: package gruntspud.ui;
018:
019: import gruntspud.Constants;
020: import gruntspud.GruntspudContext;
021: import gruntspud.actions.AbstractGruntspudAction;
022: import gruntspud.actions.DefaultGruntspudAction;
023: import gruntspud.actions.GruntspudAction;
024:
025: import java.awt.BorderLayout;
026: import java.awt.Component;
027: import java.awt.Container;
028: import java.awt.Dimension;
029: import java.awt.FlowLayout;
030: import java.awt.GridBagConstraints;
031: import java.awt.GridBagLayout;
032: import java.awt.Insets;
033: import java.awt.Window;
034: import java.awt.event.ActionEvent;
035: import java.awt.event.WindowAdapter;
036: import java.awt.event.WindowEvent;
037:
038: import javax.swing.Action;
039: import javax.swing.BorderFactory;
040: import javax.swing.JComponent;
041: import javax.swing.JDialog;
042: import javax.swing.JFrame;
043: import javax.swing.JLabel;
044: import javax.swing.JPanel;
045: import javax.swing.JProgressBar;
046: import javax.swing.SwingConstants;
047: import javax.swing.SwingUtilities;
048:
049: /**
050: * Dialog that can be used to show the current progress of a command. Two progress bars are available for use.
051: *
052: * @author magicthize
053: */
054: public class CommandProgressDialog extends JDialog {
055: // Private instance variables
056: private JProgressBar progressBar, progressBar2;
057: private GruntspudContext context;
058: private JLabel value1;
059: private JLabel value2;
060: private JLabel label1;
061: private JLabel label2;
062: private JLabel progressBar2Name;
063: private boolean cancelled;
064: private String geometryPropertyName;
065: private JPanel progressPanel, progress2Panel;
066: private boolean indeterminate1, indeterminate2;
067:
068: /**
069: * Creates a new CommandProgressDialog object.
070: *
071: * @param parent parent dialog
072: * @param modal modal to parent
073: * @param context context
074: * @param action if not null, then the action will be added as a button to the dialog
075: * @param label1Text text for label 1
076: * @param label2Text text for label 2
077: * @param title dialog title
078: * @param geometryPropertyName property name to store the dialog geometry in
079: * @param createDefaultCancelAction create the default cancel action
080: */
081: public CommandProgressDialog(JDialog parent, boolean modal,
082: GruntspudContext context, Action action, String label1Text,
083: String label2Text, String title,
084: String geometryPropertyName,
085: boolean createDefaultCancelAction) {
086: super (parent, title, modal);
087: init(context, action, label1Text, label2Text,
088: geometryPropertyName, createDefaultCancelAction);
089: }
090:
091: /**
092: * Creates a new CommandProgressDialog object.
093: *
094: * @param parent parent frame
095: * @param modal modal to parent
096: * @param context context
097: * @param action if not null, then the action will be added as a button to the dialog
098: * @param label1Text text for label 1
099: * @param label2Text text for label 2
100: * @param title dialog title
101: * @param geometryPropertyName property name to store the dialog geometry in
102: * @param createDefaultCancelAction create the default cancel action
103: */
104: public CommandProgressDialog(JFrame parent, boolean modal,
105: GruntspudContext context, Action action, String label1Text,
106: String label2Text, String title,
107: String geometryPropertyName,
108: boolean createDefaultCancelAction) {
109: super (parent, title, modal);
110: init(context, action, label1Text, label2Text,
111: geometryPropertyName, createDefaultCancelAction);
112: }
113:
114: /**
115: * Create a modal progress dialog
116: *
117: * @param parent parent component
118: * @param context context
119: * @param geometryPropertyName property name to store the dialog geometry in
120: * @param label1Text text for label 1
121: * @param label2Text text for label 2
122: * @param title dialog title
123: * @param createDefaultCancelAction create the default cancel action
124: * @return dialog
125: */
126: public static CommandProgressDialog createDialog(Component parent,
127: GruntspudContext context, String geometryPropertyName,
128: String label1Text, String label2Text, String title,
129: boolean createDefaultCancelAction) {
130: return createDialog(parent, context, geometryPropertyName,
131: null, label1Text, label2Text, title,
132: createDefaultCancelAction, true);
133: }
134:
135: /**
136: * Create a modal progress dialog
137: *
138: * @param parent parent component
139: * @param context context
140: * @param geometryPropertyName property name to store the dialog geometry in
141: * @param action if not null, then the action will be added as a button to the dialog
142: * @param label1Text text for label 1
143: * @param label2Text text for label 2
144: * @param title dialog title
145: * @param createDefaultCancelAction create the default cancel action
146: * @param modal make the dialog modal to the parent
147: * @return dialog
148: */
149: public static CommandProgressDialog createDialog(Component parent,
150: GruntspudContext context, String geometryPropertyName,
151: Action action, String label1Text, String label2Text,
152: String title, boolean createDefaultCancelAction,
153: boolean modal) {
154: Window w = (Window) SwingUtilities.getAncestorOfClass(
155: Window.class, parent);
156: CommandProgressDialog progressDialog = null;
157:
158: if ((w != null) && w instanceof JDialog) {
159: progressDialog = new CommandProgressDialog((JDialog) w,
160: modal, context, action, label1Text, label2Text,
161: title, geometryPropertyName,
162: createDefaultCancelAction);
163: } else if ((w != null) && w instanceof JFrame) {
164: progressDialog = new CommandProgressDialog((JFrame) w,
165: modal, context, action, label1Text, label2Text,
166: title, geometryPropertyName,
167: createDefaultCancelAction);
168: } else {
169: progressDialog = new CommandProgressDialog((JFrame) null,
170: modal, context, action, label1Text, label2Text,
171: title, geometryPropertyName,
172: createDefaultCancelAction);
173:
174: }
175: return progressDialog;
176: }
177:
178: private void cancel() {
179: cancelled = true;
180: }
181:
182: private void init(GruntspudContext context, Action action,
183: String label1Text, String label2Text,
184: String geometryPropertyName,
185: boolean createDefaultCancelAction) {
186: this .context = context;
187: this .geometryPropertyName = geometryPropertyName;
188:
189: //
190: progressPanel = new JPanel(new BorderLayout());
191: progressPanel.setBorder(BorderFactory.createEmptyBorder(4, 4,
192: 4, 4));
193: progressBar = new JProgressBar(0, 100);
194: setIndeterminate(true);
195: progressBar.setStringPainted(true);
196: progressBar2 = new JProgressBar(0, 100);
197: progressBar2.setStringPainted(true);
198:
199: JPanel s = new JPanel(new GridBagLayout());
200: GridBagConstraints gbc = new GridBagConstraints();
201: gbc.insets = new Insets(3, 3, 3, 3);
202: gbc.anchor = GridBagConstraints.NORTHWEST;
203: gbc.fill = GridBagConstraints.HORIZONTAL;
204: UIUtil.jGridBagAdd(s, label1 = new JLabel(label1Text), gbc,
205: GridBagConstraints.RELATIVE);
206: gbc.weightx = 1.0;
207: UIUtil.jGridBagAdd(s, value1 = new JLabel(), gbc,
208: GridBagConstraints.REMAINDER);
209: gbc.weightx = 0.0;
210: UIUtil.jGridBagAdd(s, label2 = new JLabel(label2Text), gbc,
211: GridBagConstraints.RELATIVE);
212: gbc.weightx = 1.0;
213: gbc.weighty = 1.0;
214: UIUtil.jGridBagAdd(s, value2 = new JLabel(), gbc,
215: GridBagConstraints.REMAINDER);
216: progressPanel.add(s, BorderLayout.NORTH);
217: progressBar2Name = new JLabel(" ", JLabel.CENTER);
218: progress2Panel = new JPanel(new BorderLayout(0, 4));
219: progress2Panel.add(progressBar2Name, BorderLayout.NORTH);
220: progress2Panel.add(progressBar2, BorderLayout.CENTER);
221:
222: JPanel p = new JPanel(new BorderLayout(0, 4));
223: p.setBorder(BorderFactory.createEmptyBorder(4, 8, 4, 8));
224: p.add(progressBar, BorderLayout.NORTH);
225: p.add(progress2Panel, BorderLayout.SOUTH);
226:
227: progressPanel.add(p, BorderLayout.SOUTH);
228:
229: //
230: getContentPane().setLayout(new BorderLayout());
231: getContentPane().add(progressPanel, BorderLayout.CENTER);
232:
233: // If an action was specifed, create the default
234: if ((action == null) && createDefaultCancelAction) {
235: action = new CancelAction();
236:
237: }
238: if (action != null) {
239: JPanel b = new JPanel(new FlowLayout(FlowLayout.CENTER));
240: b.add(UIUtil.createButton(action, true, false));
241: getContentPane().add(b, BorderLayout.SOUTH);
242: }
243:
244: // Postion and size
245: if (context.getHost().isGeometryStored(geometryPropertyName)) {
246: context.getHost().loadGeometry(this , geometryPropertyName);
247: } else {
248: pack();
249: UIUtil.positionComponent(SwingConstants.CENTER, this );
250: }
251:
252: setProgress2Visible(false);
253:
254: // Listen for window closing
255: addWindowListener(new WindowAdapter() {
256: public void windowClosing(WindowEvent evt) {
257: saveGeometry();
258: }
259: });
260: }
261:
262: public void saveGeometry() {
263: Constants.PLUGIN_LOG.info("Saving progress dialog geometry "
264: + CommandProgressDialog.this .geometryPropertyName
265: + " as " + getSize());
266: CommandProgressDialog.this .context.getHost().saveGeometry(
267: CommandProgressDialog.this ,
268: CommandProgressDialog.this .geometryPropertyName);
269: }
270:
271: /**
272: * Set the visibility of the dialog.
273: *
274: * @param visible visible
275: */
276: public void setVisible(boolean visible) {
277: if (isVisible() && !visible) {
278: saveGeometry();
279:
280: }
281: super .setVisible(visible);
282: }
283:
284: /**
285: * Return the main component used in the dialog
286: *
287: * @return component
288: */
289: public JComponent getMainComponent() {
290: return progressPanel;
291: }
292:
293: /**
294: * Get if the button created for the cancel action was created
295: *
296: * @return cancelled
297: */
298: public boolean isCancelled() {
299: return cancelled;
300: }
301:
302: /**
303: * Set the text for label 1
304: *
305: * @param text label 1 text
306: */
307: public void setLabel1Text(String text) {
308: label1.setText(text);
309: }
310:
311: /**
312: * Set the text for label 2
313: *
314: * @param text label 2 text
315: */
316: public void setLabel2Text(String text) {
317: label2.setText(text);
318: }
319:
320: /**
321: * Set whether progress bar 1 is indeterminate. This only works on J2SE 1.4+
322: *
323: * @param indeterminate indeterminate
324: */
325: public void setIndeterminate(boolean indeterminate) {
326: try {
327: progressBar.getClass().getMethod("setIndeterminate",
328: new Class[] { boolean.class }).invoke(progressBar,
329: new Object[] { new Boolean(indeterminate) });
330: } catch (Exception e) {
331: setProgressValue(0);
332: }
333: this .indeterminate1 = indeterminate;
334: }
335:
336: /**
337: * Set whether progress bar 1 is indeterminate. This only works on J2SE 1.4+
338: *
339: * @param indeterminate indeterminate
340: */
341: public void setIndeterminate2(boolean indeterminate) {
342: try {
343: progressBar2.getClass().getMethod("setIndeterminate",
344: new Class[] { boolean.class }).invoke(progressBar,
345: new Object[] { new Boolean(indeterminate) });
346: } catch (Exception e) {
347: setProgress2Value(0);
348: }
349: this .indeterminate2 = indeterminate;
350: }
351:
352: /**
353: * Set the text on the first progress bar
354: *
355: * @param text text on first progress bar
356: */
357: public void setString(String text) {
358: progressBar.setString(text);
359: }
360:
361: /**
362: * Set whether the text is painted on the first progress bar
363: *
364: * @param stringPainted string painted
365: */
366: public void setStringPainted(boolean stringPainted) {
367: progressBar.setStringPainted(stringPainted);
368: }
369:
370: /**
371: * Get if the text is painted on the first progress bar
372: *
373: * @return string painted on progress bar 1
374: */
375: public boolean isStringPainted() {
376: return progressBar.isStringPainted();
377: }
378:
379: /**
380: * Set the text on the second progress bar
381: *
382: * @param text text on second progress bar
383: */
384: public void setString2(String text) {
385: progressBar2.setString(text);
386: }
387:
388: /**
389: * Set whether the text is painted on the second progress bar
390: *
391: * @param stringPainted string painted
392: */
393: public void setString2Painted(boolean stringPainted) {
394: progressBar2.setStringPainted(stringPainted);
395: }
396:
397: /**
398: * Get if the text is painted on the first progress bar
399: *
400: * @return string painted on progress bar 1
401: */
402: public boolean isString2Painted() {
403: return progressBar2.isStringPainted();
404: }
405:
406: /**
407: * Set the text of the second <i>value</i> label.
408: *
409: * @param text value 2 text
410: */
411: public void setValue2Text(String text) {
412: value2.setText(text);
413: }
414:
415: /**
416: * Set the tooltip on the second <i>value</i> label.
417: *
418: * @param text tool tip text
419: */
420: public void setValue2ToolTipText(String text) {
421: value2.setToolTipText(text);
422: }
423:
424: /**
425: * Set the text of the first <i>value</i> label.
426: *
427: * @param text value 1 text
428: */
429: public void setValue1Text(String text) {
430: value1.setText(text);
431: }
432:
433: /**
434: * Set the maximum value of the first progress bar
435: *
436: * @param max progress bar 1 maximum
437: */
438: public void setProgressMaximum(int max) {
439: progressBar.setMaximum(max);
440: }
441:
442: /**
443: * Set the maximum value of the second progress bar
444: *
445: * @param max progress bar 2 maximum
446: */
447: public void setProgress2Maximum(int max) {
448: progressBar2.setMaximum(max);
449: }
450:
451: /**
452: * Set the minimum value of the first progress bar
453: *
454: * @param min progress bar 1 minimum
455: */
456: public void setProgressMinimum(int min) {
457: progressBar.setMinimum(min);
458: }
459:
460: /**
461: * Set the value of the first progress bar
462: *
463: * @param val progress bar 1 value
464: */
465: public void setProgressValue(int val) {
466: progressBar.setValue(val);
467: }
468:
469: /**
470: * Set the value of the second progress bar
471: *
472: * @param val progress bar 2 value
473: */
474: public void setProgress2Value(int val) {
475: progressBar2.setValue(val);
476: }
477:
478: /**
479: * Set the text displayed above the second progress bar
480: *
481: * @param name progress bar 2 name
482: */
483: public void setProgress2Name(String name) {
484: progressBar2Name.setText(name);
485: }
486:
487: /**
488: * Set the tool tip text of the first <i>value</i> label
489: *
490: * @param text tool tip text of value 1
491: */
492: public void setValue1ToolTipText(String text) {
493: value1.setToolTipText(text);
494: }
495:
496: /**
497: * Set whether the second progress bar and its name is visible
498: *
499: * @param visibile progress bar 2 visibile
500: */
501: public void setProgress2Visible(boolean visible) {
502: progress2Panel.setVisible(visible);
503: packHeight();
504: }
505:
506: /**
507: * Get the maximum value of the first progress bar
508: *
509: * @return maximum value of first progress bar
510: */
511: public int getProgressMaximum() {
512: return progressBar.getMaximum();
513: }
514:
515: /**
516: * Get the current value of the first progress bar
517: *
518: * @return current value of first progress bar
519: */
520: public int getProgressValue() {
521: return progressBar.getValue();
522: }
523:
524: /**
525: * Return if the first progress bar is indeterminate
526: *
527: * @return indeterminate
528: */
529: public boolean isProgressIndeterminate() {
530: return indeterminate1;
531: }
532:
533: /**
534: * Pack the dialog, but change the height only
535: */
536: public void packHeight() {
537: Container parent = getParent();
538: if (parent != null && parent.getPeer() == null) {
539: parent.addNotify();
540: }
541: if (getPeer() == null) {
542: addNotify();
543: }
544: setSize(new Dimension(getSize().width,
545: getPreferredSize().height));
546: validate();
547: }
548:
549: /**
550: * Get if the second progress bar is visible
551: *
552: * @return second progress bar
553: */
554: public boolean isProgress2Visible() {
555: return progress2Panel.isVisible();
556: }
557:
558: public class CancelAction extends AbstractGruntspudAction {
559: CancelAction() {
560: putValue(Action.NAME, "Cancel");
561: putValue(GruntspudAction.SHOW_NAME, Boolean.TRUE);
562: putValue(GruntspudAction.ICON, UIUtil
563: .getCachedIcon(Constants.ICON_TOOL_STOP_COMMAND));
564: putValue(
565: DefaultGruntspudAction.SMALL_ICON,
566: UIUtil
567: .getCachedIcon(Constants.ICON_TOOL_SMALL_STOP_COMMAND));
568: putValue(Action.MNEMONIC_KEY, new Integer('c'));
569: }
570:
571: public void actionPerformed(ActionEvent evt) {
572: cancel();
573: }
574: }
575: }
|