001: /* ***** BEGIN LICENSE BLOCK *****
002: * Version: MPL 1.1
003: * The contents of this file are subject to the Mozilla Public License Version
004: * 1.1 (the "License"); you may not use this file except in compliance with
005: * the License. You may obtain a copy of the License at
006: * http://www.mozilla.org/MPL/
007: *
008: * Software distributed under the License is distributed on an "AS IS" basis,
009: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
010: * for the specific language governing rights and limitations under the
011: * License.
012: *
013: * The Original Code is Riot.
014: *
015: * The Initial Developer of the Original Code is
016: * Neteye GmbH.
017: * Portions created by the Initial Developer are Copyright (C) 2006
018: * the Initial Developer. All Rights Reserved.
019: *
020: * Contributor(s):
021: * Felix Gnass [fgnass at neteye dot de]
022: * Alf Werder [alf dot werder at artundweise dot de]
023: *
024: * ***** END LICENSE BLOCK ***** */
025: package org.riotfamily.riot.editor;
026:
027: import org.riotfamily.common.i18n.MessageResolver;
028: import org.riotfamily.riot.editor.ui.EditorReference;
029: import org.springframework.util.Assert;
030:
031: public class TreeDefinition extends ListDefinition implements TreeList {
032:
033: protected static final String TYPE_TREE = "tree";
034:
035: private FormDefinition formDefinition;
036:
037: private ListDefinition nodeListDefinition;
038:
039: public TreeDefinition(EditorRepository editorRepository) {
040: super (editorRepository);
041: }
042:
043: private boolean isNode(Object bean) {
044: if (bean == null) {
045: return false;
046: }
047: if (!nodeListDefinition.getBeanClass().isInstance(bean)) {
048: return false;
049: }
050:
051: EditorDefinition parentDef = getParentEditorDefinition();
052:
053: if (parentDef != null
054: && !(parentDef instanceof GroupDefinition)) {
055: Class parentClass = parentDef.getBeanClass();
056: if (parentClass.isInstance(bean)) {
057: return false;
058: }
059: }
060: return true;
061: }
062:
063: public ListDefinition getNodeListDefinition() {
064: return this .nodeListDefinition;
065: }
066:
067: public void setDisplayDefinition(final EditorDefinition def) {
068: Assert.isInstanceOf(FormDefinition.class, def);
069: super .setDisplayDefinition(def);
070:
071: formDefinition = (FormDefinition) def;
072: nodeListDefinition = new NodeListDefinition();
073:
074: getEditorRepository().addEditorDefinition(nodeListDefinition);
075: formDefinition.getChildEditorDefinitions().add(0,
076: nodeListDefinition);
077:
078: FormDefinition nodeForm = formDefinition.copy("node-");
079: nodeForm.setParentEditorDefinition(nodeListDefinition);
080: nodeListDefinition.setDisplayDefinition(nodeForm);
081: nodeListDefinition.setParentEditorDefinition(nodeForm);
082: }
083:
084: public EditorReference createEditorPath(Object bean,
085: MessageResolver messageResolver) {
086:
087: if (isNode(bean)) {
088: return nodeListDefinition.createEditorPath(bean,
089: messageResolver);
090: }
091:
092: return super .createEditorPath(bean, messageResolver);
093: }
094:
095: public class NodeListDefinition extends ListDefinition implements
096: TreeList {
097:
098: NodeListDefinition() {
099: super (TreeDefinition.this , TreeDefinition.this
100: .getEditorRepository());
101: setCondition(TreeDefinition.this .getCondition());
102: }
103:
104: public String getId() {
105: return "node-" + super .getId();
106: }
107:
108: public TreeDefinition getTreeDefinition() {
109: return TreeDefinition.this ;
110: }
111:
112: public ListDefinition getNodeListDefinition() {
113: return this ;
114: }
115:
116: private EditorReference stripList(EditorReference path) {
117: EditorReference parent = path.getParent();
118: parent.setEditorUrl(path.getEditorUrl());
119: parent.setEditorType("node");
120: return parent;
121: }
122:
123: public EditorReference createEditorPath(String objectId,
124: String parentId, MessageResolver messageResolver) {
125:
126: return stripList(super .createEditorPath(objectId, parentId,
127: messageResolver));
128: }
129:
130: public EditorReference createEditorPath(Object bean,
131: MessageResolver messageResolver) {
132:
133: if (isNode(bean)) {
134: return stripList(super.createEditorPath(bean,
135: messageResolver));
136: } else {
137: return TreeDefinition.this.createEditorPath(bean,
138: messageResolver);
139: }
140: }
141:
142: }
143:
144: }
|