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