Author: mdryakhlenkov
Date: 2007-07-05 07:59:21 -0400 (Thu, 05 Jul 2007)
New Revision: 2306
Added:
trunk/hibernatetools/plugins/org.jboss.tools.hibernate.ui.veditor/src/org/jboss/tools/hibernate/ui/veditor/editors/autolayout/
trunk/hibernatetools/plugins/org.jboss.tools.hibernate.ui.veditor/src/org/jboss/tools/hibernate/ui/veditor/editors/autolayout/impl/
trunk/hibernatetools/plugins/org.jboss.tools.hibernate.ui.veditor/src/org/jboss/tools/hibernate/ui/veditor/editors/autolayout/impl/AutoLayoutImpl.java
trunk/hibernatetools/plugins/org.jboss.tools.hibernate.ui.veditor/src/org/jboss/tools/hibernate/ui/veditor/editors/autolayout/impl/Group.java
trunk/hibernatetools/plugins/org.jboss.tools.hibernate.ui.veditor/src/org/jboss/tools/hibernate/ui/veditor/editors/autolayout/impl/GroupArranger.java
trunk/hibernatetools/plugins/org.jboss.tools.hibernate.ui.veditor/src/org/jboss/tools/hibernate/ui/veditor/editors/autolayout/impl/Groups.java
trunk/hibernatetools/plugins/org.jboss.tools.hibernate.ui.veditor/src/org/jboss/tools/hibernate/ui/veditor/editors/autolayout/impl/Item.java
trunk/hibernatetools/plugins/org.jboss.tools.hibernate.ui.veditor/src/org/jboss/tools/hibernate/ui/veditor/editors/autolayout/impl/Items.java
trunk/hibernatetools/plugins/org.jboss.tools.hibernate.ui.veditor/src/org/jboss/tools/hibernate/ui/veditor/editors/autolayout/impl/LayuotConstants.java
trunk/hibernatetools/plugins/org.jboss.tools.hibernate.ui.veditor/src/org/jboss/tools/hibernate/ui/veditor/editors/autolayout/impl/TransitionArranger.java
Log:
JBIDE-559: Hibernate diagram editor cleanup
Added:
trunk/hibernatetools/plugins/org.jboss.tools.hibernate.ui.veditor/src/org/jboss/tools/hibernate/ui/veditor/editors/autolayout/impl/AutoLayoutImpl.java
===================================================================
---
trunk/hibernatetools/plugins/org.jboss.tools.hibernate.ui.veditor/src/org/jboss/tools/hibernate/ui/veditor/editors/autolayout/impl/AutoLayoutImpl.java
(rev 0)
+++
trunk/hibernatetools/plugins/org.jboss.tools.hibernate.ui.veditor/src/org/jboss/tools/hibernate/ui/veditor/editors/autolayout/impl/AutoLayoutImpl.java 2007-07-05
11:59:21 UTC (rev 2306)
@@ -0,0 +1,81 @@
+/*******************************************************************************
+ * Copyright (c) 2007 Red Hat, Inc.
+ * Distributed under license by Red Hat, Inc. All rights reserved.
+ * This program is made available under the terms of the
+ * Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at
http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributor:
+ * Red Hat, Inc. - initial API and implementation
+ ******************************************************************************/
+package org.jboss.tools.hibernate.ui.veditor.editors.autolayout.impl;
+
+import org.jboss.tools.hibernate.ui.veditor.editors.autolayout.IItemInfo;
+import org.jboss.tools.hibernate.ui.veditor.editors.autolayout.ILinkInfo;
+import org.jboss.tools.hibernate.ui.veditor.editors.autolayout.IDiagramInfo;
+
+
+public class AutoLayoutImpl {
+ LayuotConstants constants = new LayuotConstants();
+ protected Items items;
+
+ public AutoLayoutImpl() {}
+
+ public void setGridStep(String gridStep) {
+ constants.update(gridStep);
+ }
+
+ public void setItems(Items items) {
+ this.items = items;
+ items.setConstants(constants);
+ }
+
+ public void setOverride(boolean b) {
+ items.setOverride(b);
+ }
+
+ public void setProcess(IDiagramInfo process) {
+// constants.update();
+ items.setProcess(process);
+ apply();
+ if(items.override) {
+ TransitionArranger a = new TransitionArranger();
+ a.setItems(items.items);
+ a.execute();
+ }
+ }
+
+ private void apply() {
+ resetTransitions(); // temporal
+
+ Item[] is = items.items;
+ int[] yDeltas = items.groups.yDeltas;
+ for (int i = 0; i < is.length; i++) {
+ if(is[i].isSet()) continue;
+ IItemInfo o = is[i].itemInfo;
+ int x = is[i].ix * constants.deltaX + constants.indentX;
+ int y = is[i].iy * constants.deltaY + constants.indentY;
+ if(is[i].ix % 2 == 1) y += 16;
+ x += is[i].group.xDeltas[is[i].ix] * constants.incX;
+ y += yDeltas[is[i].iy] * constants.incY + is[i].yIndent;
+ o.setShape(new int[]{x, y, 0, 0});
+ }
+ }
+
+ private void resetTransitions() {
+ if(!items.override) return;
+ Item[] is = items.items;
+ for (int i = 0; i < is.length; i++) {
+ IItemInfo o = is[i].itemInfo;
+ if(o instanceof ILinkInfo) {
+ ((ILinkInfo)o).setLinkShape(new int[0]);
+ }
+ ILinkInfo[] os = items.getOutput(o);
+ for (int j = 0; j < os.length; j++) {
+ os[j].setLinkShape(new int[0]);
+ }
+ }
+
+ }
+
+}
Added:
trunk/hibernatetools/plugins/org.jboss.tools.hibernate.ui.veditor/src/org/jboss/tools/hibernate/ui/veditor/editors/autolayout/impl/Group.java
===================================================================
---
trunk/hibernatetools/plugins/org.jboss.tools.hibernate.ui.veditor/src/org/jboss/tools/hibernate/ui/veditor/editors/autolayout/impl/Group.java
(rev 0)
+++
trunk/hibernatetools/plugins/org.jboss.tools.hibernate.ui.veditor/src/org/jboss/tools/hibernate/ui/veditor/editors/autolayout/impl/Group.java 2007-07-05
11:59:21 UTC (rev 2306)
@@ -0,0 +1,237 @@
+/*******************************************************************************
+ * Copyright (c) 2007 Red Hat, Inc.
+ * Distributed under license by Red Hat, Inc. All rights reserved.
+ * This program is made available under the terms of the
+ * Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at
http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributor:
+ * Red Hat, Inc. - initial API and implementation
+ ******************************************************************************/
+package org.jboss.tools.hibernate.ui.veditor.editors.autolayout.impl;
+
+import java.util.*;
+
+public class Group {
+ LayuotConstants constants;
+ int number;
+ List itemList = new ArrayList();
+ Item[] allitems = null;
+ private int[] items = null;
+ int miny = -1;
+ int[] xDeltas = null;
+ GroupArranger arranger = new GroupArranger(this);
+
+ public Group() {}
+
+ public void setItems(Item[] items) {
+ allitems = items;
+ }
+
+ public void setConstants(LayuotConstants constants) {
+ this.constants = constants;
+ }
+
+ public void expandGroup(int _item) {
+ Item item = allitems[_item];
+ item.group = this;
+ itemList.add(new Integer(_item));
+ int[] is = item.comments;
+ for (int i = 0; i < is.length; i++) {
+ Item item2 = allitems[is[i]];
+ if(!item2.isSet()) {
+ allitems[is[i]].ix = item.ix;
+ }
+ expandGroup(is[i]);
+ }
+ is = item.inputs;
+ for (int i = 0; i < is.length; i++) {
+ Item item2 = allitems[is[i]];
+ if(item2.group != null) continue;
+ if(!item2.isSet()) {
+ item2.ix = item.ix - 1;
+ }
+ expandGroup(is[i]);
+ }
+ is = item.outputs;
+ for (int i = 0; i < is.length; i++) {
+ Item item2 = allitems[is[i]];
+ if(item2.group != null) continue;
+ if(!item2.isSet()) item2.ix = item.ix + 1;
+ expandGroup(is[i]);
+ }
+ }
+
+ int[] items() {
+ if(items == null) {
+ items = new int[itemList.size()];
+ for (int i = 0; i < items.length; i++)
+ items[i] = ((Integer)itemList.get(i)).intValue();
+ }
+ return items;
+ }
+
+ Item getItem(int i) {
+ return allitems[items[i]];
+ }
+
+ public void moveX() {
+ items();
+ int min = 0;
+ for (int i = 0; i < items.length; i++) {
+ if(getItem(i).ix < min) min = getItem(i).ix;
+ }
+ if(min == 0) return;
+ for (int i = 0; i < items.length; i++) {
+ if(!getItem(i).isSet()) {
+ getItem(i).ix -= min;
+ if(getItem(i).ix >= Groups.FX) getItem(i).ix = Groups.FX - 1;
+ }
+ }
+ }
+
+ public void buildY(Item item, int[][] field) {
+ field[item.ix][item.iy] = 1;
+ int[] is = item.comments;
+ for (int i = 0; i < is.length; i++) {
+ Item item2 = allitems[is[i]];
+ if(item2.yAssigned) continue;
+ item2.yAssigned = true;
+ if(!item2.isSet()) item2.iy = findFreeY(item2.ix, miny, item.iy, Groups.FY, field);
+ buildY(item2, field);
+ }
+ is = item.inputs;
+ for (int i = 0; i < is.length; i++) {
+ Item item2 = allitems[is[i]];
+ if(item2.yAssigned) continue;
+ item2.yAssigned = true;
+ if(!item2.isSet()) item2.iy = findFreeY(item2.ix, miny, item.iy, Groups.FY, field);
+ buildY(item2, field);
+ }
+ is = item.outputs;
+ for (int i = 0; i < is.length; i++) {
+ Item item2 = allitems[is[i]];
+ if(item2.yAssigned) continue;
+ item2.yAssigned = true;
+ if(!item2.isSet()) item2.iy = findFreeY(item2.ix, miny, item.iy, Groups.FY, field);
+ buildY(item2, field);
+ }
+ }
+
+ public void buildY_2(Item item, int[][] field) {
+ field[item.ix][item.iy] = 1;
+ int[] is = item.comments;
+ for (int i = 0; i < is.length; i++) {
+ Item item2 = allitems[is[i]];
+ if(item2.yAssigned) continue;
+ item2.yAssigned = true;
+ if(!item2.isSet()) item2.iy = findFreeY(item2.ix, miny, item.iy, Groups.FY, field);
+ buildY(item2, field);
+ }
+ is = item.inputs;
+ for (int i = 0; i < is.length; i++) {
+ Item item2 = allitems[is[i]];
+ if(item2.yAssigned) continue;
+ if(item.ix != item2.ix + 1) continue;
+ item2.yAssigned = true;
+ if(!item2.isSet()) item2.iy = findFreeY(item2.ix, miny, item.iy, Groups.FY, field);
+ buildY(item2, field);
+ }
+ is = item.outputs;
+ for (int i = 0; i < is.length; i++) {
+ Item item2 = allitems[is[i]];
+ if(item2.yAssigned) continue;
+ if(item.ix != item2.ix - 1) continue;
+ item2.yAssigned = true;
+ if(!item2.isSet()) item2.iy = findFreeY(item2.ix, miny, item.iy, Groups.FY, field);
+ buildY(item2, field);
+ }
+ }
+
+ private int findFreeY(int ix, int miny, int prefy, int maxy, int[][] field) {
+ for (int i = 0; i < 20; i++) {
+ int iy = prefy + i;
+ if(iy >= miny && iy < maxy && field[ix][iy] == 0) return iy;
+ iy = prefy - i;
+ if(iy >= miny && iy < maxy && field[ix][iy] == 0) return iy;
+ }
+ return prefy;
+ }
+
+ public int getMaxY() {
+ int maxy = 0;
+ for (int i = 0; i < items.length; i++)
+ if(getItem(i).iy > maxy) maxy = getItem(i).iy;
+ return maxy;
+ }
+
+ public boolean hasSetItems() {
+ items();
+ for (int i = 0; i < items.length; i++) if(getItem(i).isSet()) return true;
+ return false;
+ }
+
+ public void buildXDeltas() {
+ xDeltas = new int[getMaxX() + 1];
+ for (int i = 0; i < xDeltas.length; i++) xDeltas[i] = 0;
+ ///if(hasSetItems()) return;
+ for (int i = 0; i < items.length; i++) {
+ int c = getItem(i).ix;
+ if(c >= xDeltas.length) continue;
+ int sz = getItem(i).inputs.length - 1;
+ if(sz > xDeltas[c]) xDeltas[c] = sz;
+ ++c;
+ if(c >= xDeltas.length) continue;
+ sz = getItem(i).outputs.length - 1;
+ if(sz > xDeltas[c]) xDeltas[c] = sz;
+ }
+ for (int i = 0; i < xDeltas.length; i++) if(xDeltas[i] > 4) xDeltas[i] = 4;
+
+ for (int i = 0; i < items.length; i++) {
+ int c = getItem(i).ix;
+ ++c;
+ if(c >= xDeltas.length) continue;
+ int[] shape = getItem(i).getObject().getShape();
+ if(shape == null || shape.length < 4) continue;
+ int wi = (shape[2] - (constants.deltaX / 2)) / constants.incX;
+ if(wi > xDeltas[c]) xDeltas[c] = wi;
+ }
+ for (int i = 1; i < xDeltas.length; i++) xDeltas[i] += xDeltas[i - 1];
+
+ }
+
+ public int getMaxX() {
+ int maxx = 0;
+ for (int i = 0; i < items.length; i++) if(getItem(i).ix > maxx) maxx =
getItem(i).ix;
+ return maxx;
+ }
+
+
+ //////////////////////////////////////////
+
+ public void createGroup(int _item) {
+ expandGroup2(_item);
+ int length = items().length;
+ for (int i = 0; i < length; i++) getItem(i).initActivities();
+ arranger.arrange();
+ }
+
+ private void expandGroup2(int _item) {
+ Item item = allitems[_item];
+ item.group = this;
+ itemList.add(new Integer(_item));
+ int[] is = item.comments;
+ for (int i = 0; i < is.length; i++) {
+ expandGroup2(is[i]);
+ }
+ is = item.inputs;
+ for (int i = 0; i < is.length; i++) {
+ if(allitems[is[i]].group == null) expandGroup2(is[i]);
+ }
+ is = item.outputs;
+ for (int i = 0; i < is.length; i++) {
+ if(allitems[is[i]].group == null) expandGroup2(is[i]);
+ }
+ }
+
+}
Added:
trunk/hibernatetools/plugins/org.jboss.tools.hibernate.ui.veditor/src/org/jboss/tools/hibernate/ui/veditor/editors/autolayout/impl/GroupArranger.java
===================================================================
---
trunk/hibernatetools/plugins/org.jboss.tools.hibernate.ui.veditor/src/org/jboss/tools/hibernate/ui/veditor/editors/autolayout/impl/GroupArranger.java
(rev 0)
+++
trunk/hibernatetools/plugins/org.jboss.tools.hibernate.ui.veditor/src/org/jboss/tools/hibernate/ui/veditor/editors/autolayout/impl/GroupArranger.java 2007-07-05
11:59:21 UTC (rev 2306)
@@ -0,0 +1,193 @@
+/*******************************************************************************
+ * Copyright (c) 2007 Red Hat, Inc.
+ * Distributed under license by Red Hat, Inc. All rights reserved.
+ * This program is made available under the terms of the
+ * Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at
http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributor:
+ * Red Hat, Inc. - initial API and implementation
+ ******************************************************************************/
+package org.jboss.tools.hibernate.ui.veditor.editors.autolayout.impl;
+
+public class GroupArranger {
+ Group group;
+ int mgi = 0; // items with indefinite gravity
+ boolean inited = false;
+
+ public GroupArranger(Group group) {
+ this.group = group;
+ }
+
+ private void init() {
+ inited = true;
+ for (int i = 0; i < group.items().length; i++) {
+ Item item = group.getItem(i);
+ if(item.isOwned) {
+ item.gravity = -1;
+ } else if(item.inputs.length == 0) {
+ item.gravity = 0;
+ } else if(item.outputs.length == 0) {
+ item.gravity = 2000;
+ } else {
+ item.gravity = 1000;
+ ++mgi;
+ }
+ }
+ }
+
+ public void arrange() {
+ init();
+ while(mgi > 0) {
+ split();
+ if(mgi > 0) cut(findBestCutIndex());
+ }
+ reduce();
+ }
+
+ private void split() {
+ int length = group.items().length;
+ boolean work = true;
+ while(work) {
+ work = false;
+ for (int i = 0; i < length; i++) {
+ Item item = group.getItem(i);
+ if(item.gravity != 1000) continue;
+ int q = computeLowerWeight(i);
+ if(q < item.gravity) {
+ item.gravity = q;
+ work = true;
+ --mgi;
+ }
+ if(item.gravity != 1000) continue;
+ q = computeUpperWeight(i);
+ if(q > item.gravity) {
+ item.gravity = q;
+ work = true;
+ --mgi;
+ }
+ }
+ }
+ }
+
+ private int computeLowerWeight(int i0) {
+ Item item = group.getItem(i0);
+ int[] is = item.inputs;
+ int q = 0;
+ for (int i = 0; i < is.length; i++) {
+ if(!item.inputActivities[i]) continue;
+ Item si = group.allitems[is[i]];
+ if(si.gravity + 1 > q) q = si.gravity + 1;
+ }
+ return q;
+ }
+
+ private int computeUpperWeight(int i0) {
+ Item item = group.getItem(i0);
+ int[] is = item.outputs;
+ if(is.length == 0) return item.gravity;
+ int q = 2000;
+ for (int i = 0; i < is.length; i++) {
+ if(!item.outputActivities[i]) continue;
+ Item si = group.allitems[is[i]];
+ if(si.gravity - 1 < q) q = si.gravity - 1;
+ }
+ return (q == 2000) ? item.gravity : q;
+ }
+
+
+ void activateInput(Item item, int input, boolean b) {
+ item.inputActivities[input] = b;
+ Item i2 = group.allitems[item.inputs[input]];
+ int[] is = i2.outputs;
+ for (int i = 0; i < is.length; i++)
+ if(is[i] == item.n) i2.outputActivities[i] = b;
+ }
+
+ private int findBestCutIndex() {
+ int k = -1;
+ int w = 20000;
+ for (int i = 0; i < group.items().length; i++) {
+ Item item = group.getItem(i);
+ if(item.gravity != 1000) continue;
+ int w2 = getCuttingPreference(i);
+ if(w2 < w) {
+ w = w2;
+ k = i;
+ }
+ }
+ return k;
+ }
+
+ private int getCuttingPreference(int i0) {
+ Item item = group.getItem(i0);
+ int[] is = item.inputs;
+ int q = 0;
+ for (int i = 0; i < is.length; i++) {
+ if(!item.inputActivities[i]) continue;
+ Item si = group.allitems[is[i]];
+ if(si.gravity == 1000) ++q;
+ }
+ q = 10 * q;
+ is = item.outputs;
+ for (int i = 0; i < is.length; i++) {
+ if(!item.outputActivities[i]) continue;
+ Item si = group.allitems[is[i]];
+ if(si.gravity == 1000) --q;
+ }
+ return q;
+ }
+
+ private void cut(int i0) {
+ Item item = group.getItem(i0);
+ int[] is = item.inputs;
+ for (int i = 0; i < is.length; i++) {
+ if(!item.inputActivities[i]) continue;
+ Item si = group.allitems[is[i]];
+ if(si.gravity == 1000) {
+ activateInput(item, i, false);
+ }
+ }
+ }
+
+ private void reduce() {
+ boolean work = true;
+ while(work) {
+ work = false;
+ for (int i = 0; i < group.items().length; i++) {
+ Item item = group.getItem(i);
+ if(item.gravity < 0) continue;
+ int q = computeLowerWeight(i);
+ if(q < item.gravity) {
+ item.gravity = q;
+ work = true;
+ }
+ }
+ }
+ work = true;
+ while(work) {
+ work = false;
+ for (int i = 0; i < group.items().length; i++) {
+ Item item = group.getItem(i);
+ if(item.gravity < 0) continue;
+ int q = computeUpperWeight(i);
+ if(q > item.gravity) {
+ item.gravity = q;
+ work = true;
+ }
+ }
+ }
+ for (int i = 0; i < group.items().length; i++) {
+ Item item = group.getItem(i);
+ item.ix = item.gravity;
+ }
+ for (int i = 0; i < group.items().length; i++) {
+ Item item = group.getItem(i);
+ int[] cs = item.comments;
+ for (int j = 0; j < cs.length; j++) {
+ group.allitems[cs[j]].ix = item.ix;
+ }
+ }
+ }
+
+}
Added:
trunk/hibernatetools/plugins/org.jboss.tools.hibernate.ui.veditor/src/org/jboss/tools/hibernate/ui/veditor/editors/autolayout/impl/Groups.java
===================================================================
---
trunk/hibernatetools/plugins/org.jboss.tools.hibernate.ui.veditor/src/org/jboss/tools/hibernate/ui/veditor/editors/autolayout/impl/Groups.java
(rev 0)
+++
trunk/hibernatetools/plugins/org.jboss.tools.hibernate.ui.veditor/src/org/jboss/tools/hibernate/ui/veditor/editors/autolayout/impl/Groups.java 2007-07-05
11:59:21 UTC (rev 2306)
@@ -0,0 +1,189 @@
+/*******************************************************************************
+ * Copyright (c) 2007 Red Hat, Inc.
+ * Distributed under license by Red Hat, Inc. All rights reserved.
+ * This program is made available under the terms of the
+ * Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at
http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributor:
+ * Red Hat, Inc. - initial API and implementation
+ ******************************************************************************/
+package org.jboss.tools.hibernate.ui.veditor.editors.autolayout.impl;
+
+import java.util.*;
+
+public class Groups {
+ protected LayuotConstants constants;
+ static int FX = 30, FY = 120;
+ List groups = new ArrayList();
+ Item[] items;
+ int[][] field;
+ int[] yDeltas = null;
+
+ public Groups() {}
+
+ public void setConstants(LayuotConstants constants) {
+ this.constants = constants;
+ }
+
+ public void load(Item[] items) {
+ this.items = items;
+ init();
+ initField();
+ buildY();
+ buildDeltas();
+ }
+
+ public void init() {
+ groups.clear();
+ int g = 0;
+ boolean isSomethingSet = isSomethingSet();
+ for (int i = 0; i < items.length; i++) {
+ if(items[i].group != null || items[i].isOwned) continue;
+ ++g;
+ Group group = new Group();
+ group.setConstants(constants);
+ group.number = g;
+ groups.add(group);
+ if(!items[i].isSet()) items[i].ix = 0;
+ group.setItems(items);
+ if(isSomethingSet) {
+ group.expandGroup(i);
+ } else {
+ group.createGroup(i);
+ }
+ group.moveX();
+ }
+ }
+
+ private boolean isSomethingSet() {
+ for (int i = 0; i < items.length; i++)
+ if(items[i].isSet) return true;
+ return false;
+ }
+
+ public void initField() {
+ field = new int[FX][];
+ for (int i = 0; i < FX; i++) {
+ field[i] = new int[FY];
+ for (int j = 0; j < FY; j++) field[i][j] = 0;
+ }
+ for (int i = 0; i < items.length; i++) {
+ if(items[i].isSet()) field[items[i].ix][items[i].iy] = 1;
+ }
+ }
+
+ static int MAX_SINGLES_PER_ROW = 5;
+ public void buildY() {
+ int miny = 0;
+ miny = buildYForSingleComments();
+ boolean isSomethingSet = isSomethingSet();
+ for (int i = 0; i < groups.size(); i++) {
+ Group group = (Group)groups.get(i);
+ if(group.items().length < 2) continue;
+ Item item = group.getItem(0);
+ if(item.yAssigned) continue;
+ group.miny = miny;
+ item.yAssigned = true;
+ if(!item.isSet()) item.iy = miny;
+ if(!isSomethingSet) group.buildY_2(item, field);
+ group.buildY(item, field);
+ miny = group.getMaxY() + 1;
+ if(miny > FY - 2) miny = FY - 2;
+ }
+ buildYForSingles(miny);
+ }
+
+ private int buildYForSingleComments() {
+ int miny = 0;
+ int ix = 0;
+ for (int i = 0; i < groups.size(); i++) {
+ Group group = (Group)groups.get(i);
+ if(group.items().length != 1) continue;
+ Item item = group.getItem(0);
+ if(!item.isComment()) continue;
+ if(!item.isSet()) {
+ group.miny = miny;
+ item.ix = ix;
+ item.iy = miny;
+ ix += 2;
+ if(ix > 2) {
+ ix = 0;
+ ++miny;
+ }
+ } else group.miny = 0;
+ }
+ if(ix > 0) ++miny;
+ return miny;
+ }
+
+ private void buildYForSingles(int miny) {
+ int ix = 0;
+ for (int i = 0; i < groups.size(); i++) {
+ Group group = (Group)groups.get(i);
+ if(group.items().length != 1 || group.miny >= 0) continue;
+ group.miny = miny;
+ Item item = group.getItem(0);
+ while(true) {
+ while(ix < field.length && field[ix][miny] == 1) ++ix;
+ if(ix > MAX_SINGLES_PER_ROW) {
+ ix = 0;
+ if(miny < FY - 1) ++miny; else break;
+ } else break;
+ }
+ if(!item.isSet()) {
+ item.iy = miny;
+ item.ix = ix;
+ ++ix;
+ }
+ field[item.ix][item.iy] = 1;
+ }
+ }
+
+ void buildXDeltasForSingles() {
+ int[] xDeltas = new int[100];
+ for (int i = 0; i < groups.size(); i++) {
+ Group group = (Group)groups.get(i);
+ if(group.items().length != 1) continue;
+ group.xDeltas = xDeltas;
+ Item item = group.getItem(0);
+ int c = item.ix;
+ c++;
+ if(c >= xDeltas.length) continue;
+ int[] shape = item.getObject().getShape();
+ if(shape == null || shape.length < 4) continue;
+ int wi = (shape[2] - (constants.deltaX / 2)) / constants.incX;
+ if(wi > xDeltas[c]) xDeltas[c] = wi;
+ }
+ for (int i = 1; i < xDeltas.length; i++) xDeltas[i] += xDeltas[i - 1];
+ }
+
+ public void buildDeltas() {
+ for (int i = 0; i < groups.size(); i++) {
+ Group group = (Group)groups.get(i);
+ group.buildXDeltas();
+ }
+ buildYDeltas();
+ buildXDeltasForSingles();
+ }
+
+ public void buildYDeltas() {
+ yDeltas = new int[getMaxY() + 1];
+ for (int i = 0; i < yDeltas.length; i++) yDeltas[i] = 0;
+ for (int i = 0; i < items.length; i++) {
+ int c = items[i].iy + 1;
+ if(c >= yDeltas.length) continue;
+ int sz = items[i].outputs.length - 1;
+ if(sz > yDeltas[c]) yDeltas[c] = sz;
+ }
+ for (int i = 1; i < yDeltas.length; i++) yDeltas[i] += yDeltas[i - 1];
+ }
+
+ public int getMaxY() {
+ int max = 0;
+ for (int i = 0; i < items.length; i++) if(items[i].iy > max) max =
items[i].iy;
+ return max;
+ }
+
+
+}
Added:
trunk/hibernatetools/plugins/org.jboss.tools.hibernate.ui.veditor/src/org/jboss/tools/hibernate/ui/veditor/editors/autolayout/impl/Item.java
===================================================================
---
trunk/hibernatetools/plugins/org.jboss.tools.hibernate.ui.veditor/src/org/jboss/tools/hibernate/ui/veditor/editors/autolayout/impl/Item.java
(rev 0)
+++
trunk/hibernatetools/plugins/org.jboss.tools.hibernate.ui.veditor/src/org/jboss/tools/hibernate/ui/veditor/editors/autolayout/impl/Item.java 2007-07-05
11:59:21 UTC (rev 2306)
@@ -0,0 +1,103 @@
+/*******************************************************************************
+ * Copyright (c) 2007 Red Hat, Inc.
+ * Distributed under license by Red Hat, Inc. All rights reserved.
+ * This program is made available under the terms of the
+ * Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at
http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributor:
+ * Red Hat, Inc. - initial API and implementation
+ ******************************************************************************/
+package org.jboss.tools.hibernate.ui.veditor.editors.autolayout.impl;
+
+import java.util.*;
+
+import org.jboss.tools.hibernate.ui.veditor.editors.autolayout.IItemInfo;
+import org.jboss.tools.hibernate.ui.veditor.editors.autolayout.ILinkInfo;
+
+
+public class Item {
+ protected IItemInfo itemInfo;
+ protected int n;
+ protected int x = 0;
+ protected int y = 0;
+ protected int ix = -1;
+ protected int iy = -1;
+ protected int[] inputs = new int[0];
+ protected int[] outputs = new int[0];
+ protected int[] comments = new int[0];
+ protected ArrayList inputLinks = new ArrayList();
+ protected boolean isOwned = false;
+ protected int weight = 0;
+ protected Group group = null;
+ protected int yIndent = 1; // Page = 2; other = 1;
+
+ protected boolean isSet = false;
+ protected boolean yAssigned = false;
+
+ public Item() {}
+
+ public IItemInfo getObject() {
+ return itemInfo;
+ }
+
+ public boolean isSet() {
+ return isSet;
+ }
+
+ public void addInput(int i, ILinkInfo link) {
+ int[] k = new int[inputs.length + 1];
+ System.arraycopy(inputs, 0, k, 0, inputs.length);
+ k[inputs.length] = i;
+ inputs = k;
+ inputLinks.add(link);
+ }
+
+ public void addOutput(int i) {
+ int[] k = new int[outputs.length + 1];
+ System.arraycopy(outputs, 0, k, 0, outputs.length);
+ k[outputs.length] = i;
+ outputs = k;
+ }
+
+ public void addComment(int i) {
+ int[] k = new int[comments.length + 1];
+ System.arraycopy(comments, 0, k, 0, comments.length);
+ k[comments.length] = i;
+ comments = k;
+ }
+
+ public boolean isSingle() {
+ return inputs.length == 0 && outputs.length == 0;
+ }
+
+ public void print() {
+// StringBuffer sb = new StringBuffer();
+/// sb.append(object.getPathPart() + " " + n + " g = " + group
+ " ix = " + ix + " iy = " + iy);
+/// System.out.println(sb.toString());
+ }
+
+ public boolean isComment() {
+ return itemInfo != null && itemInfo.isComment();
+ }
+
+ int gravity;
+ boolean[] outputActivities;
+ boolean[] inputActivities;
+
+ public void initActivities() {
+ outputActivities = new boolean[outputs.length];
+ for (int i = 0; i < outputActivities.length; i++) outputActivities[i] = outputs[i]
!= n;
+ inputActivities = new boolean[inputs.length];
+ for (int i = 0; i < inputActivities.length; i++) inputActivities[i] = inputs[i] !=
n;
+ }
+
+ public void setWeight(int w) {
+ weight = w;
+ }
+
+ public void setYIndent(int y) {
+ yIndent = y;
+ }
+
+}
Added:
trunk/hibernatetools/plugins/org.jboss.tools.hibernate.ui.veditor/src/org/jboss/tools/hibernate/ui/veditor/editors/autolayout/impl/Items.java
===================================================================
---
trunk/hibernatetools/plugins/org.jboss.tools.hibernate.ui.veditor/src/org/jboss/tools/hibernate/ui/veditor/editors/autolayout/impl/Items.java
(rev 0)
+++
trunk/hibernatetools/plugins/org.jboss.tools.hibernate.ui.veditor/src/org/jboss/tools/hibernate/ui/veditor/editors/autolayout/impl/Items.java 2007-07-05
11:59:21 UTC (rev 2306)
@@ -0,0 +1,121 @@
+/*******************************************************************************
+ * Copyright (c) 2007 Red Hat, Inc.
+ * Distributed under license by Red Hat, Inc. All rights reserved.
+ * This program is made available under the terms of the
+ * Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at
http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributor:
+ * Red Hat, Inc. - initial API and implementation
+ ******************************************************************************/
+package org.jboss.tools.hibernate.ui.veditor.editors.autolayout.impl;
+
+import java.util.*;
+
+import org.jboss.tools.hibernate.ui.veditor.editors.autolayout.IItemInfo;
+import org.jboss.tools.hibernate.ui.veditor.editors.autolayout.ILinkInfo;
+import org.jboss.tools.hibernate.ui.veditor.editors.autolayout.IDiagramInfo;
+
+
+public class Items {
+ protected LayuotConstants constants;
+ protected IDiagramInfo process;
+ protected Item[] items;
+ protected Map paths = new HashMap();
+ protected Groups groups = new Groups();
+ protected boolean override = false;
+
+ public Items() {}
+
+ public void setConstants(LayuotConstants constants) {
+ this.constants = constants;
+ groups.setConstants(constants);
+ }
+
+ public void setOverride(boolean b) {
+ override = b;
+ }
+
+ public void setProcess(IDiagramInfo process) {
+ this.process = process;
+ try { load(); } catch (Exception e) { e.printStackTrace(); }
+ }
+
+ private void load() {
+ initItems();
+ if(isAllSet()) return;
+ buildBinds();
+ groups.load(items);
+ print();
+ }
+
+ private void initItems() {
+ IItemInfo[] is = process.getItems();
+ items = new Item[is.length];
+ for (int i = 0; i < is.length; i++) {
+ Item item = new Item();
+ items[i] = item;
+ paths.put(is[i].getID(), item);
+ item.n = i;
+ item.itemInfo = is[i];
+ int[] shape = is[i].getShape();
+ if(!override && shape != null && shape.length > 1) {
+ item.x = shape[0];
+ item.y = shape[1];
+ if(item.x != 0 && item.y != 0) item.isSet = true;
+ item.ix = (item.x / constants.deltaX);
+ item.iy = (item.y / constants.deltaY);
+ if(item.ix < 0) item.ix = 0;
+ if(item.iy < 0) item.iy = 0;
+ if(item.ix >= Groups.FX) item.ix = Groups.FX - 1;
+ if(item.iy >= Groups.FY) item.iy = Groups.FY - 1;
+ }
+ initItem(item);
+ }
+ }
+
+ // override
+ protected void initItem(Item item) {}
+
+ // override
+
+ public ILinkInfo[] getOutput(IItemInfo itemObject) {
+ return itemObject.getLinks();
+ }
+
+ private boolean isAllSet() {
+ for (int i = 0; i < items.length; i++) if(!items[i].isSet()) return false;
+ return true;
+ }
+
+ private void buildBinds() {
+ for (int i = 0; i < items.length; i++) {
+ ILinkInfo[] ts = (items[i].itemInfo instanceof ILinkInfo)
+ ? new ILinkInfo[]{(ILinkInfo)items[i].itemInfo}
+ : getOutput(items[i].itemInfo);
+ for (int j = 0; j < ts.length; j++) {
+ String target = ts[j].getTargetID();
+ if(target == null || target.length() == 0) continue;
+ Item item2 = (Item)paths.get(target);
+ if(item2 == null) {
+ continue;
+ } if(items[i].isComment()) {
+ item2.addComment(items[i].n);
+ items[i].isOwned = true;
+ } else if(item2.weight < 0) {
+ continue;
+ } else {
+ item2.addInput(items[i].n, ts[j]);
+ items[i].addOutput(item2.n);
+ }
+ }
+ }
+ }
+
+ private void print() {
+ for (int i = 0; i < items.length; i++)
+ items[i].print();
+ }
+
+}
+
Added:
trunk/hibernatetools/plugins/org.jboss.tools.hibernate.ui.veditor/src/org/jboss/tools/hibernate/ui/veditor/editors/autolayout/impl/LayuotConstants.java
===================================================================
---
trunk/hibernatetools/plugins/org.jboss.tools.hibernate.ui.veditor/src/org/jboss/tools/hibernate/ui/veditor/editors/autolayout/impl/LayuotConstants.java
(rev 0)
+++
trunk/hibernatetools/plugins/org.jboss.tools.hibernate.ui.veditor/src/org/jboss/tools/hibernate/ui/veditor/editors/autolayout/impl/LayuotConstants.java 2007-07-05
11:59:21 UTC (rev 2306)
@@ -0,0 +1,66 @@
+/*******************************************************************************
+ * Copyright (c) 2007 Red Hat, Inc.
+ * Distributed under license by Red Hat, Inc. All rights reserved.
+ * This program is made available under the terms of the
+ * Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at
http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributor:
+ * Red Hat, Inc. - initial API and implementation
+ ******************************************************************************/
+package org.jboss.tools.hibernate.ui.veditor.editors.autolayout.impl;
+
+public class LayuotConstants {
+ static int DELTA_X = 200;
+ static int DELTA_Y = 104;
+ static int X_INC = 32;
+ static int Y_INC = 24;
+
+ public int deltaX = DELTA_X;
+ public int deltaY = DELTA_Y;
+ public int incX = X_INC;
+ public int incY = Y_INC;
+ public int indentX = 24;
+ public int indentY = 16;
+
+ public void update(String gridStep) {
+ try {
+ int step = Integer.parseInt(gridStep);
+ indentX = (step < 24) ? 24 : step;
+ indentY = (step < 16) ? 16 : step;
+ if(step == 16) {
+ deltaX = 208;
+ deltaY = 112;
+ incX = 16;
+ incY = 32;
+ indentX = 32;
+ } else if(step == 24) {
+ deltaX = 240;
+ deltaY = 120;
+ incX = 24;
+ incY = 24;
+ } else if(step == 32) {
+ deltaX = 256;
+ deltaY = 128;
+ incX = 32;
+ incY = 32;
+ } else if(step == 40) {
+ deltaX = 240;
+ deltaY = 120;
+ incX = 40;
+ incY = 40;
+ } else {
+ deltaX = DELTA_X;
+ deltaY = DELTA_Y;
+ incX = X_INC;
+ incY = Y_INC;
+ }
+ } catch (Exception e) {
+ deltaX = DELTA_X;
+ deltaY = DELTA_Y;
+ incX = X_INC;
+ incY = Y_INC;
+ }
+ }
+
+}
Added:
trunk/hibernatetools/plugins/org.jboss.tools.hibernate.ui.veditor/src/org/jboss/tools/hibernate/ui/veditor/editors/autolayout/impl/TransitionArranger.java
===================================================================
---
trunk/hibernatetools/plugins/org.jboss.tools.hibernate.ui.veditor/src/org/jboss/tools/hibernate/ui/veditor/editors/autolayout/impl/TransitionArranger.java
(rev 0)
+++
trunk/hibernatetools/plugins/org.jboss.tools.hibernate.ui.veditor/src/org/jboss/tools/hibernate/ui/veditor/editors/autolayout/impl/TransitionArranger.java 2007-07-05
11:59:21 UTC (rev 2306)
@@ -0,0 +1,113 @@
+/*******************************************************************************
+ * Copyright (c) 2007 Red Hat, Inc.
+ * Distributed under license by Red Hat, Inc. All rights reserved.
+ * This program is made available under the terms of the
+ * Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at
http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributor:
+ * Red Hat, Inc. - initial API and implementation
+ ******************************************************************************/
+package org.jboss.tools.hibernate.ui.veditor.editors.autolayout.impl;
+
+import java.util.ArrayList;
+
+import org.jboss.tools.hibernate.ui.veditor.editors.autolayout.ILinkInfo;
+
+
+public class TransitionArranger {
+ Item[] items;
+
+ public void setItems(Item[] items) {
+ this.items = items;
+ }
+
+ public void execute() {
+ int maxX = getMaxX();
+ int maxY = getMaxY();
+ int[][] occ = new int[occ0.length][maxY + 1];
+ Item[] v = new Item[maxY + 1];
+ for (int ix = 0; ix <= maxX; ix++) {
+ clean(occ);
+ fill(v, ix);
+ execute(ix, occ, v);
+ }
+ }
+
+ private void clean(int[][] occ) {
+ for (int i = 0; i < occ.length; i++) for (int j = 0; j < occ[0].length; j++)
occ[i][j] = 0;
+ }
+
+ private void fill(Item[] v, int ix) {
+ for (int i = 0; i < v.length; i++) v[i] = null;
+ for (int i = 0; i < items.length; i++) {
+ if(items[i].inputs.length == 0 || items[i].isOwned) continue;
+ if(items[i].ix == ix) v[items[i].iy] = items[i];
+ }
+ }
+
+ private int[] occ0 = new int[10];
+
+ private void execute(int ix, int[][] occ, Item[] v) {
+ int delta = 0;
+ for (int iy = 0; iy < v.length; iy++) {
+ if(v[iy] == null) continue;
+ for (int i = 0; i < occ0.length; i++) occ0[i] = 0;
+ int[] is = v[iy].inputs;
+ delta = 0;
+ for (int k = 0; k < is.length; k++) {
+ int iy2 = items[is[k]].iy;
+ int miny = Math.min(iy, iy2);
+ int maxy = Math.max(iy, iy2);
+ if(maxy - miny > delta) delta = maxy - miny;
+ for (int m = 0; m < occ0.length; m++) for (int y = miny; y <= maxy; y++)
+ if(occ[m][y] > 0) occ0[m] += occ[m][y];
+ }
+ int tg = findTransitionLine(delta, occ0);
+ for (int k = 0; k < is.length; k++) {
+ int iy2 = items[is[k]].iy;
+ occ[tg][iy2]++;
+ }
+ apply(v[iy], tg);
+ }
+ }
+
+ private int getMaxX() {
+ int ix = 0;
+ for (int i = 0; i < items.length; i++) {
+ if(items[i].ix > ix) ix = items[i].ix;
+ }
+ return ix;
+ }
+
+ private int getMaxY() {
+ int iy = 0;
+ for (int i = 0; i < items.length; i++) {
+ if(items[i].iy > iy) iy = items[i].iy;
+ }
+ return iy;
+ }
+
+ private int findTransitionLine(int pref, int[] occ0) {
+ if(pref >= occ0.length) pref = occ0.length - 1;
+ int h = 1000;
+ int p = -1;
+ for (int i = 0; i < occ0.length; i++) {
+ int h1 = occ0[i] * 3 + Math.abs(i - pref);
+ if(h1 < h) {
+ h = h1;
+ p = i;
+ }
+ }
+ return p;
+ }
+
+ private void apply(Item item, int tg) {
+ ArrayList links = item.inputLinks;
+ for (int k = 0; k < links.size(); k++) {
+ ILinkInfo io = (ILinkInfo)links.get(k);
+ io.setLinkShape(new int[]{-1, 8 * (tg + 2)});
+ }
+ }
+
+}