fix #6101: add a check for multilanguage preference
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / ui / element / RepresentationElement.java
old mode 100644 (file)
new mode 100755 (executable)
index e784d8d..388f881
@@ -21,6 +21,8 @@ import java.util.Collections;
 import java.util.List;
 
 import org.eclipse.jface.util.PropertyChangeEvent;
+import org.eclipse.swt.SWT;
+import org.eclipse.swt.events.SelectionAdapter;
 import org.eclipse.swt.events.SelectionEvent;
 import org.eclipse.swt.events.SelectionListener;
 import org.eclipse.swt.widgets.Button;
@@ -30,10 +32,12 @@ import eu.etaxonomy.cdm.model.common.Representation;
 import eu.etaxonomy.cdm.model.common.TermBase;
 import eu.etaxonomy.cdm.model.common.TermType;
 import eu.etaxonomy.taxeditor.model.DefaultTermComparator;
+import eu.etaxonomy.taxeditor.model.ImageResources;
 import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
 import eu.etaxonomy.taxeditor.store.CdmStore;
 import eu.etaxonomy.taxeditor.ui.combo.TermComboElement;
 
+
 public class RepresentationElement  extends AbstractCdmFormElement implements
 SelectionListener{
 
@@ -44,11 +48,12 @@ SelectionListener{
         protected TextWithLabelElement element_text;
 
         protected Button button;
-        //protected Button deleteButton;
-
+       
         protected Representation selectedRepresentation;
         protected TermBase term;
 
+               private Button removeRepresentation;
+
        // protected final boolean isTranslationWizard;
 
 
@@ -133,13 +138,20 @@ SelectionListener{
 
         protected void createRepresentationEditingElements(
                 ICdmFormElement formElement, int style) {
-
+               removeRepresentation = formFactory.createButton(getLayoutComposite(), null,
+                               SWT.PUSH);
+               removeRepresentation.setImage(ImageResources
+                               .getImage(ImageResources.TRASH_ICON));
+               removeRepresentation.setToolTipText("Remove representation");
+               removeRepresentation.addSelectionListener(new DeleteListener(this));
+               addControl(removeRepresentation);
+               
             combo_language = formFactory.createDefinedTermComboElement(TermType.Language, formElement,
-                            "", null, true, style);
+                            "", null, false, style);
 
             combo_language.addSelectionListener(this);
-
-            addControl(formFactory.createLabel(getLayoutComposite(), ""));
+            
+            //addControl(formFactory.createLabel(getLayoutComposite(), ""));
        }
 
         public List<Language> getLanguages() {
@@ -174,7 +186,12 @@ SelectionListener{
                 element_Label.setText(selectedRepresentation.getLabel());
                 element_abbrevLabel.setText(selectedRepresentation.getAbbreviatedLabel());
                 element_text.setText(selectedRepresentation.getDescription());
-
+                
+            }else {
+                
+                element_Label.setEnabled(false);
+                 element_abbrevLabel.setEnabled(false);
+                 element_text.setEnabled(false);
             }
 
         }
@@ -204,12 +221,20 @@ SelectionListener{
             this.term = term;
             if (term.getRepresentations().isEmpty()){
                //if the term has no representation at all, create a default one.
-               Representation rep = Representation.NewInstance("", "", "", PreferencesUtil.getGlobalLanguage());
+               Representation rep = Representation.NewInstance("", "empty representation", "", PreferencesUtil.getGlobalLanguage());
                term.addRepresentation(rep);
             }
 
             if (selectedRepresentation != null) {
                combo_language.setTerms(getLanguages());
+               
+            } else{
+                element_Label.setEnabled(false);
+                 element_abbrevLabel.setEnabled(false);
+                 element_text.setEnabled(false);
+                 if (PreferencesUtil.isMultilanguageTextEditingCapability()) {
+                        removeRepresentation.setEnabled(false);
+                 }
             }
             if (update){
                updateControls();
@@ -229,6 +254,9 @@ SelectionListener{
             selectedRepresentation = preferredRepresentation;
             if (PreferencesUtil.isMultilanguageTextEditingCapability()) {
                 combo_language.setSelection(preferredRepresentation == null?CdmStore.getDefaultLanguage():preferredRepresentation.getLanguage());
+                boolean removePossible = term.getRepresentations().size() > 1 ;
+                removeRepresentation.setEnabled(removePossible);
+                
             }
         }
 
@@ -275,6 +303,20 @@ SelectionListener{
                 firePropertyChangeEvent(new CdmPropertyChangeEvent(this, event));
             } else if (eventSource == button){
                firePropertyChangeEvent(new CdmPropertyChangeEvent(this, event));
+            } else if (eventSource == combo_language){
+               if (combo_language.getSelection() != null){
+                       element_Label.setEnabled(true);
+                       element_abbrevLabel.setEnabled(true);
+                       element_text.setEnabled(true);
+                        boolean removePossible = term.getRepresentations().size() > 1 ;
+                     removeRepresentation.setEnabled(removePossible);
+               } else{
+                        element_Label.setEnabled(false);
+                     element_abbrevLabel.setEnabled(false);
+                     element_text.setEnabled(false);
+                     removeRepresentation.setEnabled(false);
+               }
+               firePropertyChangeEvent(new CdmPropertyChangeEvent(this, event));
             }
         }
 
@@ -294,6 +336,23 @@ SelectionListener{
             }
             this.selectedRepresentation = selectedRepresentation;
         }
+        
+        private class DeleteListener extends SelectionAdapter {
+
+               private final RepresentationElement element;
+
+               public DeleteListener(RepresentationElement element) {
+                       this.element = element;
+               }
+
+               @Override
+               public void widgetSelected(SelectionEvent e) {
+                       term.removeRepresentation(selectedRepresentation);
+                       firePropertyChangeEvent(new CdmPropertyChangeEvent(
+                                       this, e));
+                       updateControls();
+               }
+       }
     }