1 package org
.argeo
.cms
.ui
.internal
.text
;
3 import java
.util
.ArrayList
;
6 import org
.argeo
.cms
.CmsNames
;
7 import org
.argeo
.cms
.text
.Paragraph
;
8 import org
.argeo
.cms
.text
.TextStyles
;
9 import org
.argeo
.cms
.viewers
.EditablePart
;
10 import org
.argeo
.cms
.viewers
.SectionPart
;
11 import org
.eclipse
.rap
.rwt
.RWT
;
12 import org
.eclipse
.swt
.SWT
;
13 import org
.eclipse
.swt
.events
.MouseAdapter
;
14 import org
.eclipse
.swt
.events
.MouseEvent
;
15 import org
.eclipse
.swt
.events
.ShellEvent
;
16 import org
.eclipse
.swt
.graphics
.Point
;
17 import org
.eclipse
.swt
.layout
.GridLayout
;
18 import org
.eclipse
.swt
.widgets
.Composite
;
19 import org
.eclipse
.swt
.widgets
.Control
;
20 import org
.eclipse
.swt
.widgets
.Display
;
21 import org
.eclipse
.swt
.widgets
.Label
;
22 import org
.eclipse
.swt
.widgets
.Shell
;
24 /** Dialog to edit a text part. */
25 class TextContextMenu
extends Shell
implements CmsNames
, TextStyles
{
26 private final static String
[] DEFAULT_TEXT_STYLES
= {
27 TextStyles
.TEXT_DEFAULT
, TextStyles
.TEXT_PRE
, TextStyles
.TEXT_QUOTE
};
29 private final AbstractTextViewer textViewer
;
31 private static final long serialVersionUID
= -3826246895162050331L;
32 private List
<StyleButton
> styleButtons
= new ArrayList
<TextContextMenu
.StyleButton
>();
34 private Label deleteButton
, publishButton
, editButton
;
36 private EditablePart currentTextPart
;
38 public TextContextMenu(AbstractTextViewer textViewer
, Display display
) {
39 super(display
, SWT
.NO_TRIM
| SWT
.BORDER
| SWT
.ON_TOP
);
40 this.textViewer
= textViewer
;
41 setLayout(new GridLayout());
42 setData(RWT
.CUSTOM_VARIANT
, TEXT_STYLED_TOOLS_DIALOG
);
44 StyledToolMouseListener stml
= new StyledToolMouseListener();
45 if (textViewer
.getCmsEditable().isEditing()) {
46 for (String style
: DEFAULT_TEXT_STYLES
) {
47 StyleButton styleButton
= new StyleButton(this, SWT
.WRAP
);
48 styleButton
.setData(RWT
.CUSTOM_VARIANT
, style
);
49 styleButton
.setData(RWT
.MARKUP_ENABLED
, true);
50 styleButton
.addMouseListener(stml
);
51 styleButtons
.add(styleButton
);
55 deleteButton
= new Label(this, SWT
.NONE
);
56 deleteButton
.setText("Delete");
57 deleteButton
.addMouseListener(stml
);
60 publishButton
= new Label(this, SWT
.NONE
);
61 publishButton
.setText("Publish");
62 publishButton
.addMouseListener(stml
);
63 } else if (textViewer
.getCmsEditable().canEdit()) {
65 editButton
= new Label(this, SWT
.NONE
);
66 editButton
.setText("Edit");
67 editButton
.addMouseListener(stml
);
69 addShellListener(new ToolsShellListener());
72 public void show(EditablePart source
, Point location
) {
76 this.currentTextPart
= source
;
78 if (currentTextPart
instanceof Paragraph
) {
80 String text
= textViewer
81 .getRawParagraphText((Paragraph
) currentTextPart
);
82 String textToShow
= text
.length() > size ? text
.substring(0,
83 size
- 3) + "..." : text
;
84 for (StyleButton styleButton
: styleButtons
) {
85 styleButton
.setText(textToShow
);
90 if (source
instanceof Control
)
91 setLocation(((Control
) source
).toDisplay(location
.x
, location
.y
));
95 class StyleButton
extends Label
{
96 private static final long serialVersionUID
= 7731102609123946115L;
98 public StyleButton(Composite parent
, int swtStyle
) {
99 super(parent
, swtStyle
);
104 class StyledToolMouseListener
extends MouseAdapter
{
105 private static final long serialVersionUID
= 8516297091549329043L;
108 public void mouseDown(MouseEvent e
) {
109 Object eventSource
= e
.getSource();
110 if (eventSource
instanceof StyleButton
) {
111 StyleButton sb
= (StyleButton
) e
.getSource();
112 String style
= sb
.getData(RWT
.CUSTOM_VARIANT
).toString();
114 .setParagraphStyle((Paragraph
) currentTextPart
, style
);
115 } else if (eventSource
== deleteButton
) {
116 textViewer
.deletePart((SectionPart
) currentTextPart
);
117 } else if (eventSource
== editButton
) {
118 textViewer
.getCmsEditable().startEditing();
119 } else if (eventSource
== publishButton
) {
120 textViewer
.getCmsEditable().stopEditing();
126 class ToolsShellListener
extends org
.eclipse
.swt
.events
.ShellAdapter
{
127 private static final long serialVersionUID
= 8432350564023247241L;
130 public void shellDeactivated(ShellEvent e
) {