001/*
002 * (C) Copyright 2006-2007 Nuxeo SAS <http://nuxeo.com> and others
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the Eclipse Public License v1.0
006 * which accompanies this distribution, and is available at
007 * http://www.eclipse.org/legal/epl-v10.html
008 *
009 * Contributors:
010 *     Jean-Marc Orliaguet, Chalmers
011 *
012 * $Id$
013 */
014
015package org.nuxeo.theme.views;
016
017import java.util.ArrayList;
018import java.util.List;
019
020import org.apache.commons.logging.Log;
021import org.apache.commons.logging.LogFactory;
022import org.nuxeo.common.xmap.annotation.XNode;
023import org.nuxeo.common.xmap.annotation.XNodeList;
024import org.nuxeo.common.xmap.annotation.XObject;
025import org.nuxeo.theme.Manager;
026import org.nuxeo.theme.elements.ElementType;
027import org.nuxeo.theme.formats.FormatType;
028import org.nuxeo.theme.models.ModelType;
029import org.nuxeo.theme.templates.TemplateEngineType;
030import org.nuxeo.theme.types.Type;
031import org.nuxeo.theme.types.TypeFamily;
032
033@XObject("view")
034public final class ViewType implements Type {
035
036    private static final Log log = LogFactory.getLog(ViewType.class);
037
038    @XNode("@name")
039    public String viewName = "*";
040
041    @XNode("engine")
042    public String engineName = "default";
043
044    @XNode("@template-engine")
045    private String templateEngine;
046
047    @XNode("@merge")
048    private boolean merge = false;
049
050    @XNode("mode")
051    public String mode = "*";
052
053    @XNode("icon")
054    public String icon;
055
056    @XNode("element-type")
057    public String elementTypeName = "*";
058
059    @XNode("format-type")
060    public String formatTypeName = "*";
061
062    @XNode("model-type")
063    public String modelTypeName = "*";
064
065    @XNode("class")
066    public String className;
067
068    @XNode("template")
069    public String template;
070
071    @XNodeList(value = "resource", type = ArrayList.class, componentType = String.class)
072    public List<String> resources = new ArrayList<String>();
073
074    private View view;
075
076    public ViewType() {
077    }
078
079    public ViewType(final String viewName, final String className, final String engineName,
080            final String templateEngine, final String mode, final String elementTypeName, final String modelTypeName,
081            final String formatTypeName, final String template, final List<String> resources) {
082        this.viewName = viewName;
083        this.elementTypeName = elementTypeName;
084        this.modelTypeName = modelTypeName;
085        this.formatTypeName = formatTypeName;
086        this.engineName = engineName;
087        this.templateEngine = templateEngine;
088        this.mode = mode;
089        this.className = className;
090        this.template = template;
091        this.resources = resources;
092    }
093
094    public String getTypeName() {
095        return computeName(formatTypeName, elementTypeName, viewName, modelTypeName, engineName, mode, templateEngine);
096    }
097
098    public static String computeName(final String formatTypeName, final String elementTypeName, final String viewName,
099            final String modelTypeName, final String engineName, final String mode, final String templateEngineName) {
100
101        return String.format("%s/%s/%s/%s/%s/%s/%s", formatTypeName, elementTypeName, viewName, modelTypeName,
102                engineName, mode, templateEngineName);
103    }
104
105    public TypeFamily getTypeFamily() {
106        return TypeFamily.VIEW;
107    }
108
109    public String getViewName() {
110        return viewName;
111    }
112
113    public View getView() {
114        if (view != null) {
115            return view;
116        }
117        if (className == null) {
118            className = ((TemplateEngineType) Manager.getTypeRegistry().lookup(TypeFamily.TEMPLATE_ENGINE,
119                    templateEngine)).getTemplateView();
120        }
121        try {
122            view = (View) Class.forName(className).newInstance();
123            view.setViewType(this);
124        } catch (ReflectiveOperationException e) {
125            log.error("Could not create view for: " + className);
126            return null;
127        }
128        return view;
129    }
130
131    public ElementType getElementType() {
132        return (ElementType) Manager.getTypeRegistry().lookup(TypeFamily.ELEMENT, elementTypeName);
133    }
134
135    public ModelType getModelType() {
136        return (ModelType) Manager.getTypeRegistry().lookup(TypeFamily.MODEL, modelTypeName);
137    }
138
139    public FormatType getFormatType() {
140        return (FormatType) Manager.getTypeRegistry().lookup(TypeFamily.FORMAT, formatTypeName);
141    }
142
143    public String getTemplate() {
144        return template;
145    }
146
147    public void setTemplate(final String template) {
148        this.template = template;
149    }
150
151    public List<String> getResources() {
152        return resources;
153    }
154
155    public void setResources(final List<String> resources) {
156        this.resources = resources;
157    }
158
159    public String getMode() {
160        return mode;
161    }
162
163    public void setMode(String mode) {
164        this.mode = mode;
165    }
166
167    public String getIcon() {
168        return icon;
169    }
170
171    public void setIcon(String icon) {
172        this.icon = icon;
173    }
174
175    public String getTemplateEngine() {
176        return templateEngine;
177    }
178
179    public void setTemplateEngine(String templateEngine) {
180        this.templateEngine = templateEngine;
181    }
182
183    public boolean isMerge() {
184        return merge;
185    }
186
187    public void setMerge(boolean merge) {
188        this.merge = merge;
189    }
190
191    public void addResource(String resource) {
192        if (!resources.contains(resource)) {
193            resources.add(resource);
194        }
195    }
196}