Project

General

Profile

Download (6.4 KB) Statistics
| Branch: | Tag: | Revision:
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.TableItem;
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
	protected Object result;
41
	protected Shell shell;
42
	
43
	private WritableList list = new WritableList();
44
	private AnnotatableEntity entity;
45
	protected RemoveAnnotationAction removeAction;
46

    
47
	/**
48
	 * Create the dialog
49
	 * @param parent
50
	 * @param style
51
	 */
52
	public AnnotationsDialog(Shell parent, int style) {
53
		super(parent, style);
54
	}
55

    
56
	/**
57
	 * Create the dialog
58
	 * @param parent
59
	 */
60
	public AnnotationsDialog(Shell parent) {
61
		this(parent, SWT.NONE);
62
	}
63

    
64
	public AnnotationsDialog(Shell parent, AnnotatableEntity entity) {
65
		this(parent, SWT.NONE);
66
		this.entity = entity;
67
		Set<Annotation> annotations = entity.getAnnotations();
68
		for (Annotation annotation : annotations) {
69
			list.add(annotation);
70
		}
71
	}
72

    
73
	/**
74
	 * Open the dialog
75
	 * @return the result
76
	 */
77
	public Object open() {
78
		createContents();
79
		shell.open();
80
		shell.layout();
81
		Display display = getParent().getDisplay();
82
		while (!shell.isDisposed()) {
83
			if (!display.readAndDispatch())
84
				display.sleep();
85
		}
86
		return result;
87
	}
88

    
89
	/**
90
	 * Create contents of the dialog
91
	 */
92
	protected void createContents() {
93
		
94
		// Create shell for popup dialog
95
		shell = new Shell(getParent(), SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL);
96
		shell.setLayout(new FillLayout());
97
		shell.setSize(500, 375);
98
		shell.setText("Annotations");
99
		
100
		// Create composite for entire shell
101
		final Composite composite = new Composite(shell, SWT.NONE);
102
		final GridLayout gridLayout = new GridLayout();
103
		gridLayout.numColumns = 3;
104
		composite.setLayout(gridLayout);
105

    
106
		text = new Text(composite, SWT.BORDER);
107
		text.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 2, 1));
108

    
109
		final Button addButton = new Button(composite, SWT.NONE);
110
		addButton.setLayoutData(new GridData());
111
		addButton.setText("Add");
112
		addButton.addMouseListener(new MouseAdapter() {
113
			@Override
114
			public void mouseUp(MouseEvent e) {
115
				if (text.getText().equals("")) {
116
					return;
117
				}
118
				Annotation annotation = Annotation.NewInstance(text.getText(), null);
119
				list.add(annotation);
120
				entity.addAnnotation(annotation);
121
				text.setText("");
122
			}
123
		});
124

    
125
		final Button removeButton = new Button(composite, SWT.NONE);
126
		removeButton.setLayoutData(new GridData());
127
		removeButton.setText("Remove");
128
		removeButton.setEnabled(false);
129
		removeButton.addMouseListener(new MouseAdapter() {
130
			/* (non-Javadoc)
131
			 * @see org.eclipse.swt.events.MouseAdapter#mouseDown(org.eclipse.swt.events.MouseEvent)
132
			 */
133
			@Override
134
			public void mouseUp(MouseEvent e) {
135
				removeAnnotations();
136
			}
137
		});
138
		
139
		final TableViewer tableViewer = new TableViewer(composite, SWT.BORDER | SWT.MULTI);
140
		table = tableViewer.getTable();
141
		table.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 3, 1));
142
		table.setLinesVisible(false);
143
		table.setHeaderVisible(false);
144
		new Label(composite, SWT.NONE);
145

    
146
		ObservableListContentProvider providerList = new ObservableListContentProvider();
147
		tableViewer.setContentProvider(providerList);
148
		
149
		IObservableMap[] providerMaps = BeansObservables.observeMaps(
150
				providerList.getKnownElements(), Annotation.class, new String[]{"text"});
151
		tableViewer.setLabelProvider(new ObservableMapLabelProvider(providerMaps));
152
		tableViewer.setInput(list);
153
		
154
		final MenuManager manager = new MenuManager();
155
		final Menu menu = manager.createContextMenu(table);
156
		table.setMenu(menu);
157
		
158
		table.addSelectionListener(new SelectionAdapter() {
159
			public void widgetSelected(SelectionEvent e) {
160
				
161
				Object data = e.item.getData();
162
				
163
				if (data instanceof Annotation) {
164
					manager.removeAll();
165
					removeAction = new RemoveAnnotationAction();
166
					manager.add(removeAction);
167
				}
168
				
169
				removeButton.setEnabled(true);
170
			}
171
		});
172

    
173
		final Button cancelButton = new Button(composite, SWT.NONE);
174
		cancelButton.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false));
175
		cancelButton.setText("Cancel");
176
		cancelButton.addMouseListener(new MouseAdapter() {
177
			@Override
178
			public void mouseUp(MouseEvent e) {
179
				shell.dispose();
180
			}
181
		});
182
			
183
		final Button okButton = new Button(composite, SWT.NONE);
184
		final GridData gd_okButton = new GridData();
185
		okButton.setLayoutData(gd_okButton);
186
		okButton.setText("OK");
187
		okButton.addMouseListener(new MouseAdapter() {
188
			@Override
189
			public void mouseUp(MouseEvent e) {
190
				result = new HashSet<Object>(list);
191
				shell.dispose();
192
			}
193
		});
194
	}
195
	
196
	private void removeAnnotations() {
197
		TableItem[] selectedItems = table.getSelection();
198
		for (TableItem item : selectedItems) {
199
			removeAnnotation((Annotation) item.getData());
200
		}
201
	}
202

    
203
	private void removeAnnotation(Annotation annotation) {
204
		list.remove(annotation);
205
		entity.removeAnnotation(annotation);
206
	}
207
	
208
	public class RemoveAnnotationAction extends Action {
209
		
210
		RemoveAnnotationAction() {
211
			setText("Remove");
212
		}
213
		
214
		public void run() {	
215
			removeAnnotations();
216
		}
217
	}
218
}
(3-3/24)