Creating a YearValidator. Validator added to Descriptor - in case of invalid data...
authorp.ciardelli <p.ciardelli@localhost>
Mon, 10 Nov 2008 12:52:34 +0000 (12:52 +0000)
committerp.ciardelli <p.ciardelli@localhost>
Mon, 10 Nov 2008 12:52:34 +0000 (12:52 +0000)
.gitattributes
eclipseprojects/eu.etaxonomy.taxeditor/src/eu/etaxonomy/taxeditor/propertysheet/YearValidator.java [new file with mode: 0644]
eclipseprojects/eu.etaxonomy.taxeditor/src/eu/etaxonomy/taxeditor/propertysheet/bibref/BibRefPropertySource.java

index c59e0e70664567fbbe64642978138c41c7b35525..17329b31882ae8f986ebff533af0438b8fbbb92f 100644 (file)
@@ -522,6 +522,7 @@ eclipseprojects/eu.etaxonomy.taxeditor/src/eu/etaxonomy/taxeditor/propertysheet/
 eclipseprojects/eu.etaxonomy.taxeditor/src/eu/etaxonomy/taxeditor/propertysheet/PropertySheetValueLabelProvider.java -text
 eclipseprojects/eu.etaxonomy.taxeditor/src/eu/etaxonomy/taxeditor/propertysheet/PropertySourceAdapter.java -text
 eclipseprojects/eu.etaxonomy.taxeditor/src/eu/etaxonomy/taxeditor/propertysheet/SourceViewerConfig.java -text
+eclipseprojects/eu.etaxonomy.taxeditor/src/eu/etaxonomy/taxeditor/propertysheet/YearValidator.java -text
 eclipseprojects/eu.etaxonomy.taxeditor/src/eu/etaxonomy/taxeditor/propertysheet/bibref/BibRefPropertySource.java -text
 eclipseprojects/eu.etaxonomy.taxeditor/src/eu/etaxonomy/taxeditor/propertysheet/description/DescriptionElementPropertySource.java -text
 eclipseprojects/eu.etaxonomy.taxeditor/src/eu/etaxonomy/taxeditor/propertysheet/description/TaxonDescriptionPropertySource.java -text
diff --git a/eclipseprojects/eu.etaxonomy.taxeditor/src/eu/etaxonomy/taxeditor/propertysheet/YearValidator.java b/eclipseprojects/eu.etaxonomy.taxeditor/src/eu/etaxonomy/taxeditor/propertysheet/YearValidator.java
new file mode 100644 (file)
index 0000000..6c7535a
--- /dev/null
@@ -0,0 +1,43 @@
+/**\r
+* Copyright (C) 2007 EDIT\r
+* European Distributed Institute of Taxonomy \r
+* http://www.e-taxonomy.eu\r
+* \r
+* The contents of this file are subject to the Mozilla Public License Version 1.1\r
+* See LICENSE.TXT at the top of this package for the full license terms.\r
+*/\r
+\r
+package eu.etaxonomy.taxeditor.propertysheet;\r
+\r
+import org.apache.log4j.Logger;\r
+import org.eclipse.jface.viewers.ICellEditorValidator;\r
+\r
+import eu.etaxonomy.cdm.model.common.TimePeriod;\r
+import eu.etaxonomy.cdm.model.reference.BibtexReference;\r
+import eu.etaxonomy.cdm.model.reference.StrictReferenceBase;\r
+import eu.etaxonomy.taxeditor.model.CdmUtil;\r
+\r
+/**\r
+ * @author p.ciardelli\r
+ * @created 10.11.2008\r
+ * @version 1.0\r
+ */\r
+public class YearValidator implements ICellEditorValidator {\r
+       private static final Logger logger = Logger.getLogger(YearValidator.class);\r
+\r
+       /* (non-Javadoc)\r
+        * @see org.eclipse.jface.viewers.ICellEditorValidator#isValid(java.lang.Object)\r
+        */\r
+       public String isValid(Object value) {\r
+               String year = (String) value;\r
+               if (year.equals("")) {\r
+                       return null;\r
+               }\r
+               try {\r
+                       TimePeriod datePublished = CdmUtil.getDatePublished(year);\r
+               } catch (NumberFormatException e) {\r
+                       return "Year must be between 1750 and 2030. Two years can also be given in the format 'xxxx-xxxx'.";\r
+               }\r
+               return null;\r
+       }\r
+}
\ No newline at end of file
index a9db25cb049cbce35e3a964a217c50a3041f1efa..6385448b56743113ff63ed1ce61f9a4ac6531426 100644 (file)
@@ -22,8 +22,6 @@ import java.util.List;
 import java.util.Vector;\r
 \r
 import org.apache.log4j.Logger;\r
