Implemented default column sorting option for summary table on dashboad and option...
authorzev <zev@netxms.org>
Thu, 5 Oct 2017 15:42:06 +0000 (18:42 +0300)
committerzev <zev@netxms.org>
Thu, 5 Oct 2017 15:42:11 +0000 (18:42 +0300)
src/client/java/netxms-client/src/main/java/org/netxms/client/Table.java
src/java/netxms-eclipse/Dashboard/src/org/netxms/ui/eclipse/dashboard/propertypages/DciSummaryTable.java
src/java/netxms-eclipse/Dashboard/src/org/netxms/ui/eclipse/dashboard/widgets/DciSummaryTableElement.java
src/java/netxms-eclipse/Dashboard/src/org/netxms/ui/eclipse/dashboard/widgets/internal/DciSummaryTableConfig.java
src/java/netxms-eclipse/DataCollection/src/org/netxms/ui/eclipse/datacollection/widgets/SummaryTableWidget.java

index bde2a8f..c57e6f6 100644 (file)
@@ -429,4 +429,17 @@ public class Table
    {
       this.extendedFormat = extendedFormat;
    }
+   
+   public Table getFirstRows(int numOfRows)
+   {
+      Table t = new Table();
+      t.title = title;
+      t.source = source;
+      t.columns = new ArrayList<TableColumnDefinition>(columns);
+      t.data = new ArrayList<TableRow>();
+      for (int i = 0; i < numOfRows; i++)
+         t.data.add(data.get(i));
+      t.extendedFormat = extendedFormat;
+      return t;      
+   }
 }
index d3ffcfc..51d2a44 100644 (file)
  */
 package org.netxms.ui.eclipse.dashboard.propertypages;
 
+import java.util.List;
+import org.eclipse.jface.dialogs.InputDialog;
+import org.eclipse.jface.viewers.ArrayContentProvider;
+import org.eclipse.jface.viewers.ISelectionChangedListener;
+import org.eclipse.jface.viewers.IStructuredSelection;
+import org.eclipse.jface.viewers.SelectionChangedEvent;
+import org.eclipse.jface.viewers.TableViewer;
+import org.eclipse.jface.window.Window;
 import org.eclipse.swt.SWT;
+import org.eclipse.swt.events.SelectionEvent;
+import org.eclipse.swt.events.SelectionListener;
 import org.eclipse.swt.layout.GridData;
 import org.eclipse.swt.layout.GridLayout;
+import org.eclipse.swt.layout.RowData;
+import org.eclipse.swt.layout.RowLayout;
+import org.eclipse.swt.widgets.Button;
 import org.eclipse.swt.widgets.Composite;
 import org.eclipse.swt.widgets.Control;
+import org.eclipse.swt.widgets.Label;
 import org.eclipse.swt.widgets.Spinner;
+import org.eclipse.swt.widgets.Text;
 import org.eclipse.ui.dialogs.PropertyPage;
 import org.netxms.client.objects.AbstractObject;
 import org.netxms.ui.eclipse.dashboard.Messages;
