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
.forms
;
13 import java
.util
.HashSet
;
16 import org
.apache
.log4j
.Logger
;
17 import org
.eclipse
.swt
.SWT
;
18 import org
.eclipse
.swt
.events
.KeyAdapter
;
19 import org
.eclipse
.swt
.events
.KeyEvent
;
20 import org
.eclipse
.swt
.events
.ModifyEvent
;
21 import org
.eclipse
.swt
.events
.ModifyListener
;
22 import org
.eclipse
.swt
.events
.SelectionEvent
;
23 import org
.eclipse
.swt
.events
.SelectionListener
;
24 import org
.eclipse
.swt
.graphics
.Color
;
25 import org
.eclipse
.swt
.widgets
.Button
;
26 import org
.eclipse
.swt
.widgets
.Composite
;
27 import org
.eclipse
.swt
.widgets
.Label
;
28 import org
.eclipse
.swt
.widgets
.Text
;
30 import eu
.etaxonomy
.taxeditor
.editor
.EditorUtil
;
31 import eu
.etaxonomy
.taxeditor
.preference
.Resources
;
34 * When the button is pressed, this textfield may be edited.
37 * @created Nov 18, 2009
40 public class ToggleableTextElement
extends AbstractCdmFormElement
implements SelectionListener
, ModifyListener
, IEnableableFormElement
{
43 private static final Logger logger
= Logger
44 .getLogger(ToggleableTextElement
.class);
45 private Text text_cache
;
46 private Button button_toggle
;
48 private boolean state
;
49 private Set
<SelectionListener
> selectionListener
= new HashSet
<SelectionListener
>();
51 private Composite container
;
54 * <p>Constructor for ToggleableTextElement.</p>
57 * @param formFactory a {@link eu.etaxonomy.taxeditor.forms.CdmFormFactory} object.
58 * @param parentElement a {@link eu.etaxonomy.taxeditor.forms.ICdmFormElement} object.
59 * @param labelString a {@link java.lang.String} object.
60 * @param initialText a {@link java.lang.String} object.
61 * @param initialState a boolean.
63 protected ToggleableTextElement(CdmFormFactory formFactory
, ICdmFormElement parentElement
,
64 String labelString
, String initialText
, boolean initialState
, int style
) {
65 super(formFactory
, parentElement
);
67 label
= formFactory
.createLabel(getLayoutComposite(), labelString
, style
);
70 container
= formFactory
.createComposite(getLayoutComposite(), SWT
.WRAP
);
71 container
.setLayout(CdmFormFactory
.LAYOUT(2, false));
72 container
.setLayoutData(CdmFormFactory
.FILL_HORIZONTALLY());
73 addControl(container
);
75 text_cache
= formFactory
.createText(container
, initialText
, SWT
.WRAP
| SWT
.MULTI
);
76 addControl(text_cache
);
77 text_cache
.addModifyListener(this);
78 text_cache
.setLayoutData(CdmFormFactory
.FILL());
80 // Don't accept carriage returns as input
81 text_cache
.addKeyListener( new KeyAdapter(){
83 public void keyPressed(KeyEvent e
) {
84 if(e
.character
== SWT
.CR
)
89 button_toggle
= formFactory
.createButton(container
, "Edit", SWT
.TOGGLE
);
90 addControl(button_toggle
);
91 button_toggle
.addSelectionListener(this);
93 setState(initialState
);
99 * @param text a {@link java.lang.String} object.
101 public void setText(String text
){
103 // store current caret position
104 int caretPosition
= text_cache
.getCaretPosition();
106 text_cache
.removeModifyListener(this);
107 text_cache
.setText(text
);
108 text_cache
.addModifyListener(this);
110 // restore caret position
111 text_cache
.setSelection(caretPosition
);
118 * @return a {@link java.lang.String} object.
120 public String
getText(){
121 return text_cache
.getText();
125 * <p>Setter for the field <code>state</code>.</p>
127 * @param state a boolean.
129 public void setState(boolean state
) {
135 public void setEnabled(boolean enabled
) {
136 text_cache
.setEnabled(enabled
);
137 String symbolicName
= enabled ? Resources
.COLOR_FONT_DEFAULT
: Resources
.COLOR_TEXT_DISABLED
;
138 text_cache
.setForeground(EditorUtil
.getColor(symbolicName
));
139 button_toggle
.setSelection(enabled
);
143 * <p>Getter for the field <code>state</code>.</p>
147 public boolean getState(){
152 * @see org.eclipse.swt.events.SelectionListener#widgetSelected(org.eclipse.swt.events.SelectionEvent)
155 public void widgetSelected(SelectionEvent e
) {
156 setState(button_toggle
.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
);
182 public void widgetDefaultSelected(SelectionEvent e
) {}
185 public void setSelected(boolean selected
) {
186 label
.setBackground(getColor(selected
));
187 container
.setBackground(getColor(selected
));
191 public void modifyText(ModifyEvent e
) {
192 firePropertyChangeEvent(new CdmPropertyChangeEvent(this, e
));
196 public void setIrrelevant(boolean irrelevant
) {
197 String colorId
= irrelevant ? Resources
.COLOR_COMPOSITE_IRRELEVANT
: Resources
.COLOR_COMPOSITE_BACKGROUND
;
199 Color color
= EditorUtil
.getColor(colorId
);
200 text_cache
.setBackground(color
);
205 public void setBackground(Color color
) {
206 label
.setBackground(color
);