Skip to main content
summaryrefslogtreecommitdiffstats
blob: 1641b7f693638a3d861d11f0ec6c8f38286df9c4 (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
/*
 * Copyright (c) 2004 - 2011 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.features;

import org.eclipse.emf.cdo.dawn.examples.acore.AClass;

import org.eclipse.graphiti.features.IFeatureProvider;
import org.eclipse.graphiti.features.context.IDirectEditingContext;
import org.eclipse.graphiti.features.impl.AbstractDirectEditingFeature;
import org.eclipse.graphiti.mm.algorithms.GraphicsAlgorithm;
import org.eclipse.graphiti.mm.algorithms.Text;
import org.eclipse.graphiti.mm.pictograms.PictogramElement;
import org.eclipse.graphiti.mm.pictograms.Shape;

/**
 * @author Martin Fluegge
 */
public class AcoreDirectEditAClassFeature extends AbstractDirectEditingFeature
{

  public AcoreDirectEditAClassFeature(IFeatureProvider fp)
  {
    super(fp);
  }

  public int getEditingType()
  {
    // there are several possible editor-types supported:
    // text-field, checkbox, color-chooser, combobox, ...
    return TYPE_TEXT;
  }

  @Override
  public boolean canDirectEdit(IDirectEditingContext context)
  {
    PictogramElement pe = context.getPictogramElement();
    Object bo = getBusinessObjectForPictogramElement(pe);
    GraphicsAlgorithm ga = context.getGraphicsAlgorithm();
    // support direct editing, if it is a AClass, and the user clicked
    // directly on the text and not somewhere else in the rectangle
    if (bo instanceof AClass && ga instanceof Text)
    {
      return true;
    }
    // direct editing not supported in all other cases
    return false;
  }

  public String getInitialValue(IDirectEditingContext context)
  {
    // return the current name of the AClass
    PictogramElement pe = context.getPictogramElement();
    AClass AClass = (AClass)getBusinessObjectForPictogramElement(pe);
    return AClass.getName();
  }

  @Override
  public String checkValueValid(String value, IDirectEditingContext context)
  {
    if (value.length() < 1)
    {
      return "Please enter any text as class name.";
    }
    if (value.contains(" "))
    {
      return "Spaces are not allowed in class names.";
    }
    if (value.contains("\n"))
    {
      return "Line breakes are not allowed in class names.";
    }

    // null means, that the value is valid
    return null;
  }

  @Override
  public void setValue(String value, IDirectEditingContext context)
  {
    // set the new name for the AClass
    PictogramElement pe = context.getPictogramElement();
    AClass AClass = (AClass)getBusinessObjectForPictogramElement(pe);
    AClass.setName(value);

    // Explicitly update the shape to display the new value in the diagram
    // Note, that this might not be necessary in future versions of Graphiti
    // (currently in discussion)

    // we know, that pe is the Shape of the Text, so its container is the
    // main shape of the AClass
    updatePictogramElement(((Shape)pe).getContainer());
  }
}

Back to the top