Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » JFace » Spreadsheet-like table viewer
Spreadsheet-like table viewer [message #524119] Tue, 30 March 2010 20:02 Go to next message
Eric Buist is currently offline Eric BuistFriend
Messages: 5
Registered: March 2010
Junior Member
Hi,
I am trying to create a TableViewer object acting a bit like an Excel spreadsheet. With the basic table viewer, I got a table with rows and columns, and I can navigate rows by using the keyboard.
However, cell editing support is a bit limited. By default, one needs to click on the cell to trigger the editor. The idea is that the table will contain many rows and the user would like to quickly enter a value, press enter to validate it, then the down arrow key to go to the next row.

Here is how I could get the keyboard navigation:
                 TableViewerFocusCellManager focusCellManager = new TableViewerFocusCellManager(v, new FocusCellOwnerDrawHighlighter(v));
		ColumnViewerEditorActivationStrategy actSupport = new ColumnViewerEditorActivationStrategy(v) {
			protected boolean isEditorActivationEvent(
					ColumnViewerEditorActivationEvent event) {
				return event.eventType == ColumnViewerEditorActivationEvent.TRAVERSAL
						|| event.eventType == ColumnViewerEditorActivationEvent.MOUSE_DOUBLE_CLICK_SELECTION
						|| (event.eventType == ColumnViewerEditorActivationEvent.KEY_PRESSED && event.keyCode == SWT.CR)
						|| event.eventType == ColumnViewerEditorActivationEvent.PROGRAMMATIC;
			}
		};
		
		TableViewerEditor.create(v, focusCellManager, actSupport, 
				ColumnViewerEditor.KEYBOARD_ACTIVATION);

Here, v is an instance of TableViewer.

The program I am working on allows to perform operations on multiple rows of the table. With the above code, I get keyboard navigation working, but multiple selection does not work anymore. In fact, it works internally, but the table viewer just does not render it; it only highlights the cell currently in focus.

Thinking that the problem could come from the focus manager, I tried to put it to null. It worked, but I lost cell editing using the keyboard.
I also tried with a code snippet Snippet035TableCursorCellHighlighter providing a TableCursor cell highlighter. This highlighted full rows, with the current cell identified by a rectangle, but yet no multiple selection.

Another possible partial solution would be to use the fact that only one column will be frequently edited in my table. If it was possible to trigger the cell editing programmatically, I could make a key listener starting the editor when the user presses the Enter key. However, as I looked through documentation and source code, doing so would require to obtain a ViewerCell, which I cannot do without knowing the position of the cell in pixels or getting a ViewerRow. But I found no public API to get a ViewerRow.

I tried very hard to work around this, without any success. I even looked at the source code of the table viewer.

This may be possible to achieve using something other that TableViewer, but this would force me to rewrite all the code dealing with the table viewer. Some possible candidates for a control would be the Nebula grid (alpha state, so may change in the future) or a plain Swing JTable, if it is possible to use one inside a SWT Composite. The second solution seems heavyweight to me and may not provide the necessary flexibility.

I am using Eclipse 3.5.
Re: Spreadsheet-like table viewer [message #524128 is a reply to message #524119] Tue, 30 March 2010 20:24 Go to previous messageGo to next message
Charlie Kelly is currently offline Charlie KellyFriend
Messages: 276
Registered: July 2009
Senior Member
Hi Eric,

See the Eclipse Nebula widgets.
They might be what you are looking for.

Charlie

Eric Buist wrote:
> Hi,
> I am trying to create a TableViewer object acting a bit like an Excel
> spreadsheet. With the basic table viewer, I got a table with rows and
> columns, and I can navigate rows by using the keyboard.
> However, cell editing support is a bit limited. By default, one needs to
> click on the cell to trigger the editor. The idea is that the table will
> contain many rows and the user would like to quickly enter a value,
> press enter to validate it, then the down arrow key to go to the next row.
>
> Here is how I could get the keyboard navigation:
>
> TableViewerFocusCellManager focusCellManager = new
> TableViewerFocusCellManager(v, new FocusCellOwnerDrawHighlighter(v));
> ColumnViewerEditorActivationStrategy actSupport = new
> ColumnViewerEditorActivationStrategy(v) {
> protected boolean isEditorActivationEvent(
> ColumnViewerEditorActivationEvent event) {
> return event.eventType ==
> ColumnViewerEditorActivationEvent.TRAVERSAL
> || event.eventType ==
> ColumnViewerEditorActivationEvent.MOUSE_DOUBLE_CLICK_SELECTI ON
> || (event.eventType ==
> ColumnViewerEditorActivationEvent.KEY_PRESSED && event.keyCode == SWT.CR)
> || event.eventType ==
> ColumnViewerEditorActivationEvent.PROGRAMMATIC;
> }
> };
>
> TableViewerEditor.create(v, focusCellManager, actSupport,
> ColumnViewerEditor.KEYBOARD_ACTIVATION);
>
> Here, v is an instance of TableViewer.
>
> The program I am working on allows to perform operations on multiple
> rows of the table. With the above code, I get keyboard navigation
> working, but multiple selection does not work anymore. In fact, it works
> internally, but the table viewer just does not render it; it only
> highlights the cell currently in focus.
>
> Thinking that the problem could come from the focus manager, I tried to
> put it to null. It worked, but I lost cell editing using the keyboard.
> I also tried with a code snippet
> http://dev.eclipse.org/viewcvs/index.cgi/org.eclipse.jface.s nippets/Eclipse%20JFace%20Snippets/org/eclipse/jface/snippet s/viewers/Snippet035TableCursorCellHighlighter.java?view=mar kup
> providing a TableCursor cell highlighter. This highlighted full rows,
> with the current cell identified by a rectangle, but yet no multiple
> selection.
>
> Another possible partial solution would be to use the fact that only one
> column will be frequently edited in my table. If it was possible to
> trigger the cell editing programmatically, I could make a key listener
> starting the editor when the user presses the Enter key. However, as I
> looked through documentation and source code, doing so would require to
> obtain a ViewerCell, which I cannot do without knowing the position of
> the cell in pixels or getting a ViewerRow. But I found no public API to
> get a ViewerRow.
>
> I tried very hard to work around this, without any success. I even
> looked at the source code of the table viewer.
>
> This may be possible to achieve using something other that TableViewer,
> but this would force me to rewrite all the code dealing with the table
> viewer. Some possible candidates for a control would be the Nebula grid
> (alpha state, so may change in the future) or a plain Swing JTable, if
> it is possible to use one inside a SWT Composite. The second solution
> seems heavyweight to me and may not provide the necessary flexibility.
>
> I am using Eclipse 3.5.
Re: Spreadsheet-like table viewer [message #526012 is a reply to message #524128] Thu, 08 April 2010 14:30 Go to previous messageGo to next message
Eric Buist is currently offline Eric BuistFriend
Messages: 5
Registered: March 2010
Junior Member
Hi,
I tried to look at Nebula Grid, but I don't like the idea that it is still in alpha state: it may have bugs I will be stuck with after I migrate or the API might change without notice in the future, forcing a lot of code rewrite. Moreover, I found no API documentation, only basic examples. According to these examples, this will work as with my current TableViewer, but I'm not sure about advanced stuff such as cell editing. Moreover, the Nebula Grid seems lower level than the TableViewer: you need to manually create GridItem objects for each row. I read about a GridViewer that would act as TableViewer for the Grid, tried to find documentation or at least code examples about it: no go. I cannot even find a download for the GridViewer; maybe the project has gone offline.

I found the NatTable widget which could be a great tool. Has anyone used this?
Re: Spreadsheet-like table viewer [message #526051 is a reply to message #524119] Thu, 08 April 2010 15:19 Go to previous messageGo to next message
Thomas Schindl is currently offline Thomas SchindlFriend
Messages: 6651
Registered: July 2009
Senior Member
[...]
> Another possible partial solution would be to use the fact that only one
> column will be frequently edited in my table. If it was possible to
> trigger the cell editing programmatically, I could make a key listener
> starting the editor when the user presses the Enter key. However, as I
> looked through documentation and source code, doing so would require to
> obtain a ViewerCell, which I cannot do without knowing the position of
> the cell in pixels or getting a ViewerRow. But I found no public API to
> get a ViewerRow.

key listeners don't deliver x/y coordinates so this won't help you.
That's why the SWTFocusCellManager tracks it.

Tom
Re: Spreadsheet-like table viewer [message #526052 is a reply to message #526012] Thu, 08 April 2010 15:20 Go to previous messageGo to next message
Thomas Schindl is currently offline Thomas SchindlFriend
Messages: 6651
Registered: July 2009
Senior Member
Am 08.04.10 16:30, schrieb Eric Buist:
> Hi,
> I tried to look at Nebula Grid, but I don't like the idea that it is
> still in alpha state: it may have bugs I will be stuck with after I

We haven't been breaking any API since a long time IIRC (well we fixed a
typo in 2 of our API-methods because they differed from the
SWT-Table/Tree ones) and the widget is quite stable and many companies
use it. The main problem we didn't released a 1.0 release is that we
didn't had time yet to restructure Nebula.

You are right that you might be stuck with a bug but getting a fix in to
Grid is easier than for example into Table/Tree because of the nature of
Grid (it's emulated).

If you are not comfortable with opensource support my company can offer
you professional support for Grid, ... (probably other can/would as
well) but you can naturally fix stuff your own and believe me this is
not as hard as you might think it is :-)

Still the message is Nebula-Grid has a 99.9% stable API and you can
programm safely against it's API.

> migrate or the API might change without notice in the future, forcing a
> lot of code rewrite. Moreover, I found no API documentation, only basic
> examples. According to these examples, this will work as with my current
> TableViewer, but I'm not sure about advanced stuff such as cell editing.
> Moreover, the Nebula Grid seems lower level than the TableViewer: you

Yes Grid == Table/Tree, GridViewer/GridTreeViewer ==
TableViewer/TreeViewer if you take a close look you'll notice that you
simply have to swap TableViewer through GridViewer and your code doesn't
change beside that because those features are coming from JFace.

Sticking with GridViewer your migration path is quite easy which one of
the great with things Nebula-Grid because it follows SWT/JFace-API and
Codeing style nearly 100%, you get Eclipse-Databinding-Support by
default, ...

> need to manually create GridItem objects for each row. I read about a
> GridViewer that would act as TableViewer for the Grid, tried to find
> documentation or at least code examples about it: no go. I cannot even
> find a download for the GridViewer; maybe the project has gone offline.

http://www.eclipse.org/nebula/downloads.php => it comes with the Grid
download.

>
> I found the NatTable widget which could be a great tool. Has anyone used
> this?

We once tried to get them into Nebula but well it never happened.

Tom
Re: Spreadsheet-like table viewer [message #526121 is a reply to message #526052] Thu, 08 April 2010 18:38 Go to previous messageGo to next message
Eric Buist is currently offline Eric BuistFriend
Messages: 5
Registered: March 2010
Junior Member
Hi,

Thanks for this quick answer. I looked more closely at NatTable, and I got the same problem as with Nebula Grid: no API documentation at all and even no easily accessible source code for examples. From what I could see, the API of NatTable is very different from TableViewer so migration would be tedious.
Nebula Grid might thus be the solution, especially if it is currently used by companies.
Re: Spreadsheet-like table viewer [message #538163 is a reply to message #524119] Sat, 05 June 2010 21:19 Go to previous messageGo to next message
Mikayel Aghasyan is currently offline Mikayel AghasyanFriend
Messages: 2
Registered: April 2010
Junior Member
Hi,

I had the same problem and I've solved it by copying FocusCellOwnerDrawHighlighter and removing listener hooking stuff. In a result I have the following class:

public class MyFocusCellHighlighter extends FocusCellHighlighter {
	/**
	 * Create a new instance which can be passed to a
	 * {@link TreeViewerFocusCellManager}
	 * 
	 * @param viewer
	 *            the viewer
	 */
	public MyFocusCellHighlighter(ColumnViewer viewer) {
		super(viewer);
	}

	/**
	 * The color to use when rendering the background of the selected cell when
	 * the control has the input focus
	 * 
	 * @param cell
	 *            the cell which is colored
	 * @return the color or <code>null</code> to use the default
	 */
	protected Color getSelectedCellBackgroundColor(ViewerCell cell) {
		return null;
	}

	/**
	 * The color to use when rendering the foreground (=text) of the selected
	 * cell when the control has the input focus
	 * 
	 * @param cell
	 *            the cell which is colored
	 * @return the color or <code>null</code> to use the default
	 */
	protected Color getSelectedCellForegroundColor(ViewerCell cell) {
		return null;
	}

	/**
	 * The color to use when rendering the foreground (=text) of the selected
	 * cell when the control has <b>no</b> input focus
	 * 
	 * @param cell
	 *            the cell which is colored
	 * @return the color or <code>null</code> to use the same used when
	 *         control has focus
	 * @since 3.4
	 */
	protected Color getSelectedCellForegroundColorNoFocus(ViewerCell cell) {
		return null;
	}

	/**
	 * The color to use when rendering the background of the selected cell when
	 * the control has <b>no</b> input focus
	 * 
	 * @param cell
	 *            the cell which is colored
	 * @return the color or <code>null</code> to use the same used when
	 *         control has focus
	 * @since 3.4
	 */
	protected Color getSelectedCellBackgroundColorNoFocus(ViewerCell cell) {
		return null;
	}

	/**
	 * Controls whether the whole cell or only the text-area is highlighted
	 * 
	 * @param cell
	 *            the cell which is highlighted
	 * @return <code>true</code> if only the text area should be highlighted
	 * @since 3.4
	 */
	protected boolean onlyTextHighlighting(ViewerCell cell) {
		return false;
	}

	protected void focusCellChanged(ViewerCell newCell, ViewerCell oldCell) {
		super.focusCellChanged(newCell, oldCell);

		// Redraw new area
		if (newCell != null) {
			Rectangle rect = newCell.getBounds();
			int x = newCell.getColumnIndex() == 0 ? 0 : rect.x;
			int width = newCell.getColumnIndex() == 0 ? rect.x + rect.width
					: rect.width;
			// 1 is a fix for Linux-GTK
			newCell.getControl().redraw(x, rect.y - 1, width, rect.height + 1,
					true);
		}

		if (oldCell != null) {
			Rectangle rect = oldCell.getBounds();
			int x = oldCell.getColumnIndex() == 0 ? 0 : rect.x;
			int width = oldCell.getColumnIndex() == 0 ? rect.x + rect.width
					: rect.width;
			// 1 is a fix for Linux-GTK
			oldCell.getControl().redraw(x, rect.y - 1, width, rect.height + 1,
					true);
		}
	}
}


Not sure if it meets your needs but you can try it.
Re: Spreadsheet-like table viewer [message #631971 is a reply to message #524119] Mon, 11 October 2010 06:02 Go to previous message
pharmacie1801  is currently offline pharmacie1801 Friend
Messages: 1
Registered: October 2010
Junior Member
You can do it by

		    return event.eventType == ColumnViewerEditorActivationEvent.TRAVERSAL
			    || event.eventType == ColumnViewerEditorActivationEvent.MOUSE_DOUBLE_CLICK_SELECTION
			    || (event.eventType == ColumnViewerEditorActivationEvent.KEY_PRESSED && event.keyCode == SWT.CR)
			    || (event.eventType == ColumnViewerEditorActivationEvent.KEY_PRESSED && event.keyCode == SWT.F2)
			    || (event.eventType == ColumnViewerEditorActivationEvent.KEY_PRESSED && event.keyCode == SWT.KEYPAD_CR)
			    || (event.eventType == ColumnViewerEditorActivationEvent.KEY_PRESSED
				    && event.keyCode > 31 && event.keyCode < 127)
			    || (event.eventType == ColumnViewerEditorActivationEvent.KEY_PRESSED
				    && event.keyCode >= SWT.KEYPAD_MULTIPLY && event.keyCode <= SWT.KEYPAD_9)

			    || event.eventType == ColumnViewerEditorActivationEvent.PROGRAMMATIC;



this will make editor activated using any alphanumeric key

now the problem that editor will lose fist key because it's used to activate editor
so just do this


	ColumnViewerEditorActivationListener listener = new ColumnViewerEditorActivationListener() {
	    @Override
	    public void afterEditorActivated(
		    ColumnViewerEditorActivationEvent event) {
		final ViewerCell cell = (ViewerCell) event.getSource();

		if (pedit[cell.getColumnIndex()].editor.getControl() instanceof Text) {

		    Text tt = (Text) pedit[cell.getColumnIndex()].editor
			    .getControl();
		    if ((event.eventType == ColumnViewerEditorActivationEvent.KEY_PRESSED
			    && event.keyCode > 31 && event.keyCode < 127)
			    || (event.eventType == ColumnViewerEditorActivationEvent.KEY_PRESSED
				    && event.keyCode >= SWT.KEYPAD_MULTIPLY && event.keyCode <= SWT.KEYPAD_9)

		    ) {
			tt.setText("" + event.character);
			tt.setSelection(tt.getText().length());
		    }

		}
		}
}
//--------------- complete it  then
v.getColumnViewerEditor().addEditorActivationListener(listener);



[Updated on: Mon, 11 October 2010 06:03]

Report message to a moderator

Previous Topic:add a tooltips for treeViewer
Next Topic:[Databinding] Why isn't there an ObservableValueEditingSupport.create() that takes update strategies
Goto Forum:
  


Current Time: Fri Mar 29 08:15:07 GMT 2024

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

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

Back to the top