1 |
229cd35b
|
p.ciardelli
|
/**
|
2 |
|
|
*
|
3 |
|
|
*/
|
4 |
|
|
package eu.etaxonomy.taxeditor.propertysheet;
|
5 |
|
|
|
6 |
|
|
import java.util.ArrayList;
|
7 |
|
|
import java.util.HashSet;
|
8 |
|
|
import java.util.List;
|
9 |
|
|
import java.util.Set;
|
10 |
|
|
|
11 |
|
|
import org.eclipse.jface.dialogs.IDialogConstants;
|
12 |
|
|
import org.eclipse.jface.dialogs.TitleAreaDialog;
|
13 |
|
|
import org.eclipse.jface.viewers.CheckboxTableViewer;
|
14 |
|
|
import org.eclipse.jface.viewers.ISelectionChangedListener;
|
15 |
|
|
import org.eclipse.jface.viewers.IStructuredContentProvider;
|
16 |
|
|
import org.eclipse.jface.viewers.IStructuredSelection;
|
17 |
|
|
import org.eclipse.jface.viewers.ITableLabelProvider;
|
18 |
|
|
import org.eclipse.jface.viewers.LabelProvider;
|
19 |
|
|
import org.eclipse.jface.viewers.SelectionChangedEvent;
|
20 |
|
|
import org.eclipse.jface.viewers.Viewer;
|
21 |
|
|
import org.eclipse.jface.window.Window;
|
22 |
|
|
import org.eclipse.swt.SWT;
|
23 |
|
|
import org.eclipse.swt.events.MouseAdapter;
|
24 |
|
|
import org.eclipse.swt.events.MouseEvent;
|
25 |
|
|
import org.eclipse.swt.graphics.Image;
|
26 |
|
|
import org.eclipse.swt.layout.GridData;
|
27 |
|
|
import org.eclipse.swt.layout.GridLayout;
|
28 |
|
|
import org.eclipse.swt.widgets.Button;
|
29 |
|
|
import org.eclipse.swt.widgets.Composite;
|
30 |
|
|
import org.eclipse.swt.widgets.Control;
|
31 |
|
|
import org.eclipse.swt.widgets.Shell;
|
32 |
|
|
|
33 |
|
|
import eu.etaxonomy.cdm.model.common.AnnotatableEntity;
|
34 |
|
|
import eu.etaxonomy.cdm.model.common.Marker;
|
35 |
|
|
import eu.etaxonomy.cdm.model.common.MarkerType;
|
36 |
|
|
import eu.etaxonomy.taxeditor.store.VocabularyStore;
|
37 |
|
|
|
38 |
|
|
/**
|
39 |
|
|
* @author p.ciardelli
|
40 |
|
|
*
|
41 |
|
|
*/
|
42 |
|
|
public class MarkersDialog extends TitleAreaDialog {
|
43 |
|
|
|
44 |
|
|
private AnnotatableEntity entity;
|
45 |
|
|
private Button addButton;
|
46 |
|
|
private CheckboxTableViewer tableViewer;
|
47 |
|
|
private Button removeButton;
|
48 |
|
|
private Set<Marker> markers = new HashSet<Marker>();
|
49 |
|
|
private Button okButton;
|
50 |
|
|
|
51 |
|
|
/**
|
52 |
|
|
* @param parentShell
|
53 |
|
|
* @param entity
|
54 |
|
|
*/
|
55 |
|
|
public MarkersDialog(Shell parentShell, AnnotatableEntity entity) {
|
56 |
|
|
super(parentShell);
|
57 |
|
|
|
58 |
c078428a
|
p.ciardelli
|
this.entity = entity;
|
59 |
|
|
this.markers.addAll(entity.getMarkers());
|
60 |
229cd35b
|
p.ciardelli
|
|
61 |
|
|
}
|
62 |
|
|
|
63 |
|
|
/* (non-Javadoc)
|
64 |
|
|
* @see org.eclipse.jface.window.Window#open()
|
65 |
|
|
*/
|
66 |
|
|
@Override
|
67 |
|
|
public int open() {
|
68 |
|
|
return super.open();
|
69 |
|
|
}
|
70 |
|
|
|
71 |
|
|
/* (non-Javadoc)
|
72 |
|
|
* @see org.eclipse.jface.dialogs.TitleAreaDialog#createContents(org.eclipse.swt.widgets.Composite)
|
73 |
|
|
*/
|
74 |
|
|
@Override
|
75 |
|
|
protected Control createContents(Composite parent) {
|
76 |
|
|
Control contents = super.createContents(parent);
|
77 |
|
|
|
78 |
|
|
setTitle("Edit markers");
|
79 |
|
|
setMessage("Add or remove markers, and set their flags to true or false");
|
80 |
|
|
|
81 |
|
|
this.getShell().setText("Markers");
|
82 |
|
|
|
83 |
|
|
return contents;
|
84 |
|
|
}
|
85 |
|
|
|
86 |
|
|
/* (non-Javadoc)
|
87 |
|
|
* @see org.eclipse.jface.dialogs.TitleAreaDialog#createDialogArea(org.eclipse.swt.widgets.Composite)
|
88 |
|
|
*/
|
89 |
|
|
@Override
|
90 |
|
|
protected Control createDialogArea(Composite parent) {
|
91 |
|
|
Composite container = (Composite) super.createDialogArea(parent);
|
92 |
|
|
|
93 |
|
|
// create composite
|
94 |
|
|
Composite composite = new Composite(container, SWT.NULL);
|
95 |
|
|
composite.setLayoutData(new GridData(GridData.FILL_BOTH));
|
96 |
|
|
|
97 |
|
|
final GridLayout gridLayout = new GridLayout();
|
98 |
|
|
gridLayout.numColumns = 2;
|
99 |
|
|
composite.setLayout(gridLayout);
|
100 |
|
|
|
101 |
|
|
tableViewer = CheckboxTableViewer.newCheckList(composite, SWT.NULL);
|
102 |
|
|
GridData tableLayoutData = new GridData(SWT.FILL, SWT.FILL, true, false);
|
103 |
|
|
tableLayoutData.heightHint = 300;
|
104 |
|
|
tableViewer.getTable().setLayoutData(tableLayoutData);
|
105 |
|
|
|
106 |
|
|
tableViewer.setContentProvider(new MarkersContentProvider());
|
107 |
|
|
tableViewer.setLabelProvider(new MarkersLabelProvider());
|
108 |
|
|
|
109 |
|
|
tableViewer.addSelectionChangedListener(new ISelectionChangedListener () {
|
110 |
|
|
public void selectionChanged(SelectionChangedEvent event) {
|
111 |
|
|
setRemovedEnabled(true);
|
112 |
|
|
}
|
113 |
|
|
});
|
114 |
|
|
|
115 |
|
|
Composite buttonContainer = new Composite(composite, SWT.NULL);
|
116 |
|
|
GridData buttonContainerLayoutData = new GridData();
|
117 |
|
|
buttonContainerLayoutData.verticalAlignment = SWT.TOP;
|
118 |
|
|
buttonContainer.setLayoutData(buttonContainerLayoutData);
|
119 |
|
|
buttonContainer.setLayout(new GridLayout());
|
120 |
|
|
|
121 |
|
|
addButton = new Button(buttonContainer, SWT.PUSH);
|
122 |
|
|
addButton.setText("Add ...");
|
123 |
|
|
addButton.addMouseListener(new MouseAdapter(){
|
124 |
|
|
/* (non-Javadoc)
|
125 |
|
|
* @see org.eclipse.swt.events.MouseAdapter#mouseDown(org.eclipse.swt.events.MouseEvent)
|
126 |
|
|
*/
|
127 |
|
|
@Override
|
128 |
|
|
public void mouseUp(MouseEvent e) {
|
129 |
|
|
createMarker();
|
130 |
|
|
}
|
131 |
|
|
});
|
132 |
|
|
|
133 |
|
|
removeButton = new Button(buttonContainer, SWT.PUSH);
|
134 |
|
|
removeButton.setText("Remove");
|
135 |
|
|
removeButton.addMouseListener(new MouseAdapter() {
|
136 |
|
|
/* (non-Javadoc)
|
137 |
|
|
* @see org.eclipse.swt.events.MouseAdapter#mouseUp(org.eclipse.swt.events.MouseEvent)
|
138 |
|
|
*/
|
139 |
|
|
@Override
|
140 |
|
|
public void mouseUp(MouseEvent e) {
|
141 |
|
|
removeSelectedMarker();
|
142 |
|
|
}
|
143 |
|
|
});
|
144 |
|
|
setRemovedEnabled(false);
|
145 |
|
|
|
146 |
|
|
populateMarkers();
|
147 |
|
|
|
148 |
|
|
return container;
|
149 |
|
|
}
|
150 |
|
|
|
151 |
|
|
|
152 |
|
|
/**
|
153 |
|
|
*
|
154 |
|
|
*/
|
155 |
|
|
protected void createMarker() {
|
156 |
|
|
Set<MarkerType> markerTypes = VocabularyStore.getMarkerTypes();
|
157 |
|
|
|
158 |
|
|
// Remove marker types that are already spoken for
|
159 |
|
|
for (Marker marker : markers) {
|
160 |
|
|
markerTypes.remove(marker.getMarkerType());
|
161 |
|
|
}
|
162 |
|
|
|
163 |
|
|
SelectMarkerTypeDialog dialog = new SelectMarkerTypeDialog(getShell(), markerTypes);
|
164 |
|
|
if (dialog.open() == Window.OK) {
|
165 |
|
|
Set<MarkerType> selectedMarkerTypes = dialog.getMarkerTypes();
|
166 |
|
|
if (selectedMarkerTypes != null) {
|
167 |
|
|
for (MarkerType markerType : selectedMarkerTypes) {
|
168 |
|
|
markers.add(Marker.NewInstance(markerType, false));
|
169 |
|
|
}
|
170 |
|
|
tableViewer.refresh();
|
171 |
|
|
}
|
172 |
|
|
}
|
173 |
|
|
}
|
174 |
|
|
|
175 |
|
|
/**
|
176 |
|
|
*
|
177 |
|
|
*/
|
178 |
|
|
protected void removeSelectedMarker() {
|
179 |
|
|
Marker marker = getSelectedMarker();
|
180 |
|
|
if (marker != null) {
|
181 |
|
|
markers.remove(marker);
|
182 |
|
|
tableViewer.refresh();
|
183 |
|
|
}
|
184 |
|
|
}
|
185 |
|
|
|
186 |
|
|
/**
|
187 |
|
|
* @return
|
188 |
|
|
*
|
189 |
|
|
*/
|
190 |
|
|
private Marker getSelectedMarker() {
|
191 |
|
|
return (Marker) ((IStructuredSelection) tableViewer.getSelection()).getFirstElement();
|
192 |
|
|
}
|
193 |
|
|
|
194 |
|
|
/**
|
195 |
|
|
*
|
196 |
|
|
*/
|
197 |
|
|
protected void setRemovedEnabled(boolean enabled) {
|
198 |
|
|
removeButton.setEnabled(enabled);
|
199 |
|
|
}
|
200 |
|
|
|
201 |
|
|
/**
|
202 |
|
|
*
|
203 |
|
|
*/
|
204 |
|
|
protected void toggleSelection() {
|
205 |
|
|
// TODO Auto-generated method stub
|
206 |
|
|
|
207 |
|
|
}
|
208 |
|
|
|
209 |
|
|
/**
|
210 |
|
|
*
|
211 |
|
|
*/
|
212 |
|
|
private void populateMarkers() {
|
213 |
|
|
List<Marker> markerFlags = new ArrayList<Marker>();
|
214 |
|
|
for (Marker marker : markers) {
|
215 |
|
|
if (marker.getFlag()) {
|
216 |
|
|
markerFlags.add(marker);
|
217 |
|
|
}
|
218 |
|
|
}
|
219 |
|
|
tableViewer.setInput(markers);
|
220 |
|
|
tableViewer.setCheckedElements(markerFlags.toArray());
|
221 |
|
|
}
|
222 |
|
|
|
223 |
|
|
|
224 |
|
|
private class MarkersContentProvider implements IStructuredContentProvider{
|
225 |
|
|
|
226 |
|
|
public Object[] getElements(Object inputElement) {
|
227 |
|
|
return ((Set<?>) inputElement).toArray();
|
228 |
|
|
}
|
229 |
|
|
|
230 |
|
|
public void dispose() {}
|
231 |
|
|
|
232 |
|
|
public void inputChanged(Viewer viewer, Object oldInput,
|
233 |
|
|
Object newInput) {}
|
234 |
|
|
|
235 |
|
|
};
|
236 |
|
|
|
237 |
|
|
class MarkersLabelProvider extends LabelProvider implements ITableLabelProvider{
|
238 |
|
|
|
239 |
|
|
/* (non-Javadoc)
|
240 |
|
|
* @see org.eclipse.jface.viewers.ITableLabelProvider#getColumnImage(java.lang.Object, int)
|
241 |
|
|
*/
|
242 |
|
|
public Image getColumnImage(Object element, int columnIndex) {
|
243 |
|
|
return null;
|
244 |
|
|
}
|
245 |
|
|
|
246 |
|
|
/* (non-Javadoc)
|
247 |
|
|
* @see org.eclipse.jface.viewers.ITableLabelProvider#getColumnText(java.lang.Object, int)
|
248 |
|
|
*/
|
249 |
|
|
public String getColumnText(Object element, int columnIndex) {
|
250 |
|
|
return ((Marker)element).getMarkerType().getLabel();
|
251 |
|
|
}
|
252 |
|
|
}
|
253 |
|
|
|
254 |
|
|
protected void createButtonsForButtonBar(Composite parent) {
|
255 |
|
|
okButton = createButton(parent, IDialogConstants.OK_ID,
|
256 |
|
|
IDialogConstants.OK_LABEL, true);
|
257 |
|
|
createButton(parent, IDialogConstants.CANCEL_ID,
|
258 |
|
|
IDialogConstants.CANCEL_LABEL, false);
|
259 |
|
|
}
|
260 |
|
|
|
261 |
|
|
/* (non-Javadoc)
|
262 |
|
|
* @see org.eclipse.jface.dialogs.Dialog#okPressed()
|
263 |
|
|
*/
|
264 |
|
|
@Override
|
265 |
|
|
protected void okPressed() {
|
266 |
|
|
List<Marker> checkedMarkers = new ArrayList<Marker>();
|
267 |
|
|
for (Object element : tableViewer.getCheckedElements()){
|
268 |
|
|
checkedMarkers.add((Marker)element);
|
269 |
|
|
}
|
270 |
|
|
Set<Marker> savedMarkers = entity.getMarkers();
|
271 |
|
|
for (Marker marker : markers) {
|
272 |
|
|
|
273 |
|
|
// Set marker flags
|
274 |
|
|
marker.setFlag(checkedMarkers.contains(marker));
|
275 |
|
|
|
276 |
|
|
// Save any new markers
|
277 |
|
|
if (!savedMarkers.contains(marker)) {
|
278 |
|
|
entity.addMarker(marker);
|
279 |
|
|
}
|
280 |
|
|
}
|
281 |
|
|
// Remove deleted markers from entity
|
282 |
|
|
Set<Marker> removeMarkers = new HashSet<Marker>();
|
283 |
|
|
for (Marker marker : savedMarkers) {
|
284 |
|
|
if (!markers.contains(marker)) {
|
285 |
|
|
removeMarkers.add(marker);
|
286 |
|
|
}
|
287 |
|
|
}
|
288 |
|
|
for (Marker marker : removeMarkers) {
|
289 |
|
|
entity.removeMarker(marker);
|
290 |
|
|
}
|
291 |
|
|
super.okPressed();
|
292 |
|
|
}
|
293 |
|
|
}
|