logo       

Re: Delete Not working still: msg#00062

java.netbeans.modules.graph.user

Subject: Re: Delete Not working still

Hi David!

I have cleaned up a few things it looks alot nicer. However the core problem is that the KeyEvent never occurrs I never get into the the handler itself.

Which would imply something is consuming the mouse event. Could be a problem with ComponentWidgets?

Chris


David Kaspar wrote:
Hi Chris,

I have a few suggestion for your code:

In SelectionProviderImpl.select method use "setFocusedObject (object)"
instead.

Anyway the best would be, if you would use actions built-in for ObjectScene:
ObjectScene.createSelectAction ()
ObjectScene.createObjectHoverAction ()

The "ObjectScene.createSelectAction ()" automatically handles
selected-objects and focused-object also.

When a state of an object is changed, it also automatically changes
state of related Widget, that is representing object in the ObjectScene,
using "Widget.setState ()" method.

Therefore in AbstractCanvasModel.selectObject method there should not be
any code that changes the border of related Widget. Instead, you should
create your own class inherited from ComponentWidget. The new class
should have "notifyStateChanged" method overriden where the code for
changing its border should be placed. The new state is passed as an
argument to the method.

Regarding your key-event processing:
Unfortunately I cannot find a problem from the code itself.
You are using FOCUSED_WIDGET_AND_ITS_PARENTS event processing type.
Therefore using "Scene.getFocusedWidget ()" method check what widget is
focused. In your type, it has to be a widget where you have your
deleteAction assigned.

Let me know, what is the result.

Regards,
  David


Chris Palmer wrote:
  
Hello!

I figured out my null pointer, I think...I am not using Object Scene
correctly. I do see in my test case that the key event is still firing
with the layer approach.... I still can't figure out why its not
happening in my class....which is attached....

Chris

public class CanvasModel extends AbstractCanvasModel<DesignerHandle,
Object> {
 private WidgetAction designSelect =
ActionFactory.createSelectAction(new SelectionProviderImpl());
 private WidgetAction designDeselect =
ActionFactory.createSelectAction(new DeselectionImpl());
 private WidgetAction designAccept =
ActionFactory.createAcceptAction(new AcceptProviderImpl());
 private WidgetAction designMove;
 private WidgetAction designResize;
 private WidgetAction designDeleteAction = new KeyEventLoggerAction();
 private LayerWidget backgroundLayer;
 private LayerWidget componentLayer;
 private LayerWidget interactionLayer;

 /** Creates a new instance of CanvasModel */
 public CanvasModel() {
   init();
 }

 private void init() {
   // Furthest back
   backgroundLayer = new LayerWidget(this);
   // components sit there.
   componentLayer = new LayerWidget(this);
   // moves occur here
   interactionLayer = new LayerWidget(this);
  //    setMaximumBounds(new Rectangle(0, 0, Integer.MAX_VALUE,
Integer.MAX_VALUE));
//
   setKeyEventProcessingType
(EventProcessingType.FOCUSED_WIDGET_AND_ITS_PARENTS);
     addChild(backgroundLayer);
   addChild(componentLayer);
   addChild(interactionLayer);
 
   getActions().addAction(designAccept);
//    getActions().addAction(designDeselect);
//   
getActions().addAction(ActionFactory.createPopupMenuAction(createPopupMenuProvider()));

     designResize = ActionFactory.createAlignWithResizeAction
