kchong | 38cbf17 | 2006-03-29 03:38:21 +0000 | [diff] [blame] | 1 | /******************************************************************************* |
kchong | 5f5dca5 | 2007-03-07 05:45:54 +0000 | [diff] [blame] | 2 | * Copyright (c) 2001, 2007 IBM Corporation and others. |
kchong | 38cbf17 | 2006-03-29 03:38:21 +0000 | [diff] [blame] | 3 | * 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 | *******************************************************************************/ |
kchong | 2be71b3 | 2006-04-11 16:32:03 +0000 | [diff] [blame] | 11 | package org.eclipse.wst.xsd.ui.internal.design.editparts; |
kchong | 38cbf17 | 2006-03-29 03:38:21 +0000 | [diff] [blame] | 12 | |
kchong | da8feb8 | 2006-07-24 16:16:12 +0000 | [diff] [blame] | 13 | import java.util.Iterator; |
| 14 | |
kchong | 38cbf17 | 2006-03-29 03:38:21 +0000 | [diff] [blame] | 15 | import org.eclipse.draw2d.IFigure; |
kchong | da8feb8 | 2006-07-24 16:16:12 +0000 | [diff] [blame] | 16 | import org.eclipse.draw2d.Label; |
kchong | 5f5dca5 | 2007-03-07 05:45:54 +0000 | [diff] [blame] | 17 | import org.eclipse.draw2d.ManhattanConnectionRouter; |
kchong | 38cbf17 | 2006-03-29 03:38:21 +0000 | [diff] [blame] | 18 | import org.eclipse.draw2d.ToolbarLayout; |
kchong | 5f5dca5 | 2007-03-07 05:45:54 +0000 | [diff] [blame] | 19 | import org.eclipse.draw2d.geometry.Point; |
kchong | da8feb8 | 2006-07-24 16:16:12 +0000 | [diff] [blame] | 20 | import org.eclipse.gef.EditPart; |
kchong | 38cbf17 | 2006-03-29 03:38:21 +0000 | [diff] [blame] | 21 | import org.eclipse.gef.EditPolicy; |
kchong | 5f5dca5 | 2007-03-07 05:45:54 +0000 | [diff] [blame] | 22 | import org.eclipse.gef.Request; |
| 23 | import org.eclipse.gef.RequestConstants; |
kchong | da8feb8 | 2006-07-24 16:16:12 +0000 | [diff] [blame] | 24 | import org.eclipse.gef.editparts.AbstractGraphicalEditPart; |
kchong | 5f5dca5 | 2007-03-07 05:45:54 +0000 | [diff] [blame] | 25 | import org.eclipse.gef.requests.LocationRequest; |
| 26 | import org.eclipse.gef.ui.actions.ActionRegistry; |
| 27 | import org.eclipse.jface.action.IAction; |
| 28 | import org.eclipse.ui.IEditorPart; |
| 29 | import org.eclipse.ui.IWorkbench; |
| 30 | import org.eclipse.ui.IWorkbenchWindow; |
| 31 | import org.eclipse.ui.PlatformUI; |
kchong | 2be71b3 | 2006-04-11 16:32:03 +0000 | [diff] [blame] | 32 | import org.eclipse.wst.xsd.ui.internal.adapters.XSDSimpleTypeDefinitionAdapter; |
kchong | 5f5dca5 | 2007-03-07 05:45:54 +0000 | [diff] [blame] | 33 | import org.eclipse.wst.xsd.ui.internal.adt.actions.SetInputToGraphView; |
kchong | 2be71b3 | 2006-04-11 16:32:03 +0000 | [diff] [blame] | 34 | import org.eclipse.wst.xsd.ui.internal.adt.design.editparts.BaseEditPart; |
| 35 | import org.eclipse.wst.xsd.ui.internal.adt.design.editparts.BaseTypeConnectingEditPart; |
kchong | da8feb8 | 2006-07-24 16:16:12 +0000 | [diff] [blame] | 36 | import org.eclipse.wst.xsd.ui.internal.adt.design.editparts.CenteredConnectionAnchor; |
| 37 | import org.eclipse.wst.xsd.ui.internal.adt.design.editparts.ColumnEditPart; |
kchong | 2be71b3 | 2006-04-11 16:32:03 +0000 | [diff] [blame] | 38 | import org.eclipse.wst.xsd.ui.internal.adt.design.editparts.TypeReferenceConnection; |
| 39 | import org.eclipse.wst.xsd.ui.internal.adt.design.editpolicies.ADTDirectEditPolicy; |
| 40 | import org.eclipse.wst.xsd.ui.internal.adt.design.editpolicies.ADTSelectionFeedbackEditPolicy; |
| 41 | import org.eclipse.wst.xsd.ui.internal.adt.design.figures.IStructureFigure; |
kchong | da8feb8 | 2006-07-24 16:16:12 +0000 | [diff] [blame] | 42 | import org.eclipse.wst.xsd.ui.internal.adt.facade.IType; |
| 43 | import org.eclipse.wst.xsd.ui.internal.adt.typeviz.design.figures.HeadingFigure; |
kchong | 2be71b3 | 2006-04-11 16:32:03 +0000 | [diff] [blame] | 44 | import org.eclipse.wst.xsd.ui.internal.adt.typeviz.design.figures.RoundedLineBorder; |
| 45 | import org.eclipse.wst.xsd.ui.internal.adt.typeviz.design.figures.StructureFigure; |
kchong | 5f5dca5 | 2007-03-07 05:45:54 +0000 | [diff] [blame] | 46 | import org.eclipse.wst.xsd.ui.internal.common.actions.OpenInNewEditor; |
kchong | dd640c0 | 2007-01-15 23:28:20 +0000 | [diff] [blame] | 47 | import org.eclipse.wst.xsd.ui.internal.common.util.XSDCommonUIUtils; |
kchong | 5f5dca5 | 2007-03-07 05:45:54 +0000 | [diff] [blame] | 48 | import org.eclipse.xsd.XSDSchema; |
kchong | 38cbf17 | 2006-03-29 03:38:21 +0000 | [diff] [blame] | 49 | import org.eclipse.xsd.XSDSimpleTypeDefinition; |
| 50 | |
| 51 | public class XSDSimpleTypeEditPart extends BaseTypeConnectingEditPart |
| 52 | { |
kchong | d9d1eba | 2007-03-09 19:32:15 +0000 | [diff] [blame] | 53 | protected StructureFigure figure; |
kchong | 38cbf17 | 2006-03-29 03:38:21 +0000 | [diff] [blame] | 54 | 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); |
kchong | 38cbf17 | 2006-03-29 03:38:21 +0000 | [diff] [blame] | 73 | return figure; |
| 74 | } |
| 75 | |
| 76 | protected void refreshVisuals() |
| 77 | { |
kchong | 6e49067 | 2006-04-26 22:40:02 +0000 | [diff] [blame] | 78 | XSDSimpleTypeDefinitionAdapter adapter = (XSDSimpleTypeDefinitionAdapter)getModel(); |
| 79 | String name = adapter.getDisplayName(); |
kchong | da8feb8 | 2006-07-24 16:16:12 +0000 | [diff] [blame] | 80 | HeadingFigure headingFigure = figure.getHeadingFigure(); |
| 81 | headingFigure.setIsReadOnly(adapter.isReadOnly()); |
| 82 | Label label = headingFigure.getLabel(); |
| 83 | label.setText(name); |
kchong | aeb593a | 2007-01-17 06:10:07 +0000 | [diff] [blame] | 84 | label.setIcon(XSDCommonUIUtils.getUpdatedImage(adapter.getXSDTypeDefinition(), adapter.getImage(), adapter.isReadOnly())); |
kchong | 38cbf17 | 2006-03-29 03:38:21 +0000 | [diff] [blame] | 85 | } |
| 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 | { |
csalter | c7926c0 | 2006-07-18 16:19:46 +0000 | [diff] [blame] | 100 | super.createEditPolicies(); |
kchong | 38cbf17 | 2006-03-29 03:38:21 +0000 | [diff] [blame] | 101 | 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 | { |
kchong | da8feb8 | 2006-07-24 16:16:12 +0000 | [diff] [blame] | 125 | 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 | { |
kchong | 5f5dca5 | 2007-03-07 05:45:54 +0000 | [diff] [blame] | 135 | connectionFigure = new TypeReferenceConnection(true); |
kchong | da8feb8 | 2006-07-24 16:16:12 +0000 | [diff] [blame] | 136 | // 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 |
kchong | 5f5dca5 | 2007-03-07 05:45:54 +0000 | [diff] [blame] | 141 | connectionFigure.setTargetAnchor(new CenteredConnectionAnchor(referenceTypePart.getFigure(), CenteredConnectionAnchor.BOTTOM, 0, 0)); |
| 142 | connectionFigure.setConnectionRouter(new ManhattanConnectionRouter()); |
| 143 | ((CenteredConnectionAnchor)connectionFigure.getSourceAnchor()).setOther((CenteredConnectionAnchor)connectionFigure.getTargetAnchor()); |
kchong | da8feb8 | 2006-07-24 16:16:12 +0000 | [diff] [blame] | 144 | connectionFigure.setHighlight(false); |
kchong | 5f5dca5 | 2007-03-07 05:45:54 +0000 | [diff] [blame] | 145 | |
kchong | da8feb8 | 2006-07-24 16:16:12 +0000 | [diff] [blame] | 146 | } |
| 147 | } |
| 148 | return connectionFigure; |
| 149 | } |
| 150 | |
kchong | d9d1eba | 2007-03-09 19:32:15 +0000 | [diff] [blame] | 151 | protected EditPart getTargetEditPart(IType type) |
kchong | da8feb8 | 2006-07-24 16:16:12 +0000 | [diff] [blame] | 152 | { |
| 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 | } |
kchong | 38cbf17 | 2006-03-29 03:38:21 +0000 | [diff] [blame] | 173 | return null; |
| 174 | } |
kchong | 5f5dca5 | 2007-03-07 05:45:54 +0000 | [diff] [blame] | 175 | |
| 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 | } |
kchong | 38cbf17 | 2006-03-29 03:38:21 +0000 | [diff] [blame] | 232 | } |