View Javadoc
1   /*
2    *  Copyright 2006 the GLXDesktop Project Team, all rights reserved.
3    *  
4    *  This file is part of GLXDesktop Project.
5    *  GLXDesktop Project is free software; you can redistribute it and/or modify
6    *  it under the terms of the GNU General Public License as published by
7    *  the Free Software Foundation; either version 2 of the License.
8    *  3ddesktop-configurator is distributed in the hope that it will be useful,
9    *  but WITHOUT ANY WARRANTY; without even the implied warranty of
10   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11   *  GNU General Public License for more details.
12   *  You should have received a copy of the GNU General Public License
13   *  along with GLXDesktop Project; if not, write to the Free Software
14   *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
15   */
16  
17  package net.sf.glxdesktop.gui.runcmd;
18  
19  import java.io.BufferedReader;
20  import java.io.IOException;
21  import java.io.InputStream;
22  import java.io.InputStreamReader;
23  
24  import net.sf.glxdesktop.gui.Messages;
25  import net.sf.glxdesktop.gui.glade.LoadGladeException;
26  
27  import org.gnu.glib.CustomEvents;
28  import org.gnu.glib.Fireable;
29  import org.gnu.glib.Timer;
30  import org.gnu.gtk.TextBuffer;
31  import org.gnu.gtk.event.ButtonEvent;
32  import org.gnu.gtk.event.ButtonListener;
33  import org.gnu.gtk.event.LifeCycleEvent;
34  import org.gnu.gtk.event.LifeCycleListener;
35  
36  /***
37   * This class open a progress Gtk window and execute a system command, 
38   * The window retrive informations like command inputStream and errorInputStream.
39   *  
40   * @author Alphonse Van Assche
41   * 
42   */
43  public abstract class RunCmdProgressImpl extends RunCmdProgress implements
44  		Fireable {
45  
46  	private Timer timer = null;
47  
48  	private static final String DEFAULT_TITLE = Messages
49  			.getString("RunCmdProgressImpl.default_title"); //$NON-NLS-1$
50  
51  	private static final String DEFAULT_MESSAGE = Messages
52  			.getString("RunCmdProgressImpl.default_message"); //$NON-NLS-1$
53  
54  	private boolean canClose = false;
55  
56  	private String inputLines;
57  
58  	private String errorLines;
59  
60  	final String command;
61  
62  	public abstract void process();
63  
64  	public RunCmdProgressImpl(String commandToRun, String title, String message) {
65  		command = commandToRun;
66  		try {
67  			initialize();
68  		} catch (LoadGladeException e) {
69  			System.err
70  					.println(Messages
71  							.getString("RunCmdProgressImpl.load_glade_exeception_message") + e.getMessage()); //$NON-NLS-1$
72  			System.exit(1);
73  		}
74  		window.setTitle(title);
75  		labelMessage.setText(message);
76  		progressBar.setText(command);
77  		setListeners();
78  	}
79  
80  	public RunCmdProgressImpl(String commandToRun) {
81  		this(commandToRun, DEFAULT_TITLE, DEFAULT_MESSAGE);
82  	}
83  
84  	public void execute() {
85  		timer = new Timer(100, this);
86  		timer.start();
87  		Runnable run = new Runnable() {
88  			public void run() {
89  				try {
90  
91  					Process child = Runtime.getRuntime().exec(command);
92  					InputStream errorIs = child.getErrorStream();
93  					InputStreamReader errorIsReader = new InputStreamReader(
94  							errorIs);
95  					BufferedReader errorReader = new BufferedReader(
96  							errorIsReader);
97  					InputStream inputIs = child.getInputStream();
98  					InputStreamReader inputIsReader = new InputStreamReader(
99  							inputIs);
100 					BufferedReader inputReader = new BufferedReader(
101 							inputIsReader);
102 					String errorLine;
103 					errorLines = ""; //$NON-NLS-1$
104 					while ((errorLine = errorReader.readLine()) != null) {
105 						errorLines += errorLine + "\n"; //$NON-NLS-1$
106 					}
107 					String inputLine;
108 					inputLines = ""; //$NON-NLS-1$
109 					while ((inputLine = inputReader.readLine()) != null) {
110 						inputLines += inputLine + "\n"; //$NON-NLS-1$
111 					}
112 					final String inputTxt = inputLines;
113 					final String errorTxt = errorLines;
114 					Runnable runnable = new Runnable() {
115 						public void run() {
116 							canClose = true;
117 							timer.stop();
118 							progressBar.setFraction(100.0);
119 							if (!errorTxt.trim().equals("")) { //$NON-NLS-1$
120 								textViewCmdError
121 										.setBuffer(setTextBuffer(errorTxt));
122 								expanderCmdError.show();
123 								expanderCmdError.setExpanded(true);
124 								progressBar
125 										.setText(Messages
126 												.getString("RunCmdProgressImpl.progressbar_error_msg") + command); //$NON-NLS-1$
127 							} else {
128 								closeWindow();
129 							}
130 							if (!inputTxt.trim().equals("")) { //$NON-NLS-1$
131 								textViewCmdOutput
132 										.setBuffer(setTextBuffer(inputTxt));
133 								expanderCmdOutput.show();
134 							}
135 							process();
136 						}
137 					};
138 					CustomEvents.addEvent(runnable);
139 				} catch (IOException e) {
140 					e.printStackTrace();
141 				}
142 			}
143 		};
144 		Thread thread = new Thread(run);
145 		thread.start();
146 	}
147 
148 	public String getInputString() {
149 		return inputLines;
150 	}
151 
152 	public String getErrorString() {
153 		return errorLines;
154 	}
155 
156 	private void setListeners() {
157 
158 		window.addListener(new LifeCycleListener() {
159 			public void lifeCycleEvent(LifeCycleEvent event) {
160 			}
161 
162 			public boolean lifeCycleQuery(LifeCycleEvent event) {
163 				if (event.isOfType(LifeCycleEvent.Type.DESTROY)
164 						|| event.isOfType(LifeCycleEvent.Type.DELETE)) {
165 					closeWindow();
166 				}
167 				return true;
168 			}
169 		});
170 
171 		buttonClose.addListener(new ButtonListener() {
172 			public void buttonEvent(ButtonEvent event) {
173 				if (event.isOfType(ButtonEvent.Type.CLICK)) {
174 					closeWindow();
175 				}
176 			}
177 		});
178 
179 	}
180 
181 	private void closeWindow() {
182 		if (canClose) {
183 			window.destroy();
184 		} else {
185 			window.iconify();
186 		}
187 	}
188 
189 	private TextBuffer setTextBuffer(String txt) {
190 		TextBuffer buffer = new TextBuffer();
191 		buffer.setText(txt);
192 		return buffer;
193 	}
194 
195 	public boolean fire() {
196 		double percent = progressBar.getFraction();
197 		if (percent >= .99)
198 			percent = 0.0;
199 		else {
200 			percent += 0.05;
201 			if (percent >= 1.0)
202 				percent = .999;
203 		}
204 		progressBar.setFraction(percent);
205 		return true;
206 	}
207 }