Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » JFace » Can (Should) TableViewer be extended?(Why does TableViewer's javadoc say it should not be subclassed?)
icon5.gif  Can (Should) TableViewer be extended? [message #670395] Fri, 13 May 2011 16:37 Go to next message
Aaron L is currently offline Aaron L
Messages: 18
Registered: November 2009
Junior Member
The javadoc for TableViewer says "This class is not intended to be subclassed outside the viewer framework".

What if all of the TableViewers in my RCP application perform the same initialization of the underlying Table object based on a custom TableSettings object.

For example, what if all of my application tables allow their columns(column order, column widths) to be modified by the user and then persisted between sessions.

If the javadoc is correct, why would the following be bad? To do otherwise would mean a lot of duplicate code.

public class CustomTableViewer extends TableViewer implements ControlListener {

   private TableSettings settings;

   public CustomTableViewer(Composite parent, int style, TableSettings settings) {
      super(parent, style);

      this.settings = settings;

      initializeTable();
   }

   private void initializeTable() {
      Table table = getTable();
      table.setLinesVisible(settings.isLinesVisible());
      table.setHeaderVisible(settings.isHeaderVisible());

      for (ColumnSetting colSetting : settings.getColumnSettings()) {
         int colIndex = colSetting.getIndex();
         
         TableColumn column = new TableColumn(table, SWT.LEAD, colIndex);
         column.setText(colSetting.getName());
         column.setToolTipText(colSetting.getName());
         column.setWidth(colSetting.getWidth());
         column.setMovable(true);
         column.setResizable(true);
         column.addControlListener(this);
      }
   }

   public void controlMoved(ControlEvent e) {
      /* Update and save the TableSettings to reflect the new column order. */
   }

   public void controlResized(ControlEvent e) {
      /* Update and save the TableSettings to refelct the new colum width. */
   }
}

(no subject) [message #671115 is a reply to message #670395] Mon, 16 May 2011 15:19 Go to previous messageGo to next message
Thomas Schindl is currently offline Thomas Schindl
Messages: 5013
Registered: July 2009
Senior Member
Am 13.05.11 22:37, schrieb forums-noreply@eclipse.org:
> The javadoc for TableViewer says "This class is not intended
> to be subclassed outside the viewer framework".
>
> What if all of the TableViewers in my RCP application
> perform the same initialization of the underlying Table
> object based on a custom TableSettings object.
>
> For example, what if all of my application tables allow
> their columns(column order, column widths) to be modified
> by the user and then persisted between sessions.
>
> If the javadoc is correct, why would the following be bad? To do
> otherwise would mean a lot of duplicate code.
>

.... because if we (JFace) introduce also such a method things will be
broken :-)

Why not all this useing a factory-pattern?

Tom
Re: (no subject) [message #671318 is a reply to message #671115] Tue, 17 May 2011 09:41 Go to previous messageGo to next message
Aaron L is currently offline Aaron L
Messages: 18
Registered: November 2009
Junior Member
I could create a factory object that would return TableViewers whose Table controls were appropriately initialized based on a setttings object. However, I would then have to extend Table to provide my custom functionality.

Then, in cases where I only have a reference to the TableViewer and I want to access/use its Table control, I have to call getTable() and appropriately cast the returned Table in order to use any of the functionality provided by the Table subclass (i.e. I would have to perform "instanceof" checks every time I wanted to use the viewers Table control).

While I could perform the instanceof/casting each time I use the Table control, it would be nice to be able to extend TableViewer and override the getTable() method to return the appropriate type of Table.

I could work around not extending TableViewer, I'm just wondering if it would be OK in cases like this as long as I understand that integrating future versions of JFace may involve some extra work.
Re: (no subject) [message #671541 is a reply to message #671318] Wed, 18 May 2011 04:37 Go to previous messageGo to next message
Eclipse User
Originally posted by:

On 2011-05-17 15:41, No real name wrote:
> I could create a factory object that would return
> TableViewers whose Table controls were appropriately
> initialized based on a setttings object. However, I would
> then have to extend Table to provide my custom
> functionality.
>
> Then, in cases where I only have a reference to the
> TableViewer and I want to access/use its Table control, I
> have to call getTable() and appropriately cast the returned
> Table in order to use any of the functionality provided by
> the Table subclass (i.e. I would have to perform
> "instanceof" checks every time I wanted to use the viewers
> Table control).
>
> While I could perform the instanceof/casting each time I use
> the Table control, it would be nice to be able to extend
> TableViewer and override the getTable() method to return
> the appropriate type of Table.

I don't see the difference in effort here, because either way you would
need to derive from Table because there are no existing refinement of
Table that you could return instead. But superclassing Table is no valid
option for clients, see the corresponding SWT documentation:

"IMPORTANT: This class is not intended to be subclassed."

HTH & Greetings from Bremen,

Daniel Kr├╝gler
Re: (no subject) [message #671610 is a reply to message #671541] Wed, 18 May 2011 09:18 Go to previous message
Aaron L is currently offline Aaron L
Messages: 18
Registered: November 2009
Junior Member
Ok. I see that now. I missed the fact that Table should not be subclassed as well.

Thanks for your help!
Previous Topic:Table Viewer - Entering any character on the key board would produce a beep sound
Next Topic:JFace action accelerators interfering with typing
Goto Forum:
  


Current Time: Thu Apr 17 20:52:04 EDT 2014

Powered by FUDForum. Page generated in 0.02082 seconds