Home | History | Annotate | Download | only in ui
      1 /*******************************************************************************
      2  * Copyright (c) 2011 Google, Inc.
      3  * All rights reserved. This program and the accompanying materials
      4  * are made available under the terms of the Eclipse Public License v1.0
      5  * which accompanies this distribution, and is available at
      6  * http://www.eclipse.org/legal/epl-v10.html
      7  *
      8  * Contributors:
      9  *    Google, Inc. - initial API and implementation
     10  *******************************************************************************/
     11 package org.eclipse.wb.internal.core.utils.ui;
     12 
     13 import org.eclipse.swt.layout.GridLayout;
     14 import org.eclipse.swt.widgets.Composite;
     15 import org.eclipse.swt.widgets.Layout;
     16 
     17 /**
     18  * GridLayoutFactory provides a convenient shorthand for creating and initializing GridLayout.
     19  *
     20  * @author scheglov_ke
     21  */
     22 public final class GridLayoutFactory {
     23   private final GridLayout m_layout;
     24 
     25   ////////////////////////////////////////////////////////////////////////////
     26   //
     27   // Constructor
     28   //
     29   ////////////////////////////////////////////////////////////////////////////
     30   private GridLayoutFactory(Composite composite, GridLayout layout) {
     31     m_layout = layout;
     32     composite.setLayout(m_layout);
     33   }
     34 
     35   public static GridLayoutFactory create(Composite composite) {
     36     return new GridLayoutFactory(composite, new GridLayout());
     37   }
     38 
     39   public static GridLayoutFactory modify(Composite composite) {
     40     Layout layout = composite.getLayout();
     41     if (layout instanceof GridLayout) {
     42       return new GridLayoutFactory(composite, (GridLayout) layout);
     43     }
     44     return create(composite);
     45   }
     46 
     47   ////////////////////////////////////////////////////////////////////////////
     48   //
     49   // Access
     50   //
     51   ////////////////////////////////////////////////////////////////////////////
     52   /**
     53    * Sets number of columns in {@link GridLayout}.
     54    */
     55   public GridLayoutFactory columns(int numColumns) {
     56     m_layout.numColumns = numColumns;
     57     return this;
     58   }
     59 
     60   /**
     61    * Specifies whether all columns in the layout will be forced to have the same width.
     62    */
     63   public GridLayoutFactory equalColumns() {
     64     m_layout.makeColumnsEqualWidth = true;
     65     return this;
     66   }
     67 
     68   /**
     69    * Sets the horizontal margins.
     70    */
     71   public GridLayoutFactory marginsH(int margins) {
     72     m_layout.marginWidth = margins;
     73     return this;
     74   }
     75 
     76   /**
     77    * Sets the vertical margins.
     78    */
     79   public GridLayoutFactory marginsV(int margins) {
     80     m_layout.marginHeight = margins;
     81     return this;
     82   }
     83 
     84   /**
     85    * Sets the horizontal/vertical margins.
     86    */
     87   public GridLayoutFactory margins(int margins) {
     88     m_layout.marginWidth = m_layout.marginHeight = margins;
     89     return this;
     90   }
     91 
     92   /**
     93    * Sets zero horizontal and vertical margins.
     94    */
     95   public GridLayoutFactory noMargins() {
     96     m_layout.marginWidth = m_layout.marginHeight = 0;
     97     return this;
     98   }
     99 
    100   /**
    101    * Sets zero horizontal and vertical spacing.
    102    */
    103   public GridLayoutFactory noSpacing() {
    104     m_layout.horizontalSpacing = m_layout.verticalSpacing = 0;
    105     return this;
    106   }
    107 
    108   /**
    109    * Sets horizontal spacing.
    110    */
    111   public GridLayoutFactory spacingH(int spacing) {
    112     m_layout.horizontalSpacing = spacing;
    113     return this;
    114   }
    115 
    116   /**
    117    * Sets vertical spacing.
    118    */
    119   public GridLayoutFactory spacingV(int spacing) {
    120     m_layout.verticalSpacing = spacing;
    121     return this;
    122   }
    123 }
    124