001: package org.zilonis.tool.ui.component;
002:
003: import java.awt.Component;
004: import java.awt.Container;
005: import java.awt.Dimension;
006: import java.awt.Graphics;
007: import java.awt.LayoutManager;
008: import java.awt.Rectangle;
009: import java.util.HashMap;
010: import java.util.LinkedList;
011:
012: import javax.swing.JPanel;
013:
014: import org.zilonis.network.Condition;
015: import org.zilonis.network.alpha.AlphaMemory;
016: import org.zilonis.tool.ui.RulesGraphPanel;
017:
018: import com.sun.org.apache.bcel.internal.generic.GETSTATIC;
019:
020: public class NetworkLayout implements LayoutManager {
021:
022: private HashMap<String, LinkedList<Component>> map = new HashMap<String, LinkedList<Component>>();
023:
024: private int conditionLength;
025:
026: @Override
027: public void addLayoutComponent(String label, Component c) {
028: LinkedList<Component> list = map.get(label);
029: if (list == null) {
030: conditionLength++;
031: list = new LinkedList<Component>();
032: map.put(label, list);
033: }
034: list.add(c);
035: }
036:
037: @Override
038: public void removeLayoutComponent(Component c) {
039: }
040:
041: @Override
042: public void layoutContainer(Container target) {
043: int y = 10;
044: boolean lastNegative = false;
045: for (int i = 0; i < conditionLength; ++i) {
046: LinkedList<Component> list = map.get("" + i);
047: Rectangle r = layout(y, list, lastNegative);
048: lastNegative = getConditionType(list) == Condition.NEGATIVE_CONDITION;
049: y += r.getHeight();
050: }
051: }
052:
053: public Rectangle layout(int y, LinkedList<Component> list,
054: boolean lastNegative) {
055: int conditionType = getConditionType(list);
056: switch (conditionType) {
057: case Condition.POSITIVE_CONDITION:
058: case Condition.BOOLEAN_EXPRESSION:
059: Rectangle result = layoutPositiveCondition(10, y, list,
060: lastNegative);
061: lastNegative = false;
062: return result;
063: case Condition.NEGATIVE_CONDITION:
064: lastNegative = true;
065: return layoutNegativeCondition(10, y, list);
066: case Condition.CONJUNCTIVE_NEGATION_CONDITION:
067: return layoutConyuntiveNegation(10, y, list);
068: }
069: return new Rectangle(0, 0, 0, 0);
070:
071: }
072:
073: public Rectangle layoutConyuntiveNegation(int x, int y,
074: LinkedList<Component> list) {
075: RulesGraphPanel p = getRulesGraphPanel(list);
076: HArrowComponent hArrow = getHArrow(list);
077: VArrowComponent vArrow = getVArrow(list);
078: Dimension d = p.getPreferredSize();
079: Rectangle rect = new Rectangle(x + 50, y, x + 50
080: + (int) d.getWidth(), y + (int) d.getHeight());
081: p.setBounds(rect);
082: hArrow.setBounds(x + 51, y + 45, 17, 3);
083: vArrow.setBounds(x + 49, y, 7, (int) d.getHeight());
084: hArrow = getHArrow2(list);
085: NCCNodeComponent node1 = getNCCNodeUI(list);
086: NCCNodeComponent node2 = getNCCNodeUI2(list);
087: Dimension nccD = node1.getPreferredSize();
088: node1.setBounds(x + 30, y + (int) d.getHeight(), (int) nccD
089: .getWidth(), (int) nccD.getHeight());
090: node2.setBounds(x + 30 + (int) nccD.getWidth() + 15, y
091: + (int) d.getHeight(), (int) nccD.getWidth(),
092: (int) nccD.getHeight());
093: hArrow.setBounds(x + 30 + (int) nccD.getWidth(), y
094: + (int) d.getHeight() + 17, 15, 3);
095: vArrow = getVArrow2(list);
096: vArrow.setBounds(x + 30 + (int) nccD.getWidth() + 15 + 23, y
097: + (int) d.getHeight() - 8, 7, 8);
098: return new Rectangle(x, y, x + 100 + (int) d.getWidth(),
099: (int) nccD.getHeight() + (int) d.getHeight());
100: }
101:
102: public RulesGraphPanel getRulesGraphPanel(LinkedList<Component> list) {
103: for (Component c : list)
104: if (c instanceof RulesGraphPanel)
105: return (RulesGraphPanel) c;
106: return null;
107: }
108:
109: public int getConditionType(LinkedList<Component> list) {
110: for (Component c : list) {
111: if (c instanceof JoinNodeComponent)
112: return Condition.POSITIVE_CONDITION;
113: if (c instanceof ExpressionNodeComponent)
114: return Condition.BOOLEAN_EXPRESSION;
115: if (c instanceof NegativeNodeComponent)
116: return Condition.NEGATIVE_CONDITION;
117: if (c instanceof JPanel)
118: return Condition.CONJUNCTIVE_NEGATION_CONDITION;
119: }
120: return -1;
121: }
122:
123: /**
124: * Layout of the positive condition components in the list starting on x,y
125: *
126: * @param x
127: * @param y
128: * @param list
129: * @return
130: */
131: public Rectangle layoutPositiveCondition(int x, int y,
132: LinkedList<Component> list, boolean lastNegative) {
133: int height = 0;
134: BetaMemoryComponent betaMemory = getBetaMemory(list);
135: AlphaMemoryComponent alphaMemoryUI = getAlphaMemory(list);
136: AlphaMemory alphaMemoryModel = alphaMemoryUI
137: .getAlphaMemoryModel();
138: JoinNodeComponent joinNode = getJoinNode(list);
139: HArrowComponent hArrow = getHArrow(list);
140: VArrowComponent vArrow = getVArrow(list);
141: VArrowComponent vArrow2 = getVArrow2(list);
142: height += alphaMemoryUI.getPreferredSize().getHeight();
143: height += 10; // varrow;
144: int betaWidth = (int) betaMemory.getPreferredSize().getWidth();
145: int betaHeight = 0;
146: if (!lastNegative) {
147: betaHeight = (int) betaMemory.getPreferredSize()
148: .getHeight();
149: betaMemory.setBounds(x, y + 10, betaWidth, (int) betaMemory
150: .getPreferredSize().getHeight());
151: vArrow.setBounds(59, y + 45, 15, 10);
152: }
153: height += betaHeight;
154: int joinWidth = (int) joinNode.getPreferredSize().getWidth();
155: int xBound = (int) (x + (betaWidth - joinWidth) / 2);
156: joinNode.setBounds(xBound, y + betaHeight + 20, (int) joinNode
157: .getPreferredSize().getWidth(), (int) joinNode
158: .getPreferredSize().getHeight());
159: xBound = (int) (x + betaMemory.getPreferredSize().getWidth() + hArrow
160: .getPreferredSize().getWidth());
161: int alphaHeight = (int) alphaMemoryUI.getPreferredSize()
162: .getHeight();
163: alphaMemoryUI.setBounds(xBound, y + betaHeight + 20,
164: (int) alphaMemoryUI.getPreferredSize().getWidth(),
165: alphaHeight);
166: hArrow.setBounds(94 - 35,
167: y + betaHeight + 20 + alphaHeight / 2,
168: xBound - 94 + 35, 3);
169: vArrow2.setBounds(59, y + height + 10, 15, 10);
170: return new Rectangle(x, y, xBound
171: + (int) alphaMemoryUI.getPreferredSize().getWidth(),
172: height + 10);
173: }
174:
175: public Rectangle layoutNegativeCondition(int x, int y,
176: LinkedList<Component> list) {
177: AlphaMemoryComponent alphaMemory = getAlphaMemory(list);
178: NegativeNodeComponent negativeNode = getNegativeNode(list);
179: HArrowComponent hArrow = getHArrow(list);
180: VArrowComponent vArrow = getVArrow(list);
181: int negativeNodeWidth = (int) negativeNode.getPreferredSize()
182: .getWidth();
183: int negativeNodeHeight = (int) negativeNode.getPreferredSize()
184: .getHeight();
185: int xBound = (x + (105 - negativeNodeWidth) / 2);
186: negativeNode.setBounds(xBound, y + 10, negativeNodeWidth,
187: negativeNodeHeight);
188: int alphaHeight = (int) alphaMemory.getPreferredSize()
189: .getHeight();
190: hArrow.setBounds(xBound + negativeNodeWidth / 2, y + 10
191: + alphaHeight / 2, 200, 3);
192: xBound = (int) (x + 105 + hArrow.getPreferredSize().getWidth());
193: alphaMemory.setBounds(xBound, y + 10, (int) alphaMemory
194: .getPreferredSize().getWidth(), alphaHeight);
195: vArrow.setBounds(59, y + negativeNodeHeight + 10, 15, 10);
196: int width = xBound
197: + (int) alphaMemory.getPreferredSize().getWidth();
198: return new Rectangle(x, y, width, negativeNodeHeight + 15);
199: }
200:
201: /*
202: * public void test() { for (BetaMemUI betaMemUI : betaMemList) { betaMemUI
203: * .setBounds(x, y, (int) betaMemUI.getPreferredSize() .getWidth(), (int)
204: * betaMemUI.getPreferredSize() .getHeight()); y += v_gap * 2 + 70; } y = 10 +
205: * 35 + v_gap; for (JoinNodeUI joinNodeUI : joinNodeList) { int w = (int)
206: * joinNodeUI.getPreferredSize().getWidth(); joinNodeUI.setBounds(x + (105 -
207: * w) / 2, y, w, (int) joinNodeUI .getPreferredSize().getHeight()); y +=
208: * v_gap * 2 + 70; } y = 10 + 35 + v_gap; for (AlphaMemUI alphaMemUI :
209: * alphaMemList) { alphaMemUI.setBounds(alphaX, y, (int)
210: * alphaMemUI.getPreferredSize() .getWidth(), (int)
211: * alphaMemUI.getPreferredSize() .getHeight()); y += v_gap * 2 + 70; } y =
212: * 45; for (VArrowUI arrow : vArrowList) { arrow.setBounds(59, y, 15, 10); y +=
213: * 45; } y = 10 + 35 + v_gap + 35 / 2; for (HArrowUI arrow : hArrowList) {
214: * arrow.setBounds(94 - 35, y, alphaX - 94 + 35, 3); y += 90; } }
215: */
216: public JoinNodeComponent getJoinNode(LinkedList<Component> list) {
217: for (Component c : list) {
218: if (c instanceof JoinNodeComponent)
219: return (JoinNodeComponent) c;
220: }
221: return null;
222: }
223:
224: public NegativeNodeComponent getNegativeNode(
225: LinkedList<Component> list) {
226: for (Component c : list) {
227: if (c instanceof NegativeNodeComponent)
228: return (NegativeNodeComponent) c;
229: }
230: return null;
231: }
232:
233: public BetaMemoryComponent getBetaMemory(LinkedList<Component> list) {
234: for (Component c : list) {
235: if (c instanceof BetaMemoryComponent)
236: return (BetaMemoryComponent) c;
237: }
238: return null;
239: }
240:
241: public AlphaMemoryComponent getAlphaMemory(
242: LinkedList<Component> list) {
243: for (Component c : list) {
244: if (c instanceof AlphaMemoryComponent)
245: return (AlphaMemoryComponent) c;
246: }
247: return null;
248: }
249:
250: public HArrowComponent getHArrow(LinkedList<Component> list) {
251: for (Component c : list) {
252: if (c instanceof HArrowComponent)
253: return (HArrowComponent) c;
254: }
255: return null;
256: }
257:
258: public HArrowComponent getHArrow2(LinkedList<Component> list) {
259: int count = 0;
260: for (Component c : list) {
261: if ((c instanceof HArrowComponent) && (count++ > 0))
262: return (HArrowComponent) c;
263: }
264: return null;
265: }
266:
267: public NCCNodeComponent getNCCNodeUI(LinkedList<Component> list) {
268: for (Component c : list) {
269: if (c instanceof NCCNodeComponent)
270: return (NCCNodeComponent) c;
271: }
272: return null;
273: }
274:
275: public NCCNodeComponent getNCCNodeUI2(LinkedList<Component> list) {
276: int count = 0;
277: for (Component c : list) {
278: if ((c instanceof NCCNodeComponent) && (count++ > 0))
279: return (NCCNodeComponent) c;
280: }
281: return null;
282: }
283:
284: public VArrowComponent getVArrow(LinkedList<Component> list) {
285: for (Component c : list) {
286: if (c instanceof VArrowComponent)
287: return (VArrowComponent) c;
288: }
289: return null;
290: }
291:
292: public VArrowComponent getVArrow2(LinkedList<Component> list) {
293: int count = 0;
294: for (Component c : list) {
295: if ((c instanceof VArrowComponent) && (count++ > 0))
296: return (VArrowComponent) c;
297: }
298: return null;
299: }
300:
301: @Override
302: public Dimension minimumLayoutSize(Container c) {
303: return preferredLayoutSize(c);
304: }
305:
306: @Override
307: public Dimension preferredLayoutSize(Container target) {
308: int y = 10;
309: int width = 0;
310: boolean lastNegative = false;
311: for (int i = 0; i < conditionLength; ++i) {
312: LinkedList<Component> list = map.get("" + i);
313: Rectangle r = layout(y, list, lastNegative);
314: lastNegative = getConditionType(list) == Condition.NEGATIVE_CONDITION;
315: y += r.getHeight();
316: width = Math.max(width, (int) (r.getX() + r.getWidth()));
317: }
318: return new Dimension(width, y);
319: //return new Dimension(335, 1000); // betaMemList.size() * 95);
320: }
321: }
|