(componentLayer, interactionLayer, null);
   designMove = createMoveAction();
 
 }

 protected WidgetAction createMoveAction() {
   return ActionFactory.createAlignWithMoveAction (componentLayer,
interactionLayer, null);
 }

 protected void selectObject(Object object, boolean invertSelection) {
     if (getFocusedObject() != null) {
         findWidget(getFocusedObject()).setBorder(createDesignBorder());
     }
    
findWidget(object).setBorder(createDesignResizeBorder((DesignerHandle)object));


     setFocusedObject(object);
     if (object != null) {
         if (getSelectedObjects().contains(object))
             return;
         userSelectionSuggested(Collections.singleton(object),
invertSelection);
     } else
         userSelectionSuggested(Collections.emptySet(), invertSelection);

 }

 protected Widget attachNodeWidget(DesignerHandle handle) {
   final ComponentWidget des = new
ComponentWidget(this,handle.getRuntimeComponent().getViewDelegate().getComponent());

   componentLayer.addChild(des);
   des.setBorder(createDesignBorder());
   des.getActions().addAction(designDeleteAction);
   des.getActions().addAction(designSelect);
//    des.getActions().addAction(designResize);
//    des.getActions().addAction(designMove);
//    attachDesignActions(des, handle);
   return des;
 }

 protected Widget attachEdgeWidget(Object edge) {
     return null;
 }

 protected void attachEdgeSourceAnchor(Object edge, DesignerHandle
source, DesignerHandle target) {
 }

 protected void attachEdgeTargetAnchor(Object edge, DesignerHandle
source, DesignerHandle target) {
 }

 public static final int BORDER_SIZE = 5;

 public Border createDesignResizeBorder(DesignerHandle dh) {
   return BorderFactory.createResizeBorder(BORDER_SIZE);
 }

 public Border createDesignBorder() {
     return CanvasBorderFactory.BORDER_SHADOW_NORMAL;
 }

 private class SelectionProviderImpl implements SelectProvider {

   public boolean isAimingAllowed(Widget widget, Point localLocation,
boolean invertSelection) {
       return false;
   }

   public boolean isSelectionAllowed(Widget widget, Point
localLocation, boolean invertSelection) {
//        Object object = findObject(widget);
//        return object != null  &&  (invertSelection  ||  !
getSelectedObjects().contains(object));
     return true;
   }

   public void select(Widget widget, Point localLocation, boolean
invertSelection) {
//        Object object = findObject(widget);
//        selectObject(object, invertSelection);
//
       setFocusedWidget(widget);
   }

 }

 private class AcceptProviderImpl implements AcceptProvider {
   public ConnectorState isAcceptable(Widget widget, Point point,
Transferable transferable) {
     if (
transferable.isDataFlavorSupported(PaletteTransferable.FLAVOUR)) {
       return ConnectorState.ACCEPT;
     }
     else
       return ConnectorState.REJECT;
   }

   public void accept(Widget widget, Point point, Transferable
transferable) {
     try {
       final DesignerHandle handle =
(DesignerHandle)transferable.getTransferData(PaletteTransferable.FLAVOUR);

       Widget wid = addNode(handle);
       wid.setPreferredLocation(point);

       Dimension dim = handle.getComponentDataBean().getPreferredSize();
       if ( dim == null ) {
         dim =
handle.getRuntimeComponent().getViewDelegate().getComponent().getPreferredSize();

       }
       //TODO P3 - 10 is magic number, based on the border around the
widget.
       wid.setPreferredSize(new Dimension(dim.width + 10, dim.height +
10));
       onAccept(handle);
     } catch (UnsupportedFlavorException ex) {
       ex.printStackTrace();
     } catch (IOException ex) {
       ex.printStackTrace();
     }
   }
   }

 private class CPResolver implements ResizeControlPointResolver {
   private ResizeControlPointResolver original =
ActionFactory.createDefaultResizeControlPointResolver();

   public ResizeProvider.ControlPoint resolveControlPoint(Widget
widget, Point point) {
     ResizeProvider.ControlPoint ret =
original.resolveControlPoint(widget, point);
     if (ret == null) {
         return ret;
     }
     Object obj = CanvasModel.this.findObject(widget);
     if (obj != null) {
         DesignerHandle wr = (DesignerHandle)obj;
         int dirs =
wr.getComponentDataBean().getResizeDirections();              
         if ((dirs & ComponentDataBean.RESIZE_HORIZONTAL) == 0) {
             if (ret == ResizeProvider.ControlPoint.CENTER_LEFT ||
                 ret == ResizeProvider.ControlPoint.CENTER_RIGHT) {
                 return null;
             }
             if (ret == ResizeProvider.ControlPoint.TOP_LEFT || ret ==
ResizeProvider.ControlPoint.TOP_RIGHT) {
                 ret =  ResizeProvider.ControlPoint.TOP_CENTER;
             }
             if (ret == ResizeProvider.ControlPoint.BOTTOM_RIGHT ||
ret == ResizeProvider.ControlPoint.BOTTOM_LEFT) {
                 ret = ResizeProvider.ControlPoint.BOTTOM_CENTER;
             }
         }
         if ((dirs & ComponentDataBean.RESIZE_VERTICAL) == 0) {
             if (ret == ResizeProvider.ControlPoint.TOP_CENTER ||
                 ret == ResizeProvider.ControlPoint.BOTTOM_CENTER) {
                 return null;
             }
             if (ret == ResizeProvider.ControlPoint.TOP_LEFT || ret ==
ResizeProvider.ControlPoint.BOTTOM_LEFT) {
                 ret = ResizeProvider.ControlPoint.CENTER_LEFT;
             }
             if (ret == ResizeProvider.ControlPoint.TOP_RIGHT || ret
== ResizeProvider.ControlPoint.BOTTOM_RIGHT) {
                 ret = ResizeProvider.ControlPoint.CENTER_RIGHT;
             }
         }
     }
     return ret;
   }
 }
 private class AlignCollector implements AlignWithWidgetCollector {
   private boolean resize;
   public AlignCollector(boolean res) {
       resize = res;
   }

   public Collection<Rectangle> getRegions(Widget widget) {
       Collection<Rectangle> toRet = new ArrayList<Rectangle>();
       for (DesignerHandle dh : CanvasModel.this.getNodes()) {
           Widget wid = findWidget(dh);
           if (wid != widget) {
               toRet.add(wid.convertLocalToScene(wid.getBounds()));
           } else if (resize) {
               Point loc = wid.getLocation();
               Dimension size =
dh.getComponentDataBean().getPreferredSize();
               if (size == null) {
                   size = wid.getPreferredSize();
               }
               //magic because of borders..
               Rectangle r = new Rectangle (loc.x - BORDER_SIZE, loc.y
- BORDER_SIZE, size.width + BORDER_SIZE * 2, size.height + BORDER_SIZE
* 2);
               toRet.add(r);
           }
       }
       //somewhat hack, would be more happy with Integer.MAX_VALUE but
that one freaks out the view.
       toRet.add(new Rectangle(0, 0, 0, 10000));
       toRet.add(new Rectangle(0, 0, 10000, 0));
       return toRet;
   }
 }
 private class KeyEventLoggerAction extends WidgetAction.Adapter {

   public State keyPressed(Widget widget, WidgetKeyEvent event) {
     return State.REJECTED;
   }

   public State keyTyped(Widget widget, WidgetKeyEvent event) {
     return State.REJECTED;
   }

   public State keyReleased(Widget widget, WidgetKeyEvent event) {
     if (event.getKeyCode() == KeyEvent.VK_DELETE) {
         DesignerHandle handle =
(DesignerHandle)CanvasModel.this.findObject(widget);
         if (handle == null) {
             return State.REJECTED;
         }
         if ( JOptionPane.OK_OPTION ==
JOptionPane.showConfirmDialog(null,"Are you sure you want to remove
the widget?")) {
           CanvasModel.this.removeNode(handle);
         }
         return State.CONSUMED;
     };
     return State.REJECTED;
   }

 }

 private class DeselectionImpl implements SelectProvider {

   public boolean isAimingAllowed(Widget widget, Point localLocation,
boolean invertSelection) {
     return false;
   }

   public boolean isSelectionAllowed(Widget widget, Point
localLocation, boolean invertSelection) {
     if (widget == CanvasModel.this) {
       return true;
     }
     return false;
   }

   public void select(Widget widget, Point localLocation, boolean
invertSelection) {
     if (widget == CanvasModel.this) {
       if (getFocusedObject() != null) {
          
findWidget(getFocusedObject()).setBorder(createDesignBorder());
       }
       setFocusedObject(null);
       userSelectionSuggested(Collections.emptySet(), false);
     }
   }

 }
}



Chris Palmer wrote:

    
Hello!

I reworked the EnterKeyProcessingTest to try and match a similiar
implementation that I have.  I got a null pointer.

Thats besides the fact. With a basic scene and no layer widgets I was
able to get the a delete to work. I used EnterKeyProcessingTest a
base. But then I realized that the EnterKeyProcessingTest wasn't
using layer widgets.
So this is my attempt at reproducing my impl.


Chris


package test.keyboard;

import java.awt.Point;
import java.awt.event.KeyEvent;
import org.netbeans.api.visual.action.ActionFactory;
import org.netbeans.api.visual.action.SelectProvider;
import org.netbeans.api.visual.action.TextFieldInplaceEditor;
import org.netbeans.api.visual.action.WidgetAction;
import org.netbeans.api.visual.action.WidgetAction.State;
import org.netbeans.api.visual.layout.LayoutFactory;
import org.netbeans.api.visual.model.ObjectScene;
import org.netbeans.api.visual.widget.EventProcessingType;
import org.netbeans.api.visual.widget.LabelWidget;
import org.netbeans.api.visual.widget.LayerWidget;
import org.netbeans.api.visual.widget.Scene;
import org.netbeans.api.visual.widget.Widget;
import test.SceneSupport;

/**
* @author David Kaspar
*/
public class EnterKeyProcessingTest {
   private static WidgetAction deleteAction = new
KeyEventLoggerAction();
   public static void main(String[] args) {
       ObjectScene scene = new ObjectScene();
             LayerWidget main = new LayerWidget(scene);
       LayerWidget interaction = new LayerWidget(scene);
       scene.addChild(main);
       scene.addChild(interaction);
       main.setLayout(LayoutFactory.createVerticalFlowLayout());
//        scene.setLayout(LayoutFactory.createVerticalFlowLayout ());
             LabelWidget label1 = new LabelWidget(scene, "First
non-editable label. Should not receive any key event.");
       label1.getActions().addAction(deleteAction);
      
label1.getActions().addAction(ActionFactory.createSelectAction(new
KeySelectProvider()));
       main.addChild(label1);
             LabelWidget label2 = new LabelWidget(scene, "Second
editable label.");
       label2.getActions().addAction(deleteAction);
      
label2.getActions().addAction(ActionFactory.createSelectAction(new
KeySelectProvider()));
       main.addChild(label2);
             LabelWidget label3 = new LabelWidget(scene, "Third
non-editable label. Should not receive any key event.");
       label3.getActions().addAction(deleteAction);
      
label3.getActions().addAction(ActionFactory.createSelectAction(new
KeySelectProvider()));
       main.addChild(label3);
            
scene.setKeyEventProcessingType(EventProcessingType.FOCUSED_WIDGET_AND_ITS_CHILDREN);

//        scene.setFocusedWidget (label2);
             SceneSupport.show(scene);
   }
     private static class KeySelectProvider implements SelectProvider {
         public boolean isAimingAllowed(Widget widget, Point
localLocation, boolean invertSelection) {
       return false;
     }

     public boolean isSelectionAllowed(Widget widget, Point
localLocation, boolean invertSelection) {
       return true;
     }

     public void select(Widget widget, Point localLocation, boolean
invertSelection) {
       Scene scene = widget.getScene();
       scene.setFocusedWidget(widget);
     }
       }
   private static class KeyEventLoggerAction extends
WidgetAction.Adapter {
             public State keyPressed(Widget widget, WidgetKeyEvent
event) {
           System.out.println("KeyPressed at " + ((LabelWidget)
widget).getLabel());
           return State.REJECTED;
       }
             public State keyReleased(Widget widget, WidgetKeyEvent
event) {
         if (event.getKeyCode() == KeyEvent.VK_DELETE) {
           System.out.println("KeyReleased at " + ((LabelWidget)
widget).getLabel());
           final Scene scene = widget.getScene();
           scene.removeChild(widget);
           return State.CONSUMED;
         }
         return State.REJECTED;
       }
             public State keyTyped(Widget widget, WidgetKeyEvent
event) {
           System.out.println("KeyTyped at " + ((LabelWidget)
widget).getLabel());
           return State.REJECTED;
       }
         }
     private static class LabelEditor implements
TextFieldInplaceEditor {
             public boolean isEnabled(Widget widget) {
           return true;
       }

       public String getText(Widget widget) {
           return ((LabelWidget) widget).getLabel();
       }
             public void setText(Widget widget, String text) {
           ((LabelWidget) widget).setLabel(text);
       }
         }
  }

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe-Hi7mZnf3vbIkiw2pgZvNdR2eb7JE58TQ@xxxxxxxxxxxxxxxx
For additional commands, e-mail: users-help-Hi7mZnf3vbIkiw2pgZvNdR2eb7JE58TQ@xxxxxxxxxxxxxxxx


      
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe-Hi7mZnf3vbIkiw2pgZvNdR2eb7JE58TQ@xxxxxxxxxxxxxxxx
For additional commands, e-mail: users-help-Hi7mZnf3vbIkiw2pgZvNdR2eb7JE58TQ@xxxxxxxxxxxxxxxx

    

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe-Hi7mZnf3vbIkiw2pgZvNdR2eb7JE58TQ@xxxxxxxxxxxxxxxx
For additional commands, e-mail: users-help-Hi7mZnf3vbIkiw2pgZvNdR2eb7JE58TQ@xxxxxxxxxxxxxxxx


  

<Prev in Thread] Current Thread [Next in Thread>
Google Custom Search

News | FAQ | advertise