package research.align;
import research.Figure;
import research.ConnectionFigure;
import research.DrawingEditor;
import javax.swing.*;
import java.util.ArrayList;
import java.util.Iterator;
import java.awt.event.ActionEvent;
import java.awt.*;
/**
* Created by IntelliJ IDEA.
* User: saturn
* Date: 2003-10-15
* Time: 19:02:36
* To change this template use Options | File Templates.
*/
class HoriDistAction extends DistAction {
ArrayList arrayList = new ArrayList();
public HoriDistAction() {
this.putValue(Action.NAME, "ˮƽ�ֲ�");
}
public void actionPerformed(ActionEvent e) {
if (!isEnabled()) return;
caculateAffectedArea();
DrawingEditor drawingEditor = (DrawingEditor)getValue(ConstantDefinition.DRAWING_EDITOR);
Iterator iterator = drawingEditor.getCurrentView().getSelection().iterator();
int figureCount = 0;
arrayList.clear();
while (iterator.hasNext()) {
Figure figure = (Figure) iterator.next();
if (!(figure instanceof ConnectionFigure)) {
figureCount++;
Point center = figure.center();
if (arrayList.isEmpty()) {
arrayList.add(figure);
} else {
int i;
for (i = 0; i < arrayList.size(); i++) {
Figure fig = (Figure) arrayList.get(i);
if (center.x < fig.center().x) {
arrayList.add(i, figure);
break;
}
}
if (i == arrayList.size())
arrayList.add(figure);
}
}
}
int filled = 0;
for (int i = 0; i < arrayList.size(); i++) {
Figure fig = (Figure) arrayList.get(i);
if ((i == 0) || (i == arrayList.size() - 1)) {
filled += fig.getDisplayBox().width / 2;
} else {
filled += fig.getDisplayBox().width;
}
}
double unit = (rect.width * 1.0 - filled) / (figureCount - 1);
Rectangle r = ((Figure) arrayList.get(0)).getDisplayBox();
int base = r.x + r.width;
for (int i = 1; i < arrayList.size() - 1; i++) {
Figure fig = (Figure) arrayList.get(i);
fig.moveBy((int) (base + unit * i - fig.getDisplayBox(r).x), 0);
base += r.width;
}
drawingEditor.getCurrentView().repairDamage();
}
}