You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@directory.apache.org by pa...@apache.org on 2006/11/29 11:44:44 UTC

svn commit: r480515 - in /directory/sandbox/pamarcelot/ldapstudio/ldapstudio-browser/src/main/java/org/apache/directory/ldapstudio/browser/view/views: AttributesView.java AttributesViewSorter.java

Author: pamarcelot
Date: Wed Nov 29 02:44:43 2006
New Revision: 480515

URL: http://svn.apache.org/viewvc?view=rev&rev=480515
Log:
Adding sortable columns in the Attributes View.

Added:
    directory/sandbox/pamarcelot/ldapstudio/ldapstudio-browser/src/main/java/org/apache/directory/ldapstudio/browser/view/views/AttributesViewSorter.java
Modified:
    directory/sandbox/pamarcelot/ldapstudio/ldapstudio-browser/src/main/java/org/apache/directory/ldapstudio/browser/view/views/AttributesView.java

Modified: directory/sandbox/pamarcelot/ldapstudio/ldapstudio-browser/src/main/java/org/apache/directory/ldapstudio/browser/view/views/AttributesView.java
URL: http://svn.apache.org/viewvc/directory/sandbox/pamarcelot/ldapstudio/ldapstudio-browser/src/main/java/org/apache/directory/ldapstudio/browser/view/views/AttributesView.java?view=diff&rev=480515&r1=480514&r2=480515
==============================================================================
--- directory/sandbox/pamarcelot/ldapstudio/ldapstudio-browser/src/main/java/org/apache/directory/ldapstudio/browser/view/views/AttributesView.java (original)
+++ directory/sandbox/pamarcelot/ldapstudio/ldapstudio-browser/src/main/java/org/apache/directory/ldapstudio/browser/view/views/AttributesView.java Wed Nov 29 02:44:43 2006
@@ -21,6 +21,9 @@
 package org.apache.directory.ldapstudio.browser.view.views;
 
 
+import java.util.Comparator;
+import java.util.List;
+
 import org.apache.directory.ldapstudio.browser.Activator;
 import org.apache.directory.ldapstudio.browser.controller.AttributesViewController;
 import org.eclipse.jface.action.IToolBarManager;
@@ -49,8 +52,14 @@
     private Table table;
     private TableViewer viewer;
     private AttributesViewController controller;
+    private AttributesViewSorter sorter;
+    private TableColumn attributeColumn;
+    private TableColumn valueColumn;
 
 
+    /**
+     * Creates a new instance of AttributesView.
+     */
     public AttributesView()
     {
         controller = AttributesViewController.getInstance();
@@ -70,14 +79,15 @@
         viewer.setUseHashlookup( true );
 
         // Adding columns headers
-        TableColumn attributeColumn = new TableColumn( table, SWT.NONE );
+        attributeColumn = new TableColumn( table, SWT.NONE );
         attributeColumn.setText( "Attribute" );
-        TableColumn valueColumn = new TableColumn( table, SWT.NONE );
+        valueColumn = new TableColumn( table, SWT.NONE );
         valueColumn.setText( "Value" );
 
         // Initializing ContentProvider and LabelProvider
         viewer.setContentProvider( new AttributesViewContentProvider() );
         viewer.setLabelProvider( new AttributesViewLabelProvider() );
+        createTableSorter();
 
         // Displaying and resizing the columns
         resizeColumsToFit();
@@ -95,6 +105,47 @@
     }
 
 
+    /**
+     * Creates the table sorter
+     */
+    private void createTableSorter()
+    {
+        Comparator nameComparator = new Comparator()
+        {
+            @SuppressWarnings("unchecked")
+            public int compare( Object o1, Object o2 )
+            {
+                List<String> list1 = ( List<String> ) o1;
+                List<String> list2 = ( List<String> ) o2;
+
+                return list1.get( 0 ).compareTo( list2.get( 0 ) );
+            }
+        };
+
+        Comparator valueComparator = new Comparator()
+        {
+            @SuppressWarnings("unchecked")
+            public int compare( Object o1, Object o2 )
+            {
+                List<String> list1 = ( List<String> ) o1;
+                List<String> list2 = ( List<String> ) o2;
+
+                return list1.get( 1 ).compareTo( list2.get( 1 ) );
+            }
+        };
+
+        sorter = new AttributesViewSorter( viewer, new TableColumn[]
+            { attributeColumn, valueColumn }, new Comparator[]
+            { nameComparator, valueComparator } );
+
+        viewer.setSorter( sorter );
+
+    }
+
+
+    /**
+     * Creates the context menu
+     */
     private void createContextMenu()
     {
         // Initialization of the Menu Manager used to display context menu
@@ -108,6 +159,9 @@
     }
 
 
