Skip to main content
summaryrefslogtreecommitdiffstats
blob: 146d99e4680da3b89b62ed931c1433f86d077723 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
/*
 * Copyright (c) 2011, 2012 Eike Stepper (Berlin, Germany) and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *  
 * Contributors:
 *     Martin Fluegge - initial API and implementation
 * 
 */
package org.eclipse.emf.cdo.dawn.examples.acore.graphiti.diagram;

import org.eclipse.emf.cdo.dawn.examples.acore.graphiti.features.AcoreCollapseDummyFeature;
import org.eclipse.emf.cdo.dawn.examples.acore.graphiti.features.AcoreRenameAClassFeature;
import org.eclipse.emf.cdo.dawn.examples.acore.graphiti.util.AcoreImageProvider;

import org.eclipse.graphiti.dt.IDiagramTypeProvider;
import org.eclipse.graphiti.features.ICreateConnectionFeature;
import org.eclipse.graphiti.features.context.IDoubleClickContext;
import org.eclipse.graphiti.features.context.IPictogramElementContext;
import org.eclipse.graphiti.features.context.impl.CreateConnectionContext;
import org.eclipse.graphiti.features.context.impl.CustomContext;
import org.eclipse.graphiti.features.custom.ICustomFeature;
import org.eclipse.graphiti.mm.pictograms.Anchor;
import org.eclipse.graphiti.mm.pictograms.AnchorContainer;
import org.eclipse.graphiti.mm.pictograms.PictogramElement;
import org.eclipse.graphiti.services.Graphiti;
import org.eclipse.graphiti.tb.ContextButtonEntry;
import org.eclipse.graphiti.tb.ContextEntryHelper;
import org.eclipse.graphiti.tb.DefaultToolBehaviorProvider;
import org.eclipse.graphiti.tb.IContextButtonEntry;
import org.eclipse.graphiti.tb.IContextButtonPadData;

/**
 * @author Martin Fluegge
 */
public class AcoreToolBehaviorProvider extends DefaultToolBehaviorProvider
{
  public AcoreToolBehaviorProvider(IDiagramTypeProvider dtp)
  {
    super(dtp);
  }

  @Override
  public IContextButtonPadData getContextButtonPad(IPictogramElementContext context)
  {
    IContextButtonPadData data = super.getContextButtonPad(context);
    PictogramElement pe = context.getPictogramElement();

    // 1. set the generic context buttons
    // note, that we do not add 'remove' (just as an example)
    setGenericContextButtons(data, pe, CONTEXT_BUTTON_DELETE | CONTEXT_BUTTON_UPDATE);

    // 2. set the collapse button
    // simply use a dummy custom feature (senseless example)
    CustomContext cc = new CustomContext(new PictogramElement[] { pe });
    ICustomFeature[] cf = getFeatureProvider().getCustomFeatures(cc);
    for (int i = 0; i < cf.length; i++)
    {
      ICustomFeature iCustomFeature = cf[i];
      if (iCustomFeature instanceof AcoreCollapseDummyFeature)
      {
        IContextButtonEntry collapseButton = ContextEntryHelper.createCollapseContextButton(true, iCustomFeature, cc);
        data.setCollapseContextButton(collapseButton);
        break;
      }
    }

    // 3. add one domain specific context-button, which offers all
    // available connection-features as drag&drop features...

    // 3.a. create new CreateConnectionContext
    CreateConnectionContext ccc = new CreateConnectionContext();
    ccc.setSourcePictogramElement(pe);
    Anchor anchor = null;
    if (pe instanceof Anchor)
    {
      anchor = (Anchor)pe;
    }
    else if (pe instanceof AnchorContainer)
    {
      // assume, that our shapes always have chopbox anchors
      anchor = Graphiti.getPeService().getChopboxAnchor((AnchorContainer)pe);
    }
    ccc.setSourceAnchor(anchor);

    // 3.b. create context button and add all applicable features
    ContextButtonEntry button = new ContextButtonEntry(null, context);
    button.setText("Create connection"); //$NON-NLS-1$
    button.setIconId(AcoreImageProvider.IMG_EREFERENCE);
    ICreateConnectionFeature[] features = getFeatureProvider().getCreateConnectionFeatures();
    for (ICreateConnectionFeature feature : features)
    {
      if (feature.isAvailable(ccc) && feature.canStartConnection(ccc))
      {
        button.addDragAndDropFeature(feature);
      }
    }

    // 3.c. add context button, if it contains at least one feature
    if (button.getDragAndDropFeatures().size() > 0)
    {
      data.getDomainSpecificContextButtons().add(button);
    }

    return data;
  }

