1 package org.argeo.docbook.ui;
3 import java.util.ArrayList;
8 import org.argeo.cms.text.Paragraph;
9 import org.argeo.cms.text.TextSection;
10 import org.argeo.cms.ui.CmsEditable;
11 import org.argeo.cms.ui.util.CmsUiUtils;
12 import org.argeo.cms.ui.viewers.EditablePart;
13 import org.argeo.cms.ui.viewers.NodePart;
14 import org.argeo.cms.ui.viewers.Section;
15 import org.argeo.cms.ui.viewers.SectionPart;
16 import org.argeo.cms.ui.widgets.EditableText;
17 import org.argeo.cms.ui.widgets.Img;
18 import org.argeo.docbook.DbkMsg;
19 import org.argeo.docbook.DbkUtils;
20 import org.argeo.eclipse.ui.MouseDown;
21 import org.argeo.jcr.Jcr;
22 import org.eclipse.rap.rwt.RWT;
23 import org.eclipse.swt.SWT;
24 import org.eclipse.swt.events.MouseAdapter;
25 import org.eclipse.swt.events.MouseEvent;
26 import org.eclipse.swt.events.ShellEvent;
27 import org.eclipse.swt.graphics.Point;
28 import org.eclipse.swt.layout.GridLayout;
29 import org.eclipse.swt.widgets.Composite;
30 import org.eclipse.swt.widgets.Control;
31 import org.eclipse.swt.widgets.Label;
32 import org.eclipse.swt.widgets.Shell;
34 /** Dialog to edit a text part. */
35 class DbkContextMenu {
36 private final AbstractDbkViewer textViewer;
40 DbkContextMenu(AbstractDbkViewer textViewer, Shell parentShell) {
41 // shell = new Shell(display, SWT.NO_TRIM | SWT.BORDER | SWT.ON_TOP);
42 shell = new Shell(parentShell, SWT.BORDER);
43 // super(display, SWT.NO_TRIM | SWT.BORDER | SWT.ON_TOP);
44 this.textViewer = textViewer;
45 shell.setLayout(new GridLayout());
46 // shell.setData(RWT.CUSTOM_VARIANT, TEXT_STYLED_TOOLS_DIALOG);
48 shell.addShellListener(new ToolsShellListener());
51 void show(EditablePart editablePart, Point location, List<String> availableStyles) {
52 if (shell.isVisible())
53 shell.setVisible(false);
54 CmsUiUtils.clear(shell);
55 Composite parent = shell;
56 CmsEditable cmsEditable = textViewer.getCmsEditable();
57 // if (availableStyles.isEmpty())
60 if (editablePart instanceof Paragraph) {
61 Paragraph paragraph = (Paragraph) editablePart;
62 deletePartB(parent, DbkMsg.deleteParagraph.lead(), paragraph);
63 insertMediaB(parent, DbkMsg.insertMedia.lead(), paragraph);
65 } else if (editablePart instanceof Img) {
66 Img img = (Img) editablePart;
67 deletePartB(parent, DbkMsg.deleteMedia.lead(), img);
68 insertMediaB(parent, DbkMsg.insertMedia.lead(), img);
69 insertParagraphB(parent, DbkMsg.insertParagraph.lead(), img);
71 } else if (editablePart instanceof DocBookSectionTitle) {
72 DocBookSectionTitle sectionTitle = (DocBookSectionTitle) editablePart;
73 TextSection section = sectionTitle.getSection();
74 if (!section.isTitleReadOnly()) {
75 Label deleteB = new Label(shell, SWT.NONE);
76 deleteB.setText(DbkMsg.deleteSection.lead());
77 deleteB.addMouseListener((MouseDown) (e) -> {
78 textViewer.deleteSection(section);
82 insertMediaB(parent, DbkMsg.insertMedia.lead(), sectionTitle.getSection(), sectionTitle);
85 StyledToolMouseListener stml = new StyledToolMouseListener(editablePart);
86 List<StyleButton> styleButtons = new ArrayList<DbkContextMenu.StyleButton>();
87 if (cmsEditable.isEditing()) {
88 for (String style : availableStyles) {
89 StyleButton styleButton = new StyleButton(shell, SWT.WRAP);
90 if (!"".equals(style))
91 styleButton.setStyle(style);
93 styleButton.setStyle(null);
94 styleButton.setMouseListener(stml);
95 styleButtons.add(styleButton);
97 } else if (cmsEditable.canEdit()) {
99 // Label editButton = new Label(shell, SWT.NONE);
100 // editButton.setText("Edit");
101 // editButton.addMouseListener(stml);
104 if (editablePart instanceof Paragraph) {
106 String text = textViewer.getRawParagraphText((Paragraph) editablePart);
107 String textToShow = text.length() > size ? text.substring(0, size - 3) + "..." : text;
108 for (StyleButton styleButton : styleButtons) {
109 styleButton.setText((styleButton.style == null ? "default" : styleButton.style) + " : " + textToShow);
115 if (editablePart instanceof Control) {
116 int height = shell.getSize().y;
117 int parentShellHeight = shell.getShell().getSize().y;
118 if ((location.y + height) < parentShellHeight) {
119 shell.setLocation(((Control) editablePart).toDisplay(location.x, location.y));
121 shell.setLocation(((Control) editablePart).toDisplay(location.x, location.y - parentShellHeight));
125 if (shell.getChildren().length != 0)
130 shell.setVisible(false);
133 protected void insertMediaB(Composite parent, String msg, SectionPart sectionPart) {
134 insertMediaB(parent, msg, sectionPart.getSection(), sectionPart);
137 protected void insertMediaB(Composite parent, String msg, Section section, NodePart nodePart) {
138 Label insertMediaB = new Label(parent, SWT.NONE);
139 insertMediaB.setText(DbkMsg.insertMedia.lead());
140 insertMediaB.addMouseListener((MouseDown) (e) -> {
141 Node newNode = DbkUtils.insertImageAfter(nodePart.getNode());
143 textViewer.insertPart(section, newNode);
149 protected void insertParagraphB(Composite parent, String msg, SectionPart sectionPart) {
150 Label insertMediaB = new Label(parent, SWT.NONE);
151 insertMediaB.setText(msg);
152 insertMediaB.addMouseListener((MouseDown) (e) -> {
153 textViewer.addParagraph(sectionPart, null);
158 protected void deletePartB(Composite parent, String msg, SectionPart sectionPart) {
159 Label deleteB = new Label(shell, SWT.NONE);
160 deleteB.setText(msg);
161 deleteB.addMouseListener((MouseDown) (e) -> {
162 textViewer.deletePart(sectionPart);
167 class StyleButton extends EditableText {
168 private static final long serialVersionUID = 7731102609123946115L;
172 public StyleButton(Composite parent, int style) {
173 super(parent, style);
177 public void setStyle(String style) {
179 super.setStyle(style);
182 // private Label label;
184 // public StyleButton(Composite parent, int swtStyle) {
185 // super(parent, SWT.NONE);
186 // setLayout(new GridLayout());
187 // label = new Label(this, swtStyle);
190 // public Label getLabel() {
196 class StyledToolMouseListener extends MouseAdapter {
197 private static final long serialVersionUID = 8516297091549329043L;
198 private EditablePart editablePart;
200 public StyledToolMouseListener(EditablePart editablePart) {
202 this.editablePart = editablePart;
206 public void mouseDown(MouseEvent e) {
207 // TODO make it more robust.
208 Label sb = (Label) e.getSource();
209 Object style = sb.getData(RWT.CUSTOM_VARIANT);
210 textViewer.setParagraphStyle((Paragraph) editablePart, style == null ? null : style.toString());
215 class ToolsShellListener extends org.eclipse.swt.events.ShellAdapter {
216 private static final long serialVersionUID = 8432350564023247241L;
219 public void shellDeactivated(ShellEvent e) {