1   package com.stateofflow.eclipse.metrics.swt;
2   
3   import java.util.Stack;
4   
5   import org.eclipse.jface.preference.IntegerFieldEditor;
6   import org.eclipse.jface.preference.StringFieldEditor;
7   import org.eclipse.jface.util.IPropertyChangeListener;
8   import org.eclipse.swt.SWT;
9   import org.eclipse.swt.events.SelectionListener;
10  import org.eclipse.swt.layout.GridData;
11  import org.eclipse.swt.layout.GridLayout;
12  import org.eclipse.swt.widgets.Button;
13  import org.eclipse.swt.widgets.Composite;
14  import org.eclipse.swt.widgets.Group;
15  import org.eclipse.swt.widgets.Label;
16  
17  public class SWTBuilder {
18      private final Stack<Composite> containers;
19  
20      public SWTBuilder(final Composite parent) {
21          containers = new Stack<Composite>();
22          push(parent);
23      }
24  
25      private void push(final Composite container) {
26          containers.push(container);
27      }
28  
29      public SWTBuilder pop() {
30          containers.pop();
31          return this;
32      }
33  
34      public Composite peek() {
35          return containers.peek();
36      }
37  
38      public SWTBuilder addComposite() {
39          push(new Composite(peek(), SWT.NULL));
40          return this;
41      }
42  
43      public SWTBuilder addGroup(final String title) {
44          final Group group = new Group(peek(), SWT.NULL);
45          group.setText(title);
46          push(group);
47          return this;
48      }
49  
50      public SWTBuilder addGroup() {
51          return addGroup("");
52      }
53  
54      public SWTBuilder setGridLayout(final int columns) {
55          final GridLayout layout = new GridLayout(columns, false);
56          peek().setLayout(layout);
57          peek().setLayoutData(new GridData(SWT.FILL, SWT.NONE, true, false));
58          return this;
59      }
60  
61      public SWTBuilder addLabel(final String labelText) {
62          new Label(peek(), SWT.NULL).setText(labelText);
63          return this;
64      }
65  
66      public SWTBuilder addPushButton(final String text, final SelectionListener listener) {
67          createButton(text, SWT.PUSH, listener);
68          return this;
69      }
70  
71      public Button createCheckbox(final String label, final SelectionListener listener) {
72          return createButton(label, SWT.CHECK, listener);
73      }
74  
75      public Button createCheckbox(final String label) {
76          return createButton(label, SWT.CHECK);
77      }
78  
79      private Button createButton(final String label, final int flags) {
80          final Button button = new Button(peek(), flags);
81          button.setText(label);
82          return button;
83      }
84  
85      private Button createButton(final String label, final int flags, final SelectionListener listener) {
86          final Button button = createButton(label, flags);
87          button.addSelectionListener(listener);
88          return button;
89      }
90  
91      public StringFieldEditor createStringFieldEditor(final String label, final IPropertyChangeListener listener) {
92          addLabel(label);
93          addComposite().setGridLayout(1);
94          final StringFieldEditor editor = new StringFieldEditor("", "", peek());
95          pop();
96          editor.setPropertyChangeListener(listener);
97          return editor;
98      }
99  
100     public StringFieldEditor createDisabledStringFieldEditor(final String label) {
101         addLabel(label);
102         addComposite().setGridLayout(1);
103         final StringFieldEditor editor = new StringFieldEditor("", "", peek());
104         editor.setEnabled(false, peek());
105         pop();
106         return editor;
107     }
108 
109     public IntegerFieldEditor createIntegerFieldEditor(final String label, final int textLimit, final IPropertyChangeListener listener) {
110         addLabel(label);
111         addComposite().setGridLayout(1);
112         final IntegerFieldEditor editor = new IntegerFieldEditor("", "", peek(), textLimit);
113         pop();
114         editor.setPropertyChangeListener(listener);
115         return editor;
116     }
117 }
118