--- /dev/null
+/**
+ *
+ */
+package eu.etaxonomy.taxeditor.ui.element;
+
+import java.awt.Point;
+
+import org.eclipse.jface.dialogs.MessageDialog;
+import org.eclipse.jface.layout.GridDataFactory;
+import org.eclipse.jface.viewers.CellEditor.LayoutData;
+import org.eclipse.nebula.widgets.richtext.RichTextEditor;
+import org.eclipse.nebula.widgets.richtext.RichTextEditorConfiguration;
+import org.eclipse.nebula.widgets.richtext.toolbar.ToolbarButton;
+import org.eclipse.nebula.widgets.richtext.toolbar.ToolbarConfiguration;
+import org.eclipse.swt.SWT;
+import org.eclipse.swt.events.KeyAdapter;
+import org.eclipse.swt.events.KeyEvent;
+import org.eclipse.swt.events.ModifyEvent;
+import org.eclipse.swt.events.ModifyListener;
+import org.eclipse.swt.events.SelectionAdapter;
+import org.eclipse.swt.events.SelectionEvent;
+import org.eclipse.swt.graphics.Color;
+import org.eclipse.swt.layout.GridLayout;
+import org.eclipse.swt.layout.RowLayout;
+import org.eclipse.swt.widgets.Button;
+import org.eclipse.swt.widgets.Composite;
+import org.eclipse.swt.widgets.Control;
+import org.eclipse.swt.widgets.Label;
+import org.eclipse.swt.widgets.Listener;
+import org.eclipse.swt.widgets.Text;
+import org.eclipse.ui.forms.widgets.TableWrapData;
+import org.eclipse.ui.forms.widgets.TableWrapLayout;
+
+import eu.etaxonomy.cdm.common.CdmUtils;
+import eu.etaxonomy.taxeditor.preference.Resources;
+
+/**
+ * @author n.hoffmann
+ * @version $Id: $
+ */
+public class RichTextWithLabelElement extends AbstractCdmFormElement implements ModifyListener, IEnableableFormElement,
+ ISelectable {
+
+
+ protected RichTextEditor richtextEditor;
+ private Label label;
+ private boolean buttonVisible = true;
+
+ private final boolean isMultiLine;
+
+ public static final int MAX_HEIGHT = 0;
+ public static final int SINGLE = -1;
+
+ protected RichTextWithLabelElement(CdmFormFactory formFactory, ICdmFormElement parentElement, boolean isMultiLine) {
+ super(formFactory, parentElement);
+ this.isMultiLine = isMultiLine;
+ }
+
+ protected RichTextWithLabelElement(CdmFormFactory formFactory, ICdmFormElement parentElement, String labelString,
+ String initialText, Integer textHeight, int style) {
+ this(formFactory, parentElement, labelString, initialText, textHeight, null, true, style);
+ }
+
+ protected RichTextWithLabelElement(CdmFormFactory formFactory, ICdmFormElement parentElement, String labelString,
+ String initialText, Integer textHeight, boolean isMultiLine, int style) {
+ this(formFactory, parentElement, labelString, initialText, textHeight, null, isMultiLine, style);
+ }
+
+ protected RichTextWithLabelElement(CdmFormFactory formFactory, ICdmFormElement parentElement, String labelString,
+ String initialText, Integer textHeight, Integer textLimit, int style) {
+ this(formFactory, parentElement, labelString, initialText, textHeight, textLimit, false, style);
+ }
+
+ protected RichTextWithLabelElement(CdmFormFactory formFactory, ICdmFormElement parentElement, String labelString,
+ String initialText, Integer textHeight, Integer textLimit, boolean isMultiLine, int style) {
+ super(formFactory, parentElement);
+
+ this.isMultiLine = isMultiLine;
+
+ // initLabel(formFactory, labelString, isMultiLine, getLayoutComposite());
+
+ initText( initialText, textHeight, textLimit, isMultiLine, style, getLayoutComposite());
+ }
+
+ protected void initText(String initialText, Integer textHeight, Integer textLimit,
+ boolean isMultiLine, int style, Composite layoutComposite) {
+ layoutComposite.setLayout(new GridLayout(1, true));
+
+
+ ToolbarConfiguration toolBarConfig = new ToolbarConfiguration();
+
+ RichTextEditorConfiguration config = new RichTextEditorConfiguration();
+ config.setMinSize(800, 600);
+
+ final RichTextEditor editor = new RichTextEditor(layoutComposite, config);
+ GridDataFactory.fillDefaults().grab(true, true).applyTo(editor);
+ //editor.setText(initialText);
+ //editor.setEditable(true);
+
+ final Text htmlOutput = new Text(layoutComposite,
+ SWT.MULTI | SWT.BORDER | SWT.WRAP | SWT.V_SCROLL | SWT.READ_ONLY);
+ GridDataFactory.fillDefaults().grab(true, false).hint(SWT.DEFAULT, 100).applyTo(htmlOutput);
+ htmlOutput.setEditable(true);
+
+
+
+ editor.addModifyListener(new ModifyListener() {
+
+ @Override
+ public void modifyText(ModifyEvent e) {
+ org.eclipse.swt.graphics.Point point = htmlOutput.getSelection();
+ htmlOutput.setText(htmlOutput.getText() + " - " + point.x + ":"+point.y);
+ }
+ });
+
+ editor.addKeyListener(new KeyAdapter() {
+
+ @Override
+ public void keyPressed(KeyEvent e) {
+ org.eclipse.swt.graphics.Point point = htmlOutput.getSelection();
+ htmlOutput.setText(htmlOutput.getText() + " - " + point.x + ":"+point.y);
+
+ }
+ });
+
+// editor.addToolbarButton(new ToolbarButton("jsButton",
+// "myScriptCommand", "Execute Javascript", "other,0",
+// null) {
+// @Override
+// public String getJavascriptToExecute() {
+// return "alert('Javascript call')";
+// }
+// });
+
+ // add additional controls for showing interactions
+
+ createControlPanel(layoutComposite, editor, textHeight);
+
+ }
+
+ protected void createControlPanel(Composite parent, final RichTextEditor editor, int textHeight) {
+ Composite controlPanel = new Composite(parent, SWT.NONE);
+ TableWrapLayout layout;
+ layout = LayoutConstants.LAYOUT(3, false);
+ controlPanel.setLayout(layout);
+ GridDataFactory.fillDefaults().grab(true, false).applyTo(controlPanel);
+
+ Label inputLabel = new Label(controlPanel, SWT.NONE);
+ inputLabel.setText("Text to set:");
+ // GridDataFactory.fillDefaults().applyTo(inputLabel);
+
+ Text input = new Text(controlPanel, SWT.BORDER);
+ TableWrapData layoutData;
+ if(isMultiLine){
+ layoutData = LayoutConstants.FILL_HORIZONTALLY(2, 1);
+ }
+ else{
+ layoutData = LayoutConstants.FILL();
+ }
+ input.setLayoutData(layoutData);
+ // GridDataFactory.fillDefaults().grab(true, false).applyTo(input);
+
+ input.addKeyListener(new KeyAdapter() {
+ @Override
+ public void keyPressed(KeyEvent e) {
+ if (e.keyCode == SWT.CR || e.keyCode == SWT.KEYPAD_CR) {
+ editor.setText(input.getText());
+ }
+ }
+ });
+
+ Button setButton = new Button(controlPanel, SWT.PUSH);
+ setButton.setText("Set Text");
+ // GridDataFactory.defaultsFor(setButton).applyTo(setButton);
+
+ setButton.addSelectionListener(new SelectionAdapter() {
+ @Override
+ public void widgetSelected(SelectionEvent e) {
+ editor.setText(input.getText());
+ }
+ });
+
+ Composite buttonPanel = new Composite(controlPanel, SWT.NONE);
+ buttonPanel.setLayout(new RowLayout());
+ // GridDataFactory.fillDefaults().grab(true, false).span(3, 1).applyTo(buttonPanel);
+
+ Button getButton = new Button(buttonPanel, SWT.PUSH);
+ getButton.setText("Get text");
+ getButton.addSelectionListener(new SelectionAdapter() {
+ @Override
+ public void widgetSelected(SelectionEvent e) {
+ MessageDialog.openInformation(null, "Editor Input",
+ editor.getText());
+ }
+ });
+
+ final Button enableButton = new Button(buttonPanel, SWT.PUSH);
+ enableButton.setText("Disable");
+ enableButton.addSelectionListener(new SelectionAdapter() {
+ @Override
+ public void widgetSelected(SelectionEvent e) {
+ boolean editable = editor.isEditable();
+ editor.setEditable(!editable);
+
+ enableButton.setText(editable ? "Enable" : "Disable");
+ }
+ });
+
+ final Button updateButton = new Button(buttonPanel, SWT.PUSH);
+ updateButton.setText("Update Toolbar");
+
+ final ToolbarButton button = new ToolbarButton("javaButton",
+ "myJavaCommand", "Execute Java", "other",
+ null) {
+
+ @Override
+ public Object execute() {
+ MessageDialog.openInformation(null, "Information",
+ "Java callback: " + editor.getSelectedHTML());
+
+ editor.insertHTML("<em>" + editor.getSelectedHTML() + "</em>");
+ return null;
+ }
+ };
+
+ updateButton.addSelectionListener(new SelectionAdapter() {
+ @Override
+ public void widgetSelected(SelectionEvent e) {
+ if (!buttonVisible) {
+ editor.addToolbarButton(button);
+ } else {
+ editor.removeToolbarButton(button);
+ }
+ buttonVisible = !buttonVisible;
+ editor.updateToolbar();
+ }
+ });
+
+ Button setFocusButton = new Button(buttonPanel, SWT.PUSH);
+ setFocusButton.setText("Set Focus");
+ setFocusButton.addSelectionListener(new SelectionAdapter() {
+ @Override
+ public void widgetSelected(SelectionEvent e) {
+ editor.setFocus();
+ }
+ });
+
+ }
+
+ protected void initLabel(CdmFormFactory formFactory, String labelString, boolean isMultiLine, Composite layoutComposite) {
+ if (labelString != null) {
+ label = formFactory.createLabel(layoutComposite, CdmUtils.Nz(labelString), SWT.NULL);
+ addControl(label);
+ if(isMultiLine){
+ label.setLayoutData(LayoutConstants.FILL_HORIZONTALLY(2, 1));
+ ((TableWrapData)label.getLayoutData()).valign = TableWrapData.MIDDLE;
+ }
+ else{
+ label.setLayoutData(LayoutConstants.LEFT());
+ ((TableWrapData)label.getLayoutData()).valign = TableWrapData.MIDDLE;
+ }
+ }
+ }
+
+
+
+
+ /** {@inheritDoc} */
+ @Override
+ public void setBackground(Color color) {
+ if (label != null) {
+ label.setBackground(color);
+ }
+ }
+
+ @Override
+ public void setSelected(boolean selected) {
+ setBackground(selected ? SELECTED : getPersistentBackground());
+ }
+
+ @Override
+ public void setIrrelevant(boolean irrelevant) {
+ // TODO Auto-generated method stub
+
+ }
+
+ @Override
+ public void setEnabled(boolean enabled) {
+ // TODO Auto-generated method stub
+
+ }
+
+ @Override
+ public boolean isEnabled() {
+ // TODO Auto-generated method stub
+ return false;
+ }
+
+ @Override
+ public void modifyText(ModifyEvent e) {
+ // TODO Auto-generated method stub
+
+ }
+
+
+}