View Javadoc

1   /* ======================================
2    * Copyright (c) 2004-2005 Achim Nierbeck
3    * All rights reserved.
4    *
5    * You may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *       http://www.gnu.org/licenses/lgpl.html
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   * ======================================
16   */
17  
18  /*
19   * TimeTrackPropertyPage.java
20   * $Revision: 1.3 $
21   * Date 14.12.2005
22   * 
23   * $Author: nierbeck $ 
24   * $Date: 2006/02/02 21:31:58 $ 
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  		// Label for path field
73  		Label pathLabel = new Label(composite, SWT.NONE);
74  		pathLabel.setText(PATH_TITLE);
75  
76  		// Path text field
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  		// Label for owner field
94  		Label ownerLabel = new Label(composite, SWT.NONE);
95  		ownerLabel.setText(OWNER_TITLE);
96  
97  		// Owner text field
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 		// Populate owner text field
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 		// Populate the owner text field with the default value
153 		ownerText.setText(DEFAULT_OWNER);
154 	}
155 
156 	/***
157 	 * @return true if the Properties where stored
158 	 */
159 	public final boolean performOk() {
160 		// store the value in the owner text field
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 }