BrightSide Workbench Full Report + Source Code
ProgressSheet.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2011 Lluis TurrĂ³ Cutiller <http://www.turro.org/>
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Affero General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Affero General Public License for more details.
14  *
15  * You should have received a copy of the GNU Affero General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 package org.turro.pivot.dialog;
19 
20 import java.io.IOException;
21 import java.util.logging.Level;
22 import java.util.logging.Logger;
23 import org.apache.pivot.serialization.SerializationException;
24 import org.apache.pivot.util.Resources;
25 import org.apache.pivot.wtk.Action;
26 import org.apache.pivot.wtk.ActivityIndicator;
27 import org.apache.pivot.wtk.Label;
28 import org.apache.pivot.wtk.Meter;
29 import org.apache.pivot.wtk.PushButton;
30 import org.apache.pivot.wtk.Sheet;
31 import org.apache.pivot.wtk.Window;
32 import org.apache.pivot.wtk.content.ButtonData;
33 import org.apache.pivot.wtkx.Bindable;
34 import org.apache.pivot.wtkx.WTKX;
35 import org.apache.pivot.wtkx.WTKXSerializer;
36 
41 public class ProgressSheet extends Sheet implements Bindable {
42 
43  private boolean canceled = false, closeable = true;
44  private Resources resources;
45 
46  @WTKX private PushButton progressButton;
47  @WTKX private ActivityIndicator activityIndicator;
48  @WTKX private Label caption;
49  @WTKX private Meter meter;
50 
51  private Action progressAction = new Action(true) {
52  @Override
53  public void perform() {
54  if(closeable) {
55  closeProgress();
56  } else {
57  canceled = true;
58  progressSheet.closeable = true;
59  progressSheet.caption.setText((String) progressSheet.resources.get("canceled"));
60  ((ButtonData) progressSheet.progressButton.getButtonData()).setText(
61  (String) progressSheet.resources.get("close"));
62  }
63  }
64  };
65 
66  public void initialize(Resources resources) {
67  progressButton.setAction(progressAction);
68  this.resources = resources;
69  }
70 
71  public boolean isCanceled() {
72  return canceled;
73  }
74 
75  public static void startProgress(String caption, boolean canCancel) {
76  progressSheet.canceled = false;
77  progressSheet.closeable = false;
78  progressSheet.meter.setVisible(false);
79  progressSheet.meter.setText(null);
80  progressSheet.meter.setPercentage(0);
81  progressSheet.progressAction.setEnabled(canCancel);
82  ((ButtonData) progressSheet.progressButton.getButtonData()).setText(
83  (String) progressSheet.resources.get("cancel"));
84  progressSheet.caption.setText(caption);
85  progressSheet.activityIndicator.setActive(true);
86  progressSheet.open(window);
87  window.setEnabled(false);
88  }
89 
90  public static void startMeasuredProgress(String caption, boolean canCancel) {
91  progressSheet.canceled = false;
92  progressSheet.closeable = false;
93  progressSheet.meter.setVisible(true);
94  progressSheet.meter.setText(null);
95  progressSheet.meter.setPercentage(0);
96  progressSheet.progressAction.setEnabled(canCancel);
97  ((ButtonData) progressSheet.progressButton.getButtonData()).setText(
98  (String) progressSheet.resources.get("cancel"));
99  progressSheet.caption.setText(caption);
100  progressSheet.activityIndicator.setActive(true);
101  progressSheet.open(window);
102  window.setEnabled(false);
103  }
104 
105  public static void nextProgress(String caption, String step, double inc) {
106  if(inc > 0.0) {
107  double percent = progressSheet.meter.getPercentage();
108  if(percent + inc > 1) {
109  progressSheet.meter.setPercentage(1);
110  } else {
111  progressSheet.meter.setPercentage(percent + inc);
112  }
113  }
114  if(step != null) progressSheet.meter.setText(step);
115  if(caption != null) progressSheet.caption.setText(caption);
116  }
117 
118  public static void nextProgress(String caption) {
119  if(caption != null) progressSheet.caption.setText(caption);
120  }
121 
122  public static void endProgress(String caption) {
123  progressSheet.closeable = true;
124  progressSheet.meter.setText(null);
125  progressSheet.progressAction.setEnabled(true);
126  ((ButtonData) progressSheet.progressButton.getButtonData()).setText(
127  (String) progressSheet.resources.get("close"));
128  if(caption != null) progressSheet.caption.setText(caption);
129  progressSheet.activityIndicator.setActive(false);
130  }
131 
132  public static void closeProgress() {
133  window.setEnabled(true);
134  progressSheet.close();
135  }
136 
137  public static ProgressSheet getInstance() {
138  return progressSheet;
139  }
140 
141  public static ProgressSheet getInstance(Window parent) {
142  window = parent;
143  try {
144  WTKXSerializer wtkxSerializer = new WTKXSerializer(new Resources(ProgressSheet.class.getName()));
145  progressSheet = (ProgressSheet) wtkxSerializer.readObject("org/turro/pivot/dialog/progress_sheet.wtkx");
146  wtkxSerializer.bind(progressSheet, ProgressSheet.class);
147  } catch (IOException ex) {
148  Logger.getLogger(ProgressSheet.class.getName()).log(Level.SEVERE, null, ex);
149  } catch (SerializationException ex) {
150  Logger.getLogger(ProgressSheet.class.getName()).log(Level.SEVERE, null, ex);
151  }
152  return progressSheet;
153  }
154 
155  private static ProgressSheet progressSheet;
156  private static Window window;
157 }
static void startMeasuredProgress(String caption, boolean canCancel)
static void nextProgress(String caption)
static ProgressSheet getInstance(Window parent)
static void endProgress(String caption)
static void nextProgress(String caption, String step, double inc)
static void startProgress(String caption, boolean canCancel)
void initialize(Resources resources)