]> git.argeo.org Git - gpl/argeo-suite.git/blob - DbkContextMenu.java
57c835608c31f2c744a55b8a31ad23c7145ea5d9
[gpl/argeo-suite.git] / 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.ui.CmsEditable;
9 import org.argeo.cms.ui.util.CmsUiUtils;
10 import org.argeo.cms.ui.viewers.EditablePart;
11 import org.argeo.cms.ui.viewers.NodePart;
12 import org.argeo.cms.ui.viewers.Section;
13 import org.argeo.cms.ui.viewers.SectionPart;
14 import org.argeo.cms.ui.widgets.EditableText;
15 import org.argeo.cms.ui.widgets.Img;
16 import org.argeo.docbook.DbkMsg;
17 import org.argeo.docbook.DbkUtils;
18 import org.argeo.eclipse.ui.MouseDown;
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(EditablePart editablePart, Point location, List<String> availableStyles) {
50 if (shell.isVisible())
51 shell.setVisible(false);
52 CmsUiUtils.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, DbkMsg.insertMedia.lead(), paragraph);
62
63 } else if (editablePart instanceof Img) {
64 Img img = (Img) editablePart;
65 deletePartB(parent, DbkMsg.deleteMedia.lead(), img);
66 insertMediaB(parent, DbkMsg.insertMedia.lead(), img);
67 insertParagraphB(parent, DbkMsg.insertParagraph.lead(), img);
68
69 } else if (editablePart instanceof DocBookSectionTitle) {
70 DocBookSectionTitle sectionTitle = (DocBookSectionTitle) 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, DbkMsg.insertMedia.lead(), 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, String msg, SectionPart sectionPart) {
132 insertMediaB(parent, msg, sectionPart.getSection(), sectionPart);
133 }
134
135 protected void insertMediaB(Composite parent, String msg, Section section, NodePart nodePart) {
136 Label insertMediaB = new Label(parent, SWT.NONE);
137 insertMediaB.setText(DbkMsg.insertMedia.lead());
138 insertMediaB.addMouseListener((MouseDown) (e) -> {
139 Node newNode = DbkUtils.insertImageAfter(nodePart.getNode());
140 Jcr.save(newNode);
141 textViewer.insertPart(section, newNode);
142 hide();
143 });
144
145 }
146
147 protected void insertParagraphB(Composite parent, String msg, SectionPart sectionPart) {
148 Label insertMediaB = new Label(parent, SWT.NONE);
149 insertMediaB.setText(msg);
150 insertMediaB.addMouseListener((MouseDown) (e) -> {
151 textViewer.addParagraph(sectionPart, null);
152 hide();
153 });
154 }
155
156 protected void deletePartB(Composite parent, String msg, SectionPart sectionPart) {
157 Label deleteB = new Label(shell, SWT.NONE);
158 deleteB.setText(msg);
159 deleteB.addMouseListener((MouseDown) (e) -> {
160 textViewer.deletePart(sectionPart);
161 hide();
162 });
163 }
164
165 class StyleButton extends EditableText {
166 private static final long serialVersionUID = 7731102609123946115L;
167
168 String style;
169
170 public StyleButton(Composite parent, int style) {
171 super(parent, style);
172 }
173
174 @Override
175 public void setStyle(String style) {
176 this.style = style;
177 super.setStyle(style);
178 }
179
180 // private Label label;
181 //
182 // public StyleButton(Composite parent, int swtStyle) {
183 // super(parent, SWT.NONE);
184 // setLayout(new GridLayout());
185 // label = new Label(this, swtStyle);
186 // }
187 //
188 // public Label getLabel() {
189 // return label;
190 // }
191
192 }
193
194 class StyledToolMouseListener extends MouseAdapter {
195 private static final long serialVersionUID = 8516297091549329043L;
196 private EditablePart editablePart;
197
198 public StyledToolMouseListener(EditablePart editablePart) {
199 super();
200 this.editablePart = editablePart;
201 }
202
203 @Override
204 public void mouseDown(MouseEvent e) {
205 // TODO make it more robust.
206 Label sb = (Label) e.getSource();
207 Object style = sb.getData(RWT.CUSTOM_VARIANT);
208 textViewer.setParagraphStyle((Paragraph) editablePart, style == null ? null : style.toString());
209 hide();
210 }
211 }
212
213 class ToolsShellListener extends org.eclipse.swt.events.ShellAdapter {
214 private static final long serialVersionUID = 8432350564023247241L;
215
216 @Override
217 public void shellDeactivated(ShellEvent e) {
218 hide();
219 }
220
221 }
222 }