From kmorin@users.nuiton.org Tue Dec 11 17:51:04 2012 From: kmorin@users.nuiton.org To: jaxx-commits@list.nuiton.org Subject: [Jaxx-commits] r2529 - trunk/jaxx-widgets/src/main/java/jaxx/runtime/swing/editor/bean Date: Tue, 11 Dec 2012 17:51:04 +0100 Message-ID: <20121211165104.CF7321593A@nuiton.codelutin.com> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="===============2360972978212335235==" --===============2360972978212335235== Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Author: kmorin Date: 2012-12-11 17:51:04 +0100 (Tue, 11 Dec 2012) New Revision: 2529 Url: http://nuiton.org/projects/jaxx/repository/revisions/2529 Log: refs #2474 Create a double list widget Modified: trunk/jaxx-widgets/src/main/java/jaxx/runtime/swing/editor/bean/BeanDouble= List.jaxx trunk/jaxx-widgets/src/main/java/jaxx/runtime/swing/editor/bean/BeanDouble= ListHandler.java trunk/jaxx-widgets/src/main/java/jaxx/runtime/swing/editor/bean/BeanDouble= ListModel.java Modified: trunk/jaxx-widgets/src/main/java/jaxx/runtime/swing/editor/bean/Bea= nDoubleList.jaxx =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D --- trunk/jaxx-widgets/src/main/java/jaxx/runtime/swing/editor/bean/BeanDoubl= eList.jaxx 2012-12-10 17:58:51 UTC (rev 2528) +++ trunk/jaxx-widgets/src/main/java/jaxx/runtime/swing/editor/bean/BeanDoubl= eList.jaxx 2012-12-11 16:51:04 UTC (rev 2529) @@ -11,16 +11,20 @@ =20 + - =20 + =20 + =20 - + + + @@ -30,25 +34,20 @@ - + + + =20 - =20 - - =20 =20 \ No newline at end of file Modified: trunk/jaxx-widgets/src/main/java/jaxx/runtime/swing/editor/bean/Bea= nDoubleListHandler.java =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D --- trunk/jaxx-widgets/src/main/java/jaxx/runtime/swing/editor/bean/BeanDoubl= eListHandler.java 2012-12-10 17:58:51 UTC (rev 2528) +++ trunk/jaxx-widgets/src/main/java/jaxx/runtime/swing/editor/bean/BeanDoubl= eListHandler.java 2012-12-11 16:51:04 UTC (rev 2529) @@ -4,8 +4,6 @@ import java.awt.event.ActionListener; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; -import java.awt.event.MouseListener; -import javax.swing.event.ListSelectionEvent; import org.nuiton.util.decorator.JXPathDecorator; import org.nuiton.util.decorator.MultiJXPathDecorator; import jaxx.runtime.swing.renderer.DecoratorListCellRenderer; @@ -15,86 +13,71 @@ import javax.swing.JList; import java.beans.PropertyChangeEvent; import java.beans.PropertyChangeListener; -import java.util.ArrayList; import java.util.Arrays; import java.util.List; -import javax.swing.DefaultListModel; -import javax.swing.ListModel; -import javax.swing.event.ListSelectionListener; -import jaxx.runtime.SwingUtil; =20 /** - * Le handler d'un {@link BeanDoubleList}. + * The handler of a {@link BeanDoubleList}. *

