tags:

views:

134

answers:

0

I have created a class by subclassing SWT Canvas. The class (SceneImage) contains two standard SWT Widgets:

Image Label

The class code follows:


class SceneImage extends Canvas {

     private int    sceneNumber;

     private IFrame sceneFrame;
     private Image  sceneImage;
     private Label  sceneLabel;

     /**
      * @param parent
      * @param style
      */
     public SceneImage(Composite parent, int style, IFrame sceneFrame, int sceneNumber) {
      super(parent, style);
      this.sceneFrame = sceneFrame;
      this.sceneImage = new Image(parent.getDisplay(), 
       sceneFrame.getFrameImage());;
      this.sceneLabel = new Label(parent, style);
      this.sceneLabel.setText("Scene: " + sceneNumber);
      Rectangle imageBounds = this.sceneImage.getBounds();
      Rectangle labelBounds = this.sceneLabel.getBounds();

      this.sceneLabel.setBounds(imageBounds.x + 5, imageBounds.y + 5, labelBounds.width, labelBounds.height);

      this.sceneNumber = sceneNumber;

      // It is a SWT convention to use anonymous inner classes to forward
      // functionality to non-public methods of the same name.

      addDisposeListener(new DisposeListener() {
       public void widgetDisposed(DisposeEvent e) {
        SceneImage.this.widgetDisposed(e);
       }
      });

      addPaintListener(new PaintListener() {
       public void paintControl(PaintEvent e) {
        SceneImage.this.paintControl(e);
       }
      });

      addMouseListener(new MouseAdapter() {
       public void mouseUp(MouseEvent e) {
        SceneImage.this.mouseUp(e);
       }
      });

     } // End public SceneImage(Composite parent, int style, ...........

     /**
      * @return the preferred size of the SceneImage
      */
     public Point computeSize(int wHint, int hHint, boolean changed) {
      int width = 0, height = 0;

      if (sceneImage != null) {
       Rectangle bounds = sceneImage.getBounds();
       width = bounds.width;
       height = bounds.height;
       log_debug("sceneImage: " + getSceneNumber() + " width = " + width);
       log_debug("sceneImage: " + getSceneNumber() + " height = " + height);
      }

      if (sceneLabel != null) {
       Rectangle bounds = sceneLabel.getBounds();
       //width += bounds.width;
       //height += bounds.height;
       log_debug("sceneLabel: " + getSceneNumber() + " width = " + bounds.width);
       log_debug("sceneLabel: " + getSceneNumber() + " height = " + bounds.height);
      }

      if (wHint != SWT.DEFAULT) {
       width = wHint;
       log_debug("wHint != SWT.DEFAULT.  Width has been changed to wHint:  " + wHint);
      }

      if (hHint != SWT.DEFAULT) {
       height = hHint;
       log_debug("hHint != SWT.DEFAULT.  Height has been changed to hHint:  " + hHint);
      }

      return new Point(width + 2, height + 2);
     }

     /**
      * @return the sceneFrame
      */
     public IFrame getSceneFrame() {
      return sceneFrame;
     }

     /**
      * @return the sceneImage
      */
     public Image getSceneImage() {
      return sceneImage;
     }

     /**
      * @return the sceneLabel
      */
     public Label getSceneLabel() {
      return sceneLabel;
     }

     /**
      * @return the sceneNumber
      */
     public int getSceneNumber() {
      return sceneNumber;
     }

     public void highLightSceneLabelForeground() {
      org.eclipse.swt.graphics.Color color = getMyDisplay().getSystemColor (SWT.COLOR_RED);

      getSceneLabel().setForeground(color);
      redraw();
     }

     public void removeSceneLabelForegroundHighlighting() {

      getSceneLabel().setForeground(null);
      redraw();
     }

     /**
      * @param sceneFrame the sceneFrame to set
      */
     public void setSceneFrame(IFrame sceneFrame) {
      this.sceneFrame = sceneFrame;
     }

     /**
      * @param sceneImage the sceneImage to set
      */
     public void setSceneImage(Image sceneImage) {
      this.sceneImage = sceneImage;
     }

     /**
      * @param sceneLabel the sceneLabel to set
      */
     public void setSceneLabel(Label sceneLabel) {
      this.sceneLabel = sceneLabel;
     }

     /**
      * @param sceneNumber the sceneNumber to set
      */
     public void setSceneNumber(int sceneNumber) {
      this.sceneNumber = sceneNumber;
     }

        private void mouseUp(MouseEvent e) {
            getSceneFrame().getVideo().setCurrentFrame(getSceneFrame()
             .getFrameNumber());
        }

     private void paintControl(PaintEvent e) {
      if (getSceneImage() != null) {
          getSceneImage().dispose();
          setSceneImage(hugImageAndCanvasSizes(this, getSceneFrame()));
          e.gc.drawImage(getSceneImage(), 0, 0);
         }
     }

     private void widgetDisposed(DisposeEvent e) {
      if (null != getSceneImage()) {
                getSceneImage().dispose();
            }
      if (null != getSceneLabel()) {
                getSceneLabel().dispose();
            }
     }
    }

I want the Label displayed (centered) under it's associated image.

I generate n number of these SceneImage objects. I display them in a ScrolledComposite using GridLayout. What gets displayed is image 1 then to it's right label 1, etc.

How can I get the objects displayed the way I want in SWT?

Is there a way I can get GridLayout to see my SceneObject as one Widget instead of two?

Or is it a matter of instructing GridLayout to display the two Widgets the way I want? If yes, then how?

Should I be using a different Layout Manager to accomplish this?

Thanks for your assistance.