package de.grapheditor;
import manager.ManagerInterface;
import manager.IllegalParameterException;
import research.Drawing;
import research.DrawingView;
import javax.swing.*;
import de.reuse.DrawingEditorTabbedPane;
import java.util.Map;
import java.util.TreeMap;
import java.util.Hashtable;
import java.awt.dnd.DropTargetListener;
import component.FComponent;
import context.Context;
/**
* Created by IntelliJ IDEA.
* User: zhangwei
* Date: 2004-7-4
* Time: 1:04:57
* To change this template use File | Settings | File Templates.
*/
public class Factory implements ManagerInterface{
public static ManagerInterface getInstance() {
return new Factory();
}
public static Interface createGraphicalEditor() {
Implementation gei = new Implementation();
return gei;
}
public static Interface createGraphicalEditor(Drawing[] drawingArray) {
if (drawingArray == null)
return createGraphicalEditor();
Implementation gei = new Implementation();
for (int i = 0; i < drawingArray.length; i++) {
gei.addViewAction.putValue(ConstantDefinition.DRAWING, drawingArray[i]);
gei.addViewAction.actionPerformed(null);
}
return gei;
}
public static UI getGraphicalEditorUI(Interface graphicalEditor) {
if (!Implementation.class.isInstance(graphicalEditor)) {
return null;
}
Implementation gei = (Implementation) graphicalEditor;
return gei.getGraphicalEditorUI();
}
public static ActionMap getActionMap(Interface graphicalEditor) {
if (!Implementation.class.isInstance(graphicalEditor)) {
return null;
}
Implementation gei = (Implementation) graphicalEditor;
return gei.getActionMap();
}
public static DrawingViewContainer getDrawingViewContainer(Interface graphicalEditor) {
if (!Implementation.class.isInstance(graphicalEditor)) {
return null;
}
Implementation gei = (Implementation) graphicalEditor;
final DrawingEditorTabbedPane pane = (DrawingEditorTabbedPane) gei.getGraphicalEditorUI().getEditorArea();
DrawingViewContainer rst = new DrawingViewContainer() {
public Map getAllDrawingViews() {
DrawingView[] view= (DrawingView[])pane.toArray();
Map rst = new TreeMap();
for(int i = 0; i < view.length; i++){
rst.put(view[i].getDrawing().getTitle(), view[i]);
}
return rst;
}
};
return rst;
}
public static DropTargetListener getDropTargetListener(Interface graphicalEditor) {
if (!Implementation.class.isInstance(graphicalEditor)) {
return null;
}
Implementation gei = (Implementation) graphicalEditor;
return gei.getDropTargetListener();
}
protected Context con = null;
protected Factory() {
if (con == null) {
try {
con = (Context) context.Manager.getInstance().getInstance(context.Manager.CONTEXT, Context.class);
} catch (IllegalParameterException ipe) {
//nerver arrive here
}
}
}
public Context getContext() {
return con;
}
public final static String GRAPHICAL_EDITOR_NAME = "GraphicalEditor";
public final static String GRAPHICAL_EDITOR_UI_NAME = "GraphicalEditorUI";
public final static String ACTIONS_NAME = "Actions";
public final static String GRAPHICAL_EDITOR_INTERFACE = "GraphicalEditor";
public final static String DRAWING_VIEW_CONTAINER_INTERFACE = "DrawingViewContainer";
public final static String EDITOR_AREA_INTERFACE = "EditorArea";
public final static String EDITOR_AREA_POPUP_MENU_INTERFACE = "EditorAreaPopupMenu";
public final static String SCALE_COMPONENT_INTERFACE = "ScaleComponent";
public final static String TOOL_BAR_INTERFACE = "ToolBar";
public final static String DROP_TARGET_LISTENER_INTERFACE = "DropTargetListener";
public final static String ELEMENT_LOCATOR_INTERFACE = "ElementLocator";
public final static String DRAWING_ARRAY_CONTEXT = "DrawingArray";
public Object getInstance(String name, Class type) throws IllegalParameterException {
if ((!GRAPHICAL_EDITOR_NAME.equals(name)) || (!FComponent.class.equals(type))) {
throw new IllegalParameterException("IllegalParameterException");
}
Object value = getContext().getValue(DRAWING_ARRAY_CONTEXT);
Interface graphicalEditor;
UI graphicalEditorUI;
DrawingViewContainer drawingViewContainer;
ActionMap actionMap;
DropTargetListener dropTargetListener;
if (value == null) {
graphicalEditor = createGraphicalEditor();
} else {
if (!value.getClass().isArray()) {
throw new IllegalParameterException("IllegalParameterException");
}
Object[] objects = (Object[]) value;
if (!(objects instanceof Drawing[])) {
throw new IllegalParameterException("IllegalParameterException");
}
Drawing[] drawings = (Drawing[]) objects;
graphicalEditor = createGraphicalEditor(drawings);
}
graphicalEditorUI = getGraphicalEditorUI(graphicalEditor);
actionMap = getActionMap(graphicalEditor);
drawingViewContainer = getDrawingViewContainer(graphicalEditor);
dropTargetListener = getDropTargetListener(graphicalEditor);
Hashtable interfaces = null;
Hashtable classes = null;
//���graphicalUI��NGObjectʵ��
interfaces = new Hashtable();
classes = new Hashtable();
interfaces.put(EDITOR_AREA_INTERFACE, graphicalEditorUI.getEditorArea());
interfaces.put(EDITOR_AREA_POPUP_MENU_INTERFACE, graphicalEditorUI.getEditorAreaPopupMenu());
interfaces.put(SCALE_COMPONENT_INTERFACE, graphicalEditorUI.getScaleComponent());
interfaces.put(TOOL_BAR_INTERFACE, graphicalEditorUI.getToolBar());
classes.put(EDITOR_AREA_INTERFACE, JComponent.class);
classes.put(EDITOR_AREA_POPUP_MENU_INTERFACE, JPopupMenu.class);
classes.put(SCALE_COMPONENT_INTERFACE, JComponent.class);
classes.put(TOOL_BAR_INTERFACE, JToolBar.class);
ManagerInterface mi = component.Manager.getInstance();
mi.getContext().putValue(component.Manager.INTERFACES, interfaces);
mi.getContext().putValue(component.Manager.CLASSES, classes);
mi.getContext().putValue(component.Manager.NAME, GRAPHICAL_EDITOR_UI_NAME);
FComponent ui = (FComponent) mi.getInstance(component.Manager.NGOBJECT, FComponent.class);
//end
//���actionMap��NGObjectʵ��
interfaces = new Hashtable();
classes = new Hashtable();
Object[] keys = actionMap.keys();
for (int i = 0; i < keys.length; i++) {
interfaces.put(keys[i], actionMap.get(keys[i]));
classes.put(keys[i], Action.class);
}
mi = component.Manager.getInstance();
mi.getContext().putValue(component.Manager.INTERFACES, interfaces);
mi.getContext().putValue(component.Manager.CLASSES, classes);
mi.getContext().putValue(component.Manager.NAME, ACTIONS_NAME);
FComponent actions = (FComponent) mi.getInstance(component.Manager.NGOBJECT, FComponent.class);
//
//��װ�������յ�GraphicalEditor��NGObjectʵ��
interfaces = new Hashtable();
classes = new Hashtable();
interfaces.put(GRAPHICAL_EDITOR_INTERFACE, graphicalEditor);
interfaces.put(GRAPHICAL_EDITOR_UI_NAME, ui);
interfaces.put(ACTIONS_NAME, actions);
interfaces.put(DRAWING_VIEW_CONTAINER_INTERFACE, drawingViewContainer);
interfaces.put(DROP_TARGET_LISTENER_INTERFACE, dropTargetListener);
classes.put(GRAPHICAL_EDITOR_INTERFACE, Interface.class);
classes.put(GRAPHICAL_EDITOR_UI_NAME, FComponent.class);
classes.put(ACTIONS_NAME, FComponent.class);
classes.put(DRAWING_VIEW_CONTAINER_INTERFACE, DrawingViewContainer.class);
classes.put(DROP_TARGET_LISTENER_INTERFACE, DropTargetListener.class);
mi = component.Manager.getInstance();
mi.getContext().putValue(component.Manager.INTERFACES, interfaces);
mi.getContext().putValue(component.Manager.CLASSES, classes);
mi.getContext().putValue(component.Manager.NAME, GRAPHICAL_EDITOR_NAME);
FComponent rst = (FComponent) mi.getInstance(component.Manager.NGOBJECT, FComponent.class);
//
return rst;
}
}