@@ -39,9 +54,17 @@ import org.netxms.ui.eclipse.widgets.AbstractSelector;
 public class DciSummaryTable extends PropertyPage
 {
        private DciSummaryTableConfig config;
+       private List<String> currList;
        private ObjectSelector objectSelector;
        private SummaryTableSelector tableSelector;
        private Spinner refreshInterval;
+   private Spinner numRowsShow;
+       private TableViewer sortTables;
+       private Button buttonAdd;
+       private Button buttonEdit;
+       private Button buttonRemove;
+       private Button buttonUp;
+       private Button buttonDown;
        
        /* (non-Javadoc)
         * @see org.eclipse.jface.preference.PreferencePage#createContents(org.eclipse.swt.widgets.Composite)
@@ -52,6 +75,7 @@ public class DciSummaryTable extends PropertyPage
                config = (DciSummaryTableConfig)getElement().getAdapter(DciSummaryTableConfig.class);
                
                Composite dialogArea = new Composite(parent, SWT.NONE);
+               currList = config.getSortingColumnList();
                
                GridLayout layout = new GridLayout();
                dialogArea.setLayout(layout);
@@ -79,10 +103,259 @@ public class DciSummaryTable extends PropertyPage
       gd.grabExcessHorizontalSpace = true;
       refreshInterval = WidgetHelper.createLabeledSpinner(dialogArea, SWT.BORDER, Messages.get().DciSummaryTable_RefreshInterval, 0, 10000, gd);
       refreshInterval.setSelection(config.getRefreshInterval());
+      
+      gd = new GridData();
+      gd.verticalAlignment = SWT.TOP;
+      gd.horizontalAlignment = SWT.FILL;
+      gd.grabExcessHorizontalSpace = true;
+      numRowsShow = WidgetHelper.createLabeledSpinner(dialogArea, SWT.BORDER, "Number of lines to show", 0, 10000, gd);
+      numRowsShow.setSelection(config.getNumRowShown());
+      
+      Label label = new Label(dialogArea, SWT.NONE);
+      label.setText("Sorting list");
+      
+      sortTables  = new TableViewer(dialogArea, SWT.BORDER | SWT.MULTI | SWT.FULL_SELECTION);
+      gd = new GridData();
+      gd.horizontalAlignment = SWT.FILL;
+      gd.grabExcessHorizontalSpace = true;
+      gd.verticalAlignment = SWT.FILL;
+      gd.grabExcessVerticalSpace = true;
+      gd.verticalSpan = 2;
+      gd.heightHint = 150;
+      sortTables.getTable().setLayoutData(gd);
+      sortTables.getTable().setSortDirection(SWT.UP);
+      sortTables.setContentProvider(new ArrayContentProvider());
+      sortTables.setInput(currList);
+      sortTables.addSelectionChangedListener(new ISelectionChangedListener() {
+         
+         @Override
+         public void selectionChanged(SelectionChangedEvent event)
+         {
+            IStructuredSelection selection = (IStructuredSelection)event.getSelection();
+            int index = currList.indexOf(selection.getFirstElement());
+            buttonUp.setEnabled((selection.size() == 1) && (index > 0));
+            buttonDown.setEnabled((selection.size() == 1) && (index >= 0) && (index < currList.size() - 1));
+            buttonEdit.setEnabled(selection.size() == 1);
+            buttonRemove.setEnabled(selection.size() > 0);            
+         }
+      });
+      
+      Composite buttonsRight = new Composite(dialogArea, SWT.NONE);
+      
+      gd = new GridData();
+      gd.horizontalAlignment = SWT.FILL;
+      gd.grabExcessHorizontalSpace = true;
+      buttonsRight.setLayoutData(gd);
+      
+      RowLayout buttonLayout = new RowLayout();
+      buttonLayout.type = SWT.HORIZONTAL;
+      buttonLayout.pack = false;
+      buttonLayout.marginWidth = 0;
+      buttonsRight.setLayout(buttonLayout);
+      
+      buttonUp = new Button(buttonsRight, SWT.PUSH);
+      buttonUp.setText("UP");
+      buttonUp.addSelectionListener(new SelectionListener() {
+         @Override
+         public void widgetDefaultSelected(SelectionEvent e)
+         {
+            widgetSelected(e);
+         }
+
+         @Override
+         public void widgetSelected(SelectionEvent e)
+         {
+            moveUp();
+         }
+      });
+      RowData rd = new RowData();
+      rd.width = WidgetHelper.BUTTON_WIDTH_HINT;
+      buttonUp.setLayoutData(rd);
+      buttonUp.setEnabled(false);
+      
+      buttonDown = new Button(buttonsRight, SWT.PUSH);
+      buttonDown.setText("Down");
+      buttonDown.addSelectionListener(new SelectionListener() {
+         @Override
+         public void widgetDefaultSelected(SelectionEvent e)
+         {
+            widgetSelected(e);
+         }
+
+         @Override
+         public void widgetSelected(SelectionEvent e)
+         {
+            moveDown();
+         }
+      });
+      rd = new RowData();
+      rd.width = WidgetHelper.BUTTON_WIDTH_HINT;
+      buttonDown.setLayoutData(rd);
+      buttonDown.setEnabled(false);
+      
+      gd = new GridData();
+      gd.horizontalAlignment = SWT.RIGHT;
+      gd.verticalIndent = WidgetHelper.OUTER_SPACING - WidgetHelper.INNER_SPACING;
+      buttonsRight.setLayoutData(gd);
+
+      buttonAdd = new Button(buttonsRight, SWT.PUSH);
+      buttonAdd.setText("Add");
+      buttonAdd.addSelectionListener(new SelectionListener() {
+         @Override
+         public void widgetDefaultSelected(SelectionEvent e)
+         {
+            widgetSelected(e);
+         }
+
+         @Override
+         public void widgetSelected(SelectionEvent e)
+         {
+            add();
+         }
+      });
+      rd = new RowData();
+      rd.width = WidgetHelper.BUTTON_WIDTH_HINT;
+      buttonAdd.setLayoutData(rd);
+      
+      buttonEdit = new Button(buttonsRight, SWT.PUSH);
+      buttonEdit.setText("Edit");
+      buttonEdit.addSelectionListener(new SelectionListener() {
+         @Override
+         public void widgetDefaultSelected(SelectionEvent e)
+         {
+            widgetSelected(e);
+         }
+
+         @Override
+         public void widgetSelected(SelectionEvent e)
+         {
+            edit();
+         }
+      });
+      rd = new RowData();
+      rd.width = WidgetHelper.BUTTON_WIDTH_HINT;
+      buttonEdit.setLayoutData(rd);
+      buttonEdit.setEnabled(false);
+
+      buttonRemove = new Button(buttonsRight, SWT.PUSH);
+      buttonRemove.setText("Delete");
+      buttonRemove.addSelectionListener(new SelectionListener() {
+         @Override
+         public void widgetDefaultSelected(SelectionEvent e)
+         {
+            widgetSelected(e);
+         }
+
+         @Override
+         public void widgetSelected(SelectionEvent e)
+         {
+            remove();
+         }
+      });
+      rd = new RowData();
+      rd.width = WidgetHelper.BUTTON_WIDTH_HINT;
+      buttonRemove.setLayoutData(rd);
+      buttonRemove.setEnabled(false);
                
                return dialogArea;
        }
+   
+   /**
+    * Move selected field up 
+    */
+   private void moveUp()
+   {
+      IStructuredSelection selection = (IStructuredSelection)sortTables.getSelection();
+      if (selection.size() != 1)
+         return;
+      
+      String element = (String)selection.getFirstElement();
+      int index = currList.indexOf(element);
+      if (index <= 0)
+         return;
+      
+      String a = currList.get(index - 1);
+      currList.set(index - 1, currList.get(index));
+      currList.set(index, a);
+      sortTables.setInput(currList.toArray());
+   }
+   
+   /**
+    * Move selected field down 
+    */
+   private void moveDown()
+   {
+      IStructuredSelection selection = (IStructuredSelection)sortTables.getSelection();
+      if (selection.size() != 1)
+         return;
+      
+      String element = (String)selection.getFirstElement();
+      int index = currList.indexOf(element);
+      if (index >= (currList.size() -1) && index < 0)
+         return;
+      
+      String a = currList.get(index + 1);
+      currList.set(index + 1, currList.get(index));
+      currList.set(index, a);
+      sortTables.setInput(currList.toArray());
+   }
+   
+   /**
+    * Add new sorting column 
+    */
+   private void add()
+   {
+      InputDialog dlg = new InputDialog(getShell(), "Add sorting table", 
+            "Sorting table name", "", null); //$NON-NLS-1$
+      if (dlg.open() == Window.OK)
+      {
+         String s = dlg.getValue();
+         if (!currList.contains(s))
+         {
+            currList.add(s);
+            sortTables.setInput(currList.toArray());
+         }
+      }
+   }
+   
+   /**
+    * Edit new sorting column 
+    */
+   private void edit()
+   {
+      IStructuredSelection selection = (IStructuredSelection)sortTables.getSelection();
+      if (selection.size() != 1)
+         return;
 
+      String element = (String)selection.getFirstElement();
+      InputDialog dlg = new InputDialog(getShell(), "Edit sorting table", 
+            "Sorting table name", element, null); //$NON-NLS-1$
+      if (dlg.open() == Window.OK)
+      {
+         String s = dlg.getValue();
+         int index = currList.indexOf(element);
+         if (index < currList.size() && index >= 0)
+         {
+            currList.set(index, s);
+            sortTables.setInput(currList.toArray());
+         }
+      }
+   }
+   
+   /**
+    * Remove column for sorting
+    */
+   private void remove()
+   {
+      IStructuredSelection selection = (IStructuredSelection)sortTables.getSelection();
+      if (selection.size() != 1)
+         return;
+      
+      String element = (String)selection.getFirstElement();
+      currList.remove(element);
+      sortTables.setInput(currList.toArray());
+   }
+   
        /* (non-Javadoc)
         * @see org.eclipse.jface.preference.PreferencePage#performOk()
         */
@@ -92,6 +365,8 @@ public class DciSummaryTable extends PropertyPage
                config.setBaseObjectId(objectSelector.getObjectId());
                config.setTableId(tableSelector.getTableId());
                config.setRefreshInterval(refreshInterval.getSelection());
+      config.setNumRowShown(numRowsShow.getSelection());
+      config.setSortingColumnList(currList);
                return true;
        }
 }
