1 package com.stateofflow.eclipse.metrics.preferences.builder;
2
3 import java.util.Collection;
4
5 import org.eclipse.jface.preference.BooleanFieldEditor;
6 import org.eclipse.jface.preference.FieldEditor;
7 import org.eclipse.jface.preference.IPreferenceStore;
8 import org.eclipse.jface.preference.IntegerFieldEditor;
9 import org.eclipse.swt.SWT;
10 import org.eclipse.swt.layout.GridData;
11 import org.eclipse.swt.layout.GridLayout;
12 import org.eclipse.swt.widgets.Composite;
13 import org.eclipse.swt.widgets.Group;
14 import org.eclipse.swt.widgets.Label;
15 import org.eclipse.swt.widgets.TabFolder;
16 import org.eclipse.swt.widgets.TabItem;
17
18 import com.stateofflow.eclipse.metrics.metric.MetricId;
19 import com.stateofflow.eclipse.metrics.metric.MetricPropertyKey;
20
21 abstract class TabBuilder {
22 private final Collection<FieldEditor> fieldEditors;
23 private final IPreferenceStore preferenceStore;
24
25 public TabBuilder(final Collection<FieldEditor> fieldEditors, final IPreferenceStore preferenceStore) {
26 this.fieldEditors = fieldEditors;
27 this.preferenceStore = preferenceStore;
28 }
29
30 public abstract void create(TabFolder folder);
31
32 protected Composite createCompositeForTabItem(final TabFolder folder, final String tabName, final String tabDescription) {
33 final Composite root = new Composite(folder, SWT.NULL);
34 createMultiColumnLayout(root, 1);
35 addLabel(root, SWT.NULL, tabDescription);
36 addTab(folder, tabName, root);
37 return root;
38 }
39
40 private void addTab(final TabFolder folder, final String tabName, final Composite root) {
41 final TabItem tab = new TabItem(folder, SWT.NULL);
42 tab.setText(tabName);
43 tab.setControl(root);
44 }
45
46 protected Group createGroupForPreferredUpperBounds(final Composite root) {
47 final Group group = createGroup(root, "Preferred Upper Bounds", 2);
48 addLabel(group, SWT.LEFT, "Enable");
49 addLabel(group, SWT.LEFT, "Upper Bound");
50 return group;
51 }
52
53 protected Group createGroup(final Composite parent, final String title, final int numColumns) {
54 final Group group = new Group(parent, SWT.NULL);
55 group.setText(title);
56 createMultiColumnLayout(group, numColumns);
57 return group;
58 }
59
60 protected void addEnableMetricFields(final Composite parent, final MetricId id, final String displayName) {
61 addBooleanEditor(parent, id.createEnablementPropertyKey(), displayName);
62 addIntegerEditor(parent, id.createUpperBoundPropertyKey());
63 }
64
65 protected void addBooleanEditor(final Composite parent, final MetricPropertyKey preferenceKey, final String displayName) {
66 final Composite composite = new Composite(parent, SWT.NULL);
67 composite.setLayout(new GridLayout());
68 composite.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.GRAB_HORIZONTAL));
69 final BooleanFieldEditor button = new BooleanFieldEditor(preferenceKey.toString(), displayName, composite);
70
71 initializeFieldEditor(button);
72 }
73
74 private void addIntegerEditor(final Composite parent, final MetricPropertyKey key) {
75 final Composite editorComposite = new Composite(parent, SWT.NULL);
76 editorComposite.setLayout(new GridLayout());
77 editorComposite.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.GRAB_HORIZONTAL));
78 final IntegerFieldEditor editor = new IntegerFieldEditor(key.toString(), "", editorComposite);
79
80 initializeFieldEditor(editor);
81 }
82
83 private void initializeFieldEditor(final FieldEditor editor) {
84 fieldEditors.add(editor);
85
86 editor.setPreferenceStore(preferenceStore);
87 editor.load();
88 }
89
90 private void addLabel(final Composite parent, final int flags, final String text) {
91 new Label(parent, flags).setText(text);
92 }
93
94 private void createMultiColumnLayout(final Composite parent, final int numColumns) {
95 final GridLayout layout = new GridLayout();
96 layout.numColumns = numColumns;
97 parent.setLayout(layout);
98 parent.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.GRAB_HORIZONTAL));
99 }
100 }
101