001: /**
002: * LibreSource
003: * Copyright (C) 2004-2008 Artenum SARL / INRIA
004: * http://www.libresource.org - contact@artenum.com
005: *
006: * This file is part of the LibreSource software,
007: * which can be used and distributed under license conditions.
008: * The license conditions are provided in the LICENSE.TXT file
009: * at the root path of the packaging that enclose this file.
010: * More information can be found at
011: * - http://dev.libresource.org/home/license
012: *
013: * Initial authors :
014: *
015: * Guillaume Bort / INRIA
016: * Francois Charoy / Universite Nancy 2
017: * Julien Forest / Artenum
018: * Claude Godart / Universite Henry Poincare
019: * Florent Jouille / INRIA
020: * Sebastien Jourdain / INRIA / Artenum
021: * Yves Lerumeur / Artenum
022: * Pascal Molli / Universite Henry Poincare
023: * Gerald Oster / INRIA
024: * Mariarosa Penzi / Artenum
025: * Gerard Sookahet / Artenum
026: * Raphael Tani / INRIA
027: *
028: * Contributors :
029: *
030: * Stephane Bagnier / Artenum
031: * Amadou Dia / Artenum-IUP Blois
032: * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
033: */package org.libresource.so6.core.ui;
034:
035: import org.libresource.so6.core.StateMonitoring;
036: import org.libresource.so6.core.WsConnection;
037: import org.libresource.so6.core.client.WorkspaceListener;
038: import org.libresource.so6.core.engine.log.LogHandler;
039: import org.libresource.so6.core.engine.log.LogPrinter;
040: import org.libresource.so6.core.engine.log.LogUtils;
041: import org.libresource.so6.core.engine.log.monitoring.XMLLogHandler;
042: import org.libresource.so6.core.engine.log.monitoring.XMLMonitoringThread;
043: import org.libresource.so6.core.engine.log.monitoring.XMLProgressView;
044: import org.libresource.so6.core.ui.util.Wizard;
045: import org.libresource.so6.core.ui.util.WizardComponent;
046:
047: import java.awt.BorderLayout;
048: import java.awt.Color;
049: import java.awt.Toolkit;
050:
051: import java.util.logging.Handler;
052: import java.util.logging.LogRecord;
053: import java.util.logging.Logger;
054:
055: import javax.swing.JFrame;
056: import javax.swing.JOptionPane;
057: import javax.swing.JPanel;
058: import javax.swing.JScrollPane;
059: import javax.swing.JTextArea;
060:
061: /**
062: * @author smack
063: */
064: public class BasicProgressViewXML extends JPanel implements
065: WizardComponent, LogPrinter {
066: private XMLProgressView view;
067: private WsConnection wsc;
068: private String wsPath;
069:
070: //
071: protected JTextArea log = null;
072: protected JFrame logFrame = null;
073: private Wizard wizard;
074:
075: public BasicProgressViewXML(String wsPath) throws Exception {
076: super (new BorderLayout());
077: this .wsPath = wsPath;
078: wsc = new WsConnection(wsPath);
079:
080: final XMLLogHandler xmlLogHandler = new XMLLogHandler();
081:
082: //
083: log = new JTextArea();
084: log.setEditable(false);
085: logFrame = new JFrame("Execution log");
086: logFrame.getContentPane().add(new JScrollPane(log));
087: logFrame.setSize(400, 200);
088: logFrame.setLocation(((int) Toolkit.getDefaultToolkit()
089: .getScreenSize().getWidth() - logFrame.getWidth()) / 2,
090: (((int) Toolkit.getDefaultToolkit().getScreenSize()
091: .getHeight() - getHeight()) / 2) + 110);
092:
093: //
094: view = new XMLProgressView(xmlLogHandler);
095: add(view, BorderLayout.CENTER);
096:
097: // init output
098: LogUtils.removeAllHandlers(Logger.getLogger("ui.log"));
099: LogUtils.removeAllHandlers(StateMonitoring.getInstance()
100: .getXMLMonitoringLogger());
101:
102: XMLMonitoringThread monitoringThread = new XMLMonitoringThread(
103: xmlLogHandler);
104: monitoringThread.start();
105: }
106:
107: public void setStyle(Color back, Color forground) {
108: view.setStyle(back, forground);
109: }
110:
111: public void publish(LogRecord record) {
112: printMessage(record.getMessage());
113: }
114:
115: public void printMessage(String message) {
116: log.append(message + "\n");
117: log.setCaretPosition(log.getText().length());
118: }
119:
120: public void printError(Exception e) {
121: printMessage("###\n" + e.getMessage() + "\n###");
122:
123: StackTraceElement[] stack = e.getStackTrace();
124:
125: for (int i = 0; i < stack.length; i++) {
126: printMessage("# " + stack[i].getClassName() + ": line "
127: + stack[i].getLineNumber());
128: }
129: }
130:
131: public void initLog() {
132: Handler[] handler = Logger.getLogger("ui.log").getHandlers();
133:
134: for (int i = 0; i < handler.length; i++) {
135: Logger.getLogger("ui.log").removeHandler(handler[i]);
136: }
137:
138: //
139: Logger.getLogger("ui.log").setUseParentHandlers(false);
140: Logger.getLogger("ui.log").addHandler(new LogHandler(this ));
141: }
142:
143: public void showLog() {
144: logFrame.setVisible(true);
145: }
146:
147: public void hideLog() {
148: logFrame.setVisible(false);
149: }
150:
151: public boolean isLogShow() {
152: return logFrame.isVisible();
153: }
154:
155: public void update() {
156: initLog();
157:
158: try {
159: wsc.getClient();
160:
161: try {
162: wsc.update();
163:
164: if (wsc.getClient() instanceof WorkspaceListener) {
165: ((WorkspaceListener) wsc.getClient())
166: .notifyQueue(wsc.getNs());
167: }
168: } catch (Exception e) {
169: String message = "Error: while trying to update local workspace.";
170: printMessage(message);
171: printError(e);
172: e.printStackTrace();
173: JOptionPane.showMessageDialog(this , message);
174: }
175: } catch (Exception e) {
176: String message = "Error: Unable to instantiate client for synchronization";
177: printMessage(message);
178: printError(e);
179: e.printStackTrace();
180: JOptionPane.showMessageDialog(this , message);
181: }
182:
183: printMessage("Update finished");
184:
185: // Print report
186: if (wsc.getReport().length() > 0) {
187: printMessage("\n*** Report ***");
188: printMessage(wsc.getReport());
189: }
190: }
191:
192: public void commit(String comment) {
193: initLog();
194:
195: try {
196: wsc.getClient();
197:
198: try {
199: wsc.commit(comment);
200:
201: if (wsc.getClient() instanceof WorkspaceListener) {
202: ((WorkspaceListener) wsc.getClient())
203: .notifyQueue(wsc.getNs());
204: }
205: } catch (Exception e) {
206: String message = "Error: while trying to commit.";
207: printMessage(message);
208: printError(e);
209: JOptionPane.showMessageDialog(this , message);
210: e.printStackTrace();
211: }
212: } catch (Exception e) {
213: String message = "Error: Unable to instantiate client for synchronization";
214: printMessage(message);
215: printError(e);
216: JOptionPane.showMessageDialog(this , message);
217: e.printStackTrace();
218: }
219:
220: printMessage("Commit finished");
221:
222: // Print report
223: if (wsc.getReport().length() > 0) {
224: printMessage("\n*** Report ***");
225: printMessage(wsc.getReport());
226: }
227: }
228:
229: public String getReport() {
230: return wsc.getReport();
231: }
232:
233: public void setWizard(Wizard wizard) {
234: this .wizard = wizard;
235: }
236:
237: public Wizard getWizard() {
238: return wizard;
239: }
240: }
|