  //
  // @Override
  // public IContextMenuEntry[] getContextMenu(ICustomContext context)
  // {
  // // create a sub-menu for all custom features
  // ContextMenuEntry subMenu = new ContextMenuEntry(null, context);
  //    subMenu.setText("Cu&stom"); //$NON-NLS-1$
  //    subMenu.setDescription("Custom features submenu"); //$NON-NLS-1$
  // // display sub-menu hierarchical or flat
  // subMenu.setSubmenu(true);
  //
  // // create a menu-entry in the sub-menu for each custom feature
  // ICustomFeature[] customFeatures = getFeatureProvider().getCustomFeatures(context);
  // for (int i = 0; i < customFeatures.length; i++)
  // {
  // ICustomFeature customFeature = customFeatures[i];
  // if (customFeature.isAvailable(context))
  // {
  // ContextMenuEntry menuEntry = new ContextMenuEntry(customFeature, context);
  // subMenu.add(menuEntry);
  // }
  // }
  //
  // IContextMenuEntry ret[] = new IContextMenuEntry[] { subMenu };
  // return ret;
  // }
  //
  // @Override
  // public IPaletteCompartmentEntry[] getPalette()
  // {
  // List<IPaletteCompartmentEntry> ret = new ArrayList<IPaletteCompartmentEntry>();
  //
  // // add compartments from super class
  // IPaletteCompartmentEntry[] superCompartments = super.getPalette();
  // for (int i = 0; i < superCompartments.length; i++)
  // {
  // ret.add(superCompartments[i]);
  // }
  //
  // // add new compartment at the end of the existing compartments
  //    PaletteCompartmentEntry compartmentEntry = new PaletteCompartmentEntry("Stacked", null); //$NON-NLS-1$
  // ret.add(compartmentEntry);
  //
  // // add new stack entry to new compartment
  //    StackEntry stackEntry = new StackEntry("EObject", "EObject", null); //$NON-NLS-1$ //$NON-NLS-2$
  // compartmentEntry.addToolEntry(stackEntry);
  //
  // // add all create-features to the new stack-entry
  // IFeatureProvider featureProvider = getFeatureProvider();
  // ICreateFeature[] createFeatures = featureProvider.getCreateFeatures();
  // for (ICreateFeature cf : createFeatures)
  // {
  // ObjectCreationToolEntry objectCreationToolEntry = new ObjectCreationToolEntry(cf.getCreateName(),
  // cf.getCreateDescription(), cf.getCreateImageId(), cf.getCreateLargeImageId(), cf);
  //
  // stackEntry.addCreationToolEntry(objectCreationToolEntry);
  // }
  //
  // // add all create-connection-features to the new stack-entry
  // ICreateConnectionFeature[] createConnectionFeatures = featureProvider.getCreateConnectionFeatures();
  // for (ICreateConnectionFeature cf : createConnectionFeatures)
  // {
  // ConnectionCreationToolEntry connectionCreationToolEntry = new ConnectionCreationToolEntry(cf.getCreateName(),
  // cf.getCreateDescription(), cf.getCreateImageId(), cf.getCreateLargeImageId());
  // connectionCreationToolEntry.addCreateConnectionFeature(cf);
  // stackEntry.addCreationToolEntry(connectionCreationToolEntry);
  // }
  //
  // return ret.toArray(new IPaletteCompartmentEntry[ret.size()]);
  // }
  //

  @Override
  public ICustomFeature getDoubleClickFeature(IDoubleClickContext context)
  {
    ICustomFeature customFeature = new AcoreRenameAClassFeature(getFeatureProvider());
    // canExecute() tests especially if the context contains a AClass
    if (customFeature.canExecute(context))
    {
      return customFeature;
    }

    return super.getDoubleClickFeature(context);
  }
  //
  // @Override
  // public IDecorator[] getDecorators(PictogramElement pe)
  // {
  // IFeatureProvider featureProvider = getFeatureProvider();
  // Object bo = featureProvider.getBusinessObjectForPictogramElement(pe);
  // if (bo instanceof AClass)
  // {
  // AClass AClass = (AClass)bo;
  // String name = AClass.getName();
  // if (name != null && name.length() > 0 && !(name.charAt(0) >= 'A' && name.charAt(0) <= 'Z'))
  // {
  // IDecorator imageRenderingDecorator = new ImageDecorator(IPlatformImageConstants.IMG_ECLIPSE_WARNING_TSK);
  //        imageRenderingDecorator.setMessage("Name should start with upper case letter"); //$NON-NLS-1$
  // return new IDecorator[] { imageRenderingDecorator };
  // }
  // }
  //
  // return super.getDecorators(pe);
  // }
  //
  // @Override
  // public GraphicsAlgorithm[] getClickArea(PictogramElement pe)
  // {
  // IFeatureProvider featureProvider = getFeatureProvider();
  // Object bo = featureProvider.getBusinessObjectForPictogramElement(pe);
  // if (bo instanceof AClass)
  // {
  // GraphicsAlgorithm invisible = pe.getGraphicsAlgorithm();
  // GraphicsAlgorithm rectangle = invisible.getGraphicsAlgorithmChildren().get(0);
  // return new GraphicsAlgorithm[] { rectangle };
  // }
  // return super.getClickArea(pe);
  // }
  //
  // @Override
  // public GraphicsAlgorithm getSelectionBorder(PictogramElement pe)
  // {
  // if (pe instanceof ContainerShape)
  // {
  // GraphicsAlgorithm invisible = pe.getGraphicsAlgorithm();
  // if (!invisible.getLineVisible())
  // {
  // EList<GraphicsAlgorithm> graphicsAlgorithmChildren = invisible.getGraphicsAlgorithmChildren();
  // if (!graphicsAlgorithmChildren.isEmpty())
  // {
  // return graphicsAlgorithmChildren.get(0);
  // }
  // }
  // }
  // return super.getSelectionBorder(pe);
  // }
  //
  // @Override
  // public String getToolTip(GraphicsAlgorithm ga)
  // {
  // PictogramElement pe = ga.getPictogramElement();
  // Object bo = getFeatureProvider().getBusinessObjectForPictogramElement(pe);
  // if (bo instanceof AClass)
  // {
  // String name = ((AClass)bo).getName();
  // if (name != null && !name.equals(""))
  // {
  // return name;
  // }
  // }
  // return super.getToolTip(ga);
  // }

}

Back to the top