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/ExempleItemTreeNavigationAdapter.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/ItemNavigationCardPanel.java trunk/jaxx-runtime-swing/src/main/java/jaxx/runtime/swing/navigation/ItemTreeNavigationAdapter.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/ExempleItemTreeNavigationAdapter.java =================================================================== --- trunk/jaxx-example/src/main/java/jaxx/demo/ExempleItemTreeNavigationAdapter.java (rev 0) +++ trunk/jaxx-example/src/main/java/jaxx/demo/ExempleItemTreeNavigationAdapter.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 ItemTreeNavigationAdapter{ + + public ExempleItemTreeNavigationAdapter(JAXXObject context, JAXXTree tree, ItemNavigationCardPanel cardPanel){ + super(context, tree, cardPanel); + } + + @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 =================================================================== --- trunk/jaxx-example/src/main/java/jaxx/demo/ItemTreeNavigationDemo.jaxx (rev 0) +++ trunk/jaxx-example/src/main/java/jaxx/demo/ItemTreeNavigationDemo.jaxx 2009-10-01 10:03:15 UTC (rev 1541) @@ -0,0 +1,32 @@ +<DemoPanel> + + <String id='string' javaBean='null'/> + <Date id='date' javaBean='null'/> + + <script> +import jaxx.runtime.swing.navigation.*; +import org.jdesktop.swingx.JXDatePicker; + +new ExempleItemTreeNavigationAdapter(this, nav, content); + </script> + + <JPanel id='demoPanel' layout='{new BorderLayout()}'> + <JSplitPane constraints='BorderLayout.CENTER'> + <JScrollPane> + <JTree id='nav' rootVisible='{false}'> + <item id='string1Item' label='String1' value='{new String("Ceci est un String")}'/> + <item id='string2Item' label='String2' value='{new String("Celui-ci un autre")}'/> + <item id='dateItem' label='Date' value='{new Date()}'/> + </JTree> + </JScrollPane> + <ItemNavigationCardPanel id="content"> + <JPanel id='stringPanel' constraints='String.class'> + <JTextField text='{getString()}'/> + </JPanel> + <JPanel id='datePanel' constraints='Date.class'> + <JXDatePicker date='{getDate()}'/> + </JPanel> + </ItemNavigationCardPanel> + </JSplitPane> + </JPanel> +</DemoPanel> \ No newline at end of file Modified: trunk/jaxx-example/src/main/java/jaxx/demo/JAXXDemo.jaxx =================================================================== --- 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 @@ <item value='{validationDemo1}'/> <item value='{validationDemo2}'/> </item> + <item value='{itemTreeNavigationDemo}'/> <item value='{dataBindingDemo}'/> <item value='{boxedDecoratorDemo}'/> <item value='{statusMessagePanelDemo}'/> @@ -118,6 +119,7 @@ <JTextAreaDemo id='textAreaDemo' constraints='textAreaDemo.getLabel()'/> <ValidationListDemo id='validationDemo1' constraints='validationDemo1.getLabel()'/> <ValidationTableDemo id='validationDemo2' constraints='validationDemo2.getLabel()'/> + <ItemTreeNavigationDemo id='itemTreeNavigationDemo' constraints='itemTreeNavigationDemo.getLabel()'/> <BeanDataBindingDemo id='dataBindingDemo' constraints='dataBindingDemo.getLabel()'/> <LabelStyleDemo id='labelStyleDemo' constraints='labelStyleDemo.getLabel()'/> <CounterDemo id='counterDemo' constraints='counterDemo.getLabel()'/> Added: trunk/jaxx-runtime-swing/src/main/java/jaxx/runtime/swing/CardLayout3.java =================================================================== --- trunk/jaxx-runtime-swing/src/main/java/jaxx/runtime/swing/CardLayout3.java (rev 0) +++ trunk/jaxx-runtime-swing/src/main/java/jaxx/runtime/swing/CardLayout3.java 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}. + * <p/> + * Because in the original layout is not overridable : everything is package level accessible. + * <p/> + * 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 = 0; + + /* + * This creates a Vector to store associated + * pairs of components and their names. + * @see java.util.Vector + */ + Vector vector = new Vector(); + + /* + * A pair of Component and String that represents its name. + */ + class Card implements Serializable { + static final long serialVersionUID = 6640330810709497518L; + public Object constraint; + public Component comp; + public Card(Object constraint, Component cardComponent) { + this.constraint = constraint; + this.comp = 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); + } + + /** + * Adds the specified component to this card layout's internal + * table of names. The object specified by <code>constraints</code> + * 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 <code>show</code> 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.String) + * @exception IllegalArgumentException if the constraint is not a string. + */ + @Override + public void addLayoutComponent(Component comp, Object constraints) { + synchronized (comp.getTreeLock()) { + if (constraints instanceof String) { + addLayoutComponent((String)constraints, comp); + } else { + addLayoutComponent(constraints, comp); + } + } + } + + /** + * <code>addLayoutComponent(Component, Object)</code>. + */ + public void addLayoutComponent(Object constraint, Component comp) { + synchronized (comp.getTreeLock()) { + if (constraint == null){ + throw new IllegalArgumentException("cannot add to layout: constraint cannot be null"); + } + if (!vector.isEmpty()) { + comp.setVisible(false); + } + for (int i=0; i < vector.size(); i++) { + if (((Card)vector.get(i)).constraint.equals(constraint)) { + ((Card)vector.get(i)).comp = 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 = 0; i < vector.size(); i++) { + if (((Card)vector.get(i)).comp == comp) { + // if we remove current component we should show next one + if (comp.isVisible() && (comp.getParent() != 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 = parent.getComponentCount(); + for (int i = 0 ; i < ncomponents ; i++) { + Component comp = parent.getComponent(i); + if (comp.isVisible()) { + comp.setVisible(false); + break; + } + } + if (ncomponents > 0) { + currentCard = 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 = parent.getComponentCount(); + for (int i = 0 ; i < ncomponents ; i++) { + Component comp = parent.getComponent(i); + if (comp.isVisible()) { + comp.setVisible(false); + currentCard = (i + 1) % ncomponents; + comp = 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 = parent.getComponentCount(); + for (int i = 0 ; i < ncomponents ; i++) { + Component comp = parent.getComponent(i); + if (comp.isVisible()) { + comp.setVisible(false); + currentCard = ((i > 0) ? i-1 : ncomponents-1); + comp = parent.getComponent(currentCard); + comp.setVisible(true); + parent.validate(); + return; + } + } + showDefaultComponent(parent); + } + } + + void showDefaultComponent(Container parent) { + if (parent.getComponentCount() > 0) { + currentCard = 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 = parent.getComponentCount(); + for (int i = 0 ; i < ncomponents ; i++) { + Component comp = parent.getComponent(i); + if (comp.isVisible()) { + comp.setVisible(false); + break; + } + } + if (ncomponents > 0) { + currentCard = ncomponents - 1; + parent.getComponent(currentCard).setVisible(true); + parent.validate(); + } + } + } + + /** + * Flips to the component that was added to this layout with the + * specified <code>name</code>, using <code>addLayoutComponent</code>. + * 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 = null; + int ncomponents = vector.size(); + for (int i = 0; i < ncomponents; i++) { + Card card = (Card)vector.get(i); + if (card.constraint.equals(constraint)) { + next = card.comp; + currentCard = i; + break; + } + } + if ((next != null) && !next.isVisible()) { + ncomponents = parent.getComponentCount(); + for (int i = 0; i < ncomponents; i++) { + Component comp = 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() != this) { + throw new IllegalArgumentException("wrong parent for CardLayout"); + } + } + +} Added: trunk/jaxx-runtime-swing/src/main/java/jaxx/runtime/swing/navigation/ItemNavigationCardPanel.java =================================================================== --- trunk/jaxx-runtime-swing/src/main/java/jaxx/runtime/swing/navigation/ItemNavigationCardPanel.java (rev 0) +++ trunk/jaxx-runtime-swing/src/main/java/jaxx/runtime/swing/navigation/ItemNavigationCardPanel.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 = LogFactory.getLog(ItemNavigationCardPanel.class); + + CardLayout3 layout; + public ItemNavigationCardPanel(){ + super(); + layout = new CardLayout3(); + setLayout(layout); + } + + public void showItem(Item i){ + if (i != null && i.getValue() != 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/ItemTreeNavigationAdapter.java =================================================================== --- trunk/jaxx-runtime-swing/src/main/java/jaxx/runtime/swing/navigation/ItemTreeNavigationAdapter.java (rev 0) +++ trunk/jaxx-runtime-swing/src/main/java/jaxx/runtime/swing/navigation/ItemTreeNavigationAdapter.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@@author letellier */ +public abstract class ItemTreeNavigationAdapter implements TreeSelectionListener { + + /** to use log facility, just put in your code: log.info(\"...\"); */ + static private final Log log = LogFactory.getLog(ItemTreeNavigationAdapter.class); + + /** l'ui contenant l'arbre de navigation */ + protected JAXXObject context; + + protected JAXXTree tree; + protected ItemNavigationCardPanel cardPanel; + + public ItemTreeNavigationAdapter(JAXXObject context, JAXXTree tree, ItemNavigationCardPanel cardPanel) { + super(); + this.context = context; + this.tree = tree; + this.cardPanel = 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 = e.getNewLeadSelectionPath(); + TreeModel model = tree.getModel(); + + if (!(model instanceof JAXXTreeModel)){ + log.error("Its not an JAXXTreeModel"); + return; + } + // Find item coresponding + Item itemSelected = ((JAXXTreeModel) model).findItem(path.getLastPathComponent()); + + // 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); +}