-import org.eclipse.jface.dialogs.MessageDialog;\r
-import org.eclipse.jface.viewers.ICellEditorValidator;\r
 import org.eclipse.ui.views.properties.ComboBoxPropertyDescriptor;\r
 import org.eclipse.ui.views.properties.IPropertyDescriptor;\r
 import org.eclipse.ui.views.properties.IPropertySource;\r
@@ -57,9 +55,9 @@ import eu.etaxonomy.cdm.model.reference.StrictReferenceBase;
 import eu.etaxonomy.cdm.model.reference.Thesis;\r
 import eu.etaxonomy.cdm.model.reference.WebPage;\r
 import eu.etaxonomy.taxeditor.ITaxEditorConstants;\r
-import eu.etaxonomy.taxeditor.UiUtil;\r
 import eu.etaxonomy.taxeditor.model.CdmUtil;\r
 import eu.etaxonomy.taxeditor.propertysheet.CheckboxPropertyDescriptor;\r
+import eu.etaxonomy.taxeditor.propertysheet.YearValidator;\r
 \r
 /**\r
  * Supplies the properties of a <code>ReferenceBase</code> object for display by\r
@@ -388,31 +386,12 @@ public class BibRefPropertySource implements IPropertySource {
                }\r
                if (id.equals(P_ID_YEAR)) {\r
                        yearDescriptor = new TextPropertyDescriptor(P_ID_YEAR, P_YEAR);\r
-                       yearDescriptor.setValidator(new ICellEditorValidator() {\r
-\r
-                               public String isValid(Object value) {\r
-                                       String year = (String) value;\r
-                                       logger.warn(year);\r
-                                       if (year.equals("")) {\r
-                                               return null;\r
-                                       }\r
-                                       if (reference instanceof BibtexReference) {\r
-                                               return null;\r
-                                       }\r
-                                       if (reference instanceof StrictReferenceBase) {\r
-                                               try {\r
-                                                       TimePeriod datePublished = CdmUtil.getDatePublished(year);\r
-                                               } catch (NumberFormatException e) {\r
-                                                       return "Year must be between 1750 and 2030.\n\nTwo years can also be given in the format 'xxxx-xxxx'.";\r
-                                               }\r
-                                       }\r
-                                       return null;\r
-                               }\r
-                               \r
-                       });\r
-                       descriptors.addElement(yearDescriptor);\r
-//                     descriptors.addElement(\r
-//                                     new TextPropertyDescriptor(P_ID_YEAR, P_YEAR));         \r
+                       \r
+                       // BibtexReference.setYear() takes a String, no need for validation\r
+                       if (!(reference instanceof BibtexReference)) {\r
+                               yearDescriptor.setValidator(new YearValidator());\r
+                       }\r
+                       descriptors.addElement(yearDescriptor);         \r
                }\r
                if (id.equals(P_ID_CITATION)) {\r
                        descriptors.addElement(\r
@@ -591,7 +570,7 @@ public class BibRefPropertySource implements IPropertySource {
                        });\r
                        return bibRefPropertySource;\r
                }\r
-               return null;\r
+               return "";\r
        }\r
 \r
        /**\r
@@ -706,22 +685,9 @@ public class BibRefPropertySource implements IPropertySource {
                                                datePublished = CdmUtil.getDatePublished(year);                                         \r
                                        }\r
                                        ((StrictReferenceBase) reference).setDatePublished(datePublished);\r
-                               } catch (NumberFormatException e) {     \r
-                                       \r
-                                       // HACK to deal with the following sequence of events:\r
-                                       //      1) focus moved to another cell\r
-                                       //      2) Year cell is notified, calls setPropertyValue\r
-                                       //      3) setPropertyValue opens MessageDialog, and does not complete until user feedback\r
-                                       //      4) the new cell checks whether there is another active cell in PropertySheetViewer\r
-                                       //      5) since the MessageDialog is open, Year cell is still "active" in PropertySheetViewer\r
-                                       //      6) therefore, the new cell tries to close the Year cell, in the process calling setPropertyViewer \r
-                                       //              again and causing MessageDialog to pop up again\r
-//                                     if (!isMessageDialogOpen) {\r
-//                                             isMessageDialogOpen = true;\r
-//                                             MessageDialog.openError(UiUtil.getShell(), "Invalid year format", "Year must be between 1750 and 2030.\n\nTwo years can also be given in the format 'xxxx-xxxx'.");\r
-//                                             isMessageDialogOpen = false;\r
-//                                     }\r
-                                       return;\r
+                               } catch (NumberFormatException e) {\r
+                                       // Validator should prevent this from ever happening ...\r
+                                       return; \r
                                }\r
                        }\r
                }\r