Menu

Proxy Pattern, integrate JXLayer + JHotDraw

Help
yccheok
2010-04-25
2013-05-01
  • yccheok

    yccheok - 2010-04-25

    Apart from previous post, I try another alternative. This time, I came quite close, but still has some shortcoming.

    Step 1 :

    editor = new DefaultDrawingEditor();
    // editor.add(this.defaultDrawingView);
    editor.add(this.chartLayerUI);
    // All toolbar creation based on editor.
    // ...
    // ...
    this.chartLayerUI.setDrawing(createDrawing());
    final org.jdesktop.jxlayer.JXLayer<ChartPanel> layer = new org.jdesktop.jxlayer.JXLayer<ChartPanel>(this.chartPanel);
    this.chartLayerUI = new ChartLayerUI<ChartPanel>(this);
    layer.setUI(this.chartLayerUI);
    // JxLayer being added in to application, not JHotDraw's defaultDrawingView.
    // Whenever JXLayer receive mouse event, it will forward the event to defaultDrawingView.
    // In the painting event of JXLayer, it will trigger JHotDraw's defaultDrawingView paintComponent as well.
    getContentPane().add(layer, java.awt.BorderLayout.CENTER);
    

    Then, I apply Proxy Pattern on ChartLayerUI :

    public class ChartLayerUI<V extends javax.swing.JComponent> extends AbstractLayerUI<V> {
    

    to

    public class ChartLayerUI<V extends javax.swing.JComponent> extends AbstractLayerUI<V> implements DrawingView, EditableComponent {
    

    Add all forwarding methods :

        private final org.jhotdraw.draw.DefaultDrawingView view = new org.jhotdraw.draw.DefaultDrawingView();
        @Override
        public Drawing getDrawing() {
            return this.view.getDrawing();
        }
        @Override
        public void setDrawing(Drawing arg0) {
            this.view.setDrawing(arg0);
        }
    .....
    

    Add event forwarding code.

        private void retargetMouseEvent(MouseEvent e) {        
            MouseEvent me = SwingUtilities.convertMouseEvent(this.view, e, this.view);
            System.out.println("retarget mouse event " + me);
            this.view.dispatchEvent(me);
        }
        @Override
        protected void processMouseEvent(MouseEvent e, JXLayer<? extends V> layer) {
            super.processMouseEvent(e, layer);
            retargetMouseEvent(e);
            this.processEvent(e, layer);
        }
        @Override
        protected void processMouseMotionEvent(MouseEvent e, JXLayer<? extends V> layer) {
            super.processMouseMotionEvent(e, layer);
            retargetMouseEvent(e);
            this.processEvent(e, layer);
        }
    
    
    
    
    
    Whenever receive a graphics object during pain event, forward the graphics object to defaultDrawingView as well.
    [code]
        protected void paintLayer(Graphics2D g2, JXLayer<? extends V> layer) {
            super.paintLayer(g2, layer);
            this.view.paintComponent(g2);
            ....
    [/code]
    This work pretty fine as :
    (1) When I click on the toolbar and click on JXLayer area, the figures can be seen immediately
    (2) When I click on drag the figures, the figure can move around
    (3) When I click and drag on Text figure right side, the text area can resize
    (4) When I click and drag on Text figure left side, the font size can resize
    What the shortcoming is :
    (1) When I move my mouse across the figure, the cursor is not turning into hand cursor. I do some println, the mouse move event is already forwarded to defaultDrawingView, but how come the cursor is not turning into hand cursor?
    (2) I click on a figure, there are no sign indicated the figures is being selected (There should be 4 small rectangles around the figure to indicate it is being selected). Again, the mouse click event is already forwarded to defaultDrawingView.
    Is there any other hacks I had missed out?
    
     
  • yccheok

    yccheok - 2010-04-25

    Here is how it look like :

    One more shortcoming is that when I double click on Text figure, (JXlayer forward double click event to default drawing view, and default drawing view forward event to text figure?) the text figure doesn't seem to be in editable mode. I type from keyboard, the text wont change.

     
  • Werner Randelshofer

    >(1) When I move my mouse across the figure, the cursor is not turning into hand cursor. I do some println, the mouse move event >is already forwarded to defaultDrawingView, but how come the cursor is not turning into hand cursor?

    The cursors is determined by the component under the mouse location.
    I guess, this is JXLayer, or some other component than DefaultDrawingView.

    >(2) I click on a figure, there are no sign indicated the figures is being selected (There should be 4 small rectangles around the >figure to indicate it is being selected). Again, the mouse click event is already forwarded to defaultDrawingView.

    I guess this happens, because the focus is not transferred to DefaultDrawingView.

    > One more shortcoming is that when I double click on Text figure, (JXlayer forward double click event to default drawing view, and
    > default drawing view forward event to text figure?) the text figure doesn't seem to be in editable mode. I type from keyboard, the
    > text wont change.

    This happens because the focus is not transferred to the JTextComponent which is used to edit the text figure.

    >Is there any other hacks I had missed out?

    Almost certainly: yes.
    Overall, this is a very fragile approach.
    I doubt that you will succeed with this approach.

    Best,
    Werner

     

Log in to post a comment.

MongoDB Logo MongoDB