KickJava   Java API By Example, From Geeks To Geeks.

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


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  *
23  * ***** END LICENSE BLOCK ***** */

24 package org.riotfamily.riot.editor;
25
26 import java.util.List JavaDoc;
27 import java.util.regex.Matcher JavaDoc;
28 import java.util.regex.Pattern JavaDoc;
29
30 import org.riotfamily.common.beans.PropertyUtils;
31 import org.riotfamily.common.i18n.MessageResolver;
32 import org.riotfamily.riot.editor.ui.EditorReference;
33
34 /**
35  * Abstract base class for {@link EditorDefinition EditorDefinitions}.
36  * @author Felix Gnass [fgnass at neteye dot de]
37  */

38 public abstract class AbstractEditorDefinition implements EditorDefinition {
39
40     private String JavaDoc id;
41
42     private String JavaDoc name;
43
44     private EditorRepository editorRepository;
45
46     private EditorDefinition parentEditorDefinition;
47
48     private String JavaDoc icon;
49
50     private boolean hidden;
51
52     public void setEditorRepository(EditorRepository editorRepository) {
53         this.editorRepository = editorRepository;
54     }
55
56     public String JavaDoc getId() {
57         return id;
58     }
59
60     public void setId(String JavaDoc id) {
61         this.id = id;
62     }
63
64     public final void setName(String JavaDoc name) {
65         this.name = name;
66         if (id == null) {
67             id = name;
68         }
69     }
70
71     public final String JavaDoc getName() {
72         return name != null ? name : getDefaultName();
73     }
74
75     protected String JavaDoc getDefaultName() {
76         return null;
77     }
78
79     public Class JavaDoc getBeanClass() {
80         return null;
81     }
82
83     public EditorDefinition getParentEditorDefinition() {
84         return this.parentEditorDefinition;
85     }
86
87     public void setParentEditorDefinition(EditorDefinition parentEditorDefinition) {
88         this.parentEditorDefinition = parentEditorDefinition;
89     }
90
91     public void addReference(List JavaDoc refs, EditorDefinition parentDef,
92             Object JavaDoc parent, MessageResolver messageResolver) {
93
94         String JavaDoc parentId = null;
95         if (parent != null) {
96             parentId = EditorDefinitionUtils.getObjectId(parentDef, parent);
97         }
98         EditorReference ref = createReference(parentId, messageResolver);
99         ref.setEnabled(parent != null);
100         refs.add(ref);
101     }
102
103     protected EditorRepository getEditorRepository() {
104         return editorRepository;
105     }
106
107     public String JavaDoc getIcon() {
108         return this.icon;
109     }
110
111     public void setIcon(String JavaDoc icon) {
112         this.icon = icon;
113     }
114
115     public boolean isHidden() {
116         return this.hidden;
117     }
118
119     public void setHidden(boolean hidden) {
120         this.hidden = hidden;
121     }
122
123     protected StringBuffer JavaDoc getMessageKey() {
124         StringBuffer JavaDoc key = new StringBuffer JavaDoc();
125         key.append(getEditorType());
126         key.append('.');
127         key.append(getName());
128         return key;
129     }
130
131     public String JavaDoc getLabelProperty() {
132         return null;
133     }
134
135     public String JavaDoc getLabel(Object JavaDoc object) {
136         if (object == null) {
137             return "New"; //TODO I18nize this
138
}
139         if (getLabelProperty() != null) {
140             StringBuffer JavaDoc label = new StringBuffer JavaDoc();
141             Pattern JavaDoc p = Pattern.compile("(\\w+)(\\W*)");
142             Matcher JavaDoc m = p.matcher(getLabelProperty());
143             while (m.find()) {
144                 String JavaDoc property = m.group(1);
145                 Object JavaDoc value = PropertyUtils.getProperty(object, property);
146                 if (value != null) {
147                     label.append(value);
148                     label.append(m.group(2));
149                 }
150             }
151             if (label.length() > 0) {
152                 return label.toString();
153             }
154         }
155         return object.toString();
156     }
157
158 }
159
Popular Tags