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