kchong | 38cbf17 | 2006-03-29 03:38:21 +0000 | [diff] [blame] | 1 | /******************************************************************************* |
| 2 | * Copyright (c) 2001, 2006 IBM Corporation and others. |
| 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.adt.design.editparts; |
kchong | 38cbf17 | 2006-03-29 03:38:21 +0000 | [diff] [blame] | 12 | |
| 13 | import org.eclipse.draw2d.IFigure; |
| 14 | import org.eclipse.draw2d.geometry.Point; |
| 15 | import org.eclipse.draw2d.geometry.Rectangle; |
| 16 | import org.eclipse.gef.LayerConstants; |
| 17 | import org.eclipse.gef.commands.Command; |
| 18 | import org.eclipse.gef.requests.DirectEditRequest; |
| 19 | import org.eclipse.swt.widgets.Display; |
kchong | 2be71b3 | 2006-04-11 16:32:03 +0000 | [diff] [blame] | 20 | import org.eclipse.wst.xsd.ui.internal.adt.design.editparts.model.IFeedbackHandler; |
| 21 | import org.eclipse.wst.xsd.ui.internal.adt.design.editpolicies.IADTUpdateCommand; |
| 22 | import org.eclipse.wst.xsd.ui.internal.adt.facade.IType; |
kchong | 38cbf17 | 2006-03-29 03:38:21 +0000 | [diff] [blame] | 23 | |
| 24 | /** |
| 25 | * This class provides some base function to enable drawing connections to a referenced type |
| 26 | * |
| 27 | */ |
| 28 | public abstract class BaseTypeConnectingEditPart extends BaseEditPart implements IFeedbackHandler |
| 29 | { |
| 30 | private TypeReferenceConnection connectionFigure; |
| 31 | |
| 32 | public void activate() |
| 33 | { |
| 34 | super.activate(); |
| 35 | // TODO (cs) revisit this, perhaps has Randy for his advice |
| 36 | // it seems that the edit parts required for the target end of the connections |
| 37 | // are not always layed available when active is called. So we need to delay |
| 38 | // the activateConnection() until a short time later. For now the only way I can think |
| 39 | // of doing this is via an asyncExec. |
| 40 | // |
| 41 | Display.getCurrent().asyncExec(new Runnable() |
| 42 | { |
| 43 | public void run() |
| 44 | { |
| 45 | if (isActive()) |
| 46 | { |
| 47 | activateConnection(); |
| 48 | } |
| 49 | } |
| 50 | }); |
| 51 | } |
| 52 | |
| 53 | public void deactivate() |
| 54 | { |
| 55 | deactivateConnection(); |
| 56 | super.deactivate(); |
| 57 | } |
| 58 | |
| 59 | protected void activateConnection() |
| 60 | { |
| 61 | // If appropriate, create our connectionFigure and add it to the appropriate layer |
| 62 | if (connectionFigure == null && shouldDrawConnection()) |
| 63 | { |
| 64 | connectionFigure = createConnectionFigure(); |
| 65 | if (connectionFigure != null) |
| 66 | { |
| 67 | // Add our editpolicy as a listener on the connection, so it can stay in synch |
| 68 | //connectionFigure.addPropertyChangeListener((AttributeSelectionFeedbackPolicy) getEditPolicy(EditPolicy.SELECTION_FEEDBACK_ROLE)); |
| 69 | //connectionFigure.addMouseListener(this); |
| 70 | getLayer(LayerConstants.CONNECTION_LAYER).add(connectionFigure); |
| 71 | } |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | protected void deactivateConnection() |
| 76 | { |
| 77 | // if we have a connection, remove it |
| 78 | if (connectionFigure != null) |
| 79 | { |
| 80 | getLayer(LayerConstants.CONNECTION_LAYER).remove(connectionFigure); |
| 81 | // Remove our editpolicy listener(s) |
| 82 | //connectionFigure.removePropertyChangeListener((AttributeSelectionFeedbackPolicy) getEditPolicy(EditPolicy.SELECTION_FEEDBACK_ROLE)); |
| 83 | //connectionFigure.removeMouseListener(this); |
| 84 | connectionFigure = null; |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | protected boolean shouldDrawConnection() |
| 89 | { |
| 90 | return true; |
| 91 | } |
| 92 | |
| 93 | public abstract TypeReferenceConnection createConnectionFigure(); |
| 94 | |
| 95 | public void addFeedback() |
| 96 | { |
| 97 | if (connectionFigure != null) |
| 98 | { |
| 99 | connectionFigure.setHighlight(true); |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | public void removeFeedback() |
| 104 | { |
| 105 | if (connectionFigure != null) |
| 106 | { |
| 107 | connectionFigure.setHighlight(false); |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | protected class NameUpdateCommandWrapper extends Command implements IADTUpdateCommand |
| 112 | { |
| 113 | Command command; |
| 114 | protected DirectEditRequest request; |
| 115 | |
| 116 | public NameUpdateCommandWrapper() |
| 117 | { |
| 118 | super("Update Name"); |
| 119 | } |
| 120 | |
| 121 | public void setRequest(DirectEditRequest request) |
| 122 | { |
| 123 | this.request = request; |
| 124 | } |
| 125 | |
| 126 | public void execute() |
| 127 | { |
| 128 | IType iType = (IType)getModel(); |
| 129 | Object newValue = request.getCellEditor().getValue(); |
| 130 | if (newValue instanceof String) |
| 131 | { |
| 132 | command = iType.getUpdateNameCommand((String)newValue); |
| 133 | } |
| 134 | if (command != null) |
| 135 | command.execute(); |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | public boolean hitTest(IFigure target, Point location) |
| 140 | { |
| 141 | Rectangle b = target.getBounds().getCopy(); |
| 142 | target.translateToAbsolute(b); |
| 143 | return b.contains(location); |
| 144 | } |
| 145 | } |