editor now updatable via updateSite
[taxeditor.git] / taxeditor-editor / src / main / java / eu / etaxonomy / taxeditor / propertysheet / AnnotationsDialog.java
1 package eu.etaxonomy.taxeditor.propertysheet;
2
3 import java.util.HashSet;
4 import java.util.Set;
5
6 import org.eclipse.core.databinding.beans.BeansObservables;
7 import org.eclipse.core.databinding.observable.list.WritableList;
8 import org.eclipse.core.databinding.observable.map.IObservableMap;
9 import org.eclipse.jface.action.Action;
10 import org.eclipse.jface.action.MenuManager;
11 import org.eclipse.jface.databinding.viewers.ObservableListContentProvider;
12 import org.eclipse.jface.databinding.viewers.ObservableMapLabelProvider;
13 import org.eclipse.jface.viewers.TableViewer;
14 import org.eclipse.swt.SWT;
15 import org.eclipse.swt.events.MouseAdapter;
16 import org.eclipse.swt.events.MouseEvent;
17 import org.eclipse.swt.events.SelectionAdapter;
18 import org.eclipse.swt.events.SelectionEvent;
19 import org.eclipse.swt.layout.FillLayout;
20 import org.eclipse.swt.layout.GridData;
21 import org.eclipse.swt.layout.GridLayout;
22 import org.eclipse.swt.widgets.Button;
23 import org.eclipse.swt.widgets.Composite;
24 import org.eclipse.swt.widgets.Dialog;
25 import org.eclipse.swt.widgets.Display;
26 import org.eclipse.swt.widgets.Label;
27 import org.eclipse.swt.widgets.Menu;
28 import org.eclipse.swt.widgets.Shell;
29 import org.eclipse.swt.widgets.Table;
30 import org.eclipse.swt.widgets.Text;
31
32 import eu.etaxonomy.cdm.model.common.AnnotatableEntity;
33 import eu.etaxonomy.cdm.model.common.Annotation;
34
35 public class AnnotationsDialog extends Dialog {
36
37 private Text text;
38 private Table table;
39 protected Object result;
40 protected Shell shell;
41
42 private WritableList list = new WritableList();
43 private AnnotatableEntity entity;
44
45 /**
46 * Create the dialog
47 * @param parent
48 * @param style
49 */
50 public AnnotationsDialog(Shell parent, int style) {
51 super(parent, style);
52 }
53
54 /**
55 * Create the dialog
56 * @param parent
57 */
58 public AnnotationsDialog(Shell parent) {
59 this(parent, SWT.NONE);
60 }
61
62 public AnnotationsDialog(Shell parent, AnnotatableEntity entity) {
63 this(parent, SWT.NONE);
64 this.entity = entity;
65 Set<Annotation> annotations = entity.getAnnotations();
66 for (Annotation annotation : annotations) {
67 list.add(annotation);
68 }
69 }
70
71 /**
72 * Open the dialog
73 * @return the result
74 */
75 public Object open() {
76 createContents();
77 shell.open();
78 shell.layout();
79 Display display = getParent().getDisplay();
80 while (!shell.isDisposed()) {
81 if (!display.readAndDispatch())
82 display.sleep();
83 }
84 return result;
85 }
86
87 /**
88 * Create contents of the dialog
89 */
90 protected void createContents() {
91
92 // Create shell for popup dialog
93 shell = new Shell(getParent(), SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL);
94 shell.setLayout(new FillLayout());
95 shell.setSize(500, 375);
96 shell.setText("Annotations");
97
98 // Create composite for entire shell
99 final Composite composite = new Composite(shell, SWT.NONE);
100 final GridLayout gridLayout = new GridLayout();
101 gridLayout.numColumns = 3;
102 composite.setLayout(gridLayout);
103
104 text = new Text(composite, SWT.BORDER);
105 text.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 2, 1));
106
107 final Button addButton = new Button(composite, SWT.NONE);
108 addButton.setLayoutData(new GridData());
109 addButton.setText("Add");
110 addButton.addMouseListener(new MouseAdapter() {
111 @Override
112 public void mouseDown(MouseEvent e) {
113 if (text.getText().equals("")) {
114 return;
115 }
116 Annotation annotation = Annotation.NewInstance(text.getText(), null);
117 list.add(annotation);
118 entity.addAnnotation(annotation);
119 text.setText("");
120 }
121 });
122
123 final TableViewer tableViewer = new TableViewer(composite, SWT.BORDER | SWT.MULTI);
124 table = tableViewer.getTable();
125 table.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 3, 1));
126 table.setLinesVisible(false);
127 table.setHeaderVisible(false);
128 new Label(composite, SWT.NONE);
129
130 ObservableListContentProvider providerList = new ObservableListContentProvider();
131 tableViewer.setContentProvider(providerList);
132
133 IObservableMap[] providerMaps = BeansObservables.observeMaps(
134 providerList.getKnownElements(), Annotation.class, new String[]{"text"});
135 tableViewer.setLabelProvider(new ObservableMapLabelProvider(providerMaps));
136 tableViewer.setInput(list);
137
138 final MenuManager manager = new MenuManager();
139 final Menu menu = manager.createContextMenu(table);
140 table.setMenu(menu);
141
142 table.addSelectionListener(new SelectionAdapter() {
143 public void widgetSelected(SelectionEvent e) {
144
145 Object data = e.item.getData();
146
147 if (data instanceof Annotation) {
148 manager.removeAll();
149 manager.add(new RemoveAnnotationAction((Annotation) data));
150 }
151 }
152 });
153
154 final Button cancelButton = new Button(composite, SWT.NONE);
155 cancelButton.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false));
156 cancelButton.setText("Cancel");
157 cancelButton.addMouseListener(new MouseAdapter() {
158 @Override
159 public void mouseUp(MouseEvent e) {
160 shell.dispose();
161 }
162 });
163
164 final Button okButton = new Button(composite, SWT.NONE);
165 final GridData gd_okButton = new GridData();
166 okButton.setLayoutData(gd_okButton);
167 okButton.setText("OK");
168 okButton.addMouseListener(new MouseAdapter() {
169 @Override
170 public void mouseUp(MouseEvent e) {
171 result = new HashSet<Object>(list);
172 shell.dispose();
173 }
174 });
175 }
176
177 public class RemoveAnnotationAction extends Action {
178
179 private Annotation annotation;
180
181 RemoveAnnotationAction(Annotation annotation) {
182 setText("Remove");
183 this.annotation = annotation;
184 }
185
186 public void run() {
187 list.remove(annotation);
188 entity.removeAnnotation(annotation);
189 }
190 }
191 }