Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Remote Application Platform (RAP) » Row remains highlighted after delete item in TableViewer(Using the TableViewer, I select a row, it highlights, and then we click an icon to delete the item from our database. I then set the TableViewer input to an updated list from the database. Row is dele)
Row remains highlighted after delete item in TableViewer [message #821820] Thu, 15 March 2012 21:32 Go to next message
George R Malary is currently offline George R MalaryFriend
Messages: 36
Registered: March 2012
Location: New Jersey
Member
This is my first topic, so please bear with me Very Happy

Using the TableViewer, I select a row, it highlights, and then we click an icon to delete the item from our database. I then set the TableViewer input to an updated list from the database. Row is deleted, and viewer displays the correct list, but the row of the deleted item remains highlighted, and cannot be deselected.

Below is code. Code of interest is createViewer() where we add the listener for when the delete icon is clicked, and then method deleteNode() is called.

I have tried deleting and deselecting the selected item from viewer.getTable(). I'm still not sure the underlying cause of why the row remains highlighted after I reset the viewer input. Attached is the table and the highlighted row after deletion.


        private Table table;

	/**
	 * @param parent
	 *            parent
	 * @param style
	 *            style
	 * @param workbenchPartSite
	 *            getSite(), if not null
	 */
	public NodeOverviewComposite(final Composite parent, final int style,
			final IWorkbenchSite workbenchPartSite) {
		super(parent, style);
		partSite = workbenchPartSite;

		GridLayout layout = new GridLayout();
		setLayout(layout);
		createViewer(this);

		if (partSite != null) {
			makeActions();
			// Used for popup menu when right click on table.
			// hookContextMenu();
			contributeToActionBars();
		}

	}

	/**
	 * @param parent
	 *            parent composite for viewer.
	 */
	private void createViewer(final Composite parent) {
		viewer = new XSBTableViewer(this.getClass(), "nodeListTableViewer",
				parent, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL
						| SWT.FULL_SELECTION | SWT.BORDER);

		createColumns(parent, viewer);

		table = viewer.getTable();
		table.setHeaderVisible(true);
		table.setLinesVisible(true);
		createColumnsWeigthData(table);

		viewer.setContentProvider(new ArrayContentProvider());
		if (nodeModelProvider.getNodes().size() != 0) {
			viewer.setInput(nodeModelProvider.getNodes());
		}

		viewer.addSelectionChangedListener(new ISelectionChangedListener() {
			@Override
			public void selectionChanged(final SelectionChangedEvent event) {
				Table table = ((TableViewer) event.getSource()).getTable();
				Point pt = table.toControl(Display.getCurrent()
						.getCursorLocation());

				TableItem item = table.getItem(pt);
				if (item != null) {
					if (item.getBounds(EDIT_COLUMN_INDEX).contains(pt)) {
						// Place holder until we have an edit node dialog.
						showMessage(
								Messages.get().EditNodeDialog_dialog_title,
								NLS.bind(
										Messages.get().EditNodeDialog_dialog_message,
										item.getText(HOSTNAME_COLUMN_INDEX)));
					} else if (item.getBounds(DELETE_COLUMN_INDEX).contains(pt)) {
						XSBMessageDialog deleteNodeDialog = new XSBMessageDialog(
								parent.getShell(),
								Messages.get().DeleteNodeDialog_dialog_title,
								NLS.bind(
										Messages.get().DeleteNodeDialog_dialog_message,
										item.getText(HOSTNAME_COLUMN_INDEX)),
								MessageDialog.WARNING,
								new String[] {
										Messages.get().DeleteNodeDialog_btn_delete,
										Messages.get().DeleteNodeDialog_btn_cancel });
						int deleteReturnCode = deleteNodeDialog.open();
						// 0 if delete pressed, 1 is do no delete pressed.
						if (deleteReturnCode == 0) {
							deleteNode(event.getSelection());
						}

					}
				}

			}
		});

		if (partSite != null) {
			// Make the selection available to other views
			partSite.setSelectionProvider(viewer);
		}

		// Layout the viewer
		GridData gridData = new GridData(GridData.FILL_BOTH);
		viewer.getControl().setLayoutData(gridData);
	}

	/**
	 * This will create the columns for the table.
	 * 
	 * @param parent
	 *            parent composite for the table.
	 * @param viewer
	 *            table viewer for the columns.
	 */
	private void createColumns(final Composite parent, final TableViewer viewer) {

		// First column is for the node hostname.
		TableViewerColumn col = createTableViewerColumn(HOSTNAME_COLUMN_TITLE);
		col.setLabelProvider(new ColumnLabelProvider() {
			@Override
			public String getText(final Object element) {
				NodeDTO p = (NodeDTO) element;
				return p.getNodeAddress();
			}
		});

		// Second column is for the node type.
		col = createTableViewerColumn(NODE_TYPE_COLUMN_TITLE);
		col.setLabelProvider(new ColumnLabelProvider() {
			@Override
			public String getText(final Object element) {
				NodeDTO p = (NodeDTO) element;
				return E_NodeTypes.getTypeByInt(p.getType());
			}
		});

		// Third column is for the username.
		col = createTableViewerColumn(USERNAME_COLUMN_TITLE);
		col.setLabelProvider(new ColumnLabelProvider() {
			@Override
			public String getText(final Object element) {
				NodeDTO p = (NodeDTO) element;
				return p.getUserName();
			}
		});

		// Fourth column is for the edit icon.
		col = createTableViewerColumn(EDIT_COLUMN_TITLE);
		col.setLabelProvider(new ColumnLabelProvider() {
			@Override
			public Image getImage(final Object element) {
				return Activator.getDefault().getImageRegistry()
						.get(IImageKeys.ICON_EDIT);
			}

			@Override
			public String getText(final Object element) {
				return "";
			}
		});
		col.getColumn().setText("");
		col.getColumn().setMoveable(false);
		col.getColumn().setResizable(false);

		// Fifth column is for the delete icon.
		col = createTableViewerColumn(DELETE_COLUMN_TITLE);
		col.setLabelProvider(new ColumnLabelProvider() {
			@Override
			public Image getImage(final Object element) {
				return Activator.getDefault().getImageRegistry()
						.get(IImageKeys.ICON_TRASH);
			}

			@Override
			public String getText(final Object element) {
				return "";
			}
		});
		col.getColumn().setText("");
		col.getColumn().setMoveable(false);
		col.getColumn().setResizable(false);

	}

	/**
	 * Sets the column weights.
	 * 
	 * @param table
	 *            table for the node list data.
	 */
	public void createColumnsWeigthData(Table table) {
		TableLayout layout = new TableLayout();
		layout.addColumnData(new ColumnWeightData(4, 100, true));
		layout.addColumnData(new ColumnWeightData(8, 100, true));
		layout.addColumnData(new ColumnWeightData(4, 100, true));
		layout.addColumnData(new ColumnWeightData(1, 30, false));
		layout.addColumnData(new ColumnWeightData(1, 30, false));
		table.setLayout(layout);

	}

	/**
	 * @param title
	 *            title of column.
	 * @return Table Viewer Column
	 */
	private TableViewerColumn createTableViewerColumn(final String title) {
		final TableViewerColumn viewerColumn = new TableViewerColumn(viewer,
				SWT.NONE);
		final TableColumn column = viewerColumn.getColumn();
		column.setText(title);
		// column.setWidth(bound);
		column.setResizable(true);
		column.setMoveable(true);
		column.setData(WidgetUtil.CUSTOM_WIDGET_ID,
				"Column_" + title.replaceAll(" ", ""));
		return viewerColumn;
	}

        /**
	 * Setup the action bars for menu and toobar.
	 */
	private void contributeToActionBars() {
		IActionBars bars = ((IViewSite) partSite).getActionBars();
		fillLocalPullDown(bars.getMenuManager());
		fillLocalToolBar(bars.getToolBarManager());
	}

	/**
	 * Adds the actions to the menu.
	 * 
	 * @param manager
	 *            menu manager
	 */
	private void fillLocalPullDown(final IMenuManager manager) {
		manager.add(addNodeAction);
	}

/**
	 * Adds the actions to the toolbar of the viewer..
	 * 
	 * @param manager
	 *            menu manager
	 */
	private void fillLocalToolBar(final IToolBarManager manager) {
		manager.add(addNodeAction);
	}

	/**
	 * Creates the actions. Currently only add node actions defined for viewer.
	 */
	private void makeActions() {
		addNodeAction = new Action() {
			public void run() {
				AddNodeDialog addNodeDialog = new AddNodeDialog(partSite
						.getWorkbenchWindow().getShell(),
						Messages.get().AddNodeDialog_dialog_title,
						nodeModelProvider);
				if (addNodeDialog.open() == Window.OK) {
					if (nodeModelProvider.getNodes().size() != 0) {
						viewer.setInput(nodeModelProvider.getNodes());
					}
					viewer.refresh();
					showMessage(
							Messages.get().MessageDialog_ConfirmAddNode_title,
							Messages.get().MessageDialog_ConfirmAddNode_message);
				}
			}
		};
		addNodeAction.setText("Add Node");
		addNodeAction.setToolTipText("Add Node");
		addNodeAction.setImageDescriptor(PlatformUI.getWorkbench()
				.getSharedImages()
				.getImageDescriptor(ISharedImages.IMG_OBJ_ADD));
	}

	/**
	 * Takes the selection from the tableviewer and casts to NodeDTO. Attempts
	 * to delete NodeDTO. The resets input of the tableviewer to the new list of
	 * nodes.
	 * 
	 * @param selection
	 *            node selected from the table viewer.
	 * @return true if node deleted. false otherwise.
	 */
	public final boolean deleteNode(final ISelection selection) {
		boolean nodeDeleted = false;

		if (selection != null && selection instanceof IStructuredSelection) {
			IStructuredSelection sel = (IStructuredSelection) selection;
			NodeDTO selectedNode = (NodeDTO) sel.iterator().next();

			nodeDeleted = nodeModelProvider.deleteNode(selectedNode
					.getNodeName());

			viewer.setInput(nodeModelProvider.getNodes());

			if (nodeDeleted) {
				showMessage(
						Messages.get().MessageDialog_ConfirmDeleteNode_title,
						NLS.bind(
								Messages.get().MessageDialog_ConfirmDeleteNode_message,
								selectedNode.getNodeAddress()));
			} else {
				showMessage(
						Messages.get().MessageDialog_ConfirmDeleteNode_title,
						NLS.bind(
								Messages.get().MessageDialog_ConfirmDeleteNode_failed_message,
								selectedNode.getNodeAddress()));
			}

		}

		return nodeDeleted;
	}


Re: Row remains highlighted after delete item in TableViewer [message #821841 is a reply to message #821820] Thu, 15 March 2012 22:00 Go to previous messageGo to next message
Thorsten Schlathölter is currently offline Thorsten SchlathölterFriend
Messages: 312
Registered: February 2012
Location: Düsseldorf
Senior Member
The AbstractTableViewer tries to preserve the selection even if you set a new input. Although I do not understand why the viewer finds an item which is equal to the deleted item. Anyway: I would try to remove the selection before setting the new input.
Furthermore it seems to me like you pass the same model into the viewer after removal of the node. In that case a viewer.refresh() should be sufficient to reflect the model change.

So all together I would guess that the following should do it:

if (nodeDeleted) 
{
   // display message
   viewer.setSelection(StructuredSelection.EMPTY)
}
else
{
   // display message
}

// viewer.setInput(newInput);
viewer.refresh();

Re: Row remains highlighted after delete item in TableViewer [message #821900 is a reply to message #821841] Fri, 16 March 2012 00:02 Go to previous messageGo to next message
George R Malary is currently offline George R MalaryFriend
Messages: 36
Registered: March 2012
Location: New Jersey
Member
Hi Thorsten,

I am not passing the same model. I am calling NodeModelProvider.getNodes() which gets a new list of elements from a DB. A viewer.refresh() will not reflect the deleted node.

I tried the proposed solution, but that did not work. I also taught that clearing the selection before delete would unhighlight the row. Could the fact that a modal dialog is open at the time of attempting to remove the selection? I dont think that should matter. I even tried viewer.getTable().deselect on selected indeces, but still no success.



Re: Row remains highlighted after delete item in TableViewer [message #821903 is a reply to message #821841] Fri, 16 March 2012 00:02 Go to previous messageGo to next message
George R Malary is currently offline George R MalaryFriend
Messages: 36
Registered: March 2012
Location: New Jersey
Member
Hi Thorsten,

I am not passing the same model. I am calling NodeModelProvider.getNodes() which gets a new list of elements from a DB. A viewer.refresh() will not reflect the deleted node.

I tried the proposed solution, but that did not work. I also taught that clearing the selection before delete would unhighlight the row. Could the fact that a modal dialog is open at the time of attempting to remove the selection? I dont think that should matter. I even tried viewer.getTable().deselect on selected indeces, but still no success.
Re: Row remains highlighted after delete item in TableViewer [message #822093 is a reply to message #821903] Fri, 16 March 2012 06:56 Go to previous messageGo to next message
Thorsten Schlathölter is currently offline Thorsten SchlathölterFriend
Messages: 312
Registered: February 2012
Location: Düsseldorf
Senior Member
Uh. Can't say. I have much more experience on RCP platform. One important thing about viewers that I have learned is that the refresh is asynchron. If all this fails I would try to remove the selection after all pending UI refreshes and stuff. So maybe you want to give an async exec a try?

viewer.getControl().getDisplay().asycExec(new Runnable() {
   public void run() 
   {
      viewer.setSelection(StructuredSelection.EMPTY);
   }
});


And just to play with: have you tried to select something different rather than clearing the selection? Maybe select the first node or so. Just to check if selection works at all.
Re: Row remains highlighted after delete item in TableViewer [message #822096 is a reply to message #821900] Fri, 16 March 2012 06:56 Go to previous messageGo to next message
Thorsten Schlathölter is currently offline Thorsten SchlathölterFriend
Messages: 312
Registered: February 2012
Location: Düsseldorf
Senior Member
Uh. Can't say. I have much more experience on RCP platform. One important thing about viewers that I have learned is that the refresh is asynchron. If all this fails I would try to remove the selection after all pending UI refreshes and stuff. So maybe you want to give an async exec a try?


viewer.getControl().getDisplay().asycExec(new Runnable() {
public void run()
{
viewer.setSelection(StructuredSelection.EMPTY);
}
});


And just to play with: have you tried to select something different rather than clearing the selection? Maybe select the first node or so. Just to check if selection works at all.
Re: Row remains highlighted after delete item in TableViewer [message #822123 is a reply to message #822096] Fri, 16 March 2012 07:42 Go to previous messageGo to next message
Thomas Schindl is currently offline Thomas SchindlFriend
Messages: 6651
Registered: July 2009
Senior Member
Am 16.03.12 07:56, schrieb Thorsten Schlathölter:
> Uh. Can't say. I have much more experience on RCP platform. One
> important thing about viewers that I have learned is that the refresh is
> asynchron. If all this fails I would try to remove the selection after
> all pending UI refreshes and stuff. So maybe you want to give an async
> exec a try?

That's wrong viewer.refresh() is a synchronous call on the UI-Thread and
so is the complete Viewer-API.

Tom
Re: Row remains highlighted after delete item in TableViewer [message #822131 is a reply to message #822123] Fri, 16 March 2012 07:57 Go to previous messageGo to next message
Thorsten Schlathölter is currently offline Thorsten SchlathölterFriend
Messages: 312
Registered: February 2012
Location: Düsseldorf
Senior Member
Ups. Indeed. I thought it was asynch due to the runnables which are used allover the AbstractTableViewer to refresh. But they are not execed async. Sry for missleading and thanks for enlightning.
Re: Row remains highlighted after delete item in TableViewer [message #822136 is a reply to message #822123] Fri, 16 March 2012 07:57 Go to previous messageGo to next message
Thorsten Schlathölter is currently offline Thorsten SchlathölterFriend
Messages: 312
Registered: February 2012
Location: Düsseldorf
Senior Member
Ups. Indeed. I thought it was asynch due to the runnables which are used allover the AbstractTableViewer to refresh. But they are not execed async. Sry for missleading and thanks for enlightning.
Re: Row remains highlighted after delete item in TableViewer [message #822149 is a reply to message #822131] Fri, 16 March 2012 08:22 Go to previous messageGo to next message
Thorsten Schlathölter is currently offline Thorsten SchlathölterFriend
Messages: 312
Registered: February 2012
Location: Düsseldorf
Senior Member
Another thought: have you tried to set an empty model before setting the modified model. That way all items should get disposed before they are newly created. If it is a bug maybe this can serve as a workaround.
Re: Row remains highlighted after delete item in TableViewer [message #822153 is a reply to message #822131] Fri, 16 March 2012 08:22 Go to previous messageGo to next message
Thorsten Schlathölter is currently offline Thorsten SchlathölterFriend
Messages: 312
Registered: February 2012
Location: Düsseldorf
Senior Member
Another thought: have you tried to set an empty model before setting the modified model. That way all items should get disposed before they are newly created. If it is a bug maybe this can serve as a workaround.
Re: Row remains highlighted after delete item in TableViewer [message #822428 is a reply to message #822153] Fri, 16 March 2012 16:03 Go to previous messageGo to next message
George R Malary is currently offline George R MalaryFriend
Messages: 36
Registered: March 2012
Location: New Jersey
Member
Ok, I have this working, but am not clear as to why it works, and what the problem is. showMessage is just a mesage dialog saying the node had been deleted. Below is the code snippet that works:

if (nodeDeleted) {
    viewer.setSelection(StructuredSelection.EMPTY);
    showMessage(Messages.get().MessageDialog_ConfirmDeleteNode_title,
        NLS.bind(Messages.get().MessageDialog_ConfirmDeleteNode_message,
        selectedNode.getNodeAddress()));
    viewer.setInput(nodeModelProvider.getNodesFromDB());
}


So when user click delete on the confirm message dialog, and delete is successful in the backend, the selection is cleared, but the item is still visible in the viewer in the background because I have not reset the input. The message dialog appears confirming deletion, and then the viewer input is reset, and the viewer is updated, and selections are all cleared as expected.

Ideally, I want to clear the selection and update the viewer before showing the message dialog confirming deletion, so that the user doesnt still see his selection in the background. But I try the following, and I get the same weird behavior where the row is deleted but stays highlighted.

if (nodeDeleted) {
    viewer.setSelection(StructuredSelection.EMPTY);
    viewer.setInput(nodeModelProvider.getNodesFromDB());
    showMessage(Messages.get().MessageDialog_ConfirmDeleteNode_title,
        NLS.bind(Messages.get().MessageDialog_ConfirmDeleteNode_message,
        selectedNode.getNodeAddress()));
    
}


So I'm confused why it would work with the message dialog in between the setSelection and the setInput, and not when they are together. I also tried without the message dialog and the same error occurs. Seem buggy to me, unless I'm doing something wrong.
Re: Row remains highlighted after delete item in TableViewer [message #822431 is a reply to message #822153] Fri, 16 March 2012 16:03 Go to previous messageGo to next message
George R Malary is currently offline George R MalaryFriend
Messages: 36
Registered: March 2012
Location: New Jersey
Member
Ok, I have this working, but am not clear as to why it works, and what the problem is. showMessage is just a mesage dialog saying the node had been deleted. Below is the code snippet that works:


if (nodeDeleted) {
viewer.setSelection(StructuredSelection.EMPTY);
showMessage(Messages.get().MessageDialog_ConfirmDeleteNode_title,
NLS.bind(Messages.get().MessageDialog_ConfirmDeleteNode_message,
selectedNode.getNodeAddress()));
viewer.setInput(nodeModelProvider.getNodesFromDB());
}


So when user click delete on the confirm message dialog, and delete is successful in the backend, the selection is cleared, but the item is still visible in the viewer in the background because I have not reset the input. The message dialog appears confirming deletion, and then the viewer input is reset, and the viewer is updated, and selections are all cleared as expected.

Ideally, I want to clear the selection and update the viewer before showing the message dialog confirming deletion, so that the user doesnt still see his selection in the background. But I try the following, and I get the same weird behavior where the row is deleted but stays highlighted.


if (nodeDeleted) {
viewer.setSelection(StructuredSelection.EMPTY);
viewer.setInput(nodeModelProvider.getNodesFromDB());
showMessage(Messages.get().MessageDialog_ConfirmDeleteNode_title,
NLS.bind(Messages.get().MessageDialog_ConfirmDeleteNode_message,
selectedNode.getNodeAddress()));

}


So I'm confused why it would work with the message dialog in between the setSelection and the setInput, and not when they are together. I also tried without the message dialog and the same error occurs. Seem buggy to me, unless I'm doing something wrong.
Re: Row remains highlighted after delete item in TableViewer [message #822440 is a reply to message #822428] Fri, 16 March 2012 16:20 Go to previous messageGo to next message
Ivan Furnadjiev is currently offline Ivan FurnadjievFriend
Messages: 2426
Registered: July 2009
Location: Sofia, Bulgaria
Senior Member
Hi George,
as I mentioned before, there was a bug in the client-side Table (Tree)
implementation. See:
366272: [Tree] Full selection artifacts remain shown upon collapse of
tree node
https://bugs.eclipse.org/bugs/show_bug.cgi?id=366272
I think that this is the same issue. It has been fixed in 1.5M5. Which
RAP version are you using? Could you try your application with RAP 1.5M5
or latest nightly?
Best,
Ivan

On 3/16/2012 6:03 PM, George R Malary wrote:
> Ok, I have this working, but am not clear as to why it works, and what
> the problem is. showMessage is just a mesage dialog saying the node
> had been deleted. Below is the code snippet that works:
>
>
> if (nodeDeleted) {
> viewer.setSelection(StructuredSelection.EMPTY);
> showMessage(Messages.get().MessageDialog_ConfirmDeleteNode_title,
> NLS.bind(Messages.get().MessageDialog_ConfirmDeleteNode_message,
> selectedNode.getNodeAddress()));
> viewer.setInput(nodeModelProvider.getNodesFromDB());
> }
>
>
> So when user click delete on the confirm message dialog, and delete is
> successful in the backend, the selection is cleared, but the item is
> still visible in the viewer in the background because I have not reset
> the input. The message dialog appears confirming deletion, and then
> the viewer input is reset, and the viewer is updated, and selections
> are all cleared as expected.
>
> Ideally, I want to clear the selection and update the viewer before
> showing the message dialog confirming deletion, so that the user
> doesnt still see his selection in the background. But I try the
> following, and I get the same weird behavior where the row is deleted
> but stays highlighted.
>
>
> if (nodeDeleted) {
> viewer.setSelection(StructuredSelection.EMPTY);
> viewer.setInput(nodeModelProvider.getNodesFromDB());
> showMessage(Messages.get().MessageDialog_ConfirmDeleteNode_title,
> NLS.bind(Messages.get().MessageDialog_ConfirmDeleteNode_message,
> selectedNode.getNodeAddress()));
> }
>
>
> So I'm confused why it would work with the message dialog in between
> the setSelection and the setInput, and not when they are together. I
> also tried without the message dialog and the same error occurs. Seem
> buggy to me, unless I'm doing something wrong.
>

--
Ivan Furnadjiev

Twitter: @EclipseRAP
Blog: http://eclipsesource.com/blogs/

Professional services for RAP and RCP?
http://eclipsesource.com/services/rap/
Re: Row remains highlighted after delete item in TableViewer [message #822458 is a reply to message #822440] Fri, 16 March 2012 16:51 Go to previous messageGo to next message
George R Malary is currently offline George R MalaryFriend
Messages: 36
Registered: March 2012
Location: New Jersey
Member
Thanks Ivan, sounds like a similar problem. We are using M3, and will upgrade to M5 soon I believe. I will give it a go when I get the chance. Thanks alot for your help.
Re: Row remains highlighted after delete item in TableViewer [message #822461 is a reply to message #822440] Fri, 16 March 2012 16:51 Go to previous message
George R Malary is currently offline George R MalaryFriend
Messages: 36
Registered: March 2012
Location: New Jersey
Member
Thanks Ivan, sounds like a similar problem. We are using M3, and will upgrade to M5 soon I believe. I will give it a go when I get the chance. Thanks alot for your help.
Previous Topic:Integration of JavaFx 2.0 in RAP???
Next Topic:Row remains highlighted after delete item in TableViewer
Goto Forum:
  


Current Time: Thu Apr 25 22:41:07 GMT 2024

Powered by FUDForum. Page generated in 0.04607 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top