KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jface > preference > StringButtonFieldEditor


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  * Thierry Lach - thierry.lach@bbdodetroit.com - Fix for Bug 37155
11  *******************************************************************************/

12 package org.eclipse.jface.preference;
13
14 import org.eclipse.jface.dialogs.IDialogConstants;
15 import org.eclipse.jface.resource.JFaceResources;
16 import org.eclipse.core.runtime.Assert;
17 import org.eclipse.swt.SWT;
18 import org.eclipse.swt.events.DisposeEvent;
19 import org.eclipse.swt.events.DisposeListener;
20 import org.eclipse.swt.events.SelectionAdapter;
21 import org.eclipse.swt.events.SelectionEvent;
22 import org.eclipse.swt.graphics.Point;
23 import org.eclipse.swt.layout.GridData;
24 import org.eclipse.swt.widgets.Button;
25 import org.eclipse.swt.widgets.Composite;
26 import org.eclipse.swt.widgets.Shell;
27
28 /**
29  * An abstract field editor for a string type preference that presents
30  * a string input field with a change button to its right to edit the
31  * input field's content. When the user presses the change button, the
32  * abstract framework method <code>changePressed()</code> gets called
33  * to compute a new string.
34  */

35 public abstract class StringButtonFieldEditor extends StringFieldEditor {
36
37     /**
38      * The change button, or <code>null</code> if none
39      * (before creation and after disposal).
40      */

41     private Button changeButton;
42
43     /**
44      * The text for the change button, or <code>null</code> if missing.
45      */

46     private String JavaDoc changeButtonText;
47
48     /**
49      * Creates a new string button field editor
50      */

51     protected StringButtonFieldEditor() {
52     }
53
54     /**
55      * Creates a string button field editor.
56      *
57      * @param name the name of the preference this field editor works on
58      * @param labelText the label text of the field editor
59      * @param parent the parent of the field editor's control
60      */

61     protected StringButtonFieldEditor(String JavaDoc name, String JavaDoc labelText,
62             Composite parent) {
63         init(name, labelText);
64         createControl(parent);
65     }
66
67     /* (non-Javadoc)
68      * Method declared on FieldEditor.
69      */

70     protected void adjustForNumColumns(int numColumns) {
71         ((GridData) getTextControl().getLayoutData()).horizontalSpan = numColumns - 2;
72     }
73
74     /**
75      * Notifies that this field editor's change button has been pressed.
76      * <p>
77      * Subclasses must implement this method to provide a corresponding
78      * new string for the text field. If the returned value is <code>null</code>,
79      * the currently displayed value remains.
80      * </p>
81      *
82      * @return the new string to display, or <code>null</code> to leave the
83      * old string showing
84      */

85     protected abstract String JavaDoc changePressed();
86
87     /* (non-Javadoc)
88      * Method declared on StringFieldEditor (and FieldEditor).
89      */

90     protected void doFillIntoGrid(Composite parent, int numColumns) {
91         super.doFillIntoGrid(parent, numColumns - 1);
92         changeButton = getChangeControl(parent);
93         GridData gd = new GridData();
94         gd.horizontalAlignment = GridData.FILL;
95         int widthHint = convertHorizontalDLUsToPixels(changeButton,
96                 IDialogConstants.BUTTON_WIDTH);
97         gd.widthHint = Math.max(widthHint, changeButton.computeSize(
98                 SWT.DEFAULT, SWT.DEFAULT, true).x);
99         changeButton.setLayoutData(gd);
100     }
101
102     /**
103      * Get the change control. Create it in parent if required.
104      * @param parent
105      * @return Button
106      */

107     protected Button getChangeControl(Composite parent) {
108         if (changeButton == null) {
109             changeButton = new Button(parent, SWT.PUSH);
110             if (changeButtonText == null) {
111                 changeButtonText = JFaceResources.getString("openChange"); //$NON-NLS-1$
112
}
113             changeButton.setText(changeButtonText);
114             changeButton.setFont(parent.getFont());
115             changeButton.addSelectionListener(new SelectionAdapter() {
116                 public void widgetSelected(SelectionEvent evt) {
117                     String JavaDoc newValue = changePressed();
118                     if (newValue != null) {
119                         setStringValue(newValue);
120                     }
121                 }
122             });
123             changeButton.addDisposeListener(new DisposeListener() {
124                 public void widgetDisposed(DisposeEvent event) {
125                     changeButton = null;
126                 }
127             });
128         } else {
129             checkParent(changeButton, parent);
130         }
131         return changeButton;
132     }
133
134     /* (non-Javadoc)
135      * Method declared on FieldEditor.
136      */

137     public int getNumberOfControls() {
138         return 3;
139     }
140
141     /**
142      * Returns this field editor's shell.
143      *
144      * @return the shell
145      */

146     protected Shell getShell() {
147         if (changeButton == null) {
148             return null;
149         }
150         return changeButton.getShell();
151     }
152
153     /**
154      * Sets the text of the change button.
155      *
156      * @param text the new text
157      */

158     public void setChangeButtonText(String JavaDoc text) {
159         Assert.isNotNull(text);
160         changeButtonText = text;
161         if (changeButton != null) {
162             changeButton.setText(text);
163             Point prefSize = changeButton.computeSize(SWT.DEFAULT, SWT.DEFAULT);
164             GridData data = (GridData)changeButton.getLayoutData();
165             data.widthHint = Math.max(SWT.DEFAULT, prefSize.x);
166         }
167     }
168
169     /* (non-Javadoc)
170      * @see org.eclipse.jface.preference.FieldEditor#setEnabled(boolean, org.eclipse.swt.widgets.Composite)
171      */

172     public void setEnabled(boolean enabled, Composite parent) {
173         super.setEnabled(enabled, parent);
174         if (changeButton != null) {
175             changeButton.setEnabled(enabled);
176         }
177     }
178
179 }
180
Popular Tags