View Javadoc

1   /*
2    *  XNap - A P2P framework and client.
3    *
4    *  See the file AUTHORS for copyright information.
5    *
6    *  This program is free software; you can redistribute it and/or modify
7    *  it under the terms of the GNU General Public License as published by
8    *  the Free Software Foundation.
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 General Public License for more details.
14   *
15   *  You should have received a copy of the GNU General Public License
16   *  along with this program; if not, write to the Free Software
17   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
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      //--- Constant(s) ---
43  
44      //--- Data field(s) ---
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      //--- Constructor(s) ---
56  
57      publicPackageInstallerDialog(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  		// info panel
71  		getPanel().setBorder
72  			(GUIHelper.createDefaultBorder(XNap.tr("Download")));
73  
74  		// status
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      //--- Method(s) ---
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     // --- Inner Class(es) ---
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 }