1 package org
.argeo
.app
.ui
.docbook
;
3 import java
.util
.ArrayList
;
8 import org
.argeo
.api
.cms
.ux
.CmsEditable
;
9 import org
.argeo
.app
.docbook
.DbkMsg
;
10 import org
.argeo
.app
.docbook
.DbkUtils
;
11 import org
.argeo
.cms
.swt
.CmsSwtUtils
;
12 import org
.argeo
.cms
.swt
.SwtEditablePart
;
13 import org
.argeo
.cms
.swt
.MouseDown
;
14 import org
.argeo
.cms
.ui
.viewers
.NodePart
;
15 import org
.argeo
.cms
.ui
.viewers
.Section
;
16 import org
.argeo
.cms
.ui
.viewers
.SectionPart
;
17 import org
.argeo
.cms
.ui
.widgets
.EditableText
;
18 import org
.argeo
.cms
.ui
.widgets
.Img
;
19 import org
.argeo
.jcr
.Jcr
;
20 import org
.eclipse
.rap
.rwt
.RWT
;
21 import org
.eclipse
.swt
.SWT
;
22 import org
.eclipse
.swt
.events
.MouseAdapter
;
23 import org
.eclipse
.swt
.events
.MouseEvent
;
24 import org
.eclipse
.swt
.events
.ShellEvent
;
25 import org
.eclipse
.swt
.graphics
.Point
;
26 import org
.eclipse
.swt
.layout
.GridLayout
;
27 import org
.eclipse
.swt
.widgets
.Composite
;
28 import org
.eclipse
.swt
.widgets
.Control
;
29 import org
.eclipse
.swt
.widgets
.Label
;
30 import org
.eclipse
.swt
.widgets
.Shell
;
32 /** Dialog to edit a text part. */
33 class DbkContextMenu
{
34 private final AbstractDbkViewer textViewer
;
38 DbkContextMenu(AbstractDbkViewer textViewer
, Shell parentShell
) {
39 // shell = new Shell(display, SWT.NO_TRIM | SWT.BORDER | SWT.ON_TOP);
40 shell
= new Shell(parentShell
, SWT
.BORDER
);
41 // super(display, SWT.NO_TRIM | SWT.BORDER | SWT.ON_TOP);
42 this.textViewer
= textViewer
;
43 shell
.setLayout(new GridLayout());
44 // shell.setData(RWT.CUSTOM_VARIANT, TEXT_STYLED_TOOLS_DIALOG);
46 shell
.addShellListener(new ToolsShellListener());
49 void show(SwtEditablePart editablePart
, Point location
, List
<String
> availableStyles
) {
50 if (shell
.isVisible())
51 shell
.setVisible(false);
52 CmsSwtUtils
.clear(shell
);
53 Composite parent
= shell
;
54 CmsEditable cmsEditable
= textViewer
.getCmsEditable();
55 // if (availableStyles.isEmpty())
58 if (editablePart
instanceof Paragraph
) {
59 Paragraph paragraph
= (Paragraph
) editablePart
;
60 deletePartB(parent
, DbkMsg
.deleteParagraph
.lead(), paragraph
);
61 insertMediaB(parent
, paragraph
);
63 } else if (editablePart
instanceof Img
) {
64 Img img
= (Img
) editablePart
;
65 deletePartB(parent
, DbkMsg
.deleteMedia
.lead(), img
);
66 insertMediaB(parent
, img
);
67 insertParagraphB(parent
, DbkMsg
.insertParagraph
.lead(), img
);
69 } else if (editablePart
instanceof DbkSectionTitle
) {
70 DbkSectionTitle sectionTitle
= (DbkSectionTitle
) editablePart
;
71 TextSection section
= sectionTitle
.getSection();
72 if (!section
.isTitleReadOnly()) {
73 Label deleteB
= new Label(shell
, SWT
.NONE
);
74 deleteB
.setText(DbkMsg
.deleteSection
.lead());
75 deleteB
.addMouseListener((MouseDown
) (e
) -> {
76 textViewer
.deleteSection(section
);
80 insertMediaB(parent
, sectionTitle
.getSection(), sectionTitle
);
83 StyledToolMouseListener stml
= new StyledToolMouseListener(editablePart
);
84 List
<StyleButton
> styleButtons
= new ArrayList
<DbkContextMenu
.StyleButton
>();
85 if (cmsEditable
.isEditing()) {
86 for (String style
: availableStyles
) {
87 StyleButton styleButton
= new StyleButton(shell
, SWT
.WRAP
);
88 if (!"".equals(style
))
89 styleButton
.setStyle(style
);
91 styleButton
.setStyle(null);
92 styleButton
.setMouseListener(stml
);
93 styleButtons
.add(styleButton
);
95 } else if (cmsEditable
.canEdit()) {
97 // Label editButton = new Label(shell, SWT.NONE);
98 // editButton.setText("Edit");
99 // editButton.addMouseListener(stml);
102 if (editablePart
instanceof Paragraph
) {
104 String text
= textViewer
.getRawParagraphText((Paragraph
) editablePart
);
105 String textToShow
= text
.length() > size ? text
.substring(0, size
- 3) + "..." : text
;
106 for (StyleButton styleButton
: styleButtons
) {
107 styleButton
.setText((styleButton
.style
== null ?
"default" : styleButton
.style
) + " : " + textToShow
);
113 if (editablePart
instanceof Control
) {
114 int height
= shell
.getSize().y
;
115 int parentShellHeight
= shell
.getShell().getSize().y
;
116 if ((location
.y
+ height
) < parentShellHeight
) {
117 shell
.setLocation(((Control
) editablePart
).toDisplay(location
.x
, location
.y
));
119 shell
.setLocation(((Control
) editablePart
).toDisplay(location
.x
, location
.y
- parentShellHeight
));
123 if (shell
.getChildren().length
!= 0)
128 shell
.setVisible(false);
131 protected void insertMediaB(Composite parent
, SectionPart sectionPart
) {
132 insertMediaB(parent
, sectionPart
.getSection(), sectionPart
);
135 protected void insertMediaB(Composite parent
, Section section
, NodePart nodePart
) {
136 Label insertPictureB
= new Label(parent
, SWT
.NONE
);
137 insertPictureB
.setText(DbkMsg
.insertPicture
.lead());
138 insertPictureB
.addMouseListener((MouseDown
) (e
) -> {
139 Node newNode
= DbkUtils
.insertImageAfter(nodePart
.getNode());
141 textViewer
.insertPart(section
, newNode
);
144 Label insertVideoB
= new Label(parent
, SWT
.NONE
);
145 insertVideoB
.setText(DbkMsg
.insertVideo
.lead());
146 insertVideoB
.addMouseListener((MouseDown
) (e
) -> {
147 Node newNode
= DbkUtils
.insertVideoAfter(nodePart
.getNode());
149 textViewer
.insertPart(section
, newNode
);
155 protected void insertParagraphB(Composite parent
, String msg
, SectionPart sectionPart
) {
156 Label insertMediaB
= new Label(parent
, SWT
.NONE
);
157 insertMediaB
.setText(msg
);
158 insertMediaB
.addMouseListener((MouseDown
) (e
) -> {
159 textViewer
.addParagraph(sectionPart
, null);
164 protected void deletePartB(Composite parent
, String msg
, SectionPart sectionPart
) {
165 Label deleteB
= new Label(shell
, SWT
.NONE
);
166 deleteB
.setText(msg
);
167 deleteB
.addMouseListener((MouseDown
) (e
) -> {
168 textViewer
.deletePart(sectionPart
);
173 class StyleButton
extends EditableText
{
174 private static final long serialVersionUID
= 7731102609123946115L;
178 public StyleButton(Composite parent
, int style
) {
179 super(parent
, style
);
183 public void setStyle(String style
) {
185 super.setStyle(style
);
188 // private Label label;
190 // public StyleButton(Composite parent, int swtStyle) {
191 // super(parent, SWT.NONE);
192 // setLayout(new GridLayout());
193 // label = new Label(this, swtStyle);
196 // public Label getLabel() {
202 class StyledToolMouseListener
extends MouseAdapter
{
203 private static final long serialVersionUID
= 8516297091549329043L;
204 private SwtEditablePart editablePart
;
206 public StyledToolMouseListener(SwtEditablePart editablePart
) {
208 this.editablePart
= editablePart
;
212 public void mouseDown(MouseEvent e
) {
213 // TODO make it more robust.
214 Label sb
= (Label
) e
.getSource();
215 Object style
= sb
.getData(RWT
.CUSTOM_VARIANT
);
216 textViewer
.setParagraphStyle((Paragraph
) editablePart
, style
== null ?
null : style
.toString());
221 class ToolsShellListener
extends org
.eclipse
.swt
.events
.ShellAdapter
{
222 private static final long serialVersionUID
= 8432350564023247241L;
225 public void shellDeactivated(ShellEvent e
) {