Project

General

Profile

Download (5.05 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.INonViralName;
22
import eu.etaxonomy.cdm.model.name.TaxonName;
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
 */
38
public class NonViralNameDetailSection extends AbstractCdmDetailSection<TaxonName>
39
		implements ITaxonBaseDetailSection {
40

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

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

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

    
64
	        if(nameChoosable ){
65
	            //choose name
66
	            Action chooseNameAction = new Action("Choose Name", IAction.AS_PUSH_BUTTON){
67
	                @Override
68
	                public void run() {
69
	                    TaxonName taxonName = NameSelectionDialog.select(getShell(), getConversationHolder(), getEntity());
70
	                    if(taxonName!=null){
71
                            TaxonName nonViralName = HibernateProxyHelper.deproxy(taxonName);
72
                            taxonBase.setName(nonViralName);
73
                            //				        taxonBase.getTitleCache();
74
                            setEntity(nonViralName);
75
                            firePropertyChangeEvent(NonViralNameDetailSection.this);
76
	                    }
77
	                }
78
	            };
79
	            chooseNameAction.setToolTipText("Choose name for this taxon");
80
	            chooseNameAction.setImageDescriptor(ImageResources.getImageDescriptor(ImageResources.BROWSE_ICON));
81

    
82
	            toolBarManager.add(chooseNameAction);
83
	    }
84

    
85
		//clone
86
		if(getEntity() != null && checkForMultipleNameUsages(getEntity())){
87

    
88
			Action cloneAction = new Action("Clone", IAction.AS_PUSH_BUTTON){
89
			    @Override
90
			    public void run() {
91
					boolean confirm = MessagingUtils.confirmDialog("Confirm cloning", "Do you really want to clone the name?");
92

    
93
					if(confirm){
94
						TaxonName clonedName;
95
						clonedName = (TaxonName) getEntity().clone();
96
						setEntity(clonedName);
97
						taxonBase.setName(clonedName);
98
						taxonBase.generateTitle();
99
						firePropertyChangeEvent(new CdmPropertyChangeEvent(NonViralNameDetailSection.this, null));
100
					}
101

    
102
				};
103
			};
104

    
105
			cloneAction.setToolTipText("Clone the name if you do not want to edit the shared instance");
106

    
107
			toolBarManager.add(cloneAction);
108

    
109
		}
110
		return toolBarManager.createControl(this);
111
	}
112

    
113

    
114
	private boolean checkForMultipleNameUsages(INonViralName nonViralName) {
115
		return nonViralName.getTaxonBases().size() != 1;
116
	}
117

    
118
	/** {@inheritDoc} */
119
	@Override
120
	public String getHeading() {
121
		return "Name";
122
	}
123

    
124
	/** {@inheritDoc} */
125
	@Override
126
    public void setTaxonBase(TaxonBase taxon) {
127
		taxonBase = taxon;
128
		TaxonName name = HibernateProxyHelper.deproxy(taxon.getName());
129
		setEntity(name);
130
	}
131

    
132
	@Override
133
	public TaxonBase getTaxonBase() {
134
		return taxonBase;
135
	}
136

    
137
	@Override
138
	protected AbstractCdmDetailElement<TaxonName> createCdmDetailElement(
139
			AbstractCdmDetailSection<TaxonName> parentElement, int style) {
140
	    return formFactory.createNonViralNameDetailElement(parentElement);
141
	}
142
}
(16-16/21)