package research.util;
import research.*;
import java.awt.*;
import java.util.*;
import de.FeatureModellingTool.GraphicalEditor.VPConstraintFigure;
import de.FeatureModellingTool.GraphicalEditor.PLConnection;
public class GlobalFunction {
public static Rectangle checkMinimumViewport(DrawingView drawingView) {
FigureEnumeration k = drawingView.getDrawing().getFigures();
Rectangle rect = new Rectangle(0, 0, 0, 0);
Dimension size = drawingView.getPageSize();
int x1 = size.width, y1 = size.height;
int x2 = 0, y2 = 0;
while (k.hasMoreElements()) {
Figure fig = k.nextFigure();
Rectangle r = fig.getDisplayBox();
if (r.x < x1) x1 = r.x;
if (r.y < y1) y1 = r.y;
if (r.x + r.width > x2) x2 = r.x + r.width;
if (r.y + r.height > y2) y2 = r.y + r.height;
}
if (x1 < 0) x1 = 0;
if (y1 < 0) y1 = 0;
rect.x = x1;
rect.y = y1;
rect.width = x2 - x1;
rect.height = y2 - y1;
return rect;
}
public static java.util.List getParentFigures(Figure root) {
if (root instanceof ConnectionFigure) {
root = ((ConnectionFigure) root).endFigure();
}
ArrayList result = new ArrayList(1);
;
ConnectionFigure[] cfArray = GlobalFunction.getEndConnectedFigures(root);
for (int i = 0; i < cfArray.length; i++) {
ConnectionFigure cf = cfArray[i];
if ((cf.endFigure() == root) && (cf.startFigure() != root))
result.add(cf.startFigure());
}
return result;
}
public static interface Satisfy {
boolean isSatisfied(Figure figure);
}
public static java.util.List getOffspringFigures(Figure root) {
if (root instanceof ConnectionFigure) {
root = ((ConnectionFigure) root).startFigure();
}
ArrayList result;
int currentIndex;
ConnectionFigure[] cfArray = GlobalFunction.getStartConnectedFigures(root);
result = new ArrayList(cfArray.length * 3);
currentIndex = 0;
for (int i = 0; i < cfArray.length; i++) {
Figure fig = cfArray[i].endFigure();
if (fig != root)
result.add(fig);
}
caculateOffspringFigures(result, currentIndex);
return result;
}
public static void caculateOffspringFigures(ArrayList list, int index, Satisfy constraintToConnection) {
while (index < list.size()) {
Figure fig = (Figure) list.get(index);
ConnectionFigure[] cfArray = GlobalFunction.getStartConnectedFigures(fig, constraintToConnection);
for (int i = 0; i < cfArray.length; i++) {
Figure currentFig = cfArray[i].endFigure();
boolean isValid = true;
for (int k = 0; k < list.size(); k++) {
Figure tmpF = (Figure) list.get(k);
if (tmpF == currentFig) {
isValid = false;
break;
}
}
if (isValid)
list.add(currentFig);
}
index++;
}
}
/**
* ������������и��ü�ֵ
*
* @param list
* @param index
* @param constraintToConnection
*/
public static void caculateFMOffspringFigures(ArrayList list, int index, Satisfy constraintToConnection) {
while (index < list.size()) {
Figure fig = (Figure) list.get(index);
ConnectionFigure[] cfArray = GlobalFunction.getStartConnectedFigures(fig, constraintToConnection);
for (int i = 0; i < cfArray.length; i++) {
Figure currentFig = cfArray[i].endFigure();
boolean isValid = true;
for (int k = 0; k < list.size(); k++) {
Figure tmpF = (Figure) list.get(k);
if (tmpF == currentFig) {
isValid = false;
break;
}
}
if (isValid)
list.add(currentFig);
}
cfArray = GlobalFunction.getEndConnectedFigures(fig,
new Satisfy() {
public boolean isSatisfied(Figure fig) {
return PLConnection.class.isInstance(fig);
}
},
new Satisfy() {
public boolean isSatisfied(Figure fig) {
return VPConstraintFigure.class.isInstance(fig);
}
}
);
for (int i = 0; i < cfArray.length; i++) {
VPConstraintFigure currentFig = (VPConstraintFigure) cfArray[i].startFigure();
if (cfArray[i].getStartConnector() != currentFig.getStartConnector())
continue;
boolean isValid = true;
for (int k = 0; k < list.size(); k++) {
Figure tmpF = (Figure) list.get(k);
if (tmpF == currentFig) {
isValid = false;
break;
}
}
if (isValid)
list.add(currentFig);
}
index++;
}
}
public static void caculateOffspringFigures(ArrayList list, int index) {
while (index < list.size()) {
Figure fig = (Figure) list.get(index);
ConnectionFigure[] cfArray = GlobalFunction.getStartConnectedFigures(fig);
for (int i = 0; i < cfArray.length; i++) {
Figure currentFig = cfArray[i].endFigure();
boolean isValid = true;
for (int k = 0; k < list.size(); k++) {
Figure tmpF = (Figure) list.get(k);
if (tmpF == currentFig) {
isValid = false;
break;
}
}
if (isValid)
list.add(currentFig);
}
index++;
}
}
public static ConnectionFigure[] getEndConnectedFigures(Figure root, Satisfy constraintToConnection, Satisfy constraintToStartFigure) {
ConnectionFigure[] result = null;
Figure[] figs = GlobalFunction.getObservingFigures(root, ConnectionFigure.class);
int number = 0;
for (int i = 0; i < figs.length; i++) {
ConnectionFigure cf = (ConnectionFigure) figs[i];
if (cf.endFigure() == root && constraintToConnection.isSatisfied(cf) && constraintToStartFigure.isSatisfied(cf.startFigure()))
number++;
}
result = new ConnectionFigure[number];
if (number > 0) {
int index = 0;
for (int i = 0; i < figs.length; i++) {
ConnectionFigure cf = (ConnectionFigure) figs[i];
if (cf.endFigure() == root && constraintToConnection.isSatisfied(cf) && constraintToStartFigure.isSatisfied(cf.startFigure())) {
result[index] = cf;
index++;
}
}
}
return result;
}
public static ConnectionFigure[] getEndConnectedFigures(Figure root) {
ConnectionFigure[] result = null;
Figure[] figs = GlobalFunction.getObservingFigures(root, ConnectionFigure.class);
int number = 0;
for (int i = 0; i < figs.length; i++) {
ConnectionFigure cf = (ConnectionFigure) figs[i];
if (cf.endFigure() == root)
number++;
}
result = new ConnectionFigure[number];
if (number > 0) {
int index = 0;
for (int i = 0; i < figs.length; i++) {
ConnectionFigure cf = (ConnectionFigure) figs[i];
if (cf.endFigure() == root) {
result[index] = cf;
index++;
}
}
}
return result;
}
public static ConnectionFigure[] getStartConnectedFigures(Figure root, Satisfy constraintToConnection) {
ConnectionFigure[] result = null;
Figure[] figs = GlobalFunction.getObservingFigures(root, ConnectionFigure.class);
int number = 0;
for (int i = 0; i < figs.length; i++) {
ConnectionFigure cf = (ConnectionFigure) figs[i];
if ((constraintToConnection.isSatisfied(cf)) && (cf.startFigure() == root))
number++;
}
result = new ConnectionFigure[number];
if (number > 0) {
int index = 0;
for (int i = 0; i < figs.length; i++) {
ConnectionFigure cf = (ConnectionFigure) figs[i];
if ((constraintToConnection.isSatisfied(cf)) && (cf.startFigure() == root)) {
result[index] = cf;
index++;
}
}
}
return result;
}
public static ConnectionFigure[] getStartConnectedFigures(Figure root) {
ConnectionFigure[] result = null;
Figure[] figs = GlobalFunction.getObservingFigures(root, ConnectionFigure.class);
int number = 0;
for (int i = 0; i < figs.length; i++) {
ConnectionFigure cf = (ConnectionFigure) figs[i];
if (cf.startFigure() == root)
number++;
}
result = new ConnectionFigure[number];
if (number > 0) {
int index = 0;
for (int i = 0; i < figs.length; i++) {
ConnectionFigure cf = (ConnectionFigure) figs[i];
if (cf.startFigure() == root) {
result[index] = cf;
index++;
}
}
}
return result;
}
public static ConnectionFigure[] getConnectedFigures(Figure root) {
ConnectionFigure[] result = null;
Figure[] figs = GlobalFunction.getObservingFigures(root, ConnectionFigure.class);
int number = 0;
for (int i = 0; i < figs.length; i++) {
ConnectionFigure cf = (ConnectionFigure) figs[i];
if ((cf.startFigure() == root) || (cf.endFigure() == root))
number++;
}
result = new ConnectionFigure[number];
if (number > 0) {
int index = 0;
for (int i = 0; i < figs.length; i++) {
ConnectionFigure cf = (ConnectionFigure) figs[i];
if ((cf.startFigure() == root) || (cf.endFigure() == root)) {
result[index] = cf;
index++;
}
}
}
return result;
}
public static boolean hasParentFigures(Figure root) {
return hasParentFigures(root, ConnectionFigure.class);
/**
if (root instanceof ConnectionFigure) {
root = ((ConnectionFigure) root).endFigure();
}
Figure[] figs = GlobalFunction.getObservingFigures(root, ConnectionFigure.class);
for (int i = 0; i < figs.length; i++) {
ConnectionFigure cf = (ConnectionFigure) figs[i];
if (cf.endFigure() == root)
return true;
}
return false;
**/
}
public static boolean hasParentFigures(Figure root, Class connectionType) {
if (root instanceof ConnectionFigure) {
root = ((ConnectionFigure) root).endFigure();
}
Figure[] figs = GlobalFunction.getObservingFigures(root, connectionType);
for (int i = 0; i < figs.length; i++) {
ConnectionFigure cf = (ConnectionFigure) figs[i];
if (cf.endFigure() == root)
return true;
}
return false;
}
public static boolean hasOffspringFigures(Figure root) {
if (root instanceof ConnectionFigure) {
root = ((ConnectionFigure) root).startFigure();
}
Figure[] figs = GlobalFunction.getObservingFigures(root, ConnectionFigure.class);
for (int i = 0; i < figs.length; i++) {
ConnectionFigure cf = (ConnectionFigure) figs[i];
if (cf.startFigure() == root)
return true;
}
return false;
}
public static Figure[] getObservingFigures(Figure figure, Class figureType) {
Figure[] result = null;
if (figure.listener() == null) {
result = new Figure[0];
return result;
}
if (!FigureChangeEventMulticaster.class.isInstance(figure.listener())) {
if (figureType.isInstance(figure.listener())) {
result = new Figure[1];
result[0] = (Figure) (figure.listener());
return result;
} else {
result = new Figure[0];
return result;
}
}
FigureChangeEventMulticaster fcem = (FigureChangeEventMulticaster) figure.listener();
EventListener[] eventL = AWTEventMulticaster.getListeners(fcem, FigureChangeListener.class);
int number = 0;
for (int i = 0; i < eventL.length; i++) {
if (figureType.isInstance(eventL[i])) number++;
}
result = new Figure[number];
if (number > 0) {
int index = 0;
for (int i = 0; i < eventL.length; i++) {
if (figureType.isInstance(eventL[i])) {
result[index] = (Figure) eventL[i];
index++;
}
}
}
return result;
}
public static void setCursor(int x, int y, DrawingView view) {
Handle handle = view.findHandle(x, y);
double scale = view.getScale();
int realX = (int) (x / scale + 0.5);
int realY = (int) (y / scale + 0.5);
Figure figure = view.getDrawing().findFigure(realX, realY);
if (handle != null) {
if (LocatorHandle.class.isInstance(handle)) {
LocatorHandle lh = (LocatorHandle) handle;
Locator loc = lh.getLocator();
if (RelativeLocator.class.isInstance(loc)) {
RelativeLocator rl = (RelativeLocator) loc;
if (rl.equals(RelativeLocator.north())) {
view.setCursor(new Cursor(Cursor.N_RESIZE_CURSOR));
} else if (rl.equals(RelativeLocator.northEast())) {
view.setCursor(new Cursor(Cursor.NE_RESIZE_CURSOR));
} else if (rl.equals(RelativeLocator.east())) {
view.setCursor(new Cursor(Cursor.E_RESIZE_CURSOR));
} else if (rl.equals(RelativeLocator.southEast())) {
view.setCursor(new Cursor(Cursor.SE_RESIZE_CURSOR));
} else if (rl.equals(RelativeLocator.south())) {
view.setCursor(new Cursor(Cursor.S_RESIZE_CURSOR));
} else if (rl.equals(RelativeLocator.southWest())) {
view.setCursor(new Cursor(Cursor.SW_RESIZE_CURSOR));
} else if (rl.equals(RelativeLocator.west())) {
view.setCursor(new Cursor(Cursor.W_RESIZE_CURSOR));
} else if (rl.equals(RelativeLocator.northWest())) {
view.setCursor(new Cursor(Cursor.NW_RESIZE_CURSOR));
}
}
} else {
view.setCursor(new Cursor(Cursor.MOVE_CURSOR));
}
} else if (figure != null) {
view.setCursor(new Cursor(Cursor.MOVE_CURSOR));
} else {
view.setCursor(Cursor.getDefaultCursor());
}
}
}