3 * Copyright (C) 2013 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
.campanula
.basicFields
;
12 import java
.util
.HashSet
;
15 import org
.eclipse
.swt
.SWT
;
16 import org
.eclipse
.swt
.events
.KeyAdapter
;
17 import org
.eclipse
.swt
.events
.KeyEvent
;
18 import org
.eclipse
.swt
.events
.ModifyEvent
;
19 import org
.eclipse
.swt
.events
.ModifyListener
;
20 import org
.eclipse
.swt
.events
.SelectionEvent
;
21 import org
.eclipse
.swt
.events
.SelectionListener
;
22 import org
.eclipse
.swt
.graphics
.Color
;
23 import org
.eclipse
.swt
.widgets
.Button
;
24 import org
.eclipse
.swt
.widgets
.Listener
;
25 import org
.eclipse
.swt
.widgets
.Text
;
27 import eu
.etaxonomy
.cdm
.common
.CdmUtils
;
28 import eu
.etaxonomy
.taxeditor
.preference
.Resources
;
29 import eu
.etaxonomy
.taxeditor
.store
.StoreUtil
;
30 import eu
.etaxonomy
.taxeditor
.ui
.campanula
.compatibility
.ICdmFormElement
;
31 import eu
.etaxonomy
.taxeditor
.ui
.element
.AbstractCdmFormElement
;
32 import eu
.etaxonomy
.taxeditor
.ui
.element
.CdmFormFactory
;
33 import eu
.etaxonomy
.taxeditor
.ui
.element
.CdmPropertyChangeEvent
;
34 import eu
.etaxonomy
.taxeditor
.ui
.element
.IEnableableFormElement
;
35 import eu
.etaxonomy
.taxeditor
.ui
.element
.ISelectable
;
36 import eu
.etaxonomy
.taxeditor
.ui
.element
.LayoutConstants
;
43 public class ToggleableTextFieldController
extends AbstractCdmFormElement
implements SelectionListener
, ModifyListener
, IEnableableFormElement
, ISelectable
{
45 private ToggleableTextField toggleableTextField
;
47 private boolean state
;
48 private Set
<SelectionListener
> selectionListener
= new HashSet
<SelectionListener
>();
49 //TODO also connect label
50 // private Label label;
53 * <p>Constructor for ToggleableTextElement.</p>
56 * @param formFactory a {@link eu.etaxonomy.taxeditor.ui.element.CdmFormFactory} object.
57 * @param parentElement a {@link eu.etaxonomy.taxeditor.ui.element.ICdmFormElement} object.
58 * @param labelString a {@link java.lang.String} object.
59 * @param initialText a {@link java.lang.String} object.
60 * @param initialState a boolean.
62 public ToggleableTextFieldController(ToggleableTextField toggleableTextField
, CdmFormFactory formFactory
, ICdmFormElement parentElement
,
63 String initialText
, boolean initialState
, int style
) {
64 super(formFactory
, parentElement
);
65 setPropertyChangeListeners(formFactory
.getPropertyChangeListeners());
66 formFactory
.addPropertyChangeListener(this);
68 this.toggleableTextField
= toggleableTextField
;
70 // label = formFactory.createLabel(getLayoutComposite(), labelString, style);
73 Text text
= toggleableTextField
.getText();
75 text
.addModifyListener(this);
76 text
.setLayoutData(LayoutConstants
.FILL());
78 // Don't accept carriage returns as input
79 text
.addKeyListener( new KeyAdapter(){
81 public void keyPressed(KeyEvent e
) {
82 if(e
.character
== SWT
.CR
) {
88 Button editButton
= toggleableTextField
.getEditButton();
89 addControl(editButton
);
90 editButton
.addSelectionListener(this);
92 setState(initialState
);
98 * @param text a {@link java.lang.String} object.
100 public void setText(String text
){
102 // store current caret position
103 Text textField
= toggleableTextField
.getText();
104 int caretPosition
= textField
.getCaretPosition();
106 Listener
[] listeners
= textField
.getListeners(SWT
.Modify
);
107 for (Listener listener
: listeners
) {
108 textField
.removeListener(SWT
.Modify
, listener
);
110 textField
.setText(CdmUtils
.Nz(text
));
111 for (Listener listener
: listeners
) {
112 textField
.addListener(SWT
.Modify
, listener
);
115 // restore caret position
116 textField
.setSelection(caretPosition
);
123 * @return a {@link java.lang.String} object.
125 public String
getText(){
126 return toggleableTextField
.getText().getText();
130 * <p>Setter for the field <code>state</code>.</p>
132 * @param state a boolean.
134 public void setState(boolean state
) {
141 public void setEnabled(boolean enabled
) {
142 toggleableTextField
.getText().setEnabled(enabled
);
143 String symbolicName
= enabled ? Resources
.COLOR_FONT_DEFAULT
: Resources
.COLOR_TEXT_DISABLED
;
144 toggleableTextField
.getText().setForeground(StoreUtil
.getColor(symbolicName
));
145 toggleableTextField
.getEditButton().setSelection(enabled
);
149 * <p>Getter for the field <code>state</code>.</p>
153 public boolean getState(){
158 * @see org.eclipse.swt.events.SelectionListener#widgetSelected(org.eclipse.swt.events.SelectionEvent)
162 public void widgetSelected(SelectionEvent e
) {
163 setState(toggleableTextField
.getEditButton().getSelection());
164 for(SelectionListener listener
: selectionListener
){
165 listener
.widgetSelected(e
);
167 firePropertyChangeEvent(new CdmPropertyChangeEvent(this, e
));
171 * <p>addSelectionListener</p>
173 * @param listener a {@link org.eclipse.swt.events.SelectionListener} object.
175 public void addSelectionListener(SelectionListener listener
){
176 selectionListener
.add(listener
);
180 * <p>removeSelectionListener</p>
182 * @param listener a {@link org.eclipse.swt.events.SelectionListener} object.
184 public void removeSelectionListener(SelectionListener listener
){
185 selectionListener
.remove(listener
);
190 public void widgetDefaultSelected(SelectionEvent e
) {}
194 public void modifyText(ModifyEvent e
) {
195 firePropertyChangeEvent(new CdmPropertyChangeEvent(this, e
));
200 public void setIrrelevant(boolean irrelevant
) {
201 String colorId
= irrelevant ? Resources
.COLOR_COMPOSITE_IRRELEVANT
: Resources
.COLOR_COMPOSITE_BACKGROUND
;
203 Color color
= getColor(colorId
);
204 toggleableTextField
.getText().setBackground(color
);
209 public void setBackground(Color color
) {
210 // label.setBackground(color);
211 toggleableTextField
.setBackground(color
);
215 public void setSelected(boolean selected
) {
216 setBackground(selected ? SELECTED
: getPersistentBackground());