Project

General

Profile

Download (4.9 KB) Statistics
| Branch: | Tag: | Revision:
1
// $Id$
2
/**
3
* Copyright (C) 2009 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.editor.definedterm;
12

    
13
import java.util.EnumSet;
14
import java.util.HashMap;
15
import java.util.Map;
16
import java.util.Set;
17

    
18
import org.eclipse.jface.action.IContributionItem;
19
import org.eclipse.jface.action.MenuManager;
20
import org.eclipse.swt.SWT;
21
import org.eclipse.ui.menus.CommandContributionItem;
22
import org.eclipse.ui.menus.CommandContributionItemParameter;
23
import org.eclipse.ui.menus.ExtensionContributionFactory;
24
import org.eclipse.ui.menus.IContributionRoot;
25
import org.eclipse.ui.services.IServiceLocator;
26

    
27
import eu.etaxonomy.cdm.model.common.TermType;
28

    
29
/**
30
 * Menu factory used in the store plugin xml to dynamically generate menu (sub-menu) contribution items
31
 * for term types which when clicked open the defined term editor for the chosen term type
32
 * 
33
 * @author c.mathew
34
 * @date 18 Jul 2013
35
 *
36
 */
37

    
38
public class DefinedTermMenuFactory extends ExtensionContributionFactory {	
39
	
40
	@Override
41
	public void createContributionItems(IServiceLocator serviceLocator,
42
			IContributionRoot additions) {
43
		
44
		MenuManager dtMenuManager = 
45
				new MenuManager("Term Editor","eu.etaxonomy.taxeditor.store.definedTermEditorMenu"); 
46
		
47
		dtMenuManager.setVisible(true);
48
        
49
		additions.addContributionItem(dtMenuManager, null);
50
		
51
        for (TermType tt : EnumSet.allOf(TermType.class))
52
        {
53
        	// if term type has a parent, do not add it
54
        	// it will be added in the recursive call
55
        	if(tt.getKindOf() == null) {
56
        		IContributionItem ici = addChildTermsToMenuManager(tt, serviceLocator);
57
        		
58
        		if(ici != null) {
59
        			System.out.println("Adding : " + tt.getMessage()); 	        		
60
        			dtMenuManager.add(ici);
61
        		}
62
        	}
63
        }				       
64
	}
65
	
66
	private IContributionItem addChildTermsToMenuManager(TermType termType, IServiceLocator serviceLocator) {
67
	
68
		Set<TermType> children = termType.getGeneralizationOf();
69
		// term type has no children, so create menu item
70
		if(children.isEmpty()) {
71
			System.out.println("No children for : " + termType.getMessage());
72
			return createMenuItem(termType, serviceLocator);
73
		}
74
		// term type has children, so create sub menu
75
		System.out.println("Children for : " + termType.getMessage());
76
		MenuManager dtMenuManager = 
77
				new MenuManager(termType.getMessage(),"eu.etaxonomy.taxeditor.store." + termType.getKey() + "Menu"); 
78
		dtMenuManager.setVisible(true);
79
		dtMenuManager.add(createDefaultMenuItem(termType, serviceLocator));
80
		// add child items to the sub menu
81
		for(TermType tt : children) {
82
			System.out.println("Child : " + tt.getMessage());
83
			IContributionItem item = addChildTermsToMenuManager(tt,serviceLocator);
84
			if(item != null) {
85
				System.out.println("Adding : " + tt.getMessage() + " to "  + termType.getMessage());
86
				dtMenuManager.add(item);
87
			}					
88
		}		
89
		return dtMenuManager;
90
		
91
	}
92
	
93
	private CommandContributionItem createMenuItem(TermType termType, IServiceLocator serviceLocator) {
94
		
95
		Map<String, String> params = new HashMap<String, String>();
96
		params.put("eu.etaxonomy.taxeditor.store.openDefinedTermEditor.termTypeUuid",
97
				termType.getUuid().toString());
98
		System.out.println("Term Type UUID : " + termType.getUuid().toString());
99
		
100
        CommandContributionItemParameter p = new CommandContributionItemParameter(
101
                serviceLocator, 
102
                "",
103
                "eu.etaxonomy.taxeditor.store.openDefinedTermEditor",
104
                params,
105
                null,
106
                null,
107
                null,
108
                termType.getMessage(),
109
                "",
110
                "",                
111
                SWT.PUSH,
112
                "",
113
                true);
114
   
115
        CommandContributionItem item = new CommandContributionItem(p);   
116
        return item;
117
		
118
	}
119
	
120
	private CommandContributionItem createDefaultMenuItem(TermType termType, IServiceLocator serviceLocator) {
121
		
122
		Map<String, String> params = new HashMap<String, String>();
123
		params.put("eu.etaxonomy.taxeditor.store.openDefinedTermEditor.termTypeUuid",
124
				termType.getUuid().toString());
125
		System.out.println("Term Type UUID : " + termType.getUuid().toString());
126
		
127
        CommandContributionItemParameter p = new CommandContributionItemParameter(
128
                serviceLocator, 
129
                "",
130
                "eu.etaxonomy.taxeditor.store.openDefinedTermEditor",
131
                params,
132
                null,
133
                null,
134
                null,
135
                "All " + termType.getMessage() + "s",
136
                "",
137
                "",                
138
                SWT.PUSH,
139
                "",
140
                true);
141
   
142
        CommandContributionItem item = new CommandContributionItem(p);   
143
        return item;
144
		
145
	}
146
	
147
	
148

    
149
}
(4-4/8)