1 package eu
.etaxonomy
.taxeditor
.propertysheet
;
3 import java
.util
.HashSet
;
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
;
33 import eu
.etaxonomy
.cdm
.model
.common
.AnnotatableEntity
;
34 import eu
.etaxonomy
.cdm
.model
.common
.Annotation
;
36 public class AnnotationsDialog
extends Dialog
{
40 private static final Logger logger
= Logger
41 .getLogger(AnnotationsDialog
.class);
42 protected Object result
;
43 protected Shell shell
;
45 private WritableList list
= new WritableList();
46 private Annotation selectedAnnotation
;
47 private AnnotatableEntity entity
;
54 public AnnotationsDialog(Shell parent
, int style
) {
62 public AnnotationsDialog(Shell parent
) {
63 this(parent
, SWT
.NONE
);
66 public AnnotationsDialog(Shell parent
, AnnotatableEntity entity
) {
67 this(parent
, SWT
.NONE
);
69 Set
<Annotation
> annotations
= entity
.getAnnotations();
70 for (Annotation annotation
: annotations
) {
79 public Object
open() {
83 Display display
= getParent().getDisplay();
84 while (!shell
.isDisposed()) {
85 if (!display
.readAndDispatch())
92 * Create contents of the dialog
94 protected void createContents() {
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");
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
);
108 text
= new Text(composite
, SWT
.BORDER
);
109 text
.setLayoutData(new GridData(SWT
.FILL
, SWT
.CENTER
, true, false, 2, 1));
111 final Button addButton
= new Button(composite
, SWT
.NONE
);
112 addButton
.setLayoutData(new GridData());
113 addButton
.setText("Add");
114 addButton
.addMouseListener(new MouseAdapter() {
116 public void mouseDown(MouseEvent e
) {
117 if (text
.getText().equals("")) {
120 Annotation annotation
= Annotation
.NewInstance(text
.getText(), null);
121 list
.add(annotation
);
122 entity
.addAnnotation(annotation
);
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
);
134 ObservableListContentProvider providerList
= new ObservableListContentProvider();
135 tableViewer
.setContentProvider(providerList
);
137 IObservableMap
[] providerMaps
= BeansObservables
.observeMaps(
138 providerList
.getKnownElements(), Annotation
.class, new String
[]{"text"});
139 tableViewer
.setLabelProvider(new ObservableMapLabelProvider(providerMaps
));
140 tableViewer
.setInput(list
);
142 final MenuManager manager
= new MenuManager();
143 final Menu menu
= manager
.createContextMenu(table
);
146 table
.addSelectionListener(new SelectionAdapter() {
147 public void widgetSelected(SelectionEvent e
) {
149 Object data
= e
.item
.getData();
151 if (data
instanceof Annotation
) {
153 manager
.add(new RemoveAnnotationAction((Annotation
) data
));
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() {
163 public void mouseUp(MouseEvent e
) {
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() {
174 public void mouseUp(MouseEvent e
) {
175 result
= new HashSet(list
);
181 public class RemoveAnnotationAction
extends Action
{
183 private Annotation annotation
;
185 RemoveAnnotationAction(Annotation annotation
) {
187 this.annotation
= annotation
;
191 list
.remove(annotation
);
192 entity
.removeAnnotation(annotation
);