1 package org
.argeo
.cms
.e4
.rap
.parts
;
3 import java
.io
.IOException
;
4 import java
.nio
.charset
.StandardCharsets
;
6 import javax
.annotation
.PostConstruct
;
8 import org
.apache
.commons
.io
.IOUtils
;
9 import org
.argeo
.cms
.CmsException
;
10 import org
.argeo
.cms
.util
.CmsUtils
;
11 import org
.eclipse
.nebula
.widgets
.richtext
.RichTextEditor
;
12 import org
.eclipse
.nebula
.widgets
.richtext
.RichTextEditorConfiguration
;
13 import org
.eclipse
.swt
.SWT
;
14 import org
.eclipse
.swt
.browser
.Browser
;
15 import org
.eclipse
.swt
.events
.SelectionAdapter
;
16 import org
.eclipse
.swt
.events
.SelectionEvent
;
17 import org
.eclipse
.swt
.graphics
.Color
;
18 import org
.eclipse
.swt
.layout
.GridData
;
19 import org
.eclipse
.swt
.layout
.GridLayout
;
20 import org
.eclipse
.swt
.widgets
.Button
;
21 import org
.eclipse
.swt
.widgets
.Composite
;
22 import org
.eclipse
.swt
.widgets
.Display
;
23 import org
.eclipse
.swt
.widgets
.Shell
;
24 import org
.eclipse
.swt
.widgets
.Text
;
26 public class HtmlEditor
{
29 public void createUI(Composite parent
) {
33 toolbarGroups
= IOUtils
.toString(HtmlEditor
.class.getResourceAsStream("CkEditor-toolbarGroups.json"),
34 StandardCharsets
.UTF_8
);
35 removeButtons
= IOUtils
.toString(HtmlEditor
.class.getResourceAsStream("CkEditor-removeButtons.js"),
36 StandardCharsets
.UTF_8
);
37 } catch (IOException e
) {
38 throw new CmsException("Cannot configure toolbar", e
);
40 // System.out.println(toolbarGroups);
41 // System.out.println(removeButtons);
42 RichTextEditorConfiguration richTextEditorConfig
= new RichTextEditorConfiguration();
43 richTextEditorConfig
.setOption(RichTextEditorConfiguration
.TOOLBAR_GROUPS
, toolbarGroups
);
44 richTextEditorConfig
.setOption(RichTextEditorConfiguration
.REMOVE_BUTTONS
, removeButtons
);
45 // richTextEditorConfig.setRemoveStyles(false);
46 // richTextEditorConfig.setRemovePasteFromWord(true);
47 // richTextEditorConfig.setRemovePasteText(false);
49 // richTextEditorConfig.setToolbarCollapsible(true);
50 // richTextEditorConfig.setToolbarInitialExpanded(false);
52 final Display display
= parent
.getDisplay();
53 Composite composite
= new Composite(parent
, SWT
.NONE
);
54 // composite.setLayoutData(new Fill);
55 composite
.setLayout(new GridLayout());
56 final RichTextEditor richTextEditor
= new RichTextEditor(composite
, richTextEditorConfig
, SWT
.BORDER
);
57 richTextEditor
.setText("<a href='http://googl.com'>Google</a>");
58 GridData layoutData
= new GridData(SWT
.FILL
, SWT
.FILL
, true, true);
59 richTextEditor
.setLayoutData(layoutData
);
60 richTextEditor
.setBackground(new Color(display
, 247, 247, 247));
61 Composite toolbar
= new Composite(composite
, SWT
.NONE
);
62 toolbar
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, false));
63 toolbar
.setLayout(new GridLayout(3, false));
64 Button showContent
= new Button(toolbar
, SWT
.PUSH
);
65 showContent
.setText("Show Content");
66 showContent
.addSelectionListener(new SelectionAdapter() {
68 public void widgetSelected(SelectionEvent e
) {
69 showContent(parent
, richTextEditor
, false);
72 Button showSource
= new Button(toolbar
, SWT
.PUSH
);
73 showSource
.setText("Show Source");
74 showSource
.addSelectionListener(new SelectionAdapter() {
76 public void widgetSelected(SelectionEvent e
) {
77 showContent(parent
, richTextEditor
, true);
80 Button clearBtn
= new Button(toolbar
, SWT
.NONE
);
81 clearBtn
.setText("Clear");
82 clearBtn
.addSelectionListener(new SelectionAdapter() {
84 public void widgetSelected(SelectionEvent e
) {
85 richTextEditor
.setText("");
91 private static void showContent(Composite parent
, RichTextEditor editor
, boolean source
) {
92 int style
= SWT
.DIALOG_TRIM
| SWT
.APPLICATION_MODAL
;
93 final Shell content
= new Shell(parent
.getShell(), style
);
94 content
.setLayout(new GridLayout(1, true));
95 String text
= editor
.getText();
97 content
.setText("Rich Text Source");
98 Text viewer
= new Text(content
, SWT
.MULTI
| SWT
.WRAP
);
99 viewer
.setLayoutData(new GridData(400, 400));
100 viewer
.setText(text
);
101 viewer
.setEditable(false);
103 content
.setText("Rich Text");
104 Browser viewer
= new Browser(content
, SWT
.NONE
);
105 viewer
.setLayoutData(new GridData(400, 400));
106 viewer
.setText(text
);
107 viewer
.setEnabled(false);
109 Button ok
= new Button(content
, SWT
.PUSH
);
110 ok
.setLayoutData(new GridData(SWT
.RIGHT
, SWT
.BOTTOM
, false, false));
112 ok
.addSelectionListener(new SelectionAdapter() {
114 public void widgetSelected(SelectionEvent e
) {
118 content
.setDefaultButton(ok
);
120 Display display
= parent
.getDisplay();
121 int left
= (display
.getClientArea().width
/ 2) - (content
.getBounds().width
/ 2);
122 content
.setLocation(left
, 40);