File Source: StepLayout.java

         /* 
    P/P   *  Method: com.dmdirc.installer.ui.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.installer.ui;
    24  
    25  import com.dmdirc.installer.Step;
    26  
    27  import java.awt.Component;
    28  import java.awt.Container;
    29  import java.awt.Dimension;
    30  import java.awt.Insets;
    31  import java.awt.LayoutManager2;
    32  import java.io.Serializable;
    33  import java.util.ArrayList;
    34  import java.util.List;
    35  
    36  /**
    37   * Adjusted Card layout.
    38   */
    39  public class StepLayout implements LayoutManager2, Serializable {
    40  
    41      /**
    42       * A version number for this class. It should be changed whenever the class
    43       * structure is changed (or anything else that would prevent serialized
    44       * objects being unserialized with the new class).
    45       */
    46      private static final long serialVersionUID = 2;
    47      /** Parent container. */
    48      private Container parent;
    49      /** Cards vector. */
    50      private final List<SwingStep> steps;
    51      /** Current step. */
    52      private int currentStep;
    53      /** Vertical gap. */
    54      private int vGap;
    55      /** Horiontal gap. */
    56      private int hGap;
    57  
    58      /**
    59       * Instantiates a new step layout.
    60       */
    61      public StepLayout() {
                 /* 
    P/P           *  Method: void com.dmdirc.installer.ui.StepLayout()
                  * 
                  *  Postconditions:
                  *    this.currentStep == -1
                  *    this.hGap == 0
                  *    this.vGap == 0
                  *    this.parent == null
                  *    this.steps == &amp;new ArrayList(StepLayout#1)
                  *    new ArrayList(StepLayout#1) num objects == 1
                  */
    62          this(0, 0);
    63      }
    64  
    65      /**
    66       * Instantiates a new step layout.
    67       * 
    68       * @param parent Parent component
    69       */
    70      public StepLayout(final Container parent) {
                 /* 
    P/P           *  Method: void com.dmdirc.installer.ui.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
                  */
    71          this(0, 0, parent);
    72      }
    73  
    74      /**
    75       * Instantiates a new step layout with the specified gaps.
    76       * 
    77       * @param hGap Horizontal gap
    78       * @param vGap Vertical gap
    79       */
    80      public StepLayout(final int hGap, final int vGap) {
                 /* 
    P/P           *  Method: void com.dmdirc.installer.ui.StepLayout(int, int)
                  * 
                  *  Postconditions:
                  *    this.currentStep == -1
                  *    this.hGap == hGap
                  *    init'ed(this.hGap)
                  *    this.parent == null
                  *    this.steps == &amp;new ArrayList(StepLayout#1)
                  *    this.vGap == vGap
                  *    init'ed(this.vGap)
                  *    new ArrayList(StepLayout#1) num objects == 1
                  */
    81          this(hGap, vGap, null);
    82      }
    83  
    84      /**
    85       * Instantiates a new step layout with the specified gaps.
    86       * 
    87       * @param hGap Horizontal gap
    88       * @param vGap Vertical gap
    89       * @param parent Parent component
    90       */
             /* 
    P/P       *  Method: void com.dmdirc.installer.ui.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
              */
    91      public StepLayout(final int hGap, final int vGap, final Container parent) {
    92          steps = new ArrayList<SwingStep>();
    93          currentStep = -1;
    94          this.hGap = hGap;
    95          this.vGap = vGap;
    96          this.parent = parent;
    97      }
    98  
    99      /**
   100       * Returns the number of steps in the layout.
   101       * 
   102       * @return number of steps >= 0
   103       */
   104      public int size() {
                 /* 
    P/P           *  Method: int size()
                  * 
                  *  Preconditions:
                  *    this.steps != null
                  * 
                  *  Postconditions:
                  *    init'ed(return_value)
                  */
   105          return steps.size();
   106      }
   107  
   108      /**
   109       * Checks if the layout is empty
   110       * 
   111       * @return true iif the layout has no steps
   112       */
   113      public boolean isEmpty() {
                 /* 
    P/P           *  Method: bool isEmpty()
                  * 
                  *  Preconditions:
                  *    this.steps != null
                  * 
                  *  Postconditions:
                  *    init'ed(return_value)
                  */
   114          return steps.isEmpty();
   115      }
   116  
   117      /**
   118       * Returns the specified step from the layout.
   119       * 
   120       * @param index Step to retrieve
   121       * 
   122       * @return Step
   123       */
   124      public Step getStep(final int index) {
                 /* 
    P/P           *  Method: Step getStep(int)
                  * 
                  *  Preconditions:
                  *    this.steps != null
                  * 
                  *  Postconditions:
                  *    init'ed(return_value)
                  */
   125          return steps.get(index);
   126      }
   127  
   128      /**
   129       * Returns the step list.
   130       * 
   131       * @return List of steps
   132       */
   133      public List<SwingStep> getSteps() {
                 /* 
    P/P           *  Method: List getSteps()
                  * 
                  *  Postconditions:
                  *    return_value == this.steps
                  *    init'ed(return_value)
                  */
   134          return steps;
   135      }
   136  
   137      /**
   138       * Returns the current step index.
   139       *
   140       * @return Current step index
   141       */
   142      public int getCurrentStepIndex() {
                 /* 
    P/P           *  Method: int getCurrentStepIndex()
                  * 
                  *  Preconditions:
                  *    init'ed(this.currentStep)
                  * 
                  *  Postconditions:
                  *    return_value == this.currentStep
                  *    init'ed(return_value)
                  */
   143          return currentStep;
   144      }
   145  
   146      /**
   147       * Returns the current step name.
   148       *
   149       * @return Current step name
   150       */
   151      public String getCurrentStepName() {
                 /* 
    P/P           *  Method: String getCurrentStepName()
                  * 
                  *  Preconditions:
                  *    init'ed(this.currentStep)
                  *    this.steps != null
                  * 
                  *  Presumptions:
                  *    java.util.List:get(...)@152 != null
                  * 
                  *  Postconditions:
                  *    init'ed(return_value)
                  */
   152          return steps.get(currentStep).getStepName();
   153      }
   154  
   155      /**
   156       * Returns the current step name.
   157       *
   158       * @return Current step name
   159       */
   160      public Step getCurrentStep() {
                 /* 
    P/P           *  Method: Step getCurrentStep()
                  * 
                  *  Preconditions:
                  *    init'ed(this.currentStep)
                  *    this.steps != null
                  * 
                  *  Postconditions:
                  *    init'ed(return_value)
                  */
   161          return steps.get(currentStep);
   162      }
   163  
   164      /**
   165       * Show the first step.
   166       * 
   167       * @param parent Parent container
   168       */
   169      public void first(final Container parent) {
                 /* 
    P/P           *  Method: void first(Container)
                  * 
                  *  Preconditions:
                  *    parent != null
                  *    (soft) this.steps != null
                  * 
                  *  Postconditions:
                  *    possibly_updated(this.currentStep)
                  */
   170          show(0, parent);
   171      }
   172  
   173      /**
   174       * Show the last step.
   175       * 
   176       * @param parent Parent container
   177       */
   178      public void last(final Container parent) {
                 /* 
    P/P           *  Method: void last(Container)
                  * 
                  *  Preconditions:
                  *    parent != null
                  *    (soft) this.steps != null
                  * 
                  *  Presumptions:
                  *    java.awt.Container:getComponentCount(...)@179 >= -231+1
                  * 
                  *  Postconditions:
                  *    possibly_updated(this.currentStep)
                  */
   179          show(parent.getComponentCount() - 1, parent);
   180      }
   181  
   182      /**
   183       * Show the next step.
   184       * 
   185       * @param parent Parent container
   186       */
   187      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)
                  */
   188          show(currentStep + 1, parent);
   189      }
   190  
   191      /** 
   192       * Show the previous step.
   193       * 
   194       * @param parent Parent container
   195       */
   196      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)
                  */
   197          show(currentStep - 1, parent);
   198      }
   199  
   200      /**
   201       * Show the specified step.
   202       * 
   203       * @param step Step to show
   204       * @param parent Parent container
   205       */
   206      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)
                  */
   207          show(steps.indexOf(step), parent);
   208      }
   209  
   210      /**
   211       * Show the step at the specified index.
   212       * 
   213       * @param step Step to show
   214       * @param parent Parent container
   215       */
   216      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(...)@228 != null
                  *    java.awt.Container:getComponent(...)@236 != null
                  *    java.util.List:size(...)@220 >= -231+1
                  * 
                  *  Postconditions:
                  *    possibly_updated(this.currentStep)
                  * 
                  *  Test Vectors:
                  *    step: {-231..-2, 0..232-1}, {-1}
                  *    java.awt.Component:isVisible(...)@229: {0}, {1}
                  *    java.util.List:size(...)@219: {0..232-1}, {-231..-1}
                  */
   217          int stepNumber = step;
   218          if (stepNumber == -1) {
   219              if (stepNumber >= steps.size()) {
   220                  stepNumber = steps.size() - 1;
   221              } else {
   222                  stepNumber = 0;
   223              }
   224          }
   225          synchronized (parent.getTreeLock()) {
   226              int componentCount = parent.getComponentCount();
   227              for (int i = 0; i < componentCount; i++) {
   228                  Component comp = parent.getComponent(i);
   229                  if (comp.isVisible()) {
   230                      comp.setVisible(false);
   231                      break;
   232                  }
   233              }
   234              if (componentCount > 0) {
   235                  currentStep = stepNumber;
   236                  parent.getComponent(currentStep).setVisible(true);
   237                  parent.validate();
   238              }
   239          }
   240      }
   241  
   242      /** {@inheritDoc} */
   243      @Override
   244      public void addLayoutComponent(final Component comp,
   245                                     final Object constraints) {
                 /* 
    P/P           *  Method: void addLayoutComponent(Component, Object)
                  * 
                  *  Preconditions:
                  *    comp != null
                  *    this.steps != null
                  */
   246          if (!(comp instanceof Step)) {
   247              throw new IllegalArgumentException(
   248                      "Component must be an instance of Step");
   249          }
   250          addLayoutComponent((SwingStep) comp);
   251      }
   252  
   253      /** 
   254       * {@inheritDoc}
   255       * 
   256       * @deprecated Use addLayoutComponent(Component, Object) or 
   257       * addLayoutComponent(Component)
   258       * 
   259       * @see addLayoutComponent(Component)
   260       * @see addLayoutComponent(Component, Object)
   261       */
   262      @Override
   263      @Deprecated
   264      public void addLayoutComponent(final String name, final Component comp) {
                 /* 
    P/P           *  Method: void addLayoutComponent(String, Component)
                  * 
                  *  Preconditions:
                  *    comp != null
                  *    this.steps != null
                  */
   265          if (!(comp instanceof Step)) {
   266              throw new IllegalArgumentException(
   267                      "Component must be an instance of Step");
   268          }
   269          addLayoutComponent((SwingStep) comp);
   270      }
   271  
   272      /**
   273       * Adds a component to the layout.
   274       * 
   275       * @param step Component to add
   276       */
   277      public void addLayoutComponent(final SwingStep step) {
                 /* 
    P/P           *  Method: void addLayoutComponent(SwingStep)
                  * 
                  *  Preconditions:
                  *    step != null
                  *    this.steps != null
                  * 
                  *  Test Vectors:
                  *    java.util.List:isEmpty(...)@279: {1}, {0}
                  */
   278          synchronized (step.getTreeLock()) {
   279              if (!steps.isEmpty()) {
   280                  step.setVisible(false);
   281              }
   282              steps.add(step);
   283          }
   284      }
   285  
   286      /** {@inheritDoc} */
   287      @Override
   288      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(...)@293 != null
                  * 
                  *  Postconditions:
                  *    init'ed(this.currentStep)
                  * 
                  *  Test Vectors:
                  *    java.awt.Component:isVisible(...)@290: {0}, {1}
                  */
   289          synchronized (comp.getTreeLock()) {
   290              if (comp.isVisible()) {
   291                  comp.setVisible(false);
   292              }
   293              next(comp.getParent());
   294              steps.remove(comp);
   295          }
   296      }
   297  
   298      /** 
   299       * {@inheritDoc}
   300       * 
   301       * @return Returns the preferred size of the container
   302       */
   303      @Override
   304      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(...)@313 != null
                  *    java.awt.Container:getComponent(...)@312 != null
                  *    java.awt.Container:getInsets(...)@306 != null
                  * 
                  *  Postconditions:
                  *    return_value == &amp;new Dimension(preferredLayoutSize#1)
                  *    new Dimension(preferredLayoutSize#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 preferredDimension = comp.getPreferredSize();
   314                  if (preferredDimension.width > width) {
   315                      width = preferredDimension.width;
   316                  }
   317                  if (preferredDimension.height > height) {
   318                      height = preferredDimension.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       * @return Returns the minimum size of the container
   330       */
   331      @Override
   332      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(...)@341 != null
                  *    java.awt.Container:getComponent(...)@340 != null
                  *    java.awt.Container:getInsets(...)@334 != null
                  * 
                  *  Postconditions:
                  *    return_value == &amp;new Dimension(minimumLayoutSize#1)
                  *    new Dimension(minimumLayoutSize#1) num objects == 1
                  */
   333          synchronized (parent.getTreeLock()) {
   334              Insets insets = parent.getInsets();
   335              int componentCount = parent.getComponentCount();
   336              int width = 0;
   337              int height = 0;
   338  
   339              for (int i = 0; i < componentCount; i++) {
   340                  Component comp = parent.getComponent(i);
   341                  Dimension minimumDimension = comp.getMinimumSize();
   342                  if (minimumDimension.width > width) {
   343                      width = minimumDimension.width;
   344                  }
   345                  if (minimumDimension.height > height) {
   346                      height = minimumDimension.height;
   347                  }
   348              }
   349              return new Dimension(insets.left + insets.right + width + hGap * 2,
   350                                   insets.top + insets.bottom + height + vGap * 2);
   351          }
   352      }
   353  
   354      /** 
   355       * {@inheritDoc}
   356       * 
   357       * @param parent Container to get the size for
   358       * 
   359       * @return Returns the maximum size of the container
   360       */
   361      @Override
   362      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
                  */
   363          return new Dimension(Integer.MAX_VALUE, Integer.MAX_VALUE);
   364      }
   365  
   366      /** 
   367       * {@inheritDoc}
   368       * 
   369       * @param target Container to get the alignment from
   370       * 
   371       * @return Alignment
   372       */
   373      @Override
   374      public float getLayoutAlignmentX(final Container target) {
                 /* 
    P/P           *  Method: float getLayoutAlignmentX(Container)
                  * 
                  *  Postconditions:
                  *    return_value == 1/2
                  */
   375          return 0.5f;
   376      }
   377  
   378      /** 
   379       * {@inheritDoc}
   380       * 
   381       * @param target Container to get the alignment from
   382       * 
   383       * @return Alignment
   384       */
   385      @Override
   386      public float getLayoutAlignmentY(final Container target) {
                 /* 
    P/P           *  Method: float getLayoutAlignmentY(Container)
                  * 
                  *  Postconditions:
                  *    return_value == 1/2
                  */
   387          return 0.5f;
   388      }
   389  
   390      /** 
   391       * {@inheritDoc}
   392       * 
   393       * @param target  Container to invalidate
   394       */
   395      @Override
   396      public void invalidateLayout(final Container target) {
   397          //Ignore
             /* 
    P/P       *  Method: void invalidateLayout(Container)
              */
   398      }
   399  
   400      /** {@inheritDoc} */
   401      @Override
   402      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(...)@410 != null
                  *    java.awt.Container:getComponent(...)@422 != null
                  *    java.awt.Container:getHeight(...)@411 - (this.vGap*2 + insets.top@404 + insets.bottom@404) in {-231..232-1}
                  *    java.awt.Container:getInsets(...)@404 != null
                  *    java.awt.Container:getWidth(...)@411 - (this.hGap*2 + insets.left@404 + insets.right@404) in {-231..232-1}
                  *    ...
                  * 
                  *  Test Vectors:
                  *    java.awt.Component:isVisible(...)@416: {0}, {1}
                  */
   403          synchronized (parent.getTreeLock()) {
   404              Insets insets = parent.getInsets();
   405              int componentCount = parent.getComponentCount();
   406              Component comp = null;
   407              boolean currentFound = false;
   408  
   409              for (int i = 0; i < componentCount; i++) {
   410                  comp = parent.getComponent(i);
   411                  comp.setBounds(hGap + insets.left, vGap + insets.top,
   412                                 parent.getWidth() - (hGap * 2 + insets.left +
   413                                                      insets.right), parent.
   414                          getHeight() - (vGap * 2 +
   415                                         insets.top + insets.bottom));
   416                  if (comp.isVisible()) {
   417                      currentFound = true;
   418                  }
   419              }
   420  
   421              if (!currentFound && componentCount > 0) {
   422                  parent.getComponent(0).setVisible(true);
   423              }
   424          }
   425      }
   426  }








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