Author: tchemit Date: 2010-05-05 13:11:18 +0200 (Wed, 05 May 2010) New Revision: 1876 Url: http://nuiton.org/repositories/revision/jaxx/1876 Log: improve code Modified: trunk/jaxx-runtime/src/main/java/jaxx/runtime/swing/CardLayout2.java Modified: trunk/jaxx-runtime/src/main/java/jaxx/runtime/swing/CardLayout2.java =================================================================== --- trunk/jaxx-runtime/src/main/java/jaxx/runtime/swing/CardLayout2.java 2010-05-05 10:07:39 UTC (rev 1875) +++ trunk/jaxx-runtime/src/main/java/jaxx/runtime/swing/CardLayout2.java 2010-05-05 11:11:18 UTC (rev 1876) @@ -37,15 +37,15 @@ import java.util.List; /** - * An override of the awt {@link java.awt.CardLayout}. + * An override of the awt {@link CardLayout}. * <p/> * Because in the original layout is not overridable : everything is package level accessible. * <p/> * This new class offers to test if a constrains (as a Serializable) is actually dealed by the layout, - * via the method {@link #contains(java.io.Serializable)}. + * via the method {@link #contains(Serializable)}. * <p/> * We had also another method to obtain the current visible component in a container layouted by the class, - * via the method {@link #getVisibleComponent(java.awt.Container)}. + * via the method {@link #getVisibleComponent(Container)}. * * @author tchemit <chemit@codelutin.com> * @version 1.0 @@ -80,7 +80,8 @@ * Test if a constrains is contained in the layout. * * @param constraints l'identifiant a tester - * @return <code>true</code> si l'identifiant est deja present dans le layout, <code>false</code> autrement. + * @return {@code true} si l'identifiant est deja present dans le + * layout, {@code false} otherwise. */ public boolean contains(Serializable constraints) { return contexts.contains(constraints); @@ -93,9 +94,7 @@ * @return the component visible in the container. */ public Component getVisibleComponent(Container container) { - if (container.getLayout() != this) { - throw new IllegalArgumentException("the container is not managed by the current layout"); - } + checkContainer(container); for (Component component : container.getComponents()) { if (component.isVisible()) { return component; @@ -106,12 +105,8 @@ } public Component getComponent(Container container, String constraints) { - if (container.getLayout() != this) { - throw new IllegalArgumentException("the container is not manage by the current layout"); - } - if (!contexts.contains(constraints)) { - throw new IllegalArgumentException("the constraints '" + constraints + "' is not supported by this layout : " + contexts); - } + checkContainer(container); + checkConstraints(constraints); int index = contexts.indexOf(constraints); return container.getComponents()[index]; } @@ -123,8 +118,8 @@ * @param parent the parent container in which to do the layout * @return the preferred dimensions to lay out the subcomponents * of the specified container - * @see java.awt.Container#getPreferredSize - * @see java.awt.CardLayout#minimumLayoutSize + * @see Container#getPreferredSize + * @see CardLayout#minimumLayoutSize */ @Override public Dimension preferredLayoutSize(Container parent) { @@ -147,8 +142,8 @@ * @param parent the parent container in which to do the layout * @return the minimum dimensions required to lay out the * subcomponents of the specified container - * @see java.awt.Container#doLayout - * @see java.awt.CardLayout#preferredLayoutSize + * @see Container#doLayout + * @see CardLayout#preferredLayoutSize */ @Override public Dimension minimumLayoutSize(Container parent) { @@ -170,7 +165,7 @@ * in the specified target container. * * @param target the component which needs to be laid out - * @see java.awt.Container + * @see Container * @see #minimumLayoutSize * @see #preferredLayoutSize */ @@ -197,7 +192,7 @@ * insets, horizontal gaps, and vertical gaps. * * @param parent the parent container in which to do the layout - * @see java.awt.Container#doLayout + * @see Container#doLayout */ @Override public void layoutContainer(Container parent) { @@ -231,9 +226,7 @@ * @param parent the parent container linked with the layout */ public void reset(Container parent) { - if (parent.getLayout() != this) { - throw new IllegalArgumentException("wrong parent for CardLayout"); - } + checkContainer(parent); for (Component component : parent.getComponents()) { removeLayoutComponent(component); parent.remove(component); @@ -242,4 +235,19 @@ } + protected void checkContainer(Container container) { + if (!equals(container.getLayout())) { + throw new IllegalArgumentException("the container is not managed by the current layout"); + } + } + + protected void checkConstraints(String constraints) { + if (!contains(constraints)) { + throw new IllegalArgumentException("the constraints '" + constraints + "' is not supported by this layout : " + contexts); + } + } + + public Serializable[] getContexts() { + return contexts.toArray(new Serializable[contexts.size()]); + } }