1 package net.sf.commonclipse.preferences;
2
3 import org.eclipse.jface.preference.FieldEditor;
4 import org.eclipse.jface.preference.FieldEditorPreferencePage;
5 import org.eclipse.jface.resource.ImageDescriptor;
6 import org.eclipse.swt.SWT;
7 import org.eclipse.swt.layout.GridData;
8 import org.eclipse.swt.layout.GridLayout;
9 import org.eclipse.swt.widgets.Composite;
10 import org.eclipse.swt.widgets.TabFolder;
11 import org.eclipse.swt.widgets.TabItem;
12
13
14 /***
15 * @author fgiust
16 * @version $Revision $ ($Author $)
17 */
18 public abstract class TabbedFieldEditorPreferencePage extends FieldEditorPreferencePage
19 {
20
21 /***
22 * Tab folder.
23 */
24 private TabFolder folder;
25
26 /***
27 * Maximum number of columns for field editors.
28 */
29 private int maxNumOfColumns;
30
31 /***
32 * Creates a new field editor preference page with the given style, an empty title, and no image.
33 * @param style either <code>GRID</code> or <code>FLAT</code>
34 */
35 protected TabbedFieldEditorPreferencePage(int style)
36 {
37 super(style);
38 }
39
40 /***
41 * Creates a new field editor preference page with the given title and style, but no image.
42 * @param title the title of this preference page
43 * @param style either <code>GRID</code> or <code>FLAT</code>
44 */
45 protected TabbedFieldEditorPreferencePage(String title, int style)
46 {
47 super(title, style);
48 }
49
50 /***
51 * Creates a new field editor preference page with the given title, image, and style.
52 * @param title the title of this preference page
53 * @param image the image for this preference page, or <code>null</code> if none
54 * @param style either <code>GRID</code> or <code>FLAT</code>
55 */
56 protected TabbedFieldEditorPreferencePage(String title, ImageDescriptor image, int style)
57 {
58 super(title, image, style);
59 }
60
61 /***
62 * Adds the given field editor to this page.
63 * @param editor the field editor
64 */
65 protected void addField(FieldEditor editor)
66 {
67
68 this.maxNumOfColumns = Math.max(this.maxNumOfColumns, editor.getNumberOfControls());
69 super.addField(editor);
70 }
71
72 /***
73 * Adjust the layout of the field editors so that they are properly aligned.
74 */
75 protected void adjustGridLayout()
76 {
77 if (folder != null)
78 {
79 TabItem[] items = folder.getItems();
80 for (int j = 0; j < items.length; j++)
81 {
82 GridLayout layout = ((GridLayout) ((Composite) items[j].getControl()).getLayout());
83 layout.numColumns = this.maxNumOfColumns;
84 layout.marginHeight = 5;
85 layout.marginWidth = 5;
86 }
87 }
88
89
90 super.adjustGridLayout();
91
92
93 ((GridLayout) super.getFieldEditorParent().getLayout()).numColumns = 1;
94 }
95
96 /***
97 * Returns a parent composite for a field editor.
98 * <p>
99 * This value must not be cached since a new parent may be created each time this method called. Thus this method
100 * must be called each time a field editor is constructed.
101 * </p>
102 * @return a parent
103 */
104 protected Composite getFieldEditorParent()
105 {
106 if (folder == null || folder.getItemCount() == 0)
107 {
108 return super.getFieldEditorParent();
109 }
110 return (Composite) folder.getItem(folder.getItemCount() - 1).getControl();
111 }
112
113 /***
114 * Adds a tab to the page.
115 * @param text the tab label
116 */
117 public void addTab(String text)
118 {
119 if (folder == null)
120 {
121
122 folder = new TabFolder(super.getFieldEditorParent(), SWT.NONE);
123 folder.setLayoutData(new GridData(GridData.FILL_BOTH));
124 }
125
126 TabItem item = new TabItem(folder, SWT.NONE);
127 item.setText(text);
128
129 Composite currentTab = new Composite(folder, SWT.NULL);
130 GridLayout layout = new GridLayout();
131 currentTab.setLayout(layout);
132 currentTab.setFont(super.getFieldEditorParent().getFont());
133 currentTab.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
134
135 item.setControl(currentTab);
136 }
137
138 }