Sync. w Niels
[taxeditor.git] / eclipseprojects / eu.etaxonomy.taxeditor / src / eu / etaxonomy / taxeditor / propertysheet / bibref / BibRefPropertySource.java
index cc585d09ca1499c02ee2b2d6dfbdb096e9a262c4..7f51c51df11094f7b9aaaf77cc175f3613c696a9 100644 (file)
@@ -29,6 +29,8 @@ import org.eclipse.ui.views.properties.PropertyDescriptor;
 import org.eclipse.ui.views.properties.TextPropertyDescriptor;\r
 \r
 import eu.etaxonomy.cdm.common.CdmUtils;\r
+import eu.etaxonomy.cdm.model.agent.Team;\r
+import eu.etaxonomy.cdm.model.agent.TeamOrPersonBase;\r
 import eu.etaxonomy.cdm.model.reference.Article;\r
 import eu.etaxonomy.cdm.model.reference.BibtexReference;\r
 import eu.etaxonomy.cdm.model.reference.Book;\r
@@ -138,7 +140,7 @@ public class BibRefPropertySource implements IPropertySource {
        \r
     // Property display keys\r
        public static final String P_REFERENCETYPE = "00:Reference Type";\r
-       public static final String P_AUTHORTEAM = "01:Author Team";\r
+       public static final String P_AUTHORTEAM = "01:Author Team (Cache)";\r
        public static final String P_YEAR = "02:Year";\r
        public static final String P_CITATION = "03:Citation";\r
        public static final String P_URI = "04:URI";\r
@@ -178,7 +180,6 @@ public class BibRefPropertySource implements IPropertySource {
        public static final String P_CROSSREF = "38:BibTeX Crossref";\r
        public static final String P_NOMREFBASE = "39:Nomenclatural Citation";          \r
        \r
-               \r
        private static HashMap<Class, String> referenceTypeMap = null;\r
        \r
        private void populateReferenceTypes() {\r
@@ -212,8 +213,7 @@ public class BibRefPropertySource implements IPropertySource {
                // Default type of ReferenceBase is Generic\r
                if (reference == null) {\r
                        reference = Generic.NewInstance();\r
-               }\r
-               \r
+               }               \r
                this.reference = reference;\r
 \r
                List<String> displayFields = new ArrayList<String>();\r
@@ -271,7 +271,6 @@ public class BibRefPropertySource implements IPropertySource {
                        }\r
 \r
                        if (referenceClass == Generic.class) {\r
-                               displayFields.add(P_ID_INJOURNAL);\r
                                displayFields.add(P_ID_NOMENCLATURAL_CITATION);\r
                                displayFields.add(P_ID_PAGES);\r
                                displayFields.add(P_ID_SERIES);\r
@@ -495,6 +494,8 @@ public class BibRefPropertySource implements IPropertySource {
         */\r
        @Override\r
        public Object getPropertyValue(Object id) {\r
+               \r
+               // Reference Type: get integer index of drop-down menu\r
                if (id.equals(P_ID_REFERENCETYPE)) {\r
                        Class referenceClass = reference.getClass();\r
                        int menuIndex = 0;\r
@@ -506,6 +507,100 @@ public class BibRefPropertySource implements IPropertySource {
                        }\r
                        return 0;\r
                }\r
+\r
+               // Author Team: for now, just edit the cache\r
+               // TODO AuthorTeamPropertySource\r
+               if (id.equals(P_ID_AUTHORTEAM)) {\r
+                       TeamOrPersonBase authorTeam = reference.getAuthorTeam();\r
+                       \r
+                       if (authorTeam == null) {\r
+                               return "";\r
+                       }\r
+                       \r
+                       return CdmUtils.Nz(authorTeam.getTitleCache());\r
+               }\r
+               \r
+               // Year\r
+               if (id.equals(P_ID_YEAR)) {\r
+                       return CdmUtils.Nz(reference.getYear());\r
+               }\r
+               \r
+               // Title: used by StrictReferenceBase\r
+               if (id.equals(P_ID_TITLE)) {\r
+                       String title = (String) invokeMethod(reference, "getTitle", null);\r
+                       return CdmUtils.Nz(title);\r
+               }\r
+               \r
+               // InJournal: return an instance of BibRefPropertySource\r
+               if (id.equals(P_ID_INJOURNAL)) {\r
+                               \r
+                       Journal inJournal = null;\r
+                       try {\r
+                               inJournal = ((Article) reference).getInJournal();\r
+                       } catch (ClassCastException e) {\r
+                               // Leave method on class cast error\r
+                               logger.error(e.getMessage());\r
+                               return null;\r
+                       }\r
+                       \r
+                       if (inJournal == null) {\r
+                               inJournal = Journal.NewInstance();\r
+                       }\r
+                       \r
+                       BibRefPropertySource bibRefPropertySource = new BibRefPropertySource(inJournal);\r
+                       bibRefPropertySource.addPropertyChangeListener(new PropertyChangeListener() {\r
+                               @Override\r
+                               public void propertyChange(PropertyChangeEvent evt) {\r
+                                       if (reference instanceof Article && evt.getNewValue() instanceof Journal) {     \r
+                                               ((Article) reference).setInJournal((Journal) evt.getNewValue());\r
+                                       }\r
+                               }\r
+                       });\r
+                       return bibRefPropertySource;\r
+               }\r
+               return null;\r
+       }\r
+\r
+       /**\r
+        * Executes <code>methodName</code> on <code>obj</code> with\r
+        * the arguments <code>args</code>.\r
+        * <p>\r
+        * On failure, returns <code>null</code>. \r
+        * @param obj\r
+        * @param methodName\r
+        * @param args\r
+        * @return\r
+        */\r
+       private Object invokeMethod(Object obj, String methodName, Object... args) {\r
+               try {\r
+                       if (args != null) {\r
+                               logger.warn(args.length);\r
+                               for (Object arg : args) {\r
+                                       logger.warn("ARG " + arg.toString());\r
+                               }\r
+                       }\r
+                       Method method = null;\r
+                       if (methodName.equals("setTitle"))\r
+                               method= obj.getClass().getMethod(methodName, String.class);\r
+                       else\r
+                               method = obj.getClass().getMethod(methodName, null);\r
+                       return method.invoke(obj, args);\r
+               } catch (SecurityException e) {\r
+                       logger.error(e.getMessage());\r
+               } catch (IllegalArgumentException e) {\r
+                       logger.error(e .getMessage());\r
+               } catch (NoSuchMethodException e) {\r
+                       logger.error(e.getMessage());\r
+               } catch (IllegalAccessException e) {\r
+                       logger.error(e.getMessage());\r
+               } catch (InvocationTargetException e) {\r
+                       logger.error(e.getMessage());\r
+               }\r
+               return null;\r
+       }\r
+\r
+       private Object createBibRefPropertySource(Journal inJournal) {\r
+               // TODO Auto-generated method stub\r
                return null;\r
        }\r
 \r
@@ -529,28 +624,65 @@ public class BibRefPropertySource implements IPropertySource {
        @Override\r
        public void setPropertyValue(Object id, Object value) {\r
                \r
+               // Reference Type\r
                if (id.equals(P_ID_REFERENCETYPE)) {\r
                        \r
                        // NOTE: this code will only be reached if user actually changes choice in reference type\r
                        //      dropdown list, not if focus is merely gained and lost without changing the value\r
                        \r
-                       // Guard against class cast error\r
-                       if (!( value instanceof Integer)) {\r
-                               return;\r
-                       }\r
-                       \r
                        // Grab reference's title cache\r
                        String oldTitleCache = reference.getTitleCache();\r
                        \r
                        // Get reference's class\r
-                       int menuIndex = ((Integer) value).intValue();                   \r
+                       int menuIndex;\r
+                       try {\r
+                               menuIndex = ((Integer) value).intValue();\r
+                       } catch (ClassCastException e) {\r
+                               // Leave method / no change on class cast error\r
+                               logger.error(e.getMessage());\r
+                               return;\r
+                       }                       \r
                        Class referenceClass = (Class)(referenceTypeMap.keySet().toArray())[menuIndex];\r
                        \r
                        // Make a new reference using the class\r
                        reference = makeNewReference(referenceClass);\r
                        \r
                        // Set new reference's title cache\r
-                       reference.setTitleCache(oldTitleCache);                 \r
+                       reference.setTitleCache(oldTitleCache);\r
+               }\r
+               \r
+               // Author Team\r
+               if (id.equals(P_ID_AUTHORTEAM)) {\r
+                       TeamOrPersonBase authorTeam = reference.getAuthorTeam();\r
+                       if (authorTeam == null) {\r
+                               authorTeam = Team.NewInstance();\r
+                               reference.setAuthorTeam(authorTeam);\r
+                       }\r
+                       try {\r
+                               authorTeam.setTitleCache((String) value);\r
+                       } catch (ClassCastException e) {\r
+                               // Leave method / no change on class cast error\r
+                               logger.error(e.getMessage());\r
+                               return;\r
+                       }\r
+               }\r
+               \r
+               // Year\r
+               if (id.equals(P_ID_YEAR)) {\r
+//                     reference.getYear\r
+               }\r
+\r
+               // Title: used by StrictReferenceBase\r
+               if (id.equals(P_ID_TITLE)) {\r
+                       String title = null;\r
+                       try {\r
+                               title = (String) value;\r
+                       } catch (ClassCastException e) {\r
+                               // Leave method / no change on class cast error\r
+                               logger.error(e.getMessage());\r
+                               return;\r
+                       }\r
+                       invokeMethod(reference, "setTitle", title);\r
                }\r
                \r
                propertyChangeSupport.firePropertyChange(ITaxEditorConstants.PROPERTY_SHEET_CHANGE, null, reference);           \r