Author: tchemit Date: 2008-07-08 21:14:58 +0000 (Tue, 08 Jul 2008) New Revision: 705 Added: trunk/lutinjaxx/util/src/main/java/org/codelutin/jaxx/util/AbstractUIAction.java trunk/lutinjaxx/util/src/main/java/org/codelutin/jaxx/util/DialogUI.java trunk/lutinjaxx/util/src/main/java/org/codelutin/jaxx/util/DialogUIDef.java trunk/lutinjaxx/util/src/main/java/org/codelutin/jaxx/util/DialogUIHandler.java trunk/lutinjaxx/util/src/main/java/org/codelutin/jaxx/util/DialogUIModel.java trunk/lutinjaxx/util/src/main/java/org/codelutin/jaxx/util/FactoryWindowListener.java trunk/lutinjaxx/util/src/main/java/org/codelutin/jaxx/util/FormElement.java trunk/lutinjaxx/util/src/main/java/org/codelutin/jaxx/util/ShowUIAction.java trunk/lutinjaxx/util/src/main/java/org/codelutin/jaxx/util/UIFactory.java trunk/lutinjaxx/util/src/main/java/org/codelutin/jaxx/util/UIHelper.java trunk/lutinjaxx/util/src/main/java/org/codelutin/jaxx/util/UIProvider.java Removed: trunk/lutinjaxx/util/src/main/java/org/codelutin/util/AbstractUIAction.java trunk/lutinjaxx/util/src/main/java/org/codelutin/util/DialogUI.java trunk/lutinjaxx/util/src/main/java/org/codelutin/util/DialogUIDef.java trunk/lutinjaxx/util/src/main/java/org/codelutin/util/DialogUIHandler.java trunk/lutinjaxx/util/src/main/java/org/codelutin/util/DialogUIModel.java trunk/lutinjaxx/util/src/main/java/org/codelutin/util/FactoryWindowListener.java trunk/lutinjaxx/util/src/main/java/org/codelutin/util/FormElement.java trunk/lutinjaxx/util/src/main/java/org/codelutin/util/ShowUIAction.java trunk/lutinjaxx/util/src/main/java/org/codelutin/util/UIFactory.java trunk/lutinjaxx/util/src/main/java/org/codelutin/util/UIHelper.java trunk/lutinjaxx/util/src/main/java/org/codelutin/util/UIProvider.java Log: use org.codelutin.jaxx:util module Copied: trunk/lutinjaxx/util/src/main/java/org/codelutin/jaxx/util/AbstractUIAction.java (from rev 700, trunk/lutinjaxx/util/src/main/java/org/codelutin/util/AbstractUIAction.java) =================================================================== --- trunk/lutinjaxx/util/src/main/java/org/codelutin/jaxx/util/AbstractUIAction.java (rev 0) +++ trunk/lutinjaxx/util/src/main/java/org/codelutin/jaxx/util/AbstractUIAction.java 2008-07-08 21:14:58 UTC (rev 705) @@ -0,0 +1,53 @@ +/** + * # #% Copyright (C) 2008 Code Lutin, Tony Chemit + * 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. + * # #% + */ +package org.codelutin.jaxx.util; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + +/** @author chemit */ +public abstract class AbstractUIAction<H extends DialogUIHandler<?, ?>> extends javax.swing.AbstractAction { + + protected static Log log = LogFactory.getLog(AbstractUIAction.class); + + protected transient DialogUI<? extends H> ui; + + private static final long serialVersionUID = 1L; + + protected AbstractUIAction(String name, javax.swing.Icon icon, DialogUI<? extends H> ui) { + super(name, icon); + this.ui = ui; + } + + protected H getHandler() { + checkInit(); + return ui.getHandler(); + } + + protected void setUi(DialogUI<? extends H> ui) { + this.ui = ui; + } + + public DialogUI<? extends H> getUi() { + return ui; + } + + protected void checkInit() throws IllegalStateException { + /*if (ui == null) { + throw new IllegalStateException("no handler, nor ui referenced in " + this); + } */ + } + +} Copied: trunk/lutinjaxx/util/src/main/java/org/codelutin/jaxx/util/DialogUI.java (from rev 700, trunk/lutinjaxx/util/src/main/java/org/codelutin/util/DialogUI.java) =================================================================== --- trunk/lutinjaxx/util/src/main/java/org/codelutin/jaxx/util/DialogUI.java (rev 0) +++ trunk/lutinjaxx/util/src/main/java/org/codelutin/jaxx/util/DialogUI.java 2008-07-08 21:14:58 UTC (rev 705) @@ -0,0 +1,129 @@ +/** + * ##% Copyright (C) 2008 Code Lutin, Tony Chemit + * 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. + * ##% + */ +package org.codelutin.jaxx.util; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + +import javax.swing.AbstractAction; +import javax.swing.AbstractButton; +import javax.swing.ImageIcon; +import javax.swing.JDialog; +import java.awt.event.WindowEvent; +import java.awt.event.WindowListener; +import java.lang.reflect.Constructor; + +/** + * A abstract dialog contract to be realised by a dialogUI (WindowEvent adapter) + * <p/> + * TODO : make jaxx authorized implementing interface for root tag :) + * + * @author chemit + */ +public abstract class DialogUI<H extends DialogUIHandler> extends JDialog implements WindowListener { + + protected static Log log = LogFactory.getLog(DialogUI.class); + + public javax.swing.AbstractAction newAction(Class<?> actionClass, Object... params) { + Constructor<?> constructor = null; + for (Constructor<?> cons : actionClass.getConstructors()) { + Class<?>[] prototype = cons.getParameterTypes(); + if (prototype.length > 0 && DialogUI.class.isAssignableFrom(prototype[0])) { + // use this constructor + constructor = cons; + break; + } + } + if (constructor == null) { + throw new IllegalStateException("could not find a matching constructor for " + actionClass); + } + + // wrap params + Object[] parameters = new Object[1 + params.length]; + parameters[0] = this; + System.arraycopy(params, 0, parameters, 1, params.length); + try { + AbstractAction action = (AbstractAction) constructor.newInstance(parameters); + if (log.isInfoEnabled()) { + log.info(action); + } + return action; + } catch (Exception e) { + throw new IllegalStateException("could not init the action " + actionClass + " for reason : " + e.getMessage()); + } + } + + private H handler; + + public abstract AbstractButton getHelp(); + + public abstract Object getObjectById(java.lang.String s); + + protected DialogUI() { + UIHelper.setQuitAction(this); + addWindowListener(this); + //TODO will be handled by jaxx with javax.help... + //getHelp().setAction(newAction(HelpAction.class)); + } + + public H getHandler() { + return handler; + } + + public void setHandler(H handler) { + this.handler = handler; + } + + protected ImageIcon createActionIcon(String name) { + return UIHelper.createActionIcon(name); + } + + public void windowOpened(WindowEvent e) { + } + + public void windowClosed(WindowEvent e) { + } + + public void windowClosing(WindowEvent e) { + } + + public void windowIconified(WindowEvent e) { + } + + public void windowDeiconified(WindowEvent e) { + } + + public void windowActivated(WindowEvent e) { + } + + public void windowDeactivated(WindowEvent e) { + } + + @Override + public synchronized void addWindowListener(WindowListener l) { + super.addWindowListener(l); + if (log.isDebugEnabled()) { + log.debug("after added (" + getWindowListeners().length + ") : " + l); + } + } + + @Override + public synchronized void removeWindowListener(WindowListener l) { + super.removeWindowListener(l); + if (log.isDebugEnabled()) { + log.debug("after removed (" + getWindowListeners().length + ") : " + l); + } + } +} \ No newline at end of file Copied: trunk/lutinjaxx/util/src/main/java/org/codelutin/jaxx/util/DialogUIDef.java (from rev 700, trunk/lutinjaxx/util/src/main/java/org/codelutin/util/DialogUIDef.java) =================================================================== --- trunk/lutinjaxx/util/src/main/java/org/codelutin/jaxx/util/DialogUIDef.java (rev 0) +++ trunk/lutinjaxx/util/src/main/java/org/codelutin/jaxx/util/DialogUIDef.java 2008-07-08 21:14:58 UTC (rev 705) @@ -0,0 +1,235 @@ +/** + * ##% Copyright (C) 2008 Code Lutin, Tony Chemit + * 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. + * ##% + */ +package org.codelutin.jaxx.util; + +import static org.codelutin.i18n.I18n._; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + +import javax.swing.ImageIcon; +import java.lang.reflect.Constructor; + +/** + * Definition of an ui, with his model, handler and ui class definitions. + * <p/> + * The class contains also a shared instace of concrete ui. + * + * @author chemit + */ +public class DialogUIDef<M extends DialogUIModel, U extends DialogUI<H>, H extends DialogUIHandler<M, U>> implements java.io.Serializable { + + static protected final Log log = LogFactory.getLog(DialogUIDef.class); + + public static <M extends DialogUIModel, U extends DialogUI<H>, H extends DialogUIHandler<M, U>> DialogUIDef<M, U, H> newDef(Class<H> handlerClass, Class<U> uiClass, Class<M> modelClass, String showActionLibelle, String showActionTip, String uiTitle) { + DialogUIDef<M, U, H> result; + result = new DialogUIDef<M, U, H>(handlerClass, uiClass, modelClass, showActionLibelle, showActionTip, uiTitle); + return result; + } + + /** + * model class + */ + private final Class<M> modelClass; + + /** + * handler class + */ + private final Class<H> handlerClass; + + /** + * abstract ui class + */ + private final Class<U> uiClass; + + /** + * concrete lookup ui class + */ + private Class<? extends U> uiImplClass; + + /** + * shared instance of ui + */ + protected U uiInstance; + + /** + * unique name of ui def + */ + protected final String name; + + protected final String uiTitle; + protected final String showActionLibelle; + protected final String showActionTip; + + protected ImageIcon showUIActionIcon; + + + private static final long serialVersionUID = 1L; + + private DialogUIDef(Class<H> handlerClass, Class<U> uiClass, Class<M> modelClass, + String showActionLibelle, String showActionTip, String uiTitle) { + this.handlerClass = handlerClass; + this.uiClass = uiClass; + this.modelClass = modelClass; + this.showActionLibelle = showActionLibelle; + this.name = uiClass.getSimpleName().toLowerCase(); + this.showActionTip = showActionTip; + this.uiTitle = uiTitle; + } + + public Class<U> getUiClass() { + return uiClass; + } + + public Class<H> getHandlerClass() { + return handlerClass; + } + + public Class<M> getModelClass() { + return modelClass; + } + + public Class<? extends U> getUiImplClass() { + return uiImplClass; + } + + public String getUiTitle() { + return _(uiTitle); + } + + public String getShowActionLibelle() { + return _(showActionLibelle); + } + + public String getShowActionTip() { + return _(showActionTip); + } + + public ImageIcon getShowUIActionIcon() { + if (showUIActionIcon == null) { + showUIActionIcon = UIHelper.createActionIcon("show-" + name); + } + return showUIActionIcon; + } + + @SuppressWarnings({"unchecked"}) + public void setUiImplClass(Class<?> uiImplClass) { + this.uiImplClass = (Class<? extends U>) uiImplClass; + } + + @Override + public boolean equals(Object o) { + return this == o || o instanceof DialogUIDef && uiClass.equals(((DialogUIDef) o).uiClass); + } + + @Override + public int hashCode() { + return uiClass.hashCode(); + } + + @Override + public String toString() { + StringBuilder sb = new StringBuilder(super.toString()).append('<'); + sb.append(printClass("handler", handlerClass, true)); + sb.append(printClass("model", modelClass, true)); + sb.append(printClass("ui", uiClass, true)); + sb.append(printClass("uiImpl", uiImplClass, false)); + return sb.toString(); + } + + protected U getUiInstance() { + // no lazy instanciation, to control ui instanciation... + /*if (uiInstance == null) { + if (uiImplClass == null) { + throw new IllegalStateException("no concrete ui impl found in " + this); + } + synchronized (this) { + try { + uiInstance = uiImplClass.newInstance(); + } catch (Exception e) { + throw new IllegalStateException("could not instanciate ui " + this,e); + } + } + }*/ + return uiInstance; + } + + protected void setUiInstance(U uiInstance) { + this.uiInstance = uiInstance; + } + + protected U newUI() { + if (uiImplClass == null) { + throw new IllegalStateException("no concrete ui impl found in " + this); + } + try { + U result = uiImplClass.newInstance(); + log.info(result); + return result; + } catch (Exception e) { + throw new IllegalStateException("could not instanciate ui " + this, e); + } + } + + protected M newModel() { + if (modelClass == null) { + throw new IllegalStateException("no model impl found in " + this); + } + try { + M model = modelClass.newInstance(); + log.info(model); + return model; + } catch (Exception e) { + throw new IllegalStateException("could not instanciate ui " + this, e); + } + } + + protected H newHandler(U ui, M model, Object... params) { + if (handlerClass == null) { + throw new IllegalStateException("no handler impl found in " + this); + } + try { + Class[] prototype = getHandlerPrototype(params); + Object[] parameters = getHandlerParameters(ui, model, params); + H result = handlerClass.getConstructor(prototype).newInstance(parameters); + log.info(result); + return result; + } catch (Exception e) { + throw new IllegalStateException("could not instanciate ui " + this, e); + } + } + + protected Object[] getHandlerParameters(U ui, M model, Object[] params) { + Object[] result = new Object[2 + params.length]; + result[0] = ui; + result[1] = model; + System.arraycopy(params, 0, result, 2, params.length); + return result; + } + + protected Class[] getHandlerPrototype(Object[] params) { + int length = params.length; + for (Constructor<?> constructor : handlerClass.getConstructors()) { + Class<?>[] prototype = constructor.getParameterTypes(); + if (prototype.length == 2 + length && prototype[0] == uiClass && prototype[1] == modelClass) { + return prototype; + } + } + throw new IllegalStateException("could not find a matching constructor in " + handlerClass); + } + + protected String printClass(String s, Class<?> aClass, boolean notLast) { + return s + ':' + (aClass == null ? null : aClass.getSimpleName()) + (notLast ? ", " : ">"); + } +} Copied: trunk/lutinjaxx/util/src/main/java/org/codelutin/jaxx/util/DialogUIHandler.java (from rev 700, trunk/lutinjaxx/util/src/main/java/org/codelutin/util/DialogUIHandler.java) =================================================================== --- trunk/lutinjaxx/util/src/main/java/org/codelutin/jaxx/util/DialogUIHandler.java (rev 0) +++ trunk/lutinjaxx/util/src/main/java/org/codelutin/jaxx/util/DialogUIHandler.java 2008-07-08 21:14:58 UTC (rev 705) @@ -0,0 +1,70 @@ +/** + * ##% Copyright (C) 2008 Code Lutin, Tony Chemit + * 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. + * ##% + */ +package org.codelutin.jaxx.util; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + +import java.awt.event.WindowListener; +import java.beans.PropertyChangeListener; + +/** + * DialogUI handler + * + * @author chemit + */ +public abstract class DialogUIHandler<M extends DialogUIModel, U extends DialogUI<? extends DialogUIHandler>> implements PropertyChangeListener { + + protected static Log log = LogFactory.getLog(DialogUIHandler.class); + + /** ui handled */ + private U ui; + + /** model handled */ + private M model; + + protected DialogUIHandler(U ui, M model) { + this.ui = ui; + this.model = model; + } + + public U getUi() { + return ui; + } + + public M getModel() { + return model; + } + + public void init() { + if (model == null) { + throw new IllegalStateException("no model was defined for " + this); + } + model.addPropertyChangeListener(this); + } + + public void dispose() { + model.dispose(); + for (WindowListener windowListener : getUi().getWindowListeners()) { + getUi().removeWindowListener(windowListener); + } + } + + @Override + protected void finalize() throws Throwable { + super.finalize(); + dispose(); + } +} \ No newline at end of file Copied: trunk/lutinjaxx/util/src/main/java/org/codelutin/jaxx/util/DialogUIModel.java (from rev 700, trunk/lutinjaxx/util/src/main/java/org/codelutin/util/DialogUIModel.java) =================================================================== --- trunk/lutinjaxx/util/src/main/java/org/codelutin/jaxx/util/DialogUIModel.java (rev 0) +++ trunk/lutinjaxx/util/src/main/java/org/codelutin/jaxx/util/DialogUIModel.java 2008-07-08 21:14:58 UTC (rev 705) @@ -0,0 +1,97 @@ +/** + * ##% Copyright (C) 2008 Code Lutin, Tony Chemit + * 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. + * ##% + */ +package org.codelutin.jaxx.util; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + +import java.beans.PropertyChangeListener; +import java.beans.PropertyChangeSupport; + +/** + * Abstract ui model, with property change support. + * + * @author chemit + */ +public abstract class DialogUIModel { + + static protected final Log log = LogFactory.getLog(DialogUIModel.class); + + /** support for change properties support */ + protected PropertyChangeSupport changeSupport; + + public synchronized void addPropertyChangeListener(String propertyName, PropertyChangeListener listener) { + if (listener == null) { + return; + } + if (changeSupport == null) { + changeSupport = new PropertyChangeSupport(this); + } + changeSupport.addPropertyChangeListener(propertyName, listener); + } + + public synchronized void addPropertyChangeListener(PropertyChangeListener listener) { + if (listener == null) { + return; + } + if (changeSupport == null) { + changeSupport = new PropertyChangeSupport(this); + } + changeSupport.addPropertyChangeListener(listener); + } + + public synchronized void removePropertyChangeListener(PropertyChangeListener listener) { + if (listener == null || changeSupport == null) { + return; + } + changeSupport.removePropertyChangeListener(listener); + } + + public synchronized void removePropertyChangeListeners() { + if (changeSupport == null) { + return; + } + for (PropertyChangeListener listener : getPropertyChangeListeners()) { + changeSupport.removePropertyChangeListener(listener); + } + } + + public synchronized PropertyChangeListener[] getPropertyChangeListeners() { + if (changeSupport == null) { + return new PropertyChangeListener[0]; + } + return changeSupport.getPropertyChangeListeners(); + } + + public void firePropertyChange(String propertyName, Object oldValue, Object newValue) { + if (changeSupport == null || (oldValue == null && newValue == null) || + (oldValue != null && oldValue.equals(newValue))) { + return; + } + changeSupport.firePropertyChange(propertyName, oldValue, newValue); + } + + public void dispose() { + for (PropertyChangeListener listener : changeSupport.getPropertyChangeListeners()) { + changeSupport.removePropertyChangeListener(listener); + } + } + + @Override + protected void finalize() throws Throwable { + super.finalize(); + dispose(); + } +} \ No newline at end of file Copied: trunk/lutinjaxx/util/src/main/java/org/codelutin/jaxx/util/FactoryWindowListener.java (from rev 700, trunk/lutinjaxx/util/src/main/java/org/codelutin/util/FactoryWindowListener.java) =================================================================== --- trunk/lutinjaxx/util/src/main/java/org/codelutin/jaxx/util/FactoryWindowListener.java (rev 0) +++ trunk/lutinjaxx/util/src/main/java/org/codelutin/jaxx/util/FactoryWindowListener.java 2008-07-08 21:14:58 UTC (rev 705) @@ -0,0 +1,86 @@ +/** + * # #% Copyright (C) 2008 Code Lutin, Tony Chemit + * 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. + * # #% + */ +package org.codelutin.jaxx.util; + +import static org.codelutin.ui.UIFactory.log; + +import java.awt.event.WindowAdapter; +import java.awt.event.WindowEvent; + +/** + * A windowListenr for ui managed by {@link org.codelutin.ui.UIFactory}. + * <p/> + * To be used when all ui from factory are closed, via {@link #allWindowsClosed(java.awt.event.WindowEvent)} method. + * + * @author chemit + */ +public abstract class FactoryWindowListener extends WindowAdapter { + + /** + * method to be invoked when all ui registred in factory are really disposed. + * + * @param e event + */ + public abstract void allWindowsClosed(WindowEvent e); + + /** underlying factory of ui */ + private UIFactory factory; + + /** flag to make sure {@link #allWindowsClosed(java.awt.event.WindowEvent)} is called only once. */ + private boolean wasClosed; + + @Override + public void windowClosed(WindowEvent e) { + if (UIFactory.log.isDebugEnabled()) { + UIFactory.log.debug(this + " : " + e); + } + if (e.getWindow().isVisible()) { + // only deal with real closed and none visible windows... + return; + } + for (DialogUIDef def : factory.getDefs()) { + DialogUI ui = def.uiInstance; + if (ui != null && ui.isVisible()) { + // at least one ui visible, do not close all + return; + } + } + + if (wasClosed) { + // make sure to process only once + return; + } + if (UIFactory.log.isInfoEnabled()) { + UIFactory.log.info("closing factory listener " + this); + } + + synchronized (this) { + try { + allWindowsClosed(e); + } finally { + wasClosed = true; + factory.removeFactoryWindowListener(this); + } + } + } + + protected UIFactory getFactory() { + return factory; + } + + protected void setFactory(UIFactory factory) { + this.factory = factory; + } +} Copied: trunk/lutinjaxx/util/src/main/java/org/codelutin/jaxx/util/FormElement.java (from rev 700, trunk/lutinjaxx/util/src/main/java/org/codelutin/util/FormElement.java) =================================================================== --- trunk/lutinjaxx/util/src/main/java/org/codelutin/jaxx/util/FormElement.java (rev 0) +++ trunk/lutinjaxx/util/src/main/java/org/codelutin/jaxx/util/FormElement.java 2008-07-08 21:14:58 UTC (rev 705) @@ -0,0 +1,29 @@ +/** + * # #% Copyright (C) 2008 Code Lutin, Tony Chemit + * 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. + * # #% + */ +package org.codelutin.jaxx.util; + +/** @author chemit */ +public interface FormElement<U extends DialogUI> { + + String name(); + + int ordinal(); + + Object getValue(U ui); + + void setValue(U ui, String value); + + javax.swing.JLabel getLabel(U ui); +} Copied: trunk/lutinjaxx/util/src/main/java/org/codelutin/jaxx/util/ShowUIAction.java (from rev 700, trunk/lutinjaxx/util/src/main/java/org/codelutin/util/ShowUIAction.java) =================================================================== --- trunk/lutinjaxx/util/src/main/java/org/codelutin/jaxx/util/ShowUIAction.java (rev 0) +++ trunk/lutinjaxx/util/src/main/java/org/codelutin/jaxx/util/ShowUIAction.java 2008-07-08 21:14:58 UTC (rev 705) @@ -0,0 +1,135 @@ +/** + * # #% Copyright (C) 2008 Code Lutin, Tony Chemit + * 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. + * # #% + */ +package org.codelutin.jaxx.util; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + +import java.awt.Dimension; +import java.awt.Point; +import java.awt.event.ActionEvent; + +/** @author chemit */ +public abstract class ShowUIAction<M extends DialogUIModel, U extends DialogUI<H>, H extends DialogUIHandler<M, U>> extends javax.swing.AbstractAction { + + protected static Log log = LogFactory.getLog(AbstractUIAction.class); + + protected transient DialogUI<?> ui; + + private static final long serialVersionUID = 1L; + + protected DialogUIDef<M, U, H> uiDef; + + protected transient UIFactory factory; + + protected String position; + + protected boolean undecorated = true; + + protected U initUI(ActionEvent e) { + return getFactory().getUI(uiDef); + } + + public ShowUIAction(DialogUI<?> ui, DialogUIDef<M, U, H> uiDef, UIFactory factory, boolean showText) { + super(uiDef.getShowActionLibelle(), uiDef.getShowUIActionIcon()); + this.ui = ui; + this.uiDef = uiDef; + String name = (String) getValue(NAME); + putValue(DISPLAYED_MNEMONIC_INDEX_KEY, name.length() - 1); + putValue(ACCELERATOR_KEY, (int) name.charAt(name.length() - 1)); + if (!showText) { + putValue(NAME, null); + } + putValue(SHORT_DESCRIPTION, uiDef.getShowActionTip()); + this.factory = factory; + } + + public DialogUI<?> getUi() { + return ui; + } + + public DialogUIDef<M, U, H> getUiDef() { + return uiDef; + } + + public UIFactory getFactory() { + return factory; + } + + public void setUiDef(DialogUIDef<M, U, H> uiDef) { + this.uiDef = uiDef; + } + + public void setPosition(String position) { + this.position = position; + } + + public void setUndecorated(boolean undecorated) { + this.undecorated = undecorated; + } + + public void actionPerformed(java.awt.event.ActionEvent e) { + checkInit(); + U ui = initUI(e); + ui.setTitle(uiDef.getUiTitle()); + log.info(ui.getTitle()); + //TODO ui.setUndecorated(undecorated); + setPosition(this.getUi(), ui, position); + + ui.setVisible(true); + } + + protected void setPosition(javax.swing.JDialog parentUI, javax.swing.JDialog ui, String position) { + if (position == null || parentUI == null) { + return; + } + Point parentLocation = parentUI.getLocationOnScreen(); + Dimension parentSize = parentUI.getSize(); + + if (position.equals("bottom-left")) { + int top = (int) (parentLocation.getY() + parentSize.getHeight()); + int left = (int) (parentLocation.getX()); + Point newLocation = new Point(left, top); + newLocation.setLocation(left, top); + ui.setLocation(newLocation); + return; + } + if (position.equals("top-left")) { + int top = (int) (parentLocation.getY()); + int left = (int) (parentLocation.getX()); + Point newLocation = new Point(left, top); + newLocation.setLocation(left, top); + ui.setLocation(newLocation); + return; + } + if (position.equals("top-right")) { + int top = (int) (parentLocation.getY()); + int left = (int) (parentLocation.getX() + parentSize.getWidth()); + Point newLocation = new Point(left, top); + newLocation.setLocation(left, top); + ui.setLocation(newLocation); + return; + } + if (position.equals(("center"))) { + //TODO + } + } + + protected void checkInit() throws IllegalStateException { + if (factory == null) { + throw new IllegalStateException("no factory found in " + this); + } + } +} \ No newline at end of file Copied: trunk/lutinjaxx/util/src/main/java/org/codelutin/jaxx/util/UIFactory.java (from rev 700, trunk/lutinjaxx/util/src/main/java/org/codelutin/util/UIFactory.java) =================================================================== --- trunk/lutinjaxx/util/src/main/java/org/codelutin/jaxx/util/UIFactory.java (rev 0) +++ trunk/lutinjaxx/util/src/main/java/org/codelutin/jaxx/util/UIFactory.java 2008-07-08 21:14:58 UTC (rev 705) @@ -0,0 +1,184 @@ +/** +/** + * ##% Copyright (C) 2008 Code Lutin, Tony Chemit + * 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. + * ##% + */ +package org.codelutin.jaxx.util; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.codelutin.util.StringUtil; + +import javax.swing.event.EventListenerList; +import java.util.ArrayList; +import java.util.List; +import java.util.ServiceLoader; + +/** + * Factory for UI, using a cache and a provider to find ui implementations. + * + * @author chemit + */ +public class UIFactory { + + static protected final Log log = LogFactory.getLog(UIFactory.class); + + private final String applicationName; + + private final DialogUIDef[] defs; + + private final EventListenerList listeners; + + public UIFactory(String applicationName, DialogUIDef[] defs, FactoryWindowListener... listeners) { + this.applicationName = applicationName; + this.listeners = new EventListenerList(); + for (FactoryWindowListener listener : listeners) { + listener.setFactory(this); + addFactoryWindowListener(listener); + } + this.defs = defs; + long t0 = System.nanoTime(); + if (log.isDebugEnabled()) { + log.debug("start at " + new java.util.Date()); + } + try { + init(); + } catch (Exception e) { + log.error(e); + throw new RuntimeException(e); + } finally { + if (log.isDebugEnabled()) { + log.info("end in " + StringUtil.convertTime(t0, System.nanoTime())); + } + } + } + + public void addFactoryWindowListener(FactoryWindowListener l) { + listeners.add(FactoryWindowListener.class, l); + if (log.isDebugEnabled()) { + log.debug("after added (" + listeners.getListenerCount() + ") : " + l); + } + } + + public void removeFactoryWindowListener(FactoryWindowListener l) { + listeners.remove(FactoryWindowListener.class, l); + for (DialogUIDef def : getDefs()) { + if (def.uiInstance != null) { + def.uiInstance.removeWindowListener(l); + } + } + if (log.isDebugEnabled()) { + log.debug(" after removed (" + listeners.getListenerCount() + ") : " + l); + } + if (listeners.getListenerCount(FactoryWindowListener.class) == 0) { + // close for real factory + close(); + } + } + + public void close() { + log.info(this + " at " + new java.util.Date()); + for (DialogUIDef<?, ?, ?> def : defs) { + DialogUI<?> ui = def.uiInstance; + if (ui != null) { + ui.getHandler().dispose(); + def.uiInstance = null; + } + } + if (listeners.getListenerCount(FactoryWindowListener.class) > 0) { + log.warn("some listeners where not properly removed, force deletion..."); + for (FactoryWindowListener listener : listeners.getListeners(FactoryWindowListener.class)) { + removeFactoryWindowListener(listener); + } + } + + } + + protected void init() { + + UIProvider[] providers = detectProviders(); + + for (DialogUIDef<?, ?, ?> def : defs) { + initDef(providers, def); + if (def.getUiImplClass() == null) { + throw new IllegalStateException("could not find implementation for ui def " + def); + } + } + } + + protected void initDef(UIProvider[] providers, DialogUIDef<?, ?, ?> def) { + for (UIProvider provider : providers) { + Class<?> uiImplClass = provider.findUIImplementation(def); + if (uiImplClass != null) { + if (log.isDebugEnabled()) { + log.debug("init done for " + def); + } + // ui implementation was found + break; + } + } + } + + protected UIProvider[] detectProviders() { + long t0 = System.nanoTime(); + List<UIProvider> providers = new ArrayList<UIProvider>(); + for (UIProvider provider : ServiceLoader.load(UIProvider.class)) { + if (applicationName.equals(provider.getApplicationName())) { + if (log.isDebugEnabled()) { + log.debug("provider detected [" + provider + ']'); + } + providers.add(provider); + } + } + log.info("found " + providers.size() + " ui provider(s) in " + StringUtil.convertTime(t0, System.nanoTime()) + " : " + providers); + return providers.toArray(new UIProvider[providers.size()]); + } + + protected DialogUIDef[] getDefs() { + return defs; + } + + @Override + protected void finalize() throws Throwable { + super.finalize(); + close(); + } + + public <M extends DialogUIModel, U extends DialogUI<H>, H extends DialogUIHandler<M, U>> U getUI(DialogUIDef<M, U, H> uiType, Object... params) { + U ui = uiType.uiInstance; + if (ui == null) { + try { + ui = uiType.newUI(); + M model = uiType.newModel(); + H handler = uiType.newHandler(ui, model, params); + registerUI(uiType, ui, handler); + } catch (Exception e) { + throw new IllegalStateException("could not instanciate ui handler " + uiType + " for reason : " + e.getMessage(), e); + } + } + return ui; + } + + protected <M extends DialogUIModel, U extends DialogUI<H>, H extends DialogUIHandler<M, U>> void registerUI(DialogUIDef<M, U, H> uiType, U ui, H handler) { + ui.setHandler(handler); + handler.init(); + uiType.setUiInstance(ui); + for (FactoryWindowListener listener : listeners.getListeners(FactoryWindowListener.class)) { + if (log.isDebugEnabled()) { + log.debug("----- addFactoryWindowListener " + listener + " to " + ui); + } + ui.addWindowListener(listener); + } + } + +} \ No newline at end of file Copied: trunk/lutinjaxx/util/src/main/java/org/codelutin/jaxx/util/UIHelper.java (from rev 700, trunk/lutinjaxx/util/src/main/java/org/codelutin/util/UIHelper.java) =================================================================== --- trunk/lutinjaxx/util/src/main/java/org/codelutin/jaxx/util/UIHelper.java (rev 0) +++ trunk/lutinjaxx/util/src/main/java/org/codelutin/jaxx/util/UIHelper.java 2008-07-08 21:14:58 UTC (rev 705) @@ -0,0 +1,70 @@ +/* +* ##% Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008 Code Lutin, +* Tony Chemit +* +* 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. +* ##% */ +package org.codelutin.jaxx.util; + +import javax.swing.AbstractAction; +import javax.swing.Action; +import javax.swing.ImageIcon; +import javax.swing.JComponent; +import javax.swing.JDialog; +import javax.swing.JRootPane; +import javax.swing.KeyStroke; +import java.awt.event.ActionEvent; + +/** + * Ui helper class. + * + * @author tony + */ +public class UIHelper { + + public static ImageIcon createImageIcon(String path) { + java.net.URL imgURL = UIHelper.class.getResource("/icons/" + path); + if (imgURL != null) { + return new ImageIcon(imgURL); + } else { + throw new IllegalArgumentException("could not find icon " + path); + } + } + + /** + * Attach to <code>ui</code> an abort action,accessible by <code>ESC</code> key. + * + * @param ui ui + */ + public static void setQuitAction(final JDialog ui) { + JRootPane rootPane = ui.getRootPane(); + + Action quitAction = new AbstractAction("quit") { + private static final long serialVersionUID = -869095664995763057L; + + public void actionPerformed(ActionEvent e) { + ui.dispose(); + } + }; + rootPane.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke("ESCAPE"), "quit"); + rootPane.getActionMap().put("quit", quitAction); + ui.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); + } + + public static ImageIcon createActionIcon(String name) { + return createImageIcon("action-" + name + ".png"); + } + +} Copied: trunk/lutinjaxx/util/src/main/java/org/codelutin/jaxx/util/UIProvider.java (from rev 700, trunk/lutinjaxx/util/src/main/java/org/codelutin/util/UIProvider.java) =================================================================== --- trunk/lutinjaxx/util/src/main/java/org/codelutin/jaxx/util/UIProvider.java (rev 0) +++ trunk/lutinjaxx/util/src/main/java/org/codelutin/jaxx/util/UIProvider.java 2008-07-08 21:14:58 UTC (rev 705) @@ -0,0 +1,86 @@ +/** + * # #% Copyright (C) 2008 Code Lutin, Tony Chemit + * 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. + * # #% + */ +package org.codelutin.jaxx.util; + +/** @author chemit */ +public abstract class UIProvider { + + /** the name of application using this provider */ + protected String applicationName; + + /** the name of ui implementation used by this provider */ + protected String providerName; + + /** array of ui implementations */ + protected Class<?>[] implementations; + + protected UIProvider(String applicationName, String providerName, Class<?>... implementations) { + this.applicationName = applicationName; + this.providerName = providerName; + this.implementations = implementations; + } + + public String getProviderName() { + return providerName; + } + + public String getApplicationName() { + return applicationName; + } + + public Class<?>[] getImplementations() { + return implementations; + } + + public Class<?> findUIImplementation(DialogUIDef<?, ?, ?> def) { + Class<? extends DialogUI<?>> uiClass = def.getUiClass(); + for (Class<?> klass : implementations) { + if (uiClass.isAssignableFrom(klass)) { + def.setUiImplClass(klass); + return klass; + } + } + return null; + } + + @Override + public String toString() { + StringBuilder sb = new StringBuilder(super.toString()).append('<'); + sb.append(printClass("application", applicationName, true)); + sb.append(printClass("provider", providerName, true)); + sb.append(printClass("uis", implementations.length, false)); + return sb.toString(); + } + + protected String printClass(String s, Object aClass, boolean notLast) { + return s + ':' + (aClass == null ? null : aClass) + (notLast ? ", " : ">"); + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (!(o instanceof UIProvider)) return false; + + UIProvider that = (UIProvider) o; + return applicationName.equals(that.applicationName) && providerName.equals(that.providerName); + + } + + @Override + public int hashCode() { + return (31 * applicationName.hashCode()) + providerName.hashCode(); + } + +} \ No newline at end of file Deleted: trunk/lutinjaxx/util/src/main/java/org/codelutin/util/AbstractUIAction.java =================================================================== --- trunk/lutinjaxx/util/src/main/java/org/codelutin/util/AbstractUIAction.java 2008-07-08 21:14:13 UTC (rev 704) +++ trunk/lutinjaxx/util/src/main/java/org/codelutin/util/AbstractUIAction.java 2008-07-08 21:14:58 UTC (rev 705) @@ -1,53 +0,0 @@ -/** - * # #% Copyright (C) 2008 Code Lutin, Tony Chemit - * 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. - * # #% - */ -package org.codelutin.ui; - -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; - -/** @author chemit */ -public abstract class AbstractUIAction<H extends DialogUIHandler<?, ?>> extends javax.swing.AbstractAction { - - protected static Log log = LogFactory.getLog(AbstractUIAction.class); - - protected transient DialogUI<? extends H> ui; - - private static final long serialVersionUID = 1L; - - protected AbstractUIAction(String name, javax.swing.Icon icon, DialogUI<? extends H> ui) { - super(name, icon); - this.ui = ui; - } - - protected H getHandler() { - checkInit(); - return ui.getHandler(); - } - - protected void setUi(DialogUI<? extends H> ui) { - this.ui = ui; - } - - public DialogUI<? extends H> getUi() { - return ui; - } - - protected void checkInit() throws IllegalStateException { - /*if (ui == null) { - throw new IllegalStateException("no handler, nor ui referenced in " + this); - } */ - } - -} Deleted: trunk/lutinjaxx/util/src/main/java/org/codelutin/util/DialogUI.java =================================================================== --- trunk/lutinjaxx/util/src/main/java/org/codelutin/util/DialogUI.java 2008-07-08 21:14:13 UTC (rev 704) +++ trunk/lutinjaxx/util/src/main/java/org/codelutin/util/DialogUI.java 2008-07-08 21:14:58 UTC (rev 705) @@ -1,129 +0,0 @@ -/** - * ##% Copyright (C) 2008 Code Lutin, Tony Chemit - * 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. - * ##% - */ -package org.codelutin.ui; - -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; - -import javax.swing.AbstractAction; -import javax.swing.AbstractButton; -import javax.swing.ImageIcon; -import javax.swing.JDialog; -import java.awt.event.WindowEvent; -import java.awt.event.WindowListener; -import java.lang.reflect.Constructor; - -/** - * A abstract dialog contract to be realised by a dialogUI (WindowEvent adapter) - * <p/> - * TODO : make jaxx authorized implementing interface for root tag :) - * - * @author chemit - */ -public abstract class DialogUI<H extends DialogUIHandler> extends JDialog implements WindowListener { - - protected static Log log = LogFactory.getLog(DialogUI.class); - - public javax.swing.AbstractAction newAction(Class<?> actionClass, Object... params) { - Constructor<?> constructor = null; - for (Constructor<?> cons : actionClass.getConstructors()) { - Class<?>[] prototype = cons.getParameterTypes(); - if (prototype.length > 0 && DialogUI.class.isAssignableFrom(prototype[0])) { - // use this constructor - constructor = cons; - break; - } - } - if (constructor == null) { - throw new IllegalStateException("could not find a matching constructor for " + actionClass); - } - - // wrap params - Object[] parameters = new Object[1 + params.length]; - parameters[0] = this; - System.arraycopy(params, 0, parameters, 1, params.length); - try { - AbstractAction action = (AbstractAction) constructor.newInstance(parameters); - if (log.isInfoEnabled()) { - log.info(action); - } - return action; - } catch (Exception e) { - throw new IllegalStateException("could not init the action " + actionClass + " for reason : " + e.getMessage()); - } - } - - private H handler; - - public abstract AbstractButton getHelp(); - - public abstract Object getObjectById(java.lang.String s); - - protected DialogUI() { - UIHelper.setQuitAction(this); - addWindowListener(this); - //TODO will be handled by jaxx with javax.help... - //getHelp().setAction(newAction(HelpAction.class)); - } - - public H getHandler() { - return handler; - } - - public void setHandler(H handler) { - this.handler = handler; - } - - protected ImageIcon createActionIcon(String name) { - return UIHelper.createActionIcon(name); - } - - public void windowOpened(WindowEvent e) { - } - - public void windowClosed(WindowEvent e) { - } - - public void windowClosing(WindowEvent e) { - } - - public void windowIconified(WindowEvent e) { - } - - public void windowDeiconified(WindowEvent e) { - } - - public void windowActivated(WindowEvent e) { - } - - public void windowDeactivated(WindowEvent e) { - } - - @Override - public synchronized void addWindowListener(WindowListener l) { - super.addWindowListener(l); - if (log.isDebugEnabled()) { - log.debug("after added (" + getWindowListeners().length + ") : " + l); - } - } - - @Override - public synchronized void removeWindowListener(WindowListener l) { - super.removeWindowListener(l); - if (log.isDebugEnabled()) { - log.debug("after removed (" + getWindowListeners().length + ") : " + l); - } - } -} \ No newline at end of file Deleted: trunk/lutinjaxx/util/src/main/java/org/codelutin/util/DialogUIDef.java =================================================================== --- trunk/lutinjaxx/util/src/main/java/org/codelutin/util/DialogUIDef.java 2008-07-08 21:14:13 UTC (rev 704) +++ trunk/lutinjaxx/util/src/main/java/org/codelutin/util/DialogUIDef.java 2008-07-08 21:14:58 UTC (rev 705) @@ -1,235 +0,0 @@ -/** - * ##% Copyright (C) 2008 Code Lutin, Tony Chemit - * 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. - * ##% - */ -package org.codelutin.ui; - -import static org.codelutin.i18n.I18n._; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; - -import javax.swing.ImageIcon; -import java.lang.reflect.Constructor; - -/** - * Definition of an ui, with his model, handler and ui class definitions. - * <p/> - * The class contains also a shared instace of concrete ui. - * - * @author chemit - */ -public class DialogUIDef<M extends DialogUIModel, U extends DialogUI<H>, H extends DialogUIHandler<M, U>> implements java.io.Serializable { - - static protected final Log log = LogFactory.getLog(DialogUIDef.class); - - public static <M extends DialogUIModel, U extends DialogUI<H>, H extends DialogUIHandler<M, U>> DialogUIDef<M, U, H> newDef(Class<H> handlerClass, Class<U> uiClass, Class<M> modelClass, String showActionLibelle, String showActionTip, String uiTitle) { - DialogUIDef<M, U, H> result; - result = new DialogUIDef<M, U, H>(handlerClass, uiClass, modelClass, showActionLibelle, showActionTip, uiTitle); - return result; - } - - /** - * model class - */ - private final Class<M> modelClass; - - /** - * handler class - */ - private final Class<H> handlerClass; - - /** - * abstract ui class - */ - private final Class<U> uiClass; - - /** - * concrete lookup ui class - */ - private Class<? extends U> uiImplClass; - - /** - * shared instance of ui - */ - protected U uiInstance; - - /** - * unique name of ui def - */ - protected final String name; - - protected final String uiTitle; - protected final String showActionLibelle; - protected final String showActionTip; - - protected ImageIcon showUIActionIcon; - - - private static final long serialVersionUID = 1L; - - private DialogUIDef(Class<H> handlerClass, Class<U> uiClass, Class<M> modelClass, - String showActionLibelle, String showActionTip, String uiTitle) { - this.handlerClass = handlerClass; - this.uiClass = uiClass; - this.modelClass = modelClass; - this.showActionLibelle = showActionLibelle; - this.name = uiClass.getSimpleName().toLowerCase(); - this.showActionTip = showActionTip; - this.uiTitle = uiTitle; - } - - public Class<U> getUiClass() { - return uiClass; - } - - public Class<H> getHandlerClass() { - return handlerClass; - } - - public Class<M> getModelClass() { - return modelClass; - } - - public Class<? extends U> getUiImplClass() { - return uiImplClass; - } - - public String getUiTitle() { - return _(uiTitle); - } - - public String getShowActionLibelle() { - return _(showActionLibelle); - } - - public String getShowActionTip() { - return _(showActionTip); - } - - public ImageIcon getShowUIActionIcon() { - if (showUIActionIcon == null) { - showUIActionIcon = UIHelper.createActionIcon("show-" + name); - } - return showUIActionIcon; - } - - @SuppressWarnings({"unchecked"}) - public void setUiImplClass(Class<?> uiImplClass) { - this.uiImplClass = (Class<? extends U>) uiImplClass; - } - - @Override - public boolean equals(Object o) { - return this == o || o instanceof DialogUIDef && uiClass.equals(((DialogUIDef) o).uiClass); - } - - @Override - public int hashCode() { - return uiClass.hashCode(); - } - - @Override - public String toString() { - StringBuilder sb = new StringBuilder(super.toString()).append('<'); - sb.append(printClass("handler", handlerClass, true)); - sb.append(printClass("model", modelClass, true)); - sb.append(printClass("ui", uiClass, true)); - sb.append(printClass("uiImpl", uiImplClass, false)); - return sb.toString(); - } - - protected U getUiInstance() { - // no lazy instanciation, to control ui instanciation... - /*if (uiInstance == null) { - if (uiImplClass == null) { - throw new IllegalStateException("no concrete ui impl found in " + this); - } - synchronized (this) { - try { - uiInstance = uiImplClass.newInstance(); - } catch (Exception e) { - throw new IllegalStateException("could not instanciate ui " + this,e); - } - } - }*/ - return uiInstance; - } - - protected void setUiInstance(U uiInstance) { - this.uiInstance = uiInstance; - } - - protected U newUI() { - if (uiImplClass == null) { - throw new IllegalStateException("no concrete ui impl found in " + this); - } - try { - U result = uiImplClass.newInstance(); - log.info(result); - return result; - } catch (Exception e) { - throw new IllegalStateException("could not instanciate ui " + this, e); - } - } - - protected M newModel() { - if (modelClass == null) { - throw new IllegalStateException("no model impl found in " + this); - } - try { - M model = modelClass.newInstance(); - log.info(model); - return model; - } catch (Exception e) { - throw new IllegalStateException("could not instanciate ui " + this, e); - } - } - - protected H newHandler(U ui, M model, Object... params) { - if (handlerClass == null) { - throw new IllegalStateException("no handler impl found in " + this); - } - try { - Class[] prototype = getHandlerPrototype(params); - Object[] parameters = getHandlerParameters(ui, model, params); - H result = handlerClass.getConstructor(prototype).newInstance(parameters); - log.info(result); - return result; - } catch (Exception e) { - throw new IllegalStateException("could not instanciate ui " + this, e); - } - } - - protected Object[] getHandlerParameters(U ui, M model, Object[] params) { - Object[] result = new Object[2 + params.length]; - result[0] = ui; - result[1] = model; - System.arraycopy(params, 0, result, 2, params.length); - return result; - } - - protected Class[] getHandlerPrototype(Object[] params) { - int length = params.length; - for (Constructor<?> constructor : handlerClass.getConstructors()) { - Class<?>[] prototype = constructor.getParameterTypes(); - if (prototype.length == 2 + length && prototype[0] == uiClass && prototype[1] == modelClass) { - return prototype; - } - } - throw new IllegalStateException("could not find a matching constructor in " + handlerClass); - } - - protected String printClass(String s, Class<?> aClass, boolean notLast) { - return s + ':' + (aClass == null ? null : aClass.getSimpleName()) + (notLast ? ", " : ">"); - } -} Deleted: trunk/lutinjaxx/util/src/main/java/org/codelutin/util/DialogUIHandler.java =================================================================== --- trunk/lutinjaxx/util/src/main/java/org/codelutin/util/DialogUIHandler.java 2008-07-08 21:14:13 UTC (rev 704) +++ trunk/lutinjaxx/util/src/main/java/org/codelutin/util/DialogUIHandler.java 2008-07-08 21:14:58 UTC (rev 705) @@ -1,70 +0,0 @@ -/** - * ##% Copyright (C) 2008 Code Lutin, Tony Chemit - * 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. - * ##% - */ -package org.codelutin.ui; - -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; - -import java.awt.event.WindowListener; -import java.beans.PropertyChangeListener; - -/** - * DialogUI handler - * - * @author chemit - */ -public abstract class DialogUIHandler<M extends DialogUIModel, U extends DialogUI<? extends DialogUIHandler>> implements PropertyChangeListener { - - protected static Log log = LogFactory.getLog(DialogUIHandler.class); - - /** ui handled */ - private U ui; - - /** model handled */ - private M model; - - protected DialogUIHandler(U ui, M model) { - this.ui = ui; - this.model = model; - } - - public U getUi() { - return ui; - } - - public M getModel() { - return model; - } - - public void init() { - if (model == null) { - throw new IllegalStateException("no model was defined for " + this); - } - model.addPropertyChangeListener(this); - } - - public void dispose() { - model.dispose(); - for (WindowListener windowListener : getUi().getWindowListeners()) { - getUi().removeWindowListener(windowListener); - } - } - - @Override - protected void finalize() throws Throwable { - super.finalize(); - dispose(); - } -} \ No newline at end of file Deleted: trunk/lutinjaxx/util/src/main/java/org/codelutin/util/DialogUIModel.java =================================================================== --- trunk/lutinjaxx/util/src/main/java/org/codelutin/util/DialogUIModel.java 2008-07-08 21:14:13 UTC (rev 704) +++ trunk/lutinjaxx/util/src/main/java/org/codelutin/util/DialogUIModel.java 2008-07-08 21:14:58 UTC (rev 705) @@ -1,97 +0,0 @@ -/** - * ##% Copyright (C) 2008 Code Lutin, Tony Chemit - * 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. - * ##% - */ -package org.codelutin.ui; - -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; - -import java.beans.PropertyChangeListener; -import java.beans.PropertyChangeSupport; - -/** - * Abstract ui model, with property change support. - * - * @author chemit - */ -public abstract class DialogUIModel { - - static protected final Log log = LogFactory.getLog(DialogUIModel.class); - - /** support for change properties support */ - protected PropertyChangeSupport changeSupport; - - public synchronized void addPropertyChangeListener(String propertyName, PropertyChangeListener listener) { - if (listener == null) { - return; - } - if (changeSupport == null) { - changeSupport = new PropertyChangeSupport(this); - } - changeSupport.addPropertyChangeListener(propertyName, listener); - } - - public synchronized void addPropertyChangeListener(PropertyChangeListener listener) { - if (listener == null) { - return; - } - if (changeSupport == null) { - changeSupport = new PropertyChangeSupport(this); - } - changeSupport.addPropertyChangeListener(listener); - } - - public synchronized void removePropertyChangeListener(PropertyChangeListener listener) { - if (listener == null || changeSupport == null) { - return; - } - changeSupport.removePropertyChangeListener(listener); - } - - public synchronized void removePropertyChangeListeners() { - if (changeSupport == null) { - return; - } - for (PropertyChangeListener listener : getPropertyChangeListeners()) { - changeSupport.removePropertyChangeListener(listener); - } - } - - public synchronized PropertyChangeListener[] getPropertyChangeListeners() { - if (changeSupport == null) { - return new PropertyChangeListener[0]; - } - return changeSupport.getPropertyChangeListeners(); - } - - public void firePropertyChange(String propertyName, Object oldValue, Object newValue) { - if (changeSupport == null || (oldValue == null && newValue == null) || - (oldValue != null && oldValue.equals(newValue))) { - return; - } - changeSupport.firePropertyChange(propertyName, oldValue, newValue); - } - - public void dispose() { - for (PropertyChangeListener listener : changeSupport.getPropertyChangeListeners()) { - changeSupport.removePropertyChangeListener(listener); - } - } - - @Override - protected void finalize() throws Throwable { - super.finalize(); - dispose(); - } -} \ No newline at end of file Deleted: trunk/lutinjaxx/util/src/main/java/org/codelutin/util/FactoryWindowListener.java =================================================================== --- trunk/lutinjaxx/util/src/main/java/org/codelutin/util/FactoryWindowListener.java 2008-07-08 21:14:13 UTC (rev 704) +++ trunk/lutinjaxx/util/src/main/java/org/codelutin/util/FactoryWindowListener.java 2008-07-08 21:14:58 UTC (rev 705) @@ -1,86 +0,0 @@ -/** - * # #% Copyright (C) 2008 Code Lutin, Tony Chemit - * 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. - * # #% - */ -package org.codelutin.ui; - -import static org.codelutin.ui.UIFactory.log; - -import java.awt.event.WindowAdapter; -import java.awt.event.WindowEvent; - -/** - * A windowListenr for ui managed by {@link org.codelutin.ui.UIFactory}. - * <p/> - * To be used when all ui from factory are closed, via {@link #allWindowsClosed(java.awt.event.WindowEvent)} method. - * - * @author chemit - */ -public abstract class FactoryWindowListener extends WindowAdapter { - - /** - * method to be invoked when all ui registred in factory are really disposed. - * - * @param e event - */ - public abstract void allWindowsClosed(WindowEvent e); - - /** underlying factory of ui */ - private UIFactory factory; - - /** flag to make sure {@link #allWindowsClosed(java.awt.event.WindowEvent)} is called only once. */ - private boolean wasClosed; - - @Override - public void windowClosed(WindowEvent e) { - if (log.isDebugEnabled()) { - log.debug(this + " : " + e); - } - if (e.getWindow().isVisible()) { - // only deal with real closed and none visible windows... - return; - } - for (DialogUIDef def : factory.getDefs()) { - DialogUI ui = def.uiInstance; - if (ui != null && ui.isVisible()) { - // at least one ui visible, do not close all - return; - } - } - - if (wasClosed) { - // make sure to process only once - return; - } - if (log.isInfoEnabled()) { - log.info("closing factory listener " + this); - } - - synchronized (this) { - try { - allWindowsClosed(e); - } finally { - wasClosed = true; - factory.removeFactoryWindowListener(this); - } - } - } - - protected UIFactory getFactory() { - return factory; - } - - protected void setFactory(UIFactory factory) { - this.factory = factory; - } -} Deleted: trunk/lutinjaxx/util/src/main/java/org/codelutin/util/FormElement.java =================================================================== --- trunk/lutinjaxx/util/src/main/java/org/codelutin/util/FormElement.java 2008-07-08 21:14:13 UTC (rev 704) +++ trunk/lutinjaxx/util/src/main/java/org/codelutin/util/FormElement.java 2008-07-08 21:14:58 UTC (rev 705) @@ -1,29 +0,0 @@ -/** - * # #% Copyright (C) 2008 Code Lutin, Tony Chemit - * 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. - * # #% - */ -package org.codelutin.ui; - -/** @author chemit */ -public interface FormElement<U extends DialogUI> { - - String name(); - - int ordinal(); - - Object getValue(U ui); - - void setValue(U ui, String value); - - javax.swing.JLabel getLabel(U ui); -} Deleted: trunk/lutinjaxx/util/src/main/java/org/codelutin/util/ShowUIAction.java =================================================================== --- trunk/lutinjaxx/util/src/main/java/org/codelutin/util/ShowUIAction.java 2008-07-08 21:14:13 UTC (rev 704) +++ trunk/lutinjaxx/util/src/main/java/org/codelutin/util/ShowUIAction.java 2008-07-08 21:14:58 UTC (rev 705) @@ -1,135 +0,0 @@ -/** - * # #% Copyright (C) 2008 Code Lutin, Tony Chemit - * 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. - * # #% - */ -package org.codelutin.ui; - -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; - -import java.awt.Dimension; -import java.awt.Point; -import java.awt.event.ActionEvent; - -/** @author chemit */ -public abstract class ShowUIAction<M extends DialogUIModel, U extends DialogUI<H>, H extends DialogUIHandler<M, U>> extends javax.swing.AbstractAction { - - protected static Log log = LogFactory.getLog(AbstractUIAction.class); - - protected transient DialogUI<?> ui; - - private static final long serialVersionUID = 1L; - - protected DialogUIDef<M, U, H> uiDef; - - protected transient UIFactory factory; - - protected String position; - - protected boolean undecorated = true; - - protected U initUI(ActionEvent e) { - return getFactory().getUI(uiDef); - } - - public ShowUIAction(DialogUI<?> ui, DialogUIDef<M, U, H> uiDef, UIFactory factory, boolean showText) { - super(uiDef.getShowActionLibelle(), uiDef.getShowUIActionIcon()); - this.ui = ui; - this.uiDef = uiDef; - String name = (String) getValue(NAME); - putValue(DISPLAYED_MNEMONIC_INDEX_KEY, name.length() - 1); - putValue(ACCELERATOR_KEY, (int) name.charAt(name.length() - 1)); - if (!showText) { - putValue(NAME, null); - } - putValue(SHORT_DESCRIPTION, uiDef.getShowActionTip()); - this.factory = factory; - } - - public DialogUI<?> getUi() { - return ui; - } - - public DialogUIDef<M, U, H> getUiDef() { - return uiDef; - } - - public UIFactory getFactory() { - return factory; - } - - public void setUiDef(DialogUIDef<M, U, H> uiDef) { - this.uiDef = uiDef; - } - - public void setPosition(String position) { - this.position = position; - } - - public void setUndecorated(boolean undecorated) { - this.undecorated = undecorated; - } - - public void actionPerformed(java.awt.event.ActionEvent e) { - checkInit(); - U ui = initUI(e); - ui.setTitle(uiDef.getUiTitle()); - log.info(ui.getTitle()); - //TODO ui.setUndecorated(undecorated); - setPosition(this.getUi(), ui, position); - - ui.setVisible(true); - } - - protected void setPosition(javax.swing.JDialog parentUI, javax.swing.JDialog ui, String position) { - if (position == null || parentUI == null) { - return; - } - Point parentLocation = parentUI.getLocationOnScreen(); - Dimension parentSize = parentUI.getSize(); - - if (position.equals("bottom-left")) { - int top = (int) (parentLocation.getY() + parentSize.getHeight()); - int left = (int) (parentLocation.getX()); - Point newLocation = new Point(left, top); - newLocation.setLocation(left, top); - ui.setLocation(newLocation); - return; - } - if (position.equals("top-left")) { - int top = (int) (parentLocation.getY()); - int left = (int) (parentLocation.getX()); - Point newLocation = new Point(left, top); - newLocation.setLocation(left, top); - ui.setLocation(newLocation); - return; - } - if (position.equals("top-right")) { - int top = (int) (parentLocation.getY()); - int left = (int) (parentLocation.getX() + parentSize.getWidth()); - Point newLocation = new Point(left, top); - newLocation.setLocation(left, top); - ui.setLocation(newLocation); - return; - } - if (position.equals(("center"))) { - //TODO - } - } - - protected void checkInit() throws IllegalStateException { - if (factory == null) { - throw new IllegalStateException("no factory found in " + this); - } - } -} \ No newline at end of file Deleted: trunk/lutinjaxx/util/src/main/java/org/codelutin/util/UIFactory.java =================================================================== --- trunk/lutinjaxx/util/src/main/java/org/codelutin/util/UIFactory.java 2008-07-08 21:14:13 UTC (rev 704) +++ trunk/lutinjaxx/util/src/main/java/org/codelutin/util/UIFactory.java 2008-07-08 21:14:58 UTC (rev 705) @@ -1,184 +0,0 @@ -/** -/** - * ##% Copyright (C) 2008 Code Lutin, Tony Chemit - * 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. - * ##% - */ -package org.codelutin.ui; - -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; -import org.codelutin.util.StringUtil; - -import javax.swing.event.EventListenerList; -import java.util.ArrayList; -import java.util.List; -import java.util.ServiceLoader; - -/** - * Factory for UI, using a cache and a provider to find ui implementations. - * - * @author chemit - */ -public class UIFactory { - - static protected final Log log = LogFactory.getLog(UIFactory.class); - - private final String applicationName; - - private final DialogUIDef[] defs; - - private final EventListenerList listeners; - - public UIFactory(String applicationName, DialogUIDef[] defs, FactoryWindowListener... listeners) { - this.applicationName = applicationName; - this.listeners = new EventListenerList(); - for (FactoryWindowListener listener : listeners) { - listener.setFactory(this); - addFactoryWindowListener(listener); - } - this.defs = defs; - long t0 = System.nanoTime(); - if (log.isDebugEnabled()) { - log.debug("start at " + new java.util.Date()); - } - try { - init(); - } catch (Exception e) { - log.error(e); - throw new RuntimeException(e); - } finally { - if (log.isDebugEnabled()) { - log.info("end in " + StringUtil.convertTime(t0, System.nanoTime())); - } - } - } - - public void addFactoryWindowListener(FactoryWindowListener l) { - listeners.add(FactoryWindowListener.class, l); - if (log.isDebugEnabled()) { - log.debug("after added (" + listeners.getListenerCount() + ") : " + l); - } - } - - public void removeFactoryWindowListener(FactoryWindowListener l) { - listeners.remove(FactoryWindowListener.class, l); - for (DialogUIDef def : getDefs()) { - if (def.uiInstance != null) { - def.uiInstance.removeWindowListener(l); - } - } - if (log.isDebugEnabled()) { - log.debug(" after removed (" + listeners.getListenerCount() + ") : " + l); - } - if (listeners.getListenerCount(FactoryWindowListener.class) == 0) { - // close for real factory - close(); - } - } - - public void close() { - log.info(this + " at " + new java.util.Date()); - for (DialogUIDef<?, ?, ?> def : defs) { - DialogUI<?> ui = def.uiInstance; - if (ui != null) { - ui.getHandler().dispose(); - def.uiInstance = null; - } - } - if (listeners.getListenerCount(FactoryWindowListener.class) > 0) { - log.warn("some listeners where not properly removed, force deletion..."); - for (FactoryWindowListener listener : listeners.getListeners(FactoryWindowListener.class)) { - removeFactoryWindowListener(listener); - } - } - - } - - protected void init() { - - UIProvider[] providers = detectProviders(); - - for (DialogUIDef<?, ?, ?> def : defs) { - initDef(providers, def); - if (def.getUiImplClass() == null) { - throw new IllegalStateException("could not find implementation for ui def " + def); - } - } - } - - protected void initDef(UIProvider[] providers, DialogUIDef<?, ?, ?> def) { - for (UIProvider provider : providers) { - Class<?> uiImplClass = provider.findUIImplementation(def); - if (uiImplClass != null) { - if (log.isDebugEnabled()) { - log.debug("init done for " + def); - } - // ui implementation was found - break; - } - } - } - - protected UIProvider[] detectProviders() { - long t0 = System.nanoTime(); - List<UIProvider> providers = new ArrayList<UIProvider>(); - for (UIProvider provider : ServiceLoader.load(UIProvider.class)) { - if (applicationName.equals(provider.getApplicationName())) { - if (log.isDebugEnabled()) { - log.debug("provider detected [" + provider + ']'); - } - providers.add(provider); - } - } - log.info("found " + providers.size() + " ui provider(s) in " + StringUtil.convertTime(t0, System.nanoTime()) + " : " + providers); - return providers.toArray(new UIProvider[providers.size()]); - } - - protected DialogUIDef[] getDefs() { - return defs; - } - - @Override - protected void finalize() throws Throwable { - super.finalize(); - close(); - } - - public <M extends DialogUIModel, U extends DialogUI<H>, H extends DialogUIHandler<M, U>> U getUI(DialogUIDef<M, U, H> uiType, Object... params) { - U ui = uiType.uiInstance; - if (ui == null) { - try { - ui = uiType.newUI(); - M model = uiType.newModel(); - H handler = uiType.newHandler(ui, model, params); - registerUI(uiType, ui, handler); - } catch (Exception e) { - throw new IllegalStateException("could not instanciate ui handler " + uiType + " for reason : " + e.getMessage(), e); - } - } - return ui; - } - - protected <M extends DialogUIModel, U extends DialogUI<H>, H extends DialogUIHandler<M, U>> void registerUI(DialogUIDef<M, U, H> uiType, U ui, H handler) { - ui.setHandler(handler); - handler.init(); - uiType.setUiInstance(ui); - for (FactoryWindowListener listener : listeners.getListeners(FactoryWindowListener.class)) { - if (log.isDebugEnabled()) { - log.debug("----- addFactoryWindowListener " + listener + " to " + ui); - } - ui.addWindowListener(listener); - } - } - -} \ No newline at end of file Deleted: trunk/lutinjaxx/util/src/main/java/org/codelutin/util/UIHelper.java =================================================================== --- trunk/lutinjaxx/util/src/main/java/org/codelutin/util/UIHelper.java 2008-07-08 21:14:13 UTC (rev 704) +++ trunk/lutinjaxx/util/src/main/java/org/codelutin/util/UIHelper.java 2008-07-08 21:14:58 UTC (rev 705) @@ -1,70 +0,0 @@ -/* -* ##% Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008 Code Lutin, -* Tony Chemit -* -* 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. -* ##% */ -package org.codelutin.ui; - -import javax.swing.AbstractAction; -import javax.swing.Action; -import javax.swing.ImageIcon; -import javax.swing.JComponent; -import javax.swing.JDialog; -import javax.swing.JRootPane; -import javax.swing.KeyStroke; -import java.awt.event.ActionEvent; - -/** - * Ui helper class. - * - * @author tony - */ -public class UIHelper { - - public static ImageIcon createImageIcon(String path) { - java.net.URL imgURL = UIHelper.class.getResource("/icons/" + path); - if (imgURL != null) { - return new ImageIcon(imgURL); - } else { - throw new IllegalArgumentException("could not find icon " + path); - } - } - - /** - * Attach to <code>ui</code> an abort action,accessible by <code>ESC</code> key. - * - * @param ui ui - */ - public static void setQuitAction(final JDialog ui) { - JRootPane rootPane = ui.getRootPane(); - - Action quitAction = new AbstractAction("quit") { - private static final long serialVersionUID = -869095664995763057L; - - public void actionPerformed(ActionEvent e) { - ui.dispose(); - } - }; - rootPane.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke("ESCAPE"), "quit"); - rootPane.getActionMap().put("quit", quitAction); - ui.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); - } - - public static ImageIcon createActionIcon(String name) { - return createImageIcon("action-" + name + ".png"); - } - -} Deleted: trunk/lutinjaxx/util/src/main/java/org/codelutin/util/UIProvider.java =================================================================== --- trunk/lutinjaxx/util/src/main/java/org/codelutin/util/UIProvider.java 2008-07-08 21:14:13 UTC (rev 704) +++ trunk/lutinjaxx/util/src/main/java/org/codelutin/util/UIProvider.java 2008-07-08 21:14:58 UTC (rev 705) @@ -1,86 +0,0 @@ -/** - * # #% Copyright (C) 2008 Code Lutin, Tony Chemit - * 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. - * # #% - */ -package org.codelutin.ui; - -/** @author chemit */ -public abstract class UIProvider { - - /** the name of application using this provider */ - protected String applicationName; - - /** the name of ui implementation used by this provider */ - protected String providerName; - - /** array of ui implementations */ - protected Class<?>[] implementations; - - protected UIProvider(String applicationName, String providerName, Class<?>... implementations) { - this.applicationName = applicationName; - this.providerName = providerName; - this.implementations = implementations; - } - - public String getProviderName() { - return providerName; - } - - public String getApplicationName() { - return applicationName; - } - - public Class<?>[] getImplementations() { - return implementations; - } - - public Class<?> findUIImplementation(DialogUIDef<?, ?, ?> def) { - Class<? extends DialogUI<?>> uiClass = def.getUiClass(); - for (Class<?> klass : implementations) { - if (uiClass.isAssignableFrom(klass)) { - def.setUiImplClass(klass); - return klass; - } - } - return null; - } - - @Override - public String toString() { - StringBuilder sb = new StringBuilder(super.toString()).append('<'); - sb.append(printClass("application", applicationName, true)); - sb.append(printClass("provider", providerName, true)); - sb.append(printClass("uis", implementations.length, false)); - return sb.toString(); - } - - protected String printClass(String s, Object aClass, boolean notLast) { - return s + ':' + (aClass == null ? null : aClass) + (notLast ? ", " : ">"); - } - - @Override - public boolean equals(Object o) { - if (this == o) return true; - if (!(o instanceof UIProvider)) return false; - - UIProvider that = (UIProvider) o; - return applicationName.equals(that.applicationName) && providerName.equals(that.providerName); - - } - - @Override - public int hashCode() { - return (31 * applicationName.hashCode()) + providerName.hashCode(); - } - -} \ No newline at end of file