Added a reload function so that the checklist editor doesn't need to be relaunched...
authorAlexander Oppermann <a.oppermann@bgbm.org>
Fri, 5 Dec 2014 16:13:48 +0000 (16:13 +0000)
committerAlexander Oppermann <a.oppermann@bgbm.org>
Fri, 5 Dec 2014 16:13:48 +0000 (16:13 +0000)
eu.etaxonomy.taxeditor.editor/src/main/java/eu/etaxonomy/taxeditor/editor/view/checklist/ChecklistEditor.java
eu.etaxonomy.taxeditor.editor/src/main/java/eu/etaxonomy/taxeditor/editor/view/checklist/ChecklistLabelProvider.java
eu.etaxonomy.taxeditor.editor/src/main/java/eu/etaxonomy/taxeditor/editor/view/checklist/listener/ChecklistDropdownSelectionListener.java

index b99aee8d286bf20aea964b4a14e2427f66f8e796..0ae9da33f10131cc497e8f47569dca5a80b07ca2 100644 (file)
@@ -37,6 +37,7 @@ import org.eclipse.swt.widgets.Composite;
 import org.eclipse.swt.widgets.Display;
 import org.eclipse.swt.widgets.Label;
 import org.eclipse.swt.widgets.Menu;
+import org.eclipse.swt.widgets.MenuItem;
 import org.eclipse.swt.widgets.Table;
 import org.eclipse.swt.widgets.TableColumn;
 import org.eclipse.swt.widgets.Text;
@@ -64,7 +65,6 @@ import eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap;
 import eu.etaxonomy.taxeditor.editor.EditorUtil;
 import eu.etaxonomy.taxeditor.editor.SimpleSelectionProvider;
 import eu.etaxonomy.taxeditor.editor.view.checklist.edit.DistributionEditingSupport;
-import eu.etaxonomy.taxeditor.editor.view.checklist.edit.RankEditingSupport;
 import eu.etaxonomy.taxeditor.editor.view.checklist.filter.ChecklistEditorFilter;
 import eu.etaxonomy.taxeditor.editor.view.checklist.listener.ChecklistDropdownSelectionListener;
 import eu.etaxonomy.taxeditor.editor.view.checklist.listener.ChecklistFocusListener;
@@ -132,7 +132,7 @@ public class ChecklistEditor extends EditorPart implements ISelectionListener, I
 
                     @Override
                     public void run() {
-                        viewer.setInput(taxonList);
+                       viewer.setInput(taxonList);
                     }
                 });
                 monitor.worked(1);
@@ -182,6 +182,8 @@ public class ChecklistEditor extends EditorPart implements ISelectionListener, I
     private Label statusLabel;
 
     private SortedSet<DefinedTermBase> terms = null;
