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
.element
;
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
.model
.ImageResources
;
30 import eu
.etaxonomy
.taxeditor
.preference
.Resources
;
31 import eu
.etaxonomy
.taxeditor
.store
.StoreUtil
;
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
, ISelectable
{
42 private final Text text_cache
;
43 private final Button button_toggle
;
45 private boolean state
;
46 private final Set
<SelectionListener
> selectionListener
= new HashSet
<SelectionListener
>();
47 private final Label label
;
48 private final Composite container
;
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 protected ToggleableTextElement(CdmFormFactory formFactory
, ICdmFormElement parentElement
,
61 String labelString
, String initialText
, boolean initialState
, int style
) {
62 super(formFactory
, parentElement
);
64 label
= formFactory
.createLabel(getLayoutComposite(), labelString
, style
);
67 container
= formFactory
.createComposite(getLayoutComposite(), SWT
.WRAP
);
68 container
.setLayout(LayoutConstants
.LAYOUT(2, false));
69 container
.setLayoutData(LayoutConstants
.FILL_HORIZONTALLY());
70 addControl(container
);
72 text_cache
= formFactory
.createText(container
, initialText
, SWT
.WRAP
| SWT
.MULTI
);
73 addControl(text_cache
);
74 text_cache
.addModifyListener(this);
75 text_cache
.setLayoutData(LayoutConstants
.FILL());
77 // Don't accept carriage returns as input
78 text_cache
.addKeyListener( new KeyAdapter(){
80 public void keyPressed(KeyEvent e
) {
81 if(e
.character
== SWT
.CR
) {
87 button_toggle
= formFactory
.createButton(container
, "Edit", SWT
.TOGGLE
);
88 button_toggle
.setText("");
89 button_toggle
.setImage(ImageResources
.getImage(ImageResources
.LOCK_ICON
));
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 private void setState(boolean state
) {
131 button_toggle
.setImage(ImageResources
.getImage(ImageResources
.LOCK_OPEN_ICON
));
134 button_toggle
.setImage(ImageResources
.getImage(ImageResources
.LOCK_ICON
));
136 text_cache
.setEnabled(state
);
142 public void setEnabled(boolean enabled
) {
143 text_cache
.setEnabled(enabled
);
144 button_toggle
.setEnabled(enabled
);
145 button_toggle
.setGrayed(enabled
);
146 button_toggle
.setSelection(enabled
);
147 String symbolicName
= enabled ? Resources
.COLOR_FONT_DEFAULT
: Resources
.COLOR_TEXT_DISABLED
;
148 text_cache
.setForeground(StoreUtil
.getColor(symbolicName
));
152 * @see eu.etaxonomy.taxeditor.ui.element.IEnableableFormElement#isEnabled()
155 public boolean isEnabled() {
156 return text_cache
.isEnabled();
160 * <p>Getter for the field <code>state</code>.</p>
164 public boolean getState(){
169 * @see org.eclipse.swt.events.SelectionListener#widgetSelected(org.eclipse.swt.events.SelectionEvent)
173 public void widgetSelected(SelectionEvent e
) {
174 setState(button_toggle
.getSelection());
175 for(SelectionListener listener
: selectionListener
){
176 listener
.widgetSelected(e
);
178 firePropertyChangeEvent(new CdmPropertyChangeEvent(this, e
));
182 * <p>addSelectionListener</p>
184 * @param listener a {@link org.eclipse.swt.events.SelectionListener} object.
186 public void addSelectionListener(SelectionListener listener
){
187 selectionListener
.add(listener
);
191 * <p>removeSelectionListener</p>
193 * @param listener a {@link org.eclipse.swt.events.SelectionListener} object.
195 public void removeSelectionListener(SelectionListener listener
){
196 selectionListener
.remove(listener
);
201 public void widgetDefaultSelected(SelectionEvent e
) {}
205 public void modifyText(ModifyEvent e
) {
206 firePropertyChangeEvent(new CdmPropertyChangeEvent(this, e
));
211 public void setIrrelevant(boolean irrelevant
) {
212 String colorId
= irrelevant ? Resources
.COLOR_COMPOSITE_IRRELEVANT
: Resources
.COLOR_COMPOSITE_BACKGROUND
;
214 Color color
= getColor(colorId
);
215 text_cache
.setBackground(color
);
220 public void setBackground(Color color
) {
221 label
.setBackground(color
);
222 container
.setBackground(color
);
226 public void setSelected(boolean selected
) {
227 setBackground(selected ? SELECTED
: getPersistentBackground());