index f18d02f..3f10465 100644 (file)
@@ -56,6 +56,8 @@ public class DciSummaryTableElement extends ElementWidget
                setLayout(layout);
 
                SummaryTableWidget viewer = new SummaryTableWidget(this, SWT.NONE, viewPart, config.getTableId(), config.getBaseObjectId());
+               viewer.setShowNumLine(config.getNumRowShown());
+               viewer.setSortColumns(config.getSortingColumnList());
                viewer.setAutoRefresh(config.getRefreshInterval());
                viewer.refresh();
        }
index f3f6873..12a4396 100644 (file)
@@ -20,10 +20,13 @@ package org.netxms.ui.eclipse.dashboard.widgets.internal;
 
 import java.io.StringWriter;
 import java.io.Writer;
+import java.util.ArrayList;
+import java.util.List;
 import java.util.Map;
 import java.util.Set;
 import org.netxms.ui.eclipse.dashboard.dialogs.helpers.ObjectIdMatchingData;
 import org.simpleframework.xml.Element;
+import org.simpleframework.xml.ElementList;
 import org.simpleframework.xml.Serializer;
 import org.simpleframework.xml.core.Persister;
 
@@ -40,6 +43,12 @@ public class DciSummaryTableConfig extends DashboardElementConfig
 
    @Element(required=false)
    private int refreshInterval = 0;
