001: // @@
002: // @@
003: /*
004: * Wi.Ser Framework
005: *
006: * Version: 1.8.1, 20-September-2007
007: * Copyright (C) 2005 Dirk von der Weiden <dvdw@imail.de>
008: *
009: * This library is free software; you can redistribute it and/or
010: * modify it under the terms of the GNU Lesser General Public
011: * License as published by the Free Software Foundation; either
012: * version 2 of the License, or (at your option) any later version.
013: *
014: * This library is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
017: * Lesser General Public License for more details.
018: *
019: * You should have received a copy of the GNU Lesser General Public
020: * License along with this library located in LGPL.txt in the
021: * license directory; if not, write to the
022: * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
023: * Boston, MA 02111-1307, USA.
024: *
025: * If this agreement does not cover your requirements, please contact us
026: * via email to get detailed information about the commercial license
027: * or our service offerings!
028: *
029: */
030: // @@
031: package de.ug2t.model.values;
032:
033: import java.util.*;
034:
035: import de.ug2t.kernel.*;
036: import de.ug2t.model.*;
037: import de.ug2t.model.persistence.*;
038: import de.ug2t.unifiedGui.*;
039: import de.ug2t.unifiedGui.transformer.*;
040: import de.ug2t.unifiedGui.validator.*;
041:
042: /**
043: *
044: * @author dvonderweiden
045: *
046: * Created: 06:58:41, 2003 Comment: Kann auch mehrere Kindobjekte mit dem selben
047: * schlüssel speichern, ist vom KeTreeNode abgeleitet ist daher kompatibel zu
048: * allen Ui-elementen
049: * Arbeitet nur lokal!
050: */
051: public class MoValueTree extends KeTreeNode implements
052: IMoValueContainer {
053: protected IUnValidator pdm_validator = null;
054: protected IUnTransformer pdm_transformer = null;
055: protected Vector pdm_observers = null;
056: protected UnComponent pdm_uiMap = null;
057: protected IMoMappingErrorHandler pdm_uiErr = null;
058: protected IMoReader pdm_reader = null;
059: protected IMoWriter pdm_writer = null;
060: protected String pdm_myTemplate = null;
061: protected IMoValue pdm_myTemplateObj = null;
062: private TreeMap pem_desc = null;
063:
064: public MoChildDescriptor pcmf_getDesc(String xName) {
065: if (pem_desc == null || xName == null)
066: return (null);
067:
068: return ((MoChildDescriptor) this .pem_desc.get(xName));
069: }
070:
071: public MoChildDescriptor pcmf_addDesc(String xName,
072: MoChildDescriptor xDesc) {
073: if (this .pem_desc == null)
074: this .pem_desc = new TreeMap();
075:
076: return ((MoChildDescriptor) this .pem_desc.put(xName, xDesc));
077: }
078:
079: /**
080: * @param xName
081: * @param xParent
082: * @throws Exception
083: */
084: public MoValueTree(String xName, KeTreeNode xParent,
085: IUnValidator xVal, IUnTransformer xTr, Vector xObs,
086: UnComponent xUiMap, IMoMappingErrorHandler xUiErr)
087: throws Exception {
088: super (xName, xParent);
089: this .pcmf_setEarlyAutoDelete(true);
090:
091: this .pdm_validator = xVal;
092: this .pdm_transformer = xTr;
093: this .pdm_observers = xObs;
094: this .pdm_uiMap = xUiMap;
095: this .pdm_uiErr = xUiErr;
096:
097: if (xParent == null) {
098: this .pdm_myTemplate = null;
099: this .pdm_myTemplateObj = this ;
100: } else {
101: this .pdm_myTemplate = xParent.pcmf_getName();
102: this .pdm_myTemplateObj = (IMoValueContainer) xParent;
103: }
104:
105: return;
106: }
107:
108: /**
109: * @param xName
110: * @param xParent
111: * @throws Exception
112: */
113: public MoValueTree(String xName, KeTreeNode xParent)
114: throws Exception {
115: super (xName, xParent);
116: this .pcmf_setEarlyAutoDelete(true);
117:
118: if (xParent == null) {
119: this .pdm_myTemplate = null;
120: this .pdm_myTemplateObj = this ;
121: } else {
122: this .pdm_myTemplate = xParent.pcmf_getName();
123: this .pdm_myTemplateObj = (IMoValueContainer) xParent;
124: }
125:
126: return;
127: }
128:
129: /**
130: *
131: * @param xName
132: * @param xParent
133: * @throws Exception
134: */
135: public MoValueTree() throws Exception {
136: super ("", null);
137: this .pcmf_setEarlyAutoDelete(true);
138:
139: this .pdm_myTemplate = null;
140: this .pdm_myTemplateObj = this ;
141:
142: return;
143: }
144:
145: /**
146: * <p>
147: * ...
148: * </p>
149: * <p>
150: *
151: * </p>
152: */
153: protected void pdmf_callObservers() {
154: if (this .pdm_observers != null) {
155: Iterator l_it = this .pdm_observers.iterator();
156: while (l_it.hasNext())
157: ((IMoObserver) l_it.next()).pcmf_modelChanged(this );
158: }
159: }
160:
161: /**
162: * <p>
163: * Does...
164: * </p>
165: * <p>
166: *
167: * @return a Type with
168: * </p>
169: * <p>
170: * @param
171: * </p>
172: */
173: public IUnValidator pcmf_setValidator(IUnValidator xValidator) {
174: IUnValidator l_val = this .pdm_validator;
175: this .pdm_validator = xValidator;
176:
177: return (l_val);
178: }
179:
180: /**
181: * <p>
182: * Does...
183: * </p>
184: * <p>
185: *
186: * @return a Type with
187: * </p>
188: * <p>
189: * @param
190: * </p>
191: */
192: public UnComponent pcmf_setUiMap(UnComponent xMap) {
193: UnComponent l_ui = this .pdm_uiMap;
194: this .pdm_uiMap = xMap;
195:
196: return (l_ui);
197: }
198:
199: /**
200: * <p>
201: * Does...
202: * </p>
203: * <p>
204: *
205: * @return a Type with
206: * </p>
207: * <p>
208: * @param
209: * </p>
210: */
211: public IMoObserver pcmf_addObserver(IMoObserver xObserv) {
212: if (this .pdm_observers == null)
213: this .pdm_observers = new Vector();
214:
215: this .pdm_observers.add(xObserv);
216:
217: return (xObserv);
218: }
219:
220: /**
221: * <p>
222: * Does...
223: * </p>
224: * <p>
225: *
226: * @return a Type with
227: * </p>
228: * <p>
229: * @param
230: * </p>
231: */
232: public IMoObserver pcmf_removeObserver(IMoObserver xObs) {
233: if (this .pdm_observers == null)
234: return (null);
235:
236: return (this .pdm_observers.remove(xObs) ? xObs : null);
237: }
238:
239: /**
240: * <p>
241: * Does...
242: * </p>
243: * <p>
244: *
245: * @return a Type with
246: * </p>
247: * <p>
248: * @param
249: * </p>
250: */
251: public IMoMappingErrorHandler pcmf_setUiErrorGW(
252: IMoMappingErrorHandler xGw) {
253: IMoMappingErrorHandler l_err = this .pdm_uiErr;
254: this .pdm_uiErr = xGw;
255:
256: return (l_err);
257: }
258:
259: /**
260: * <p>
261: * Does...
262: * </p>
263: * <p>
264: *
265: * @return a Type with
266: * </p>
267: * <p>
268: * @param
269: * </p>
270: */
271: public IMoValue pcmf_addValue(String xName, IMoValue xValue) {
272: KeTreeNode l_node = null;
273:
274: if (this .pdm_transformer != null)
275: if (xValue instanceof IMoSingleValue) {
276: Object l_obj = null;
277: l_obj = this .pdm_transformer
278: .pcmf_transform(((IMoSingleValue) xValue)
279: .pcmf_getValue());
280: ((IMoSingleValue) xValue).pcmf_setValue(l_obj);
281: ((IMoSingleValue) xValue).pcmf_setParent(this );
282: }
283:
284: try {
285: if (xValue instanceof KeTreeNode)
286: l_node = (KeTreeNode) xValue;
287: else {
288: if (xValue instanceof IMoSingleValue)
289: ((IMoSingleValue) xValue).pcmf_setParent(this );
290:
291: l_node = new KeTreeNode(xName, null);
292: l_node.pcmf_setEarlyAutoDelete(true);
293: l_node.pcmf_setValue(xValue);
294: }
295: } catch (Exception e) {
296: KeLog.pcmf_logException("ug2t", this , e);
297: }
298: ;
299: l_node = this .pcmf_addNode(xName, l_node);
300: this .pdmf_callObservers();
301:
302: if (l_node == null)
303: return (null);
304: else
305: return (l_node instanceof KeTreeNode ? (IMoValue) l_node
306: : (IMoValue) l_node.pcmf_getValue());
307: }
308:
309: /**
310: * <p>
311: * Does...
312: * </p>
313: * <p>
314: *
315: * @return a Type with
316: * </p>
317: * <p>
318: * @param
319: * </p>
320: */
321: public IMoValue pcmf_getModelValue(String xName) {
322: KeTreeNode l_node = null;
323: l_node = (KeTreeNode) this .pcmf_getSubNode(xName);
324: if (l_node == null)
325: return (null);
326: if (l_node instanceof IMoValue)
327: return ((IMoValue) l_node);
328: else
329: return ((IMoValue) l_node.pcmf_getValue());
330: }
331:
332: /**
333: * <p>
334: * Does...
335: * </p>
336: * <p>
337: *
338: * @return a Type with
339: * </p>
340: * <p>
341: * @param
342: * </p>
343: */
344: public IMoValue pcmf_removeValue(String xName) {
345: KeTreeNode l_node = null;
346: l_node = (KeTreeNode) this .pcmf_removeNode(xName);
347: IMoValue l_ret = null;
348:
349: if (l_node instanceof IMoValue)
350: l_ret = (IMoValue) l_node;
351: else
352: l_ret = (IMoValue) l_node.pcmf_getValue();
353:
354: if (l_ret instanceof IMoSingleValue)
355: ((IMoSingleValue) l_ret).pcmf_setParent(null);
356:
357: this .pdmf_callObservers();
358:
359: return (l_ret);
360: }
361:
362: /**
363: * <p>
364: * Does...
365: * </p>
366: * <p>
367: *
368: * @return a Type with
369: * </p>
370: * <p>
371: * @param
372: * </p>
373: */
374:
375: public IMoValue pcmf_cloneModelValue(boolean xChilds, boolean xFull) {
376: IMoValueContainer l_new = null;
377: String l_name = null;
378:
379: try {
380: l_new = new MoValueTree(this .pcmf_getName(), null,
381: this .pdm_validator, this .pdm_transformer,
382: this .pdm_observers, this .pdm_uiMap, this .pdm_uiErr);
383: l_new.pcmf_setMyTemplate(this .pcmf_getMyTemplate());
384: l_new.pcmf_setMyTemplateObj(this .pcmf_getMyTemplateObj());
385: ((MoValueTree) l_new).pcmf_setView(IKeViewable.ViewTools
386: .pcmf_cloneViewToLocal(this , this .pcmf_getView()));
387:
388: if (xChilds == true && xFull == false) {
389: Iterator l_itk = this .pcmf_getSubNameIterator();
390: Iterator l_itv = this .pcmf_getSubIterator();
391: IMoValue l_val = null;
392: Object l_sub = null;
393:
394: while (l_itk.hasNext()) {
395: l_name = (String) l_itk.next();
396: l_sub = l_itv.next();
397: MoChildDescriptor l_desc = this
398: .pcmf_getDesc(l_name);
399: if (l_desc != null) {
400: if (l_desc.pcmf_isManatory()) {
401: if (l_sub instanceof MoValueTree)
402: l_val = (IMoValue) l_sub;
403: else
404: l_val = (IMoValue) ((KeTreeNode) l_sub)
405: .pcmf_getValue();
406:
407: l_new.pcmf_addValue(l_name, l_val
408: .pcmf_cloneModelValue(xChilds,
409: xFull));
410: }
411: }
412: }
413: } else if (xChilds == true && xFull == true) {
414: Iterator l_itk = this .pcmf_getSubNameIterator();
415: Iterator l_itv = this .pcmf_getSubIterator();
416: IMoValue l_val = null;
417: Object l_sub = null;
418:
419: while (l_itk.hasNext()) {
420: l_name = (String) l_itk.next();
421: l_sub = l_itv.next();
422:
423: if (l_sub instanceof MoValueTree)
424: l_val = (IMoValue) l_sub;
425: else
426: l_val = (IMoValue) ((KeTreeNode) l_sub)
427: .pcmf_getValue();
428:
429: l_new.pcmf_addValue(l_name, l_val
430: .pcmf_cloneModelValue(xChilds, xFull));
431: }
432: }
433: } catch (Exception e) {
434: KeLog.pcmf_logException("ug2t", this , e);
435: }
436: ;
437:
438: l_new.pcmf_setReader(this .pdm_reader);
439: l_new.pcmf_setWriter(this .pdm_writer);
440:
441: return (l_new);
442: }
443:
444: public IMoValue pcmf_cloneModelValue(boolean xChilds) {
445: return (pcmf_cloneModelValue(xChilds, true));
446: }
447:
448: /**
449: * <p>
450: * Does ...
451: * </p>
452: * <p>
453: *
454: *
455: * @return a boolean with ...
456: * </p>
457: */
458: public boolean pcmf_model2UI() {
459: boolean l_ret = true;
460: Iterator l_it = this .pcmf_getSubValueIt();
461: MoChildDescriptor l_desc = null;
462: String l_name = null;
463: boolean l_doMap = true;
464: IMoValueContainer l_tpl = (IMoValueContainer) this
465: .pcmf_getMyTemplateObj();
466:
467: if (l_tpl == null)
468: l_tpl = this ;
469:
470: while (l_it.hasNext()) {
471: IMoValue l_val = (IMoValue) l_it.next();
472: l_name = l_val.pcmf_getMyTemplate();
473: l_desc = l_tpl.pcmf_getDesc(l_name);
474: if (l_desc != null)
475: l_doMap = l_desc.isPem_mapWithParent2Ui();
476:
477: if (l_doMap)
478: l_ret = l_val.pcmf_model2UI();
479: else
480: l_doMap = true;
481: }
482: return (l_ret);
483: } // end pcmf_model2UI
484:
485: /**
486: * <p>
487: * Does ...
488: * </p>
489: * <p>
490: *
491: *
492: * @return a boolean with ...
493: * </p>
494: */
495: public boolean pcmf_UI2Model() {
496: boolean l_ret = true;
497: Iterator l_it = this .pcmf_getSubValueIt();
498: MoChildDescriptor l_desc = null;
499: String l_name = null;
500: boolean l_doMap = true;
501: IMoValueContainer l_tpl = (IMoValueContainer) this
502: .pcmf_getMyTemplateObj();
503:
504: if (l_tpl == null)
505: l_tpl = this ;
506:
507: while (l_it.hasNext()) {
508: IMoValue l_val = (IMoValue) l_it.next();
509: l_name = l_val.pcmf_getMyTemplate();
510: l_desc = l_tpl.pcmf_getDesc(l_name);
511: if (l_desc != null)
512: l_doMap = l_desc.isPem_mapWithParent2Ui();
513:
514: if (l_doMap)
515: l_ret = l_val.pcmf_UI2Model() ? l_ret : false;
516: else
517: l_doMap = true;
518: }
519: return (l_ret);
520: } // end pcmf_UI2Model
521:
522: /**
523: * <p>
524: * Does...
525: * </p>
526: * <p>
527: *
528: * @return a Type with
529: * </p>
530: * <p>
531: * @param
532: * </p>
533: */
534:
535: public boolean pcmf_validate() {
536: boolean l_ret = true;
537: Iterator l_it = this .pcmf_getSubValueIt();
538: Vector l_error = new Vector();
539:
540: while (l_it.hasNext()) {
541: IMoValue l_val = (IMoValue) l_it.next();
542: if (l_val.pcmf_validate() == false) {
543: l_ret = false;
544: l_error.add(l_val.pcmf_getUiMap());
545: }
546: }
547: ;
548: if (l_ret == true && this .pdm_validator != null) {
549: l_ret = this .pdm_validator.pcmf_validate(this ) == null ? true
550: : false;
551: if (l_ret == false && this .pdm_uiMap != null)
552: l_error.add(this .pdm_uiMap);
553: }
554: ;
555: if (l_ret == false && this .pdm_uiErr != null)
556: this .pdm_uiErr.pcmf_setUiElemets2Error(l_error);
557:
558: if (l_ret == true)
559: this .pdmf_callObservers();
560:
561: return (l_ret);
562: }
563:
564: /**
565: * <p>
566: * Does...
567: * </p>
568: * <p>
569: *
570: * @return a Type with
571: * </p>
572: * <p>
573: * @param
574: * </p>
575: */
576: public boolean pcmf_read(IMoValue xTpl) {
577: return (this .pdm_reader.pcmf_tplRead(xTpl, this ));
578: }
579:
580: /**
581: * <p>
582: * Does...
583: * </p>
584: * <p>
585: *
586: * @return a Type with
587: * </p>
588: * <p>
589: * @param
590: * </p>
591: */
592: public boolean pcmf_write() {
593: return (this .pdm_writer.pcmf_writeModel(this ));
594: }
595:
596: /**
597: * <p>
598: * Does...
599: * </p>
600: * <p>
601: *
602: * @return a Type with
603: * </p>
604: * <p>
605: * @param
606: * </p>
607: */
608: public IMoReader pcmf_setReader(IMoReader xReader) {
609: IMoReader l_pr = this .pdm_reader;
610: this .pdm_reader = xReader;
611:
612: return (l_pr);
613: }
614:
615: /**
616: * <p>
617: * Does...
618: * </p>
619: * <p>
620: *
621: * @return a Type with
622: * </p>
623: * <p>
624: * @param
625: * </p>
626: */
627: public IMoWriter pcmf_setWriter(IMoWriter xWriter) {
628: IMoWriter l_pw = this .pdm_writer;
629: this .pdm_writer = xWriter;
630:
631: return (l_pw);
632: }
633:
634: /**
635: * <p>
636: * Does...
637: * </p>
638: * <p>
639: *
640: * @return a Type with
641: * </p>
642: * <p>
643: * @param
644: * </p>
645: */
646: public Iterator pcmf_getSubValueNameIt() {
647: return (this .pcmf_getSubNameIterator());
648: }
649:
650: /**
651: * <p>
652: * Does ...
653: * </p>
654: * <p>
655: *
656: * @param ...
657: * </p>
658: * <p>
659: * @return ...
660: * </p>
661: */
662: public Iterator pcmf_getSubValueIt() {
663: if (this .pdm_allSubs == null)
664: return (new ArrayList().iterator());
665:
666: Iterator l_it = this .pdm_allSubs.iterator();
667: ArrayList l_vect = new ArrayList(this .pdm_allSubs.size());
668: Object l_value = null;
669:
670: while (l_it.hasNext()) {
671: l_value = l_it.next();
672: if (l_value instanceof MoValueTree)
673: l_vect.add(l_value);
674: else
675: l_vect.add(((KeTreeNode) l_value).pcmf_getValue());
676: }
677:
678: return (l_vect.iterator());
679: }
680:
681: /**
682: * <p>
683: * Does ...
684: * </p>
685: * <p>
686: *
687: *
688: *
689: * @return a IUnTransformer with ...
690: * </p>
691: * <p>
692: * @param xTr
693: * ...
694: * </p>
695: */
696: public IUnTransformer pcmf_getTransformer() {
697: return (this .pdm_transformer);
698: }
699:
700: /**
701: * <p>
702: * Does ...
703: * </p>
704: * <p>
705: *
706: *
707: *
708: * @return a IUnTransformer with ...
709: * </p>
710: * <p>
711: * @param xTr
712: * ...
713: * </p>
714: */
715: public IUnTransformer pcmf_setTransformer(IUnTransformer xTrans) {
716: IUnTransformer l_trans = this .pdm_transformer;
717: this .pdm_transformer = xTrans;
718: return (l_trans);
719: }
720:
721: /**
722: * <p>
723: * Does ...
724: * </p>
725: * <p>
726: *
727: *
728: *
729: * @return a Type with ...
730: * </p>
731: * <p>
732: * @param ...
733: * </p>
734: */
735: public IMoMappingErrorHandler pcmf_getUiErrorGW() {
736: return (this .pdm_uiErr);
737: }
738:
739: /**
740: * <p>
741: * Does ...
742: * </p>
743: * <p>
744: *
745: *
746: *
747: * @return a UnComponent with ...
748: * </p>
749: * <p>
750: * @param xMap
751: * ...
752: * </p>
753: */
754: public UnComponent pcmf_getUiMap() {
755: return (this .pdm_uiMap);
756: }
757:
758: /**
759: * <p>
760: * Does...
761: * </p>
762: * <p>
763: *
764: * @return a Type with
765: * </p>
766: * <p>
767: * @param
768: * </p>
769: */
770: public IMoReader pcmf_getReader() {
771: return (this .pdm_reader);
772: }
773:
774: /**
775: * <p>
776: * Does ...
777: * </p>
778: * <p>
779: *
780: *
781: *
782: * @return a UnComponent with ...
783: * </p>
784: * <p>
785: * @param xMap
786: * ...
787: * </p>
788: */
789: public Vector pcmf_getObservers() {
790: return (this .pdm_observers);
791: }
792:
793: /**
794: * <p>
795: * Does...
796: * </p>
797: * <p>
798: *
799: * @return a Type with
800: * </p>
801: * <p>
802: * @param
803: * </p>
804: */
805: public IMoWriter pcmf_getWriter() {
806: return (this .pdm_writer);
807: }
808:
809: /**
810: * <p>
811: * Does...
812: * </p>
813: * <p>
814: *
815: * @return a Type with
816: * </p>
817: * <p>
818: * @param
819: * </p>
820: */
821: public String pcmf_getMyTemplate() {
822: return (this .pdm_myTemplate);
823: }
824:
825: /**
826: * <p>
827: * Does...
828: * </p>
829: * <p>
830: *
831: * @return a Type with
832: * </p>
833: * <p>
834: * @param
835: * </p>
836: */
837: public String pcmf_setMyTemplate(String xTpl) {
838: String l_old = this .pdm_myTemplate;
839: this .pdm_myTemplate = xTpl;
840:
841: return (l_old);
842: };
843:
844: /**
845: * <p>
846: * Does ...
847: * </p>
848: * <p>
849: *
850: * @param ...
851: * </p>
852: * <p>
853: * @return ...
854: * </p>
855: */
856: public IMoValue pcmf_getNewModelValue(String xName, boolean xChilds) {
857: IMoValue l_mod = null;
858:
859: try {
860: IMoValue l_val = this .pcmf_getModelValue(xName);
861: if (l_val == null) {
862: KeLog.pcmf_log("ug2t",
863: "this model-value-type is not allowed: "
864: + xName, this , KeLog.ERROR);
865: return (null);
866: }
867: l_mod = l_val.pcmf_cloneModelValue(xChilds, false);
868: l_mod.pcmf_setMyTemplate(xName);
869: l_mod.pcmf_setMyTemplateObj(l_val);
870: } catch (Exception e) {
871: KeLog.pcmf_logException("ug2t", this , e);
872: }
873: ;
874:
875: return (l_mod);
876: }
877:
878: public IMoValue pcmf_getMyTemplateObj() {
879: return (this .pdm_myTemplateObj);
880: }
881:
882: public IMoValue pcmf_setMyTemplateObj(IMoValue xTpl) {
883: IMoValue l_old = this .pdm_myTemplateObj;
884: this .pdm_myTemplateObj = xTpl;
885:
886: return (l_old);
887: }
888:
889: public IMoValueContainer pcmf_getValueContainer(String xName) {
890: return ((IMoValueContainer) this .pcmf_getModelValue(xName));
891: }
892:
893: public IMoSingleValue pcmf_getSingleValue(String xName) {
894: return ((IMoSingleValue) this .pcmf_getModelValue(xName));
895: }
896:
897: public IMoValueContainer pcmf_getParent() {
898: return ((IMoValueContainer) this.pcmf_getParentNode());
899: }
900: }
|