]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.views.swt.client/src/org/simantics/views/swt/client/base/SWTViewUtils.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.views.swt.client / src / org / simantics / views / swt / client / base / SWTViewUtils.java
1 package org.simantics.views.swt.client.base;
2
3 import org.eclipse.jface.layout.GridLayoutFactory;
4 import org.eclipse.jface.layout.RowLayoutFactory;
5 import org.eclipse.swt.SWT;
6 import org.eclipse.swt.layout.GridData;
7 import org.eclipse.swt.layout.GridLayout;
8 import org.eclipse.swt.layout.RowData;
9 import org.eclipse.swt.layout.RowLayout;
10 import org.eclipse.swt.widgets.Layout;
11 import org.simantics.browsing.ui.Column;
12 import org.simantics.browsing.ui.Column.Align;
13 import org.simantics.views.ViewUtils.ColumnBean;
14 import org.simantics.views.ViewUtils.GridDataBean;
15 import org.simantics.views.ViewUtils.GridLayoutBean;
16 import org.simantics.views.ViewUtils.LayoutBean;
17 import org.simantics.views.ViewUtils.LayoutDataBean;
18 import org.simantics.views.ViewUtils.RowDataBean;
19 import org.simantics.views.ViewUtils.RowLayoutBean;
20
21 public class SWTViewUtils {
22
23         public static Layout toLayout(GridLayoutBean bean) {
24                 GridLayout result = GridLayoutFactory.fillDefaults().numColumns(1).equalWidth(false).margins(0, 0).spacing(0, 0).create();
25                 result.numColumns = bean.numColumns;
26                 result.horizontalSpacing = bean.horizontalSpacing;
27                 result.verticalSpacing = bean.verticalSpacing;
28                 result.marginLeft = bean.marginLeft;
29                 result.marginRight = bean.marginRight;
30                 result.marginTop = bean.marginTop;
31                 result.marginBottom = bean.marginBottom;
32                 return result;
33         }
34
35         public static Layout toLayout(RowLayoutBean bean) {
36                 RowLayout result = RowLayoutFactory.fillDefaults().create();
37                 result.type = bean.type;
38                 result.spacing = bean.spacing;
39                 result.center = bean.center;
40                 result.fill = bean.fill;
41                 result.justify = bean.justify;
42                 result.pack = bean.pack;
43                 result.wrap = bean.wrap;
44                 result.marginLeft = bean.marginLeft;
45                 result.marginRight = bean.marginRight;
46                 result.marginTop = bean.marginTop;
47                 result.marginBottom = bean.marginBottom;
48                 return result;
49         }
50
51         public static GridData toGridData(GridDataBean bean) {
52                 GridData result = new GridData(SWT.FILL, SWT.FILL, true, false);
53                 result.horizontalSpan = bean.horizontalSpan;
54                 result.grabExcessHorizontalSpace = bean.grabExcessHorizontalSpace;
55                 result.grabExcessVerticalSpace = bean.grabExcessVerticalSpace;
56                 result.horizontalAlignment = bean.horizontalAlignment;
57                 result.verticalAlignment = bean.verticalAlignment;
58                 result.widthHint = bean.widthHint;
59                 result.heightHint = bean.heightHint;
60                 return result;
61                 
62         }
63
64         public static RowData toRowData(RowDataBean bean) {
65                 return new RowData(bean.width, bean.height);
66         }
67
68         public static Layout toLayout(LayoutBean layout) {
69                 if (layout instanceof GridLayoutBean)
70                         return toLayout((GridLayoutBean) layout);
71                 if (layout instanceof RowLayoutBean)
72                         return toLayout((RowLayoutBean) layout);
73                 throw new IllegalArgumentException("unrecognized layout: " + layout);
74         }
75
76
77         public static Object toLayoutData(LayoutDataBean layoutData) {
78                 if (layoutData instanceof GridDataBean)
79                         return toGridData((GridDataBean) layoutData);
80                 if (layoutData instanceof RowDataBean)
81                         return toRowData((RowDataBean) layoutData);
82                 throw new IllegalArgumentException("unrecognized layout data: " + layoutData);
83         }
84
85         private static Align getAlign(String alignment) {
86                 if("LEFT".equals(alignment)) return Align.LEFT;
87                 else if("CENTER".equals(alignment)) return Align.CENTER;
88                 else if("RIGHT".equals(alignment)) return Align.RIGHT;
89                 else throw new IllegalArgumentException("Align should be either LEFT, CENTER or RIGHT (was " + alignment + ")");
90         }
91         
92         public static Column toColumn(ColumnBean bean) {
93                 Align align = getAlign(bean.alignment);
94                 return new Column(bean.key, bean.label, align, bean.width, bean.tooltip, bean.grab, bean.weight);
95         }
96         
97 }