Disable Termeditor menu if CdmStore is disconnected
authorPatrick Plitzner <p.plitzner@bgbm.org>
Tue, 21 Jul 2015 08:54:16 +0000 (10:54 +0200)
committerPatrick Plitzner <p.plitzner@bgbm.org>
Tue, 21 Jul 2015 10:38:51 +0000 (12:38 +0200)
eu.etaxonomy.taxeditor.store/plugin.xml
eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/editor/definedterm/DefinedTermMenu.java [new file with mode: 0644]

index 85871d6ad15fdc11ed223da8bdb2b3ce372efd75..3b026cdf1defab4f62980e29d838b4e9da7d8d98 100644 (file)
             </command>
          </menu>
       </menuContribution>
             </command>
          </menu>
       </menuContribution>
-      <menuContribution
-            class="eu.etaxonomy.taxeditor.editor.definedterm.DefinedTermMenuFactory"
-            locationURI="menu:org.eclipse.ui.main.menu.window?before=eu.etaxonomy.taxeditor.application.windowMenu.last">
-      </menuContribution>
       <menuContribution
             locationURI="menu:org.eclipse.ui.main.menu.file?after=eu.etaxonomy.taxeditor.application.filemenu.io">
          <command
       <menuContribution
             locationURI="menu:org.eclipse.ui.main.menu.file?after=eu.etaxonomy.taxeditor.application.filemenu.io">
          <command
             </visibleWhen>
          </command>
       </menuContribution>
             </visibleWhen>
          </command>
       </menuContribution>
+      <menuContribution
+            allPopups="false"
+            locationURI="menu:org.eclipse.ui.main.menu.window?before=eu.etaxonomy.taxeditor.application.windowMenu.last">
+         <dynamic
+               class="eu.etaxonomy.taxeditor.editor.definedterm.DefinedTermMenu"
+               id="eu.etaxonomy.taxeditor.editor.definedterm.DefinedTermMenu">
+            <visibleWhen
+                  checkEnabled="true">
+               <reference
+                     definitionId="isCdmStoreConnected">
+               </reference>
+            </visibleWhen>
+         </dynamic>
+      </menuContribution>
    </extension>
    <extension
          point="org.eclipse.ui.handlers">
    </extension>
    <extension
          point="org.eclipse.ui.handlers">
diff --git a/eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/editor/definedterm/DefinedTermMenu.java b/eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/editor/definedterm/DefinedTermMenu.java
new file mode 100644 (file)
index 0000000..4f3e777
--- /dev/null
@@ -0,0 +1,164 @@
+// $Id$
+/**
+* Copyright (C) 2009 EDIT
+* 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.
+*/
+
+package eu.etaxonomy.taxeditor.editor.definedterm;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.Comparator;
+import java.util.EnumSet;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+import org.eclipse.jface.action.IContributionItem;
+import org.eclipse.jface.action.MenuManager;
+import org.eclipse.jface.action.Separator;
+import org.eclipse.swt.SWT;
+import org.eclipse.ui.PlatformUI;
+import org.eclipse.ui.actions.CompoundContributionItem;
+import org.eclipse.ui.menus.CommandContributionItem;
+import org.eclipse.ui.menus.CommandContributionItemParameter;
+
+import eu.etaxonomy.cdm.model.common.TermType;
+
+/**
+ * Menu used in the store plugin xml to dynamically generate menu (sub-menu) contribution items
+ * for term types which when clicked open the defined term editor for the chosen term type
+ *
+ * @author pplitzner
+ * @date 21 Jul 2015
+ *
+ */
+
+public class DefinedTermMenu extends CompoundContributionItem {
+
+
+    @Override
+    protected IContributionItem[] getContributionItems() {
+        Collection<IContributionItem> items = new ArrayList<IContributionItem>();
+           MenuManager dtMenuManager =
+                   new MenuManager("Term Editor","eu.etaxonomy.taxeditor.store.definedTermEditorMenu");
+
+           dtMenuManager.setVisible(true);
+
+           items.add(dtMenuManager);
+           List<TermType> ttList = new ArrayList<TermType>(EnumSet.allOf(TermType.class));
+           Collections.sort(ttList,new SortByTermTypeMessage());
+           for (TermType tt : ttList)
+           {
+               // if term type has a parent, do not add it
+               // it will be added in the recursive call
+               if(tt.getKindOf() == null) {
+                   IContributionItem ici = addChildTermsToMenuManager(tt);
+                   if(ici != null) {
+                       dtMenuManager.add(ici);
+                   }
+               }
+           }
+           return items.toArray(new IContributionItem[]{});
+       }
+
+       private IContributionItem addChildTermsToMenuManager(TermType termType) {
+
+               //FIXME : need a better way to find out if a term type can be editable (ticket 3853)
+               if(termType.getEmptyDefinedTermBase() != null) {
+                       Set<TermType> children = termType.getGeneralizationOf();
+                       // term type has no children, so create menu item
+                       if(children.isEmpty()) {
+                               return createMenuItem(termType);
+                       }
+                       // term type has children, so create sub menu
+                       MenuManager dtMenuManager =
+                                       new MenuManager(termType.getMessage(),"eu.etaxonomy.taxeditor.store." + termType.getKey() + "Menu");
+                       dtMenuManager.setVisible(true);
+                       dtMenuManager.add(createDefaultMenuItem(termType));
+
+                       Separator sep = new Separator();
+                       dtMenuManager.add(sep);
+                       // add child items to the sub menu
+                       for(TermType tt : children) {
+                               IContributionItem item = addChildTermsToMenuManager(tt);
+                               if(item != null) {
+                                       dtMenuManager.add(item);
+                               }
+                       }
+                       return dtMenuManager;
+               } else {
+                       return null;
+               }
+
+       }
+
+       private CommandContributionItem createMenuItem(TermType termType) {
+
+               Map<String, String> params = new HashMap<String, String>();
+               params.put("eu.etaxonomy.taxeditor.store.openDefinedTermEditor.termTypeUuid",
+                               termType.getUuid().toString());
+
+        CommandContributionItemParameter p = new CommandContributionItemParameter(
+                PlatformUI.getWorkbench(),
+                "",
+                "eu.etaxonomy.taxeditor.store.openDefinedTermEditor",
+                params,
+                null,
+                null,
+                null,
+                termType.getMessage(),
+                "",
+                "",
+                SWT.PUSH,
+                "",
+                true);
+
+        CommandContributionItem item = new CommandContributionItem(p);
+        return item;
+
+       }
+
+       private CommandContributionItem createDefaultMenuItem(TermType termType) {
+
+               Map<String, String> params = new HashMap<String, String>();
+               params.put("eu.etaxonomy.taxeditor.store.openDefinedTermEditor.termTypeUuid",
+                               termType.getUuid().toString());
+
+        CommandContributionItemParameter p = new CommandContributionItemParameter(
+                PlatformUI.getWorkbench(),
+                "",
+                "eu.etaxonomy.taxeditor.store.openDefinedTermEditor",
+                params,
+                null,
+                null,
+                null,
+                "Other " + termType.getMessage() + "s",
+                "",
+                "",
+                SWT.PUSH,
+                "",
+                true);
+
+
+
+        CommandContributionItem item = new CommandContributionItem(p);
+        return item;
+
+       }
+
+       private class SortByTermTypeMessage implements Comparator<TermType> {
+           @Override
+        public int compare(TermType t1, TermType t2) {
+               return t1.getMessage().compareTo(t2.getMessage());
+           }
+       }
+
+
+}