- added support for term retrieval by TermVocabulary
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / ui / element / ToggleableTextElement.java
index 5054fa74918693da3685935ad30e239d8a879a39..0d1c7be8c4ebed2a77a28a05777485ab7e9a7924 100644 (file)
@@ -1,9 +1,9 @@
 // $Id$
 /**
 * Copyright (C) 2007 EDIT
-* European Distributed Institute of Taxonomy 
+* European Distributed Institute of Taxonomy
 * http://www.e-taxonomy.eu
-* 
+*
 * The contents of this file are subject to the Mozilla Public License Version 1.1
 * See LICENSE.TXT at the top of this package for the full license terms.
 */
@@ -26,9 +26,9 @@ import org.eclipse.swt.widgets.Composite;
 import org.eclipse.swt.widgets.Label;
 import org.eclipse.swt.widgets.Text;
 
+import eu.etaxonomy.taxeditor.model.ImageResources;
 import eu.etaxonomy.taxeditor.preference.Resources;
 import eu.etaxonomy.taxeditor.store.StoreUtil;
-import eu.etaxonomy.taxeditor.ui.campanula.compatibility.ICdmFormElement;
 
 /**
  * When the button is pressed, this textfield may be edited.
@@ -39,14 +39,14 @@ import eu.etaxonomy.taxeditor.ui.campanula.compatibility.ICdmFormElement;
  */
 public class ToggleableTextElement extends AbstractCdmFormElement implements SelectionListener, ModifyListener, IEnableableFormElement, ISelectable {
 
-       private Text text_cache;
-       private Button button_toggle;
-       
+       private final Text text_cache;
+       private final Button button_toggle;
+
        private boolean state;
-       private Set<SelectionListener> selectionListener = new HashSet<SelectionListener>();
-       private Label label;
-       private Composite container;
-       
+       private final Set<SelectionListener> selectionListener = new HashSet<SelectionListener>();
+       private final Label label;
+       private final Composite container;
+
        /**
         * <p>Constructor for ToggleableTextElement.</p>
         *
@@ -60,15 +60,15 @@ public class ToggleableTextElement extends AbstractCdmFormElement implements Sel
        protected ToggleableTextElement(CdmFormFactory formFactory, ICdmFormElement parentElement,
                        String labelString, String initialText, boolean initialState, int style) {
                super(formFactory, parentElement);
-               
+
                label = formFactory.createLabel(getLayoutComposite(), labelString, style);
                addControl(label);
-               
-               container = formFactory.createComposite(getLayoutComposite(), SWT.WRAP);                
+
+               container = formFactory.createComposite(getLayoutComposite(), SWT.WRAP);
                container.setLayout(LayoutConstants.LAYOUT(2, false));
                container.setLayoutData(LayoutConstants.FILL_HORIZONTALLY());
                addControl(container);
-               
+
                text_cache = formFactory.createText(container, initialText, SWT.WRAP | SWT.MULTI);
                addControl(text_cache);
                text_cache.addModifyListener(this);
@@ -78,15 +78,18 @@ public class ToggleableTextElement extends AbstractCdmFormElement implements Sel
                text_cache.addKeyListener( new KeyAdapter(){
                        @Override
                        public void keyPressed(KeyEvent e) {
-                       if(e.character == SWT.CR)
-                               e.doit = false;
+                       if(e.character == SWT.CR) {
+                e.doit = false;
+            }
                        }
                });
-               
+
                button_toggle = formFactory.createButton(container, "Edit", SWT.TOGGLE);
+               button_toggle.setText("");
+               button_toggle.setImage(ImageResources.getImage(ImageResources.LOCK_ICON));
                addControl(button_toggle);
                button_toggle.addSelectionListener(this);
-               
+
                setState(initialState);
        }
 
@@ -99,7 +102,7 @@ public class ToggleableTextElement extends AbstractCdmFormElement implements Sel
                if(text != null){
                        // store current caret position
                        int caretPosition = text_cache.getCaretPosition();
-                       
+
                        text_cache.removeModifyListener(this);
                        text_cache.setText(text);
                        text_cache.addModifyListener(this);
@@ -117,23 +120,40 @@ public class ToggleableTextElement extends AbstractCdmFormElement implements Sel
        public String getText(){
                return text_cache.getText();
        }
-       
+
        /**
         * <p>Setter for the field <code>state</code>.</p>
         *
         * @param state a boolean.
         */
-       public void setState(boolean state) {
+       private void setState(boolean state) {
+           if(state){
+               button_toggle.setImage(ImageResources.getImage(ImageResources.LOCK_OPEN_ICON));
+           }
+           else{
+               button_toggle.setImage(ImageResources.getImage(ImageResources.LOCK_ICON));
+           }
+           text_cache.setEnabled(state);
                this.state = state;
-               setEnabled(state);
        }
-       
+
        /** {@inheritDoc} */
-       public void setEnabled(boolean enabled) {
+       @Override
+    public void setEnabled(boolean enabled) {
                text_cache.setEnabled(enabled);
+        button_toggle.setEnabled(enabled);
+               button_toggle.setGrayed(enabled);
+               button_toggle.setSelection(enabled);
                String symbolicName = enabled ? Resources.COLOR_FONT_DEFAULT : Resources.COLOR_TEXT_DISABLED;
                text_cache.setForeground(StoreUtil.getColor(symbolicName));
-               button_toggle.setSelection(enabled);
+       }
+
+       /* (non-Javadoc)
+        * @see eu.etaxonomy.taxeditor.ui.element.IEnableableFormElement#isEnabled()
+        */
+       @Override
+       public boolean isEnabled() {
+           return text_cache.isEnabled();
        }
 
        /**
@@ -144,19 +164,20 @@ public class ToggleableTextElement extends AbstractCdmFormElement implements Sel
        public boolean getState(){
                return state;
        }
-       
+
        /* (non-Javadoc)
         * @see org.eclipse.swt.events.SelectionListener#widgetSelected(org.eclipse.swt.events.SelectionEvent)
         */
        /** {@inheritDoc} */
-       public void widgetSelected(SelectionEvent e) {
+       @Override
+    public void widgetSelected(SelectionEvent e) {
                setState(button_toggle.getSelection());
                for(SelectionListener listener : selectionListener){
                        listener.widgetSelected(e);
                }
                firePropertyChangeEvent(new CdmPropertyChangeEvent(this, e));
        }
-       
+
        /**
         * <p>addSelectionListener</p>
         *
@@ -165,7 +186,7 @@ public class ToggleableTextElement extends AbstractCdmFormElement implements Sel
        public void addSelectionListener(SelectionListener listener){
                selectionListener.add(listener);
        }
-       
+
        /**
         * <p>removeSelectionListener</p>
         *
@@ -174,30 +195,33 @@ public class ToggleableTextElement extends AbstractCdmFormElement implements Sel
        public void removeSelectionListener(SelectionListener listener){
                selectionListener.remove(listener);
        }
-       
+
        /** {@inheritDoc} */
-       public void widgetDefaultSelected(SelectionEvent e) {}
+       @Override
+    public void widgetDefaultSelected(SelectionEvent e) {}
 
        /** {@inheritDoc} */
-       public void modifyText(ModifyEvent e) {
+       @Override
+    public void modifyText(ModifyEvent e) {
                firePropertyChangeEvent(new CdmPropertyChangeEvent(this, e));
        }
 
        /** {@inheritDoc} */
-       public void setIrrelevant(boolean irrelevant) {
+       @Override
+    public void setIrrelevant(boolean irrelevant) {
                String colorId = irrelevant ? Resources.COLOR_COMPOSITE_IRRELEVANT : Resources.COLOR_COMPOSITE_BACKGROUND;
-               
+
                Color color = getColor(colorId);
                text_cache.setBackground(color);
        }
-       
+
        /** {@inheritDoc} */
        @Override
        public void setBackground(Color color) {
                label.setBackground(color);
                container.setBackground(color);
        }
-       
+
        @Override
        public void setSelected(boolean selected) {
                setBackground(selected ? SELECTED : getPersistentBackground());