+       private ToolItem toolItem;
+       private ChecklistDropdownSelectionListener dropListener;
 
     /**
      * @return the selectedTaxonNodes
@@ -231,10 +233,7 @@ public class ChecklistEditor extends EditorPart implements ISelectionListener, I
         parent.setLayout(gridLayout);
 
         final Text searchText = createSearchBar(parent);
-
-//        if (terms != null && !terms.isEmpty()) {
-            createToolbar(parent);
-//        }
+        createToolbar(parent);
         viewer = new TableViewer(parent, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL | SWT.FULL_SELECTION);
         getSite().setSelectionProvider(viewer);
 
@@ -242,27 +241,29 @@ public class ChecklistEditor extends EditorPart implements ISelectionListener, I
 
         searchText.addFocusListener(new ChecklistFocusListener(searchText, modifyListener));
         searchText.addModifyListener(modifyListener);
-
-        // Layout the viewer
-        GridData gridData = new GridData();
-        gridData.verticalAlignment = GridData.FILL;
-        gridData.horizontalSpan = 3;
-        gridData.grabExcessHorizontalSpace = true;
-        gridData.grabExcessVerticalSpace = true;
-        gridData.horizontalAlignment = GridData.FILL;
-        viewer.getControl().setLayoutData(gridData);
+        createGridDataForViewerLayout();
 
         viewer.addFilter(filter);
-        createColumns(viewer);
+        createTable();
 
         viewer.setContentProvider(new ChecklistContentProvider());
-        viewer.setLabelProvider(new ChecklistLabelProvider());
+        viewer.setLabelProvider(new ChecklistLabelProvider(viewer));
         comparator = new ChecklistEditorComparator();
         viewer.setComparator(comparator);
 
         createStatusBar(parent);
     }
 
+       private void createGridDataForViewerLayout() {
+               GridData gridData = new GridData();
+               gridData.verticalAlignment = GridData.FILL;
+        gridData.horizontalSpan = 3;
+        gridData.grabExcessHorizontalSpace = true;
+        gridData.grabExcessVerticalSpace = true;
+        gridData.horizontalAlignment = GridData.FILL;
+        viewer.getControl().setLayoutData(gridData);
+       }
+
     /**
      * @param parent
      * @return
@@ -282,18 +283,22 @@ public class ChecklistEditor extends EditorPart implements ISelectionListener, I
      */
     private void createToolbar(Composite parent) {
         ToolBar toolBar = new ToolBar(parent, SWT.NONE);
-        ToolItem toolItem = new ToolItem(toolBar, SWT.DROP_DOWN | SWT.BORDER);
+        toolItem = new ToolItem(toolBar, SWT.DROP_DOWN | SWT.BORDER);
         toolItem.setText("Distribution Status");
         toolItem.setToolTipText("Show Distribution Status for selected Areas");
-        ChecklistDropdownSelectionListener dropListener = new ChecklistDropdownSelectionListener(toolItem, this, terms);
+        createToolbarItems();
+        toolItem.addSelectionListener(dropListener);
+        toolBar.pack();
+    }
+
+       private void createToolbarItems() {
+               dropListener = new ChecklistDropdownSelectionListener(toolItem, this, terms);
         for (DefinedTermBase<DefinedTermBase> term : terms) {
             if(term!=null){
                 dropListener.add(term);
             }
         }
-        toolItem.addSelectionListener(dropListener);
-        toolBar.pack();
-    }
+       }
 
     private void createStatusBar(Composite composite) {
         GridData gridData = new GridData();
@@ -306,55 +311,105 @@ public class ChecklistEditor extends EditorPart implements ISelectionListener, I
         statusLabel.setLayoutData(gridData);
     }
 
