package com.iconic.richtexteditor.example;
import java.util.logging.Logger;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import com.iconic.richtexteditor.IStyledTextWidgetImageCreator;
import com.iconic.richtexteditor.RichTextEditorWidget;
import com.iconic.richtexteditor.RichTextEditorWidgetBasicImageCreator;
import com.iconic.richtexteditor.RichTextEditorWidgetFeatureSet;
import com.iconic.richtexteditor.html.HTMLTextEditorWidget;
import com.iconic.richtexteditor.html.HTMLTextModel;
public class HTMLTextEditorExample {
private static final Logger logger = Logger.getLogger(HTMLTextEditorExample.class.getName());
// private static final String iconsDirPath = "../RichTextEditorWidgetIcons/16x16/bmp";
private static final String iconsDirPath = "../RichTextEditorWidgetIcons/16x16/png";
// private static final String iconsDirPath = "../RichTextEditorWidgetIcons/24x24/bmp";
// private static final String iconsDirPath = "../RichTextEditorWidgetIcons/24x24/png";
private static final String HTML_LINE_BREAK = "
";
private static final String testText =
"Normal bold italic underline strikeout" + HTML_LINE_BREAK +
"This is another line." + HTML_LINE_BREAK +
"This is the last line.";
private static final int SHELL_WIDTH = 600;
private static final int SHELL_HEIGHT = 500;
private final Display display;
private final Shell shell;
private final GridLayout shellLayout;
private HTMLTextEditorWidget htmlTextEditorWidget;
private Button extractHTMLButton;
private Text htmlText;
public static void main(String[] args) {
RichTextEditorWidget.setRegistrationKey("your registration key");
HTMLTextEditorExample richTextEditorWidgetTest = new HTMLTextEditorExample();
richTextEditorWidgetTest.start();
}
private void start() {
shell.setVisible(true);
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}
public HTMLTextEditorExample() {
display = new Display();
shell = new Shell(display);
shellLayout = new GridLayout();
shellLayout.numColumns = 1;
shellLayout.verticalSpacing = 1;
shell.setLayout(shellLayout);
shell.setText("Rich text editor");
int shellWidth = SHELL_WIDTH;
int shellHeight = SHELL_HEIGHT;
shell.setSize(shellWidth, shellHeight);
shell.setLocation(20, 20);
createAndAddHTMLTextEditorWidget();
createAndAddExtractHTMLButton();
createAndAddHTMLTextAreaWidget();
}
private void createAndAddHTMLTextEditorWidget() {
final String menuHeaderName = "Edit";
final boolean preventTypingOfHTMLTags = true;
final int compositeStyle = SWT.BORDER;
final int toolBarStyle = SWT.NULL;
final int styledTextStyle = SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL | SWT.WRAP;
// RichTextEditorWidgetFeatureSet featureSet = new RichTextEditorWidgetFeatureSet(
// RichTextEditorWidgetFeatureSet.BOLD_ITALIC_UNDERLINE_FEATURES,
// RichTextEditorWidgetFeatureSet.CUT_COPY_PASTE_FEATURES);
// featureSet = new RichTextEditorWidgetFeatureSet(
// featureSet,
// RichTextEditorWidgetFeatureSet.CLEAR_STYLES_FEATURE);
RichTextEditorWidgetFeatureSet featureSet = RichTextEditorWidgetFeatureSet.ALL_FEATURES;
IStyledTextWidgetImageCreator imageCreator = new RichTextEditorWidgetBasicImageCreator(
iconsDirPath, false);
Composite parent = shell;
htmlTextEditorWidget = new HTMLTextEditorWidget(
featureSet,
imageCreator,
parent,
menuHeaderName,
preventTypingOfHTMLTags,
compositeStyle,
toolBarStyle,
styledTextStyle);
GridData gridData = new GridData();
gridData.grabExcessHorizontalSpace = true;
gridData.grabExcessVerticalSpace = true;
gridData.horizontalAlignment = SWT.BEGINNING;
gridData.verticalAlignment = SWT.BEGINNING;
gridData.minimumWidth = 200;
gridData.minimumHeight = 50;
htmlTextEditorWidget.setLayoutData(gridData);
HTMLTextModel htmlTextModel = new HTMLTextModel(testText);
htmlTextEditorWidget.setFormattedText(htmlTextModel);
shell.layout();
}
private void createAndAddExtractHTMLButton() {
extractHTMLButton = new Button(shell, SWT.BORDER);
extractHTMLButton.setText("Extract as HTML");
SelectionAdapter extractButtonAdapter = new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
if (!htmlTextEditorWidget.isDisposed()) {
handleExtractHTMLButton();
}
}
};
extractHTMLButton.addSelectionListener(extractButtonAdapter);
}
private void createAndAddHTMLTextAreaWidget() {
htmlText = new Text(shell, SWT.BORDER | SWT.MULTI | SWT.READ_ONLY | SWT.V_SCROLL | SWT.H_SCROLL);
GridData gridData = new GridData();
gridData.grabExcessHorizontalSpace = false;
gridData.grabExcessVerticalSpace = false;
gridData.horizontalAlignment = SWT.BEGINNING;
gridData.verticalAlignment = SWT.BEGINNING;
gridData.widthHint = SHELL_WIDTH - 100;
gridData.heightHint = 100;
htmlText.setLayoutData(gridData);
}
private void handleExtractHTMLButton() {
if (logger.isLoggable(java.util.logging.Level.INFO)) {
logger.info("### Handling extract button pressed...");
}
HTMLTextModel htmlTextModel = htmlTextEditorWidget.getFormattedTextModel();
String formattedText = htmlTextModel.getHtml();
if (logger.isLoggable(java.util.logging.Level.INFO)) {
logger.info("### Formatted text is as follows:\n" + formattedText);
}
String html = htmlTextModel.getHtml();
htmlText.setText(html);
}
}