]> git.argeo.org Git - gpl/argeo-suite.git/blob - docbook/ui/DbkContextMenu.java
Prepare next development cycle
[gpl/argeo-suite.git] / docbook / ui / DbkContextMenu.java
1 package org.argeo.docbook.ui;
2
3 import java.util.ArrayList;
4 import java.util.List;
5
6 import org.argeo.cms.text.Paragraph;
7 import org.argeo.cms.ui.viewers.EditablePart;
8 import org.argeo.cms.ui.viewers.SectionPart;
9 import org.argeo.cms.ui.widgets.TextStyles;
10 import org.eclipse.rap.rwt.RWT;
11 import org.eclipse.swt.SWT;
12 import org.eclipse.swt.events.MouseAdapter;
13 import org.eclipse.swt.events.MouseEvent;
14 import org.eclipse.swt.events.ShellEvent;
15 import org.eclipse.swt.graphics.Point;
16 import org.eclipse.swt.layout.GridLayout;
17 import org.eclipse.swt.widgets.Composite;
18 import org.eclipse.swt.widgets.Control;
19 import org.eclipse.swt.widgets.Display;
20 import org.eclipse.swt.widgets.Label;
21 import org.eclipse.swt.widgets.Shell;
22
23 /** Dialog to edit a text part. */
24 class DbkContextMenu implements TextStyles {
25 private final static String[] DEFAULT_TEXT_STYLES = { TextStyles.TEXT_DEFAULT, TextStyles.TEXT_PRE,
26 TextStyles.TEXT_QUOTE };
27
28 private final AbstractDbkViewer textViewer;
29
30 private List<StyleButton> styleButtons = new ArrayList<DbkContextMenu.StyleButton>();
31
32 private Label deleteButton, publishButton, editButton;
33
34 private EditablePart currentTextPart;
35
36 private Shell shell;
37
38 public DbkContextMenu(AbstractDbkViewer textViewer, Display display) {
39 shell = new Shell(display, SWT.NO_TRIM | SWT.BORDER | SWT.ON_TOP);
40 // super(display, SWT.NO_TRIM | SWT.BORDER | SWT.ON_TOP);
41 this.textViewer = textViewer;
42 shell.setLayout(new GridLayout());
43 shell.setData(RWT.CUSTOM_VARIANT, TEXT_STYLED_TOOLS_DIALOG);
44
45 StyledToolMouseListener stml = new StyledToolMouseListener();
46 if (textViewer.getCmsEditable().isEditing()) {
47 for (String style : DEFAULT_TEXT_STYLES) {
48 StyleButton styleButton = new StyleButton(shell, SWT.WRAP);
49 styleButton.getLabel().setData(RWT.CUSTOM_VARIANT, style);
50 styleButton.getLabel().setData(RWT.MARKUP_ENABLED, true);
51 styleButton.getLabel().addMouseListener(stml);
52 styleButtons.add(styleButton);
53 }
54
55 // Delete
56 deleteButton = new Label(shell, SWT.NONE);
57 deleteButton.setText("Delete");
58 deleteButton.addMouseListener(stml);
59
60 // Publish
61 publishButton = new Label(shell, SWT.NONE);
62 publishButton.setText("Publish");
63 publishButton.addMouseListener(stml);
64 } else if (textViewer.getCmsEditable().canEdit()) {
65 // Edit
66 editButton = new Label(shell, SWT.NONE);
67 editButton.setText("Edit");
68 editButton.addMouseListener(stml);
69 }
70 shell.addShellListener(new ToolsShellListener());
71 }
72
73 public void show(EditablePart source, Point location) {
74 if (shell.isVisible())
75 shell.setVisible(false);
76
77 this.currentTextPart = source;
78
79 if (currentTextPart instanceof Paragraph) {
80 final int size = 32;
81 String text = textViewer.getRawParagraphText((Paragraph) currentTextPart);
82 String textToShow = text.length() > size ? text.substring(0, size - 3) + "..." : text;
83 for (StyleButton styleButton : styleButtons) {
84 styleButton.getLabel().setText(textToShow);
85 }
86 }
87 shell.pack();
88 shell.layout();
89 if (source instanceof Control)
90 shell.setLocation(((Control) source).toDisplay(location.x, location.y));
91 shell.open();
92 }
93
94 class StyleButton extends Composite {
95 private static final long serialVersionUID = 7731102609123946115L;
96 private Label label;
97
98 public StyleButton(Composite parent, int swtStyle) {
99 super(parent, SWT.NONE);
100 label = new Label(this, swtStyle);
101 }
102
103 public Label getLabel() {
104 return label;
105 }
106
107 }
108
109 class StyledToolMouseListener extends MouseAdapter {
110 private static final long serialVersionUID = 8516297091549329043L;
111
112 @Override
113 public void mouseDown(MouseEvent e) {
114 Object eventSource = e.getSource();
115 if (eventSource instanceof StyleButton) {
116 StyleButton sb = (StyleButton) e.getSource();
117 String style = sb.getData(RWT.CUSTOM_VARIANT).toString();
118 textViewer.setParagraphStyle((Paragraph) currentTextPart, style);
119 } else if (eventSource == deleteButton) {
120 textViewer.deletePart((SectionPart) currentTextPart);
121 } else if (eventSource == editButton) {
122 textViewer.getCmsEditable().startEditing();
123 } else if (eventSource == publishButton) {
124 textViewer.getCmsEditable().stopEditing();
125 }
126 shell.setVisible(false);
127 }
128 }
129
130 class ToolsShellListener extends org.eclipse.swt.events.ShellAdapter {
131 private static final long serialVersionUID = 8432350564023247241L;
132
133 @Override
134 public void shellDeactivated(ShellEvent e) {
135 shell.setVisible(false);
136 }
137
138 }
139 }