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
.preference
.Resources
;
30 import eu
.etaxonomy
.taxeditor
.store
.StoreUtil
;
31 import eu
.etaxonomy
.taxeditor
.ui
.campanula
.compatibility
.ICdmFormElement
;
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 Text text_cache
;
43 private Button button_toggle
;
45 private boolean state
;
46 private Set
<SelectionListener
> selectionListener
= new HashSet
<SelectionListener
>();
48 private 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
)
86 button_toggle
= formFactory
.createButton(container
, "Edit", SWT
.TOGGLE
);
87 addControl(button_toggle
);
88 button_toggle
.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 int caretPosition
= text_cache
.getCaretPosition();
103 text_cache
.removeModifyListener(this);
104 text_cache
.setText(text
);
105 text_cache
.addModifyListener(this);
107 // restore caret position
108 text_cache
.setSelection(caretPosition
);
115 * @return a {@link java.lang.String} object.
117 public String
getText(){
118 return text_cache
.getText();
122 * <p>Setter for the field <code>state</code>.</p>
124 * @param state a boolean.
126 public void setState(boolean state
) {
132 public void setEnabled(boolean enabled
) {
133 text_cache
.setEnabled(enabled
);
134 String symbolicName
= enabled ? Resources
.COLOR_FONT_DEFAULT
: Resources
.COLOR_TEXT_DISABLED
;
135 text_cache
.setForeground(StoreUtil
.getColor(symbolicName
));
136 button_toggle
.setSelection(enabled
);
140 * <p>Getter for the field <code>state</code>.</p>
144 public boolean getState(){
149 * @see org.eclipse.swt.events.SelectionListener#widgetSelected(org.eclipse.swt.events.SelectionEvent)
152 public void widgetSelected(SelectionEvent e
) {
153 setState(button_toggle
.getSelection());
154 for(SelectionListener listener
: selectionListener
){
155 listener
.widgetSelected(e
);
157 firePropertyChangeEvent(new CdmPropertyChangeEvent(this, e
));
161 * <p>addSelectionListener</p>
163 * @param listener a {@link org.eclipse.swt.events.SelectionListener} object.
165 public void addSelectionListener(SelectionListener listener
){
166 selectionListener
.add(listener
);
170 * <p>removeSelectionListener</p>
172 * @param listener a {@link org.eclipse.swt.events.SelectionListener} object.
174 public void removeSelectionListener(SelectionListener listener
){
175 selectionListener
.remove(listener
);
179 public void widgetDefaultSelected(SelectionEvent e
) {}
182 public void modifyText(ModifyEvent e
) {
183 firePropertyChangeEvent(new CdmPropertyChangeEvent(this, e
));
187 public void setIrrelevant(boolean irrelevant
) {
188 String colorId
= irrelevant ? Resources
.COLOR_COMPOSITE_IRRELEVANT
: Resources
.COLOR_COMPOSITE_BACKGROUND
;
190 Color color
= getColor(colorId
);
191 text_cache
.setBackground(color
);
196 public void setBackground(Color color
) {
197 label
.setBackground(color
);
198 container
.setBackground(color
);
202 public void setSelected(boolean selected
) {
203 setBackground(selected ? SELECTED
: getPersistentBackground());