comparison dwtx/jface/preference/StringButtonFieldEditor.d @ 34:b3c8e32d406f

preference
author Frank Benoit <benoit@tionex.de>
date Sat, 05 Apr 2008 01:45:47 +0200
parents
children ea8ff534f622
comparison
equal deleted inserted replaced
33:f25582573129 34:b3c8e32d406f
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 * Port to the D programming language:
12 * Frank Benoit <benoit@tionex.de>
13 *******************************************************************************/
14 module dwtx.jface.preference.StringButtonFieldEditor;
15
16 import dwtx.jface.preference.StringFieldEditor;
17
18 import dwt.DWT;
19 import dwt.events.DisposeEvent;
20 import dwt.events.DisposeListener;
21 import dwt.events.SelectionAdapter;
22 import dwt.events.SelectionEvent;
23 import dwt.graphics.Point;
24 import dwt.layout.GridData;
25 import dwt.widgets.Button;
26 import dwt.widgets.Composite;
27 import dwt.widgets.Shell;
28 import dwtx.core.runtime.Assert;
29 import dwtx.jface.dialogs.IDialogConstants;
30 import dwtx.jface.resource.JFaceResources;
31
32 import dwt.dwthelper.utils;
33
34 /**
35 * An abstract field editor for a string type preference that presents
36 * a string input field with a change button to its right to edit the
37 * input field's content. When the user presses the change button, the
38 * abstract framework method <code>changePressed()</code> gets called
39 * to compute a new string.
40 */
41 public abstract class StringButtonFieldEditor : StringFieldEditor {
42
43 /**
44 * The change button, or <code>null</code> if none
45 * (before creation and after disposal).
46 */
47 private Button changeButton;
48
49 /**
50 * The text for the change button, or <code>null</code> if missing.
51 */
52 private String changeButtonText;
53
54 /**
55 * Creates a new string button field editor
56 */
57 protected this() {
58 }
59
60 /**
61 * Creates a string button field editor.
62 *
63 * @param name the name of the preference this field editor works on
64 * @param labelText the label text of the field editor
65 * @param parent the parent of the field editor's control
66 */
67 protected this(String name, String labelText,
68 Composite parent) {
69 init(name, labelText);
70 createControl(parent);
71 }
72
73 /* (non-Javadoc)
74 * Method declared on FieldEditor.
75 */
76 protected void adjustForNumColumns(int numColumns) {
77 (cast(GridData) getTextControl().getLayoutData()).horizontalSpan = numColumns - 2;
78 }
79
80 /**
81 * Notifies that this field editor's change button has been pressed.
82 * <p>
83 * Subclasses must implement this method to provide a corresponding
84 * new string for the text field. If the returned value is <code>null</code>,
85 * the currently displayed value remains.
86 * </p>
87 *
88 * @return the new string to display, or <code>null</code> to leave the
89 * old string showing
90 */
91 protected abstract String changePressed();
92
93 /* (non-Javadoc)
94 * Method declared on StringFieldEditor (and FieldEditor).
95 */
96 protected void doFillIntoGrid(Composite parent, int numColumns) {
97 super.doFillIntoGrid(parent, numColumns - 1);
98 changeButton = getChangeControl(parent);
99 GridData gd = new GridData();
100 gd.horizontalAlignment = GridData.FILL;
101 int widthHint = convertHorizontalDLUsToPixels(changeButton,
102 IDialogConstants.BUTTON_WIDTH);
103 gd.widthHint = Math.max(widthHint, changeButton.computeSize(
104 DWT.DEFAULT, DWT.DEFAULT, true).x);
105 changeButton.setLayoutData(gd);
106 }
107
108 /**
109 * Get the change control. Create it in parent if required.
110 * @param parent
111 * @return Button
112 */
113 protected Button getChangeControl(Composite parent) {
114 if (changeButton is null) {
115 changeButton = new Button(parent, DWT.PUSH);
116 if (changeButtonText is null) {
117 changeButtonText = JFaceResources.getString("openChange"); //$NON-NLS-1$
118 }
119 changeButton.setText(changeButtonText);
120 changeButton.setFont(parent.getFont());
121 changeButton.addSelectionListener(new class SelectionAdapter {
122 public void widgetSelected(SelectionEvent evt) {
123 String newValue = changePressed();
124 if (newValue !is null) {
125 setStringValue(newValue);
126 }
127 }
128 });
129 changeButton.addDisposeListener(new class DisposeListener {
130 public void widgetDisposed(DisposeEvent event) {
131 changeButton = null;
132 }
133 });
134 } else {
135 checkParent(changeButton, parent);
136 }
137 return changeButton;
138 }
139
140 /* (non-Javadoc)
141 * Method declared on FieldEditor.
142 */
143 public int getNumberOfControls() {
144 return 3;
145 }
146
147 /**
148 * Returns this field editor's shell.
149 *
150 * @return the shell
151 */
152 protected Shell getShell() {
153 if (changeButton is null) {
154 return null;
155 }
156 return changeButton.getShell();
157 }
158
159 /**
160 * Sets the text of the change button.
161 *
162 * @param text the new text
163 */
164 public void setChangeButtonText(String text) {
165 Assert.isNotNull(text);
166 changeButtonText = text;
167 if (changeButton !is null) {
168 changeButton.setText(text);
169 Point prefSize = changeButton.computeSize(DWT.DEFAULT, DWT.DEFAULT);
170 GridData data = cast(GridData)changeButton.getLayoutData();
171 data.widthHint = Math.max(DWT.DEFAULT, prefSize.x);
172 }
173 }
174
175 /* (non-Javadoc)
176 * @see dwtx.jface.preference.FieldEditor#setEnabled(bool, dwt.widgets.Composite)
177 */
178 public void setEnabled(bool enabled, Composite parent) {
179 super.setEnabled(enabled, parent);
180 if (changeButton !is null) {
181 changeButton.setEnabled(enabled);
182 }
183 }
184
185 }