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