package rocks.inspectit.ui.rcp.editor.text.input;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.eclipse.core.runtime.Assert;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.ui.forms.widgets.ExpandableComposite;
import org.eclipse.ui.forms.widgets.FormText;
import org.eclipse.ui.forms.widgets.FormToolkit;
import org.eclipse.ui.forms.widgets.Section;
import rocks.inspectit.ui.rcp.editor.inputdefinition.InputDefinition;
/**
* General implementation of {@link TextInputController}, where most of the methods are not doing
* anything. Classes that extend should override the methods they want to define the proper
* behavior.
*
* @author Ivan Senic
*
*/
public abstract class AbstractTextInputController implements TextInputController {
/**
* The input definition.
*/
private InputDefinition inputDefinition;
/**
* The {@link HashMap} containing the different sections.
*/
protected Map<String, Composite> sections = new HashMap<>();
/**
* {@inheritDoc}
*/
@Override
public void setInputDefinition(InputDefinition inputDefinition) {
Assert.isNotNull(inputDefinition);
this.inputDefinition = inputDefinition;
}
/**
* Returns the input definition.
*
* @return The input definition.
*/
protected InputDefinition getInputDefinition() {
Assert.isNotNull(inputDefinition);
return inputDefinition;
}
/**
* Adds a section to bundle some content.
*
* @param parent
* The parent used to draw the elements to.
* @param toolkit
* The form toolkit.
* @param sectionTitle
* The section title
*/
protected void addSection(Composite parent, FormToolkit toolkit, String sectionTitle) {
Section section = toolkit.createSection(parent, ExpandableComposite.TITLE_BAR);
section.setText(sectionTitle);
section.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
Composite sectionComposite = toolkit.createComposite(section);
GridLayout gridLayout = new GridLayout(4, false);
gridLayout.marginLeft = 5;
gridLayout.marginTop = 5;
sectionComposite.setLayout(gridLayout);
section.setClient(sectionComposite);
if (!sections.containsKey(sectionTitle)) {
sections.put(sectionTitle, sectionComposite);
}
}
/**
* Adds an item to the specified section.
*
* @param toolkit
* The form toolkit.
* @param sectionTitle
* The section title.
* @param text
* The text which will be shown.
*/
protected void addItemToSection(FormToolkit toolkit, String sectionTitle, String text) {
addItemToSection(toolkit, sectionTitle, text, null, SWT.DEFAULT);
}
/**
* Adds an item to the specified section.
*
* @param toolkit
* The form toolkit.
* @param sectionTitle
* The section title.
* @param text
* The text which will be shown.
* @param image
* The image which will be shown.
* @param minColumnWidth
* the minimum width of the column.
*/
protected void addItemToSection(FormToolkit toolkit, String sectionTitle, String text, Image image, int minColumnWidth) {
if (sections.containsKey(sectionTitle)) {
FormText formText = toolkit.createFormText(sections.get(sectionTitle), false);
if (image != null) {
formText.setText("<form><p><img href=\"img\"/> " + text + "</p></form>", true, false);
formText.setImage("img", image);
} else {
formText.setText("<form><p>" + text + "</p></form>", true, false);
}
formText.setLayoutData(new GridData(minColumnWidth, SWT.DEFAULT));
}
}
/**
* Adds an item to the specified section.
*
* @param toolkit
* The form toolkit.
* @param sectionTitle
* The section title.
* @param text
* The text which will be shown.
* @param minColumnWidth
* the minimum width of the column.
*/
protected void addItemToSection(FormToolkit toolkit, String sectionTitle, String text, int minColumnWidth) {
if (sections.containsKey(sectionTitle)) {
Label label = toolkit.createLabel(sections.get(sectionTitle), text, SWT.LEFT);
label.setLayoutData(new GridData(minColumnWidth, SWT.DEFAULT));
}
}
/**
* {@inheritDoc}
*/
@Override
public void createPartControl(Composite parent, FormToolkit toolkit) {
}
/**
* {@inheritDoc}
*/
@Override
public void doRefresh() {
}
/**
* {@inheritDoc}
*/
@Override
public void dispose() {
}
/**
* {@inheritDoc}
*/
@Override
public void setDataInput(List<? extends Object> data) {
}
}