001: package org.enhydra.jawe.base.panel.panels;
002:
003: import java.awt.Component;
004: import java.util.Arrays;
005: import java.util.List;
006:
007: import javax.swing.Box;
008:
009: import org.enhydra.jawe.base.panel.PanelContainer;
010: import org.enhydra.jawe.base.panel.SpecialChoiceElement;
011: import org.enhydra.shark.xpdl.XMLComplexElement;
012: import org.enhydra.shark.xpdl.XMLElement;
013:
014: /**
015: * Creates titled group panel with vertical or horizontal BoxLayout, that consists of
016: * panels of given elements.
017: *
018: * @author Sasa Bojanic
019: * @author Zoran Milakovic
020: */
021: public class XMLGroupPanel extends XMLBasicPanel {
022:
023: protected boolean hasDummyStartEl = false;
024:
025: protected boolean hasDummyEndEl = false;
026:
027: public XMLGroupPanel(PanelContainer pc, XMLElement myOwnerL,
028: Object[] elements, String title, boolean isVertical,
029: boolean hasBorder, boolean hasEmptyBorder) {
030: this (pc, myOwnerL, Arrays.asList(elements), title, isVertical,
031: hasBorder, hasEmptyBorder);
032: }
033:
034: public XMLGroupPanel(PanelContainer pc, XMLElement myOwnerL,
035: List elements, String title, boolean isVertical,
036: boolean hasBorder, boolean hasEmptyBorder) {
037:
038: super (pc, myOwnerL, title, isVertical, hasBorder,
039: hasEmptyBorder);
040:
041: initPanel(isVertical, elements);
042: }
043:
044: protected void initPanel(boolean isVertical, List elements) {
045: boolean rightAllignment = true;
046:
047: if (pc != null) {
048: rightAllignment = pc.getSettings().getSettingBoolean(
049: "XMLBasicPanel.RightAllignment");
050: }
051:
052: initTopLeft(isVertical, rightAllignment);
053:
054: initElements(elements, isVertical);
055:
056: initBottomRight(isVertical, rightAllignment);
057: }
058:
059: protected void initElements(List elements, boolean isVertical) {
060: for (int i = 0; i < elements.size(); i++) {
061: XMLPanel dtdp = null;
062: Object el = elements.get(i);
063: if (el instanceof XMLElement) {
064: dtdp = pc.getPanelGenerator().getPanel((XMLElement) el);
065: } else if (el instanceof String) {
066: dtdp = pc.getPanelGenerator().getPanel(
067: ((XMLComplexElement) myOwner).get((String) el));
068: } else if (el instanceof XMLPanel) {
069: dtdp = (XMLPanel) el;
070: }
071: // dtdp.setEnabled(!myOwner.isReadOnly());
072: if (dtdp != null) {
073: add(dtdp);
074: } else if (el instanceof Component) {
075: add((Component) el);
076: }
077: }
078: }
079:
080: protected void initTopLeft(boolean isVertical,
081: boolean rightAllignment) {
082: if (isVertical) {
083: add(Box.createVerticalStrut(5));
084: hasDummyStartEl = true;
085: } else {
086: if (!rightAllignment) {
087: add(Box.createHorizontalGlue());
088: hasDummyStartEl = true;
089: }
090: }
091: }
092:
093: protected void initBottomRight(boolean isVertical,
094: boolean rightAllignment) {
095: if (isVertical) {
096: add(Box.createVerticalGlue());
097: hasDummyEndEl = true;
098: } else {
099: if (rightAllignment) {
100: add(Box.createHorizontalGlue());
101: hasDummyEndEl = true;
102: }
103: }
104: }
105:
106: public XMLPanel getPanel(int no) {
107: int gps = getComponentCount() - ((hasDummyStartEl) ? 1 : 0)
108: - ((hasDummyEndEl) ? 1 : 0);
109: if (no >= gps || no < 0) {
110: throw new RuntimeException(
111: "There's no element at position " + no);
112: }
113: if (hasDummyStartEl) {
114: no++;
115: }
116: return (XMLPanel) getComponent(no);
117: }
118:
119: public void addToGroup(Object newEl) {
120: XMLPanel newPanel = null;
121: if (newEl instanceof XMLElement) {
122: newPanel = pc.getPanelGenerator().getPanel(
123: (XMLElement) newEl);
124: } else if (newEl instanceof String) {
125: newPanel = pc.getPanelGenerator().getPanel(
126: ((XMLComplexElement) myOwner).get((String) newEl));
127: } else if (newEl instanceof XMLPanel) {
128: newPanel = (XMLPanel) newEl;
129: } else {
130: throw new RuntimeException("Can't add element " + newEl
131: + " to the group!");
132: }
133: if (!hasDummyEndEl) {
134: add(newPanel);
135: } else {
136: int cc = getComponentCount();
137: add(newPanel, cc - 1);
138: }
139: }
140:
141: public void addToGroup(Object newEl, int where) {
142: int gps = getComponentCount() - ((hasDummyStartEl) ? 1 : 0)
143: - ((hasDummyEndEl) ? 1 : 0);
144: if (where >= gps || where < 0) {
145: throw new RuntimeException(
146: "Invalid position "
147: + where
148: + " for adding element into the XMLGroupPanel with size "
149: + gps);
150:
151: }
152: XMLPanel newPanel = null;
153: if (newEl instanceof XMLElement) {
154: newPanel = pc.getPanelGenerator().getPanel(
155: (XMLElement) newEl);
156: } else if (newEl instanceof String) {
157: newPanel = pc.getPanelGenerator().getPanel(
158: ((XMLComplexElement) myOwner).get((String) newEl));
159: } else if (newEl instanceof XMLPanel) {
160: newPanel = (XMLPanel) newEl;
161: } else {
162: throw new RuntimeException("Can't add element " + newEl
163: + " to the group!");
164: }
165: if (hasDummyStartEl) {
166: where++;
167: }
168: add(newPanel, where);
169: }
170:
171: public void removeFromGroup(int no) {
172: int gps = getComponentCount() - ((hasDummyStartEl) ? 1 : 0)
173: - ((hasDummyEndEl) ? 1 : 0);
174: if (no >= gps) {
175: throw new RuntimeException(
176: "There's no element at position " + no);
177: }
178: if (hasDummyStartEl) {
179: no++;
180: }
181: remove(no);
182: }
183:
184: public XMLPanel getPanelForElement(XMLElement el) {
185: for (int i = 0; i < getComponentCount(); i++) {
186: Component c = getComponent(i);
187: if (c instanceof XMLPanel) {
188: XMLPanel p = (XMLPanel) c;
189: if (p instanceof XMLGroupPanel) {
190: p = ((XMLGroupPanel) p).getPanelForElement(el);
191: if (p != null) {
192: return p;
193: }
194: } else if (p.getOwner() == el) {
195: return p;
196: } else if (p.getOwner() instanceof SpecialChoiceElement) {
197: SpecialChoiceElement sc = (SpecialChoiceElement) p
198: .getOwner();
199: if (sc.getControlledElement() == el) {
200: return p;
201: }
202: }
203: }
204: }
205: return null;
206: }
207:
208: public int getPanelPositionForElement(XMLElement el) {
209: for (int i = 0; i < getComponentCount(); i++) {
210: Component c = getComponent(i);
211: if (c instanceof XMLPanel) {
212: XMLPanel p = (XMLPanel) c;
213: if (p instanceof XMLGroupPanel) {
214: p = ((XMLGroupPanel) p).getPanelForElement(el);
215: if (p != null) {
216: return i;
217: }
218: } else if (p.getOwner() == el) {
219: return i;
220: } else if (p.getOwner() instanceof SpecialChoiceElement) {
221: SpecialChoiceElement sc = (SpecialChoiceElement) p
222: .getOwner();
223: if (sc.getControlledElement() == el) {
224: return i;
225: }
226: }
227: }
228: }
229: return -1;
230: }
231:
232: public boolean validateEntry() {
233: if (isEmpty() && !getOwner().isRequired())
234: return true;
235: boolean isOK = true;
236: for (int i = 0; i < getComponentCount(); i++) {
237: Component c = getComponent(i);
238: if (c instanceof XMLPanel) {
239: isOK = isOK && ((XMLPanel) c).validateEntry();
240: }
241: }
242: return isOK;
243: }
244:
245: public boolean isEmpty() {
246: boolean isEmpty = true;
247: for (int i = 0; i < getComponentCount(); i++) {
248: Component c = getComponent(i);
249: if (c instanceof XMLPanel) {
250: isEmpty = isEmpty && ((XMLPanel) c).isEmpty();
251: }
252: }
253: return isEmpty;
254: }
255:
256: public void setOwner(XMLElement el) {
257: super .setOwner(el);
258: this .myOwner = el;
259: for (int i = 0; i < getComponentCount(); i++) {
260: Component c = getComponent(i);
261: if (c instanceof XMLPanel) {
262: ((XMLPanel) c).setOwner(el);
263: }
264: }
265: }
266:
267: public void setElements() {
268: if (!getOwner().isReadOnly()) {
269: for (int i = 0; i < getComponentCount(); i++) {
270: Component c = getComponent(i);
271: if (c instanceof XMLPanel) {
272: // System.err.println("Comp no "+(i+1)+" =
273: // "+getComponent(i).getClass().getName());
274: ((XMLPanel) c).setElements();
275: }
276: }
277: }
278: }
279:
280: public void updateView() {
281: for (int i = 0; i < getComponentCount(); i++) {
282: Component c = getComponent(i);
283: if (c instanceof XMLPanel) {
284: ((XMLPanel) c).updateView();
285: }
286: }
287: }
288:
289: public void cleanup() {
290: for (int i = 0; i < getComponentCount(); i++) {
291: Component c = getComponent(i);
292: if (c instanceof XMLPanel) {
293: ((XMLPanel) c).cleanup();
294: }
295: }
296: }
297:
298: }
|