| 1 | /* |
| 2 | * Copyright 2007, SDQ, IPD, U KA |
| 3 | */ |
| 4 | package de.uka.ipd.sdq.pcm.gmf.allocation.edit.parts; |
| 5 | |
| 6 | import org.eclipse.draw2d.FigureUtilities; |
| 7 | import org.eclipse.draw2d.Label; |
| 8 | import org.eclipse.draw2d.geometry.Dimension; |
| 9 | import org.eclipse.draw2d.geometry.Rectangle; |
| 10 | import org.eclipse.gef.EditPart; |
| 11 | import org.eclipse.gef.EditPartFactory; |
| 12 | import org.eclipse.gef.tools.CellEditorLocator; |
| 13 | import org.eclipse.gmf.runtime.diagram.ui.editparts.ITextAwareEditPart; |
| 14 | import org.eclipse.gmf.runtime.draw2d.ui.figures.WrapLabel; |
| 15 | import org.eclipse.gmf.runtime.notation.View; |
| 16 | import org.eclipse.jface.viewers.CellEditor; |
| 17 | import org.eclipse.swt.SWT; |
| 18 | import org.eclipse.swt.widgets.Text; |
| 19 | |
| 20 | import de.uka.ipd.sdq.pcm.gmf.allocation.part.PalladioComponentModelVisualIDRegistry; |
| 21 | |
| 22 | /** |
| 23 | * @generated |
| 24 | */ |
| 25 | public class PalladioComponentModelEditPartFactory implements EditPartFactory { |
| 26 | |
| 27 | /** |
| 28 | * @generated |
| 29 | */ |
| 30 | public EditPart createEditPart(EditPart context, Object model) { |
| 31 | if (model instanceof View) { |
| 32 | View view = (View) model; |
| 33 | switch (PalladioComponentModelVisualIDRegistry.getVisualID(view)) { |
| 34 | |
| 35 | case AllocationEditPart.VISUAL_ID: |
| 36 | return new AllocationEditPart(view); |
| 37 | |
| 38 | case ResourceContainerEditPart.VISUAL_ID: |
| 39 | return new ResourceContainerEditPart(view); |
| 40 | |
| 41 | case ResourceContainerEntityNameEditPart.VISUAL_ID: |
| 42 | return new ResourceContainerEntityNameEditPart(view); |
| 43 | |
| 44 | case AllocationContextEditPart.VISUAL_ID: |
| 45 | return new AllocationContextEditPart(view); |
| 46 | |
| 47 | case AllocationContextEntityNameEditPart.VISUAL_ID: |
| 48 | return new AllocationContextEntityNameEditPart(view); |
| 49 | |
| 50 | case AllocationComponentLabelEditPart.VISUAL_ID: |
| 51 | return new AllocationComponentLabelEditPart(view); |
| 52 | |
| 53 | case ResourceContainerAllocationCompartmentEditPart.VISUAL_ID: |
| 54 | return new ResourceContainerAllocationCompartmentEditPart(view); |
| 55 | } |
| 56 | } |
| 57 | return createUnrecognizedEditPart(context, model); |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * @generated |
| 62 | */ |
| 63 | private EditPart createUnrecognizedEditPart(EditPart context, Object model) { |
| 64 | // Handle creation of unrecognized child node EditParts here |
| 65 | return null; |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * @generated |
| 70 | */ |
| 71 | public static CellEditorLocator getTextCellEditorLocator( |
| 72 | ITextAwareEditPart source) { |
| 73 | if (source.getFigure() instanceof WrapLabel) |
| 74 | return new TextCellEditorLocator((WrapLabel) source.getFigure()); |
| 75 | else { |
| 76 | return new LabelCellEditorLocator((Label) source.getFigure()); |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * @generated |
| 82 | */ |
| 83 | static private class TextCellEditorLocator implements CellEditorLocator { |
| 84 | |
| 85 | /** |
| 86 | * @generated |
| 87 | */ |
| 88 | private WrapLabel wrapLabel; |
| 89 | |
| 90 | /** |
| 91 | * @generated |
| 92 | */ |
| 93 | public TextCellEditorLocator(WrapLabel wrapLabel) { |
| 94 | this.wrapLabel = wrapLabel; |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * @generated |
| 99 | */ |
| 100 | public WrapLabel getWrapLabel() { |
| 101 | return wrapLabel; |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * @generated |
| 106 | */ |
| 107 | public void relocate(CellEditor celleditor) { |
| 108 | Text text = (Text) celleditor.getControl(); |
| 109 | Rectangle rect = getWrapLabel().getTextBounds().getCopy(); |
| 110 | getWrapLabel().translateToAbsolute(rect); |
| 111 | if (getWrapLabel().isTextWrapped() |
| 112 | && getWrapLabel().getText().length() > 0) { |
| 113 | rect.setSize(new Dimension(text.computeSize(rect.width, |
| 114 | SWT.DEFAULT))); |
| 115 | } else { |
| 116 | int avr = FigureUtilities.getFontMetrics(text.getFont()) |
| 117 | .getAverageCharWidth(); |
| 118 | rect.setSize(new Dimension(text.computeSize(SWT.DEFAULT, |
| 119 | SWT.DEFAULT)).expand(avr * 2, 0)); |
| 120 | } |
| 121 | if (!rect.equals(new Rectangle(text.getBounds()))) { |
| 122 | text.setBounds(rect.x, rect.y, rect.width, rect.height); |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * @generated |
| 130 | */ |
| 131 | private static class LabelCellEditorLocator implements CellEditorLocator { |
| 132 | |
| 133 | /** |
| 134 | * @generated |
| 135 | */ |
| 136 | private Label label; |
| 137 | |
| 138 | /** |
| 139 | * @generated |
| 140 | */ |
| 141 | public LabelCellEditorLocator(Label label) { |
| 142 | this.label = label; |
| 143 | } |
| 144 | |
| 145 | /** |
| 146 | * @generated |
| 147 | */ |
| 148 | public Label getLabel() { |
| 149 | return label; |
| 150 | } |
| 151 | |
| 152 | /** |
| 153 | * @generated |
| 154 | */ |
| 155 | public void relocate(CellEditor celleditor) { |
| 156 | Text text = (Text) celleditor.getControl(); |
| 157 | Rectangle rect = getLabel().getTextBounds().getCopy(); |
| 158 | getLabel().translateToAbsolute(rect); |
| 159 | int avr = FigureUtilities.getFontMetrics(text.getFont()) |
| 160 | .getAverageCharWidth(); |
| 161 | rect.setSize(new Dimension(text.computeSize(SWT.DEFAULT, |
| 162 | SWT.DEFAULT)).expand(avr * 2, 0)); |
| 163 | if (!rect.equals(new Rectangle(text.getBounds()))) { |
| 164 | text.setBounds(rect.x, rect.y, rect.width, rect.height); |
| 165 | } |
| 166 | } |
| 167 | } |
| 168 | } |