+// $Id$
+/**
+* Copyright (C) 2009 EDIT
+* European Distributed Institute of Taxonomy
+* http://www.e-taxonomy.eu
+*
+* The contents of this file are subject to the Mozilla Public License Version 1.1
+* See LICENSE.TXT at the top of this package for the full license terms.
+*/
+package eu.etaxonomy.taxeditor.ui.section.vocabulary;
+
+import java.text.ParseException;
+
+import org.eclipse.swt.events.MouseEvent;
+import org.eclipse.swt.events.MouseListener;
+import org.eclipse.swt.graphics.Color;
+import org.eclipse.swt.graphics.RGB;
+import org.eclipse.swt.widgets.ColorDialog;
+import org.eclipse.swt.widgets.Label;
+
+import eu.etaxonomy.cdm.model.common.TermVocabulary;
+import eu.etaxonomy.cdm.model.description.PresenceAbsenceTerm;
+import eu.etaxonomy.taxeditor.model.AbstractUtility;
+import eu.etaxonomy.taxeditor.model.MessagingUtils;
+import eu.etaxonomy.taxeditor.ui.element.CdmFormFactory;
+import eu.etaxonomy.taxeditor.ui.element.CdmPropertyChangeEvent;
+import eu.etaxonomy.taxeditor.ui.element.ICdmFormElement;
+
+/**
+ *
+ * @author pplitzner
+ * @date Nov 24, 2016
+ *
+ */
+public class PresenceAbsenceTermDetailElement extends DefinedTermDetailElement<PresenceAbsenceTerm> implements MouseListener{
+
+ private Label colorLabel;
+
+ private Color color;
+
+ public PresenceAbsenceTermDetailElement(CdmFormFactory formFactory,
+ ICdmFormElement formElement) {
+ super(formFactory, formElement);
+ }
+
+ @Override
+ protected void createControls(ICdmFormElement formElement, PresenceAbsenceTerm entity, int style) {
+ super.createControls(formElement, entity, style);
+
+ Label colorTextLabel = new Label(getLayoutComposite(), style);
+ colorTextLabel.setText("Color");
+ colorLabel = new Label(getLayoutComposite(), style);
+ colorLabel.addMouseListener(this);
+
+ String defaultColor = entity.getDefaultColor();
+ if(defaultColor!=null){
+ java.awt.Color awtColor = java.awt.Color.decode("#"+defaultColor);
+ RGB rgb = new RGB(awtColor.getRed(), awtColor.getGreen(), awtColor.getBlue());
+ color = new Color(AbstractUtility.getShell().getDisplay(), rgb);
+ colorLabel.setBackground(color);
+ }
+ }
+
+ @Override
+ public void handleEvent(Object eventSource) {
+ super.handleEvent(eventSource);
+ }
+
+ @Override
+ public TermVocabulary getVocabulary() {
+ return getEntity() != null ? getEntity().getVocabulary() : null;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public void mouseDoubleClick(MouseEvent e) {
+
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public void mouseDown(MouseEvent e) {
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public void mouseUp(MouseEvent e) {
+ ColorDialog dialog = new ColorDialog(AbstractUtility.getShell());
+ RGB rgb = dialog.open();
+ if(rgb!=null){
+ if(color!=null){
+ color.dispose();
+ }
+ color = new Color(AbstractUtility.getShell().getDisplay(), rgb);
+ colorLabel.setBackground(color);
+ try {
+ String hexCode = String.format("%02x%02x%02x", rgb.red,rgb.green,rgb.blue);
+ getEntity().setDefaultColor(hexCode);
+ } catch (ParseException pe) {
+ MessagingUtils.error(PresenceAbsenceTermDetailElement.class, "Color could not be set", pe);
+ }
+ }
+ firePropertyChangeEvent(new CdmPropertyChangeEvent(this, e));
+ }
+
+}