001: /*******************************************************************************
002: * Copyright (c) 2000, 2005 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.jdt.internal.ui.wizards.dialogfields;
011:
012: import org.eclipse.swt.SWT;
013: import org.eclipse.swt.events.ModifyEvent;
014: import org.eclipse.swt.events.ModifyListener;
015: import org.eclipse.swt.layout.GridData;
016: import org.eclipse.swt.widgets.Composite;
017: import org.eclipse.swt.widgets.Control;
018: import org.eclipse.swt.widgets.Label;
019: import org.eclipse.swt.widgets.Text;
020:
021: import org.eclipse.jface.text.contentassist.IContentAssistProcessor;
022:
023: import org.eclipse.jdt.internal.ui.refactoring.contentassist.ControlContentAssistHelper;
024:
025: /**
026: * Dialog field containing a label and a text control.
027: */
028: public class StringDialogField extends DialogField {
029:
030: private String fText;
031: private Text fTextControl;
032: private ModifyListener fModifyListener;
033: private IContentAssistProcessor fContentAssistProcessor;
034:
035: public StringDialogField() {
036: super ();
037: fText = ""; //$NON-NLS-1$
038: }
039:
040: public void setContentAssistProcessor(
041: IContentAssistProcessor processor) {
042: fContentAssistProcessor = processor;
043: if (fContentAssistProcessor != null && isOkToUse(fTextControl)) {
044: ControlContentAssistHelper.createTextContentAssistant(
045: fTextControl, fContentAssistProcessor);
046: }
047: }
048:
049: public IContentAssistProcessor getContentAssistProcessor() {
050: return fContentAssistProcessor;
051: }
052:
053: // ------- layout helpers
054:
055: /*
056: * @see DialogField#doFillIntoGrid
057: */
058: public Control[] doFillIntoGrid(Composite parent, int nColumns) {
059: assertEnoughColumns(nColumns);
060:
061: Label label = getLabelControl(parent);
062: label.setLayoutData(gridDataForLabel(1));
063: Text text = getTextControl(parent);
064: text.setLayoutData(gridDataForText(nColumns - 1));
065:
066: return new Control[] { label, text };
067: }
068:
069: /*
070: * @see DialogField#getNumberOfControls
071: */
072: public int getNumberOfControls() {
073: return 2;
074: }
075:
076: protected static GridData gridDataForText(int span) {
077: GridData gd = new GridData();
078: gd.horizontalAlignment = GridData.FILL;
079: gd.grabExcessHorizontalSpace = false;
080: gd.horizontalSpan = span;
081: return gd;
082: }
083:
084: // ------- focus methods
085:
086: /*
087: * @see DialogField#setFocus
088: */
089: public boolean setFocus() {
090: if (isOkToUse(fTextControl)) {
091: fTextControl.setFocus();
092: fTextControl.setSelection(0, fTextControl.getText()
093: .length());
094: }
095: return true;
096: }
097:
098: // ------- ui creation
099:
100: /**
101: * Creates or returns the created text control.
102: * @param parent The parent composite or <code>null</code> when the widget has
103: * already been created.
104: */
105: public Text getTextControl(Composite parent) {
106: if (fTextControl == null) {
107: assertCompositeNotNull(parent);
108: fModifyListener = new ModifyListener() {
109: public void modifyText(ModifyEvent e) {
110: doModifyText(e);
111: }
112: };
113:
114: fTextControl = new Text(parent, SWT.SINGLE | SWT.BORDER);
115: // moved up due to 1GEUNW2
116: fTextControl.setText(fText);
117: fTextControl.setFont(parent.getFont());
118: fTextControl.addModifyListener(fModifyListener);
119:
120: fTextControl.setEnabled(isEnabled());
121: if (fContentAssistProcessor != null) {
122: ControlContentAssistHelper.createTextContentAssistant(
123: fTextControl, fContentAssistProcessor);
124: }
125: }
126: return fTextControl;
127: }
128:
129: private void doModifyText(ModifyEvent e) {
130: if (isOkToUse(fTextControl)) {
131: fText = fTextControl.getText();
132: }
133: dialogFieldChanged();
134: }
135:
136: // ------ enable / disable management
137:
138: /*
139: * @see DialogField#updateEnableState
140: */
141: protected void updateEnableState() {
142: super .updateEnableState();
143: if (isOkToUse(fTextControl)) {
144: fTextControl.setEnabled(isEnabled());
145: }
146: }
147:
148: // ------ text access
149:
150: /**
151: * Gets the text. Can not be <code>null</code>
152: */
153: public String getText() {
154: return fText;
155: }
156:
157: /**
158: * Sets the text. Triggers a dialog-changed event.
159: */
160: public void setText(String text) {
161: fText = text;
162: if (isOkToUse(fTextControl)) {
163: fTextControl.setText(text);
164: } else {
165: dialogFieldChanged();
166: }
167: }
168:
169: /**
170: * Sets the text without triggering a dialog-changed event.
171: */
172: public void setTextWithoutUpdate(String text) {
173: fText = text;
174: if (isOkToUse(fTextControl)) {
175: fTextControl.removeModifyListener(fModifyListener);
176: fTextControl.setText(text);
177: fTextControl.addModifyListener(fModifyListener);
178: }
179: }
180:
181: /* (non-Javadoc)
182: * @see org.eclipse.jdt.internal.ui.wizards.dialogfields.DialogField#refresh()
183: */
184: public void refresh() {
185: super.refresh();
186: if (isOkToUse(fTextControl)) {
187: setTextWithoutUpdate(fText);
188: }
189: }
190:
191: }
|