//{HEADER
/**
* This class is part of jnex 'Nexirius Application Framework for Java'
*
* Copyright (C) Nexirius GmbH, CH-4450 Sissach, Switzerland (www.nexirius.ch)
*
* <p>This library is free software; you can redistribute it and/or<br>
* modify it under the terms of the GNU Lesser General Public<br>
* License as published by the Free Software Foundation; either<br>
* version 2.1 of the License, or (at your option) any later version.</p>
*
* <p>This library is distributed in the hope that it will be useful,<br>
* but WITHOUT ANY WARRANTY; without even the implied warranty of<br>
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU<br>
* Lesser General Public License for more details.</p>
*
* <p>You should have received a copy of the GNU Lesser General Public<br>
* License along with this library; if not, write to the Free Software<br>
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA</p>
* </blockquote>
*
* <p>
* Nexirius GmbH, hereby disclaims all copyright interest in<br>
* the library jnex' 'Nexirius Application Framework for Java' written<br>
* by Marcel Baumann.</p>
*/
//}HEADER
package com.nexirius.framework.dataviewer;
import com.nexirius.framework.datamodel.DataModel;
import com.nexirius.framework.datamodel.Viewable;
import com.nexirius.framework.swing.SwingViewerCreator;
import com.nexirius.util.assertion.Assert;
import java.util.Hashtable;
/**
* Maps viewer names to viewer creators and layout items.
*
* @author Marcel Baumann
* @see BasicViewerCreatorMap
*/
public class ViewerCreatorMap {
Hashtable viewerCreatorTable = new Hashtable();
Hashtable editorCreatorTable = new Hashtable();
Hashtable treeComponentCreatorTable = new Hashtable();
/**
* Returns a creator instance to create a viewer or an editor from a viewable
*
* @param viewable The viewable intance which needs a GUI
* @param isEditor If true the editor table is looked for an entry before the viewer table is seeked
* @throws Exception If their is no creator registered for the specified viewable an exception is thrown
*/
public ViewerCreator getViewerCreator(Viewable viewable, boolean isEditor)
throws Exception {
if (viewable == null) {
Assert.pre(viewable != null, "getViewerCreator needs a non parameter null viewable");
}
return getViewerCreator(viewable.getClass(), isEditor);
}
/**
* Returns a creator instance to create a viewer or an editor from a viewable class
*
* @param c The class of the viewable intance which needs a GUI
* @param isEditor if true the editor table is looked for an entry before the viewer table is seeked
* @throws Exception if their is no creator registered for the specified viewable an exception is thrown
*/
public ViewerCreator getViewerCreator(Class c, boolean isEditor)
throws Exception {
String ident = c.getName();
CreatorMapEntry entry = null;
if (isEditor) {
// search first in the editorCreatorTable
entry = (CreatorMapEntry) editorCreatorTable.get(ident);
if (entry != null && entry.getViewerCreator() != null) {
return entry.getViewerCreator();
}
}
// search in viewerCreatorTable
entry = (CreatorMapEntry) viewerCreatorTable.get(ident);
if (entry != null && entry.getViewerCreator() != null) {
return entry.getViewerCreator();
}
// go up the hierarchie and try to find a class which is mapped
Class superclass = c.getSuperclass();
if (superclass != null) {
try {
return getViewerCreator(superclass, isEditor);
} catch (Exception ex) {
// ignore
}
}
throw new Exception("No viewer creator defined for: " + ident);
}
/**
* Returns a creator instance to create a tree component editor from a data model
*
* @param modelClass The data model class which needs a GUI in a DataModelTreeViewer
*/
public DataModelTreeComponentCreator getTreeComponentViewerCreator(Class modelClass) {
DataModelTreeComponentCreator ret = (DataModelTreeComponentCreator) treeComponentCreatorTable.get(modelClass);
while (modelClass != null && ret == null) {
modelClass = modelClass.getSuperclass();
if (modelClass == null) {
return null;
}
ret = (DataModelTreeComponentCreator) treeComponentCreatorTable.get(modelClass);
}
return ret;
}
/**
* registerTemplate a factory for tree components
*
* @param modelClass the data model class
* @param creator the factory
*/
public void registerTreeComponentViewerCreator(Class modelClass, DataModelTreeComponentCreator creator) {
treeComponentCreatorTable.put(modelClass, creator);
}
/**
* registerTemplate a factory for tree components
*
* @param modelClass the data model class
* @param creator the factory (used to render the body part of the TreeComponent
*/
public void registerViewerCreatorForTreeComponentBody(Class modelClass, ViewerCreator creator) {
DataModelTreeComponentCreator tvc = new DataModelTreeComponentCreator() {
public DataModelTreeComponent createDataModelTreeComponent(DataModelTreeViewer treeViewer, DataModel model) {
DataModelTreeComponent treeComponent = new DataModelTreeComponent(treeViewer, model);
treeComponent.init();
treeComponent.setDisplayAsEditor(true);
return treeComponent;
}
};
treeComponentCreatorTable.put(modelClass, tvc);
}
/**
* Returns the layout information which is registered for a data model (or null)
*
* @param viewable The data model intance which needs a GUI
* @param isEditor If true the editor table is looked for an entry before the viewer table is seeked
*/
public LayoutItem getLayoutItem(Viewable viewable, boolean isEditor) {
if (viewable == null) {
Assert.pre(viewable != null, "getLayout needs a non null viewable");
}
String ident = viewable.getClass().getName();
CreatorMapEntry entry = null;
if (isEditor) {
// search first in the editorCreatorTable
entry = (CreatorMapEntry) editorCreatorTable.get(ident);
if (entry != null && entry.getLayoutItem() != null) {
return entry.getLayoutItem();
}
}
// search in viewerCreatorTable
entry = (CreatorMapEntry) viewerCreatorTable.get(ident);
if (entry != null && entry.getLayoutItem() != null) {
return entry.getLayoutItem();
}
return null;
}
/**
* Register a creator instance for the specified data model identifier.
* If the viewer creator and the editor creator are both the same then it is sufficient to only registerTemplate the viewer creator (isEditor == false)
*
* @param viewableClassName The class name of the viewable class which will be registered
* @param viewerCreator An instance of the creator which will be used to create viewers from viewables
*/
public void register(String viewableClassName, ViewerCreator viewerCreator) {
boolean isEditor = viewerCreator.isEditor();
Hashtable table = (isEditor ? editorCreatorTable : viewerCreatorTable);
CreatorMapEntry entry = (CreatorMapEntry) table.get(viewableClassName);
if (entry == null) {
entry = new CreatorMapEntry(viewableClassName, viewerCreator);
table.put(viewableClassName, entry);
}
entry.setViewerCreator(viewerCreator);
}
/**
* Register a creator instance for the specified data model identifier.
* If the viewer creator and the editor creator are both the same then it is sufficient to only registerTemplate the viewer creator (isEditor == false)
*
* @param viewableClass The class of the Viewable (DataModel) which will be registered
* @param viewerCreator An instance of the creator which will be used to create viewers from viewables
*/
public void register(Class viewableClass, ViewerCreator viewerCreator) {
register(viewableClass.getName(), viewerCreator);
}
/**
* Register a creator instance for the specified data model identifier.
* If the viewer creator and the editor creator are both the same then it is sufficient to only registerTemplate the viewer creator (isEditor == false)
*
* @param viewableClass The class of the Viewable (DataModel) which will be registered
* @param swingViewerClass The implementing class of a SwingViewer
*/
public void registerSwingViewer(Class viewableClass, Class swingViewerClass) {
register(viewableClass.getName(), new SwingViewerCreator(swingViewerClass));
}
/**
* Register a layout for the specified data model identifier
* If the viewer layout and the editor layout are both the same then it is sufficient to only registerTemplate the viewer layout (isEditor == false)
*
* @param viewableClassName The class name of the viewable class which will be registered
* @param layout The layout which will be registered as default layout for the specified viewable class
* @param isEditor If true then this layout is used for editor panels
*/
public void register(String viewableClassName, LayoutItem layout, boolean isEditor) {
Hashtable table = (isEditor ? editorCreatorTable : viewerCreatorTable);
CreatorMapEntry entry = (CreatorMapEntry) table.get(viewableClassName);
if (entry == null) {
entry = new CreatorMapEntry(viewableClassName, layout, isEditor);
table.put(viewableClassName, entry);
}
entry.setLayoutItem(layout);
}
/**
* Register a layout for the specified data model identifier
* If the viewer layout and the editor layout are both the same then it is sufficient to only registerTemplate the viewer layout (isEditor == false)
*
* @param viewableClass The class of the Viewable (DataModel) which will be registered
* @param layout The layout which will be registered as default layout for the specified viewable class
* @param isEditor If true then this layout is used for editor panels
*/
public void register(Class viewableClass, LayoutItem layout, boolean isEditor) {
register(viewableClass.getName(), layout, isEditor);
}
/**
* Register by CreatorMapEntry
*/
public void register(CreatorMapEntry entry) {
Hashtable table = (entry.isEditor() ? editorCreatorTable : viewerCreatorTable);
CreatorMapEntry existingEntry = (CreatorMapEntry) table.get(entry.getViewableClassName());
if (existingEntry == null) {
table.put(entry.getViewableClassName(), entry);
} else {
existingEntry.merge(entry);
}
}
}