Index: lutinutil/src/java/org/codelutin/log/LutinLog.java diff -u lutinutil/src/java/org/codelutin/log/LutinLog.java:1.1 lutinutil/src/java/org/codelutin/log/LutinLog.java:1.2 --- lutinutil/src/java/org/codelutin/log/LutinLog.java:1.1 Sat Sep 9 02:44:35 2006 +++ lutinutil/src/java/org/codelutin/log/LutinLog.java Wed Sep 13 13:26:42 2006 @@ -23,15 +23,27 @@ * Created: 9 sept. 06 03:53:09 * * @author poussin - * @version $Revision: 1.1 $ + * @version $Revision: 1.2 $ * - * Last update: $Date: 2006/09/09 02:44:35 $ + * Last update: $Date: 2006/09/13 13:26:42 $ * by : $Author: bpoussin $ */ package org.codelutin.log; +import java.awt.Component; +import java.util.Timer; +import java.util.TimerTask; + +import javax.swing.Box; +import javax.swing.JFrame; +import javax.swing.JLabel; +import javax.swing.JProgressBar; +import javax.swing.SwingConstants; +import javax.swing.SwingUtilities; + import org.apache.commons.logging.Log; +import org.codelutin.util.CategorisedListenerSet; /** @@ -41,75 +53,257 @@ public class LutinLog implements Log { - protected LutinLogFactory factory = null; - protected Log parent = null; + private Timer timer = new Timer("LutinLogTimer", true); + private LutinLogFactory factory = null; + private Log parent = null; + private long activeProgressMonitorTime; + private ProgressState progressState = ProgressState.end; + private long timeout = 0; + private int min; + private int max; + private int value; + private ProgressMonitorFrame progressMonitorFrame; + private TimerTask activeProgressMonitorTask = new TimerTask() { + @Override + public void run() { + if (SwingUtilities.isEventDispatchThread()) { + Component frame = getProgressMonitorFrame(); + frame.setVisible(true); + } else { + SwingUtilities.invokeLater(this); + } + } + }; + + private TimerTask timeoutTask = new TimerTask() { + @Override + public void run() { + end(); + } + }; protected LutinLog(LutinLogFactory factory, Log parent) { this.factory = factory; this.parent = parent; - } - - /* (non-Javadoc) - * @see org.apache.commons.logging.Log#debug(java.lang.Object) - */ - public void debug(Object arg0) { - parent.debug(arg0); + activeProgressMonitorTime = factory.getDefaultActiveProgressMonitorTime(); + timeout = factory.getDefaultTimeout(); + + // Force progressMonitorFrame instanciation to have good message + // displayed when frame is showing + getProgressMonitorFrame(); } /* (non-Javadoc) - * @see org.apache.commons.logging.Log#debug(java.lang.Object, java.lang.Throwable) + * @see java.lang.Object#toString() */ - public void debug(Object arg0, Throwable arg1) { - parent.debug(arg0, arg1); + @Override + public String toString() { + String result = "LutinLog min: " + getMin() + " value:" + getValue() + " max:" + getMax() + " timeout:" + getTimeout(); + return result; + } + + /////////////////////////////////////////////////////////////////////////// + // + // L I S T E N E R + // + /////////////////////////////////////////////////////////////////////////// + + public void addLutinLogListener(LutinLogListener l) { + factory.addLutinLogListener(this, l); } - /* (non-Javadoc) - * @see org.apache.commons.logging.Log#error(java.lang.Object) + public void removeLutinLogListener(LutinLogListener l) { + factory.removeLutinLogListener(this, l); + } + + /////////////////////////////////////////////////////////////////////////// + // + // F I R E E V E N T + // + /////////////////////////////////////////////////////////////////////////// + + protected void fireLogEvent(LutinLogEvent.LogType type, Object msg, Throwable eee) { + LutinLogEvent e = new LutinLogEvent(this, type, String.valueOf(msg), eee); + try { + factory.listeners.fire(this, "logEvent", e); + } catch (Exception eee1) { + if (parent.isDebugEnabled()) { + parent.debug("Can't fire event", eee1); + } + } + } + + protected void fireProgressEvent(LutinProgressEvent.ProgressType type) { + LutinProgressEvent e = new LutinProgressEvent(this, type); + try { + factory.listeners.fire(this, "progressEvent", e); + } catch (Exception eee1) { + if (parent.isDebugEnabled()) { + parent.debug("Can't fire event", eee1); + } + } + } + + /////////////////////////////////////////////////////////////////////////// + // + // P R O G R E S S + // + /////////////////////////////////////////////////////////////////////////// + + /** + * apres un start le temps avant de mettre le progress en marche (<=0 pour jamais) */ - public void error(Object arg0) { - parent.error(arg0); + public void setActivateProgressMonitorTime(long time) { + activeProgressMonitorTime = time; } - - /* (non-Javadoc) - * @see org.apache.commons.logging.Log#error(java.lang.Object, java.lang.Throwable) + + /** + * une tache indique son debut */ - public void error(Object arg0, Throwable arg1) { - parent.error(arg0, arg1); + public void start(){ + progressState = ProgressState.start; + value = -1; + + if (activeProgressMonitorTime > 0) { + timer.schedule(activeProgressMonitorTask, activeProgressMonitorTime); + } + if (timeout > 0) { + timer.schedule(timeoutTask, timeout); + } + fireProgressEvent(LutinProgressEvent.ProgressType.start); + } + /** + * @return + */ + protected Component getProgressMonitorFrame() { + if (progressMonitorFrame == null) { + progressMonitorFrame = new ProgressMonitorFrame(this, null); + } + return progressMonitorFrame; + } + + /** + * si on a demande a ce que la tache soit arretee + * @return + */ + public boolean isAskStopTask() { + boolean result = progressState == ProgressState.stopAsked; + return result; } - - /* (non-Javadoc) - * @see org.apache.commons.logging.Log#fatal(java.lang.Object) + /** + * demande ou annule la demande de fin de tache + * @param v + */ + public void setAskStopTask(boolean v) { + if (progressState != ProgressState.end) { + if (v) { + progressState = ProgressState.stopAsked; + fireProgressEvent(LutinProgressEvent.ProgressType.askStop); + } else { + progressState = ProgressState.start; + fireProgressEvent(LutinProgressEvent.ProgressType.cancelAskStop); + } + } + } + + /** + * indique la fin complete d'une tache */ - public void fatal(Object arg0) { - parent.fatal(arg0); + public void end() { + progressState = ProgressState.end; + activeProgressMonitorTask.cancel(); + timeoutTask.cancel(); + if (progressMonitorFrame != null) { + progressMonitorFrame.setVisible(false); + } + fireProgressEvent(LutinProgressEvent.ProgressType.end); } - + + /** + * donne le timeout particulier pour une tache + * + * @return + */ + public long getTimeout() { + return timeout; + } + /** + * modifie le temps specifiquement pour une tache. A la fin du time out + * cela n'arrete pas la tâche mais arrete simplement de la suivre + * @param time + */ + public void setTimeout(long time) { + timeout = time; + } + public int getMin() { + return min; + } + /** + * une tache indique le min que peut prendre value + * @param value + */ + public void setMin(int value) { + min = value; + fireProgressEvent(LutinProgressEvent.ProgressType.min); + } + public int getMax() { + return max; + } + /** + * une tache indique le max que peut prendre value + * @param value + */ + public void setMax(int value) { + max = value; + fireProgressEvent(LutinProgressEvent.ProgressType.max); + } + public int getValue() { + return value; + } + /** + * la valeur courante pour la tache, -1 indique l'indetermination de la valeur + * @param value + */ + public void setValue(int value) { + this.value = value; + fireProgressEvent(LutinProgressEvent.ProgressType.value); + } + + /////////////////////////////////////////////////////////////////////////// + // + // L O G + // + /////////////////////////////////////////////////////////////////////////// + /* (non-Javadoc) - * @see org.apache.commons.logging.Log#fatal(java.lang.Object, java.lang.Throwable) + * @see org.apache.commons.logging.Log#isTraceEnabled() */ - public void fatal(Object arg0, Throwable arg1) { - parent.fatal(arg0, arg1); + public boolean isTraceEnabled() { + boolean result = parent.isTraceEnabled(); + return result; } /* (non-Javadoc) - * @see org.apache.commons.logging.Log#info(java.lang.Object) + * @see org.apache.commons.logging.Log#isDebugEnabled() */ - public void info(Object arg0) { - parent.info(arg0); + public boolean isDebugEnabled() { + boolean result = parent.isDebugEnabled(); + return result; } /* (non-Javadoc) - * @see org.apache.commons.logging.Log#info(java.lang.Object, java.lang.Throwable) + * @see org.apache.commons.logging.Log#isInfoEnabled() */ - public void info(Object arg0, Throwable arg1) { - parent.info(arg0, arg1); + public boolean isInfoEnabled() { + boolean result = parent.isInfoEnabled(); + return result; } /* (non-Javadoc) - * @see org.apache.commons.logging.Log#isDebugEnabled() + * @see org.apache.commons.logging.Log#isWarnEnabled() */ - public boolean isDebugEnabled() { - boolean result = parent.isDebugEnabled(); + public boolean isWarnEnabled() { + boolean result = parent.isWarnEnabled(); return result; } @@ -129,62 +323,116 @@ return result; } + public void user(Object msg) { + if(isInfoEnabled()) { + parent.info(msg); + } + fireLogEvent(LutinLogEvent.LogType.user, msg, null); + } + + public void user(Object msg, Throwable eee) { + if(isInfoEnabled()) { + parent.info(msg, eee); + } + fireLogEvent(LutinLogEvent.LogType.user, msg, eee); + } + /* (non-Javadoc) - * @see org.apache.commons.logging.Log#isInfoEnabled() + * @see org.apache.commons.logging.Log#trace(java.lang.Object) */ - public boolean isInfoEnabled() { - boolean result = parent.isInfoEnabled(); - return result; + public void trace(Object msg) { + parent.trace(msg); + fireLogEvent(LutinLogEvent.LogType.trace, msg, null); } /* (non-Javadoc) - * @see org.apache.commons.logging.Log#isTraceEnabled() + * @see org.apache.commons.logging.Log#trace(java.lang.Object, java.lang.Throwable) */ - public boolean isTraceEnabled() { - boolean result = parent.isTraceEnabled(); - return result; + public void trace(Object msg, Throwable eee) { + parent.trace(msg, eee); + fireLogEvent(LutinLogEvent.LogType.trace, msg, eee); } /* (non-Javadoc) - * @see org.apache.commons.logging.Log#isWarnEnabled() + * @see org.apache.commons.logging.Log#debug(java.lang.Object) */ - public boolean isWarnEnabled() { - boolean result = parent.isWarnEnabled(); - return result; + public void debug(Object msg) { + parent.debug(msg); + fireLogEvent(LutinLogEvent.LogType.debug, msg, null); } /* (non-Javadoc) - * @see org.apache.commons.logging.Log#trace(java.lang.Object) + * @see org.apache.commons.logging.Log#debug(java.lang.Object, java.lang.Throwable) */ - public void trace(Object arg0) { - parent.trace(arg0); + public void debug(Object msg, Throwable eee) { + parent.debug(msg, eee); + fireLogEvent(LutinLogEvent.LogType.debug, msg, eee); } /* (non-Javadoc) - * @see org.apache.commons.logging.Log#trace(java.lang.Object, java.lang.Throwable) + * @see org.apache.commons.logging.Log#info(java.lang.Object) */ - public void trace(Object arg0, Throwable arg1) { - parent.trace(arg0, arg1); + public void info(Object msg) { + parent.info(msg); + fireLogEvent(LutinLogEvent.LogType.info, msg, null); + } + + /* (non-Javadoc) + * @see org.apache.commons.logging.Log#info(java.lang.Object, java.lang.Throwable) + */ + public void info(Object msg, Throwable eee) { + parent.info(msg, eee); + fireLogEvent(LutinLogEvent.LogType.info, msg, eee); } /* (non-Javadoc) * @see org.apache.commons.logging.Log#warn(java.lang.Object) */ - public void warn(Object arg0) { - parent.warn(arg0); + public void warn(Object msg) { + parent.warn(msg); + fireLogEvent(LutinLogEvent.LogType.warn, msg, null); } /* (non-Javadoc) * @see org.apache.commons.logging.Log#warn(java.lang.Object, java.lang.Throwable) */ - public void warn(Object arg0, Throwable arg1) { - parent.warn(arg0, arg1); + public void warn(Object msg, Throwable eee) { + parent.warn(msg, eee); + fireLogEvent(LutinLogEvent.LogType.warn, msg, eee); } - protected void fireEvent(LutinLogEvent e) { - + /* (non-Javadoc) + * @see org.apache.commons.logging.Log#error(java.lang.Object) + */ + public void error(Object msg) { + parent.error(msg); + fireLogEvent(LutinLogEvent.LogType.error, msg, null); } - + + /* (non-Javadoc) + * @see org.apache.commons.logging.Log#error(java.lang.Object, java.lang.Throwable) + */ + public void error(Object msg, Throwable eee) { + parent.error(msg, eee); + fireLogEvent(LutinLogEvent.LogType.error, msg, eee); + } + + /* (non-Javadoc) + * @see org.apache.commons.logging.Log#fatal(java.lang.Object) + */ + public void fatal(Object msg) { + parent.fatal(msg); + fireLogEvent(LutinLogEvent.LogType.fatal, msg, null); + } + + /* (non-Javadoc) + * @see org.apache.commons.logging.Log#fatal(java.lang.Object, java.lang.Throwable) + */ + public void fatal(Object msg, Throwable eee) { + parent.fatal(msg, eee); + fireLogEvent(LutinLogEvent.LogType.fatal, msg, eee); + } + } Index: lutinutil/src/java/org/codelutin/log/LutinLogEvent.java diff -u lutinutil/src/java/org/codelutin/log/LutinLogEvent.java:1.1 lutinutil/src/java/org/codelutin/log/LutinLogEvent.java:1.2 --- lutinutil/src/java/org/codelutin/log/LutinLogEvent.java:1.1 Sat Sep 9 02:44:35 2006 +++ lutinutil/src/java/org/codelutin/log/LutinLogEvent.java Wed Sep 13 13:26:42 2006 @@ -23,9 +23,9 @@ * Created: 9 sept. 06 04:01:26 * * @author poussin - * @version $Revision: 1.1 $ + * @version $Revision: 1.2 $ * - * Last update: $Date: 2006/09/09 02:44:35 $ + * Last update: $Date: 2006/09/13 13:26:42 $ * by : $Author: bpoussin $ */ @@ -43,23 +43,23 @@ /** */ private static final long serialVersionUID = 1L; - private Object type; + private LogType logType; private String msg; - private Object eee; + private Throwable eee; + private long time; - static enum ChangeType {log, min, max, value, askStop, start, end} - static enum LogType {user, trace, debug, info, warn, error, fatal} + static public enum LogType {user, trace, debug, info, warn, error, fatal} /** * @param source */ - public LutinLogEvent(Object source, ChangeType type, LogType logType, String msg, Throwable eee) { + public LutinLogEvent(Object source, LogType logType, String msg, Throwable eee) { super(source); - this.type = type; this.logType = logType; this.msg = msg; this.eee = eee; + this.time = System.currentTimeMillis(); } /* (non-Javadoc) @@ -70,13 +70,6 @@ } /** - * @return the type - */ - public Object getType() { - return this.type; - } - - /** * @return the logType */ public LogType getLogType() { @@ -93,10 +86,16 @@ /** * @return the eee */ - public Object getThrowabl() { + public Throwable getThrowable() { return this.eee; } + /** + * @return the time + */ + public long getTime() { + return this.time; + } } Index: lutinutil/src/java/org/codelutin/log/LutinLogFactory.java diff -u lutinutil/src/java/org/codelutin/log/LutinLogFactory.java:1.1 lutinutil/src/java/org/codelutin/log/LutinLogFactory.java:1.2 --- lutinutil/src/java/org/codelutin/log/LutinLogFactory.java:1.1 Sat Sep 9 02:44:35 2006 +++ lutinutil/src/java/org/codelutin/log/LutinLogFactory.java Wed Sep 13 13:26:42 2006 @@ -23,9 +23,9 @@ * Created: 9 sept. 06 03:45:29 * * @author poussin - * @version $Revision: 1.1 $ + * @version $Revision: 1.2 $ * - * Last update: $Date: 2006/09/09 02:44:35 $ + * Last update: $Date: 2006/09/13 13:26:42 $ * by : $Author: bpoussin $ */ @@ -35,6 +35,7 @@ import org.apache.commons.logging.LogConfigurationException; import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.impl.LogFactoryImpl; +import org.codelutin.util.CategorisedListenerSet; /** @@ -44,10 +45,112 @@ public class LutinLogFactory extends LogFactory { + static private LutinLogFactory instance = null; + protected LogFactory parentFactory = null; + protected CategorisedListenerSet listeners = new CategorisedListenerSet(); + protected long DefaultActiveProgressMonitorTime = 0; + protected long DefaultTimeout = 0; + + static public LutinLogFactory getInstance() { + if (instance == null) { + LogFactory factory = LogFactory.getFactory(); + if (factory instanceof LutinLogFactory) { + instance = (LutinLogFactory)factory; + } else { + instance = new LutinLogFactory(); + } + } + return instance; + } + + static public LutinLog getLutinLog(Class clazz) { + LutinLogFactory factory = LutinLogFactory.getInstance(); + LutinLog result = (LutinLog)factory.getInstance(clazz); + return result; + } + + static public LutinLog getLutinLog(String cat) { + LutinLogFactory factory = LutinLogFactory.getInstance(); + LutinLog result = (LutinLog)factory.getInstance(cat); + return result; + } + + /** + * @return the defaultActiveProgressMonitorTime + */ + public long getDefaultActiveProgressMonitorTime() { + return this.DefaultActiveProgressMonitorTime; + } + /** + * @param defaultActiveProgressMonitorTime the defaultActiveProgressMonitorTime to set + */ + public void setDefaultActiveProgressMonitorTime( + long defaultActiveProgressMonitorTime) { + this.DefaultActiveProgressMonitorTime = defaultActiveProgressMonitorTime; + } /** + * @return the defaultTimeout + */ + public long getDefaultTimeout() { + return this.DefaultTimeout; + } + + /** + * @param defaultTimeout the defaultTimeout to set + */ + public void setDefaultTimeout(long defaultTimeout) { + this.DefaultTimeout = defaultTimeout; + } + + /////////////////////////////////////////////////////////////////////////// + // + // L I S T E N E R + // + /////////////////////////////////////////////////////////////////////////// + + /** + * Add listener on all LutinLog + * + * @param l + */ + public void addLutinLogListener(LutinLogListener l) { + listeners.add(this, l); + } + + /** + * Add listener on specific LutinLog + * + * @param log + * @param l + */ + public void addLutinLogListener(LutinLog log, LutinLogListener l) { + listeners.add(log, l); + } + + /** + * Add listener on all LutinLog + * + * @param l + */ + public void removeLutinLogListener(LutinLogListener l) { + listeners.remove(this, l); + } + + /** + * Add listener on specific LutinLog + * + * @param log + * @param l + */ + public void removeLutinLogListener(LutinLog log, LutinLogListener l) { + listeners.remove(log, l); + } + + /** + * Return le réel commons-logging a utiliser * @return the parentFactory */ protected LogFactory getParentFactory() { @@ -57,6 +160,12 @@ return this.parentFactory; } + /////////////////////////////////////////////////////////////////////////// + // + // C O M M O N S - L O G G I N G M E T H O D + // + /////////////////////////////////////////////////////////////////////////// + /* (non-Javadoc) * @see org.apache.commons.logging.LogFactory#getAttribute(java.lang.String) */ @@ -82,6 +191,7 @@ public Log getInstance(Class arg0) throws LogConfigurationException { Log log = getParentFactory().getInstance(arg0); Log result = new LutinLog(this, log); + listeners.addCategory(this, result); return result; } @@ -92,6 +202,7 @@ public Log getInstance(String arg0) throws LogConfigurationException { Log log = getParentFactory().getInstance(arg0); Log result = new LutinLog(this, log); + listeners.addCategory(this, result); return result; } Index: lutinutil/src/java/org/codelutin/log/LutinLogListener.java diff -u /dev/null lutinutil/src/java/org/codelutin/log/LutinLogListener.java:1.1 --- /dev/null Wed Sep 13 13:26:47 2006 +++ lutinutil/src/java/org/codelutin/log/LutinLogListener.java Wed Sep 13 13:26:42 2006 @@ -0,0 +1,49 @@ +/* *##% + * Copyright (C) 2006 + * Code Lutin, Cédric Pineau, Benjamin Poussin + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + *##%*/ + +/* * + * LutinLogListener.java + * + * Created: 11 sept. 06 11:35:53 + * + * @author poussin + * @version $Revision: 1.1 $ + * + * Last update: $Date: 2006/09/13 13:26:42 $ + * by : $Author: bpoussin $ + */ + +package org.codelutin.log; + +import java.util.EventListener; + + +/** + * @author poussin + * + */ + +public interface LutinLogListener extends EventListener { + + public void progressEvent(LutinProgressEvent e); + public void logEvent(LutinLogEvent e); + +} + + Index: lutinutil/src/java/org/codelutin/log/LutinProgressEvent.java diff -u /dev/null lutinutil/src/java/org/codelutin/log/LutinProgressEvent.java:1.1 --- /dev/null Wed Sep 13 13:26:47 2006 +++ lutinutil/src/java/org/codelutin/log/LutinProgressEvent.java Wed Sep 13 13:26:42 2006 @@ -0,0 +1,74 @@ +/* *##% + * Copyright (C) 2006 + * Code Lutin, Cédric Pineau, Benjamin Poussin + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + *##%*/ + +/* * + * LutinLogEvent.java + * + * Created: 9 sept. 06 04:01:26 + * + * @author poussin + * @version $Revision: 1.1 $ + * + * Last update: $Date: 2006/09/13 13:26:42 $ + * by : $Author: bpoussin $ + */ + +package org.codelutin.log; + +import java.util.EventObject; + + +/** + * @author poussin + * + */ + +public class LutinProgressEvent extends EventObject { + + /** */ + private static final long serialVersionUID = 1L; + private ProgressType type; + + static public enum ProgressType {min, max, value, askStop, cancelAskStop, start, end} + + /** + * @param source + */ + public LutinProgressEvent(Object source, ProgressType type) { + super(source); + this.type = type; + } + + /* (non-Javadoc) + * @see java.util.EventObject#getSource() + */ + public LutinLog getLutinLog() { + return (LutinLog)super.getSource(); + } + + /** + * @return the type + */ + public ProgressType getType() { + return this.type; + } + +} + + Index: lutinutil/src/java/org/codelutin/log/MonitorMapper.java diff -u /dev/null lutinutil/src/java/org/codelutin/log/MonitorMapper.java:1.1 --- /dev/null Wed Sep 13 13:26:47 2006 +++ lutinutil/src/java/org/codelutin/log/MonitorMapper.java Wed Sep 13 13:26:42 2006 @@ -0,0 +1,232 @@ +/* *##% + * Copyright (C) 2006 + * Code Lutin, Cédric Pineau, Benjamin Poussin + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + *##%*/ + +/* * + * MonitorMapper.java + * + * Created: 13 sept. 06 11:39:06 + * + * @author poussin + * @version $Revision: 1.1 $ + * + * Last update: $Date: 2006/09/13 13:26:42 $ + * by : $Author: bpoussin $ + */ + +package org.codelutin.log; + +import java.awt.Button; +import java.awt.Component; +import java.awt.Container; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.beans.Statement; + +import javax.swing.JButton; +import javax.swing.JLabel; +import javax.swing.JProgressBar; +import javax.swing.JTextField; +import javax.swing.SwingUtilities; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.codelutin.log.LutinProgressEvent.ProgressType; + + +/** + * Helper to monitor LutinLog. This class permit to say which component used + * to display text, which component used to display progress and what is the + * cancel button + * + * @author poussin + */ + +public class MonitorMapper implements LutinLogListener, ActionListener { + + /** to use log facility, just put in your code: log.info(\"...\"); */ + static private Log log = LogFactory.getLog(MonitorMapper.class); + + protected LutinLog lutinLog; + protected Container parentContainer; + protected Component label; + protected JProgressBar progress; + protected Component cancel; + protected boolean changeCancelText; + + /** + * + * @param lutinLog LutinLog to monitor + * @param parentContainer component that contains all other component + * if not null, then when task begin setVisible(true) is called + * and when task end setVisible(false) is called + * @param label component that have setText method to display user log + * ({@link JLabel}, {@link JTextField}, ...) + * @param progress progress widget + * @param cancel componant that have addActionListener + * ({@link JButton},{@link Button}, ...) + * @param changeCancelText if true try to change text display in cancel + * component switch between "Cancel" and "Don't cancel" + */ + public MonitorMapper(LutinLog lutinLog, Container parentContainer, + Component label, JProgressBar progress, Component cancel, + boolean changeCancelText) { + this.lutinLog = lutinLog; + this.parentContainer = parentContainer; + this.label = label; + this.progress = progress; + this.cancel = cancel; + this.changeCancelText = changeCancelText; + + lutinLog.addLutinLogListener(this); + addActionListener(cancel, this); + } + + /** + * Try to call addActionListener on component, do nothing if call failed + * @param c + * @param action + */ + protected void addActionListener(Component c, ActionListener action) { + try { + if (c != null) { + Statement statement = new Statement(c, "addActionListener", new Object[]{action}); + statement.execute(); + } + } catch (Exception eee) { + if (log.isTraceEnabled()) { + log.trace("Can't modify call addActionListener on: " + c, eee); + } + } + } + /** + * try to call setText on component, do nothing if call failed + * @param c + * @param text text to put in component + */ + protected void setText(Component c, String text) { + try { + if (c != null) { + Statement statement = new Statement(c, "setText", new Object[]{text}); + statement.execute(); + } + } catch (Exception eee) { + if (log.isTraceEnabled()) { + log.trace("Can't modify call setText on: " + c, eee); + } + } + } + + /* (non-Javadoc) + * @see org.codelutin.log.LutinLogListener#logEvent(org.codelutin.log.LutinLogEvent) + */ + public void logEvent(LutinLogEvent e) { + if (e.getLogType() == LutinLogEvent.LogType.user) { + SwingUtilities.invokeLater(new HandleLogEvent(e)); + } + } + + /* (non-Javadoc) + * @see org.codelutin.log.LutinLogListener#progressEvent(org.codelutin.log.LutinProgressEvent) + */ + public void progressEvent(LutinProgressEvent e) { + SwingUtilities.invokeLater(new HandleProgressEvent(e)); + } + + /* (non-Javadoc) + * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent) + */ + public void actionPerformed(ActionEvent e) { + boolean oldValue = lutinLog.isAskStopTask(); + lutinLog.setAskStopTask(!oldValue); + } + + + /** + * Used to update UI + * @author poussin + */ + class HandleLogEvent implements Runnable { + protected LutinLogEvent e; + public HandleLogEvent(LutinLogEvent e) { + this.e = e; + } + public void run() { + setText(label, e.getMsg()); + } + } + + /** + * Used to update UI + * @author poussin + */ + class HandleProgressEvent implements Runnable { + protected LutinProgressEvent e; + public HandleProgressEvent(LutinProgressEvent e) { + this.e = e; + } + public void run() { + if (e.getType() == ProgressType.start) { + int min = e.getLutinLog().getMin(); + int max = e.getLutinLog().getMax(); + int value = e.getLutinLog().getValue(); + progress.setMinimum(min); + progress.setMaximum(max); + progress.setValue(value); + String text = value + " of " + max; // TODO i18n + progress.setString(text); + if (parentContainer != null) { + parentContainer.setVisible(true); + } + } else if (e.getType() == ProgressType.min) { + int min = e.getLutinLog().getMin(); + progress.setMinimum(min); + } else if (e.getType() == ProgressType.max) { + int max = e.getLutinLog().getMax(); + progress.setMaximum(max); + int value = e.getLutinLog().getValue(); + String text = value + " of " + max; // TODO i18n + progress.setString(text); + } else if (e.getType() == ProgressType.value) { + int max = e.getLutinLog().getMax(); + int value = e.getLutinLog().getValue(); + progress.setValue(value); + String text = value + " of " + max; // TODO i18n + progress.setString(text); + } else if (e.getType() == ProgressType.askStop) { + if (changeCancelText) { + setText(cancel, "Don't cancel"); // TODO i18n + } + } else if (e.getType() == ProgressType.cancelAskStop) { + if (changeCancelText) { + setText(cancel, "Cancel"); // TODO i18n + } + } else if (e.getType() == ProgressType.end) { + if (parentContainer != null) { + parentContainer.setVisible(false); + } + progress.setMinimum(0); + progress.setMaximum(0); + progress.setValue(0); + progress.setString(""); + } + } + } +} + + Index: lutinutil/src/java/org/codelutin/log/ProgressMonitorFrame.java diff -u /dev/null lutinutil/src/java/org/codelutin/log/ProgressMonitorFrame.java:1.1 --- /dev/null Wed Sep 13 13:26:47 2006 +++ lutinutil/src/java/org/codelutin/log/ProgressMonitorFrame.java Wed Sep 13 13:26:42 2006 @@ -0,0 +1,103 @@ +/* *##% + * Copyright (C) 2006 + * Code Lutin, Cédric Pineau, Benjamin Poussin + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + *##%*/ + +/* * + * ProgressMonitorFrame.java + * + * Created: 13 sept. 06 11:05:41 + * + * @author poussin + * @version $Revision: 1.1 $ + * + * Last update: $Date: 2006/09/13 13:26:42 $ + * by : $Author: bpoussin $ + */ + +package org.codelutin.log; + +import java.awt.Component; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; + +import javax.swing.Box; +import javax.swing.JButton; +import javax.swing.JFrame; +import javax.swing.JLabel; +import javax.swing.JProgressBar; +import javax.swing.SwingConstants; +import javax.swing.WindowConstants; + +import org.codelutin.log.LutinProgressEvent.ProgressType; + +// 20060923 poussin FIXME we must can used i18n in util lib. (perhaps put i18n directly in util lib) +//import static org.codelutin.i18n.I18n._; + +/** + * @author poussin + * + */ + +public class ProgressMonitorFrame extends JFrame { + + protected LutinLog lutinLog; + protected JLabel label; + protected JProgressBar progress; + protected JButton cancel; + protected MonitorMapper monitorMapper; + + /** + * + * @param lutinLog log that ProgressMonitorFrame must monitor + * @param extraComponent optionnal (can be null) user component to put in + * frame + */ + public ProgressMonitorFrame(LutinLog lutinLog, Component extraComponent) { + super("ProgressMonitor"); + + this.lutinLog = lutinLog; + + Box box = Box.createVerticalBox(); + + if (extraComponent != null) { + box.add(extraComponent); + } + + label = new JLabel("Message"); + label.setHorizontalTextPosition(SwingConstants.CENTER); + box.add(label); + + progress = new JProgressBar(); + progress.setStringPainted(true); + box.add(progress); + + cancel = new JButton(); + cancel.setText("Cancel"); // TODO i18n + box.add(cancel); + + this.add(box); + this.setDefaultCloseOperation(WindowConstants.HIDE_ON_CLOSE); + this.pack(); + + monitorMapper = new MonitorMapper(lutinLog, null, label, progress, cancel, true); + } + + +} + + Index: lutinutil/src/java/org/codelutin/log/ProgressState.java diff -u /dev/null lutinutil/src/java/org/codelutin/log/ProgressState.java:1.1 --- /dev/null Wed Sep 13 13:26:47 2006 +++ lutinutil/src/java/org/codelutin/log/ProgressState.java Wed Sep 13 13:26:42 2006 @@ -0,0 +1,44 @@ +/* *##% + * Copyright (C) 2006 + * Code Lutin, Cédric Pineau, Benjamin Poussin + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + *##%*/ + +/* * + * ProgressState.java + * + * Created: 11 sept. 06 12:41:41 + * + * @author poussin + * @version $Revision: 1.1 $ + * + * Last update: $Date: 2006/09/13 13:26:42 $ + * by : $Author: bpoussin $ + */ + +package org.codelutin.log; + + +/** + * @author poussin + * + */ + +public enum ProgressState { + start, stopAsked, end +} + +