Merge branch 'move-to-luna' into remoting-4.0
[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 * <p>NonViralNameDetailSection class.</p>
40 *
41 * @author n.hoffmann
42 * @created May 20, 2010
43 * @version 1.0
44 */
45 public class NonViralNameDetailSection extends AbstractCdmDetailSection<NonViralName>
46 implements ITaxonBaseDetailSection {
47
48 private TaxonBase taxonBase;
49
50 /**
51 * <p>Constructor for NonViralNameDetailSection.</p>
52 *
53 * @param formFactory a {@link eu.etaxonomy.taxeditor.ui.element.CdmFormFactory} object.
54 * @param conversation a {@link eu.etaxonomy.cdm.api.conversation.ConversationHolder} object.
55 * @param parentElement a {@link eu.etaxonomy.taxeditor.ui.element.ICdmFormElement} object.
56 * @param selectionProvider a {@link org.eclipse.jface.viewers.ISelectionProvider} object.
57 * @param style a int.
58 */
59 public NonViralNameDetailSection(CdmFormFactory formFactory, ConversationHolder conversation,
60 ICdmFormElement parentElement, ISelectionProvider selectionProvider, int style) {
61 super(formFactory, conversation, parentElement, selectionProvider, style);
62 }
63
64 /* (non-Javadoc)
65 * @see eu.etaxonomy.taxeditor.section.AbstractCdmDetailSection#createToolbar()
66 */
67 @Override
68 protected Control createToolbar() {
69 ToolBarManager toolBarManager = new ToolBarManager(SWT.FLAT);
70
71 //choose name
72 Action chooseNameAction = new Action("Choose Name", IAction.AS_PUSH_BUTTON){
73 @Override
74 public void run() {
75 TaxonNameBase taxonName = NameSelectionDialog.select(getShell(), getConversationHolder(), null);
76 if(taxonName!=null){
77 if(taxonName.isInstanceOf(NonViralName.class)){
78 CdmStore.getService(ITaxonNodeService.class).list(TaxonNode.class, null, null, null, null);
79 NonViralName nonViralName = HibernateProxyHelper.deproxy(taxonName, NonViralName.class);
80 taxonBase.setName(nonViralName);
81 // taxonBase.getTitleCache();
82 setEntity(nonViralName);
83 firePropertyChangeEvent(NonViralNameDetailSection.this);
84 }
85 else{
86 MessagingUtils.warningDialog("Invalid name", this, "The selected name can not be used for this taxon.");
87 }
88 }
89 }
90 };
91 chooseNameAction.setToolTipText("Choose name for this taxon");
92 chooseNameAction.setImageDescriptor(ImageResources.getImageDescriptor(ImageResources.BROWSE_ICON));
93
94 toolBarManager.add(chooseNameAction);
95
96 //clone
97 if(getEntity() != null && checkForMultipleNameUsages(getEntity())){
98
99 Action cloneAction = new Action("Clone", IAction.AS_PUSH_BUTTON){
100 @Override
101 public void run() {
102 boolean confirm = MessagingUtils.confirmDialog("Confirm cloning", "Do you really want to clone the name?");
103
104 if(confirm){
105 NonViralName clonedName;
106 clonedName = (NonViralName) getEntity().clone();
107 setEntity(clonedName);
108 taxonBase.setName(clonedName);
109 taxonBase.generateTitle();
110 firePropertyChangeEvent(new CdmPropertyChangeEvent(NonViralNameDetailSection.this, null));
111 }
112
113 };
114 };
115
116 cloneAction.setToolTipText("Clone the name if you do not want to edit the shared instance");
117
118 toolBarManager.add(cloneAction);
119
120 }
121 return toolBarManager.createControl(this);
122 }
123
124
125 /**
126 * @param nonViralName
127 *
128 */
129 private boolean checkForMultipleNameUsages(NonViralName nonViralName) {
130 return nonViralName.getTaxonBases().size() != 1;
131 }
132
133 /* (non-Javadoc)
134 * @see eu.etaxonomy.taxeditor.section.AbstractCdmDetailSection#getHeading()
135 */
136 /** {@inheritDoc} */
137 @Override
138 public String getHeading() {
139 return "Name";
140 }
141
142 /* (non-Javadoc)
143 * @see eu.etaxonomy.taxeditor.section.ITaxonDetailSection#setTaxon(eu.etaxonomy.cdm.model.taxon.TaxonBase)
144 */
145 /** {@inheritDoc} */
146 @Override
147 public void setTaxonBase(TaxonBase taxon) {
148 taxonBase = taxon;
149 NonViralName name = (NonViralName) HibernateProxyHelper.deproxy(taxon.getName());
150 setEntity(name);
151 }
152
153 /* (non-Javadoc)
154 * @see eu.etaxonomy.taxeditor.ui.section.AbstractCdmDetailSection#setSectionTitle()
155 */
156 @Override
157 protected void setSectionTitle() {
158 super.setSectionTitle();
159 String title = getText();
160 // we have to duplicate ampersands otherwise they are treated as
161 // mnenomic (see Label.setText() documentation)
162 // see also #4302
163 title = title.replace("&", "&&");
164 setText(title);
165 }
166
167
168 /* (non-Javadoc)
169 * @see eu.etaxonomy.taxeditor.section.ITaxonDetailSection#getTaxon(eu.etaxonomy.cdm.model.taxon.TaxonBase)
170 */
171 @Override
172 public TaxonBase getTaxonBase() {
173 return taxonBase;
174 }
175
176 /* (non-Javadoc)
177 * @see eu.etaxonomy.taxeditor.ui.section.AbstractCdmDetailSection#createCdmDetailElement(eu.etaxonomy.taxeditor.ui.section.AbstractCdmDetailSection, int)
178 */
179 @Override
180 protected AbstractCdmDetailElement<NonViralName> createCdmDetailElement(AbstractCdmDetailSection<NonViralName> parentElement, int style) {
181 return formFactory.createNonViralNameDetailElement(parentElement);
182 }
183 }