Merge branch 'develop' into LibrAlign
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / ui / section / name / NonViralNameDetailSection.java
1 /**
2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
5 *
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.
8 */
9
10 package eu.etaxonomy.taxeditor.ui.section.name;
11
12 import org.eclipse.jface.action.Action;
13 import org.eclipse.jface.action.IAction;
14 import org.eclipse.jface.action.ToolBarManager;
15 import org.eclipse.jface.viewers.ISelectionProvider;
16 import org.eclipse.swt.SWT;
17 import org.eclipse.swt.widgets.Control;
18
19 import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
20 import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
21 import eu.etaxonomy.cdm.model.name.NonViralName;
22 import eu.etaxonomy.cdm.model.name.TaxonNameBase;
23 import eu.etaxonomy.cdm.model.taxon.TaxonBase;
24 import eu.etaxonomy.taxeditor.model.ImageResources;
25 import eu.etaxonomy.taxeditor.model.MessagingUtils;
26 import eu.etaxonomy.taxeditor.preference.IPreferenceKeys;
27 import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
28 import eu.etaxonomy.taxeditor.ui.dialog.selection.NameSelectionDialog;
29 import eu.etaxonomy.taxeditor.ui.element.CdmFormFactory;
30 import eu.etaxonomy.taxeditor.ui.element.CdmPropertyChangeEvent;
31 import eu.etaxonomy.taxeditor.ui.element.ICdmFormElement;
32 import eu.etaxonomy.taxeditor.ui.section.AbstractCdmDetailElement;
33 import eu.etaxonomy.taxeditor.ui.section.AbstractCdmDetailSection;
34 import eu.etaxonomy.taxeditor.ui.section.ITaxonBaseDetailSection;
35
36 /**
37 * @author n.hoffmann
38 * @created May 20, 2010
39 * @version 1.0
40 */
41 public class NonViralNameDetailSection extends AbstractCdmDetailSection<NonViralName>
42 implements ITaxonBaseDetailSection {
43
44 private TaxonBase taxonBase;
45 boolean nameChoosable = false;
46
47 /**
48 * <p>Constructor for NonViralNameDetailSection.</p>
49 *
50 * @param formFactory a {@link eu.etaxonomy.taxeditor.ui.element.CdmFormFactory} object.
51 * @param conversation a {@link eu.etaxonomy.cdm.api.conversation.ConversationHolder} object.
52 * @param parentElement a {@link eu.etaxonomy.taxeditor.ui.element.ICdmFormElement} object.
53 * @param selectionProvider a {@link org.eclipse.jface.viewers.ISelectionProvider} object.
54 * @param nameChoosable if <code>true</code> adds a button to choose the displayed name
55 * @param style a int.
56 */
57 public NonViralNameDetailSection(CdmFormFactory formFactory, ConversationHolder conversation,
58 ICdmFormElement parentElement, ISelectionProvider selectionProvider, boolean nameChoosable, int style) {
59 super(formFactory, conversation, parentElement, selectionProvider, style);
60 this.nameChoosable = nameChoosable;
61 }
62
63 @Override
64 protected Control createToolbar() {
65 ToolBarManager toolBarManager = new ToolBarManager(SWT.FLAT);
66
67 if(nameChoosable && !PreferencesUtil.getPreferenceStore().getBoolean(IPreferenceKeys.SHOW_SIMPLE_NAME_DETAILS_SECTION)){
68 //choose name
69 Action chooseNameAction = new Action("Choose Name", IAction.AS_PUSH_BUTTON){
70 @Override
71 public void run() {
72 TaxonNameBase taxonName = NameSelectionDialog.select(getShell(), getConversationHolder(), getEntity());
73 if(taxonName!=null){
74 if(taxonName.isInstanceOf(NonViralName.class)){
75 NonViralName nonViralName = HibernateProxyHelper.deproxy(taxonName, NonViralName.class);
76 taxonBase.setName(nonViralName);
77 // taxonBase.getTitleCache();
78 setEntity(nonViralName);
79 firePropertyChangeEvent(NonViralNameDetailSection.this);
80 }
81 else{
82 MessagingUtils.warningDialog("Invalid name", this, "The selected name can not be used for this taxon.");
83 }
84 }
85 }
86 };
87 chooseNameAction.setToolTipText("Choose name for this taxon");
88 chooseNameAction.setImageDescriptor(ImageResources.getImageDescriptor(ImageResources.BROWSE_ICON));
89
90 toolBarManager.add(chooseNameAction);
91 }
92
93 //clone
94 if(getEntity() != null && checkForMultipleNameUsages(getEntity())){
95
96 Action cloneAction = new Action("Clone", IAction.AS_PUSH_BUTTON){
97 @Override
98 public void run() {
99 boolean confirm = MessagingUtils.confirmDialog("Confirm cloning", "Do you really want to clone the name?");
100
101 if(confirm){
102 NonViralName clonedName;
103 clonedName = (NonViralName) getEntity().clone();
104 setEntity(clonedName);
105 taxonBase.setName(clonedName);
106 taxonBase.generateTitle();
107 firePropertyChangeEvent(new CdmPropertyChangeEvent(NonViralNameDetailSection.this, null));
108 }
109
110 };
111 };
112
113 cloneAction.setToolTipText("Clone the name if you do not want to edit the shared instance");
114
115 toolBarManager.add(cloneAction);
116
117 }
118 return toolBarManager.createControl(this);
119 }
120
121
122 private boolean checkForMultipleNameUsages(NonViralName nonViralName) {
123 return nonViralName.getTaxonBases().size() != 1;
124 }
125
126 /** {@inheritDoc} */
127 @Override
128 public String getHeading() {
129 return "Name";
130 }
131
132 /** {@inheritDoc} */
133 @Override
134 public void setTaxonBase(TaxonBase taxon) {
135 taxonBase = taxon;
136 NonViralName name = (NonViralName) HibernateProxyHelper.deproxy(taxon.getName());
137 setEntity(name);
138 }
139
140 @Override
141 public TaxonBase getTaxonBase() {
142 return taxonBase;
143 }
144
145 @Override
146 protected AbstractCdmDetailElement<NonViralName> createCdmDetailElement(AbstractCdmDetailSection<NonViralName> parentElement, int style) {
147 return formFactory.createNonViralNameDetailElement(parentElement);
148 }
149 }