KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > riotfamily > riot > editor > TreeDefinition


1 /* ***** BEGIN LICENSE BLOCK *****
2  * Version: MPL 1.1
3  * The contents of this file are subject to the Mozilla Public License Version
4  * 1.1 (the "License"); you may not use this file except in compliance with
5  * the License. You may obtain a copy of the License at
6  * http://www.mozilla.org/MPL/
7  *
8  * Software distributed under the License is distributed on an "AS IS" basis,
9  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
10  * for the specific language governing rights and limitations under the
11  * License.
12  *
13  * The Original Code is Riot.
14  *
15  * The Initial Developer of the Original Code is
16  * Neteye GmbH.
17  * Portions created by the Initial Developer are Copyright (C) 2006
18  * the Initial Developer. All Rights Reserved.
19  *
20  * Contributor(s):
21  * Felix Gnass [fgnass at neteye dot de]
22  * Alf Werder [alf dot werder at artundweise dot de]
23  *
24  * ***** END LICENSE BLOCK ***** */

25 package org.riotfamily.riot.editor;
26
27 import java.util.List JavaDoc;
28
29 import org.riotfamily.common.i18n.MessageResolver;
30 import org.riotfamily.riot.editor.ui.EditorReference;
31 import org.springframework.util.Assert;
32
33 public class TreeDefinition extends ListDefinition {
34     
35     protected static final String JavaDoc TYPE_TREE = "tree";
36     
37     private Class JavaDoc branchClass;
38     
39     private FormDefinition formDefinition;
40     
41     private ListDefinition nodeListDefinition;
42     
43     
44     public TreeDefinition(EditorRepository editorRepository) {
45         super(editorRepository);
46     }
47     
48     private boolean isNode(Object JavaDoc bean) {
49         if (bean == null) {
50             return false;
51         }
52         if (!nodeListDefinition.getBeanClass().isInstance(bean)) {
53             return false;
54         }
55         
56         EditorDefinition parentDef = getParentEditorDefinition();
57         
58         if (parentDef != null && !(parentDef instanceof GroupDefinition)) {
59             Class JavaDoc parentClass = parentDef.getBeanClass();
60             if (parentClass.isInstance(bean)) {
61                 return false;
62             }
63         }
64         return true;
65     }
66
67     
68     public void setBranchClass(Class JavaDoc nodeClass) {
69         this.branchClass = nodeClass;
70     }
71
72     public ListDefinition getNodeListDefinition() {
73         return this.nodeListDefinition;
74     }
75
76     public void setDisplayDefinition(final EditorDefinition def) {
77         Assert.isInstanceOf(FormDefinition.class, def);
78         super.setDisplayDefinition(def);
79         
80         formDefinition = (FormDefinition) def;
81         nodeListDefinition = new NodeListDefinition();
82         
83         getEditorRepository().addEditorDefinition(nodeListDefinition);
84         formDefinition.getChildEditorDefinitions().add(0, nodeListDefinition);
85         
86         FormDefinition nodeForm = formDefinition.copy("node-");
87         nodeForm.setParentEditorDefinition(nodeListDefinition);
88         nodeListDefinition.setDisplayDefinition(nodeForm);
89         nodeListDefinition.setParentEditorDefinition(nodeForm);
90     }
91     
92     public EditorReference createEditorPath(Object JavaDoc bean,
93         MessageResolver messageResolver) {
94         
95         if (isNode(bean)) {
96             return nodeListDefinition.createEditorPath(bean, messageResolver);
97         }
98         
99         return super.createEditorPath(bean, messageResolver);
100     }
101
102     private class NodeListDefinition extends ListDefinition {
103
104         NodeListDefinition() {
105             super(TreeDefinition.this, TreeDefinition.this.getEditorRepository());
106         }
107         
108         public String JavaDoc getId() {
109             return "node-" + super.getId();
110         }
111
112         private EditorReference stripListIfTreeIsHomogeneous(
113                 EditorReference path) {
114             
115             if (formDefinition.getChildEditorDefinitions().size() == 1) {
116                 EditorReference parent = path.getParent();
117                 parent.setEditorUrl(path.getEditorUrl());
118                 parent.setEditorType("node");
119                 return parent;
120             }
121             return path;
122         }
123         
124         public EditorReference createEditorPath(String JavaDoc objectId,
125                 String JavaDoc parentId, MessageResolver messageResolver) {
126             
127             return stripListIfTreeIsHomogeneous(super.createEditorPath(
128                     objectId, parentId, messageResolver));
129         }
130         
131         public EditorReference createEditorPath(Object JavaDoc bean,
132                 MessageResolver messageResolver) {
133                     
134             if (isNode(bean)) {
135                 return stripListIfTreeIsHomogeneous(super.createEditorPath(
136                         bean, messageResolver));
137             }
138             else {
139                 return TreeDefinition.this.createEditorPath(
140                         bean, messageResolver);
141             }
142         }
143         
144         public void addReference(List JavaDoc refs, EditorDefinition parentDef,
145                 Object JavaDoc parent, MessageResolver messageResolver) {
146             
147             if (branchClass == null || branchClass.isInstance(parent)) {
148                 super.addReference(refs, parentDef, parent, messageResolver);
149             }
150         }
151     }
152
153 }
154
Popular Tags