ref #7095 Add functionality to toggle between tree and flat list
authorPatrick Plitzner <p.plitzner@bgbm.org>
Thu, 25 Jan 2018 14:10:28 +0000 (15:10 +0100)
committerPatrick Plitzner <p.plitzner@bgbm.org>
Thu, 25 Jan 2018 18:11:20 +0000 (19:11 +0100)
eu.etaxonomy.taxeditor.editor/src/main/java/eu/etaxonomy/taxeditor/editor/EditorUtil.java
eu.etaxonomy.taxeditor.editor/src/main/java/eu/etaxonomy/taxeditor/editor/workingSet/matrix/CharacterMatrix.java
eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/model/ImageResources.java

index 6df572b6d592830371bdee916a1ca2c7c7447f80..efa974673a9ddd5a6b6d533d80b7178b7882798b 100644 (file)
@@ -96,7 +96,7 @@ public class EditorUtil extends AbstractUtility {
         }
         part = partService.showPart(part, PartState.ACTIVATE);
         CharacterMatrix editor = (CharacterMatrix) part.getObject();
-        editor.init(workingSetUuid);
+        editor.init(workingSetUuid, true);
     }
 
     public static void openSpecimenEditor(DerivateViewEditorInput input, EModelService modelService, EPartService partService, MApplication application){
@@ -357,7 +357,7 @@ public class EditorUtil extends AbstractUtility {
             }
         }
     }
