• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 733
  • Last Modified:

SWT: wrap Canvas widget

I’m going to write some code using the Eclipse SWT.  I want a window that will show a grid of colored squares.  I was thinking I would extend the Canvas widget and draw on that.

In the Programmer’s Guide it suggests wrapping a widget.  “To wrap a widget, you create a subclass of the…Canvas widget”.  Then later it says “Extending a widget by subclassing it is highly discouraged, since it will make your widget dependent on the implementation of the superclass.”  A bit contradictory.  So apparently, I want to wrap the widget without subclassing it.  How?  The design patterns Adaptor and Decorator don’t fit with what I want to do.

0
jdaues
Asked:
jdaues
1 Solution
 
heyhey_Commented:
if I was you, I would just go and extend Canvas.
0
 
objectsCommented:
Why not use a grid of components?
0
 
jdauesAuthor Commented:
Grid of components:
Having played around with it a little more, I will want not rectangles, but with pixels.  That is, I want to manipulate a grid of pixels, say 500x500, controlling the color of each pixel.  Making each pixel a compoment would be a lot of overhead, I think.  So a grid of components doesn't seem viable with this new setup.
0
 
objectsCommented:
Then try using an approach similiar to what JTable in Swing does.
Have a component that manages the grid, and use a renderer for painting individual cells.
0
 
Jim CakalicSenior Developer/ArchitectCommented:
From an article entitled "How to Use the Eclipse API"
    http://www.eclipse.org/articles/Article-API%20use/eclipse-api-usage-rules.html
"Only a subset of the API classes were designed to be subclassed. API classes have a subclass contract indicating the terms under which subclasses may be declared. This contract also covers initialization responsibilities and lifecycle responsibilities. Classes that are designed to be subclassed by clients are explicitly flagged in the Javadoc class comment (with words like "Clients may subclass.")."

From "Creating Your Own Widgets Using SWT"
    http://www.eclipse.org/articles/Article-Writing%20Your%20Own%20Widget/Writing%20Your%20Own%20Widget.htm
"Custom widgets are created by subclassing in the existing widget class hierarchy."
"The SWT library provides two widget classes that are typically used as the basis for a custom 100% Java portable widget: Canvas - to create basic widgets, Composite - to create compound widgets."
"To create a custom basic widget, subclass Canvas."
"Imagine we are building an application where we need a widget that displays an image with a line of text to the right ... Since we plan to draw both the image and the text, we subclass Canvas."

And from the javadoc for org.eclipse.swt.widgets.Canvas
    http://www.eclipse.org/documentation/html/plugins/org.eclipse.platform.doc.isv/doc/reference/api/org/eclipse/swt/widgets/Canvas.html

"This class may be subclassed by custom control implementors who are building controls that are not constructed from aggregates of other controls. That is, they are either painted using SWT graphics calls or are handled by native methods."

Evidently then, Canvas is designed to be subclassed given the specified constraints. If your implementation abides by those constraints then I see no reason why you would not directly subclass Canvas as you originally intended.

Best regards,
Jim Cakalic
0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now