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