]> git.argeo.org Git - gpl/argeo-suite.git/blob - publishing/org.argeo.publishing.ui/src/org/argeo/docbook/ui/DbkContextMenu.java
[maven-release-plugin] prepare release argeo-suite-2.1.21
[gpl/argeo-suite.git] / publishing / org.argeo.publishing.ui / src / org / argeo / docbook / ui / DbkContextMenu.java
1 package org.argeo.docbook.ui;
2
3 import java.util.ArrayList;
4 import java.util.List;
5
6 import javax.jcr.Node;
7
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;
33
34 /** Dialog to edit a text part. */
35 class DbkContextMenu {
36 private final AbstractDbkViewer textViewer;
37
38 private Shell shell;
39
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);
47
48 shell.addShellListener(new ToolsShellListener());
49 }
50
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())
58 // return;
59
60 if (editablePart instanceof Paragraph) {
61 Paragraph paragraph = (Paragraph) editablePart;
62 deletePartB(parent, DbkMsg.deleteParagraph.lead(), paragraph);
63 insertMediaB(parent, DbkMsg.insertMedia.lead(), paragraph);
64
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);
70
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);
79 hide();
80 });
81 }
82 insertMediaB(parent, DbkMsg.insertMedia.lead(), sectionTitle.getSection(), sectionTitle);
83 }
84
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);
92 else
93 styleButton.setStyle(null);
94 styleButton.setMouseListener(stml);
95 styleButtons.add(styleButton);
96 }
97 } else if (cmsEditable.canEdit()) {
98 // Edit
99 // Label editButton = new Label(shell, SWT.NONE);
100 // editButton.setText("Edit");
101 // editButton.addMouseListener(stml);
102 }
103
104 if (editablePart instanceof Paragraph) {
105 final int size = 32;
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);
110 }
111 }
112
113 shell.pack();
114 shell.layout();
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));
120 } else {
121 shell.setLocation(((Control) editablePart).toDisplay(location.x, location.y - parentShellHeight));
122 }
123 }
124
125 if (shell.getChildren().length != 0)
126 shell.open();
127 }
128
129 void hide() {
130 shell.setVisible(false);
131 }
132
133 protected void insertMediaB(Composite parent, String msg, SectionPart sectionPart) {
134 insertMediaB(parent, msg, sectionPart.getSection(), sectionPart);
135 }
136
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());
142 Jcr.save(newNode);
143 textViewer.insertPart(section, newNode);
144 hide();
145 });
146
147 }
148
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);
154 hide();
155 });
156 }
157
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);
163 hide();
164 });
165 }
166
167 class StyleButton extends EditableText {
168 private static final long serialVersionUID = 7731102609123946115L;
169
170 String style;
171
172 public StyleButton(Composite parent, int style) {
173 super(parent, style);
174 }
175
176 @Override
177 public void setStyle(String style) {
178 this.style = style;
179 super.setStyle(style);
180 }
181
182 // private Label label;
183 //
184 // public StyleButton(Composite parent, int swtStyle) {
185 // super(parent, SWT.NONE);
186 // setLayout(new GridLayout());
187 // label = new Label(this, swtStyle);
188 // }
189 //
190 // public Label getLabel() {
191 // return label;
192 // }
193
194 }
195
196 class StyledToolMouseListener extends MouseAdapter {
197 private static final long serialVersionUID = 8516297091549329043L;
198 private EditablePart editablePart;
199
200 public StyledToolMouseListener(EditablePart editablePart) {
201 super();
202 this.editablePart = editablePart;
203 }
204
205 @Override
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());
211 hide();
212 }
213 }
214
215 class ToolsShellListener extends org.eclipse.swt.events.ShellAdapter {
216 private static final long serialVersionUID = 8432350564023247241L;
217
218 @Override
219 public void shellDeactivated(ShellEvent e) {
220 hide();
221 }
222
223 }
224 }