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.models; 016 017import java.util.ArrayList; 018import java.util.List; 019 020import org.nuxeo.theme.Manager; 021 022public abstract class AbstractModel implements Model { 023 024 private final List<Model> items = new ArrayList<Model>(); 025 026 public String getModelTypeName() { 027 ModelType modelType = getModelType(); 028 if (modelType == null) { 029 return null; 030 } 031 return modelType.getTypeName(); 032 } 033 034 public ModelType getModelType() { 035 final String className = this.getClass().getCanonicalName(); 036 return Manager.getThemeManager().getModelByClassname(className); 037 } 038 039 public Model addItem(Model model) throws ModelException { 040 ModelType modelType = getModelType(); 041 if (modelType == null) { 042 throw new ModelException("Model type not found: " + getModelTypeName()); 043 } 044 if (!getModelType().getAllowedTypes().contains(model.getModelTypeName())) { 045 throw new ModelException("Model type: " + model.getModelTypeName() + " not allowed in: " 046 + getModelTypeName()); 047 } 048 items.add(model); 049 return model; 050 } 051 052 public Model insertItem(int index, Model model) throws ModelException { 053 if (!getModelType().getAllowedTypes().contains(model.getModelTypeName())) { 054 throw new ModelException("Model type: " + model.getModelTypeName() + " not allowed in: " 055 + getModelTypeName()); 056 } 057 items.add(index, model); 058 return model; 059 } 060 061 public List<Model> getItems() { 062 return items; 063 } 064 065 public boolean hasItems() { 066 return !items.isEmpty(); 067 } 068}