3 * Copyright (C) 2009 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
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.
10 package eu
.etaxonomy
.taxeditor
.ui
.section
.vocabulary
;
12 import java
.text
.ParseException
;
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
;
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
;
44 public class PresenceAbsenceTermDetailElement
extends DefinedTermDetailElement
<PresenceAbsenceTerm
> implements SelectionListener
{
46 private Label colorLabel
;
50 private Button btnColorChooser
;
52 public PresenceAbsenceTermDetailElement(CdmFormFactory formFactory
,
53 ICdmFormElement formElement
) {
54 super(formFactory
, formElement
);
58 protected void createControls(ICdmFormElement formElement
, PresenceAbsenceTerm entity
, int style
) {
59 super.createControls(formElement
, entity
, style
);
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
);
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
);
75 colorLabel
= new Label(colorAndButton
, style
|SWT
.BORDER
);
76 colorLabel
.setLayoutData(new GridData(16, 16));
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
);
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);
95 public void handleEvent(Object eventSource
) {
96 super.handleEvent(eventSource
);
100 public TermVocabulary
getVocabulary() {
101 return getEntity() != null ?
getEntity().getVocabulary() : null;
108 public void widgetSelected(SelectionEvent e
) {
109 ColorDialog dialog
= new ColorDialog(AbstractUtility
.getShell());
111 dialog
.setRGB(color
.getRGB());
113 RGB rgb
= dialog
.open();
118 color
= new Color(AbstractUtility
.getShell().getDisplay(), rgb
);
119 colorLabel
.setBackground(color
);
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
);
127 firePropertyChangeEvent(new CdmPropertyChangeEvent(this, e
));
134 public void widgetDefaultSelected(SelectionEvent e
) {