2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
6 * The contents of this file are subject to the Mozilla Public License Version 1.1
7 * See LICENSE.TXT at the top of this package for the full license terms.
10 package eu
.etaxonomy
.taxeditor
.propertysheet
.namerelationswizard
;
12 import org
.apache
.log4j
.Logger
;
13 import org
.eclipse
.core
.databinding
.beans
.BeansObservables
;
14 import org
.eclipse
.core
.databinding
.observable
.list
.WritableList
;
15 import org
.eclipse
.core
.databinding
.observable
.map
.IObservableMap
;
16 import org
.eclipse
.jface
.databinding
.viewers
.ObservableListContentProvider
;
17 import org
.eclipse
.jface
.databinding
.viewers
.ObservableMapLabelProvider
;
18 import org
.eclipse
.jface
.viewers
.DoubleClickEvent
;
19 import org
.eclipse
.jface
.viewers
.IDoubleClickListener
;
20 import org
.eclipse
.jface
.viewers
.StructuredSelection
;
21 import org
.eclipse
.jface
.viewers
.TableViewer
;
22 import org
.eclipse
.jface
.wizard
.WizardPage
;
23 import org
.eclipse
.swt
.SWT
;
24 import org
.eclipse
.swt
.events
.SelectionAdapter
;
25 import org
.eclipse
.swt
.events
.SelectionEvent
;
26 import org
.eclipse
.swt
.graphics
.Image
;
27 import org
.eclipse
.swt
.layout
.GridData
;
28 import org
.eclipse
.swt
.layout
.GridLayout
;
29 import org
.eclipse
.swt
.widgets
.Button
;
30 import org
.eclipse
.swt
.widgets
.Composite
;
31 import org
.eclipse
.swt
.widgets
.Table
;
32 import org
.eclipse
.swt
.widgets
.TableItem
;
34 import eu
.etaxonomy
.cdm
.model
.name
.NameRelationship
;
35 import eu
.etaxonomy
.cdm
.model
.name
.NameRelationshipType
;
36 import eu
.etaxonomy
.cdm
.model
.name
.TaxonNameBase
;
37 import eu
.etaxonomy
.cdm
.model
.taxon
.Taxon
;
38 import eu
.etaxonomy
.taxeditor
.ITaxEditorConstants
;
39 import eu
.etaxonomy
.taxeditor
.TaxEditorPlugin
;
40 import eu
.etaxonomy
.taxeditor
.actions
.cdm
.DeleteNameRelationAction
;
41 import eu
.etaxonomy
.taxeditor
.actions
.ui
.OpenNameRelationWizardAction
;
42 import eu
.etaxonomy
.taxeditor
.model
.CdmUtil
;
49 public class ListNameRelationsWizardPage
extends WizardPage
{
50 private static final Logger logger
= Logger
51 .getLogger(ListNameRelationsWizardPage
.class);
53 private Table nameRelationsTable
;
54 private WritableList nameRelationsList
= new WritableList();
55 private TaxonNameBase name
;
57 private Button btnRemove
;
58 private Button btnEdit
;
60 private NameRelationship selectedRelation
;
62 public ListNameRelationsWizardPage(TaxonNameBase name
) {
67 this.nameRelationsList
.addAll(name
.getNameRelations());
69 setTitle("Nomenclatural relations for \""
70 + CdmUtil
.getDisplayName(name
) + "\".");
71 setDescription("Select a relation and click \"Edit ...\" or \"Remove\", or click \"Add ...\" to create a new relation.");
75 public void createControl(Composite parent
) {
76 Composite container
= new Composite(parent
, SWT
.NULL
);
77 final GridLayout gridLayout
= new GridLayout();
78 gridLayout
.numColumns
= 3;
79 container
.setLayout(gridLayout
);
81 setControl(container
);
83 btnEdit
= new Button(container
, SWT
.NONE
);
84 btnEdit
.setText("Edit ...");
86 btnRemove
= new Button(container
, SWT
.NONE
);
87 btnRemove
.setText("Remove");
88 btnRemove
.addSelectionListener(new SelectionAdapter() {
89 public void widgetSelected(SelectionEvent e
) {
90 if (getSelectedRelation() != null) {
91 new DeleteNameRelationAction(name
, getSelectedRelation()).run();
92 nameRelationsList
.remove(getSelectedRelation());
93 setSelectedRelation(null);
98 setEnableExistingRelationButtons(false);
100 final Button btnAdd
= new Button(container
, SWT
.NONE
);
101 final GridData gd_btnAdd
= new GridData(SWT
.RIGHT
, SWT
.CENTER
, true,
103 btnAdd
.setLayoutData(gd_btnAdd
);
104 btnAdd
.setText("Add ...");
105 btnAdd
.addSelectionListener(new SelectionAdapter() {
106 public void widgetSelected(SelectionEvent e
) {
107 createRelationWizard(null);
111 final TableViewer tableViewer
= new TableViewer(container
, SWT
.BORDER
| SWT
.SINGLE
);
113 nameRelationsTable
= tableViewer
.getTable();
114 nameRelationsTable
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true,
116 nameRelationsTable
.addSelectionListener(new SelectionAdapter() {
117 public void widgetSelected(SelectionEvent e
) {
118 setEnableExistingRelationButtons(true);
119 TableItem
[] selectedItem
= nameRelationsTable
.getSelection();
120 if (e
.item
.getData() instanceof NameRelationship
) {
121 setSelectedRelation((NameRelationship
) e
.item
.getData());
126 ObservableListContentProvider providerList
= new ObservableListContentProvider();
127 tableViewer
.setContentProvider(providerList
);
129 IObservableMap
[] providerMaps
= BeansObservables
.observeMaps(
130 providerList
.getKnownElements(), NameRelationship
.class,
131 new String
[] { "fromName", "toName", "type" });
132 tableViewer
.setLabelProvider(new ObservableMapLabelProvider(
134 public String
getColumnText(Object element
, int columnIndex
) {
135 if (element
instanceof NameRelationship
) {
136 NameRelationship nameRelationship
= (NameRelationship
) element
;
137 return getRelationshipString(nameRelationship
);
142 public Image
getColumnImage(Object element
, int columnIndex
) {
143 if (element
instanceof NameRelationship
) {
144 NameRelationship nameRelationship
= (NameRelationship
) element
;
145 return getRelationshipImage(nameRelationship
);
150 // TODO try name.getNameRelations()
151 tableViewer
.setInput(nameRelationsList
);
152 tableViewer
.addDoubleClickListener(new IDoubleClickListener() {
154 public void doubleClick(DoubleClickEvent event
) {
155 if (((StructuredSelection
) event
.getSelection())
156 .getFirstElement() instanceof NameRelationship
) {
157 NameRelationship relation
= (NameRelationship
) ((StructuredSelection
) event
158 .getSelection()).getFirstElement();
159 createRelationWizard(relation
);
165 private void setSelectedRelation(NameRelationship selectedRelation
) {
166 this.selectedRelation
= selectedRelation
;
169 private NameRelationship
getSelectedRelation() {
170 return selectedRelation
;
173 private void createRelationWizard(NameRelationship relation
) {
174 if (relation
== null) {
175 new OpenNameRelationWizardAction(name
, nameRelationsList
).run();
177 // new OpenNameRelationWizardAction(name, relation).run();
181 protected void setEnableExistingRelationButtons(boolean enabled
) {
182 btnRemove
.setEnabled(enabled
);
183 btnEdit
.setEnabled(false);
186 private Image
getRelationshipImage(NameRelationship nameRelationship
) {
187 if (nameRelationship
.getType().equals(NameRelationshipType
.BASIONYM())) {
188 return TaxEditorPlugin
.getDefault().getImage(
189 ITaxEditorConstants
.BASIONYM_ICON
);
191 if (nameRelationship
.getType().equals(
192 NameRelationshipType
.ORTHOGRAPHIC_VARIANT())) {
193 return TaxEditorPlugin
.getDefault().getImage(
194 ITaxEditorConstants
.ORTHOGRAPHIC_VARIANT_ICON
);
196 // return TaxEditorPlugin.getDefault().getImage(
197 // ITaxEditorConstants.EDIT_ICON);
201 private String
getRelationshipString(NameRelationship nameRelationship
) {
202 String fromName
= null;
204 String toName
= null;
205 if (nameRelationship
.getFromName() == null) {
208 fromName
= CdmUtil
.getDisplayName(nameRelationship
.getFromName());
210 if (nameRelationship
.getType() == null) {
213 type
= nameRelationship
.getType().getLabel();
215 if (nameRelationship
.getToName() == null) {
218 toName
= CdmUtil
.getDisplayName(nameRelationship
.getToName());
220 return "\"" + fromName
+ "\" is \"" + type
+ "\" of \"" + toName
+ "\"";