From sletellier@users.nuiton.org Thu Oct 1 12:03:16 2009 From: sletellier@users.nuiton.org To: jaxx-commits@list.nuiton.org Subject: [Jaxx-commits] r1541 - in trunk: jaxx-example/src/main/java/jaxx/demo jaxx-runtime-swing/src/main/java/jaxx/runtime/swing jaxx-runtime-swing/src/main/java/jaxx/runtime/swing/navigation Date: Thu, 01 Oct 2009 12:03:16 +0200 Message-ID: <20091001100316.4FE4F16688@nuiton.codelutin.com> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="===============5399057485467889135==" --===============5399057485467889135== Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Author: sletellier Date: 2009-10-01 12:03:15 +0200 (Thu, 01 Oct 2009) New Revision: 1541 Added: trunk/jaxx-example/src/main/java/jaxx/demo/ExempleItemTreeNavigationAdapte= r.java trunk/jaxx-example/src/main/java/jaxx/demo/ItemTreeNavigationDemo.jaxx trunk/jaxx-runtime-swing/src/main/java/jaxx/runtime/swing/CardLayout3.java trunk/jaxx-runtime-swing/src/main/java/jaxx/runtime/swing/navigation/ItemN= avigationCardPanel.java trunk/jaxx-runtime-swing/src/main/java/jaxx/runtime/swing/navigation/ItemT= reeNavigationAdapter.java Modified: trunk/jaxx-example/src/main/java/jaxx/demo/JAXXDemo.jaxx Log: Ajout d'un systeme d'arbre de navigation base sur les items Added: trunk/jaxx-example/src/main/java/jaxx/demo/ExempleItemTreeNavigationAd= apter.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-example/src/main/java/jaxx/demo/ExempleItemTreeNavigationAdapt= er.java (rev 0) +++ trunk/jaxx-example/src/main/java/jaxx/demo/ExempleItemTreeNavigationAdapt= er.java 2009-10-01 10:03:15 UTC (rev 1541) @@ -0,0 +1,28 @@ +package jaxx.demo; + +import java.util.Date; +import jaxx.runtime.JAXXObject; +import jaxx.runtime.swing.JAXXTree; +import jaxx.runtime.swing.navigation.ItemNavigationCardPanel; +import jaxx.runtime.swing.navigation.ItemTreeNavigationAdapter; + +/** + * + * @author letellier + */ +public class ExempleItemTreeNavigationAdapter extends ItemTreeNavigationAdap= ter{ + + public ExempleItemTreeNavigationAdapter(JAXXObject context, JAXXTree tre= e, ItemNavigationCardPanel cardPanel){ + super(context, tree, cardPanel); + } + =20 + @Override + protected void valueChanged(Object data) { + if (data instanceof String){ + ((ItemTreeNavigationDemo)context).setString((String)data); + } else if (data instanceof Date){ + ((ItemTreeNavigationDemo)context).setDate((Date)data); + } + } + +} Added: trunk/jaxx-example/src/main/java/jaxx/demo/ItemTreeNavigationDemo.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-example/src/main/java/jaxx/demo/ItemTreeNavigationDemo.jaxx = (rev 0) +++ trunk/jaxx-example/src/main/java/jaxx/demo/ItemTreeNavigationDemo.jaxx 20= 09-10-01 10:03:15 UTC (rev 1541) @@ -0,0 +1,32 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file Modified: trunk/jaxx-example/src/main/java/jaxx/demo/JAXXDemo.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-example/src/main/java/jaxx/demo/JAXXDemo.jaxx 2009-09-30 13:40= :43 UTC (rev 1540) +++ trunk/jaxx-example/src/main/java/jaxx/demo/JAXXDemo.jaxx 2009-10-01 10:03= :15 UTC (rev 1541) @@ -83,6 +83,7 @@ + @@ -118,6 +119,7 @@ + Added: trunk/jaxx-runtime-swing/src/main/java/jaxx/runtime/swing/CardLayout3.= 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-runtime-swing/src/main/java/jaxx/runtime/swing/CardLayout3.jav= a (rev 0) +++ trunk/jaxx-runtime-swing/src/main/java/jaxx/runtime/swing/CardLayout3.jav= a 2009-10-01 10:03:15 UTC (rev 1541) @@ -0,0 +1,301 @@ +package jaxx.runtime.swing; + +import java.awt.CardLayout; +import java.awt.Component; +import java.awt.Container; +import java.io.Serializable; +import java.util.Vector; + +/** + * An override of the awt {@link java.awt.CardLayout}. + *

+ * Because in the original layout is not overridable : everything is package= level accessible. + *

+ * This new class allow using Object for constrains, and not only String + * + * @author letellier + * @version 1.0 + */ +public class CardLayout3 extends CardLayout{ + + /* + * Index of Component currently displayed by CardLayout. + */ + int currentCard =3D 0; + + /* + * This creates a Vector to store associated + * pairs of components and their names. + * @see java.util.Vector + */ + Vector vector =3D new Vector(); + + /* + * A pair of Component and String that represents its name. + */ + class Card implements Serializable { + static final long serialVersionUID =3D 6640330810709497518L; + public Object constraint; + public Component comp; + public Card(Object constraint, Component cardComponent) { + this.constraint =3D constraint; + this.comp =3D cardComponent; + } + } + + /** + * Creates a new card layout with gaps of size zero. + */ + public CardLayout3() { + this(0, 0); + } + + /** + * Creates a new card layout with the specified horizontal and + * vertical gaps. The horizontal gaps are placed at the left and + * right edges. The vertical gaps are placed at the top and bottom + * edges. + * @param hgap the horizontal gap. + * @param vgap the vertical gap. + */ + public CardLayout3(int hgap, int vgap) { + super(hgap,vgap); + } + =20 + /** + * Adds the specified component to this card layout's internal + * table of names. The object specified by constraints + * must be a string. The card layout stores this string as a key-value + * pair that can be used for random access to a particular card. + * By calling the show method, an application can + * display the component with the specified name. + * @param comp the component to be added. + * @param constraints a tag that identifies a particular + * card in the layout. + * @see java.awt.CardLayout#show(java.awt.Container, java.lang.Str= ing) + * @exception IllegalArgumentException if the constraint is not a stri= ng. + */ + @Override + public void addLayoutComponent(Component comp, Object constraints) { + synchronized (comp.getTreeLock()) { + if (constraints instanceof String) { + addLayoutComponent((String)constraints, comp); + } else { + addLayoutComponent(constraints, comp); + } + } + } + + /** + * addLayoutComponent(Component, Object). + */ + public void addLayoutComponent(Object constraint, Component comp) { + synchronized (comp.getTreeLock()) { + if (constraint =3D=3D null){ + throw new IllegalArgumentException("cannot add to layout: co= nstraint cannot be null"); + } + if (!vector.isEmpty()) { + comp.setVisible(false); + } + for (int i=3D0; i < vector.size(); i++) { + if (((Card)vector.get(i)).constraint.equals(constraint)) { + ((Card)vector.get(i)).comp =3D comp; + return; + } + } + vector.add(new Card(constraint, comp)); + } + } + + /** + * Removes the specified component from the layout. + * If the card was visible on top, the next card underneath it is shown. + * @param comp the component to be removed. + * @see java.awt.Container#remove(java.awt.Component) + * @see java.awt.Container#removeAll() + */ + @Override + public void removeLayoutComponent(Component comp) { + synchronized (comp.getTreeLock()) { + for (int i =3D 0; i < vector.size(); i++) { + if (((Card)vector.get(i)).comp =3D=3D comp) { + // if we remove current component we should show next one + if (comp.isVisible() && (comp.getParent() !=3D null)) { + next(comp.getParent()); + } + + vector.remove(i); + + // correct currentCard if this is necessary + if (currentCard > i) { + currentCard--; + } + break; + } + } + } + } + + /** + * Flips to the first card of the container. + * @param parent the parent container in which to do the layout + * @see java.awt.CardLayout#last + */ + @Override + public void first(Container parent) { + synchronized (parent.getTreeLock()) { + checkLayout(parent); + int ncomponents =3D parent.getComponentCount(); + for (int i =3D 0 ; i < ncomponents ; i++) { + Component comp =3D parent.getComponent(i); + if (comp.isVisible()) { + comp.setVisible(false); + break; + } + } + if (ncomponents > 0) { + currentCard =3D 0; + parent.getComponent(0).setVisible(true); + parent.validate(); + } + } + } + + /** + * Flips to the next card of the specified container. If the + * currently visible card is the last one, this method flips to the + * first card in the layout. + * @param parent the parent container in which to do the layout + * @see java.awt.CardLayout#previous + */ + @Override + public void next(Container parent) { + synchronized (parent.getTreeLock()) { + checkLayout(parent); + int ncomponents =3D parent.getComponentCount(); + for (int i =3D 0 ; i < ncomponents ; i++) { + Component comp =3D parent.getComponent(i); + if (comp.isVisible()) { + comp.setVisible(false); + currentCard =3D (i + 1) % ncomponents; + comp =3D parent.getComponent(currentCard); + comp.setVisible(true); + parent.validate(); + return; + } + } + showDefaultComponent(parent); + } + } + + /** + * Flips to the previous card of the specified container. If the + * currently visible card is the first one, this method flips to the + * last card in the layout. + * @param parent the parent container in which to do the layout + * @see java.awt.CardLayout#next + */ + @Override + public void previous(Container parent) { + synchronized (parent.getTreeLock()) { + checkLayout(parent); + int ncomponents =3D parent.getComponentCount(); + for (int i =3D 0 ; i < ncomponents ; i++) { + Component comp =3D parent.getComponent(i); + if (comp.isVisible()) { + comp.setVisible(false); + currentCard =3D ((i > 0) ? i-1 : ncomponents-1); + comp =3D parent.getComponent(currentCard); + comp.setVisible(true); + parent.validate(); + return; + } + } + showDefaultComponent(parent); + } + } + + void showDefaultComponent(Container parent) { + if (parent.getComponentCount() > 0) { + currentCard =3D 0; + parent.getComponent(0).setVisible(true); + parent.validate(); + } + } + + /** + * Flips to the last card of the container. + * @param parent the parent container in which to do the layout + * @see java.awt.CardLayout#first + */ + @Override + public void last(Container parent) { + synchronized (parent.getTreeLock()) { + checkLayout(parent); + int ncomponents =3D parent.getComponentCount(); + for (int i =3D 0 ; i < ncomponents ; i++) { + Component comp =3D parent.getComponent(i); + if (comp.isVisible()) { + comp.setVisible(false); + break; + } + } + if (ncomponents > 0) { + currentCard =3D ncomponents - 1; + parent.getComponent(currentCard).setVisible(true); + parent.validate(); + } + } + } + + /** + * Flips to the component that was added to this layout with the + * specified name, using addLayoutComponent. + * If no such component exists, then nothing happens. + * @param parent the parent container in which to do the layout + * @param name the component name + * @see java.awt.CardLayout#addLayoutComponent(java.awt.Component,= java.lang.Object) + */ + public void show(Container parent, Object constraint) { + synchronized (parent.getTreeLock()) { + checkLayout(parent); + Component next =3D null; + int ncomponents =3D vector.size(); + for (int i =3D 0; i < ncomponents; i++) { + Card card =3D (Card)vector.get(i); + if (card.constraint.equals(constraint)) { + next =3D card.comp; + currentCard =3D i; + break; + } + } + if ((next !=3D null) && !next.isVisible()) { + ncomponents =3D parent.getComponentCount(); + for (int i =3D 0; i < ncomponents; i++) { + Component comp =3D parent.getComponent(i); + if (comp.isVisible()) { + comp.setVisible(false); + break; + } + } + next.setVisible(true); + parent.validate(); + } + } + } + + public Component getShowedComponent(Container parent){ + return parent.getComponent(currentCard); + } + + /** + * Make sure that the Container really has a CardLayout installed. + * Otherwise havoc can ensue! + */ + void checkLayout(Container parent) { + if (parent.getLayout() !=3D this) { + throw new IllegalArgumentException("wrong parent for CardLayout"); + } + } + +} Added: trunk/jaxx-runtime-swing/src/main/java/jaxx/runtime/swing/navigation/I= temNavigationCardPanel.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-runtime-swing/src/main/java/jaxx/runtime/swing/navigation/Item= NavigationCardPanel.java (rev 0) +++ trunk/jaxx-runtime-swing/src/main/java/jaxx/runtime/swing/navigation/Item= NavigationCardPanel.java 2009-10-01 10:03:15 UTC (rev 1541) @@ -0,0 +1,40 @@ +/* + * To change this template, choose Tools | Templates + * and open the template in the editor. + */ + +package jaxx.runtime.swing.navigation; + +import java.awt.Component; +import javax.swing.JPanel; +import jaxx.runtime.swing.CardLayout3; +import jaxx.runtime.swing.Item; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + +/** + * + * @author letellier + */ +public class ItemNavigationCardPanel extends JPanel{ + + /** to use log facility, just put in your code: log.info(\"...\"); */ + static private final Log log =3D LogFactory.getLog(ItemNavigationCardPan= el.class); + + CardLayout3 layout; + public ItemNavigationCardPanel(){ + super(); + layout =3D new CardLayout3(); + setLayout(layout); + } + + public void showItem(Item i){ + if (i !=3D null && i.getValue() !=3D null){ + layout.show(this, i.getValue().getClass()); + } + } + + public Component getShowedComponent(){ + return layout.getShowedComponent(this); + } +} Added: trunk/jaxx-runtime-swing/src/main/java/jaxx/runtime/swing/navigation/I= temTreeNavigationAdapter.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-runtime-swing/src/main/java/jaxx/runtime/swing/navigation/Item= TreeNavigationAdapter.java (rev 0) +++ trunk/jaxx-runtime-swing/src/main/java/jaxx/runtime/swing/navigation/Item= TreeNavigationAdapter.java 2009-10-01 10:03:15 UTC (rev 1541) @@ -0,0 +1,64 @@ +package jaxx.runtime.swing.navigation; + +import javax.swing.event.TreeSelectionEvent; +import javax.swing.event.TreeSelectionListener; +import javax.swing.tree.TreeModel; +import javax.swing.tree.TreePath; +import jaxx.runtime.JAXXObject; +import jaxx.runtime.swing.Item; +import jaxx.runtime.swing.JAXXTree; +import jaxx.runtime.swing.JAXXTree.JAXXTreeModel; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + +/** A {@link javax.swing.event.TreeSelectionListener} implementation@@autho= r letellier */ +public abstract class ItemTreeNavigationAdapter implements TreeSelectionLis= tener { + + /** to use log facility, just put in your code: log.info(\"...\"); */ + static private final Log log =3D LogFactory.getLog(ItemTreeNavigationAda= pter.class); + + /** l'ui contenant l'arbre de navigation */ + protected JAXXObject context; + + protected JAXXTree tree; + protected ItemNavigationCardPanel cardPanel; + + public ItemTreeNavigationAdapter(JAXXObject context, JAXXTree tree, Item= NavigationCardPanel cardPanel) { + super(); + this.context =3D context; + this.tree =3D tree; + this.cardPanel =3D cardPanel; + + // Attache ce listener a l'arbre de navigation + tree.addTreeSelectionListener(this); + } + + @Override + public void valueChanged(TreeSelectionEvent e) { + log.debug("Selection in JAXXTree changed" + e.getPath()); + + // Recuperation du path selectionne + TreePath path =3D e.getNewLeadSelectionPath(); + TreeModel model =3D tree.getModel(); + + if (!(model instanceof JAXXTreeModel)){ + log.error("Its not an JAXXTreeModel"); + return; + } + // Find item coresponding + Item itemSelected =3D ((JAXXTreeModel) model).findItem(path.getLastP= athComponent()); + + // Show corresponding panel + cardPanel.showItem(itemSelected); + + // Notifie change + valueChanged(itemSelected.getValue()); + } + + /** + * Method abstraite pour pouvoir propager la donnee selectionner + * + * @param data value of the node selected + */ + protected abstract void valueChanged(Object data); +} --===============5399057485467889135==--