View Javadoc

1   /*
2    *   Magic-Project is a turn based strategy simulator
3    *   Copyright (C) 2003-2007 Fabrice Daugan
4    *
5    *   This program is free software; you can redistribute it and/or modify it 
6    * under the terms of the GNU General Public License as published by the Free 
7    * Software Foundation; either version 2 of the License, or (at your option) any
8    * later version.
9    *
10   *   This program is distributed in the hope that it will be useful, but WITHOUT 
11   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12   * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more 
13   * details.
14   *
15   *   You should have received a copy of the GNU General Public License along  
16   * with this program; if not, write to the Free Software Foundation, Inc., 
17   * 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18   */
19  package net.sf.magicproject.ui.component;
20  
21  import java.awt.BorderLayout;
22  import java.awt.event.ActionEvent;
23  import java.awt.event.ActionListener;
24  import java.awt.event.WindowEvent;
25  
26  import javax.swing.JButton;
27  import javax.swing.JDialog;
28  import javax.swing.JProgressBar;
29  import javax.swing.JScrollPane;
30  import javax.swing.JTextArea;
31  import javax.swing.ScrollPaneConstants;
32  import javax.swing.border.EtchedBorder;
33  
34  import net.sf.magicproject.network.NetworkActor;
35  import net.sf.magicproject.token.IdConst;
36  import net.sf.magicproject.tools.Picture;
37  import net.sf.magicproject.ui.MagicUIComponents;
38  import net.sf.magicproject.ui.ToolKit;
39  import net.sf.magicproject.ui.i18n.LanguageManager;
40  
41  /***
42   * MLoader.java Created on 7 d�c. 2003
43   * 
44   * @author Fabrice Daugan
45   * @since 0.53
46   * @since 0.86 JDialog icon updated
47   * @since 'cancel' button is defined as default and cancel button, and window
48   *        closing is handled.
49   */
50  public class LoaderConsole extends JDialog {
51  
52  	private static LoaderConsole instance;
53  
54  	/***
55  	 * Creates a new instance of MLoader <br>
56  	 */
57  	private LoaderConsole() {
58  		super(MagicUIComponents.magicForm, false);
59  		loadingText = new JTextArea();
60  		loadingText.setEditable(false);
61  		loadingText.setLineWrap(true);
62  		loadingText.setWrapStyleWord(true);
63  		loadingText.setBorder(new EtchedBorder());
64  		JButton cancelLoadBtn = new JButton(LanguageManager.getString("cancel"));
65  		JScrollPane sPane = new JScrollPane(loadingText,
66  				ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,
67  				ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
68  		getContentPane().add(cancelLoadBtn, BorderLayout.SOUTH);
69  		getRootPane().setDefaultButton(cancelLoadBtn);
70  		loadingPorgressbar = new JProgressBar(0, 100);
71  		getContentPane().add(loadingPorgressbar, BorderLayout.NORTH);
72  		getContentPane().add(sPane, BorderLayout.CENTER);
73  
74  		cancelLoadBtn.addActionListener(new ActionListener() {
75  
76  			public void actionPerformed(ActionEvent evt) {
77  				NetworkActor.cancelling = true;
78  				setVisible(false);
79  			}
80  		});
81  		ToolKit.addCancelByEscapeKey(this, cancelLoadBtn);
82  	}
83  
84  	/***
85  	 * hide the current panel and show the loader form
86  	 */
87  	public static void launchLoader() {
88  		instance = new LoaderConsole();
89  		instance.loadingText.setText("");
90  		instance.setLocation(
91  				(instance.getToolkit().getScreenSize().width - 450) / 2, (instance
92  						.getToolkit().getScreenSize().height - 220) / 2);
93  		instance.setSize(450, 220);
94  		loadingPorgressbar.setValue(0);
95  		instance.setVisible(true);
96  	}
97  
98  	/***
99  	 * hide the current panel and show the loader form
100 	 */
101 	public static void endTask() {
102 		if (instance != null) {
103 			instance.setVisible(false);
104 		}
105 	}
106 
107 	@Override
108 	public void setVisible(boolean visible) {
109 		try {
110 			if (visible) {
111 				MagicUIComponents.magicForm.setIconImage(Picture
112 						.loadImage(IdConst.IMAGES_DIR + "newlan.gif"));
113 			} else {
114 				MagicUIComponents.magicForm.setIconImage(Picture
115 						.loadImage(IdConst.IMAGES_DIR + "mp.gif"));
116 				instance = null;
117 			}
118 		} catch (Exception e) {
119 			// IGNORING
120 		}
121 		super.setVisible(visible);
122 	}
123 
124 	@Override
125 	protected void processWindowEvent(WindowEvent e) {
126 		super.processWindowEvent(e);
127 
128 		if (e.getID() == WindowEvent.WINDOW_CLOSING) {
129 			NetworkActor.cancelling = true;
130 		}
131 	}
132 
133 	/***
134 	 * Begin a new task.
135 	 * 
136 	 * @param taskName
137 	 *          the task name.
138 	 */
139 	public static void beginTask(String taskName) {
140 		if (instance != null) {
141 			instance.loadingText.append(taskName + "\n");
142 		}
143 	}
144 
145 	/***
146 	 * Begin a new task.
147 	 * 
148 	 * @param taskName
149 	 *          the task name.
150 	 * @param percent
151 	 *          the percent of task.
152 	 */
153 	public static void beginTask(String taskName, int percent) {
154 		if (instance != null) {
155 			beginTask(taskName);
156 			setTaskPercent(percent);
157 		}
158 	}
159 
160 	/***
161 	 * Update the percent of current task.
162 	 * 
163 	 * @param percent
164 	 *          the percent of task.
165 	 */
166 	public static void setTaskPercent(int percent) {
167 		loadingPorgressbar.setValue(percent);
168 	}
169 
170 	/***
171 	 * The progressbar
172 	 */
173 	private static JProgressBar loadingPorgressbar;
174 
175 	/***
176 	 * The current displayed text
177 	 */
178 	private JTextArea loadingText;
179 
180 }