Page Index Toggle Pages: 1 Send TopicPrint
Normal Topic SnapToBorder not working sometimes (Read 2934 times)
koosala
Full Member
***
Offline


Java Happens

Posts: 136
Joined: May 13th, 2007
SnapToBorder not working sometimes
Apr 22nd, 2008 at 3:24am
Print Post  
Hi,

I have given a sample code here:

[code]

package research;

import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JApplet;

import com.mindfusion.jdiagram.Arrow;
import com.mindfusion.jdiagram.ArrowStyle;
import com.mindfusion.jdiagram.Box;
import com.mindfusion.jdiagram.FlowChart;
import com.mindfusion.jdiagram.FlowChartAdapter;
import com.mindfusion.jdiagram.GraphicsUnit;
import com.mindfusion.jdiagram.ItemMouseEvent;
import com.mindfusion.jdiagram.RerouteArrows;
import com.mindfusion.jdiagram.ShadowsStyle;
import com.mindfusion.jdiagram.SnapToAnchor;

public class Goop extends JApplet {
private static final long serialVersionUID = 1L;

private FlowChart flowChart;

@Override
public void init() {
super.init();
flowChart = new FlowChart();
flowChart.setSelectionOnTop(false);
flowChart.setShadowsStyle(ShadowsStyle.None);
this.getContentPane().add(flowChart);
flowChart.setMeasureUnit(GraphicsUnit.Pixel);

flowChart.setArrowStyle(ArrowStyle.Cascading);
flowChart.setSnapToAnchor(SnapToAnchor.OnCreateOrModify);
flowChart.setRouteArrows(true);
flowChart.getRoutingOptions().setTriggerRerouting(
RerouteArrows.WhileCreating | RerouteArrows.WhenModified
| RerouteArrows.WhenIntersectNode);

testRouting();
}

Box one, two;

Arrow three;

private void testRouting() {
one = new Box(flowChart);
two = new Box(flowChart);
one.setBounds(12, 379, 125, 92);
two.setBounds(112, 87, 125, 75);
flowChart.add(one);
flowChart.add(two);

three = new Arrow(flowChart, one, two);
three.setSnapToNodeBorder(true);
three.setAutoRoute(false);
three.setStyle(ArrowStyle.Polyline);

three.setSegmentCount(2);
three.getControlPoints().set(0, new Point(137, 424));
three.getControlPoints().set(1, new Point(170, 424));
three.getControlPoints().set(2, new Point(170, 162));

flowChart.add(three);

flowChart.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
if (flowChart.getItemAt(e.getPoint(), true) != null) {
return;
}

one.setBounds(12, 70, 125, 92);
for (Arrow arrow : one.getOutgoingArrows()) {
arrow.setStyle(ArrowStyle.Cascading);
arrow.route();
}
}
});

flowChart.addFlowChartListener(new FlowChartAdapter() {
@Override
public void itemModified(ItemMouseEvent e) {
three.setStyle(ArrowStyle.Cascading);
three.route();
}
});
}
}

[/code]

I have a lot of functionality build around JDiagram and there are specific situations I want the arrow to be polyline initially and on modifying a symbol, its arrows are made cascading (aligning in this case).

If you click on a blank area of the canvas, the bottom box aligns its lower border with the top symbol. During the aligning, the arrow is made cascading. The arrow routes between the centers of the two symbols.

Also, if you drag the top symbol to the right, you will notice that the start and end points of the arrow where it snapped to the boxes change.

Can I change this behavior?

Thanks,
Praveen
  
Back to top
 
IP Logged
 
Stoyo
God Member
*****
Offline


MindFusion support

Posts: 13230
Joined: Jul 20th, 2005
Re: SnapToBorder not working sometimes
Reply #1 - May 4th, 2008 at 8:29am
Print Post  
That seems to happen because calling setStyle also resets all arrow points as if the arrow is created anew. To work around this, save the first and last point positions before calling setStyle, and restore them to the saved values before calling route().

Stoyan
  
Back to top
 
IP Logged
 
koosala
Full Member
***
Offline


Java Happens

Posts: 136
Joined: May 13th, 2007
Re: SnapToBorder not working sometimes
Reply #2 - May 6th, 2008 at 9:50am
Print Post  
Hi Stoyan,

I tried the following code, on the mouse click handling:

[code]
arrow.setStyle(ArrowStyle.Cascading);
three.getControlPoints().set(0, new Point(137, 115));
three.getControlPoints().set(2, new Point(170, 162));
arrow.route();
[/code]

But still the behavior is not completely clean. If you now drag the second box to the right, you will see the arrow jumps to the center of the second box.

This seems to happen only when the box are overlapping.

If the second box was placed further right

[code]
two.setBounds(112, 87, 125, 75);
[/code]

and the arrows changed appropriately, this problem doesn't appear.

Is overlapping the issue then?

Thanks,
Praveen
  
Back to top
 
IP Logged
 
