package de.FeatureModellingTool.GraphicalEditor;
import de.reuse.DrawingEditorTabbedPane;
import javax.swing.*;
import research.DrawingView;
import research.Drawing;
import manager.ManagerInterface;
import manager.IllegalParameterException;
import context.Context;
import component.FComponent;
import java.util.Hashtable;
import java.awt.dnd.DropTargetListener;
/**
* author: zhangwei
* Date: 2003-5-30
* Time: 20:01:13
*/
public final class Manager implements ManagerInterface {
public static ManagerInterface getInstance() {
return new Manager();
}
public static GraphicalEditor createGraphicalEditor() {
GraphicalEditorImplementation gei = new GraphicalEditorImplementation();
return gei;
}
public static GraphicalEditor createGraphicalEditor(Drawing[] drawingArray) {
if (drawingArray == null)
return createGraphicalEditor();
GraphicalEditorImplementation gei = new GraphicalEditorImplementation();
for (int i = 0; i < drawingArray.length; i++) {
gei.addViewAction.putValue(ConstantDefinition.DRAWING, drawingArray[i]);
gei.addViewAction.actionPerformed(null);
}
return gei;
}
public static GraphicalEditorUI getGraphicalEditorUI(GraphicalEditor graphicalEditor) {
if (!GraphicalEditorImplementation.class.isInstance(graphicalEditor)) {
return null;
}
GraphicalEditorImplementation gei = (GraphicalEditorImplementation) graphicalEditor;
return gei.getGraphicalEditorUI();
}
public static ElementLocator getElementLocator(GraphicalEditor graphicalEditor){
if (!GraphicalEditorImplementation.class.isInstance(graphicalEditor)) {
return null;
}
GraphicalEditorImplementation gei = (GraphicalEditorImplementation) graphicalEditor;
return gei.getElementLocator();
}
public static ActionMap getActionMap(GraphicalEditor graphicalEditor) {
if (!GraphicalEditorImplementation.class.isInstance(graphicalEditor)) {
return null;
}
GraphicalEditorImplementation gei = (GraphicalEditorImplementation) graphicalEditor;
return gei.getActionMap();
}
public static DrawingViewContainer getDrawingViewContainer(GraphicalEditor graphicalEditor) {
if (!GraphicalEditorImplementation.class.isInstance(graphicalEditor)) {
return null;
}
GraphicalEditorImplementation gei = (GraphicalEditorImplementation) graphicalEditor;
final DrawingEditorTabbedPane pane = (DrawingEditorTabbedPane) gei.getGraphicalEditorUI().getEditorArea();
DrawingViewContainer rst = new DrawingViewContainer() {
public DrawingView[] getAllDrawingViews() {
return pane.toArray();
}
};
return rst;
}
public static DropTargetListener getDropTargetListener(GraphicalEditor graphicalEditor) {
if (!GraphicalEditorImplementation.class.isInstance(graphicalEditor)) {
return null;
}
GraphicalEditorImplementation gei = (GraphicalEditorImplementation) graphicalEditor;
return gei.getDropTargetListener();
}
protected Context con = null;
protected Manager() {
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);
GraphicalEditor graphicalEditor;
GraphicalEditorUI graphicalEditorUI;
DrawingViewContainer drawingViewContainer;
ActionMap actionMap;
DropTargetListener dropTargetListener;
ElementLocator elementLocator;
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);
elementLocator = getElementLocator(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);
interfaces.put(ELEMENT_LOCATOR_INTERFACE, elementLocator);
classes.put(GRAPHICAL_EDITOR_INTERFACE, GraphicalEditor.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);
classes.put(ELEMENT_LOCATOR_INTERFACE, ElementLocator.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;
}
}