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. */
23 class TextContextMenu
extends Shell
implements CmsNames
, TextStyles
{
24 private final static String
[] DEFAULT_TEXT_STYLES
= {
25 TextStyles
.TEXT_DEFAULT
, TextStyles
.TEXT_PRE
, TextStyles
.TEXT_QUOTE
};
27 private final AbstractTextViewer textViewer
;
29 private static final long serialVersionUID
= -3826246895162050331L;
30 private List
<StyleButton
> styleButtons
= new ArrayList
<TextContextMenu
.StyleButton
>();
32 private Label deleteButton
, publishButton
, editButton
;
34 private EditablePart currentTextPart
;
36 public TextContextMenu(AbstractTextViewer textViewer
, Display display
) {
37 super(display
, SWT
.NO_TRIM
| SWT
.BORDER
| SWT
.ON_TOP
);
38 this.textViewer
= textViewer
;
39 setLayout(new GridLayout());
40 setData(RWT
.CUSTOM_VARIANT
, TEXT_STYLED_TOOLS_DIALOG
);
42 StyledToolMouseListener stml
= new StyledToolMouseListener();
43 if (textViewer
.getCmsEditable().isEditing()) {
44 for (String style
: DEFAULT_TEXT_STYLES
) {
45 StyleButton styleButton
= new StyleButton(this, SWT
.WRAP
);
46 styleButton
.setData(RWT
.CUSTOM_VARIANT
, style
);
47 styleButton
.setData(RWT
.MARKUP_ENABLED
, true);
48 styleButton
.addMouseListener(stml
);
49 styleButtons
.add(styleButton
);
53 deleteButton
= new Label(this, SWT
.NONE
);
54 deleteButton
.setText("Delete");
55 deleteButton
.addMouseListener(stml
);
58 publishButton
= new Label(this, SWT
.NONE
);
59 publishButton
.setText("Publish");
60 publishButton
.addMouseListener(stml
);
61 } else if (textViewer
.getCmsEditable().canEdit()) {
63 editButton
= new Label(this, SWT
.NONE
);
64 editButton
.setText("Edit");
65 editButton
.addMouseListener(stml
);
67 addShellListener(new ToolsShellListener());
70 public void show(EditablePart source
, Point location
) {
74 this.currentTextPart
= source
;
76 if (currentTextPart
instanceof Paragraph
) {
78 String text
= textViewer
79 .getRawParagraphText((Paragraph
) currentTextPart
);
80 String textToShow
= text
.length() > size ? text
.substring(0,
81 size
- 3) + "..." : text
;
82 for (StyleButton styleButton
: styleButtons
) {
83 styleButton
.setText(textToShow
);
88 if (source
instanceof Control
)
89 setLocation(((Control
) source
).toDisplay(location
.x
, location
.y
));
93 class StyleButton
extends Label
{
94 private static final long serialVersionUID
= 7731102609123946115L;
96 public StyleButton(Composite parent
, int swtStyle
) {
97 super(parent
, swtStyle
);
102 class StyledToolMouseListener
extends MouseAdapter
{
103 private static final long serialVersionUID
= 8516297091549329043L;
106 public void mouseDown(MouseEvent e
) {
107 Object eventSource
= e
.getSource();
108 if (eventSource
instanceof StyleButton
) {
109 StyleButton sb
= (StyleButton
) e
.getSource();
110 String style
= sb
.getData(RWT
.CUSTOM_VARIANT
).toString();
112 .setParagraphStyle((Paragraph
) currentTextPart
, style
);
113 } else if (eventSource
== deleteButton
) {
114 textViewer
.deletePart((SectionPart
) currentTextPart
);
115 } else if (eventSource
== editButton
) {
116 textViewer
.getCmsEditable().startEditing();
117 } else if (eventSource
== publishButton
) {
118 textViewer
.getCmsEditable().stopEditing();
124 class ToolsShellListener
extends org
.eclipse
.swt
.events
.ShellAdapter
{
125 private static final long serialVersionUID
= 8432350564023247241L;
128 public void shellDeactivated(ShellEvent e
) {