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