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