]> git.argeo.org Git - gpl/argeo-suite.git/blob - DbkContextMenu.java
1673bd8743fdda242a346b22c57cd444e3ab9175
[gpl/argeo-suite.git] / DbkContextMenu.java
1 package org.argeo.app.ui.docbook;
2
3 import java.util.ArrayList;
4 import java.util.List;
5
6 import javax.jcr.Node;
7
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;
31
32 /** Dialog to edit a text part. */
33 class DbkContextMenu {
34 private final AbstractDbkViewer textViewer;
35
36 private Shell shell;
37
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);
45
46 shell.addShellListener(new ToolsShellListener());
47 }
48
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())
56 // return;
57
58 if (editablePart instanceof Paragraph) {
59 Paragraph paragraph = (Paragraph) editablePart;
60 deletePartB(parent, DbkMsg.deleteParagraph.lead(), paragraph);
61 insertMediaB(parent, paragraph);
62
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);
68
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);
77 hide();
78 });
79 }
80 insertMediaB(parent, sectionTitle.getSection(), sectionTitle);
81 }
82
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);
90 else
91 styleButton.setStyle(null);
92 styleButton.setMouseListener(stml);
93 styleButtons.add(styleButton);
94 }
95 } else if (cmsEditable.canEdit()) {
96 // Edit
97 // Label editButton = new Label(shell, SWT.NONE);
98 // editButton.setText("Edit");
99 // editButton.addMouseListener(stml);
100 }
101
102 if (editablePart instanceof Paragraph) {
103 final int size = 32;
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);
108 }
109 }
110
111 shell.pack();
112 shell.layout();
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));
118 } else {
119 shell.setLocation(((Control) editablePart).toDisplay(location.x, location.y - parentShellHeight));
120 }
121 }
122
123 if (shell.getChildren().length != 0)
124 shell.open();
125 }
126
127 void hide() {
128 shell.setVisible(false);
129 }
130
131 protected void insertMediaB(Composite parent, SectionPart sectionPart) {
132 insertMediaB(parent, sectionPart.getSection(), sectionPart);
133 }
134
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());
140 Jcr.save(newNode);
141 textViewer.insertPart(section, newNode);
142 hide();
143 });
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());
148 Jcr.save(newNode);
149 textViewer.insertPart(section, newNode);
150 hide();
151 });
152
153 }
154
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);
160 hide();
161 });
162 }
163
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);
169 hide();
170 });
171 }
172
173 class StyleButton extends EditableText {
174 private static final long serialVersionUID = 7731102609123946115L;
175
176 String style;
177
178 public StyleButton(Composite parent, int style) {
179 super(parent, style);
180 }
181
182 @Override
183 public void setStyle(String style) {
184 this.style = style;
185 super.setStyle(style);
186 }
187
188 // private Label label;
189 //
190 // public StyleButton(Composite parent, int swtStyle) {
191 // super(parent, SWT.NONE);
192 // setLayout(new GridLayout());
193 // label = new Label(this, swtStyle);
194 // }
195 //
196 // public Label getLabel() {
197 // return label;
198 // }
199
200 }
201
202 class StyledToolMouseListener extends MouseAdapter {
203 private static final long serialVersionUID = 8516297091549329043L;
204 private SwtEditablePart editablePart;
205
206 public StyledToolMouseListener(SwtEditablePart editablePart) {
207 super();
208 this.editablePart = editablePart;
209 }
210
211 @Override
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());
217 hide();
218 }
219 }
220
221 class ToolsShellListener extends org.eclipse.swt.events.ShellAdapter {
222 private static final long serialVersionUID = 8432350564023247241L;
223
224 @Override
225 public void shellDeactivated(ShellEvent e) {
226 hide();
227 }
228
229 }
230 }