1 package org.argeo.docbook.ui;
3 import java.util.ArrayList;
6 import org.argeo.cms.text.Paragraph;
7 import org.argeo.cms.ui.util.CmsUiUtils;
8 import org.argeo.cms.ui.viewers.EditablePart;
9 import org.argeo.cms.ui.widgets.EditableText;
10 import org.argeo.cms.ui.widgets.TextStyles;
11 import org.eclipse.rap.rwt.RWT;
12 import org.eclipse.swt.SWT;
13 import org.eclipse.swt.events.MouseAdapter;
14 import org.eclipse.swt.events.MouseEvent;
15 import org.eclipse.swt.events.ShellEvent;
16 import org.eclipse.swt.graphics.Point;
17 import org.eclipse.swt.layout.GridLayout;
18 import org.eclipse.swt.widgets.Composite;
19 import org.eclipse.swt.widgets.Control;
20 import org.eclipse.swt.widgets.Label;
21 import org.eclipse.swt.widgets.Shell;
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 };
28 private final AbstractDbkViewer textViewer;
30 // private List<StyleButton> styleButtons = new ArrayList<DbkContextMenu.StyleButton>();
32 // private Label deleteButton, publishButton, editButton;
34 private EditablePart currentTextPart;
38 public DbkContextMenu(AbstractDbkViewer textViewer, Shell parentShell) {
39 // shell = new Shell(display, SWT.NO_TRIM | SWT.BORDER | SWT.ON_TOP);
40 shell = new Shell(parentShell, SWT.BORDER);
41 // super(display, SWT.NO_TRIM | SWT.BORDER | SWT.ON_TOP);
42 this.textViewer = textViewer;
43 shell.setLayout(new GridLayout());
44 // shell.setData(RWT.CUSTOM_VARIANT, TEXT_STYLED_TOOLS_DIALOG);
46 shell.addShellListener(new ToolsShellListener());
49 public void show(EditablePart source, Point location, List<String> availableStyles) {
50 if (shell.isVisible())
51 shell.setVisible(false);
52 CmsUiUtils.clear(shell);
54 if (availableStyles.isEmpty())
57 StyledToolMouseListener stml = new StyledToolMouseListener();
58 List<StyleButton> styleButtons = new ArrayList<DbkContextMenu.StyleButton>();
59 if (textViewer.getCmsEditable().isEditing()) {
60 for (String style : availableStyles) {
61 StyleButton styleButton = new StyleButton(shell, SWT.WRAP);
62 if (!"".equals(style))
63 styleButton.setStyle(style);
65 styleButton.setStyle(null);
66 // if (!"".equals(style))
67 // styleButton.getLabel().setData(RWT.CUSTOM_VARIANT, style);
68 // styleButton.getLabel().setData(RWT.MARKUP_ENABLED, true);
69 styleButton.setMouseListener(stml);
70 styleButtons.add(styleButton);
74 // deleteButton = new Label(shell, SWT.NONE);
75 // deleteButton.setText("Delete");
76 // deleteButton.addMouseListener(stml);
79 // publishButton = new Label(shell, SWT.NONE);
80 // publishButton.setText("Publish");
81 // publishButton.addMouseListener(stml);
82 } else if (textViewer.getCmsEditable().canEdit()) {
84 Label editButton = new Label(shell, SWT.NONE);
85 editButton.setText("Edit");
86 editButton.addMouseListener(stml);
89 this.currentTextPart = source;
91 if (currentTextPart instanceof Paragraph) {
93 String text = textViewer.getRawParagraphText((Paragraph) currentTextPart);
94 String textToShow = text.length() > size ? text.substring(0, size - 3) + "..." : text;
95 for (StyleButton styleButton : styleButtons) {
96 styleButton.setText((styleButton.style == null ? "default" : styleButton.style) + " : " + textToShow);
101 if (source instanceof Control) {
102 int height = shell.getSize().y;
103 int parentShellHeight = shell.getShell().getSize().y;
104 if ((location.y + height) < parentShellHeight) {
105 shell.setLocation(((Control) source).toDisplay(location.x, location.y));
107 shell.setLocation(((Control) source).toDisplay(location.x, location.y - parentShellHeight));
113 class StyleButton extends EditableText {
114 private static final long serialVersionUID = 7731102609123946115L;
118 public StyleButton(Composite parent, int style) {
119 super(parent, style);
123 public void setStyle(String style) {
125 super.setStyle(style);
128 // private Label label;
130 // public StyleButton(Composite parent, int swtStyle) {
131 // super(parent, SWT.NONE);
132 // setLayout(new GridLayout());
133 // label = new Label(this, swtStyle);
136 // public Label getLabel() {
142 class StyledToolMouseListener extends MouseAdapter {
143 private static final long serialVersionUID = 8516297091549329043L;
146 public void mouseDown(MouseEvent e) {
147 // Object eventSource = e.getSource();
148 //// if (eventSource instanceof StyleButton) {
149 // TODO make it more robust.
150 Label sb = (Label) e.getSource();
151 Object style = sb.getData(RWT.CUSTOM_VARIANT);
152 textViewer.setParagraphStyle((Paragraph) currentTextPart, style == null ? null : style.toString());
154 // } else if (eventSource == deleteButton) {
155 // textViewer.deletePart((SectionPart) currentTextPart);
156 // } else if (eventSource == editButton) {
157 // textViewer.getCmsEditable().startEditing();
158 // } else if (eventSource == publishButton) {
159 // textViewer.getCmsEditable().stopEditing();
160 shell.setVisible(false);
164 class ToolsShellListener extends org.eclipse.swt.events.ShellAdapter {
165 private static final long serialVersionUID = 8432350564023247241L;
168 public void shellDeactivated(ShellEvent e) {
169 shell.setVisible(false);