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