| 1 | /* |
| 2 | *Copyright 2007, SDQ, IPD, University of Karlsruhe |
| 3 | */ |
| 4 | package de.uka.ipd.sdq.pcm.gmf.usage.providers; |
| 5 | |
| 6 | import java.lang.ref.WeakReference; |
| 7 | |
| 8 | import org.eclipse.gef.EditPart; |
| 9 | import org.eclipse.gef.EditPartFactory; |
| 10 | import org.eclipse.gmf.runtime.common.core.service.IOperation; |
| 11 | import org.eclipse.gmf.runtime.diagram.ui.editparts.IGraphicalEditPart; |
| 12 | import org.eclipse.gmf.runtime.diagram.ui.services.editpart.AbstractEditPartProvider; |
| 13 | import org.eclipse.gmf.runtime.diagram.ui.services.editpart.CreateGraphicEditPartOperation; |
| 14 | import org.eclipse.gmf.runtime.diagram.ui.services.editpart.IEditPartOperation; |
| 15 | import org.eclipse.gmf.runtime.notation.View; |
| 16 | |
| 17 | import de.uka.ipd.sdq.pcm.gmf.usage.edit.parts.PalladioComponentModelEditPartFactory; |
| 18 | import de.uka.ipd.sdq.pcm.gmf.usage.edit.parts.UsageScenarioEditPart; |
| 19 | import de.uka.ipd.sdq.pcm.gmf.usage.part.PalladioComponentModelVisualIDRegistry; |
| 20 | |
| 21 | /** |
| 22 | * @generated |
| 23 | */ |
| 24 | public class PalladioComponentModelEditPartProvider extends |
| 25 | AbstractEditPartProvider { |
| 26 | |
| 27 | /** |
| 28 | * @generated |
| 29 | */ |
| 30 | private EditPartFactory factory; |
| 31 | |
| 32 | /** |
| 33 | * @generated |
| 34 | */ |
| 35 | private boolean allowCaching; |
| 36 | |
| 37 | /** |
| 38 | * @generated |
| 39 | */ |
| 40 | private WeakReference cachedPart; |
| 41 | |
| 42 | /** |
| 43 | * @generated |
| 44 | */ |
| 45 | private WeakReference cachedView; |
| 46 | |
| 47 | /** |
| 48 | * @generated |
| 49 | */ |
| 50 | public PalladioComponentModelEditPartProvider() { |
| 51 | setFactory(new PalladioComponentModelEditPartFactory()); |
| 52 | setAllowCaching(true); |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * @generated |
| 57 | */ |
| 58 | public final EditPartFactory getFactory() { |
| 59 | return factory; |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * @generated |
| 64 | */ |
| 65 | protected void setFactory(EditPartFactory factory) { |
| 66 | this.factory = factory; |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * @generated |
| 71 | */ |
| 72 | public final boolean isAllowCaching() { |
| 73 | return allowCaching; |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * @generated |
| 78 | */ |
| 79 | protected synchronized void setAllowCaching(boolean allowCaching) { |
| 80 | this.allowCaching = allowCaching; |
| 81 | if (!allowCaching) { |
| 82 | cachedPart = null; |
| 83 | cachedView = null; |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * @generated |
| 89 | */ |
| 90 | protected IGraphicalEditPart createEditPart(View view) { |
| 91 | EditPart part = factory.createEditPart(null, view); |
| 92 | if (part instanceof IGraphicalEditPart) { |
| 93 | return (IGraphicalEditPart) part; |
| 94 | } |
| 95 | return null; |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * @generated |
| 100 | */ |
| 101 | protected IGraphicalEditPart getCachedPart(View view) { |
| 102 | if (cachedView != null && cachedView.get() == view) { |
| 103 | return (IGraphicalEditPart) cachedPart.get(); |
| 104 | } |
| 105 | return null; |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * @generated |
| 110 | */ |
| 111 | public synchronized IGraphicalEditPart createGraphicEditPart(View view) { |
| 112 | if (isAllowCaching()) { |
| 113 | IGraphicalEditPart part = getCachedPart(view); |
| 114 | cachedPart = null; |
| 115 | cachedView = null; |
| 116 | if (part != null) { |
| 117 | return part; |
| 118 | } |
| 119 | } |
| 120 | return createEditPart(view); |
| 121 | } |
| 122 | |
| 123 | /** |
| 124 | * @generated |
| 125 | */ |
| 126 | public synchronized boolean provides(IOperation operation) { |
| 127 | if (operation instanceof CreateGraphicEditPartOperation) { |
| 128 | View view = ((IEditPartOperation) operation).getView(); |
| 129 | if (!UsageScenarioEditPart.MODEL_ID |
| 130 | .equals(PalladioComponentModelVisualIDRegistry |
| 131 | .getModelID(view))) { |
| 132 | return false; |
| 133 | } |
| 134 | if (isAllowCaching() && getCachedPart(view) != null) { |
| 135 | return true; |
| 136 | } |
| 137 | IGraphicalEditPart part = createEditPart(view); |
| 138 | if (part != null) { |
| 139 | if (isAllowCaching()) { |
| 140 | cachedPart = new WeakReference(part); |
| 141 | cachedView = new WeakReference(view); |
| 142 | } |
| 143 | return true; |
| 144 | } |
| 145 | } |
| 146 | return false; |
| 147 | } |
| 148 | } |