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