1 package org
.argeo
.docbook
.ui
;
3 import java
.util
.ArrayList
;
6 import org
.argeo
.cms
.text
.Paragraph
;
7 import org
.argeo
.cms
.ui
.viewers
.EditablePart
;
8 import org
.argeo
.cms
.ui
.viewers
.SectionPart
;
9 import org
.argeo
.cms
.ui
.widgets
.TextStyles
;
10 import org
.eclipse
.rap
.rwt
.RWT
;
11 import org
.eclipse
.swt
.SWT
;
12 import org
.eclipse
.swt
.events
.MouseAdapter
;
13 import org
.eclipse
.swt
.events
.MouseEvent
;
14 import org
.eclipse
.swt
.events
.ShellEvent
;
15 import org
.eclipse
.swt
.graphics
.Point
;
16 import org
.eclipse
.swt
.layout
.GridLayout
;
17 import org
.eclipse
.swt
.widgets
.Composite
;
18 import org
.eclipse
.swt
.widgets
.Control
;
19 import org
.eclipse
.swt
.widgets
.Display
;
20 import org
.eclipse
.swt
.widgets
.Label
;
21 import org
.eclipse
.swt
.widgets
.Shell
;
23 /** Dialog to edit a text part. */
24 class DbkContextMenu
implements TextStyles
{
25 private final static String
[] DEFAULT_TEXT_STYLES
= { TextStyles
.TEXT_DEFAULT
, TextStyles
.TEXT_PRE
,
26 TextStyles
.TEXT_QUOTE
};
28 private final AbstractDbkViewer textViewer
;
30 private List
<StyleButton
> styleButtons
= new ArrayList
<DbkContextMenu
.StyleButton
>();
32 private Label deleteButton
, publishButton
, editButton
;
34 private EditablePart currentTextPart
;
38 public DbkContextMenu(AbstractDbkViewer textViewer
, Display display
) {
39 shell
= new Shell(display
, SWT
.NO_TRIM
| SWT
.BORDER
| SWT
.ON_TOP
);
40 // super(display, SWT.NO_TRIM | SWT.BORDER | SWT.ON_TOP);
41 this.textViewer
= textViewer
;
42 shell
.setLayout(new GridLayout());
43 shell
.setData(RWT
.CUSTOM_VARIANT
, TEXT_STYLED_TOOLS_DIALOG
);
45 StyledToolMouseListener stml
= new StyledToolMouseListener();
46 if (textViewer
.getCmsEditable().isEditing()) {
47 for (String style
: DEFAULT_TEXT_STYLES
) {
48 StyleButton styleButton
= new StyleButton(shell
, SWT
.WRAP
);
49 styleButton
.getLabel().setData(RWT
.CUSTOM_VARIANT
, style
);
50 styleButton
.getLabel().setData(RWT
.MARKUP_ENABLED
, true);
51 styleButton
.getLabel().addMouseListener(stml
);
52 styleButtons
.add(styleButton
);
56 deleteButton
= new Label(shell
, SWT
.NONE
);
57 deleteButton
.setText("Delete");
58 deleteButton
.addMouseListener(stml
);
61 publishButton
= new Label(shell
, SWT
.NONE
);
62 publishButton
.setText("Publish");
63 publishButton
.addMouseListener(stml
);
64 } else if (textViewer
.getCmsEditable().canEdit()) {
66 editButton
= new Label(shell
, SWT
.NONE
);
67 editButton
.setText("Edit");
68 editButton
.addMouseListener(stml
);
70 shell
.addShellListener(new ToolsShellListener());
73 public void show(EditablePart source
, Point location
) {
74 if (shell
.isVisible())
75 shell
.setVisible(false);
77 this.currentTextPart
= source
;
79 if (currentTextPart
instanceof Paragraph
) {
81 String text
= textViewer
.getRawParagraphText((Paragraph
) currentTextPart
);
82 String textToShow
= text
.length() > size ? text
.substring(0, size
- 3) + "..." : text
;
83 for (StyleButton styleButton
: styleButtons
) {
84 styleButton
.getLabel().setText(textToShow
);
89 if (source
instanceof Control
)
90 shell
.setLocation(((Control
) source
).toDisplay(location
.x
, location
.y
));
94 class StyleButton
extends Composite
{
95 private static final long serialVersionUID
= 7731102609123946115L;
98 public StyleButton(Composite parent
, int swtStyle
) {
99 super(parent
, SWT
.NONE
);
100 label
= new Label(this, swtStyle
);
103 public Label
getLabel() {
109 class StyledToolMouseListener
extends MouseAdapter
{
110 private static final long serialVersionUID
= 8516297091549329043L;
113 public void mouseDown(MouseEvent e
) {
114 Object eventSource
= e
.getSource();
115 if (eventSource
instanceof StyleButton
) {
116 StyleButton sb
= (StyleButton
) e
.getSource();
117 String style
= sb
.getData(RWT
.CUSTOM_VARIANT
).toString();
118 textViewer
.setParagraphStyle((Paragraph
) currentTextPart
, style
);
119 } else if (eventSource
== deleteButton
) {
120 textViewer
.deletePart((SectionPart
) currentTextPart
);
121 } else if (eventSource
== editButton
) {
122 textViewer
.getCmsEditable().startEditing();
123 } else if (eventSource
== publishButton
) {
124 textViewer
.getCmsEditable().stopEditing();
126 shell
.setVisible(false);
130 class ToolsShellListener
extends org
.eclipse
.swt
.events
.ShellAdapter
{
131 private static final long serialVersionUID
= 8432350564023247241L;
134 public void shellDeactivated(ShellEvent e
) {
135 shell
.setVisible(false);