X-Git-Url: http://git.argeo.org/?a=blobdiff_plain;f=eclipse%2Fplugins%2Forg.argeo.slc.client.ui%2Fsrc%2Fmain%2Fjava%2Forg%2Fargeo%2Fslc%2Fclient%2Fui%2Fviews%2FResultListView.java;h=cf83ebf0141a3f86cabc481371cc725a49ca498c;hb=918580bf6039346b6bd6891d475c77487a702d4b;hp=d49d67026942d3677def6753bf882df44fb25b26;hpb=a857d591b8658b2f94ec9398b94e9447f130eb9d;p=gpl%2Fargeo-slc.git diff --git a/eclipse/plugins/org.argeo.slc.client.ui/src/main/java/org/argeo/slc/client/ui/views/ResultListView.java b/eclipse/plugins/org.argeo.slc.client.ui/src/main/java/org/argeo/slc/client/ui/views/ResultListView.java index d49d67026..cf83ebf01 100644 --- a/eclipse/plugins/org.argeo.slc.client.ui/src/main/java/org/argeo/slc/client/ui/views/ResultListView.java +++ b/eclipse/plugins/org.argeo.slc.client.ui/src/main/java/org/argeo/slc/client/ui/views/ResultListView.java @@ -2,14 +2,17 @@ package org.argeo.slc.client.ui.views; import java.text.SimpleDateFormat; import java.util.ArrayList; -import java.util.HashMap; +import java.util.Iterator; import java.util.List; -import java.util.Map; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import org.argeo.ArgeoException; +import org.argeo.eclipse.ui.GenericTableComparator; import org.argeo.slc.SlcException; import org.argeo.slc.client.ui.ClientUiPlugin; +import org.argeo.slc.client.ui.commands.RemoveSelectedResultFromResultList; +import org.argeo.slc.client.ui.commands.ResultDetailsDisplayHandler; import org.argeo.slc.core.test.tree.ResultAttributes; import org.argeo.slc.dao.test.tree.TreeTestResultCollectionDao; import org.eclipse.core.commands.Command; @@ -31,8 +34,9 @@ import org.eclipse.jface.viewers.SelectionChangedEvent; import org.eclipse.jface.viewers.TableViewer; import org.eclipse.jface.viewers.Viewer; import org.eclipse.swt.SWT; +import org.eclipse.swt.events.SelectionAdapter; +import org.eclipse.swt.events.SelectionEvent; import org.eclipse.swt.graphics.Image; -import org.eclipse.swt.layout.GridData; import org.eclipse.swt.widgets.Composite; import org.eclipse.swt.widgets.Menu; import org.eclipse.swt.widgets.Table; @@ -51,21 +55,30 @@ public class ResultListView extends ViewPart { public static final String ID = "org.argeo.slc.client.ui.resultListView"; - private final static String DISPLAY_CMD_ID = "org.argeo.slc.client.ui.displayResultDetails"; + // TODO : remove dependency from SLC on Sparta private final static String DISPLAY_AS_XLS_CMD_ID = "com.capco.sparta.client.ui.displayResultDetailsWithExcel"; private final static String SAVE_AS_XLS_CMD_ID = "com.capco.sparta.client.ui.saveResultAsExcelFile"; - private final static String UUID_PARAM_ID = "org.argeo.slc.client.commands.resultUuid"; - private final static String NAME_PARAM_ID = "org.argeo.slc.client.commands.resultName"; + private final static String PLATFORM = SWT.getPlatform(); + private final static String DISPLAY_CMD_ID = ResultDetailsDisplayHandler.ID; + private final static String REMOVE_CMD_ID = RemoveSelectedResultFromResultList.ID; +// private final static String UUID_PARAM_ID = "org.argeo.slc.client.commands.resultUuid"; +// private final static String NAME_PARAM_ID = "org.argeo.slc.client.commands.resultName"; + private final static SimpleDateFormat dateFormatter = new SimpleDateFormat( - "MM/dd/yyyy 'at' HH:mm:ss"); + "MM/dd/yy', 'HH:mm:ss"); private TableViewer viewer; private TreeTestResultCollectionDao testResultCollectionDao; + private CurrentTableComparator comparator; + private ResultAttributes selectedRa; + // handle locally which result are shown or not + private List removedResults = new ArrayList(); + public void createPartControl(Composite parent) { Table table = createTable(parent); viewer = new TableViewer(table); @@ -75,8 +88,13 @@ public class ResultListView extends ViewPart { viewer.addDoubleClickListener(new ViewDoubleClickListener()); viewer.addSelectionChangedListener(new SelectionChangedListener()); - // create the context menu + // Initializes sort mecanism + // by default we sort by date asc + comparator = new CurrentTableComparator(1, + GenericTableComparator.ASCENDING); + viewer.setComparator(comparator); + // create the context menu MenuManager menuManager = new MenuManager(); Menu menu = menuManager.createContextMenu(viewer.getControl()); menuManager.addMenuListener(new IMenuListener() { @@ -90,16 +108,18 @@ public class ResultListView extends ViewPart { } protected Table createTable(Composite parent) { - int style = SWT.SINGLE | SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL - | SWT.FULL_SELECTION | SWT.HIDE_SELECTION; + int style = SWT.MULTI | SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL + | SWT.FULL_SELECTION; + // does not function with RAP, commented for the moment being + // | SWT.HIDE_SELECTION; Table table = new Table(parent, style); // table.addMouseListener(new RightClickListener()); - //GridData gridData = new GridData(GridData.FILL_BOTH); - //gridData.grabExcessVerticalSpace = true; - //gridData.grabExcessHorizontalSpace = true; - //gridData.horizontalSpan = 3; - //table.setLayoutData(gridData); + // GridData gridData = new GridData(GridData.FILL_BOTH); + // gridData.grabExcessVerticalSpace = true; + // gridData.grabExcessHorizontalSpace = true; + // gridData.horizontalSpan = 3; + // table.setLayoutData(gridData); table.setLinesVisible(true); table.setHeaderVisible(true); @@ -107,37 +127,86 @@ public class ResultListView extends ViewPart { TableColumn column = new TableColumn(table, SWT.LEFT, 0); column.setText("Test Case"); column.setWidth(200); + column.addSelectionListener(getSelectionAdapter(column, 0)); column = new TableColumn(table, SWT.LEFT, 1); column.setText("Close Date"); - column.setWidth(120); + column.setWidth(130); + column.addSelectionListener(getSelectionAdapter(column, 1)); column = new TableColumn(table, SWT.LEFT, 2); column.setText("UUID"); column.setWidth(300); + column.addSelectionListener(getSelectionAdapter(column, 2)); return table; } - // TODO : Improve this methods. - // For now it is a workaround because we cannot dynamically update context - // menu to pass the UUID as command parameter - // public String[] getSelectedResult() { - // Object obj = ((IStructuredSelection) viewer.getSelection()) - // .getFirstElement(); - // - // String[] attributes = new String[2]; - // - // if (obj == null || !(obj instanceof ResultAttributes)) - // return null; - // else { - // ResultAttributes ra = (ResultAttributes) obj; - // attributes[0] = ra.getUuid(); - // attributes[1] = (ra.getAttributes().get("testCase") == null) ? null - // : ra.getAttributes().get("testCase"); - // return attributes; - // } - // } + // Manage list of the result + public void setFocus() { + viewer.getControl().setFocus(); + } + + /** Initialize the view **/ + public void retrieveResults() { + try { + List lst = testResultCollectionDao + .listResultAttributes(null); + if (log.isTraceEnabled()) + log.trace("Result attributes count: " + lst.size()); + + // handle removed result from the list locally + // Might be optimised. + Iterator it = removedResults.iterator(); + removeResult: while (it.hasNext()) { + ResultAttributes curRa = it.next(); + String curRaId = curRa.getUuid(); + for (Iterator it2 = lst.iterator(); it2 + .hasNext();) { + ResultAttributes tmpRa = it2.next(); + if (tmpRa.getUuid().equals(curRaId)) { + lst.remove(tmpRa); + continue removeResult; + } + } + if (lst.contains(curRa)) + lst.remove(curRa); + } + viewer.setInput(lst); + } catch (Exception e) { + throw new ArgeoException("Cannot refresh the result List", e); + } + } + + public List getSelectedResultsId() { + List resultIds = new ArrayList(); + IStructuredSelection curSelection = (IStructuredSelection) viewer + .getSelection(); + Iterator it = curSelection.iterator(); + while (it.hasNext()) { + ResultAttributes curResult = (ResultAttributes) it.next(); + resultIds.add(curResult.getUuid()); + } + return resultIds; + } + + /** + * Remove selected items from the result list. This process is handled + * locally in the UI side : trying to handle that on the server side throws + * HibernateLazyInit exception, especially when trying to access + * TreeTestResultCollection items. + */ + public void removeSelected() { + IStructuredSelection curSelection = (IStructuredSelection) viewer + .getSelection(); + Iterator it = curSelection.iterator(); + while (it.hasNext()) { + ResultAttributes curResult = (ResultAttributes) it.next(); + if (!removedResults.contains(curResult)) + removedResults.add(curResult); + } + retrieveResults(); + } // View Specific inner class @@ -155,7 +224,10 @@ public class ResultListView extends ViewPart { } } - private void refreshCommand(IMenuManager menuManager, + // Relatively useless in the current case. + // Yet we keep it as sample code to show how we can implement a context menu + // which commands are dynamically linked to the selected item of a control + protected void refreshCommand(IMenuManager menuManager, IServiceLocator locator, String cmdId, String label, String iconPath) { IContributionItem ici = menuManager.find(cmdId); if (ici != null) @@ -167,13 +239,16 @@ public class ResultListView extends ViewPart { contributionItemParameter.label = label; contributionItemParameter.icon = ClientUiPlugin .getImageDescriptor(iconPath); + - Map params = new HashMap(); - params.put(UUID_PARAM_ID, selectedRa.getUuid()); - params.put(NAME_PARAM_ID, - (selectedRa.getAttributes().get("testCase") == null) ? null - : selectedRa.getAttributes().get("testCase")); - contributionItemParameter.parameters = params; + // if (!REMOVE_CMD_ID.equals(cmdId)) { + // Map params = new HashMap(); + // params.put(UUID_PARAM_ID, selectedRa.getUuid()); + // params.put(NAME_PARAM_ID, + // (selectedRa.getAttributes().get("testCase") == null) ? null + // : selectedRa.getAttributes().get("testCase")); + // contributionItemParameter.parameters = params; + // } CommandContributionItem cci = new CommandContributionItem( contributionItemParameter); @@ -181,24 +256,31 @@ public class ResultListView extends ViewPart { menuManager.add(cci); } - private void contextMenuAboutToShow(IMenuManager menuManager) { + /** + * Defines the commands that will pop up in the context menu. Might be + * overriden by user to add or remove certain commands. + **/ + protected void contextMenuAboutToShow(IMenuManager menuManager) { IWorkbenchWindow window = ClientUiPlugin.getDefault().getWorkbench() .getActiveWorkbenchWindow(); refreshCommand(menuManager, window, DISPLAY_CMD_ID, - "Display as a tree", "icons/result_details.gif"); + "Display selected as tree", "icons/result_details.gif"); // We only show this command on windows OS if (PLATFORM.equals("win32") || PLATFORM.equals("wpf")) { refreshCommand(menuManager, window, DISPLAY_AS_XLS_CMD_ID, - "Display with Excel", "icons/excel.png"); + "Display selected with Excel", "icons/excel.png"); } refreshCommand(menuManager, window, SAVE_AS_XLS_CMD_ID, - "Save as Excel file", "icons/excel.png"); + "Save selected as Excel files", "icons/excel.png"); + + refreshCommand(menuManager, window, REMOVE_CMD_ID, "Remove selected", + "icons/removeAll.png"); + } // Providers - protected static class ViewContentProvider implements IStructuredContentProvider { @@ -243,22 +325,72 @@ public class ResultListView extends ViewPart { } - public void setFocus() { - viewer.getControl().setFocus(); - } + // Sort Mecanism + private class CurrentTableComparator extends GenericTableComparator { - public void retrieveResults() { - try { - List lst = testResultCollectionDao - .listResultAttributes(null); - if (log.isTraceEnabled()) - log.trace("Result attributes count: " + lst.size()); - viewer.setInput(lst); - // viewer.refresh(); - } catch (Exception e) { - // TODO Auto-generated catch block - e.printStackTrace(); + public CurrentTableComparator(int colIndex, int direction) { + super(colIndex, direction); } + + @Override + public int compare(Viewer viewer, Object r1, Object r2) { + + int rc = 0; + ResultAttributes ra1 = (ResultAttributes) r1; + ResultAttributes ra2 = (ResultAttributes) r2; + + switch (propertyIndex) { + case 0: + if (ra1.getAttributes().get("testCase") == null) + rc = -1; + else if (ra2.getAttributes().get("testCase") == null) + rc = 1; + else + rc = ra1.getAttributes().get("testCase") + .compareTo(ra2.getAttributes().get("testCase")); + break; + case 1: + // result with close date == null are put at the end : either + // they are nor finished or in error + if (ra1.getCloseDate() == null) + rc = 1; + else if (ra2.getCloseDate() == null) + rc = -1; + else + rc = ra1.getCloseDate().compareTo(ra2.getCloseDate()); + break; + case 2: + rc = ra1.getUuid().compareTo(ra2.getUuid()); + break; + } + + if (direction == DESCENDING) { + rc = -rc; + } + return rc; + } + } + + private SelectionAdapter getSelectionAdapter(final TableColumn column, + final int index) { + + SelectionAdapter selectionAdapter = new SelectionAdapter() { + @Override + public void widgetSelected(SelectionEvent e) { + + comparator.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(); + } + }; + return selectionAdapter; } // Handle Events