adresses #2257
[taxeditor.git] / taxeditor-store / src / main / java / eu / etaxonomy / taxeditor / ui / section / reference / ReferenceDetailElement.java
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.reference;
12
13 import java.net.URISyntaxException;
14 import java.util.ArrayList;
15 import java.util.Arrays;
16 import java.util.List;
17
18 import org.eclipse.swt.SWT;
19
20 import eu.etaxonomy.cdm.model.agent.Institution;
21 import eu.etaxonomy.cdm.model.agent.TeamOrPersonBase;
22 import eu.etaxonomy.cdm.model.reference.Reference;
23 import eu.etaxonomy.cdm.model.reference.ReferenceFactory;
24 import eu.etaxonomy.cdm.model.reference.ReferenceType;
25 import eu.etaxonomy.taxeditor.model.AuthorHelper;
26 import eu.etaxonomy.taxeditor.store.StoreUtil;
27 import eu.etaxonomy.taxeditor.ui.dialogs.filteredSelection.ReferenceSelectionDialog;
28 import eu.etaxonomy.taxeditor.ui.forms.CdmFormFactory;
29 import eu.etaxonomy.taxeditor.ui.forms.CdmFormFactory.EnumComboType;
30 import eu.etaxonomy.taxeditor.ui.forms.CdmFormFactory.SelectionType;
31 import eu.etaxonomy.taxeditor.ui.forms.CdmPropertyChangeEvent;
32 import eu.etaxonomy.taxeditor.ui.forms.ICdmFormElement;
33 import eu.etaxonomy.taxeditor.ui.forms.IErrorIntolerableElement;
34 import eu.etaxonomy.taxeditor.ui.forms.IExceptionHandler;
35 import eu.etaxonomy.taxeditor.ui.forms.TextWithLabelElement;
36 import eu.etaxonomy.taxeditor.ui.forms.TimePeriodElement;
37 import eu.etaxonomy.taxeditor.ui.forms.ToggleableTextElement;
38 import eu.etaxonomy.taxeditor.ui.forms.UriWithLabelElement;
39 import eu.etaxonomy.taxeditor.ui.section.AbstractCdmDetailElement;
40 import eu.etaxonomy.taxeditor.ui.selection.AbstractSelectionElement;
41 import eu.etaxonomy.taxeditor.ui.selection.AgentSelectionElement;
42 import eu.etaxonomy.taxeditor.ui.selection.InstitutionSelectionElement;
43 import eu.etaxonomy.taxeditor.ui.selection.ReferenceSelectionElement;
44 import eu.etaxonomy.taxeditor.ui.term.ReferenceTypeComboElement;
45
46 /**
47 * <p>ReferenceBaseDetailElement class.</p>
48 *
49 * @author n.hoffmann
50 * @created Mar 2, 2010
51 * @version 1.0
52 */
53 public class ReferenceDetailElement extends AbstractCdmDetailElement<Reference> implements IErrorIntolerableElement, IExceptionHandler {
54
55 // property holder
56 private ToggleableTextElement toggleable_cache;
57 private TextWithLabelElement text_edition;
58 private TextWithLabelElement text_editor;
59 private TextWithLabelElement text_isbn;
60 private TextWithLabelElement text_issn;
61 private TextWithLabelElement text_organisation;
62 private TextWithLabelElement text_pages;
63 private TextWithLabelElement text_placePublished;
64 private TextWithLabelElement text_publisher;
65 private TextWithLabelElement text_referenceAbstract;
66 private TextWithLabelElement text_series;
67 private TextWithLabelElement text_seriesPart;
68 private TextWithLabelElement text_title;
69 private TextWithLabelElement text_volume;
70 private UriWithLabelElement text_uri;
71 private AgentSelectionElement selection_authorTeam;
72 private TimePeriodElement element_timePeriod;
73 private ReferenceSelectionElement selection_inReference;
74 private InstitutionSelectionElement selection_institution;
75 private ReferenceTypeComboElement combo_referenceType;
76
77 private ReferenceSelectionElement selection_inSeries;
78
79
80 /**
81 * <p>Constructor for ReferenceBaseDetailElement.</p>
82 *
83 * @param cdmFormFactory a {@link eu.etaxonomy.taxeditor.ui.forms.CdmFormFactory} object.
84 * @param formElement a {@link eu.etaxonomy.taxeditor.ui.forms.ICdmFormElement} object.
85 * @param style a int.
86 */
87 public ReferenceDetailElement(CdmFormFactory cdmFormFactory,
88 ICdmFormElement formElement, int style) {
89 super(cdmFormFactory, formElement);
90 }
91
92 /*
93 * (non-Javadoc)
94 * @see eu.etaxonomy.taxeditor.forms.section.cdmdetail.AbstractCdmDetailComposite#createControl(org.eclipse.swt.widgets.Composite, int)
95 */
96 /** {@inheritDoc} */
97 @Override
98 protected void createControls(ICdmFormElement formElement, Reference entity, int style) {
99 toggleable_cache = formFactory.createToggleableTextField(formElement, "Reference Cache", entity.getTitleCache(), entity.isProtectedTitleCache(), style);
100
101 combo_referenceType = (ReferenceTypeComboElement) formFactory.createEnumComboElement(EnumComboType.REFERENCETYPE, formElement, style);
102
103 combo_referenceType.setSelection(entity.getType());
104
105 // title
106 text_title = formFactory.createTextWithLabelElement(formElement, "Title", entity.getTitle(), style);
107
108 // author team
109 selection_authorTeam = (AgentSelectionElement) formFactory.createSelectionElement(SelectionType.AGENT, getConversationHolder(), formElement, "Author", AuthorHelper.getAuthor(entity.getAuthorTeam()), style);
110
111 ReferenceType referenceType = entity.getType();
112
113 if(referenceType != null){
114
115 createInReferenceByType(this, referenceType, entity, SWT.NULL);
116
117 if(referenceType.equals(ReferenceType.Generic)){
118 createGenericControls(this, entity, SWT.NULL);
119 createPublicationControls(this, referenceType, entity, SWT.NULL);
120 }else{
121 if(referenceType.isPrintedUnit()){
122 createPrintedUnitControls(this, referenceType, entity, SWT.NULL);
123 }
124 if(referenceType.isPublication()){
125 createPublicationControls(this, referenceType, entity, SWT.NULL);
126 }
127 if(referenceType.isVolumeReference()){
128 createVolumeControls(this, entity, SWT.NULL);
129 }
130 if(referenceType.isSection()){
131 createSectionControls(this, entity, SWT.NULL);
132 }
133 }
134 }
135
136 // date published
137 element_timePeriod = formFactory.createTimePeriodElement(formElement, "Date Published", entity.getDatePublished(), style);
138
139 createUriAndAbstract(this, entity, SWT.NULL);
140 }
141
142 /*
143 * (non-Javadoc)
144 * @see eu.etaxonomy.taxeditor.forms.section.cdmdetail.AbstractCdmDetailComposite#updateContent()
145 */
146 /** {@inheritDoc} */
147 @Override
148 protected void updateContent() {
149 if(getEntity() == null){
150 setEntity(ReferenceFactory.newGeneric());
151 }
152
153 super.updateContent();
154
155 // enable elements according to state of cache protection
156 // setEnabled(getEntity().isProtectedTitleCache(), Arrays.asList(new Object[]{text_cache}));
157 toggleable_cache.setEnabled(getEntity().isProtectedTitleCache());
158 setIrrelevant(toggleable_cache.getState(), Arrays.asList(new Object[]{toggleable_cache}));
159 }
160
161 /**
162 * Creates an in reference for reference types that do hav this.
163 *
164 * @param element
165 * @param referenceType
166 * @param reference
167 * @param style
168 */
169 private void createInReferenceByType(
170 ICdmFormElement element,
171 ReferenceType referenceType, Reference reference, int style) {
172 boolean createInReferenceWidget = true;
173 String label = "In Reference";
174
175 switch(referenceType){
176 case Generic:
177 break;
178 case BookSection:
179 label = "Book";
180 break;
181 case InProceedings:
182 label = "Print Series";
183 break;
184 case Article:
185 label = "Journal";
186 break;
187 case WebPage:
188 break;
189 default:
190 createInReferenceWidget = false;
191 }
192
193 if(createInReferenceWidget){
194 selection_inReference = (ReferenceSelectionElement) formFactory.createSelectionElement(SelectionType.REFERENCE, getConversationHolder(),
195 element, label, reference.getInReference(), style);
196 }
197 }
198
199 /**
200 * Creates additional controls to edit a generic reference
201 *
202 * @param element
203 * @param reference
204 * @param listener
205 * @param style
206 */
207 private void createGenericControls(ICdmFormElement element, Reference reference, int style){
208
209 //editor
210 text_editor = formFactory.createTextWithLabelElement(element, "Editor", reference.getEditor(), style);
211
212 // series
213 text_series = formFactory.createTextWithLabelElement(element, "Series", reference.getSeries(), style);
214
215 // volume
216 text_volume = formFactory.createTextWithLabelElement(element, "Volume", reference.getVolume(), style);
217
218 // pages
219 text_pages = formFactory.createTextWithLabelElement(element, "Pages", reference.getPages(), style);
220 }
221
222
223 /**
224 * Creates additional controls to edit a publication
225 *
226 * @param element
227 * @param reference
228 * @param listener
229 * @param style
230 */
231 private void createPublicationControls(ICdmFormElement element, ReferenceType referenceType, Reference reference, int style){
232
233 // placePublished
234 text_placePublished = formFactory.createTextWithLabelElement(element, "Place Published", reference.getPlacePublished(), style);
235
236 // publisher
237 text_publisher = formFactory.createTextWithLabelElement(element, "Publisher", reference.getPublisher(), style);
238
239 if(referenceType.equals(ReferenceType.Journal)){
240 //issn
241 text_issn = formFactory.createTextWithLabelElement(element, "ISSN", reference.getIssn(), style);
242 }
243
244 if(referenceType.equals(ReferenceType.Report)){
245 // institution
246 selection_institution = (InstitutionSelectionElement) formFactory.createSelectionElement(SelectionType.INSTITUTION, getConversationHolder(), element, "Institution", reference.getInstitution(), style);
247 }
248
249 if(referenceType.equals(ReferenceType.Thesis)){
250 // school
251 selection_institution = (InstitutionSelectionElement) formFactory.createSelectionElement(SelectionType.INSTITUTION, getConversationHolder(), element, "School", reference.getSchool(), style);
252 }
253
254 if(referenceType.equals(ReferenceType.Proceedings)){
255 //organization
256 text_organisation = formFactory.createTextWithLabelElement(element, "Organisation", reference.getOrganization(), style);
257 }
258 }
259
260
261 /**
262 * Creates additional controls to edit a section
263 *
264 * @param element
265 * @param reference
266 * @param style
267 */
268 private void createSectionControls(ICdmFormElement element, Reference reference, int style){
269 // pages
270 text_pages = formFactory.createTextWithLabelElement(element, "Pages", reference.getPages(), style);
271 }
272
273 /**
274 * Creates additional controls to edit a volume
275 *
276 * @param element
277 * @param reference
278 * @param style
279 */
280 private void createVolumeControls(ICdmFormElement element, Reference reference, int style){
281
282 text_volume = formFactory.createTextWithLabelElement(element, "Volume", reference.getVolume(), style);
283
284 text_series = formFactory.createTextWithLabelElement(element, "Series", reference.getSeries(), style);
285 }
286
287
288 /**
289 *
290 * @param element
291 * @param referenceType
292 * @param reference
293 * @param style
294 */
295 private void createPrintedUnitControls(ICdmFormElement element, ReferenceType referenceType, Reference reference, int style){
296 // in series
297 selection_inSeries = (ReferenceSelectionElement) formFactory.createSelectionElement(SelectionType.REFERENCE, getConversationHolder(), element, "In Series", (Reference) reference.getInSeries(), style);
298
299 //editor
300 text_editor = formFactory.createTextWithLabelElement(element, "Editor", reference.getEditor(), style);
301
302 // series part
303 text_seriesPart = formFactory.createTextWithLabelElement(element, "Series Part", reference.getSeriesPart(), style);
304
305 if(referenceType.equals(ReferenceType.Book)){
306 // edition
307 text_edition = formFactory.createTextWithLabelElement(element, "Edition", reference.getEdition(), style);
308
309 //isbn
310 text_isbn = formFactory.createTextWithLabelElement(element, "ISBN", reference.getIsbn(), style);
311 }
312 }
313
314 private void createUriAndAbstract(ICdmFormElement element,
315 Reference reference, int style) {
316 // uri
317 text_uri = formFactory.createUriWithLabelElement(element, "URI", reference.getUri(), style);
318 // abstract
319 text_referenceAbstract = formFactory.createMultilineTextWithLabel(element, "Reference Abstract", 100, style);
320 text_referenceAbstract.setText(reference.getReferenceAbstract());
321 }
322
323
324 /** {@inheritDoc} */
325 @Override
326 public void handleEvent(Object eventSource) {
327 if(eventSource == combo_referenceType){
328 getEntity().setType(combo_referenceType.getSelection());
329 getParentElement().refresh();
330 }
331 else if(eventSource == element_timePeriod){
332 getEntity().setDatePublished(element_timePeriod.getTimePeriod());
333 clearException();
334 }
335 // selections
336 else if(eventSource == selection_authorTeam){
337 getEntity().setAuthorTeam((TeamOrPersonBase)selection_authorTeam.getSelection());
338 }
339 else if(eventSource == selection_inReference){
340 getEntity().setInReference(selection_inReference.getSelection());
341 }
342 else if(eventSource == selection_inSeries){
343 getEntity().setInReference(selection_inSeries.getSelection());
344 }
345 else if(eventSource == selection_institution){
346 getEntity().setInstitution(selection_institution.getSelection());
347 }
348
349 // textfields
350 else if(eventSource == toggleable_cache){
351 getEntity().setTitleCache(toggleable_cache.getText(), toggleable_cache.getState());
352 // setEnabled(! text_cache.getState(), Arrays.asList(new Object[]{text_cache}));
353 setIrrelevant(toggleable_cache.getState(), Arrays.asList(new Object[]{toggleable_cache}));
354 }
355 else if(eventSource == text_edition){
356 getEntity().setEdition(text_edition.getText());
357 }
358 else if(eventSource == text_editor){
359 getEntity().setEditor(text_editor.getText());
360 }
361 else if(eventSource == text_isbn){
362 getEntity().setIsbn(text_isbn.getText());
363 }
364 else if(eventSource == text_issn){
365 getEntity().setIssn(text_issn.getText());
366 }
367 else if(eventSource == text_organisation){
368 getEntity().setOrganization(text_organisation.getText());
369 }
370 else if(eventSource == text_pages){
371 getEntity().setPages(text_pages.getText());
372 }
373 else if(eventSource == text_placePublished){
374 getEntity().setPlacePublished(text_placePublished.getText());
375 }
376 else if(eventSource == text_publisher){
377 getEntity().setPublisher(text_publisher.getText());
378 }
379 else if(eventSource == text_referenceAbstract){
380 getEntity().setReferenceAbstract(text_referenceAbstract.getText());
381 }
382 else if(eventSource == text_series){
383 getEntity().setSeries(text_series.getText());
384 }
385 else if(eventSource == text_seriesPart){
386 getEntity().setSeriesPart(text_seriesPart.getText());
387 }
388 else if(eventSource == text_title){
389 getEntity().setTitle(text_title.getText());
390 }
391 else if(eventSource == text_uri){
392 try {
393 getEntity().setUri(text_uri.getUri());
394 } catch (URISyntaxException e) {
395 StoreUtil.warningDialog("Invlid URI", getClass(), e.getLocalizedMessage());
396 }
397 }
398 else if(eventSource == text_volume){
399 getEntity().setVolume(text_volume.getText());
400 }
401
402 else if(eventSource != toggleable_cache){
403 toggleable_cache.setText(getEntity().getTitleCache());
404 }
405 }
406
407 /*
408 * (non-Javadoc)
409 * @see eu.etaxonomy.taxeditor.section.AbstractCdmDetailElement#handleException(eu.etaxonomy.taxeditor.forms.CdmPropertyChangeEvent)
410 */
411 /** {@inheritDoc} */
412 @Override
413 public void handleException(CdmPropertyChangeEvent event) {
414 exceptionOccurred(event);
415 }
416
417 /* (non-Javadoc)
418 * @see eu.etaxonomy.taxeditor.forms.IExceptionHandler#clearException()
419 */
420 /** {@inheritDoc} */
421 @Override
422 public void clearException() {
423 exceptionOccurred(null);
424 }
425
426 private List<IExceptionHandler> exceptionHandlers = new ArrayList<IExceptionHandler>();
427
428 /* (non-Javadoc)
429 * @see eu.etaxonomy.taxeditor.forms.IErrorIntolerableElement#addExceptionHandler(eu.etaxonomy.taxeditor.forms.IExceptionHandler)
430 */
431 /** {@inheritDoc} */
432 @Override
433 public void addExceptionHandler(IExceptionHandler handler) {
434 exceptionHandlers.add(handler);
435 }
436
437 /* (non-Javadoc)
438 * @see eu.etaxonomy.taxeditor.forms.IErrorIntolerableElement#removeExceptionHandler(eu.etaxonomy.taxeditor.forms.IExceptionHandler)
439 */
440 /** {@inheritDoc} */
441 @Override
442 public void removeExceptionHandler(IExceptionHandler handler) {
443 exceptionHandlers.remove(handler);
444 }
445
446 /* (non-Javadoc)
447 * @see eu.etaxonomy.taxeditor.forms.IErrorIntolerableElement#exceptionOccurred(eu.etaxonomy.taxeditor.forms.CdmPropertyChangeEvent)
448 */
449 /** {@inheritDoc} */
450 @Override
451 public void exceptionOccurred(CdmPropertyChangeEvent event) {
452 for(IExceptionHandler handler : exceptionHandlers){
453 if(event == null) {
454 clearException();
455 }else{
456 handler.handleException(event);
457 }
458 }
459 }
460 }