Project

General

Profile

Download (5.88 KB) Statistics
| Branch: | Tag: | Revision:
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.preference.IPreferenceKeys;
30
import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
31
import eu.etaxonomy.taxeditor.store.CdmStore;
32
import eu.etaxonomy.taxeditor.ui.dialog.selection.NameSelectionDialog;
33
import eu.etaxonomy.taxeditor.ui.element.CdmFormFactory;
34
import eu.etaxonomy.taxeditor.ui.element.CdmPropertyChangeEvent;
35
import eu.etaxonomy.taxeditor.ui.element.ICdmFormElement;
36
import eu.etaxonomy.taxeditor.ui.section.AbstractCdmDetailElement;
37
import eu.etaxonomy.taxeditor.ui.section.AbstractCdmDetailSection;
38
import eu.etaxonomy.taxeditor.ui.section.ITaxonBaseDetailSection;
39

    
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
	boolean nameChoosable = false;
50

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

    
67
	@Override
68
	protected Control createToolbar() {
69
	        ToolBarManager toolBarManager = new ToolBarManager(SWT.FLAT);
70

    
71
	        if(nameChoosable && !PreferencesUtil.getPreferenceStore().getBoolean(IPreferenceKeys.SHOW_SIMPLE_NAME_DETAILS_SECTION)){
72
	            //choose name
73
	            Action chooseNameAction = new Action("Choose Name", IAction.AS_PUSH_BUTTON){
74
	                @Override
75
	                public void run() {
76
	                    TaxonNameBase taxonName = NameSelectionDialog.select(getShell(), getConversationHolder(), null);
77
	                    if(taxonName!=null){
78
	                        if(taxonName.isInstanceOf(NonViralName.class)){
79
	                            CdmStore.getService(ITaxonNodeService.class).list(TaxonNode.class, null, null, null, null);
80
	                            NonViralName nonViralName = HibernateProxyHelper.deproxy(taxonName, NonViralName.class);
81
	                            taxonBase.setName(nonViralName);
82
	                            //				        taxonBase.getTitleCache();
83
	                            setEntity(nonViralName);
84
	                            firePropertyChangeEvent(NonViralNameDetailSection.this);
85
	                        }
86
	                        else{
87
	                            MessagingUtils.warningDialog("Invalid name", this, "The selected name can not be used for this taxon.");
88
	                        }
89
	                    }
90
	                }
91
	            };
92
	            chooseNameAction.setToolTipText("Choose name for this taxon");
93
	            chooseNameAction.setImageDescriptor(ImageResources.getImageDescriptor(ImageResources.BROWSE_ICON));
94

    
95
	            toolBarManager.add(chooseNameAction);
96
	    }
97

    
98
		//clone
99
		if(getEntity() != null && checkForMultipleNameUsages(getEntity())){
100

    
101
			Action cloneAction = new Action("Clone", IAction.AS_PUSH_BUTTON){
102
			    @Override
103
			    public void run() {
104
					boolean confirm = MessagingUtils.confirmDialog("Confirm cloning", "Do you really want to clone the name?");
105

    
106
					if(confirm){
107
						NonViralName clonedName;
108
						clonedName = (NonViralName) getEntity().clone();
109
						setEntity(clonedName);
110
						taxonBase.setName(clonedName);
111
						taxonBase.generateTitle();
112
						firePropertyChangeEvent(new CdmPropertyChangeEvent(NonViralNameDetailSection.this, null));
113
					}
114

    
115
				};
116
			};
117

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

    
120
			toolBarManager.add(cloneAction);
121

    
122
		}
123
		return toolBarManager.createControl(this);
124
	}
125

    
126

    
127
	private boolean checkForMultipleNameUsages(NonViralName nonViralName) {
128
		return nonViralName.getTaxonBases().size() != 1;
129
	}
130

    
131
	/** {@inheritDoc} */
132
	@Override
133
	public String getHeading() {
134
		return "Name";
135
	}
136

    
137
	/** {@inheritDoc} */
138
	@Override
139
    public void setTaxonBase(TaxonBase taxon) {
140
		taxonBase = taxon;
141
		NonViralName name = (NonViralName) HibernateProxyHelper.deproxy(taxon.getName());
142
		setEntity(name);
143
	}
144

    
145
	@Override
146
	public TaxonBase getTaxonBase() {
147
		return taxonBase;
148
	}
149

    
150
	@Override
151
	protected AbstractCdmDetailElement<NonViralName> createCdmDetailElement(AbstractCdmDetailSection<NonViralName> parentElement, int style) {
152
	    return formFactory.createNonViralNameDetailElement(parentElement);
153
	}
154
}
(16-16/25)