1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28 package de.nierbeck.timeTrack.properties;
29
30 import org.eclipse.core.resources.IResource;
31 import org.eclipse.core.runtime.CoreException;
32 import org.eclipse.core.runtime.QualifiedName;
33 import org.eclipse.swt.SWT;
34 import org.eclipse.swt.layout.GridData;
35 import org.eclipse.swt.layout.GridLayout;
36 import org.eclipse.swt.widgets.Composite;
37 import org.eclipse.swt.widgets.Control;
38 import org.eclipse.swt.widgets.Label;
39 import org.eclipse.swt.widgets.Text;
40 import org.eclipse.ui.dialogs.PropertyPage;
41
42 /***
43 * Builds the Property Page
44 *
45 * @author Achim
46 *
47 */
48 public class TimeTrackPropertyPage extends PropertyPage {
49
50 private static final String PATH_TITLE = "Path:";
51
52 private static final String OWNER_TITLE = "&Owner:";
53
54 private static final String OWNER_PROPERTY = "OWNER";
55
56 private static final String DEFAULT_OWNER = "John Doe";
57
58 private static final int TEXT_FIELD_WIDTH = 50;
59
60 private Text ownerText;
61
62 /***
63 * Constructor for SamplePropertyPage.
64 */
65 public TimeTrackPropertyPage() {
66 super();
67 }
68
69 private void addFirstSection(Composite parent) {
70 Composite composite = createDefaultComposite(parent);
71
72
73 Label pathLabel = new Label(composite, SWT.NONE);
74 pathLabel.setText(PATH_TITLE);
75
76
77 Text pathValueText = new Text(composite, SWT.WRAP | SWT.READ_ONLY);
78 pathValueText.setText(((IResource) getElement()).getFullPath()
79 .toString());
80 }
81
82 private void addSeparator(Composite parent) {
83 Label separator = new Label(parent, SWT.SEPARATOR | SWT.HORIZONTAL);
84 GridData gridData = new GridData();
85 gridData.horizontalAlignment = GridData.FILL;
86 gridData.grabExcessHorizontalSpace = true;
87 separator.setLayoutData(gridData);
88 }
89
90 private void addSecondSection(Composite parent) {
91 Composite composite = createDefaultComposite(parent);
92
93
94 Label ownerLabel = new Label(composite, SWT.NONE);
95 ownerLabel.setText(OWNER_TITLE);
96
97
98 ownerText = new Text(composite, SWT.SINGLE | SWT.BORDER);
99 GridData gd = new GridData();
100 gd.widthHint = convertWidthInCharsToPixels(TEXT_FIELD_WIDTH);
101 ownerText.setLayoutData(gd);
102
103
104 try {
105 String owner = ((IResource) getElement())
106 .getPersistentProperty(new QualifiedName("", OWNER_PROPERTY));
107 ownerText.setText((owner != null) ? owner : DEFAULT_OWNER);
108 } catch (CoreException e) {
109 ownerText.setText(DEFAULT_OWNER);
110 }
111 }
112
113 /***
114 * @return the Preference Page
115 *
116 * @param parent - the parent on wich this dialog is created
117 *
118 * @see PreferencePage#createContents(Composite)
119 */
120 protected final Control createContents(Composite parent) {
121 Composite composite = new Composite(parent, SWT.NONE);
122 GridLayout layout = new GridLayout();
123 composite.setLayout(layout);
124 GridData data = new GridData(GridData.FILL);
125 data.grabExcessHorizontalSpace = true;
126 composite.setLayoutData(data);
127
128 addFirstSection(composite);
129 addSeparator(composite);
130 addSecondSection(composite);
131 return composite;
132 }
133
134 private Composite createDefaultComposite(Composite parent) {
135 Composite composite = new Composite(parent, SWT.NULL);
136 GridLayout layout = new GridLayout();
137 layout.numColumns = 2;
138 composite.setLayout(layout);
139
140 GridData data = new GridData();
141 data.verticalAlignment = GridData.FILL;
142 data.horizontalAlignment = GridData.FILL;
143 composite.setLayoutData(data);
144
145 return composite;
146 }
147
148 /***
149 *
150 */
151 protected final void performDefaults() {
152
153 ownerText.setText(DEFAULT_OWNER);
154 }
155
156 /***
157 * @return true if the Properties where stored
158 */
159 public final boolean performOk() {
160
161 try {
162 ((IResource) getElement()).setPersistentProperty(new QualifiedName(
163 "", OWNER_PROPERTY), ownerText.getText());
164 } catch (CoreException e) {
165 return false;
166 }
167 return true;
168 }
169
170 }