+   
+   @Element(required=false)
+   private int numRowShown = 0;
+   
+   @ElementList(required = false,inline=true)
+   private List<String> SortingColumnList = new ArrayList<String>();
 
        /**
         * Create line chart settings object from XML document
@@ -136,4 +145,36 @@ public class DciSummaryTableConfig extends DashboardElementConfig
    {
       this.refreshInterval = refreshInterval;
    }
+
+   /**
+    * @return the numCollShown
+    */
+   public int getNumRowShown()
+   {
+      return numRowShown;
+   }
+
+   /**
+    * @param numCollShown the numCollShown to set
+    */
+   public void setNumRowShown(int numRowShown)
+   {
+      this.numRowShown = numRowShown;
+   }
+
+   /**
+    * @return the sortingColumnList
+    */
+   public List<String> getSortingColumnList()
+   {
+      return SortingColumnList;
+   }
+
+   /**
+    * @param sortingColumnList the sortingColumnList to set
+    */
+   public void setSortingColumnList(List<String> sortingColumnList)
+   {
+      SortingColumnList = sortingColumnList;
+   }
 }
index 58d266d..7e763c6 100644 (file)
@@ -20,6 +20,7 @@ package org.netxms.ui.eclipse.datacollection.widgets;
 
 import java.util.ArrayList;
 import java.util.Arrays;
+import java.util.Comparator;
 import java.util.List;
 import org.eclipse.core.runtime.IProgressMonitor;
 import org.eclipse.jface.action.Action;
