Merge branch 'release/4.4.0'
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / ui / section / vocabulary / PresenceAbsenceTermDetailElement.java
1 // $Id$
2 /**
3 * Copyright (C) 2009 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 package eu.etaxonomy.taxeditor.ui.section.vocabulary;
11
12 import java.text.ParseException;
13
14 import org.eclipse.swt.SWT;
15 import org.eclipse.swt.events.SelectionEvent;
16 import org.eclipse.swt.events.SelectionListener;
17 import org.eclipse.swt.graphics.Color;
18 import org.eclipse.swt.graphics.RGB;
19 import org.eclipse.swt.layout.GridData;
20 import org.eclipse.swt.layout.GridLayout;
21 import org.eclipse.swt.widgets.Button;
22 import org.eclipse.swt.widgets.ColorDialog;
23 import org.eclipse.swt.widgets.Composite;
24 import org.eclipse.swt.widgets.Label;
25 import org.eclipse.ui.forms.widgets.TableWrapData;
26
27 import eu.etaxonomy.cdm.model.common.TermVocabulary;
28 import eu.etaxonomy.cdm.model.description.PresenceAbsenceTerm;
29 import eu.etaxonomy.taxeditor.Messages;
30 import eu.etaxonomy.taxeditor.model.AbstractUtility;
31 import eu.etaxonomy.taxeditor.model.ImageResources;
32 import eu.etaxonomy.taxeditor.model.MessagingUtils;
33 import eu.etaxonomy.taxeditor.ui.element.CdmFormFactory;
34 import eu.etaxonomy.taxeditor.ui.element.CdmPropertyChangeEvent;
35 import eu.etaxonomy.taxeditor.ui.element.ICdmFormElement;
36 import eu.etaxonomy.taxeditor.ui.element.LayoutConstants;
37
38 /**
39 *
40 * @author pplitzner
41 * @date Nov 24, 2016
42 *
43 */
44 public class PresenceAbsenceTermDetailElement extends DefinedTermDetailElement<PresenceAbsenceTerm> implements SelectionListener{
45
46 private Label colorLabel;
47
48 private Color color;
49
50 private Button btnColorChooser;
51
52 public PresenceAbsenceTermDetailElement(CdmFormFactory formFactory,
53 ICdmFormElement formElement) {
54 super(formFactory, formElement);
55 }
56
57 @Override
58 protected void createControls(ICdmFormElement formElement, PresenceAbsenceTerm entity, int style) {
59 super.createControls(formElement, entity, style);
60
61 Label colorTextLabel = new Label(getLayoutComposite(), style);
62 colorTextLabel.setText(Messages.PresenceAbsenceTermDetailElement_LABEL_COLOR);
63 TableWrapData left = LayoutConstants.LEFT();
64 left.valign = TableWrapData.MIDDLE;
65 colorTextLabel.setLayoutData(left);
66 addControl(colorTextLabel);
67
68 //composite(color label + button)
69 Composite colorAndButton = formFactory.createComposite(getLayoutComposite(), style);
70 addControl(colorAndButton);
71 GridLayout layout = new GridLayout(2, false);
72 layout.marginWidth = 0;
73 colorAndButton.setLayout(layout);
74
75 colorLabel = new Label(colorAndButton, style|SWT.BORDER);
76 colorLabel.setLayoutData(new GridData(16, 16));
77
78 String defaultColor = entity.getDefaultColor();
79 if(defaultColor!=null){
80 //TODO: Is there any utility method from SWT?
81 java.awt.Color awtColor = java.awt.Color.decode("#"+defaultColor); //$NON-NLS-1$
82 RGB rgb = new RGB(awtColor.getRed(), awtColor.getGreen(), awtColor.getBlue());
83 color = new Color(AbstractUtility.getShell().getDisplay(), rgb);
84 colorLabel.setBackground(color);
85 }
86
87 //button
88 btnColorChooser = formFactory.createButton(colorAndButton, "", SWT.NONE); //$NON-NLS-1$
89 btnColorChooser.setImage(ImageResources.getImage(ImageResources.BROWSE_ICON));
90 btnColorChooser.setToolTipText(Messages.PresenceAbsenceTermDetailElement_CHOOSE_COLOR);
91 btnColorChooser.addSelectionListener(this);
92 }
93
94 @Override
95 public void handleEvent(Object eventSource) {
96 super.handleEvent(eventSource);
97 }
98
99 @Override
100 public TermVocabulary getVocabulary() {
101 return getEntity() != null ? getEntity().getVocabulary() : null;
102 }
103
104 /**
105 * {@inheritDoc}
106 */
107 @Override
108 public void widgetSelected(SelectionEvent e) {
109 ColorDialog dialog = new ColorDialog(AbstractUtility.getShell());
110 if(color!=null){
111 dialog.setRGB(color.getRGB());
112 }
113 RGB rgb = dialog.open();
114 if(rgb!=null){
115 if(color!=null){
116 color.dispose();
117 }
118 color = new Color(AbstractUtility.getShell().getDisplay(), rgb);
119 colorLabel.setBackground(color);
120 try {
121 String hexCode = String.format("%02x%02x%02x", rgb.red,rgb.green,rgb.blue); //$NON-NLS-1$
122 getEntity().setDefaultColor(hexCode);
123 } catch (ParseException pe) {
124 MessagingUtils.error(PresenceAbsenceTermDetailElement.class, Messages.PresenceAbsenceTermDetailElement_COLOR_NOT_SET, pe);
125 }
126 }
127 firePropertyChangeEvent(new CdmPropertyChangeEvent(this, e));
128 }
129
130 /**
131 * {@inheritDoc}
132 */
133 @Override
134 public void widgetDefaultSelected(SelectionEvent e) {
135 }
136
137 }