]> git.argeo.org Git - gpl/argeo-suite.git/blob - publishing/org.argeo.publishing.ui/src/org/argeo/docbook/ui/DbkContextMenu.java
Do not refresh UI right away.
[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
70 } else if (editablePart instanceof DocBookSectionTitle) {
71 DocBookSectionTitle sectionTitle = (DocBookSectionTitle) editablePart;
72 TextSection section = sectionTitle.getSection();
73 if (!section.isTitleReadOnly()) {
74 Label deleteB = new Label(shell, SWT.NONE);
75 deleteB.setText(DbkMsg.deleteSection.lead());
76 deleteB.addMouseListener((MouseDown) (e) -> {
77 textViewer.deleteSection(section);
78 hide();
79 });
80 }
81 insertMediaB(parent, DbkMsg.insertMedia.lead(), sectionTitle.getSection(), sectionTitle);
82 }
83
84 StyledToolMouseListener stml = new StyledToolMouseListener(editablePart);
85 List<StyleButton> styleButtons = new ArrayList<DbkContextMenu.StyleButton>();
86 if (cmsEditable.isEditing()) {
87 for (String style : availableStyles) {
88 StyleButton styleButton = new StyleButton(shell, SWT.WRAP);
89 if (!"".equals(style))
90 styleButton.setStyle(style);
91 else
92 styleButton.setStyle(null);
93 styleButton.setMouseListener(stml);
94 styleButtons.add(styleButton);
95 }
96 } else if (cmsEditable.canEdit()) {
97 // Edit
98 // Label editButton = new Label(shell, SWT.NONE);
99 // editButton.setText("Edit");
100 // editButton.addMouseListener(stml);
101 }
102
103 if (editablePart instanceof Paragraph) {
104 final int size = 32;
105 String text = textViewer.getRawParagraphText((Paragraph) editablePart);
106 String textToShow = text.length() > size ? text.substring(0, size - 3) + "..." : text;
107 for (StyleButton styleButton : styleButtons) {
108 styleButton.setText((styleButton.style == null ? "default" : styleButton.style) + " : " + textToShow);
109 }
110 }
111
112 shell.pack();
113 shell.layout();
114 if (editablePart instanceof Control) {
115 int height = shell.getSize().y;
116 int parentShellHeight = shell.getShell().getSize().y;
117 if ((location.y + height) < parentShellHeight) {
118 shell.setLocation(((Control) editablePart).toDisplay(location.x, location.y));
119 } else {
120 shell.setLocation(((Control) editablePart).toDisplay(location.x, location.y - parentShellHeight));
121 }
122 }
123
124 if (shell.getChildren().length != 0)
125 shell.open();
126 }
127
128 void hide() {
129 shell.setVisible(false);
130 }
131
132 protected void insertMediaB(Composite parent, String msg, SectionPart sectionPart) {
133 insertMediaB(parent, msg, sectionPart.getSection(), sectionPart);
134 }
135
136 protected void insertMediaB(Composite parent, String msg, Section section, NodePart nodePart) {
137 Label insertMediaB = new Label(parent, SWT.NONE);
138 insertMediaB.setText(DbkMsg.insertMedia.lead());
139 insertMediaB.addMouseListener((MouseDown) (e) -> {
140 Node newNode = DbkUtils.insertImageAfter(nodePart.getNode());
141 Jcr.save(newNode);
142 textViewer.insertPart(section, newNode);
143 hide();
144 });
145
146 }
147
148 protected void deletePartB(Composite parent, String msg, SectionPart sectionPart) {
149 Label deleteB = new Label(shell, SWT.NONE);
150 deleteB.setText(msg);
151 deleteB.addMouseListener((MouseDown) (e) -> {
152 textViewer.deletePart(sectionPart);
153 hide();
154 });
155 }
156
157 class StyleButton extends EditableText {
158 private static final long serialVersionUID = 7731102609123946115L;
159
160 String style;
161
162 public StyleButton(Composite parent, int style) {
163 super(parent, style);
164 }
165
166 @Override
167 public void setStyle(String style) {
168 this.style = style;
169 super.setStyle(style);
170 }
171
172 // private Label label;
173 //
174 // public StyleButton(Composite parent, int swtStyle) {
175 // super(parent, SWT.NONE);
176 // setLayout(new GridLayout());
177 // label = new Label(this, swtStyle);
178 // }
179 //
180 // public Label getLabel() {
181 // return label;
182 // }
183
184 }
185
186 class StyledToolMouseListener extends MouseAdapter {
187 private static final long serialVersionUID = 8516297091549329043L;
188 private EditablePart editablePart;
189
190 public StyledToolMouseListener(EditablePart editablePart) {
191 super();
192 this.editablePart = editablePart;
193 }
194
195 @Override
196 public void mouseDown(MouseEvent e) {
197 // TODO make it more robust.
198 Label sb = (Label) e.getSource();
199 Object style = sb.getData(RWT.CUSTOM_VARIANT);
200 textViewer.setParagraphStyle((Paragraph) editablePart, style == null ? null : style.toString());
201 hide();
202 }
203 }
204
205 class ToolsShellListener extends org.eclipse.swt.events.ShellAdapter {
206 private static final long serialVersionUID = 8432350564023247241L;
207
208 @Override
209 public void shellDeactivated(ShellEvent e) {
210 hide();
211 }
212
213 }
214 }