-    
+
     public static void updateEditor(TaxonNode taxonNode, EPartService partService, MApplication application){
         String treeIndex = taxonNode.treeIndex();
         Collection<MPart> parts = partService.getParts();
index 4b77b90a2845cbf9ac7e40d287c0fee04f6ec404..7e472d667b950a579e8568ba46315453bce23971 100644 (file)
@@ -42,6 +42,7 @@ import org.eclipse.jface.viewers.LabelProvider;
 import org.eclipse.jface.viewers.StructuredSelection;
 import org.eclipse.jface.window.Window;
 import org.eclipse.nebula.widgets.nattable.NatTable;
+import org.eclipse.nebula.widgets.nattable.command.StructuralRefreshCommand;
 import org.eclipse.nebula.widgets.nattable.config.AbstractRegistryConfiguration;
 import org.eclipse.nebula.widgets.nattable.config.CellConfigAttributes;
 import org.eclipse.nebula.widgets.nattable.config.ConfigRegistry;
@@ -55,6 +56,7 @@ import org.eclipse.nebula.widgets.nattable.edit.editor.IComboBoxDataProvider;
 import org.eclipse.nebula.widgets.nattable.export.command.ExportCommand;
 import org.eclipse.nebula.widgets.nattable.export.command.ExportCommandHandler;
 import org.eclipse.nebula.widgets.nattable.extension.glazedlists.GlazedListsEventLayer;
+import org.eclipse.nebula.widgets.nattable.extension.glazedlists.GlazedListsSortModel;
 import org.eclipse.nebula.widgets.nattable.extension.glazedlists.tree.GlazedListTreeData;
 import org.eclipse.nebula.widgets.nattable.extension.glazedlists.tree.GlazedListTreeRowModel;
 import org.eclipse.nebula.widgets.nattable.grid.GridRegion;
@@ -85,6 +87,8 @@ import org.eclipse.nebula.widgets.nattable.reorder.ColumnReorderLayer;
 import org.eclipse.nebula.widgets.nattable.reorder.RowReorderLayer;
 import org.eclipse.nebula.widgets.nattable.selection.SelectionLayer;
 import org.eclipse.nebula.widgets.nattable.selection.event.CellSelectionEvent;
+import org.eclipse.nebula.widgets.nattable.sort.SortHeaderLayer;
+import org.eclipse.nebula.widgets.nattable.sort.config.SingleClickSortConfiguration;
 import org.eclipse.nebula.widgets.nattable.style.DisplayMode;
 import org.eclipse.nebula.widgets.nattable.summaryrow.FixedSummaryRowLayer;
 import org.eclipse.nebula.widgets.nattable.summaryrow.SummaryRowLayer;
@@ -165,8 +169,6 @@ ICdmEntitySessionEnabled{
 
     private WorkingSet workingSet;
 
-    private Composite parent;
-
     private ConversationHolder conversation;
 
     private ICdmEntitySession cdmEntitySession;
@@ -194,6 +196,28 @@ ICdmEntitySessionEnabled{
 
     private ListDataProvider<Object> bodyDataProvider;
 
+    private Label wsLabel;
+
+    private Button btnToggleTree;
+
+    private Button btnToggleFlat;
+
+    private Button btnCollapseAll;
+
+    private Button btnExpandAll;
+
+    private ComboViewer comboStates;
+
+    private Button btnManageState;
+
+    private Button btnExcelExport;
+
+    private Composite buttonPanel;
+
+    private Composite parent;
+
+    private List<Feature> features;
+
     @PostConstruct
     public void create(Composite parent) {
         if(CdmStore.isActive() && conversation==null){
@@ -205,42 +229,195 @@ ICdmEntitySessionEnabled{
         else{
             return;
         }
-        parent.setLayout(new GridLayout());
         this.parent = parent;
-    }
+        parent.setLayout(new GridLayout());
 
+        Composite toolbarComposite = new Composite(parent, SWT.NONE);
+        toolbarComposite.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
+        toolbarComposite.setLayout(new GridLayout(8, false));
+        natTable = new NatTable(parent, false);
 
-    public void init(UUID workingSetUuid) {
-        this.workingSet = CdmStore.getService(IWorkingSetService.class).load(workingSetUuid, WS_PROPERTY_PATH);
-        if(workingSet.getDescriptiveSystem()==null){
-            MessagingUtils.informationDialog("Editor could not be opened", "The working set has no feature tree selected.");
-            return;
+        wsLabel = new Label(toolbarComposite, SWT.NONE);
+
+        btnToggleTree = new Button(toolbarComposite, SWT.PUSH);
+        btnToggleFlat = new Button(toolbarComposite, SWT.PUSH);
+        btnCollapseAll = new Button(toolbarComposite, SWT.PUSH);
+        btnExpandAll = new Button(toolbarComposite, SWT.PUSH);
+        comboStates = new ComboViewer(toolbarComposite, SWT.DROP_DOWN);
+        btnManageState = new Button(toolbarComposite, SWT.PUSH);
+        btnExcelExport = new Button(toolbarComposite, SWT.PUSH);
+
+        /**
+         * Toogle tree button
+         */
+        btnToggleTree.setImage(ImageResources.getImage(ImageResources.HIERARCHICAL));
+        btnToggleTree.setToolTipText("Show taxon hierarchy");
+        btnToggleTree.setSelection(true);
+        btnToggleTree.setEnabled(false);
+        btnToggleTree.addSelectionListener(new SelectionAdapter() {
+            @Override
+            public void widgetSelected(SelectionEvent e) {
+                toggleTreeFlat(true);
+            }
+        });
+
+        /**
+         * Toogle flat button
+         */
+        btnToggleFlat.setImage(ImageResources.getImage(ImageResources.FLAT));
+        btnToggleFlat.setToolTipText("Show flat list");
+        btnToggleFlat.addSelectionListener(new SelectionAdapter() {
+            @Override
+            public void widgetSelected(SelectionEvent e) {
+                toggleTreeFlat(false);
+            }
+        });
+
+        /**
+         *
+         * Collapse button
+         */
+        btnCollapseAll.setImage(ImageResources.getImage(ImageResources.COLLAPSE_ALL));
+        btnCollapseAll.setToolTipText("Collapse all");
+        btnCollapseAll.addSelectionListener(new SelectionAdapter() {
+            @Override
+            public void widgetSelected(SelectionEvent e) {
+                natTable.doCommand(new TreeCollapseAllCommand());
+            }
+        });
+
+        /**
+         * Expand button
+         */
+        btnExpandAll.setImage(ImageResources.getImage(ImageResources.EXPAND_ALL));
+        btnExpandAll.setToolTipText("Expand all");
+        btnExpandAll.addSelectionListener(new SelectionAdapter() {
+            @Override
+            public void widgetSelected(SelectionEvent e) {
+                natTable.doCommand(new TreeExpandAllCommand());
+            }
+        });
+
+        /**
+         * Table state persistence
+         */
+        natTableState = new Properties();
+        //load persisted state
+        File statePropertiesFile = getStatePropertiesFile();
+        FileInputStream inputStream;
+        try {
+            inputStream = new FileInputStream(statePropertiesFile);
+            natTableState.load(inputStream);
+        } catch (IOException e) {
+            MessagingUtils.info("No initial state properties file found for character matrix");
         }
-        thisPart.setLabel(workingSet.getLabel());
 
-        Composite toolbarComposite = new Composite(parent, SWT.NONE);
-        toolbarComposite.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
-        toolbarComposite.setLayout(new GridLayout(6, false));
+        DisplayPersistenceDialogCommandHandler handler =
+                new DisplayPersistenceDialogCommandHandler(natTableState, natTable);
+        natTable.registerCommandHandler(handler);
+        // create a combobox for showing the available view states
+        Collection<String> availableStates = PersistenceHelper.getAvailableStates(natTableState);
+        comboStates.setLabelProvider(new LabelProvider(){
+            @Override
+            public String getText(Object element) {
+                if(element instanceof String && ((String) element).isEmpty()){
+                    return "-default-";
+                }
+                return super.getText(element);
+            }
+        });
+        comboStates.setContentProvider(new ArrayContentProvider());
+        comboStates.addSelectionChangedListener(e->
+        {
+            int index = comboStates.getCombo().getSelectionIndex();
+            if (index >= 0) {
+                String selected = comboStates.getCombo().getItem(index);
+                // load the state
+                natTable.loadState(selected, natTableState);
+                natTableState.setProperty(PersistenceDialog.ACTIVE_VIEW_CONFIGURATION_KEY, selected);
+            }
+        });
+        comboStates.setInput(availableStates);
+        if(comboStates.getCombo().getItemCount()>0){
+            comboStates.getCombo().select(0);
+        }
+
+        // add listener to update the combo on view state management changes
+        handler.addStateChangeListener(new IStateChangedListener() {
+            @Override
+            public void handleStateChange(StateChangeEvent event) {
+                comboStates.setInput(PersistenceHelper.getAvailableStates(natTableState));
+                selectStateItem(comboStates, event.getViewConfigName());
+            }
+        });
+
+        // add button to show dialog
+        btnManageState.setImage(ImageResources.getImage(ImageResources.SETTINGS));
+        btnManageState.setToolTipText("View configuration");
+        btnManageState.addSelectionListener(new SelectionAdapter() {
+            @Override
+            public void widgetSelected(SelectionEvent e) {
+                natTable.doCommand(new DisplayPersistenceDialogCommand(natTable));
+                selectStateItem(comboStates, natTableState.get(PersistenceDialog.ACTIVE_VIEW_CONFIGURATION_KEY).toString());
+            }
+        });
+
+        /**
+         * excel export
+         */
+        btnExcelExport.setToolTipText("Export to Excel");
+        btnExcelExport.setImage(ImageResources.getImage(ImageResources.EXPORT));
+        btnExcelExport.addSelectionListener(new SelectionAdapter() {
+            @Override
+            public void widgetSelected(SelectionEvent e) {
+                natTable.doCommand(
+                        new ExportCommand(
+                                natTable.getConfigRegistry(),
+                                natTable.getShell()));
+            }
+        });
+
+        buttonPanel = new Composite(parent, SWT.NONE);
+    }
+
+    private void toggleTreeFlat(boolean isTree) {
+        init(workingSet.getUuid(), isTree);
+        btnToggleFlat.setEnabled(isTree);
+        btnToggleTree.setEnabled(!isTree);
+        btnCollapseAll.setEnabled(isTree);
+        btnExpandAll.setEnabled(isTree);
+        natTable.doCommand(new StructuralRefreshCommand());
+    }
+
+    public void init(UUID workingSetUuid, boolean treeView) {
+        if(workingSet==null){
+            this.workingSet = CdmStore.getService(IWorkingSetService.class).load(workingSetUuid, WS_PROPERTY_PATH);
+            if(workingSet.getDescriptiveSystem()==null){
+                MessagingUtils.informationDialog("Editor could not be opened", "The working set has no feature tree selected.");
+                return;
+            }
+            thisPart.setLabel(workingSet.getLabel());
 
-        //get features/columns stored in working set
-        FeatureTree tree = workingSet.getDescriptiveSystem();
-        List<Feature> features = new ArrayList<>(tree.getDistinctFeatures());
-        Collections.sort(features);
+            //get features/columns stored in working set
+            FeatureTree tree = workingSet.getDescriptiveSystem();
+            features = new ArrayList<>(tree.getDistinctFeatures());
+            Collections.sort(features);
 
-        descriptions = GlazedLists.eventList(getDescriptions(workingSet));
+            descriptions = GlazedLists.eventList(getDescriptions(workingSet));
+        }
         // use the SortedList constructor with 'null' for the Comparator
         // because the Comparator will be set by configuration
         SortedList<Object> sortedList = new SortedList<>(descriptions, null);
         // wrap the SortedList with the TreeList
-        TreeList treeList = new TreeList(sortedList, new DescriptionTreeFormat(workingSet.getMaxRank()), TreeList.NODES_START_EXPANDED);
-
-        ConfigRegistry configRegistry = new ConfigRegistry();
-
+        TreeList<Object> treeList = new TreeList(sortedList, new DescriptionTreeFormat(workingSet.getMaxRank()), TreeList.NODES_START_EXPANDED);
         /**
          * data provider
          */
         SpecimenColumnPropertyAccessor columnPropertyAccessor = new SpecimenColumnPropertyAccessor(this);
-        bodyDataProvider = new ListDataProvider<Object>(treeList, columnPropertyAccessor);
+        bodyDataProvider = treeView?new ListDataProvider<>(treeList, columnPropertyAccessor):new ListDataProvider<>(sortedList, columnPropertyAccessor);
+
+        ConfigRegistry configRegistry = new ConfigRegistry();
+
 
         /**
          * BODY layer
@@ -255,7 +432,7 @@ ICdmEntitySessionEnabled{
         ViewportLayer
 
              ^
-        TreeLayer
+        TreeLayer (default visible)
 
              ^
         SelectionLayer
@@ -307,7 +484,7 @@ ICdmEntitySessionEnabled{
         RowHideShowLayer rowHideShowLayer = new RowHideShowLayer(columnHideShowLayer);
         SelectionLayer selectionLayer = new SelectionLayer(rowHideShowLayer);
         TreeLayer treeLayer = new TreeLayer(selectionLayer, treeRowModel);
-        ViewportLayer viewportLayer = new ViewportLayer(treeLayer);
+        ViewportLayer viewportLayer = treeView?new ViewportLayer(treeLayer):new ViewportLayer(selectionLayer);
 
         // create a standalone FixedSummaryRowLayer
         // since the summary row should be fixed at the top of the body
@@ -344,14 +521,13 @@ ICdmEntitySessionEnabled{
         // add the SortHeaderLayer to the column header layer stack
         // as we use GlazedLists, we use the GlazedListsSortModel which
         // delegates the sorting to the SortedList
-        //TODO currently disabled sorting because of tree list
-//        final SortHeaderLayer<SpecimenDescription> sortHeaderLayer = new SortHeaderLayer<>(
-//                columnHeaderLayer,
-//                new GlazedListsSortModel<>(
-//                        sortedList,
-//                        columnPropertyAccessor,
-//                        configRegistry,
-//                        columnHeaderDataLayer));
+        final SortHeaderLayer<SpecimenDescription> sortHeaderLayer = new SortHeaderLayer<>(
+                columnHeaderLayer,
+                new GlazedListsSortModel<>(
+                        sortedList,
+                        columnPropertyAccessor,
+                        configRegistry,
+                        columnHeaderDataLayer));
 
 
         /**
@@ -369,25 +545,21 @@ ICdmEntitySessionEnabled{
          */
         ILayer cornerLayer = new CornerLayer(
                 new DataLayer(new DefaultCornerDataProvider(columnHeaderDataProvider, rowHeaderDataProvider)),
-                fixedSummaryRowHeaderLayer, columnHeaderLayer);
+                fixedSummaryRowHeaderLayer, sortHeaderLayer);
 
 
         /**
          * GRID layer (composition of all other layers)
          */
-        GridLayer gridLayer = new GridLayer(composite, columnHeaderLayer, fixedSummaryRowHeaderLayer, cornerLayer);
-
-
-
-        natTable = new NatTable(parent, gridLayer, false);
+        GridLayer gridLayer = new GridLayer(composite, sortHeaderLayer, fixedSummaryRowHeaderLayer, cornerLayer);
 
+        natTable.setLayer(gridLayer);
 
         /**
          * CONFIGURATION
          */
         natTable.setConfigRegistry(configRegistry);
 
-
         //add default configuration because autoconfigure is set to false in constructor
         natTable.addConfiguration(new DefaultNatTableStyleConfiguration());
 
@@ -397,8 +569,7 @@ ICdmEntitySessionEnabled{
         // override the default sort configuration and change the mouse bindings
         // to sort on a single click
         //currently removed sorting because of using the TreeConfiguration
-//        natTable.addConfiguration(new SingleClickSortConfiguration());
-
+        natTable.addConfiguration(new SingleClickSortConfiguration());
 
         // add the header menu configuration for adding the column header menu
         // with hide/show actions
@@ -487,122 +658,11 @@ ICdmEntitySessionEnabled{
 
         GridDataFactory.fillDefaults().grab(true, true).applyTo(natTable);
 
-        /**
-         * Info label
-         */
-        Label wsLabel = new Label(toolbarComposite, SWT.NONE);
+
         wsLabel.setText(workingSet.getLabel());
         wsLabel.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
+        wsLabel.getParent().layout();
 
-
-        /**
-         * Expand/Collapse button
-         */
-        Button collapseAllButton = new Button(toolbarComposite, SWT.PUSH);
-        collapseAllButton.setImage(ImageResources.getImage(ImageResources.COLLAPSE_ALL));
-        collapseAllButton.setToolTipText("Collapse all");
-        collapseAllButton.addSelectionListener(new SelectionAdapter() {
-            @Override
-            public void widgetSelected(SelectionEvent e) {
-                natTable.doCommand(new TreeCollapseAllCommand());
-            }
-        });
-        Button expandAllButton = new Button(toolbarComposite, SWT.PUSH);
-        expandAllButton.setImage(ImageResources.getImage(ImageResources.EXPAND_ALL));
-        expandAllButton.setToolTipText("Expand all");
-        expandAllButton.addSelectionListener(new SelectionAdapter() {
-            @Override
-            public void widgetSelected(SelectionEvent e) {
-                natTable.doCommand(new TreeExpandAllCommand());
-            }
-        });
-
-        /**
-         * Table state persistence
-         */
-        natTableState = new Properties();
-        //load persisted state
-        File statePropertiesFile = getStatePropertiesFile();
-        FileInputStream inputStream;
-        try {
-            inputStream = new FileInputStream(statePropertiesFile);
-            natTableState.load(inputStream);
-        } catch (IOException e) {
-            MessagingUtils.info("No initial state properties file found for character matrix");
-        }
-
-        DisplayPersistenceDialogCommandHandler handler =
-                new DisplayPersistenceDialogCommandHandler(natTableState, natTable);
-        gridLayer.registerCommandHandler(handler);
-        // create a combobox for showing the available view states
-        ComboViewer comboStates= new ComboViewer(toolbarComposite, SWT.DROP_DOWN) ;
-        Collection<String> availableStates = PersistenceHelper.getAvailableStates(natTableState);
-        comboStates.setLabelProvider(new LabelProvider(){
-            @Override
-            public String getText(Object element) {
-                if(element instanceof String && ((String) element).isEmpty()){
-                    return "-default-";
-                }
-                return super.getText(element);
-            }
-        });
-        comboStates.setContentProvider(new ArrayContentProvider());
-        comboStates.addSelectionChangedListener(e->
-        {
-            int index = comboStates.getCombo().getSelectionIndex();
-            if (index >= 0) {
-                String selected = comboStates.getCombo().getItem(index);
-                // load the state
-                natTable.loadState(selected, natTableState);
-                natTableState.setProperty(PersistenceDialog.ACTIVE_VIEW_CONFIGURATION_KEY, selected);
-            }
-        });
-        comboStates.setInput(availableStates);
-        if(comboStates.getCombo().getItemCount()>0){
-            comboStates.getCombo().select(0);
-        }
-
-        // add listener to update the combo on view state management changes
-        handler.addStateChangeListener(new IStateChangedListener() {
-            @Override
-            public void handleStateChange(StateChangeEvent event) {
-                comboStates.setInput(PersistenceHelper.getAvailableStates(natTableState));
-                selectStateItem(comboStates, event.getViewConfigName());
-            }
-        });
-
-        // add button to show dialog
-        Button btnManageState = new Button(toolbarComposite, SWT.PUSH);
-        btnManageState.setImage(ImageResources.getImage(ImageResources.SETTINGS));
-        btnManageState.setToolTipText("View configuration");
-        btnManageState.addSelectionListener(new SelectionAdapter() {
-            @Override
-            public void widgetSelected(SelectionEvent e) {
-                natTable.doCommand(new DisplayPersistenceDialogCommand(natTable));
-                selectStateItem(comboStates, natTableState.get(PersistenceDialog.ACTIVE_VIEW_CONFIGURATION_KEY).toString());
-            }
-        });
-
-        /**
-         * excel export
-         */
-        Button btnExcelExport = new Button(toolbarComposite, SWT.PUSH);
-        btnExcelExport.setToolTipText("Export to Excel");
-        btnExcelExport.setImage(ImageResources.getImage(ImageResources.EXPORT));
-        btnExcelExport.addSelectionListener(new SelectionAdapter() {
-            @Override
-            public void widgetSelected(SelectionEvent e) {
-                natTable.doCommand(
-                        new ExportCommand(
-                                natTable.getConfigRegistry(),
-                                natTable.getShell()));
-            }
-        });
-
-        /**
-         * bottom button toolbar
-         */
-        Composite buttonPanel = new Composite(parent, SWT.NONE);
         buttonPanel.setLayout(new RowLayout());
         GridDataFactory.fillDefaults().grab(true, false).applyTo(buttonPanel);
 
index b3bccc30d767d144e7fd55e43d106d5dc851a21e..37c0f03d78fd620ef20b0797b5f4bbcad9b56898 100644 (file)
@@ -171,6 +171,9 @@ public class ImageResources {
     public static final String WEB = "web";
     public static final String DATE = "dates";
 
+    public static final String FLAT = "flat";
+    public static final String HIERARCHICAL = "hierarchical";
+
 
        /***************************************************************************
         * IMAGE REGISTRY
@@ -410,6 +413,11 @@ public class ImageResources {
                registerImage(registry, DATE,
                        "dates.gif");
 
+               registerImage(registry, FLAT,
+                       "flat.gif");
+               registerImage(registry, HIERARCHICAL,
+                       "hierarchical.gif");
+
        }
 
        private void registerImage(ImageRegistry registry, String key,