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