Project

General

Profile

Download (5.41 KB) Statistics
| Branch: | Tag: | Revision:
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.ui.dialog.selection.NameSelectionDialog;
27
import eu.etaxonomy.taxeditor.ui.element.CdmFormFactory;
28
import eu.etaxonomy.taxeditor.ui.element.CdmPropertyChangeEvent;
29
import eu.etaxonomy.taxeditor.ui.element.ICdmFormElement;
30
import eu.etaxonomy.taxeditor.ui.section.AbstractCdmDetailElement;
31
import eu.etaxonomy.taxeditor.ui.section.AbstractCdmDetailSection;
32
import eu.etaxonomy.taxeditor.ui.section.ITaxonBaseDetailSection;
33

    
34
/**
35
 * @author n.hoffmann
36
 * @created May 20, 2010
37
 * @version 1.0
38
 */
39
public class NonViralNameDetailSection extends AbstractCdmDetailSection<NonViralName>
40
		implements ITaxonBaseDetailSection {
41

    
42
	private TaxonBase taxonBase;
43
	boolean nameChoosable = false;
44

    
45
	/**
46
	 * <p>Constructor for NonViralNameDetailSection.</p>
47
	 *
48
	 * @param formFactory a {@link eu.etaxonomy.taxeditor.ui.element.CdmFormFactory} object.
49
	 * @param conversation a {@link eu.etaxonomy.cdm.api.conversation.ConversationHolder} object.
50
	 * @param parentElement a {@link eu.etaxonomy.taxeditor.ui.element.ICdmFormElement} object.
51
	 * @param selectionProvider a {@link org.eclipse.jface.viewers.ISelectionProvider} object.
52
	 * @param nameChoosable if <code>true</code> adds a button to choose the displayed name
53
	 * @param style a int.
54
	 */
55
	public NonViralNameDetailSection(CdmFormFactory formFactory, ConversationHolder conversation,
56
			ICdmFormElement parentElement, ISelectionProvider selectionProvider, boolean nameChoosable, int style) {
57
		super(formFactory, conversation, parentElement, selectionProvider, style);
58
		this.nameChoosable = nameChoosable;
59
	}
60

    
61
	@Override
62
	protected Control createToolbar() {
63
	        ToolBarManager toolBarManager = new ToolBarManager(SWT.FLAT);
64

    
65
	        if(nameChoosable ){
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(), getEntity());
71
	                    if(taxonName!=null){
72
	                        if(taxonName.isInstanceOf(NonViralName.class)){
73
	                            NonViralName nonViralName = HibernateProxyHelper.deproxy(taxonName, NonViralName.class);
74
	                            taxonBase.setName(nonViralName);
75
	                            //				        taxonBase.getTitleCache();
76
	                            setEntity(nonViralName);
77
	                            firePropertyChangeEvent(NonViralNameDetailSection.this);
78
	                        }
79
	                        else{
80
	                            MessagingUtils.warningDialog("Invalid name", this, "The selected name can not be used for this taxon.");
81
	                        }
82
	                    }
83
	                }
84
	            };
85
	            chooseNameAction.setToolTipText("Choose name for this taxon");
86
	            chooseNameAction.setImageDescriptor(ImageResources.getImageDescriptor(ImageResources.BROWSE_ICON));
87

    
88
	            toolBarManager.add(chooseNameAction);
89
	    }
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
}
(16-16/21)