Refactored view and sorter; changed ascending/descending in sorter
diff --git a/org.eclipse.photran.ui.vpg/src/org/eclipse/photran/internal/ui/views/vpgproblems/TableSorter.java b/org.eclipse.photran.ui.vpg/src/org/eclipse/photran/internal/ui/views/vpgproblems/TableSorter.java
index 57038c9..722a166 100644
--- a/org.eclipse.photran.ui.vpg/src/org/eclipse/photran/internal/ui/views/vpgproblems/TableSorter.java
+++ b/org.eclipse.photran.ui.vpg/src/org/eclipse/photran/internal/ui/views/vpgproblems/TableSorter.java
@@ -13,6 +13,7 @@
import org.eclipse.core.resources.IMarker;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.jface.viewers.ViewerSorter;
+import org.eclipse.photran.internal.ui.views.vpgproblems.VPGProblemView.VPGViewColumn;
import org.eclipse.ui.texteditor.MarkerUtilities;
/**
@@ -31,77 +32,87 @@
*/
public class TableSorter extends ViewerSorter
{
- private int propertyIndex;
- private boolean DESCENDING = true;
- private static final int LESS = -1;
- private static final int GREATER = 1;
- private static final int EQUAL = 0;
+ private int columnIndex;
+ private boolean ascending = true;
public TableSorter()
{
- this.propertyIndex = 0;
- this.DESCENDING = true;
+ this.columnIndex = 0;
+ this.ascending = true;
}
- public TableSorter(int columnIndex, boolean descending)
- {
- this.propertyIndex = columnIndex;
- this.DESCENDING = descending;
- }
-
+ /**
+ * Invoked when the user clicks on a column header. Responds by changing the sort order.
+ */
public void setColumn(int column)
{
- if(column == this.propertyIndex)
- {
- this.DESCENDING = !this.DESCENDING;
- }
+ if (column == this.columnIndex)
+ toggleSortOrder();
else
- {
- this.propertyIndex = column;
- this.DESCENDING = true;
- }
+ changeSortColumnTo(column);
}
+ private void toggleSortOrder()
+ {
+ this.ascending = !this.ascending;
+ }
+
+ private void changeSortColumnTo(int column)
+ {
+ this.columnIndex = column;
+ this.ascending = true;
+ }
+
+ /*
+ * Callback invoked to compare table items for sorting.
+ */
@Override
public int compare(Viewer viewer, Object e1, Object e2)
{
IMarker m1 = (IMarker)e1;
IMarker m2 = (IMarker)e2;
- int result = EQUAL;
-
- //Based on which column we want to sort, we compare different data.
- //For the list of columns, look in VPGProblemView.java
- switch(this.propertyIndex)
+ return compare(m1, m2);
+ }
+
+ private int compare(IMarker m1, IMarker m2)
+ {
+ if (this.ascending)
+ return compareAscending(m1, m2);
+ else
+ return compareDescending(m1, m2);
+ }
+
+ private int compareAscending(IMarker m1, IMarker m2)
+ {
+ switch (VPGViewColumn.values()[this.columnIndex])
{
- //FIRST COLUMN: Description
- case 0:
+ case DESCRIPTION:
{
String msg1 = MarkerUtilities.getMessage(m1);
String msg2 = MarkerUtilities.getMessage(m2);
- result = msg1.compareTo(msg2);
- break;
+ return msg1.compareTo(msg2);
}
- //SECOND COLUMN: Resource
- case 1:
+
+ case RESOURCE:
{
String resource1 = m1.getResource().getName().toString();
String resource2 = m2.getResource().getName().toString();
- result = resource1.compareTo(resource2);
- break;
+ return resource1.compareTo(resource2);
}
- //THIRD COLUMN: Path
- case 2:
+
+ case PATH:
{
String path1 = m1.getResource().getProjectRelativePath().toString();
String path2 = m2.getResource().getProjectRelativePath().toString();
- result = path1.compareTo(path2);
- break;
+ return path1.compareTo(path2);
}
- }//end switch
-
- //If it needs to be ascending sort, simply flip the resulting value
- if(!this.DESCENDING)
- result = -result;
- return result;
+
+ default: throw new IllegalStateException();
+ }
+ }
+
+ private int compareDescending(IMarker m1, IMarker m2)
+ {
+ return -compareAscending(m1, m2);
}
}
diff --git a/org.eclipse.photran.ui.vpg/src/org/eclipse/photran/internal/ui/views/vpgproblems/VPGProblemView.java b/org.eclipse.photran.ui.vpg/src/org/eclipse/photran/internal/ui/views/vpgproblems/VPGProblemView.java
index 4147578..6dd5138 100644
--- a/org.eclipse.photran.ui.vpg/src/org/eclipse/photran/internal/ui/views/vpgproblems/VPGProblemView.java
+++ b/org.eclipse.photran.ui.vpg/src/org/eclipse/photran/internal/ui/views/vpgproblems/VPGProblemView.java
@@ -34,8 +34,8 @@
import org.eclipse.rephraserengine.core.vpg.eclipse.VPGSchedulingRule;
import org.eclipse.swt.SWT;
import org.eclipse.swt.dnd.Clipboard;
+import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
-import org.eclipse.swt.events.MouseListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
@@ -50,10 +50,15 @@
/**
* Fortran Analysis/Refactoring Problems view, A.K.A. VPG Problems view.
* <p>
- * Most of the code was copied from Eclipse JFace TableView Tutorial
- * (http://www.vogella.de/articles/EclipseJFaceTable/aritcle.html) and
- * Java Developer's Guide to Eclipse, Chapter 18,
- * (http://www.jdg2e.com/jdg2e_CD_for_eclipse321/plug-in_development/examples/com.ibm.jdg2e.view.marker/src-marker/com/ibm/jdg2e/view/marker/MarkerView.java)
+ * Based on Eclipse JFace TableView Tutorial; thanks to Lars Vogel
+ * for posting the tutorial
+ * (http://www.vogella.de/articles/EclipseJFaceTable/aritcle.html)
+ * Based on samples provided in Java DeveloperÕs Guide to Eclipse,
+ * Chapter 18 (http://www.jdg2e.com/ch18.views/doc/index.htm and
+ * http://www.jdg2e.com/jdg2e_CD_for_eclipse321/plug-in_development/examples/com.ibm.jdg2e.view.marker/src-marker/com/ibm/jdg2e/view/marker/MarkerView.java)
+ * © Copyright International Business Machines Corporation, 2003, 2004, 2006.
+ * All Rights Reserved.
+ * Code or samples provided therein are provided without warranty of any kind.
*
* @author Timofey Yuvashev
*
@@ -64,6 +69,24 @@
*/
public class VPGProblemView extends ViewPart implements VPGLog.ILogListener
{
+ static enum VPGViewColumn
+ {
+ DESCRIPTION("Description", 44),
+ RESOURCE("Resource", 10),
+ PATH("Path", 20);
+
+ public final String name;
+ public final int width;
+
+ private VPGViewColumn(String name, int width)
+ {
+ this.name = name;
+ this.width = width;
+ }
+ }
+
+ private static RecreateMarkers markersTask = null;
+
private TableViewer tableViewer = null;
private TableSorter tableSorter = null;
private Clipboard clipboard = null;
@@ -76,45 +99,26 @@
private VPGViewFilterAction warningsMarkerFilterAction = null;
private VPGViewFilterAction errorsMarkerFilterAction = null;
- private static final String[] COLUMN_NAMES = {"Description",
- "Resource", "Path" /*,
- "Location"*/};
- private static final int[] COLUMN_WIDTHS = {44,
- 10, 20/*,
- 6*/};
-
//TODO: Depending on how we will handle updates to markers, we might need a
// way to update this array. Currently, it is populated as Workbench's start-time
// and remains unchaged since then
public static int[] MARKER_COUNT = {0,0,0}; //Number of Infos, Warnings and Errors respectively
-
-// /* (non-Javadoc)
-// * @see org.eclipse.ui.part.WorkbenchPart#createPartControl(org.eclipse.swt.widgets.Composite)
-// */
@Override
public void createPartControl(Composite parent)
{
GridLayout overallLayout = new GridLayout(1,false);
parent.setLayout(overallLayout);
- tableViewer = new TableViewer(parent, SWT.H_SCROLL | SWT.V_SCROLL
- | SWT.MULTI | SWT.FULL_SELECTION);
-
- tableSorter = new TableSorter();
- tableViewer.setComparator(this.tableSorter);
-
+ createTableViewer(parent);
createTableColumns(tableViewer);
setTableGridData();
- //Share Viewer Selection with other workbench parts
getSite().setSelectionProvider(tableViewer);
//TODO: Change the default string
- MenuManager manager = new VPGProblemContextMenu(getViewSite(), "Problems View Menu");
- tableViewer.getTable().setMenu(manager.createContextMenu(tableViewer.getTable()));
+ MenuManager manager = createMenuManager();
- //Register Viewer ContextMenu with other workbench parts
getSite().registerContextMenu(manager, tableViewer);
tableViewer.setContentProvider(new VPGProblemContentProvider());
@@ -126,7 +130,21 @@
initEvents();
}
- private static RecreateMarkers markersTask = null;
+ private void createTableViewer(Composite parent)
+ {
+ tableViewer = new TableViewer(parent, SWT.H_SCROLL | SWT.V_SCROLL
+ | SWT.MULTI | SWT.FULL_SELECTION);
+
+ tableSorter = new TableSorter();
+ tableViewer.setComparator(this.tableSorter);
+ }
+
+ private MenuManager createMenuManager()
+ {
+ MenuManager manager = new VPGProblemContextMenu(getViewSite(), "Problems View Menu");
+ tableViewer.getTable().setMenu(manager.createContextMenu(tableViewer.getTable()));
+ return manager;
+ }
public void onLogChange()
{
@@ -296,10 +314,8 @@
private TableLayout createTableLayout()
{
TableLayout layout = new TableLayout();
- for(int i = 0; i < COLUMN_WIDTHS.length; i++)
- {
- layout.addColumnData(new ColumnWeightData(COLUMN_WIDTHS[i],true));
- }
+ for (VPGViewColumn col : VPGViewColumn.values())
+ layout.addColumnData(new ColumnWeightData(col.width, true));
return layout;
}
@@ -314,83 +330,73 @@
TableLayout layout = createTableLayout();
table.setLayout(layout);
- for(int i = 0; i < COLUMN_NAMES.length; i++)
+ for (final VPGViewColumn vpgCol : VPGViewColumn.values())
{
- //We need these variables to be final, b/c we want to use them later on in the
- // definition of widgetSelected() method
- final int index = i;
- final TableViewerColumn viewerColumn = new TableViewerColumn(viewer, SWT.NONE);
- final TableColumn column = viewerColumn.getColumn();
+ final TableColumn viewerCol = new TableViewerColumn(viewer, SWT.NONE).getColumn();
- //Create column and set its parameters
- column.setText(COLUMN_NAMES[i]);
- column.setToolTipText(COLUMN_NAMES[i]);
- column.setAlignment(SWT.LEFT);
- column.setResizable(true);
- column.setMoveable(true);
+ viewerCol.setText(vpgCol.name);
+ viewerCol.setToolTipText(vpgCol.name);
+ viewerCol.setAlignment(SWT.LEFT);
+ viewerCol.setResizable(true);
+ viewerCol.setMoveable(true);
- //Add an even listener to the column
- column.addSelectionListener(new SelectionAdapter()
- {
- public void widgetSelected(SelectionEvent e)
- {
- tableSorter.setColumn(index);
- int dir = viewer.getTable().getSortDirection();
- if(viewer.getTable().getSortColumn() == column)
- dir = dir == SWT.UP ? SWT.DOWN : SWT.UP;
- else
- dir = SWT.DOWN;
- viewer.getTable().setSortDirection(dir);
- viewer.getTable().setSortColumn(column);
- viewer.refresh();
- }
- });
+ viewerCol.addSelectionListener(new ColumnSelectionListener(viewerCol, vpgCol, viewer));
}
table.setLinesVisible(true);
table.setHeaderVisible(true);
}
+ private final class ColumnSelectionListener extends SelectionAdapter
+ {
+ private final TableColumn column;
+ private final VPGViewColumn col;
+ private final TableViewer viewer;
+
+ private ColumnSelectionListener(TableColumn column, VPGViewColumn col, TableViewer viewer)
+ {
+ this.column = column;
+ this.col = col;
+ this.viewer = viewer;
+ }
+
+ public void widgetSelected(SelectionEvent e)
+ {
+ tableSorter.setColumn(col.ordinal());
+ int dir = viewer.getTable().getSortDirection();
+ if(viewer.getTable().getSortColumn() == column)
+ dir = dir == SWT.UP ? SWT.DOWN : SWT.UP;
+ else
+ dir = SWT.DOWN;
+ viewer.getTable().setSortDirection(dir);
+ viewer.getTable().setSortColumn(column);
+ viewer.refresh();
+ }
+ }
+
/*
* Initializes event Listeners and Actions for the Table
*/
private void initEvents()
{
- tableViewer.getTable().addMouseListener(new MouseListener()
- {
- public void mouseDoubleClick(MouseEvent dblClick)
- {
- Table t = (Table)dblClick.getSource();
- TableItem[] selection = t.getSelection();
- for(int i = 0; i < selection.length; i++)
- {
- if (selection[i].getData() instanceof IMarker)
- {
- IMarker marker = (IMarker)(selection[i].getData());
- if (marker.getResource() != null)
- {
- try
- {
- OpenMarkedFileAction openAction = new OpenMarkedFileAction(getViewSite());
- openAction.run(marker);
- }
- catch (Throwable x)
- {
- ;
- }
- }
- }
+ tableViewer.getTable().addMouseListener(new DoubleClickListener());
+ }
- }
- }
+ private final class DoubleClickListener extends MouseAdapter
+ {
+ public void mouseDoubleClick(MouseEvent dblClick)
+ {
+ Table t = (Table)dblClick.getSource();
+ for (TableItem item : t.getSelection())
+ if (item.getData() instanceof IMarker)
+ openMarker((IMarker)item.getData());
+ }
- public void mouseDown(MouseEvent arg0)
- {}
-
- public void mouseUp(MouseEvent arg0)
- {}
-
- });
+ private void openMarker(IMarker marker)
+ {
+ if (marker.getResource() != null)
+ new OpenMarkedFileAction(getViewSite()).run(marker);
+ }
}
public Clipboard getClipboard()
@@ -400,9 +406,6 @@
return clipboard;
}
- /* (non-Javadoc)
- * @see org.eclipse.ui.part.WorkbenchPart#setFocus()
- */
@Override
public void setFocus()
{
@@ -413,7 +416,7 @@
@Override
public void dispose()
{
- if(clipboard != null)
+ if (clipboard != null)
clipboard.dispose();
super.dispose();
}