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
.Text
;
26 import eu
.etaxonomy
.taxeditor
.preference
.Resources
;
27 import eu
.etaxonomy
.taxeditor
.store
.StoreUtil
;
28 import eu
.etaxonomy
.taxeditor
.ui
.campanula
.compatibility
.ICdmFormElement
;
29 import eu
.etaxonomy
.taxeditor
.ui
.element
.AbstractCdmFormElement
;
30 import eu
.etaxonomy
.taxeditor
.ui
.element
.CdmFormFactory
;
31 import eu
.etaxonomy
.taxeditor
.ui
.element
.CdmPropertyChangeEvent
;
32 import eu
.etaxonomy
.taxeditor
.ui
.element
.IEnableableFormElement
;
33 import eu
.etaxonomy
.taxeditor
.ui
.element
.ISelectable
;
34 import eu
.etaxonomy
.taxeditor
.ui
.element
.LayoutConstants
;
41 public class ToggleableTextFieldController
extends AbstractCdmFormElement
implements SelectionListener
, ModifyListener
, IEnableableFormElement
, ISelectable
{
43 private ToggleableTextField toggleableTextField
;
45 private boolean state
;
46 private Set
<SelectionListener
> selectionListener
= new HashSet
<SelectionListener
>();
47 //TODO also connect label
48 // private Label label;
51 * <p>Constructor for ToggleableTextElement.</p>
54 * @param formFactory a {@link eu.etaxonomy.taxeditor.ui.element.CdmFormFactory} object.
55 * @param parentElement a {@link eu.etaxonomy.taxeditor.ui.element.ICdmFormElement} object.
56 * @param labelString a {@link java.lang.String} object.
57 * @param initialText a {@link java.lang.String} object.
58 * @param initialState a boolean.
60 public ToggleableTextFieldController(ToggleableTextField toggleableTextField
, CdmFormFactory formFactory
, ICdmFormElement parentElement
,
61 String initialText
, boolean initialState
, int style
) {
62 super(formFactory
, parentElement
);
63 setPropertyChangeListeners(formFactory
.getPropertyChangeListeners());
64 formFactory
.addPropertyChangeListener(this);
66 this.toggleableTextField
= toggleableTextField
;
68 // label = formFactory.createLabel(getLayoutComposite(), labelString, style);
71 Text text
= toggleableTextField
.getText();
73 text
.addModifyListener(this);
74 text
.setLayoutData(LayoutConstants
.FILL());
76 // Don't accept carriage returns as input
77 text
.addKeyListener( new KeyAdapter(){
79 public void keyPressed(KeyEvent e
) {
80 if(e
.character
== SWT
.CR
) {
86 Button editButton
= toggleableTextField
.getEditButton();
87 addControl(editButton
);
88 editButton
.addSelectionListener(this);
90 setState(initialState
);
96 * @param text a {@link java.lang.String} object.
98 public void setText(String text
){
100 // store current caret position
101 Text textField
= toggleableTextField
.getText();
102 int caretPosition
= textField
.getCaretPosition();
104 textField
.removeModifyListener(this);
105 textField
.setText(text
);
106 textField
.addModifyListener(this);
108 // restore caret position
109 textField
.setSelection(caretPosition
);
116 * @return a {@link java.lang.String} object.
118 public String
getText(){
119 return toggleableTextField
.getText().getText();
123 * <p>Setter for the field <code>state</code>.</p>
125 * @param state a boolean.
127 public void setState(boolean state
) {
134 public void setEnabled(boolean enabled
) {
135 toggleableTextField
.getText().setEnabled(enabled
);
136 String symbolicName
= enabled ? Resources
.COLOR_FONT_DEFAULT
: Resources
.COLOR_TEXT_DISABLED
;
137 toggleableTextField
.getText().setForeground(StoreUtil
.getColor(symbolicName
));
138 toggleableTextField
.getEditButton().setSelection(enabled
);
142 * <p>Getter for the field <code>state</code>.</p>
146 public boolean getState(){
151 * @see org.eclipse.swt.events.SelectionListener#widgetSelected(org.eclipse.swt.events.SelectionEvent)
155 public void widgetSelected(SelectionEvent e
) {
156 setState(toggleableTextField
.getEditButton().getSelection());
157 for(SelectionListener listener
: selectionListener
){
158 listener
.widgetSelected(e
);
160 firePropertyChangeEvent(new CdmPropertyChangeEvent(this, e
));
164 * <p>addSelectionListener</p>
166 * @param listener a {@link org.eclipse.swt.events.SelectionListener} object.
168 public void addSelectionListener(SelectionListener listener
){
169 selectionListener
.add(listener
);
173 * <p>removeSelectionListener</p>
175 * @param listener a {@link org.eclipse.swt.events.SelectionListener} object.
177 public void removeSelectionListener(SelectionListener listener
){
178 selectionListener
.remove(listener
);
183 public void widgetDefaultSelected(SelectionEvent e
) {}
187 public void modifyText(ModifyEvent e
) {
188 firePropertyChangeEvent(new CdmPropertyChangeEvent(this, e
));
193 public void setIrrelevant(boolean irrelevant
) {
194 String colorId
= irrelevant ? Resources
.COLOR_COMPOSITE_IRRELEVANT
: Resources
.COLOR_COMPOSITE_BACKGROUND
;
196 Color color
= getColor(colorId
);
197 toggleableTextField
.getText().setBackground(color
);
202 public void setBackground(Color color
) {
203 // label.setBackground(color);
204 toggleableTextField
.setBackground(color
);
208 public void setSelected(boolean selected
) {
209 setBackground(selected ? SELECTED
: getPersistentBackground());