+    /**
+     * Create the toolbar buttons
+     */
     private void createToolbarButtons()
     {
         IToolBarManager toolBarManager = getViewSite().getActionBars().getToolBarManager();
@@ -153,6 +207,10 @@
     }
 
 
+    /**
+     * Gets the currently selected Attribute TableItem 
+     * @return the currently selected Attribute TableItem
+     */
     public TableItem getSelectedAttributeTableItem()
     {
         return table.getSelection()[0];

Added: directory/sandbox/pamarcelot/ldapstudio/ldapstudio-browser/src/main/java/org/apache/directory/ldapstudio/browser/view/views/AttributesViewSorter.java
URL: http://svn.apache.org/viewvc/directory/sandbox/pamarcelot/ldapstudio/ldapstudio-browser/src/main/java/org/apache/directory/ldapstudio/browser/view/views/AttributesViewSorter.java?view=auto&rev=480515
==============================================================================
--- directory/sandbox/pamarcelot/ldapstudio/ldapstudio-browser/src/main/java/org/apache/directory/ldapstudio/browser/view/views/AttributesViewSorter.java (added)
+++ directory/sandbox/pamarcelot/ldapstudio/ldapstudio-browser/src/main/java/org/apache/directory/ldapstudio/browser/view/views/AttributesViewSorter.java Wed Nov 29 02:44:43 2006
@@ -0,0 +1,134 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *  
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *  
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License. 
+ *  
+ */
+
+package org.apache.directory.ldapstudio.browser.view.views;
+
+
+import java.util.Comparator;
+
+import org.eclipse.jface.viewers.TableViewer;
+import org.eclipse.jface.viewers.Viewer;
+import org.eclipse.jface.viewers.ViewerSorter;
+import org.eclipse.swt.events.SelectionAdapter;
+import org.eclipse.swt.events.SelectionEvent;
+import org.eclipse.swt.widgets.TableColumn;
+
+
+/**
+ * This class implements the Sorter for the Attributes View
+ *
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ */
+public class AttributesViewSorter extends ViewerSorter
+{
+    /**
+     * Simple data structure for grouping sort information by column.
+     * 
+     * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+     */
+    private class SortableColumnInfo
+    {
+        Comparator comparator;
+        boolean descending;
+    }
+
+    /** The associated viewer */
+    private TableViewer viewer;
+
+    /** The information on the sortable columns */
+    private SortableColumnInfo[] infos;
+
+    /** The currently selected sorter */
+    private SortableColumnInfo currentSorter;
+
+
+    /**
+     * Creates a new instance of AttributesViewSorter.
+     *
+     * @param viewer the associated viewer
+     * @param columns the array containing the sortable columns
+     * @param comparators the array containing the associated comparators
+     */
+    public AttributesViewSorter( TableViewer viewer, TableColumn[] columns, Comparator[] comparators )
+    {
+        this.viewer = viewer;
+        infos = new SortableColumnInfo[columns.length];
+        for ( int i = 0; i < columns.length; i++ )
+        {
+            infos[i] = new SortableColumnInfo();
+            infos[i].comparator = comparators[i];
+            infos[i].descending = false;
+            createSelectionListener( columns[i], infos[i] );
+            if ( i == 0 )
+            {
+                currentSorter = infos[i];
+            }
+        }
+    }
+
+
+    /* (non-Javadoc)
+     * @see org.eclipse.jface.viewers.ViewerComparator#compare(org.eclipse.jface.viewers.Viewer, java.lang.Object, java.lang.Object)
+     */
+    @SuppressWarnings("unchecked")
+    public int compare( Viewer viewer, Object attribute1, Object attribute2 )
+    {
+        int result = currentSorter.comparator.compare( attribute1, attribute2 );
+        if ( currentSorter.descending )
+        {
+            return -result;
+        }
+        else
+        {
+            return result;
+        }
+    }
+
+
+    /**
+     * Adds a SelectionLister on the given column and launches sorting when a SelectionEvent is triggerd
+     *
+     * @param column
+     * @param info
+     */
+    private void createSelectionListener( TableColumn column, final SortableColumnInfo info )
+    {
+        column.addSelectionListener( new SelectionAdapter()
+        {
+            public void widgetSelected( SelectionEvent e )
+            {
+                sortUsing( info );
+            }
+        } );
+    }
+
+
+    /**
+     * Sorts the viewer using given sort information
+     *
+     * @param info the associated sort information
+     */
+    private void sortUsing( SortableColumnInfo info )
+    {
+        currentSorter = info;
+        currentSorter.descending = !currentSorter.descending;
+        viewer.refresh();
+    }
+}