3 * Copyright (C) 2007 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.
11 package eu
.etaxonomy
.taxeditor
.ui
.forms
;
13 import java
.util
.HashSet
;
16 import org
.eclipse
.swt
.SWT
;
17 import org
.eclipse
.swt
.events
.KeyAdapter
;
18 import org
.eclipse
.swt
.events
.KeyEvent
;
19 import org
.eclipse
.swt
.events
.ModifyEvent
;
20 import org
.eclipse
.swt
.events
.ModifyListener
;
21 import org
.eclipse
.swt
.events
.SelectionEvent
;
22 import org
.eclipse
.swt
.events
.SelectionListener
;
23 import org
.eclipse
.swt
.graphics
.Color
;
24 import org
.eclipse
.swt
.widgets
.Button
;
25 import org
.eclipse
.swt
.widgets
.Composite
;
26 import org
.eclipse
.swt
.widgets
.Label
;
27 import org
.eclipse
.swt
.widgets
.Text
;
29 import eu
.etaxonomy
.taxeditor
.preference
.Resources
;
30 import eu
.etaxonomy
.taxeditor
.store
.StoreUtil
;
33 * When the button is pressed, this textfield may be edited.
36 * @created Nov 18, 2009
39 public class ToggleableTextElement
extends AbstractCdmFormElement
implements SelectionListener
, ModifyListener
, IEnableableFormElement
{
41 private Text text_cache
;
42 private Button button_toggle
;
44 private boolean state
;
45 private Set
<SelectionListener
> selectionListener
= new HashSet
<SelectionListener
>();
47 private Composite container
;
50 * <p>Constructor for ToggleableTextElement.</p>
53 * @param formFactory a {@link eu.etaxonomy.taxeditor.ui.forms.CdmFormFactory} object.
54 * @param parentElement a {@link eu.etaxonomy.taxeditor.ui.forms.ICdmFormElement} object.
55 * @param labelString a {@link java.lang.String} object.
56 * @param initialText a {@link java.lang.String} object.
57 * @param initialState a boolean.
59 protected ToggleableTextElement(CdmFormFactory formFactory
, ICdmFormElement parentElement
,
60 String labelString
, String initialText
, boolean initialState
, int style
) {
61 super(formFactory
, parentElement
);
63 label
= formFactory
.createLabel(getLayoutComposite(), labelString
, style
);
66 container
= formFactory
.createComposite(getLayoutComposite(), SWT
.WRAP
);
67 container
.setLayout(CdmFormFactory
.LAYOUT(2, false));
68 container
.setLayoutData(CdmFormFactory
.FILL_HORIZONTALLY());
69 addControl(container
);
71 text_cache
= formFactory
.createText(container
, initialText
, SWT
.WRAP
| SWT
.MULTI
);
72 addControl(text_cache
);
73 text_cache
.addModifyListener(this);
74 text_cache
.setLayoutData(CdmFormFactory
.FILL());
76 // Don't accept carriage returns as input
77 text_cache
.addKeyListener( new KeyAdapter(){
79 public void keyPressed(KeyEvent e
) {
80 if(e
.character
== SWT
.CR
)
85 button_toggle
= formFactory
.createButton(container
, "Edit", SWT
.TOGGLE
);
86 addControl(button_toggle
);
87 button_toggle
.addSelectionListener(this);
89 setState(initialState
);
95 * @param text a {@link java.lang.String} object.
97 public void setText(String text
){
99 // store current caret position
100 int caretPosition
= text_cache
.getCaretPosition();
102 text_cache
.removeModifyListener(this);
103 text_cache
.setText(text
);
104 text_cache
.addModifyListener(this);
106 // restore caret position
107 text_cache
.setSelection(caretPosition
);
114 * @return a {@link java.lang.String} object.
116 public String
getText(){
117 return text_cache
.getText();
121 * <p>Setter for the field <code>state</code>.</p>
123 * @param state a boolean.
125 public void setState(boolean state
) {
131 public void setEnabled(boolean enabled
) {
132 text_cache
.setEnabled(enabled
);
133 String symbolicName
= enabled ? Resources
.COLOR_FONT_DEFAULT
: Resources
.COLOR_TEXT_DISABLED
;
134 text_cache
.setForeground(StoreUtil
.getColor(symbolicName
));
135 button_toggle
.setSelection(enabled
);
139 * <p>Getter for the field <code>state</code>.</p>
143 public boolean getState(){
148 * @see org.eclipse.swt.events.SelectionListener#widgetSelected(org.eclipse.swt.events.SelectionEvent)
151 public void widgetSelected(SelectionEvent e
) {
152 setState(button_toggle
.getSelection());
153 for(SelectionListener listener
: selectionListener
){
154 listener
.widgetSelected(e
);
156 firePropertyChangeEvent(new CdmPropertyChangeEvent(this, e
));
160 * <p>addSelectionListener</p>
162 * @param listener a {@link org.eclipse.swt.events.SelectionListener} object.
164 public void addSelectionListener(SelectionListener listener
){
165 selectionListener
.add(listener
);
169 * <p>removeSelectionListener</p>
171 * @param listener a {@link org.eclipse.swt.events.SelectionListener} object.
173 public void removeSelectionListener(SelectionListener listener
){
174 selectionListener
.remove(listener
);
178 public void widgetDefaultSelected(SelectionEvent e
) {}
181 public void setSelected(boolean selected
) {
182 label
.setBackground(getColor(selected
));
183 container
.setBackground(getColor(selected
));
187 public void modifyText(ModifyEvent e
) {
188 firePropertyChangeEvent(new CdmPropertyChangeEvent(this, e
));
192 public void setIrrelevant(boolean irrelevant
) {
193 String colorId
= irrelevant ? Resources
.COLOR_COMPOSITE_IRRELEVANT
: Resources
.COLOR_COMPOSITE_BACKGROUND
;
195 Color color
= getColor(colorId
);
196 text_cache
.setBackground(color
);
201 public void setBackground(Color color
) {
202 label
.setBackground(color
);