Stoyo
God Member
*****
Offline


MindFusion support

Posts: 13230
Joined: Jul 20th, 2005
Re: SnapToBorder not working sometimes
Reply #3 - May 7th, 2008 at 11:20am
Print Post  
Hi Praveen,

This works fine:

Code
Select All
@Override
public void itemModified(ItemMouseEvent e)
{
	Point2D fp = (Point2D)
		three.getControlPoints().get(0).clone();
	Point2D lp = (Point2D)
		three.getControlPoints().get(three.getControlPoints().size() - 1).clone();
	three.setStyle(ArrowStyle.Cascading);
	three.getControlPoints().get(0).setLocation(fp);
	three.getControlPoints().get(three.getControlPoints().size() - 1).setLocation(lp);
	three.route();
}
 



I hope that helps,
Stoyan
  
Back to top
 
IP Logged
 
koosala
Full Member
***
Offline


Java Happens

Posts: 136
Joined: May 13th, 2007
Re: SnapToBorder not working sometimes
Reply #4 - May 8th, 2008 at 9:35am
Print Post  
Hi Stoyan,

I have the following code now:

[code]
package research;

import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.geom.Point2D;

import javax.swing.JApplet;

import com.mindfusion.jdiagram.Arrow;
import com.mindfusion.jdiagram.ArrowStyle;
import com.mindfusion.jdiagram.Box;
import com.mindfusion.jdiagram.FlowChart;
import com.mindfusion.jdiagram.FlowChartAdapter;
import com.mindfusion.jdiagram.GraphicsUnit;
import com.mindfusion.jdiagram.ItemMouseEvent;
import com.mindfusion.jdiagram.RerouteArrows;
import com.mindfusion.jdiagram.ShadowsStyle;
import com.mindfusion.jdiagram.SnapToAnchor;

public class Goop extends JApplet {
private static final long serialVersionUID = 1L;

private FlowChart flowChart;

@Override
public void init() {
super.init();
flowChart = new FlowChart();
flowChart.setSelectionOnTop(false);
flowChart.setShadowsStyle(ShadowsStyle.None);
this.getContentPane().add(flowChart);
flowChart.setMeasureUnit(GraphicsUnit.Pixel);

flowChart.setArrowStyle(ArrowStyle.Cascading);
flowChart.setSnapToAnchor(SnapToAnchor.OnCreateOrModify);
flowChart.setRouteArrows(true);
flowChart.getRoutingOptions().setTriggerRerouting(
RerouteArrows.WhileCreating | RerouteArrows.WhenModified
| RerouteArrows.WhenIntersectNode);

testRouting();
}

Box one, two;

Arrow three;

private void testRouting() {
one = new Box(flowChart);
two = new Box(flowChart);
one.setBounds(12, 379, 125, 92);
one.setObstacle(false);
two.setObstacle(false);
two.setBounds(112, 87, 125, 75);
flowChart.add(one);
flowChart.add(two);

three = new Arrow(flowChart, one, two);
three.setSnapToNodeBorder(true);
three.setAutoRoute(false);
three.setStyle(ArrowStyle.Polyline);

three.setSegmentCount(2);
three.getControlPoints().set(0, new Point(137, 424));
three.getControlPoints().set(1, new Point(170, 424));
three.getControlPoints().set(2, new Point(170, 162));

flowChart.add(three);

flowChart.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
if (flowChart.getItemAt(e.getPoint(), true) != null) {
return;
}

Point2D fp = (Point2D) three.getControlPoints().get(0).clone();
Point2D lp =
(Point2D) three.getControlPoints().get(
three.getControlPoints().size() - 1).clone();
three.setStyle(ArrowStyle.Cascading);
three.getControlPoints().get(0).setLocation(fp);
three.getControlPoints().get(
three.getControlPoints().size() - 1).setLocation(lp);

one.setBounds(12, 70, 125, 92);

three.route();
}
});

flowChart.addFlowChartListener(new FlowChartAdapter() {
@Override
public void itemModified(ItemMouseEvent e) {
three.route();
super.itemModified(e);
}
});
}
}

[/code]

I had to put your changes into the click handler itself as the modified event is not fired for programmatically relocating the symbol.

The arrow now seems to stay at the borders, which is nice.

One more problem I see is that the origin and the destination have now moved.

The origin was on the right border, and the destination was on the bottom border.

After the act, the origin moved to the top border and the destination to the left border.

Thanks,
Praveen
  
Back to top
 
IP Logged
 
Stoyo
God Member
*****
Offline


MindFusion support

Posts: 13230
Joined: Jul 20th, 2005
Re: SnapToBorder not working sometimes
Reply #5 - May 8th, 2008 at 10:52am
Print Post  
Hi Praveen,

Call Arrow.updateFromPoints(); immediately after setting the point positions.

Stoyan
  
Back to top
 
IP Logged
 
Page Index Toggle Pages: 1
Send TopicPrint