Style setter for DocBook.
[gpl/argeo-suite.git] / publishing / org.argeo.publishing.ui / src / org / argeo / 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.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;
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, 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);
45
46                 shell.addShellListener(new ToolsShellListener());
47         }
48
49         public void show(EditablePart source, Point location, List<String> availableStyles) {
50                 if (shell.isVisible())
51                         shell.setVisible(false);
52                 CmsUiUtils.clear(shell);
53
54                 if (availableStyles.isEmpty())
55                         return;
56
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);
64                                 else
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);
71                         }
72
73 //                      // Delete
74 //                      deleteButton = new Label(shell, SWT.NONE);
75 //                      deleteButton.setText("Delete");
76 //                      deleteButton.addMouseListener(stml);
77 //
78 //                      // Publish
79 //                      publishButton = new Label(shell, SWT.NONE);
80 //                      publishButton.setText("Publish");
81 //                      publishButton.addMouseListener(stml);
82                 } else if (textViewer.getCmsEditable().canEdit()) {
83                         // Edit
84                         Label editButton = new Label(shell, SWT.NONE);
85                         editButton.setText("Edit");
86                         editButton.addMouseListener(stml);
87                 }
88
89                 this.currentTextPart = source;
90
91                 if (currentTextPart instanceof Paragraph) {
92                         final int size = 32;
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);
97                         }
98                 }
99                 shell.pack();
100                 shell.layout();
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));
106                         } else {
107                                 shell.setLocation(((Control) source).toDisplay(location.x, location.y - parentShellHeight));
108                         }
109                 }
110                 shell.open();
111         }
112
113         class StyleButton extends EditableText {
114                 private static final long serialVersionUID = 7731102609123946115L;
115
116                 String style;
117
118                 public StyleButton(Composite parent, int style) {
119                         super(parent, style);
120                 }
121
122                 @Override
123                 public void setStyle(String style) {
124                         this.style = style;
125                         super.setStyle(style);
126                 }
127
128 //              private Label label;
129 //
130 //              public StyleButton(Composite parent, int swtStyle) {
131 //                      super(parent, SWT.NONE);
132 //                      setLayout(new GridLayout());
133 //                      label = new Label(this, swtStyle);
134 //              }
135 //
136 //              public Label getLabel() {
137 //                      return label;
138 //              }
139
140         }
141
142         class StyledToolMouseListener extends MouseAdapter {
143                 private static final long serialVersionUID = 8516297091549329043L;
144
145                 @Override
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());
153 //                      }
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);
161                 }
162         }
163
164         class ToolsShellListener extends org.eclipse.swt.events.ShellAdapter {
165                 private static final long serialVersionUID = 8432350564023247241L;
166
167                 @Override
168                 public void shellDeactivated(ShellEvent e) {
169                         shell.setVisible(false);
170                 }
171
172         }
173 }