1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.xnap.gui.pkg;
21
22 import java.awt.event.ActionEvent;
23
24 import javax.swing.AbstractAction;
25 import javax.swing.Action;
26 import javax.swing.JCheckBox;
27 import javax.swing.JProgressBar;
28 import javax.swing.SwingUtilities;
29
30 import org.xnap.XNap;
31 import org.xnap.gui.component.ProgressDialog;
32 import org.xnap.gui.component.XNapButton;
33 import org.xnap.gui.util.GUIHelper;
34 import org.xnap.gui.util.GridBagHelper;
35 import org.xnap.pkg.PackageInfo;
36 import org.xnap.pkg.PackageInstaller;
37 import org.xnap.pkg.PackageInstallerListener;
38
39 public class PackageInstallerDialog extends ProgressDialog
40 implements PackageInstallerListener {
41
42
43
44
45
46 private PackageInstaller installer;
47
48 private JProgressBar totalProgressBar;
49 private JCheckBox closeWhenDoneCheckBox;
50
51 private StartAction startAction = new StartAction();
52
53 private boolean running = false;
54
55
56
57 public PackageInstallerDialog(PackageInfo[] packages)/package-summary.html">ong> PackageInstallerDialog(PackageInfo[] packages)
58 {
59 super(BUTTON_CANCEL | BUTTON_CLOSE, XNap.tr("Package Download"));
60 setModal(false);
61 setCancelEnabled(false);
62
63 initialize();
64
65 this.installPackageInstaller(packages, this)/package-summary.html">er = new PackageInstaller(packages, this);
66 }
67
68 public void initialize()
69 {
70
71 getPanel().setBorder
72 (GUIHelper.createDefaultBorder(XNap.tr("Download")));
73
74
75 GridBagHelper.addLabel(getPanel(), XNap.tr("Total Progress"), true);
76 totalProgressBar = new JProgressBar();
77 GridBagHelper.add(getPanel(), totalProgressBar);
78
79 closeWhenDoneCheckBox = new JCheckBox(XNap.tr("Automatically close dialog when finished"), true);
80 GridBagHelper.add(getPanel(), closeWhenDoneCheckBox);
81
82 getButtonPanel().add(new XNapButton(startAction), 0);
83
84 pack();
85 }
86
87
88
89 public void close()
90 {
91 if (!running) {
92 dispose();
93 }
94 }
95
96 public void done()
97 {
98 Runnable r = new Runnable()
99 {
100 public void run()
101 {
102 running = false;
103
104 if (installer.isSuccessful()
105 && closeWhenDoneCheckBox.isSelected()) {
106 close();
107 }
108
109 startAction.setEnabled(!installer.isSuccessful());
110 setCancelEnabled(false);
111 getCloseAction().setEnabled(true);
112 }
113 };
114 SwingUtilities.invokeLater(r);
115 }
116
117 public PackageInstaller getInstaller()
118 {
119 return installer;
120 }
121
122 public boolean isRunning()
123 {
124 return running;
125 }
126
127 public void setTotalPercent(final double percent)
128 {
129 Runnable r = new Runnable()
130 {
131 public void run()
132 {
133 int value = (int)((totalProgressBar.getMaximum()
134 - totalProgressBar.getMinimum())
135 * percent);
136 totalProgressBar.setValue(value);
137 }
138 };
139 SwingUtilities.invokeLater(r);
140 }
141
142 public void setTotalMinimum(final int max)
143 {
144 Runnable r = new Runnable()
145 {
146 public void run()
147 {
148 totalProgressBar.setMinimum(max);
149 }
150 };
151 SwingUtilities.invokeLater(r);
152 }
153
154 public void setTotalMaximum(final int max)
155 {
156 Runnable r = new Runnable()
157 {
158 public void run()
159 {
160 totalProgressBar.setMaximum(max);
161 }
162 };
163 SwingUtilities.invokeLater(r);
164 }
165
166 public void setTotalValue(final int value)
167 {
168 Runnable r = new Runnable()
169 {
170 public void run()
171 {
172 totalProgressBar.setValue(value);
173 }
174 };
175 SwingUtilities.invokeLater(r);
176 }
177
178
179
180 private class StartAction extends AbstractAction {
181
182 public StartAction()
183 {
184 putValue(Action.NAME, XNap.tr("Start"));
185 putValue(Action.SHORT_DESCRIPTION,
186 XNap.tr("Downloads the plugin."));
187 }
188
189 public void actionPerformed(ActionEvent event)
190 {
191 startAction.setEnabled(false);
192 getCloseAction().setEnabled(false);
193 setCancelEnabled(true);
194
195 installer.start();
196 running = true;
197 }
198
199 }
200
201 }