@@ -46,6 +47,7 @@ import org.eclipse.ui.PartInitException;
 import org.eclipse.ui.PlatformUI;
 import org.netxms.client.NXCSession;
 import org.netxms.client.Table;
+import org.netxms.client.TableCell;
 import org.netxms.client.TableRow;
 import org.netxms.client.objects.AbstractObject;
 import org.netxms.ui.eclipse.actions.ExportToCsvAction;
@@ -84,6 +86,8 @@ public class SummaryTableWidget extends Composite
    private boolean useMultipliers = true;
    private TreeColumn currentColumn = null;
    private ObjectSelectionProvider objectSelectionProvider;
+   private int showLineCount;
+   private List<String> sortingColumnList = null;
 
    /**
     * Create summary table widget
@@ -318,9 +322,80 @@ public class SummaryTableWidget extends Composite
          viewer.setComparator(new SummaryTableItemComparator(table));
       }
       labelProvider.setColumnDataTypes(table.getColumnDataTypes());
-      viewer.setInput(table);
+      if(sortingColumnList != null && sortingColumnList.size() > 0)
+      {
+         List<SortItem> sortItem = new ArrayList<SortItem>();
+         for(int i = 0; i < sortingColumnList.size() ; i++)
+         {            
+            boolean isDesc = sortingColumnList.get(i).charAt(0) == '>' ? true : false;
+            int index = table.getColumnIndex(sortingColumnList.get(i).substring(1));
+            if(index >= 0)
+            {
+               sortItem.add(new SortItem(index, isDesc));
+            }
+         }
+         
+         //find index of columns to compare and desc or asc
+         final List<SortItem> sortItemFin = sortItem;
+         table.sort(new Comparator<TableRow>() {
+            public int compare(TableRow row1, TableRow row2)
+            {
+               //compare lines
+               int result = 0;
+               int i = 0;
+               while(result == 0 && i < sortItemFin.size())
+               {
+                  result = compareItem(row1, row2, sortItemFin.get(i).colIndex, sortItemFin.get(i).isDesc);
+                  i++;
+               }
+               return result;
+            }
+            
+            private int compareItem(TableRow row1, TableRow row2, int index, boolean sortDesc)
+            {
+               TableCell c1 = row1.get(index);
+               TableCell c2 = row2.get(index);
+
+               String s1 = (c1 != null) ? c1.getValue() : "";
+               String s2 = (c2 != null) ? c2.getValue() : "";
+               
+               int result = 0;
+               try
+               {
+                  double value1 = Double.parseDouble(s1);
+                  double value2 = Double.parseDouble(s2);
+                  result = Double.compare(value1, value2);
+               }
+               catch(NumberFormatException e)
+               {
+                  result = s1.compareToIgnoreCase(s2);
+               }
+               return sortDesc ? -result : result;
+            }
+         });
+         viewer.setComparator(null);
+      }
+      if(showLineCount > 0)
+         viewer.setInput(table.getFirstRows(showLineCount));
+      else
+         viewer.setInput(table);
       viewer.expandAll();
    }
+   
+   /**
+    * Class for advanced column sorting
+    */
+   class SortItem
+   {
+      public int colIndex;
+      public boolean isDesc;
+      
+      public SortItem(int colIndex, boolean isDesc)
+      {
+         this.colIndex = colIndex;
+         this.isDesc = isDesc;
+      }
+   };
 
    /**
     * @return the viewer
@@ -485,4 +560,24 @@ public class SummaryTableWidget extends Composite
          this.dciId = dciId;
       }
    }
+   
+   /**
+    * Set number of lines to show
+    * 
+    * @param lineCount line count to be shown
+    */
+   public void setShowNumLine(int lineCount)
+   {
+      this.showLineCount = lineCount;
+   }
+
+   /**
+    * Set columns that will be used to sort table
+    * 
+    * @param sortingColumnList columns to be used while sorting
+    */
+   public void setSortColumns(List<String> sortingColumnList)
+   {
+      this.sortingColumnList = sortingColumnList;      
+   }
 }