⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 swtgridlayouthelper.java

📁 A translator that converts Qt Designer UI files into SWT java classes. Use the power of Qt Designer
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/* This file is part of ui2swt.
 *
 * $Revision: 1.4 $
 * $Date: 2007/01/23 10:04:18 $
 * $Name:  $
 *
 * Copyright (C) 2006-2007 James Forbes, All Rights Reserved.
 *
 * This software is provided 'as-is', without any express or implied warranty.
 * In no event will the authors be held liable for any damages arising from the
 * use of this software.
 *
 * Permission is granted to anyone to use this software for any purpose,
 * including commercial applications, and to alter it and redistribute it
 * freely, subject to the following restrictions:
 *
 *  1. The origin of this software must not be misrepresented; you must not
 *     claim that you wrote the original software. If you use this software in
 *     a product, an acknowledgment in the product documentation would be
 *     appreciated but is not required.
 *
 *  2. Altered source versions must be plainly marked as such, and must not be
 *     misrepresented as being the original software.
 *
 *  3. This notice may not be removed or altered from any source distribution.
 */

package ui2swt.swt;

import ui2swt.ClassDefinition;
import ui2swt.Layout;
import ui2swt.UIDefinition;
import ui2swt.Widget;


public class SWTGridLayoutHelper extends SWTLayoutHelper
{
    public static final String SWT_MARGIN       = "swt.margin";
    public static final String SWT_SPACING      = "swt.spacing";
    public static final String SWT_NUM_ROWS     = "swt.numRows";
    public static final String SWT_NUM_COLUMNS  = "swt.numColumns";
    public static final String SWT_ROW          = "swt.row";
    public static final String SWT_COLUMN       = "swt.column";
    
    
    //
    //
    //
    
    private static final String UI_MARGIN                       = "margin.number";
    private static final String UI_SPACING                      = "spacing.number";

    private static final int    SWT_MARGIN_DEFAULT              = 5;
    private static final int    SWT_SPACING_DEFAULT             = 5;
    
    private static final String SWT_GRID_DATA_ALIGN_FILL        = "GridData.FILL";
    private static final String SWT_GRID_DATA_ALIGN_BEGINNING   = "GridData.BEGINNING";
    private static final String SWT_GRID_DATA_ALIGN_CENTER      = "GridData.CENTER";

    
    //
    //
    //
    
    public static void setProperties(
        UIDefinition    iUIDefinition,
        Layout          iLayout )
    {
        iLayout.setProperty(SWT_CLASS, "GridLayout");
        
        iLayout.setIntProperty(SWT_MARGIN,  iLayout.getIntProperty(UI_MARGIN, SWT_MARGIN_DEFAULT));
        iLayout.setIntProperty(SWT_SPACING, iLayout.getIntProperty(UI_SPACING, SWT_SPACING_DEFAULT));
    }
    
    
    public static void generateFieldsCode(
        UIDefinition    iUIDefinition,
        Layout          iLayout,
        ClassDefinition iClassDefinition )
    {
        String layoutVar  = iLayout.getProperty(SWT_VAR);
        int    margin     = iLayout.getIntProperty(SWT_MARGIN, 0);
        int    spacing    = iLayout.getIntProperty(SWT_SPACING, 0);
        int    numColumns = iLayout.getIntProperty(SWT_NUM_COLUMNS, 1);
        
        iClassDefinition.getConstructor()
            .addVariableFieldAssignment(layoutVar, "numColumns", numColumns)
            .addVariableFieldAssignment(layoutVar, "marginLeft", margin)
            .addVariableFieldAssignment(layoutVar, "marginTop", margin)
            .addVariableFieldAssignment(layoutVar, "marginRight", margin)
            .addVariableFieldAssignment(layoutVar, "marginBottom", margin)
            .addVariableFieldAssignment(layoutVar, "horizontalSpacing", spacing)
            .addVariableFieldAssignment(layoutVar, "verticalSpacing", spacing);
    }
    
    
    //
    //
    //

