001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.ui.examples.readmetool;
011:
012: import org.eclipse.jface.preference.IPreferenceStore;
013: import org.eclipse.jface.preference.PreferencePage;
014: import org.eclipse.swt.SWT;
015: import org.eclipse.swt.events.ModifyEvent;
016: import org.eclipse.swt.events.ModifyListener;
017: import org.eclipse.swt.events.SelectionEvent;
018: import org.eclipse.swt.events.SelectionListener;
019: import org.eclipse.swt.layout.GridData;
020: import org.eclipse.swt.layout.GridLayout;
021: import org.eclipse.swt.widgets.Button;
022: import org.eclipse.swt.widgets.Composite;
023: import org.eclipse.swt.widgets.Control;
024: import org.eclipse.swt.widgets.Label;
025: import org.eclipse.swt.widgets.Text;
026: import org.eclipse.ui.IWorkbench;
027: import org.eclipse.ui.IWorkbenchPreferencePage;
028: import org.eclipse.ui.PlatformUI;
029:
030: /**
031: * This class implements a sample preference page that is
032: * added to the preference dialog based on the registration.
033: */
034: public class ReadmePreferencePage extends PreferencePage implements
035: IWorkbenchPreferencePage, SelectionListener, ModifyListener {
036: private Button radioButton1;
037:
038: private Button radioButton2;
039:
040: private Button radioButton3;
041:
042: private Button checkBox1;
043:
044: private Button checkBox2;
045:
046: private Button checkBox3;
047:
048: private Text textField;
049:
050: /**
051: * Creates an new checkbox instance and sets the default
052: * layout data.
053: *
054: * @param group the composite in which to create the checkbox
055: * @param label the string to set into the checkbox
056: * @return the new checkbox
057: */
058: private Button createCheckBox(Composite group, String label) {
059: Button button = new Button(group, SWT.CHECK | SWT.LEFT);
060: button.setText(label);
061: button.addSelectionListener(this );
062: GridData data = new GridData();
063: button.setLayoutData(data);
064: return button;
065: }
066:
067: /**
068: * Creates composite control and sets the default layout data.
069: *
070: * @param parent the parent of the new composite
071: * @param numColumns the number of columns for the new composite
072: * @return the newly-created coposite
073: */
074: private Composite createComposite(Composite parent, int numColumns) {
075: Composite composite = new Composite(parent, SWT.NULL);
076:
077: //GridLayout
078: GridLayout layout = new GridLayout();
079: layout.numColumns = numColumns;
080: composite.setLayout(layout);
081:
082: //GridData
083: GridData data = new GridData();
084: data.verticalAlignment = GridData.FILL;
085: data.horizontalAlignment = GridData.FILL;
086: composite.setLayoutData(data);
087: return composite;
088: }
089:
090: /** (non-Javadoc)
091: * Method declared on PreferencePage
092: */
093: protected Control createContents(Composite parent) {
094: PlatformUI.getWorkbench().getHelpSystem().setHelp(parent,
095: IReadmeConstants.PREFERENCE_PAGE_CONTEXT);
096:
097: //composite_textField << parent
098: Composite composite_textField = createComposite(parent, 2);
099: createLabel(composite_textField, MessageUtil
100: .getString("Text_Field")); //$NON-NLS-1$
101: textField = createTextField(composite_textField);
102: createPushButton(composite_textField, MessageUtil
103: .getString("Change")); //$NON-NLS-1$
104:
105: //composite_tab << parent
106: Composite composite_tab = createComposite(parent, 2);
107: createLabel(composite_tab, MessageUtil
108: .getString("Radio_Button_Options")); //$NON-NLS-1$
109:
110: //
111: tabForward(composite_tab);
112: //radio button composite << tab composite
113: Composite composite_radioButton = createComposite(
114: composite_tab, 1);
115: radioButton1 = createRadioButton(composite_radioButton,
116: MessageUtil.getString("Radio_button_1")); //$NON-NLS-1$
117: radioButton2 = createRadioButton(composite_radioButton,
118: MessageUtil.getString("Radio_button_2")); //$NON-NLS-1$
119: radioButton3 = createRadioButton(composite_radioButton,
120: MessageUtil.getString("Radio_button_3")); //$NON-NLS-1$
121:
122: //composite_tab2 << parent
123: Composite composite_tab2 = createComposite(parent, 2);
124: createLabel(composite_tab2, MessageUtil
125: .getString("Check_Box_Options")); //$NON-NLS-1$
126:
127: //
128: tabForward(composite_tab2);
129: //composite_checkBox << composite_tab2
130: Composite composite_checkBox = createComposite(composite_tab2,
131: 1);
132: checkBox1 = createCheckBox(composite_checkBox, MessageUtil
133: .getString("Check_box_1")); //$NON-NLS-1$
134: checkBox2 = createCheckBox(composite_checkBox, MessageUtil
135: .getString("Check_box_2")); //$NON-NLS-1$
136: checkBox3 = createCheckBox(composite_checkBox, MessageUtil
137: .getString("Check_box_3")); //$NON-NLS-1$
138:
139: initializeValues();
140:
141: //font = null;
142: return new Composite(parent, SWT.NULL);
143: }
144:
145: /**
146: * Utility method that creates a label instance
147: * and sets the default layout data.
148: *
149: * @param parent the parent for the new label
150: * @param text the text for the new label
151: * @return the new label
152: */
153: private Label createLabel(Composite parent, String text) {
154: Label label = new Label(parent, SWT.LEFT);
155: label.setText(text);
156: GridData data = new GridData();
157: data.horizontalSpan = 2;
158: data.horizontalAlignment = GridData.FILL;
159: label.setLayoutData(data);
160: return label;
161: }
162:
163: /**
164: * Utility method that creates a push button instance
165: * and sets the default layout data.
166: *
167: * @param parent the parent for the new button
168: * @param label the label for the new button
169: * @return the newly-created button
170: */
171: private Button createPushButton(Composite parent, String label) {
172: Button button = new Button(parent, SWT.PUSH);
173: button.setText(label);
174: button.addSelectionListener(this );
175: GridData data = new GridData();
176: data.horizontalAlignment = GridData.FILL;
177: button.setLayoutData(data);
178: return button;
179: }
180:
181: /**
182: * Utility method that creates a radio button instance
183: * and sets the default layout data.
184: *
185: * @param parent the parent for the new button
186: * @param label the label for the new button
187: * @return the newly-created button
188: */
189: private Button createRadioButton(Composite parent, String label) {
190: Button button = new Button(parent, SWT.RADIO | SWT.LEFT);
191: button.setText(label);
192: button.addSelectionListener(this );
193: GridData data = new GridData();
194: button.setLayoutData(data);
195: return button;
196: }
197:
198: /**
199: * Create a text field specific for this application
200: *
201: * @param parent the parent of the new text field
202: * @return the new text field
203: */
204: private Text createTextField(Composite parent) {
205: Text text = new Text(parent, SWT.SINGLE | SWT.BORDER);
206: text.addModifyListener(this );
207: GridData data = new GridData();
208: data.horizontalAlignment = GridData.FILL;
209: data.grabExcessHorizontalSpace = true;
210: data.verticalAlignment = GridData.CENTER;
211: data.grabExcessVerticalSpace = false;
212: text.setLayoutData(data);
213: return text;
214: }
215:
216: /**
217: * The <code>ReadmePreferencePage</code> implementation of this
218: * <code>PreferencePage</code> method
219: * returns preference store that belongs to the our plugin.
220: * This is important because we want to store
221: * our preferences separately from the workbench.
222: */
223: protected IPreferenceStore doGetPreferenceStore() {
224: return ReadmePlugin.getDefault().getPreferenceStore();
225: }
226:
227: /* (non-Javadoc)
228: * Method declared on IWorkbenchPreferencePage
229: */
230: public void init(IWorkbench workbench) {
231: // do nothing
232: }
233:
234: /**
235: * Initializes states of the controls using default values
236: * in the preference store.
237: */
238: private void initializeDefaults() {
239: IPreferenceStore store = getPreferenceStore();
240: checkBox1.setSelection(store
241: .getDefaultBoolean(IReadmeConstants.PRE_CHECK1));
242: checkBox2.setSelection(store
243: .getDefaultBoolean(IReadmeConstants.PRE_CHECK2));
244: checkBox3.setSelection(store
245: .getDefaultBoolean(IReadmeConstants.PRE_CHECK3));
246:
247: radioButton1.setSelection(false);
248: radioButton2.setSelection(false);
249: radioButton3.setSelection(false);
250: int choice = store
251: .getDefaultInt(IReadmeConstants.PRE_RADIO_CHOICE);
252: switch (choice) {
253: case 1:
254: radioButton1.setSelection(true);
255: break;
256: case 2:
257: radioButton2.setSelection(true);
258: break;
259: case 3:
260: radioButton3.setSelection(true);
261: break;
262: }
263: textField.setText(store
264: .getDefaultString(IReadmeConstants.PRE_TEXT));
265: }
266:
267: /**
268: * Initializes states of the controls from the preference store.
269: */
270: private void initializeValues() {
271: IPreferenceStore store = getPreferenceStore();
272: checkBox1.setSelection(store
273: .getBoolean(IReadmeConstants.PRE_CHECK1));
274: checkBox2.setSelection(store
275: .getBoolean(IReadmeConstants.PRE_CHECK2));
276: checkBox3.setSelection(store
277: .getBoolean(IReadmeConstants.PRE_CHECK3));
278:
279: int choice = store.getInt(IReadmeConstants.PRE_RADIO_CHOICE);
280: switch (choice) {
281: case 1:
282: radioButton1.setSelection(true);
283: break;
284: case 2:
285: radioButton2.setSelection(true);
286: break;
287: case 3:
288: radioButton3.setSelection(true);
289: break;
290: }
291: textField.setText(store.getString(IReadmeConstants.PRE_TEXT));
292: }
293:
294: /** (non-Javadoc)
295: * Method declared on ModifyListener
296: */
297: public void modifyText(ModifyEvent event) {
298: //Do nothing on a modification in this example
299: }
300:
301: /* (non-Javadoc)
302: * Method declared on PreferencePage
303: */
304: protected void performDefaults() {
305: super .performDefaults();
306: initializeDefaults();
307: }
308:
309: /* (non-Javadoc)
310: * Method declared on PreferencePage
311: */
312: public boolean performOk() {
313: storeValues();
314: ReadmePlugin.getDefault().savePluginPreferences();
315: return true;
316: }
317:
318: /**
319: * Stores the values of the controls back to the preference store.
320: */
321: private void storeValues() {
322: IPreferenceStore store = getPreferenceStore();
323: store.setValue(IReadmeConstants.PRE_CHECK1, checkBox1
324: .getSelection());
325: store.setValue(IReadmeConstants.PRE_CHECK2, checkBox2
326: .getSelection());
327: store.setValue(IReadmeConstants.PRE_CHECK3, checkBox3
328: .getSelection());
329:
330: int choice = 1;
331:
332: if (radioButton2.getSelection())
333: choice = 2;
334: else if (radioButton3.getSelection())
335: choice = 3;
336:
337: store.setValue(IReadmeConstants.PRE_RADIO_CHOICE, choice);
338: store.setValue(IReadmeConstants.PRE_TEXT, textField.getText());
339: }
340:
341: /**
342: * Creates a tab of one horizontal spans.
343: *
344: * @param parent the parent in which the tab should be created
345: */
346: private void tabForward(Composite parent) {
347: Label vfiller = new Label(parent, SWT.LEFT);
348: GridData gridData = new GridData();
349: gridData = new GridData();
350: gridData.horizontalAlignment = GridData.BEGINNING;
351: gridData.grabExcessHorizontalSpace = false;
352: gridData.verticalAlignment = GridData.CENTER;
353: gridData.grabExcessVerticalSpace = false;
354: vfiller.setLayoutData(gridData);
355: }
356:
357: /** (non-Javadoc)
358: * Method declared on SelectionListener
359: */
360: public void widgetDefaultSelected(SelectionEvent event) {
361: //Handle a default selection. Do nothing in this example
362: }
363:
364: /** (non-Javadoc)
365: * Method declared on SelectionListener
366: */
367: public void widgetSelected(SelectionEvent event) {
368: //Do nothing on selection in this example;
369: }
370: }
|