ref #6190 removing svn property place holder in first line of code - java files
[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.api.service.ITaxonNodeService;
21 import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
22 import eu.etaxonomy.cdm.model.name.NonViralName;
23 import eu.etaxonomy.cdm.model.name.TaxonNameBase;
24 import eu.etaxonomy.cdm.model.taxon.TaxonBase;
25 import eu.etaxonomy.cdm.model.taxon.TaxonNode;
26 import eu.etaxonomy.taxeditor.model.ImageResources;
27 import eu.etaxonomy.taxeditor.model.MessagingUtils;
28 import eu.etaxonomy.taxeditor.preference.IPreferenceKeys;
29 import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
30 import eu.etaxonomy.taxeditor.store.CdmStore;
31 import eu.etaxonomy.taxeditor.ui.dialog.selection.NameSelectionDialog;
32 import eu.etaxonomy.taxeditor.ui.element.CdmFormFactory;
33 import eu.etaxonomy.taxeditor.ui.element.CdmPropertyChangeEvent;
34 import eu.etaxonomy.taxeditor.ui.element.ICdmFormElement;
35 import eu.etaxonomy.taxeditor.ui.section.AbstractCdmDetailElement;
36 import eu.etaxonomy.taxeditor.ui.section.AbstractCdmDetailSection;
37 import eu.etaxonomy.taxeditor.ui.section.ITaxonBaseDetailSection;
38
39 /**
40 * @author n.hoffmann
41 * @created May 20, 2010
42 * @version 1.0
43 */
44 public class NonViralNameDetailSection extends AbstractCdmDetailSection<NonViralName>
45 implements ITaxonBaseDetailSection {
46
47 private TaxonBase taxonBase;
48 boolean nameChoosable = false;
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 nameChoosable if <code>true</code> adds a button to choose the displayed name
58 * @param style a int.
59 */
60 public NonViralNameDetailSection(CdmFormFactory formFactory, ConversationHolder conversation,
61 ICdmFormElement parentElement, ISelectionProvider selectionProvider, boolean nameChoosable, int style) {
62 super(formFactory, conversation, parentElement, selectionProvider, style);
63 this.nameChoosable = nameChoosable;
64 }
65
66 @Override
67 protected Control createToolbar() {
68 ToolBarManager toolBarManager = new ToolBarManager(SWT.FLAT);
69
70 if(nameChoosable && !PreferencesUtil.getPreferenceStore().getBoolean(IPreferenceKeys.SHOW_SIMPLE_NAME_DETAILS_SECTION)){
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(), getEntity());
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
97 //clone
98 if(getEntity() != null && checkForMultipleNameUsages(getEntity())){
99
100 Action cloneAction = new Action("Clone", IAction.AS_PUSH_BUTTON){
101 @Override
102 public void run() {
103 boolean confirm = MessagingUtils.confirmDialog("Confirm cloning", "Do you really want to clone the name?");
104
105 if(confirm){
106 NonViralName clonedName;
107 clonedName = (NonViralName) getEntity().clone();
108 setEntity(clonedName);
109 taxonBase.setName(clonedName);
110 taxonBase.generateTitle();
111 firePropertyChangeEvent(new CdmPropertyChangeEvent(NonViralNameDetailSection.this, null));
112 }
113
114 };
115 };
116
117 cloneAction.setToolTipText("Clone the name if you do not want to edit the shared instance");
118
119 toolBarManager.add(cloneAction);
120
121 }
122 return toolBarManager.createControl(this);
123 }
124
125
126 private boolean checkForMultipleNameUsages(NonViralName nonViralName) {
127 return nonViralName.getTaxonBases().size() != 1;
128 }
129
130 /** {@inheritDoc} */
131 @Override
132 public String getHeading() {
133 return "Name";
134 }
135
136 /** {@inheritDoc} */
137 @Override
138 public void setTaxonBase(TaxonBase taxon) {
139 taxonBase = taxon;
140 NonViralName name = (NonViralName) HibernateProxyHelper.deproxy(taxon.getName());
141 setEntity(name);
142 }
143
144 @Override
145 public TaxonBase getTaxonBase() {
146 return taxonBase;
147 }
148
149 @Override
150 protected AbstractCdmDetailElement<NonViralName> createCdmDetailElement(AbstractCdmDetailSection<NonViralName> parentElement, int style) {
151 return formFactory.createNonViralNameDetailElement(parentElement);
152 }
153 }