Merge branch 'release/4.6.0'
[taxeditor.git] / eu.etaxonomy.taxeditor.bulkeditor / src / main / java / eu / etaxonomy / taxeditor / bulkeditor / input / BulkEditorInputType.java
1 package eu.etaxonomy.taxeditor.bulkeditor.input;
2
3 import java.util.HashMap;
4 import java.util.Map;
5 import java.util.ResourceBundle;
6
7 import org.eclipse.jface.action.IContributionItem;
8 import org.eclipse.swt.SWT;
9 import org.eclipse.ui.IEditorInput;
10 import org.eclipse.ui.PlatformUI;
11 import org.eclipse.ui.menus.CommandContributionItem;
12 import org.eclipse.ui.menus.CommandContributionItemParameter;
13
14 import eu.etaxonomy.cdm.api.service.IAgentService;
15 import eu.etaxonomy.cdm.api.service.IGroupService;
16 import eu.etaxonomy.cdm.api.service.IMediaService;
17 import eu.etaxonomy.cdm.api.service.INameService;
18 import eu.etaxonomy.cdm.api.service.IOccurrenceService;
19 import eu.etaxonomy.cdm.api.service.IReferenceService;
20 import eu.etaxonomy.cdm.api.service.ITaxonService;
21 import eu.etaxonomy.cdm.api.service.IUserService;
22 import eu.etaxonomy.cdm.model.agent.AgentBase;
23 import eu.etaxonomy.cdm.model.common.Group;
24 import eu.etaxonomy.cdm.model.common.User;
25 import eu.etaxonomy.cdm.model.media.Media;
26 import eu.etaxonomy.cdm.model.name.NameRelationship;
27 import eu.etaxonomy.cdm.model.name.TaxonNameBase;
28 import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
29 import eu.etaxonomy.cdm.model.reference.Reference;
30 import eu.etaxonomy.cdm.model.taxon.TaxonBase;
31 import eu.etaxonomy.taxeditor.bulkeditor.IBulkEditorConstants;
32 import eu.etaxonomy.taxeditor.l10n.Messages;
33
34 public enum BulkEditorInputType {
35 AGENT(Messages.BulkEditorInputType_0, AgentEditorInput.ID),
36 REFERENCE(Messages.BulkEditorInputType_1, ReferenceEditorInput.ID),
37 NAME(Messages.BulkEditorInputType_2, NameEditorInput.ID),
38 NAME_RELATIONSHIP(Messages.BulkEditorInputType_3, NameRelationshipEditorInput.ID),
39 OCCURRENCE(Messages.BulkEditorInputType_4, OccurrenceEditorInput.ID),
40 USER(Messages.BulkEditorInputType_5, UserEditorInput.ID),
41 GROUP(Messages.BulkEditorInputType_6, GroupEditorInput.ID),
42 TAXON(Messages.BulkEditorInputType_7, TaxonEditorInput.ID),
43 MEDIA(Messages.BulkEditorInputType_8, MediaEditorInput.ID);
44
45 public String id;
46 public String label;
47 public ResourceBundle resourceBundle;
48
49 BulkEditorInputType(String label, String id) {
50 this.id = id;
51 this.label = label;
52 }
53
54 public IContributionItem createContributionItem(){
55 return createContributionItem(label, id);
56 }
57
58 /**
59 * @param key
60 * @param object
61 * @return
62 */
63 private IContributionItem createContributionItem(String label,
64 String inputType) {
65 CommandContributionItemParameter parameter = new CommandContributionItemParameter(
66 PlatformUI.getWorkbench().getActiveWorkbenchWindow(), null,
67 IBulkEditorConstants.DYNAMIC_OPEN_MENU_ID, SWT.NONE);
68
69 parameter.label = label;
70
71 Map parameters = new HashMap();
72 parameters.put(IBulkEditorConstants.INPUT_TYPE_PARAMETER_ID, inputType);
73 parameter.parameters = parameters;
74
75 return new CommandContributionItem(parameter);
76 }
77
78 public static BulkEditorInputType getById(String id) {
79
80 for (BulkEditorInputType type : values()) {
81 if (id.equals(type.id)) {
82 return type;
83 }
84 }
85
86 return null;
87 }
88
89 public static BulkEditorInputType getByType(Class clazz) {
90 if (Reference.class.isAssignableFrom(clazz)) {
91 return REFERENCE;
92 } else if (TaxonNameBase.class.isAssignableFrom(clazz)) {
93 return NAME;
94 } else if (AgentBase.class.isAssignableFrom(clazz)) {
95 return AGENT;
96 } else if (SpecimenOrObservationBase.class.isAssignableFrom(clazz)) {
97 return OCCURRENCE;
98 } else if (NameRelationship.class.isAssignableFrom(clazz)) {
99 return NAME_RELATIONSHIP;
100 } else if (Group.class.isAssignableFrom(clazz)) {
101 return GROUP;
102 } else if (User.class.isAssignableFrom(clazz)) {
103 return USER;
104 } else if (TaxonBase.class.isAssignableFrom(clazz)){
105 return TAXON;
106 } else if (Media.class.isAssignableFrom(clazz)){
107 return MEDIA;
108 }
109 return null;
110 }
111
112 public static BulkEditorInputType getByInput(IEditorInput input) {
113 if (input instanceof ReferenceEditorInput) {
114 return REFERENCE;
115 } else if (input instanceof NameEditorInput) {
116 return NAME;
117 } else if (input instanceof AgentEditorInput) {
118 return AGENT;
119 } else if (input instanceof OccurrenceEditorInput) {
120 return OCCURRENCE;
121 } else if (input instanceof NameRelationshipEditorInput) {
122 return NAME_RELATIONSHIP;
123 } else if (input instanceof UserEditorInput) {
124 return USER;
125 } else if (input instanceof GroupEditorInput) {
126 return GROUP;
127 } else if (input instanceof TaxonEditorInput){
128 return TAXON;
129 }else if (input instanceof MediaEditorInput){
130 return MEDIA;
131 }
132 return null;
133 }
134
135 public static AbstractBulkEditorInput getInput(
136 BulkEditorInputType inputType) {
137 switch (inputType) {
138 case REFERENCE:
139 return new ReferenceEditorInput();
140 case NAME:
141 return new NameEditorInput();
142 case AGENT:
143 return new AgentEditorInput();
144 case OCCURRENCE:
145 return new OccurrenceEditorInput();
146 case NAME_RELATIONSHIP:
147 return new NameRelationshipEditorInput();
148 case USER:
149 return new UserEditorInput();
150 case GROUP:
151 return new GroupEditorInput();
152 case TAXON:
153 return new TaxonEditorInput();
154 case MEDIA:
155 return new MediaEditorInput();
156 default:
157 throw new IllegalStateException(
158 "No input class for the given input type defined."); //$NON-NLS-1$
159 }
160 }
161
162 public static Class getServiceClass(
163 BulkEditorInputType inputType) {
164 switch (inputType) {
165 case REFERENCE:
166 return IReferenceService.class;
167 case NAME:
168 return INameService.class;
169 case AGENT:
170 return IAgentService.class;
171 case OCCURRENCE:
172 return IOccurrenceService.class;
173 case NAME_RELATIONSHIP:
174 return INameService.class;
175 case USER:
176 return IUserService.class;
177 case GROUP:
178 return IGroupService.class;
179 case TAXON:
180 return ITaxonService.class;
181 case MEDIA:
182 return IMediaService.class;
183 default:
184 throw new IllegalStateException(
185 "No input class for the given input type defined."); //$NON-NLS-1$
186 }
187 }
188 }