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