File Source: StepLayout.java

         /* 
    P/P   *  Method: com.dmdirc.addons.ui_swing.wizard.StepLayout__static_init
          */
     1  /*
     2   * Copyright (c) 2006-2009 Chris Smith, Shane Mc Cormack, Gregory Holmes
     3   *
     4   * Permission is hereby granted, free of charge, to any person obtaining a copy
     5   * of this software and associated documentation files (the "Software"), to deal
     6   * in the Software without restriction, including without limitation the rights
     7   * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     8   * copies of the Software, and to permit persons to whom the Software is
     9   * furnished to do so, subject to the following conditions:
    10   *
    11   * The above copyright notice and this permission notice shall be included in
    12   * all copies or substantial portions of the Software.
    13   *
    14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
    20   * SOFTWARE.
    21   */
    22  
    23  package com.dmdirc.addons.ui_swing.wizard;
    24  
    25  import java.awt.Component;
    26  import java.awt.Container;
    27  import java.awt.Dimension;
    28  import java.awt.Insets;
    29  import java.awt.LayoutManager2;
    30  import java.io.Serializable;
    31  import java.util.ArrayList;
    32  import java.util.List;
    33  
    34  /**
    35   * Adjusted Card layout.
    36   */
    37  public class StepLayout implements LayoutManager2, Serializable {
    38  
    39      /**
    40       * A version number for this class. It should be changed whenever the class
    41       * structure is changed (or anything else that would prevent serialized
    42       * objects being unserialized with the new class).
    43       */
    44      private static final long serialVersionUID = 2;
    45      /** Parent container. */
    46      private Container parent;
    47      /** Cards vector. */
    48      private final List<Step> steps;
    49      /** Current step. */
    50      private int currentStep;
    51      /** Vertical gap. */
    52      private int vGap;
    53      /** Horiontal gap. */
    54      private int hGap;
    55  
    56      /**
    57       * Instantiates a new step layout.
    58       */
    59      public StepLayout() {
                 /* 
    P/P           *  Method: void com.dmdirc.addons.ui_swing.wizard.StepLayout()
                  * 
                  *  Postconditions:
                  *    this.currentStep == -1
                  *    this.hGap == 0
                  *    this.vGap == 0
                  *    this.steps == &amp;new ArrayList(StepLayout#1)
                  *    new ArrayList(StepLayout#1) num objects == 1
                  */
    60          this(0, 0);
    61      }
    62      
    63      /**
    64       * Instantiates a new step layout.
    65       * 
    66       * @param parent Parent component
    67       */
    68      public StepLayout(final Container parent) {
                 /* 
    P/P           *  Method: void com.dmdirc.addons.ui_swing.wizard.StepLayout(Container)
                  * 
                  *  Postconditions:
                  *    this.currentStep == -1
                  *    this.hGap == 0
                  *    this.vGap == 0
                  *    this.parent == parent
                  *    init'ed(this.parent)
                  *    this.steps == &amp;new ArrayList(StepLayout#1)
                  *    new ArrayList(StepLayout#1) num objects == 1
                  */
    69          this(0, 0, parent);
    70      }
    71      
    72      /**
    73       * Instantiates a new step layout with the specified gaps.
    74       * 
    75       * @param hGap Horizontal gap
    76       * @param vGap Vertical gap
    77       */
             /* 
    P/P       *  Method: void com.dmdirc.addons.ui_swing.wizard.StepLayout(int, int)
              * 
              *  Postconditions:
              *    this.currentStep == -1
              *    this.hGap == hGap
              *    init'ed(this.hGap)
              *    this.steps == &amp;new ArrayList(StepLayout#1)
              *    this.vGap == vGap
              *    init'ed(this.vGap)
              *    new ArrayList(StepLayout#1) num objects == 1
              */
    78      public StepLayout(final int hGap, final int vGap) {
    79          steps = new ArrayList<Step>();
    80          currentStep = -1;
    81          this.hGap = hGap;
    82          this.vGap = vGap;
    83      }
    84  
    85      /**
    86       * Instantiates a new step layout with the specified gaps.
    87       * 
    88       * @param hGap Horizontal gap
    89       * @param vGap Vertical gap
    90       * @param parent Parent component
    91       */
             /* 
    P/P       *  Method: void com.dmdirc.addons.ui_swing.wizard.StepLayout(int, int, Container)
              * 
              *  Postconditions:
              *    this.currentStep == -1
              *    this.hGap == hGap
              *    init'ed(this.hGap)
              *    this.parent == parent
              *    init'ed(this.parent)
              *    this.steps == &amp;new ArrayList(StepLayout#1)
              *    this.vGap == vGap
              *    init'ed(this.vGap)
              *    new ArrayList(StepLayout#1) num objects == 1
              */
    92      public StepLayout(final int hGap, final int vGap, final Container parent) {
    93          steps = new ArrayList<Step>();
    94          currentStep = -1;
    95          this.hGap = hGap;
    96          this.vGap = vGap;
    97          this.parent = parent;
    98      }
    99  
   100      /**
   101       * Returns the number of steps in the layout.
   102       * 
   103       * @return number of steps >= 0
   104       */
   105      public int size() {
                 /* 
    P/P           *  Method: int size()
                  * 
                  *  Preconditions:
                  *    this.steps != null
                  * 
                  *  Postconditions:
                  *    init'ed(return_value)
                  */
   106          return steps.size();
   107      }
   108  
   109      /**
   110       * Checks if the layout is empty
   111       * 
   112       * @return true iif the layout has no steps
   113       */
   114      public boolean isEmpty() {
                 /* 
    P/P           *  Method: bool isEmpty()
                  * 
                  *  Preconditions:
                  *    this.steps != null
                  * 
                  *  Postconditions:
                  *    init'ed(return_value)
                  */
   115          return steps.isEmpty();
   116      }
   117  
   118      /**
   119       * Returns the specified step from the layout.
   120       * 
   121       * @param index Step to retrieve
   122       * 
   123       * @return Step
   124       */
   125      public Step getStep(final int index) {
                 /* 
    P/P           *  Method: Step getStep(int)
                  * 
                  *  Preconditions:
                  *    this.steps != null
                  * 
                  *  Postconditions:
                  *    init'ed(return_value)
                  */
   126          return steps.get(index);
   127      }
   128  
   129      /**
   130       * Returns the step list.
   131       * 
   132       * @return List of steps
   133       */
   134      public List getSteps() {
                 /* 
    P/P           *  Method: List getSteps()
                  * 
                  *  Postconditions:
                  *    return_value == this.steps
                  *    init'ed(return_value)
                  */
   135          return steps;
   136      }
   137  
   138      /**
   139       * Show the first step.
   140       * 
   141       * @param parent Parent container
   142       */
   143      public void first(final Container parent) {
                 /* 
    P/P           *  Method: void first(Container)
                  * 
                  *  Preconditions:
                  *    parent != null
                  *    (soft) this.steps != null
                  * 
                  *  Postconditions:
                  *    possibly_updated(this.currentStep)
                  */
   144          show(0, parent);
   145      }
   146  
   147      /**
   148       * Show the last step.
   149       * 
   150       * @param parent Parent container
   151       */
   152      public void last(final Container parent) {
                 /* 
    P/P           *  Method: void last(Container)
                  * 
                  *  Preconditions:
                  *    parent != null
                  *    (soft) this.steps != null
                  * 
                  *  Presumptions:
                  *    java.awt.Container:getComponentCount(...)@153 >= -231+1
                  * 
                  *  Postconditions:
                  *    possibly_updated(this.currentStep)
                  */
   153          show(parent.getComponentCount() - 1, parent);
   154      }
   155  
   156      /**
   157       * Show the next step.
   158       * 
   159       * @param parent Parent container
   160       */
   161      public void next(final Container parent) {
                 /* 
    P/P           *  Method: void next(Container)
                  * 
                  *  Preconditions:
                  *    this.currentStep <= 232-2
                  *    parent != null
                  *    (soft) this.steps != null
                  * 
                  *  Postconditions:
                  *    init'ed(this.currentStep)
                  */
   162          show(currentStep + 1, parent);
   163      }
   164  
   165      /** 
   166       * Show the previous step.
   167       * 
   168       * @param parent Parent container
   169       */
   170      public void previous(final Container parent) {
                 /* 
    P/P           *  Method: void previous(Container)
                  * 
                  *  Preconditions:
                  *    this.currentStep >= -231+1
                  *    parent != null
                  *    (soft) this.steps != null
                  * 
                  *  Postconditions:
                  *    init'ed(this.currentStep)
                  */
   171          show(currentStep - 1, parent);
   172      }
   173  
   174      /**
   175       * Show the specified step.
   176       * 
   177       * @param step Step to show
   178       * @param parent Parent container
   179       */
   180      public void show(final Step step, final Container parent) {
                 /* 
    P/P           *  Method: void show(Step, Container)
                  * 
                  *  Preconditions:
                  *    parent != null
                  *    this.steps != null
                  * 
                  *  Postconditions:
                  *    possibly_updated(this.currentStep)
                  */
   181          show(steps.indexOf(step), parent);
   182      }
   183  
   184      /**
   185       * Show the step at the specified index.
   186       * 
   187       * @param step Step to show
   188       * @param parent Parent container
   189       */
   190      public void show(final int step, final Container parent) {
                 /* 
    P/P           *  Method: void show(int, Container)
                  * 
                  *  Preconditions:
                  *    parent != null
                  *    (soft) this.steps != null
                  * 
                  *  Presumptions:
                  *    java.awt.Container:getComponent(...)@202 != null
                  *    java.awt.Container:getComponent(...)@210 != null
                  *    java.util.List:size(...)@194 >= -231+1
                  * 
                  *  Postconditions:
                  *    possibly_updated(this.currentStep)
                  * 
                  *  Test Vectors:
                  *    step: {-231..-2, 0..232-1}, {-1}
                  *    java.awt.Component:isVisible(...)@203: {0}, {1}
                  *    java.util.List:size(...)@193: {0..232-1}, {-231..-1}
                  */
   191          int stepNumber = step;
   192          if (stepNumber == -1) {
   193              if (stepNumber >= steps.size()) {
   194                  stepNumber = steps.size() - 1;
   195              } else {
   196                  stepNumber = 0;
   197              }
   198          }
   199          synchronized (parent.getTreeLock()) {
   200              int componentCount = parent.getComponentCount();
   201              for (int i = 0; i < componentCount; i++) {
   202                  Component comp = parent.getComponent(i);
   203                  if (comp.isVisible()) {
   204                      comp.setVisible(false);
   205                      break;
   206                  }
   207              }
   208              if (componentCount > 0) {
   209                  currentStep = stepNumber;
   210                  parent.getComponent(currentStep).setVisible(true);
   211                  parent.validate();
   212              }
   213          }
   214      }
   215  
   216      /** {@inheritDoc} */
   217      @Override
   218      public void addLayoutComponent(final Component comp,
   219              final Object constraints) {
                 /* 
    P/P           *  Method: void addLayoutComponent(Component, Object)
                  * 
                  *  Preconditions:
                  *    comp != null
                  *    this.steps != null
                  */
   220          if (!(comp instanceof Step)) {
   221              throw new IllegalArgumentException("Component must be an instance of Step");
   222          }
   223          addLayoutComponent((Step) comp);
   224      }
   225  
   226      /** 
   227       * {@inheritDoc}
   228       * 
   229       * @deprecated Use addLayoutComponent(Component, Object) or 
   230       * addLayoutComponent(Component)
   231       * 
   232       * @see addLayoutComponent(Component)
   233       * @see addLayoutComponent(Component, Object)
   234       */
   235      @Override
   236      @Deprecated
   237      public void addLayoutComponent(final String name, final Component comp) {
                 /* 
    P/P           *  Method: void addLayoutComponent(String, Component)
                  * 
                  *  Preconditions:
                  *    comp != null
                  *    this.steps != null
                  */
   238          if (!(comp instanceof Step)) {
   239              throw new IllegalArgumentException("Component must be an instance of Step");
   240          }
   241          addLayoutComponent((Step) comp);
   242      }
   243  
   244      /**
   245       * Adds a component to the layout.
   246       * 
   247       * @param step Component to add
   248       */
   249      public void addLayoutComponent(final Step step) {
                 /* 
    P/P           *  Method: void addLayoutComponent(Step)
                  * 
                  *  Preconditions:
                  *    step != null
                  *    this.steps != null
                  * 
                  *  Test Vectors:
                  *    java.util.List:isEmpty(...)@251: {1}, {0}
                  */
   250          synchronized (step.getTreeLock()) {
   251              if (!steps.isEmpty()) {
   252                  step.setVisible(false);
   253              }
   254              steps.add(step);
   255          }
   256      }
   257  
   258      /** {@inheritDoc} */
   259      @Override
   260      public void removeLayoutComponent(final Component comp) {
                 /* 
    P/P           *  Method: void removeLayoutComponent(Component)
                  * 
                  *  Preconditions:
                  *    comp != null
                  *    this.currentStep <= 232-2
                  *    (soft) this.steps != null
                  * 
                  *  Presumptions:
                  *    java.awt.Component:getParent(...)@265 != null
                  * 
                  *  Postconditions:
                  *    init'ed(this.currentStep)
                  * 
                  *  Test Vectors:
                  *    java.awt.Component:isVisible(...)@262: {0}, {1}
                  */
   261          synchronized (comp.getTreeLock()) {
   262              if (comp.isVisible()) {
   263                  comp.setVisible(false);
   264              }
   265              next(comp.getParent());
   266              steps.remove(comp);
   267          }
   268      }
   269  
   270      /** 
   271       * {@inheritDoc}
   272       * 
   273       * @return Returns the preferred size of the container
   274       */
   275      @Override
   276      public Dimension preferredLayoutSize(final Container parent) {
                 /* 
    P/P           *  Method: Dimension preferredLayoutSize(Container)
                  * 
                  *  Preconditions:
                  *    parent != null
                  *    init'ed(this.hGap)
                  *    init'ed(this.vGap)
                  * 
                  *  Presumptions:
                  *    java.awt.Component:getPreferredSize(...)@285 != null
                  *    java.awt.Container:getComponent(...)@284 != null
                  *    java.awt.Container:getInsets(...)@278 != null
                  * 
                  *  Postconditions:
                  *    return_value == &amp;new Dimension(preferredLayoutSize#1)
                  *    new Dimension(preferredLayoutSize#1) num objects == 1
                  */
   277          synchronized (parent.getTreeLock()) {
   278              Insets insets = parent.getInsets();
   279              int componentCount = parent.getComponentCount();
   280              int width = 0;
   281              int height = 0;
   282  
   283              for (int i = 0; i < componentCount; i++) {
   284                  Component comp = parent.getComponent(i);
   285                  Dimension preferredDimension = comp.getPreferredSize();
   286                  if (preferredDimension.width > width) {
   287                      width = preferredDimension.width;
   288                  }
   289                  if (preferredDimension.height > height) {
   290                      height = preferredDimension.height;
   291                  }
   292              }
   293              return new Dimension(insets.left + insets.right + width + hGap * 2,
   294                      insets.top + insets.bottom + height + vGap * 2);
   295          }
   296      }
   297  
   298      /** 
   299       * {@inheritDoc}
   300       * 
   301       * @return Returns the minimum size of the container
   302       */
   303      @Override
   304      public Dimension minimumLayoutSize(final Container parent) {
                 /* 
    P/P           *  Method: Dimension minimumLayoutSize(Container)
                  * 
                  *  Preconditions:
                  *    parent != null
                  *    init'ed(this.hGap)
                  *    init'ed(this.vGap)
                  * 
                  *  Presumptions:
                  *    java.awt.Component:getMinimumSize(...)@313 != null
                  *    java.awt.Container:getComponent(...)@312 != null
                  *    java.awt.Container:getInsets(...)@306 != null
                  * 
                  *  Postconditions:
                  *    return_value == &amp;new Dimension(minimumLayoutSize#1)
                  *    new Dimension(minimumLayoutSize#1) num objects == 1
                  */
   305          synchronized (parent.getTreeLock()) {
   306              Insets insets = parent.getInsets();
   307              int componentCount = parent.getComponentCount();
   308              int width = 0;
   309              int height = 0;
   310  
   311              for (int i = 0; i < componentCount; i++) {
   312                  Component comp = parent.getComponent(i);
   313                  Dimension minimumDimension = comp.getMinimumSize();
   314                  if (minimumDimension.width > width) {
   315                      width = minimumDimension.width;
   316                  }
   317                  if (minimumDimension.height > height) {
   318                      height = minimumDimension.height;
   319                  }
   320              }
   321              return new Dimension(insets.left + insets.right + width + hGap * 2,
   322                      insets.top + insets.bottom + height + vGap * 2);
   323          }
   324      }
   325  
   326      /** 
   327       * {@inheritDoc}
   328       * 
   329       * @param parent Container to get the size for
   330       * 
   331       * @return Returns the maximum size of the container
   332       */
   333      @Override
   334      public Dimension maximumLayoutSize(final Container parent) {
                 /* 
    P/P           *  Method: Dimension maximumLayoutSize(Container)
                  * 
                  *  Postconditions:
                  *    return_value == &amp;new Dimension(maximumLayoutSize#1)
                  *    new Dimension(maximumLayoutSize#1) num objects == 1
                  */
   335          return new Dimension(Integer.MAX_VALUE, Integer.MAX_VALUE);
   336      }
   337  
   338      /** 
   339       * {@inheritDoc}
   340       * 
   341       * @param target Container to get the alignment from
   342       * 
   343       * @return Alignment
   344       */
   345      @Override
   346      public float getLayoutAlignmentX(final Container target) {
                 /* 
    P/P           *  Method: float getLayoutAlignmentX(Container)
                  * 
                  *  Postconditions:
                  *    return_value == 1/2
                  */
   347          return 0.5f;
   348      }
   349  
   350      /** 
   351       * {@inheritDoc}
   352       * 
   353       * @param target Container to get the alignment from
   354       * 
   355       * @return Alignment
   356       */
   357      @Override
   358      public float getLayoutAlignmentY(final Container target) {
                 /* 
    P/P           *  Method: float getLayoutAlignmentY(Container)
                  * 
                  *  Postconditions:
                  *    return_value == 1/2
                  */
   359          return 0.5f;
   360      }
   361  
   362      /** 
   363       * {@inheritDoc}
   364       * 
   365       * @param target  Container to invalidate
   366       */
   367      @Override
   368      public void invalidateLayout(final Container target) {
   369      //Ignore
             /* 
    P/P       *  Method: void invalidateLayout(Container)
              */
   370      }
   371  
   372      /** {@inheritDoc} */
   373      @Override
   374      public void layoutContainer(final Container parent) {
                 /* 
    P/P           *  Method: void layoutContainer(Container)
                  * 
                  *  Preconditions:
                  *    parent != null
                  *    (soft) init'ed(this.hGap)
                  *    (soft) init'ed(this.vGap)
                  * 
                  *  Presumptions:
                  *    java.awt.Container:getComponent(...)@382 != null
                  *    java.awt.Container:getComponent(...)@393 != null
                  *    java.awt.Container:getHeight(...)@383 - (this.vGap*2 + insets.top@376 + insets.bottom@376) in {-231..232-1}
                  *    java.awt.Container:getInsets(...)@376 != null
                  *    java.awt.Container:getWidth(...)@383 - (this.hGap*2 + insets.left@376 + insets.right@376) in {-231..232-1}
                  *    ...
                  * 
                  *  Test Vectors:
                  *    java.awt.Component:isVisible(...)@387: {0}, {1}
                  */
   375          synchronized (parent.getTreeLock()) {
   376              Insets insets = parent.getInsets();
   377              int componentCount = parent.getComponentCount();
   378              Component comp = null;
   379              boolean currentFound = false;
   380  
   381              for (int i = 0; i < componentCount; i++) {
   382                  comp = parent.getComponent(i);
   383                  comp.setBounds(hGap + insets.left, vGap + insets.top,
   384                          parent.getWidth() - (hGap * 2 + insets.left +
   385                          insets.right), parent.getHeight() - (vGap * 2 +
   386                          insets.top + insets.bottom));
   387                  if (comp.isVisible()) {
   388                      currentFound = true;
   389                  }
   390              }
   391  
   392              if (!currentFound && componentCount > 0) {
   393                  parent.getComponent(0).setVisible(true);
   394              }
   395          }
   396      }
   397  }








SofCheck Inspector Build Version : 2.17854
StepLayout.java 2009-Jun-25 01:54:24
StepLayout.class 2009-Sep-02 17:04:16