/*******************************************************************************
* Copyright (c) 2009 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* IBM Corporation - initial API and implementation
* Zend Technologies
*******************************************************************************/
package org.eclipse.php.internal.ui.wizards.fields;
import org.eclipse.jface.contentassist.SubjectControlContentAssistant;
import org.eclipse.jface.text.DefaultInformationControl;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.contentassist.IContentAssistProcessor;
import org.eclipse.jface.text.contentassist.IContentAssistant;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.contentassist.ContentAssistHandler;
import org.eclipse.wst.sse.ui.internal.derived.HTMLTextPresenter;
/**
* Dialog field containing a label and a text control.
*/
public class StringDialogField extends DialogField {
protected String fText;
protected Text fTextControl;
protected ModifyListener fModifyListener;
protected IContentAssistProcessor fContentAssistProcessor;
public StringDialogField() {
super();
fText = ""; //$NON-NLS-1$
}
public void setContentAssistProcessor(IContentAssistProcessor processor) {
fContentAssistProcessor = processor;
if (fContentAssistProcessor != null && isOkToUse(fTextControl)) {
ContentAssistHandler.createHandlerForText(fTextControl, createPHPContentAssistant(fContentAssistProcessor));
}
}
public static SubjectControlContentAssistant createPHPContentAssistant(IContentAssistProcessor processor) {
final SubjectControlContentAssistant contentAssistant = new SubjectControlContentAssistant();
contentAssistant.setContentAssistProcessor(processor, IDocument.DEFAULT_CONTENT_TYPE);
contentAssistant.setContextInformationPopupOrientation(IContentAssistant.CONTEXT_INFO_ABOVE);
contentAssistant.setInformationControlCreator(
parent -> new DefaultInformationControl(parent, SWT.NONE, new HTMLTextPresenter(true)));
return contentAssistant;
}
public IContentAssistProcessor getContentAssistProcessor() {
return fContentAssistProcessor;
}
// ------- layout helpers
/*
* @see DialogField#doFillIntoGrid
*/
@Override
public Control[] doFillIntoGrid(Composite parent, int nColumns) {
assertEnoughColumns(nColumns);
Label label = getLabelControl(parent);
label.setLayoutData(gridDataForLabel(1));
Text text = getTextControl(parent);
text.setLayoutData(gridDataForText(nColumns - 1));
return new Control[] { label, text };
}
/*
* @see DialogField#getNumberOfControls
*/
@Override
public int getNumberOfControls() {
return 2;
}
protected static GridData gridDataForText(int span) {
GridData gd = new GridData();
gd.horizontalAlignment = GridData.FILL;
gd.grabExcessHorizontalSpace = true;
gd.horizontalSpan = span;
return gd;
}
// ------- focus methods
/*
* @see DialogField#setFocus
*/
@Override
public boolean setFocus() {
if (isOkToUse(fTextControl)) {
fTextControl.setFocus();
fTextControl.setSelection(0, fTextControl.getText().length());
}
return true;
}
// ------- ui creation
/**
* Creates or returns the created text control.
*
* @param parent
* The parent composite or <code>null</code> when the widget has
* already been created.
*/
public Text getTextControl(Composite parent) {
if (fTextControl == null) {
assertCompositeNotNull(parent);
fModifyListener = e -> doModifyText(e);
fTextControl = new Text(parent, SWT.SINGLE | SWT.BORDER);
// moved up due to 1GEUNW2
fTextControl.setText(fText);
fTextControl.setFont(parent.getFont());
fTextControl.addModifyListener(fModifyListener);
fTextControl.setEnabled(isEnabled());
if (fContentAssistProcessor != null) {
ContentAssistHandler.createHandlerForText(fTextControl,
createPHPContentAssistant(fContentAssistProcessor));
}
}
return fTextControl;
}
protected void doModifyText(ModifyEvent e) {
if (isOkToUse(fTextControl)) {
fText = fTextControl.getText();
}
dialogFieldChanged();
}
// ------ enable / disable management
/*
* @see DialogField#updateEnableState
*/
@Override
protected void updateEnableState() {
super.updateEnableState();
if (isOkToUse(fTextControl)) {
fTextControl.setEnabled(isEnabled());
}
}
// ------ text access
/**
* Gets the text. Can not be <code>null</code>
*/
public String getText() {
return fText;
}
/**
* Sets the text. Triggers a dialog-changed event.
*/
public void setText(String text) {
fText = text;
if (isOkToUse(fTextControl)) {
fTextControl.setText(text);
} else {
dialogFieldChanged();
}
}
/**
* Sets the text without triggering a dialog-changed event.
*/
public void setTextWithoutUpdate(String text) {
fText = text;
if (isOkToUse(fTextControl)) {
fTextControl.removeModifyListener(fModifyListener);
fTextControl.setText(text);
fTextControl.addModifyListener(fModifyListener);
}
}
/*
* (non-Javadoc)
*
* @see
* org.eclipse.jdt.internal.ui.wizards.dialogfields.DialogField#refresh()
*/
@Override
public void refresh() {
super.refresh();
if (isOkToUse(fTextControl)) {
setTextWithoutUpdate(fText);
}
}
}