Project

General

Profile

Download (5.32 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.event.EventUtility;
25
import eu.etaxonomy.taxeditor.event.WorkbenchEventConstants;
26
import eu.etaxonomy.taxeditor.model.ImageResources;
27
import eu.etaxonomy.taxeditor.model.MessagingUtils;
28
import eu.etaxonomy.taxeditor.ui.dialog.selection.NameSelectionDialog;
29
import eu.etaxonomy.taxeditor.ui.element.CdmFormFactory;
30
import eu.etaxonomy.taxeditor.ui.element.CdmPropertyChangeEvent;
31
import eu.etaxonomy.taxeditor.ui.element.ICdmFormElement;
32
import eu.etaxonomy.taxeditor.ui.section.AbstractCdmDetailElement;
33
import eu.etaxonomy.taxeditor.ui.section.AbstractCdmDetailSection;
34
import eu.etaxonomy.taxeditor.ui.section.ITaxonBaseDetailSection;
35

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

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

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

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

    
66
	        if(nameChoosable ){
67
	            //choose name
68
	            Action chooseNameAction = new Action("Choose Name", IAction.AS_PUSH_BUTTON){
69
	                @Override
70
	                public void run() {
71
	                    TaxonName taxonName = NameSelectionDialog.select(getShell(), //getConversationHolder(),
72
	                            getEntity());
73
	                    if(taxonName!=null){
74
                            TaxonName nonViralName = HibernateProxyHelper.deproxy(taxonName);
75
                            taxonBase.setName(nonViralName);
76
                            setEntity(nonViralName);
77

    
78
                            firePropertyChangeEvent(NonViralNameDetailSection.this);
79
                            EventUtility.postEvent(WorkbenchEventConstants.REFRESH_NAVIGATOR, true);
80
                            EventUtility.postEvent(WorkbenchEventConstants.REFRESH_TAXON_DETAILS, true);
81
	                    }
82
	                }
83
	            };
84
	            chooseNameAction.setToolTipText("Choose name for this taxon");
85
	            chooseNameAction.setImageDescriptor(ImageResources.getImageDescriptor(ImageResources.BROWSE_ICON));
86

    
87
	            toolBarManager.add(chooseNameAction);
88
	    }
89

    
90
		//clone
91
		if(getEntity() != null && checkForMultipleNameUsages(getEntity())){
92

    
93
			Action cloneAction = new Action("Clone", IAction.AS_PUSH_BUTTON){
94
			    @Override
95
			    public void run() {
96
					boolean confirm = MessagingUtils.confirmDialog("Confirm cloning", "Do you really want to clone the name?");
97

    
98
					if(confirm){
99
						TaxonName clonedName;
100
						clonedName = (TaxonName) getEntity().clone();
101
						setEntity(clonedName);
102
						taxonBase.setName(clonedName);
103
						taxonBase.generateTitle();
104
						firePropertyChangeEvent(new CdmPropertyChangeEvent(NonViralNameDetailSection.this, null));
105
					}
106

    
107
				};
108
			};
109

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

    
112
			toolBarManager.add(cloneAction);
113

    
114
		}
115
		return toolBarManager.createControl(this);
116
	}
117

    
118

    
119
	private boolean checkForMultipleNameUsages(INonViralName nonViralName) {
120
		return nonViralName.getTaxonBases().size() != 1;
121
	}
122

    
123
	/** {@inheritDoc} */
124
	@Override
125
	public String getHeading() {
126
		return "Name";
127
	}
128

    
129
	/** {@inheritDoc} */
130
	@Override
131
    public void setTaxonBase(TaxonBase taxon) {
132
		taxonBase = taxon;
133
		TaxonName name = HibernateProxyHelper.deproxy(taxon.getName());
134
		setEntity(name);
135
	}
136

    
137
	@Override
138
	public TaxonBase getTaxonBase() {
139
		return taxonBase;
140
	}
141

    
142
	@Override
143
	protected AbstractCdmDetailElement<TaxonName> createCdmDetailElement(
144
			AbstractCdmDetailSection<TaxonName> parentElement, int style) {
145
	    return formFactory.createNonViralNameDetailElement(parentElement);
146
	}
147
}
(16-16/21)