    public static void generateChildrenLayoutData(
        UIDefinition    iUIDefinition,
        Layout          iLayout,
        Widget          iWidget,
        Widget[]        iChildren,
        ClassDefinition iClassDefinition )
    {
        int     numRows         = iLayout.getIntProperty(SWT_NUM_ROWS, 1);
        int     numColumns      = iLayout.getIntProperty(SWT_NUM_COLUMNS, 1);
        int[]   rowExpand       = new int[numRows];
        int[]   columnExpand    = new int[numColumns];

        // Determine the expand property for the rows and columns.
        getRowColumnExpandProperties(iChildren, rowExpand, columnExpand);
        
        // Determine the expand property for the widget based on the row/column
        // expand properties of the children.
        int widgetExpandHorizontal = getWidgetExpandHorizontalProperty(columnExpand);
        int widgetExpandVertical   = getWidgetExpandVerticalProperty(rowExpand);

        // Generate the layout data code for each child
        for ( int i = 0; i < iChildren.length; i++ )
        {
            Widget  child   = iChildren[i];
            int     row     = child.getIntProperty(SWT_ROW, 0);
            int     column  = child.getIntProperty(SWT_COLUMN, 0);
            
            int     childExpandHorizontal = getChildExpandHorizontalProperty(child);
            int     childExpandVertical   = getChildExpandVerticalProperty(child);

            generateChildLayoutData(
                iUIDefinition,
                child,
                widgetExpandHorizontal,
                widgetExpandVertical,
                rowExpand[row],
                columnExpand[column],
                childExpandHorizontal,
                childExpandVertical,
                iClassDefinition);
        }
    }
    
    
    //
    //
    //
    
    private static int getChildExpandHorizontalProperty( Widget iChild )
    {
        if ( iChild.containsProperty(SWT_EXPAND_HORIZONTAL) )
        {
            return iChild.getIntProperty(SWT_EXPAND_HORIZONTAL, SWT_EXPAND_PREFER_NO);
        }
        else if ( iChild.containsProperty(SWT_DEFAULT_EXPAND_HORIZONTAL) )
        {
            return iChild.getIntProperty(SWT_DEFAULT_EXPAND_HORIZONTAL, SWT_EXPAND_PREFER_NO);
        }
        else
        {
            return SWT_EXPAND_PREFER_NO;
        }
    }
    
    
    private static int getChildExpandVerticalProperty( Widget iChild )
    {
        if ( iChild.containsProperty(SWT_EXPAND_VERTICAL) )
        {
            return iChild.getIntProperty(SWT_EXPAND_VERTICAL, SWT_EXPAND_PREFER_NO);
        }
        else if ( iChild.containsProperty(SWT_DEFAULT_EXPAND_VERTICAL) )
        {
            return iChild.getIntProperty(SWT_DEFAULT_EXPAND_VERTICAL, SWT_EXPAND_PREFER_NO);
        }
        else
        {
            return SWT_EXPAND_PREFER_NO;
        }
    }
    
    
    private static int mergeRowColumnExpandProperty( int iExpand1, int iExpand2 )
    {
        if ( (iExpand1 == SWT_EXPAND_NO) || (iExpand2 == SWT_EXPAND_NO) )
        {
            return SWT_EXPAND_NO;
        }
        else if ( (iExpand1 == SWT_EXPAND_YES) || (iExpand2 == SWT_EXPAND_YES) )
        {
            return SWT_EXPAND_YES;
        }
        else if ( (iExpand1 == SWT_EXPAND_PREFER_YES) || (iExpand2 == SWT_EXPAND_PREFER_YES) )
        {
            return SWT_EXPAND_PREFER_YES;
        }
        else
        {
            return SWT_EXPAND_PREFER_NO;
        }
    }
    
    
    private static int mergeWidgetExpandProperty( int iExpand1, int iExpand2 )
    {
        if ( (iExpand1 == SWT_EXPAND_YES) || (iExpand2 == SWT_EXPAND_YES) )
        {
            return SWT_EXPAND_YES;
        }
        else if ( (iExpand1 == SWT_EXPAND_PREFER_YES) || (iExpand2 == SWT_EXPAND_PREFER_YES) )
        {
            return SWT_EXPAND_PREFER_YES;
        }
        else if ( (iExpand1 == SWT_EXPAND_PREFER_NO) || (iExpand2 == SWT_EXPAND_PREFER_NO) )
        {
            return SWT_EXPAND_PREFER_NO;
        }
        else
        {
            return SWT_EXPAND_NO;
        }
    }
    
    
    private static void getRowColumnExpandProperties(
        Widget[]    iChildren,
        int[]       iRowExpand,

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -