To make the JGoArea grab JGoObjects


#1

Hi,
I have classes that extend JGoArea and JGoObject.
When the Object of the JGoObject subclass is dragged from the toolbar onto the Object of the JGoArea subclass, it is grabbed by the JGoArea object within the JGoView.
But when am trying to move the JGoObject subclass object within the JGoView onto the JGoArea object, it is not being grabbed by the JGoArea Object.
I would be thankful if anyone can thrown some light on this.
Thanks,
Swapna


#2

You didn’t say how you were capturing those events, but I assume by means of overrides of JGoView’s dragEnter/dragOver/drop. These methods just get called for external drag-and-drops–i.e. ones that start from a different window.
For internal drag-and-drops–i.e. ones that start from the same JGoView–the doMouseMove and doMouseUp methods are called. The actual drag/drop event handler methods that are called are onDragEnter/onDragOver/onDrop, which call doMouseMove/doMouseUp for internal drag-and-drops, and which call dragEnter/dragOver/drop for external drag-and-drops.
Also, when drag-and-drop is disabled for whatever reason, the same doMouseMove and doMouseUp methods are called, so the behavior is reasonably compatible.
The doMouseMove/doMouseUp methods then look at the current JGoView.getState() to decide what to do. I suppose you’ll care about the state being MouseStateMove.


#3

Hi,
Thanks for your immediate reply.
But I would like to know that whether i need to modify doMouseup or doMouseMove method in order for the grab functionality to be in effect.
Here we didnt override the dragEnter/dragOver/drop methods of the JGoView.
Just overridden the doMoveSelection,doMouseUp, doMouseDown and doMouseClick method in the view class which extend JGoView.


#4

Yes, I think an override of doMouseUp would be the right time; that method just calls doMoveSelection when getState() == MouseStateMove.


#5

Hi,
I have added the following code to overriden method of doMouseUp
//if mousestate is move
if(getState()==MouseStateMove)
{
JGoObject object = pickDocObject(dc, true);


if(object instanceof BPNode&& ((BPNode) object).isSubGraph()) {
BPNode lNode = (BPNode) object;
lNode.setGrabChildSelection(true);
lNode.setText(“NewWhile”);
}//end of inner if
}//end of outter if
The text is getting changed when i move a JGoObject onto the JGoArea within the canvas but the object is not grabbed by the JGoArea.
Does the setGrabChildSelection method will do the needed ?
Thanks,
Swapna


#6

No, you shouldn’t call JGoArea.setGrabChildSelection because that just affects whether pickDocObject will pick that JGoArea when the user tries to pick a child object that is not selectable. In other words, that flag affects whether the user can select a JGoSubGraph, for example, by clicking on the background of the JGoSubGraph.
I had thought you would want to call JGoArea.addObjectAtTail with each object in the selection.


#7

Hi,
Tried with JGoArea.addObjectAtTail but no use. Any other method to be tried?
Thanks,
Swapna


#8

Hi Walter,
I had overridden the doMouseUp method as follows:
public boolean doMouseUp(int modifiers, Point dc, Point vc) {
if(getState()==MouseStateMove)
{


JGoSelection sel = getSelection();
JGoDocument doc = getDocument();

if (sel != null)
{

for (JGoListPosition pos = sel.getFirstObjectPos(); pos != null;
pos = sel.getNextObjectPosAtTop(pos))
{
JGoObject obj = sel.getObjectAtPos(pos);

BPNode lNode = null;
JGoObject object = pickDocObject(dc, false);

if(object instanceof BPNode&& ((BPNode) object).isSubGraph)) {

if (doc != null &&doc.getDefaultLayer().isModifiable())
{
doc.startTransaction();
lNode = (BPNode) object;
lNode.setText(“tester”);
lNode.addObjectAtTail(obj);
lNode.addObjectAtHead(obj);
doc.endTransaction(true);
}
}
}
}

}
return super.doMouseUp(modifiers, dc, vc);
}
The problem is that the following statement
JGoObject object = pickDocObject(dc, false);

is not returning the JGoArea object but its returning the JGoObject object that is being moved onto the JGoArea.
I have tried different methods but still no use, which method will give me the object already existing at particular position where another object is moved?
Thanks,
Swapna


#9

Ah, yes, that’s the problem. Here’s some code adapted from the next release of JGo, which you can add to your subclass of JGoDocument:
public ArrayList pickObjects(Point p, boolean selectableOnly, ArrayList coll, int max) {
if (coll == null)
coll = new ArrayList();
JGoLayer layer = getLastLayer();
while (layer != null) {
if (layer.isVisible()) {
if (coll.size() >= max) return coll;
pickLayerObjects(layer, p, selectableOnly, coll, max);
}
layer = layer.getPrevLayer();
}
return coll;
}
private ArrayList pickLayerObjects(JGoLayer layer, Point p, boolean selectableOnly, ArrayList coll, int max) {
if (coll == null)
coll = new ArrayList();
if (coll.size() >= max) return coll;
if (!layer.isVisible()) return coll;

JGoListPosition pos = layer.getLastObjectPos();
while (pos != null) {
JGoObject obj = layer.getObjectAtPos(pos);
pos = layer.getPrevObjectPos(pos);
if (obj instanceof JGoSubGraph) {
JGoSubGraph sg = (JGoSubGraph)obj;
pickSubGraphObjects(sg, p, selectableOnly, coll, max);
} else if (obj instanceof JGoArea) {
JGoArea area = (JGoArea)obj;
pickAreaObjects(area, p, selectableOnly, coll, max);
}
if (obj.isVisible() && obj.isPointInObj§ && (!selectableOnly || obj.isSelectable())) {
coll.add(obj);
if (coll.size() >= max) return coll;
}
}
return coll;
}
private ArrayList pickAreaObjects(JGoArea area, Point p, boolean selectableOnly, ArrayList coll, int max) {
if (coll == null)
coll = new ArrayList();
if (coll.size() >= max) return coll;
if (!area.isVisible()) return coll;
JGoObject picked = area.pickObject(p, selectableOnly);
if (picked != null) {
coll.add(picked);
}
return coll;
}
private ArrayList pickSubGraphObjects(JGoSubGraph sg, Point p, boolean selectableOnly, ArrayList coll, int max) {
if (coll == null)
coll = new ArrayList();
if (coll.size() >= max) return coll;
if (!sg.isVisible()) return coll;
// make sure that the point is even in the area
if (!sg.isPointInObj§) return coll;
JGoListPosition pos = sg.getLastObjectPos();
while (pos != null) {
JGoObject obj = sg.getObjectAtPos(pos);
pos = sg.getPrevObjectPos(pos);
if (obj.isVisible() && obj.isPointInObj§) {
if (obj instanceof JGoSubGraph) {
pickSubGraphObjects(((JGoSubGraph)obj), p, selectableOnly, coll, max);
if (coll.size() >= max) return coll;
} else if (obj instanceof JGoArea) {
pickAreaObjects(((JGoArea)obj), p, selectableOnly, coll, max);
if (coll.size() >= max) return coll;
} else if (!selectableOnly || obj.isSelectable()) {
coll.add(obj);
if (coll.size() >= max) return coll;
}
}
}
if (sg.isGrabChildSelection()) {
coll.add(sg);
}
return coll;
}
You’ll want to call the pickObjects method and then iterate over the resulting ArrayList, skipping over the object(s) that you are moving, including any parts of the objects that you are moving, to find the first object underneath what you are moving. If there is just a single simple object that you are moving, that object will presumably be found as the first object in the ArrayList, and the second one will be the front-most selectable object underneath it.
ArrayList found = myDoc.pickObjects(dc, true, null, 2);


#10

Hi Walter,
I have added the methods to subclass of JGoDocument and tried using the method:
myDoc.pickObjects(dc, true, null, 2);
But the problem is that

  1. if am moving a JGoObject present within JGoArea its returning the JGoArea object
  2. when am moving a JGoObject within document which is not within JGoArea onto the JGoArea its returning the JGoObject itself.
    public boolean doMouseUp(int modifiers, Point dc, Point vc) {
    if(getState()==MouseStateMove)
    {

    JGoSelection sel = getSelection();
    JGoDocument doc = getDocument();
    if (sel != null) {
    for (JGoListPosition pos = sel.getFirstObjectPos(); pos != null;
    pos = sel.getNextObjectPosAtTop(pos)) {
    JGoObject obj = sel.getObjectAtPos(pos);

    BPNode lNode = null;
    ArrayList a1 = this.getModel().pickObjects(dc, true, null, 2);
    JGoObject object = null;
    javax.swing.JOptionPane.showMessageDialog (null,“size:”+a1.size(),“I am in BPCanvasView.java”,javax.swing.JOptionPane.INFORMATION_MESSA GE);
    for(int i=0;i<a1.size();i++)
    {
    object = (JGoObject)a1.get(i);

    javax.swing.JOptionPane.showMessageDialog (null,“main1:”+object.getClass().toString(),“I am in BPCanvasView.java”,javax.swing.JOptionPane.INFORMATION_MESSA GE);
    javax.swing.JOptionPane.showMessageDialog (null,“parent:”+object.getParent().getClass().toString(),“I am in BPCanvasView.java”,javax.swing.JOptionPane.INFORMATION_MESSA GE);
    }

    if(object instanceof BPNode&& ((BPNode) object).isSubGraph()){

    if (doc != null && doc.getDefaultLayer().isModifiable())
    {
    doc.startTransaction();
    lNode = (BPNode) object;

    lNode.setText(“tester”);

    lNode.addObjectAtTail(obj);
    lNode.addObjectAtHead(obj);
    doc.endTransaction(true);
    }
    }
    }
    }

    }
    return super.doMouseUp(modifiers, dc, vc);
    }
    Thanks,
    Swapna

#11

I thought 1. was what you wanted.
If it isn’t exactly what you wanted, you can adapt the code for your own purposes.


#12

Hi Walter,
I have written a method which identifies the JGoArea object, when I move a JGoObject on to JGoArea of which the JGoObject is not part within the JGoView.
Which method adds the JGoObject onto the JGoArea?
I had tried using addObjectAtHead and addObjectAtTail methods, but not serving the purpose.
Thanks in Advance,
Swapna G


#13

Yes, that’s right, but perhaps you need to remove it from it’s old parent area first.
In version 5.1 (almost in beta), there’s an addCollection method on both JGoArea and JGoLayer which lets you “re-parent” objects without disconnecting any links, caused by doing the removal.


#14

Hi Walter,
I added setParent method to subclasses of JGoObject and its working fine.
But the problem is that, if am moving multiple objects on to the JGoArea, they r getting attached to JGoArea and other functionalities are working as usual. But When am moving a single JGoObject onto the JGoArea, after it gets attached to JGoArea the selection of the other objects is not working.
Only am able to select and move the single JGoObject that i moved.
Thanks,
Swapna


#15

I don’t understand your situation. Are you saying that after reparenting a single object, users can’t select the other children of the JGoArea? Are they selectable, and are they not occluded by other objects in front of them?