blob: c752addb810ee21a8c7fa37c1dc5bc9080ce2ab9 [file] [log] [blame]
kchong38cbf172006-03-29 03:38:21 +00001/*******************************************************************************
kchong5f5dca52007-03-07 05:45:54 +00002 * Copyright (c) 2001, 2007 IBM Corporation and others.
kchong38cbf172006-03-29 03:38:21 +00003 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
7 *
8 * Contributors:
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
kchong2be71b32006-04-11 16:32:03 +000011package org.eclipse.wst.xsd.ui.internal.design.editparts;
kchong38cbf172006-03-29 03:38:21 +000012
kchongda8feb82006-07-24 16:16:12 +000013import java.util.Iterator;
14
kchong38cbf172006-03-29 03:38:21 +000015import org.eclipse.draw2d.IFigure;
kchongda8feb82006-07-24 16:16:12 +000016import org.eclipse.draw2d.Label;
kchong5f5dca52007-03-07 05:45:54 +000017import org.eclipse.draw2d.ManhattanConnectionRouter;
kchong38cbf172006-03-29 03:38:21 +000018import org.eclipse.draw2d.ToolbarLayout;
kchong5f5dca52007-03-07 05:45:54 +000019import org.eclipse.draw2d.geometry.Point;
kchongda8feb82006-07-24 16:16:12 +000020import org.eclipse.gef.EditPart;
kchong38cbf172006-03-29 03:38:21 +000021import org.eclipse.gef.EditPolicy;
kchong5f5dca52007-03-07 05:45:54 +000022import org.eclipse.gef.Request;
23import org.eclipse.gef.RequestConstants;
kchongda8feb82006-07-24 16:16:12 +000024import org.eclipse.gef.editparts.AbstractGraphicalEditPart;
kchong5f5dca52007-03-07 05:45:54 +000025import org.eclipse.gef.requests.LocationRequest;
26import org.eclipse.gef.ui.actions.ActionRegistry;
27import org.eclipse.jface.action.IAction;
28import org.eclipse.ui.IEditorPart;
29import org.eclipse.ui.IWorkbench;
30import org.eclipse.ui.IWorkbenchWindow;
31import org.eclipse.ui.PlatformUI;
kchong2be71b32006-04-11 16:32:03 +000032import org.eclipse.wst.xsd.ui.internal.adapters.XSDSimpleTypeDefinitionAdapter;
kchong5f5dca52007-03-07 05:45:54 +000033import org.eclipse.wst.xsd.ui.internal.adt.actions.SetInputToGraphView;
kchong2be71b32006-04-11 16:32:03 +000034import org.eclipse.wst.xsd.ui.internal.adt.design.editparts.BaseEditPart;
35import org.eclipse.wst.xsd.ui.internal.adt.design.editparts.BaseTypeConnectingEditPart;
kchongda8feb82006-07-24 16:16:12 +000036import org.eclipse.wst.xsd.ui.internal.adt.design.editparts.CenteredConnectionAnchor;
37import org.eclipse.wst.xsd.ui.internal.adt.design.editparts.ColumnEditPart;
kchong2be71b32006-04-11 16:32:03 +000038import org.eclipse.wst.xsd.ui.internal.adt.design.editparts.TypeReferenceConnection;
39import org.eclipse.wst.xsd.ui.internal.adt.design.editpolicies.ADTDirectEditPolicy;
40import org.eclipse.wst.xsd.ui.internal.adt.design.editpolicies.ADTSelectionFeedbackEditPolicy;
41import org.eclipse.wst.xsd.ui.internal.adt.design.figures.IStructureFigure;
kchongda8feb82006-07-24 16:16:12 +000042import org.eclipse.wst.xsd.ui.internal.adt.facade.IType;
43import org.eclipse.wst.xsd.ui.internal.adt.typeviz.design.figures.HeadingFigure;
kchong2be71b32006-04-11 16:32:03 +000044import org.eclipse.wst.xsd.ui.internal.adt.typeviz.design.figures.RoundedLineBorder;
45import org.eclipse.wst.xsd.ui.internal.adt.typeviz.design.figures.StructureFigure;
kchong5f5dca52007-03-07 05:45:54 +000046import org.eclipse.wst.xsd.ui.internal.common.actions.OpenInNewEditor;
kchongdd640c02007-01-15 23:28:20 +000047import org.eclipse.wst.xsd.ui.internal.common.util.XSDCommonUIUtils;
kchong5f5dca52007-03-07 05:45:54 +000048import org.eclipse.xsd.XSDSchema;
kchong38cbf172006-03-29 03:38:21 +000049import org.eclipse.xsd.XSDSimpleTypeDefinition;
50
51public class XSDSimpleTypeEditPart extends BaseTypeConnectingEditPart
52{
kchongd9d1eba2007-03-09 19:32:15 +000053 protected StructureFigure figure;
kchong38cbf172006-03-29 03:38:21 +000054 protected ADTDirectEditPolicy adtDirectEditPolicy = new ADTDirectEditPolicy();
55
56 public XSDSimpleTypeEditPart()
57 {
58 super();
59 }
60
61 public XSDSimpleTypeDefinition getXSDSimpleTypeDefinition()
62 {
63 return (XSDSimpleTypeDefinition)((XSDSimpleTypeDefinitionAdapter)getModel()).getTarget();
64 }
65
66 protected IFigure createFigure()
67 {
68 figure = new StructureFigure();
69 figure.setBorder(new RoundedLineBorder(1, 10));
70 ToolbarLayout toolbarLayout = new ToolbarLayout();
71 toolbarLayout.setStretchMinorAxis(true);
72 figure.setLayoutManager(toolbarLayout);
kchong38cbf172006-03-29 03:38:21 +000073 return figure;
74 }
75
76 protected void refreshVisuals()
77 {
kchong6e490672006-04-26 22:40:02 +000078 XSDSimpleTypeDefinitionAdapter adapter = (XSDSimpleTypeDefinitionAdapter)getModel();
79 String name = adapter.getDisplayName();
kchongda8feb82006-07-24 16:16:12 +000080 HeadingFigure headingFigure = figure.getHeadingFigure();
81 headingFigure.setIsReadOnly(adapter.isReadOnly());
82 Label label = headingFigure.getLabel();
83 label.setText(name);
kchongaeb593a2007-01-17 06:10:07 +000084 label.setIcon(XSDCommonUIUtils.getUpdatedImage(adapter.getXSDTypeDefinition(), adapter.getImage(), adapter.isReadOnly()));
kchong38cbf172006-03-29 03:38:21 +000085 }
86
87 public IStructureFigure getStructureFigure()
88 {
89 return (IStructureFigure)getFigure();
90 }
91
92 public IFigure getContentPane()
93 {
94 return getStructureFigure().getContentPane();
95 }
96
97
98 protected void createEditPolicies()
99 {
csalterc7926c02006-07-18 16:19:46 +0000100 super.createEditPolicies();
kchong38cbf172006-03-29 03:38:21 +0000101 installEditPolicy(EditPolicy.SELECTION_FEEDBACK_ROLE, new ADTSelectionFeedbackEditPolicy());
102 installEditPolicy(EditPolicy.DIRECT_EDIT_ROLE, adtDirectEditPolicy);
103 }
104
105 public void addFeedback()
106 {
107 getStructureFigure().addSelectionFeedback();
108 super.addFeedback();
109 }
110
111 public void removeFeedback()
112 {
113 getStructureFigure().removeSelectionFeedback();
114 super.removeFeedback();
115 }
116
117 public ReferenceConnection createConnectionFigure(BaseEditPart child)
118 {
119 // TODO Auto-generated method stub
120 return null;
121 }
122
123 public TypeReferenceConnection createConnectionFigure()
124 {
kchongda8feb82006-07-24 16:16:12 +0000125 TypeReferenceConnection connectionFigure = null;
126 XSDSimpleTypeDefinitionAdapter adapter = (XSDSimpleTypeDefinitionAdapter)getModel();
127 IType superType = adapter.getSuperType();
128
129 if (superType != null)
130 {
131 AbstractGraphicalEditPart referenceTypePart = (AbstractGraphicalEditPart)getTargetEditPart(superType);
132
133 if (referenceTypePart != null)
134 {
kchong5f5dca52007-03-07 05:45:54 +0000135 connectionFigure = new TypeReferenceConnection(true);
kchongda8feb82006-07-24 16:16:12 +0000136 // draw a line out from the top
137 connectionFigure.setSourceAnchor(new CenteredConnectionAnchor(getFigure(), CenteredConnectionAnchor.TOP, 1));
138
139 // TODO (cs) need to draw the target anchor to look like a UML inheritance relationship
140 // adding a label to the connection would help to
kchong5f5dca52007-03-07 05:45:54 +0000141 connectionFigure.setTargetAnchor(new CenteredConnectionAnchor(referenceTypePart.getFigure(), CenteredConnectionAnchor.BOTTOM, 0, 0));
142 connectionFigure.setConnectionRouter(new ManhattanConnectionRouter());
143 ((CenteredConnectionAnchor)connectionFigure.getSourceAnchor()).setOther((CenteredConnectionAnchor)connectionFigure.getTargetAnchor());
kchongda8feb82006-07-24 16:16:12 +0000144 connectionFigure.setHighlight(false);
kchong5f5dca52007-03-07 05:45:54 +0000145
kchongda8feb82006-07-24 16:16:12 +0000146 }
147 }
148 return connectionFigure;
149 }
150
kchongd9d1eba2007-03-09 19:32:15 +0000151 protected EditPart getTargetEditPart(IType type)
kchongda8feb82006-07-24 16:16:12 +0000152 {
153 ColumnEditPart columnEditPart = null;
154 for (EditPart editPart = this; editPart != null; editPart = editPart.getParent())
155 {
156 if (editPart instanceof ColumnEditPart)
157 {
158 columnEditPart = (ColumnEditPart)editPart;
159 break;
160 }
161 }
162 if (columnEditPart != null)
163 {
164 for (Iterator i = columnEditPart.getChildren().iterator(); i.hasNext(); )
165 {
166 EditPart child = (EditPart)i.next();
167 if (child.getModel() == type)
168 {
169 return child;
170 }
171 }
172 }
kchong38cbf172006-03-29 03:38:21 +0000173 return null;
174 }
kchong5f5dca52007-03-07 05:45:54 +0000175
176 public void performRequest(Request request)
177 {
178 if (request.getType() == RequestConstants.REQ_OPEN)
179 {
180 Object model = getModel();
181 if (request instanceof LocationRequest)
182 {
183 LocationRequest locationRequest = (LocationRequest)request;
184 Point p = locationRequest.getLocation();
185
186 if (getStructureFigure().hitTestHeader(p))
187 {
188 // TODO: !!! This should be moved to the adt-xsd package
189 //
190 if (model instanceof XSDSimpleTypeDefinitionAdapter)
191 {
192 XSDSimpleTypeDefinitionAdapter adapter = (XSDSimpleTypeDefinitionAdapter)model;
193 XSDSimpleTypeDefinition st = (XSDSimpleTypeDefinition)adapter.getTarget();
194 IWorkbench workbench = PlatformUI.getWorkbench();
195 IWorkbenchWindow workbenchWindow = workbench.getActiveWorkbenchWindow();
196 IEditorPart editorPart = workbenchWindow.getActivePage().getActiveEditor();
197 Object schema = editorPart.getAdapter(XSDSchema.class);
198 ActionRegistry registry = getEditorActionRegistry(editorPart);
199 if (registry != null)
200 {
201 if (schema == st.getSchema())
202 {
203 IAction action = registry.getAction(SetInputToGraphView.ID);
204 action.run();
205 }
206 else
207 {
208 IAction action = registry.getAction(OpenInNewEditor.ID);
209 action.run();
210 }
211 }
212 }
213 }
214 }
215 }
216 }
217
218 protected ActionRegistry getEditorActionRegistry(IEditorPart editor)
219 {
220 return (ActionRegistry) editor.getAdapter(ActionRegistry.class);
221 }
222
223 protected boolean shouldDrawConnection()
224 {
225 if (getModel() instanceof IType)
226 {
227 if (((IType)getModel()).getSuperType() != null)
228 return true;
229 }
230 return false;
231 }
kchong38cbf172006-03-29 03:38:21 +0000232}