-    // This will create the columns for the table
-    private void createColumns(TableViewer viewer) {
-        Table table = viewer.getTable();
+    private void createTable() {
+        Table table = viewer.getTable();//new Table(parent, viewer.getTable().getStyle());
         List<String> titles = new ArrayList<String>();
         Collections.addAll(titles, "Taxon", "Author", "Reference", "Rank");
         List<Integer> bounds = new ArrayList<Integer>();
         Collections.addAll(bounds, 300, 200, 200, 200);
-        Map<Integer, Boolean>restoreColumnWidth = new HashMap<Integer, Boolean>();
-        if (terms != null) {
-            int i = 4;
-            for (DefinedTermBase<DefinedTermBase> term : terms) {
-                if(term != null){
-                    restoreColumnWidth.put(i, PreferencesUtil.getPreferenceStore().getBoolean(term.getUuid().toString()));
-                    titles.add(term.getTitleCache());
-                    bounds.add(200);
-                    i++;
-                }
-            }
-        }
-
-        for (int i = 0; i < titles.size(); i++) {
+        Map<Integer, Boolean> restoreValuesForColumnWidth = restoreValuesForColumnWidth(titles, bounds);
+        createInitalDistributionColumns(table, titles, bounds, restoreValuesForColumnWidth);
+        table.setSortDirection(SWT.DOWN);
+        table.setHeaderVisible(true);
+        table.setLinesVisible(true);
+       }
+    /**
+     * This method creates initially the distribution columns for a table. It should only be called for creation.<br>
+     *<p> 
+     *
+     *<b>Notice:</b> If you want to add additional columns later please use <b>addTableViewerColumn()</b>
+     *
+     * @param table
+     * @param titles
+     * @param bounds
+     * @param restoreValuesForColumnWidth
+     */
+       private void createInitalDistributionColumns(Table table,
+                       List<String> titles, List<Integer> bounds,
+                       Map<Integer, Boolean> restoreValuesForColumnWidth) {
+               for (int columnIndex = 0; columnIndex < titles.size(); columnIndex++) {
             TableViewerColumn column = new TableViewerColumn(viewer, SWT.NONE);
-            column.getColumn().setText(titles.get(i));
-            column.getColumn().setWidth(bounds.get(i));
+            column.getColumn().setText(titles.get(columnIndex));
+            column.getColumn().setWidth(bounds.get(columnIndex));
             column.getColumn().setResizable(true);
             column.getColumn().setMoveable(true);
-            column.getColumn().addSelectionListener(getSelectionAdapter(column.getColumn(), i));
-            if (i == 1) {
+            column.getColumn().addSelectionListener(getSelectionAdapter(column.getColumn(), columnIndex));
+            if (columnIndex == 1) {
                 table.setSortColumn(column.getColumn());
             }
-            if (i == 3) {
+            if (columnIndex == 3) {
                /** uncommented it for now because no rank editing is wanted **/
 //                column.setEditingSupport(new RankEditingSupport(viewer, this));
             }
-            if (i >= 4) {
+            if (columnIndex >= 4) {
                 //read PrefrenceStore to setWidth according to last saved state
-                if(restoreColumnWidth.get(i)){
+                if(restoreValuesForColumnWidth.get(columnIndex)){
                     column.getColumn().setWidth(100);
                 }else{
                     column.getColumn().setWidth(0);
                 }
-                column.setEditingSupport(new DistributionEditingSupport(viewer, this, i));
+                column.setEditingSupport(new DistributionEditingSupport(viewer, this, columnIndex));
             }
         }
-        table.setSortDirection(SWT.DOWN);
-        table.setHeaderVisible(true);
-        table.setLinesVisible(true);
-    }
+       }
+
+    /**
+     * This methods loads the last opened distribution columns for the table viewer from the prefrence store.<br>
+     *<p> 
+     * <b>Notice:</b> It adds also the TitleCache to the titles list for the header of each column.<p>
+     * 
+     * @param titles
+     * @param bounds
+     * @return Map<Integer, Boolean>
+     */
+       private Map<Integer, Boolean> restoreValuesForColumnWidth(List<String> titles,
+                       List<Integer> bounds) {
+               Map<Integer, Boolean> restoreColumnWidth = new HashMap<Integer, Boolean>();
+               if (terms != null) {
+            int columnIndex = 4;
+            for (DefinedTermBase<DefinedTermBase> term : terms) {
+                if(term != null){
+                    restoreColumnWidth.put(columnIndex, PreferencesUtil.getPreferenceStore().getBoolean(term.getUuid().toString()));
+                    titles.add(term.getTitleCache());
+                    bounds.add(200);
+                    columnIndex++;
+                }
+            }
+        }
+               return restoreColumnWidth;
+       }
 
+    /**
+     * This method adds new DistributionColumns to an existing table. 
+     * 
+     * @param title
+     * @param bound
+     * @param colNumber
+     * @return
+     */
+    private TableViewerColumn addTableViewerColumn(String title, int bound, final int colNumber) {
+        final TableViewerColumn viewerColumn = new TableViewerColumn(viewer, SWT.NONE);
+        final TableColumn column = viewerColumn.getColumn();
+        column.setText(title);
+        column.setWidth(200);
+        viewerColumn.setEditingSupport(new DistributionEditingSupport(viewer, this, colNumber));
+        column.setResizable(true);
+        column.setMoveable(true);
+        return viewerColumn;
+      }
+    
+    
+    
     /**
      *
      * pull data from database and set input for view
@@ -428,6 +483,65 @@ public class ChecklistEditor extends EditorPart implements ISelectionListener, I
         viewer.refresh();
     }
 
+    /**
+     * This method should only be called for adding new Distribution columns and reloading the table.<br>
+     * It will hide the old distribution column and load the newly added columns.<br>
+     * <p>
+     * <b>Notice:</b> for data update please use <b>refresh()</b>
+     * 
+     */
+    @SuppressWarnings({ "unchecked", "rawtypes" })
+       public void reload(){
+       //create new Items for Dropdown menue
+       SortedSet<DefinedTermBase> oldTerms = terms;
+       ChecklistLabelProvider labelProvider = new ChecklistLabelProvider();
+       terms = (SortedSet<DefinedTermBase>) labelProvider.getTermsOrderedByLabels(labelProvider.getNamedAreas(), CdmStore.getDefaultLanguage());
+       SortedSet<DefinedTermBase> newTerms = terms;
+       toolItem.removeSelectionListener(dropListener);
+       hideDistributionColumns(oldTerms);
+       createToolbarItems();
+       toolItem.addSelectionListener(dropListener);
+        newTerms.removeAll(oldTerms);
+        if(newTerms.size() > 0){
+               for(DefinedTermBase term:newTerms){
+                       int count = viewer.getTable().getColumnCount();
+                       addTableViewerColumn(term.getTitleCache(), 200, count);
+                       acitivateNewColumnInDropDownMenu(term);
+               }
+        }
+        viewer.setLabelProvider(new ChecklistLabelProvider(viewer));
+        getService().schedule(new ChecklistJob("loading Taxa", selectedTaxonNodes), Job.LONG);
+       viewer.refresh();
+    }
+
+       private void acitivateNewColumnInDropDownMenu(DefinedTermBase term) {
+               Menu menu = dropListener.getMenu();
+               MenuItem[] items = menu.getItems();
+               for(MenuItem item: items){
+                       if(item.getText().equalsIgnoreCase(term.getTitleCache())){
+                               item.setSelection(true);
+                               PreferencesUtil.getPreferenceStore().setValue(term.getUuid().toString(), true);
+                       }
+               }
+       }
+
+       private void hideDistributionColumns(SortedSet<DefinedTermBase> oldTerms) {
+               //FIXME: THERE IS A BUG WITH THE getColumns() functin.
+               TableColumn[] columns = viewer.getTable().getColumns();
+       for(int i=4; i<columns.length; i++){
+               columns[i].setWidth(0);
+               Menu menu = dropListener.getMenu();
+               int itemCount = menu.getItemCount();
+               MenuItem item = menu.getItem(i-3);
+               item.setSelection(false);
+       }
+       if(oldTerms != null){
+               for(DefinedTermBase term : oldTerms){
+                       PreferencesUtil.getPreferenceStore().setValue(term.getUuid().toString(), false);
+               }
+       }
+       }
+    
     /**
      * <p>
      * Getter for the field <code>service</code>.
@@ -474,8 +588,6 @@ public class ChecklistEditor extends EditorPart implements ISelectionListener, I
 
     @Override
     public void doSaveAs() {
-        // TODO Auto-generated method stub
-
     }
 
     @Override
@@ -493,7 +605,6 @@ public class ChecklistEditor extends EditorPart implements ISelectionListener, I
 
     @Override
     public boolean isSaveAsAllowed() {
-        // TODO Auto-generated method stub
         return false;
     }
 
@@ -505,8 +616,6 @@ public class ChecklistEditor extends EditorPart implements ISelectionListener, I
      */
     @Override
     public void selectionChanged(IWorkbenchPart part, ISelection selection) {
-        // TODO Auto-generated method stub
-
     }
 
     /*
@@ -555,7 +664,6 @@ public class ChecklistEditor extends EditorPart implements ISelectionListener, I
      */
     @Override
     public void contextStop(IMemento memento, IProgressMonitor monitor) {
-        // TODO Auto-generated method stub
 //        IStructuredSelection sel = (IStructuredSelection) this.viewer.getSelection();
 //        if (sel.isEmpty()) {
 //            return;
@@ -589,8 +697,6 @@ public class ChecklistEditor extends EditorPart implements ISelectionListener, I
      */
     @Override
     public void contextRefresh(IProgressMonitor monitor) {
-        // TODO Auto-generated method stub
-
     }
 
     /*
index a1160c1de1d3545e8b1b2e2d853870a06528c309..56af4186aa5a57b11bbacc59372bb4763820188b 100644 (file)
@@ -20,7 +20,9 @@ import java.util.UUID;
 import org.eclipse.jface.preference.IPreferenceStore;
 import org.eclipse.jface.viewers.ITableLabelProvider;
 import org.eclipse.jface.viewers.LabelProvider;
+import org.eclipse.jface.viewers.TableViewer;
 import org.eclipse.swt.graphics.Image;
+import org.eclipse.swt.widgets.TableColumn;
 
 import eu.etaxonomy.cdm.api.service.IDescriptionService;
 import eu.etaxonomy.cdm.api.service.ITermService;
@@ -49,8 +51,7 @@ public class ChecklistLabelProvider extends LabelProvider implements ITableLabel
     public static final String DEFAULT_ENTRY = "";
     private IDescriptionService descriptionService;
     private final List<DefinedTermBase<?>> namedAreas;
-
-
+    TableViewer viewer = null;
 
     /**
      *
@@ -59,6 +60,11 @@ public class ChecklistLabelProvider extends LabelProvider implements ITableLabel
         namedAreas = loadNamedAreas();
     }
 
+    public ChecklistLabelProvider(TableViewer viewer) {
+        namedAreas = loadNamedAreas();
+        this.viewer = viewer;
+    }
+    
     /*
      * (non-Javadoc)
      *
@@ -81,12 +87,11 @@ public class ChecklistLabelProvider extends LabelProvider implements ITableLabel
      * .Object, int)
      */
     /** {@inheritDoc} */
-    @Override
+       @Override
     public String getColumnText(Object element, int columnIndex) {
         descriptionService = CdmStore.getService(IDescriptionService.class);
         Taxon taxon = (Taxon) element;
         //TODO load areas by this list
-        SortedSet<DefinedTermBase<?>> sortedNamedAreas = (SortedSet<DefinedTermBase<?>>) getTermsOrderedByLabels(namedAreas, CdmStore.getDefaultLanguage());
 
         List<TaxonDescription> listTaxonDescriptions = descriptionService.listTaxonDescriptions(taxon, null, null, null, null, null, DESC_INIT_STRATEGY);;
         NonViralName<?> nonVirlaName = HibernateProxyHelper.deproxy(taxon.getName(), NonViralName.class);
@@ -124,14 +129,11 @@ public class ChecklistLabelProvider extends LabelProvider implements ITableLabel
                     if (deb instanceof Distribution) {
                         Distribution distribution = (Distribution) deb;
                         String area = distribution.toString();
-                        if(sortedNamedAreas != null){
-                            Object[] array = sortedNamedAreas.toArray();
-                            DefinedTermBase<DefinedTermBase> term = (DefinedTermBase<DefinedTermBase>) array[columnIndex-4];
-                            if(term != null){
-                                if (area.equalsIgnoreCase(term.getTitleCache())) {
-                                    return (distribution.getStatus().getTitleCache() != null)?distribution.getStatus().getTitleCache():DEFAULT_ENTRY;
-                                }
-                            }
+                        if(viewer != null){
+                               TableColumn column = viewer.getTable().getColumn(columnIndex);
+                               if (area.equalsIgnoreCase(column.getText())) {
+                                       return (distribution.getStatus().getTitleCache() != null)?distribution.getStatus().getTitleCache():DEFAULT_ENTRY;
+                               }
                         }
                     }
                 }
index 45c558e9b3dd87b3c7d996d2249d62b894b5b0be..68bda9a28d1a701c55941ccf1e4a68d6a0243101 100644 (file)
@@ -2,6 +2,7 @@ package eu.etaxonomy.taxeditor.editor.view.checklist.listener;
 
 import java.util.SortedSet;
 
+import org.apache.log4j.Logger;
 import org.eclipse.jface.viewers.TableViewer;
 import org.eclipse.jface.wizard.WizardDialog;
 import org.eclipse.swt.SWT;
@@ -32,13 +33,12 @@ import eu.etaxonomy.taxeditor.store.StoreUtil;
 public class ChecklistDropdownSelectionListener extends SelectionAdapter {
 
     private final Menu menu;
-    private final ToolItem dropdown;
     private final ChecklistEditor editor;
     private final SortedSet<DefinedTermBase> termSet;
-
+    private Logger logger = Logger.getLogger(ChecklistDropdownSelectionListener.class);
+    
     public ChecklistDropdownSelectionListener(ToolItem dropdown, ChecklistEditor editor,
             SortedSet<DefinedTermBase> termSet) {
-        this.dropdown = dropdown;
         this.editor = editor;
         this.termSet = termSet;
         menu = new Menu(dropdown.getParent().getShell());
@@ -65,16 +65,19 @@ public class ChecklistDropdownSelectionListener extends SelectionAdapter {
                 TableColumn column = null;
                 DefinedTermBase<DefinedTermBase> selectedTerm = null;
                 if (termSet != null) {
-                    int i = 0;
-                    for (DefinedTermBase<DefinedTermBase> term : termSet) {
-                        if (term != null) {
-                            if (selected.getText().equalsIgnoreCase(term.getTitleCache())) {
-                                selectedTerm = term;
-                                column = table.getColumn(4 + i);
-                            }
-                            i++;
-                        }
-                    }
+                       TableColumn[] columns = viewer.getTable().getColumns();
+                       for(int i=0; i<columns.length; i++){
+                               if(selected.getText().equalsIgnoreCase(columns[i].getText())){
+                                       column = columns[i];
+                                       logger.info("Column no " +i +" Column Header "+ column.getText() );
+                                       for (DefinedTermBase<DefinedTermBase> term : termSet) {
+                                               if(column.getText().equalsIgnoreCase(term.getTitleCache())){
+                                                       selectedTerm = term;
+                                                       break;
+                                               }
+                                       }
+                               }
+                       }
                 }
                 if (column != null && column.getWidth() == 0) {
                     // save column status if shown or not
@@ -88,7 +91,6 @@ public class ChecklistDropdownSelectionListener extends SelectionAdapter {
                     }
                     column.setWidth(0);
                 }
-                viewer.refresh();
             }
         });
     }
@@ -103,7 +105,10 @@ public class ChecklistDropdownSelectionListener extends SelectionAdapter {
                 WizardDialog dialog = new WizardDialog(StoreUtil.getShell(),
                         availableDistributionWizard);
 
-                dialog.open();
+                int open = dialog.open();
+                if(open == 0){
+                       editor.reload();
+                }
             }
         });
         new MenuItem(menu, SWT.SEPARATOR);
@@ -121,4 +126,8 @@ public class ChecklistDropdownSelectionListener extends SelectionAdapter {
             menu.setVisible(true);
         }
     }
+    
+    public Menu getMenu(){
+       return menu;
+    }
 }
\ No newline at end of file