2 * Copyright (C) 2009 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
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.
9 package eu
.etaxonomy
.taxeditor
.ui
.section
.vocabulary
;
11 import java
.text
.ParseException
;
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
;
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
;
43 public class PresenceAbsenceTermDetailElement
extends DefinedTermDetailElement
<PresenceAbsenceTerm
> implements SelectionListener
{
45 private Label colorLabel
;
49 private Button btnColorChooser
;
51 public PresenceAbsenceTermDetailElement(CdmFormFactory formFactory
,
52 ICdmFormElement formElement
) {
53 super(formFactory
, formElement
);
57 protected void createControls(ICdmFormElement formElement
, PresenceAbsenceTerm entity
, int style
) {
58 super.createControls(formElement
, entity
, style
);
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
);
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
);
74 colorLabel
= new Label(colorAndButton
, style
|SWT
.BORDER
);
75 colorLabel
.setLayoutData(new GridData(16, 16));
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
);
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);
94 public void handleEvent(Object eventSource
) {
95 super.handleEvent(eventSource
);
99 public TermVocabulary
getVocabulary() {
100 return getEntity() != null ?
getEntity().getVocabulary() : null;
107 public void widgetSelected(SelectionEvent e
) {
108 ColorDialog dialog
= new ColorDialog(AbstractUtility
.getShell());
110 dialog
.setRGB(color
.getRGB());
112 RGB rgb
= dialog
.open();
117 color
= new Color(AbstractUtility
.getShell().getDisplay(), rgb
);
118 colorLabel
.setBackground(color
);
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
);
126 firePropertyChangeEvent(new CdmPropertyChangeEvent(this, e
));
133 public void widgetDefaultSelected(SelectionEvent e
) {