]> git.argeo.org Git - gpl/argeo-suite.git/blob - publishing/org.argeo.publishing.ui/src/org/argeo/cms/text/TextContextMenu.java
[maven-release-plugin] prepare release argeo-suite-2.1.17
[gpl/argeo-suite.git] / publishing / org.argeo.publishing.ui / src / org / argeo / cms / text / TextContextMenu.java
1 package org.argeo.cms.text;
2
3 import java.util.ArrayList;
4 import java.util.List;
5
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;
21
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 };
26
27 private final AbstractTextViewer textViewer;
28
29 private static final long serialVersionUID = -3826246895162050331L;
30 private List<StyleButton> styleButtons = new ArrayList<TextContextMenu.StyleButton>();
31
32 private Label deleteButton, publishButton, editButton;
33
34 private EditablePart currentTextPart;
35
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);
41
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);
50 }
51
52 // Delete
53 deleteButton = new Label(this, SWT.NONE);
54 deleteButton.setText("Delete");
55 deleteButton.addMouseListener(stml);
56
57 // Publish
58 publishButton = new Label(this, SWT.NONE);
59 publishButton.setText("Publish");
60 publishButton.addMouseListener(stml);
61 } else if (textViewer.getCmsEditable().canEdit()) {
62 // Edit
63 editButton = new Label(this, SWT.NONE);
64 editButton.setText("Edit");
65 editButton.addMouseListener(stml);
66 }
67 addShellListener(new ToolsShellListener());
68 }
69
70 public void show(EditablePart source, Point location) {
71 if (isVisible())
72 setVisible(false);
73
74 this.currentTextPart = source;
75
76 if (currentTextPart instanceof Paragraph) {
77 final int size = 32;
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);
84 }
85 }
86 pack();
87 layout();
88 if (source instanceof Control)
89 setLocation(((Control) source).toDisplay(location.x, location.y));
90 open();
91 }
92
93 class StyleButton extends Label {
94 private static final long serialVersionUID = 7731102609123946115L;
95
96 public StyleButton(Composite parent, int swtStyle) {
97 super(parent, swtStyle);
98 }
99
100 }
101
102 class StyledToolMouseListener extends MouseAdapter {
103 private static final long serialVersionUID = 8516297091549329043L;
104
105 @Override
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();
111 textViewer
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();
119 }
120 setVisible(false);
121 }
122 }
123
124 class ToolsShellListener extends org.eclipse.swt.events.ShellAdapter {
125 private static final long serialVersionUID = 8432350564023247241L;
126
127 @Override
128 public void shellDeactivated(ShellEvent e) {
129 setVisible(false);
130 }
131
132 }
133 }