* * @author kmorin - * @param le type des objet contenus dans le mod=C3=A8le du composant. + * @param the type of the objects contained in the list. * @see BeanDoubleList * @since 2.5.8 */ public class BeanDoubleListHandler implements PropertyChangeListener { =20 - public static final Log log =3D LogFactory.getLog(BeanDoubleListHandler.= class); + private static final Log log =3D LogFactory.getLog(BeanDoubleListHandler= .class); =20 - /** ui if the handler */ + /** ui of the handler */ protected BeanDoubleList ui; =20 /** the decorator of data */ protected MultiJXPathDecorator decorator; =20 - /** flag to mark when handler was init (it can be init only once). */ - protected boolean init; - =20 public BeanDoubleListHandler(BeanDoubleList ui) { this.ui =3D ui; } =20 /** - * Initialise le handler de l'ui - * - * @param decorator le decorateur a utiliser - * @param data la liste des donn=C3=A9es a g=C3=A9rer + * Initializes the handler of the UI + * @param decorator the decorator to use to display the data nicely + * @param universe the list of all the available items + * @param selected the list of selected items */ - public void init(JXPathDecorator decorator, List data) { - if (init) { - throw new IllegalStateException("can not init the handler twice"= ); - } - init =3D true; - =20 - ui.getModel().setUniverse(data); - -// // set datas -// ui.getDoubleListHeader().setBeanType(ui.getBeanType()); -// ui.getDoubleListHeader().setLabelText(ui.getLabelText()); -// ui.getDoubleListHeader().init(decorator, data); - -// // list could have changed and the complex binding is not registre= d... -// ui.removeDataBinding(BeanDoubleListHandler.BINDING_RESET_SELECTION= _ENABLED); -// ui.applyDataBinding(BeanDoubleListHandler.BINDING_RESET_SELECTION_= ENABLED); - + public void init(JXPathDecorator decorator, List universe, List= selected) { this.decorator =3D BeanUIUtil.createDecorator(decorator); -// - // init combobox renderer base on given decorator + =20 + initModel(universe, selected); + =20 final JList universeList =3D ui.getUniverseList(); universeList.setCellRenderer(new DecoratorListCellRenderer(this.deco= rator)); - SwingUtil.fillList(universeList, data, null); - =20 final JList selectedList =3D ui.getSelectedList(); selectedList.setCellRenderer(new DecoratorListCellRenderer(this.deco= rator)); =20 -// // build popup -// popupHandler.preparePopup(ui.getSelectedToolTipText(), -// ui.getNotSelectedToolTipText(), -// ui.getI18nPrefix(), -// ui.getPopupTitleText(), -// indexes, -// ui.getPopupSeparator(), -// ui.getPopupLabel(), -// ui.getSortUp(), -// ui.getSortDown(), -// this.decorator); -// + initListeners(); =20 + ui.addPropertyChangeListener(this); + + } + + /** + * Initialize the model of the {@link BeanDoubleList} + * @param universe the list of all the available items + * @param selected the selected items + */ + protected void initModel(List universe, List selected) { + ui.getModel().setUniverse(universe); + ui.getModel().setSelected(selected); + } + =20 + /** + * Initializes the listeners. + */ + protected void initListeners() { + final JList universeList =3D ui.getUniverseList(); + final JList selectedList =3D ui.getSelectedList(); + =20 universeList.addMouseListener(new MouseAdapter() { =20 public void mouseClicked(MouseEvent event) { @@ -132,26 +115,10 @@ ui.getModel().removeFromSelected(selection); } }); - =20 - ui.addPropertyChangeListener(this); - } - =20 - protected void switchElement(MouseEvent event, JList container, JList re= ceiver) { - int index =3D container.locationToIndex(event.getPoint()); - DefaultListModel containerModel =3D (DefaultListModel)container.getM= odel(); - Object item =3D containerModel.remove(index); =20 - DefaultListModel receiverModel =3D (DefaultListModel)receiver.getMod= el(); - receiverModel.addElement(item); - } - @Override public void propertyChange(PropertyChangeEvent evt) { } =20 - public O getSelectedValue() { - JList list =3D ui.getUniverseList(); - return list =3D=3D null ? null : (O) list.getSelectedValue(); - } } Modified: trunk/jaxx-widgets/src/main/java/jaxx/runtime/swing/editor/bean/Bea= nDoubleListModel.java =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D --- trunk/jaxx-widgets/src/main/java/jaxx/runtime/swing/editor/bean/BeanDoubl= eListModel.java 2012-12-10 17:58:51 UTC (rev 2528) +++ trunk/jaxx-widgets/src/main/java/jaxx/runtime/swing/editor/bean/BeanDoubl= eListModel.java 2012-12-11 16:51:04 UTC (rev 2529) @@ -4,36 +4,80 @@ import java.util.List; import javax.swing.DefaultListModel; import javax.swing.ListModel; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; =20 /** - * The model of the BeanDoubleList widget + * The model of the {@link BeanDoubleList} widget + * @param the type of the object in the list * @author kmorin - * @since 2.5.8 + * @see BeanDoubleList + * @since 2.5.8=20 */ public class BeanDoubleListModel { =20 + private static final Log log =3D LogFactory.getLog(BeanDoubleListModel.c= lass); + =20 + /** List of all the available items */ protected List universe =3D new ArrayList(); =20 + /** Model containing the remaining available items */ protected ListModel universeModel =3D new DefaultListModel(); =20 + /** List of the selected items */ protected List selected =3D new ArrayList(); =20 + /** Model containing the selected items */ protected ListModel selectedModel =3D new DefaultListModel(); - + =20 + /** + * To get the selected items. + * @return a list of O + */ public List getSelected() { return selected; } =20 + /** + * Sets the list of selected items.=20 + * It fills the model of the list of the selected items with these items + * and removes them from the model of list of the universe. + * @param selected a list of O + */ public void setSelected(List selected) { - this.selected =3D selected; + if (selected =3D=3D null) { + selected =3D new ArrayList(); + } + this.selected =3D new ArrayList(selected); + ((DefaultListModel)selectedModel).clear(); + for (O item : selected) { + ((DefaultListModel)selectedModel).addElement(item); + ((DefaultListModel)universeModel).removeElement(item); + } } =20 + /** + * To get all the available items. + * @return a list of O + */ public List getUniverse() { return universe; } =20 + /** + * Sets the list of the available items.=20 + * It fills the model of the universe list with these items. + * @param universe a list of O + */ public void setUniverse(List universe) { - this.universe =3D universe; + if (universe =3D=3D null) { + universe =3D new ArrayList(); + } + this.universe =3D new ArrayList(universe); + ((DefaultListModel)universeModel).clear(); + for (O item : universe) { + ((DefaultListModel)universeModel).addElement(item); + } } =20 public ListModel getSelectedModel() { @@ -52,12 +96,20 @@ this.universeModel =3D universeModel; } =20 + /** + * Adds an item to the selected items. + * @param item the item to select + */ public void addToSelected(O item) { selected.add(item); ((DefaultListModel)selectedModel).addElement(item); ((DefaultListModel)universeModel).removeElement(item); } =20 + /** + * Adds a list of items to the selected items. + * @param items the list of the items to select + */ public void addToSelected(List items) { selected.addAll(items); for (O item : items) { @@ -66,18 +118,56 @@ } } =20 + /** + * Removes an item from the selected items. + * @param item the item to unselect + */ public void removeFromSelected(O item) { selected.remove(item); + log.info(selectedModel.getClass()); ((DefaultListModel)selectedModel).removeElement(item); - ((DefaultListModel)universeModel).addElement(item); + addToUniverseList(item); } =20 - public void removeFromSelected(List items){ + /** + * Removes a list of items from the list of selected items. + * @param items the list of the items to unselect + */ + public void removeFromSelected(List items) { selected.removeAll(items); for (O item : items) { ((DefaultListModel)selectedModel).removeElement(item); - ((DefaultListModel)universeModel).addElement(item); + addToUniverseList(item); } } =20 + /** + * Adds an item to the available items list at the right index + * to keep always the same order. + * @param item the item to add to the universe list + */ + protected void addToUniverseList(O item) { + DefaultListModel universeDefaultListModel =3D (DefaultListModel)univ= erseModel; + // the maximum index where we should insert the item is its index=20 + // in the list of all the available items=20 + int index =3D universe.indexOf(item); + // if the index is upper than the size of the list of the remaining = available items, + // get the size of this list + int insertionIndex =3D Math.min(index, universeDefaultListModel.getS= ize()); + =20 + // we decrease the index to insert until we meet an item whose index + // in the list of all the available index is lower than the one of t= he item + // we want to insert + while (insertionIndex > 0) { + O o =3D (O)universeDefaultListModel.get(--insertionIndex); + int oIndex =3D universe.indexOf(o); + if (oIndex < index) { + insertionIndex++; + break; + } + }; + =20 + universeDefaultListModel.add(insertionIndex, item); + } + =20 } --===============2360972978212335235==--