Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
View | Details | Raw Unified | Return to bug 330482 | Differences between
and this patch

Collapse All | Expand All

(-)META-INF/MANIFEST.MF (-1 / +6 lines)
Lines 12-18 Link Here
12
 org.eclipse.wst.xml.ui,
12
 org.eclipse.wst.xml.ui,
13
 org.eclipse.ui,
13
 org.eclipse.ui,
14
 org.eclipse.ui.views,
14
 org.eclipse.ui.views,
15
 org.eclipse.help
15
 org.eclipse.help,
16
 org.eclipse.emf.transaction,
17
 org.eclipse.ui.views.properties.tabbed,
18
 org.eclipse.gef,
19
 org.eclipse.graphiti,
20
 org.eclipse.graphiti.ui
16
Bundle-ActivationPolicy: lazy
21
Bundle-ActivationPolicy: lazy
17
Bundle-RequiredExecutionEnvironment: J2SE-1.5
22
Bundle-RequiredExecutionEnvironment: J2SE-1.5
18
Bundle-Vendor: %providerName
23
Bundle-Vendor: %providerName
(-)plugin.xml (+10 lines)
Lines 146-150 Link Here
146
      id="org.eclipse.sapphire.samples.ezbug.ui.BugDatabaseEditor"
146
      id="org.eclipse.sapphire.samples.ezbug.ui.BugDatabaseEditor"
147
      name="Bug Database Editor (Sapphire Sample)"/>
147
      name="Bug Database Editor (Sapphire Sample)"/>
148
  </extension>
148
  </extension>
149
150
  <extension point="org.eclipse.ui.editors">
151
    <editor
152
      class="org.eclipse.sapphire.samples.map.ui.MapEditor"
153
      default="true"
154
      filenames="map.xml"
155
      id="org.eclipse.sapphire.samples.map.ui.MapEditor"
156
      name="Map Editor"/>
157
  </extension>
158
149
  
159
  
150
</plugin>
160
</plugin>
(-)sdef/MapEditor.sdef (+67 lines)
Added Link Here
1
<definition>
2
3
  <import>
4
    <bundle>org.eclipse.sapphire.samples</bundle>
5
    <package>org.eclipse.sapphire.samples.map</package>
6
    <package>org.eclipse.sapphire.samples.map.ui</package>
7
  </import>
8
  
9
  <diagram-page>
10
    <id>diagram</id>
11
    <page-name>Diagram</page-name>
12
    <page-header-text>Diagram</page-header-text>
13
    <node>
14
      <id>destinations</id>
15
      <tool-palette-label>Destination</tool-palette-label>
16
      <tool-palette-desc>Map Destination</tool-palette-desc>
17
      <property>Destinations</property>
18
      <label>${Name == null ? "&lt;Destination&gt;" : Name}</label>
19
      <instance-id>${Name}</instance-id>
20
    </node>
21
    <connection>
22
      <id>connections</id>
23
      <tool-palette-label>Route</tool-palette-label>
24
      <tool-palette-desc>Map Route</tool-palette-desc>
25
      <property>Routes</property>
26
      <label>${Distance == null ? "&lt;Distance&gt;" : Distance}</label>
27
      <endpoint1>
28
        <property>FromDestination</property>
29
        <value>${Name}</value>
30
        <type>NONE</type>
31
      </endpoint1>
32
      <endpoint2>
33
        <property>ToDestination</property>
34
        <value>${Name}</value>
35
        <type>ARROW</type>
36
      </endpoint2>
37
      <instance-id>${FromDestination}-${ToDestination}</instance-id>
38
    </connection>
39
  </diagram-page>
40
  
41
  <editor-page>
42
    <id>overview</id>
43
    <page-name>Overview</page-name>
44
    <page-header-text>Overview</page-header-text>
45
    <root-node>
46
      <node>
47
        <label>Destinations</label>
48
        <section>
49
          <description>Destinations in the map</description>
50
          <content>
51
            <property-editor>Destinations</property-editor>
52
          </content>
53
        </section>
54
      </node>
55
      <node>
56
        <label>Routes</label>
57
        <section>
58
          <description>Routes in the map</description>
59
          <content>
60
            <property-editor>Routes</property-editor>
61
          </content>
62
        </section>
63
      </node>
64
    </root-node>
65
  </editor-page>
66
</definition>
67
    
(-)src/org/eclipse/sapphire/samples/map/IDestination.java (+30 lines)
Added Link Here
1
package org.eclipse.sapphire.samples.map;
2
3
import org.eclipse.sapphire.modeling.IModelElement;
4
import org.eclipse.sapphire.modeling.ModelElementType;
5
import org.eclipse.sapphire.modeling.Value;
6
import org.eclipse.sapphire.modeling.ValueProperty;
7
import org.eclipse.sapphire.modeling.annotations.GenerateImpl;
8
import org.eclipse.sapphire.modeling.annotations.Label;
9
import org.eclipse.sapphire.modeling.annotations.NonNullValue;
10
import org.eclipse.sapphire.modeling.xml.annotations.XmlBinding;
11
12
@GenerateImpl
13
14
public interface IDestination extends IModelElement 
15
{
16
	ModelElementType TYPE = new ModelElementType( IDestination.class );
17
	
18
    // *** Name ***
19
    
20
    @XmlBinding( path = "name" )
21
    @Label( standard = "name" )
22
    @NonNullValue
23
    //@DefaultValue( text = "Destination" )
24
25
    ValueProperty PROP_NAME = new ValueProperty( TYPE, "Name" );
26
27
    Value<String> getName();
28
    void setName( String name );
29
	
30
}
(-)src/org/eclipse/sapphire/samples/map/IMap.java (+37 lines)
Added Link Here
1
package org.eclipse.sapphire.samples.map;
2
3
import org.eclipse.sapphire.modeling.IModelElement;
4
import org.eclipse.sapphire.modeling.ListProperty;
5
import org.eclipse.sapphire.modeling.ModelElementList;
6
import org.eclipse.sapphire.modeling.ModelElementType;
7
import org.eclipse.sapphire.modeling.annotations.GenerateImpl;
8
import org.eclipse.sapphire.modeling.annotations.Type;
9
import org.eclipse.sapphire.modeling.xml.annotations.XmlListBinding;
10
import org.eclipse.sapphire.modeling.xml.annotations.XmlRootBinding;
11
12
@GenerateImpl
13
@XmlRootBinding( elementName = "map")
14
15
public interface IMap extends IModelElement
16
{
17
	ModelElementType TYPE = new ModelElementType( IMap.class );
18
	
19
    // *** Destinations ***
20
21
    @Type( base = IDestination.class )
22
    @XmlListBinding( mappings = @XmlListBinding.Mapping( element = "destination", type = IDestination.class ) )
23
    
24
    ListProperty PROP_DESTINATIONS = new ListProperty( TYPE, "Destinations" );
25
    
26
    ModelElementList<IDestination> getDestinations();
27
	
28
    // *** Routes ***
29
    
30
    @Type( base = IRoute.class )
31
    @XmlListBinding( mappings = @XmlListBinding.Mapping( element = "route", type = IRoute.class ) )
32
    
33
    ListProperty PROP_ROUTES = new ListProperty( TYPE, "Routes" );
34
    
35
    ModelElementList<IRoute> getRoutes();
36
    
37
}
(-)src/org/eclipse/sapphire/samples/map/IRoute.java (+56 lines)
Added Link Here
1
package org.eclipse.sapphire.samples.map;
2
3
import org.eclipse.sapphire.modeling.IModelElement;
4
import org.eclipse.sapphire.modeling.ModelElementType;
5
import org.eclipse.sapphire.modeling.ReferenceValue;
6
import org.eclipse.sapphire.modeling.Value;
7
import org.eclipse.sapphire.modeling.ValueProperty;
8
import org.eclipse.sapphire.modeling.annotations.GenerateImpl;
9
import org.eclipse.sapphire.modeling.annotations.Label;
10
import org.eclipse.sapphire.modeling.annotations.NonNullValue;
11
import org.eclipse.sapphire.modeling.annotations.Reference;
12
import org.eclipse.sapphire.modeling.xml.annotations.XmlBinding;
13
import org.eclipse.sapphire.samples.map.internal.DestinationReferenceService;
14
15
@GenerateImpl
16
17
public interface IRoute extends IModelElement 
18
{
19
	ModelElementType TYPE = new ModelElementType( IRoute.class );
20
	
21
	// *** FromDestination ***
22
	
23
	@Reference( target = IDestination.class, service = DestinationReferenceService.class )
24
	@XmlBinding( path = "from-destination")
25
	@NonNullValue
26
	@Label(standard = "from destination")
27
28
	ValueProperty PROP_FROM_DESTINATION = new ValueProperty( TYPE, "FromDestination" );
29
30
    ReferenceValue<IDestination> getFromDestination();
31
    void setFromDestination( String name );
32
	
33
	// *** ToDestination ***
34
	
35
	@Reference( target = IDestination.class, service = DestinationReferenceService.class )
36
	@XmlBinding( path = "to-destination")
37
	@NonNullValue
38
	@Label(standard = "to destination")
39
40
	ValueProperty PROP_TO_DESTINATION = new ValueProperty( TYPE, "ToDestination" );
41
42
    ReferenceValue<IDestination> getToDestination();
43
    void setToDestination( String name );
44
    
45
    // *** Distance ***
46
    
47
    @XmlBinding( path = "distance" )
48
    @Label( standard = "distance" )
49
    @NonNullValue
50
51
    ValueProperty PROP_DISTANCE = new ValueProperty( TYPE, "Distance" );
52
53
    Value<String> getDistance();
54
    void setDistance( String distance );
55
    
56
}
(-)src/org/eclipse/sapphire/samples/map/internal/DestinationReferenceService.java (+29 lines)
Added Link Here
1
package org.eclipse.sapphire.samples.map.internal;
2
3
import org.eclipse.sapphire.modeling.ModelElementList;
4
import org.eclipse.sapphire.modeling.ReferenceService;
5
import org.eclipse.sapphire.samples.map.IDestination;
6
import org.eclipse.sapphire.samples.map.IMap;
7
8
public class DestinationReferenceService extends ReferenceService 
9
{
10
11
	@Override
12
	public Object resolve(String reference) 
13
	{
14
		if (reference != null)
15
		{
16
			IMap map = element().nearest(IMap.class);
17
			ModelElementList<IDestination> dests = map.getDestinations();
18
			for (IDestination dest : dests)
19
			{
20
				if (reference.equals(dest.getName().getContent()))
21
				{
22
					return dest;
23
				}
24
			}
25
		}
26
		return null;
27
	}
28
29
}
(-)src/org/eclipse/sapphire/samples/map/ui/MapEditor.java (+83 lines)
Added Link Here
1
package org.eclipse.sapphire.samples.map.ui;
2
3
import org.eclipse.core.runtime.IPath;
4
import org.eclipse.core.runtime.IProgressMonitor;
5
import org.eclipse.core.runtime.Path;
6
import org.eclipse.sapphire.modeling.IModelElement;
7
import org.eclipse.sapphire.modeling.xml.RootXmlResource;
8
import org.eclipse.sapphire.samples.map.IMap;
9
import org.eclipse.sapphire.ui.SapphireEditor;
10
import org.eclipse.sapphire.ui.diagram.editor.SapphireDiagramEditorPart;
11
import org.eclipse.sapphire.ui.diagram.graphiti.editor.SapphireDiagramEditor;
12
import org.eclipse.sapphire.ui.diagram.graphiti.editor.SapphireDiagramEditorInput;
13
import org.eclipse.sapphire.ui.editor.views.masterdetails.MasterDetailsPage;
14
import org.eclipse.sapphire.ui.xml.XmlEditorResourceStore;
15
import org.eclipse.ui.PartInitException;
16
import org.eclipse.ui.part.FileEditorInput;
17
import org.eclipse.wst.sse.ui.StructuredTextEditor;
18
19
public class MapEditor extends SapphireEditor 
20
{
21
	private IMap modelMap;
22
	private StructuredTextEditor mapSourceEditor;
23
	private SapphireDiagramEditorPart mapDiagramPart;
24
	private MasterDetailsPage mapOverviewPage;
25
	
26
    public MapEditor()
27
    {
28
        super( "org.eclipse.sapphire.samples" );
29
    }
30
31
	@Override
32
	protected void createSourcePages() throws PartInitException 
33
	{
34
		this.mapSourceEditor = new StructuredTextEditor();
35
		this.mapSourceEditor.setEditorPart(this);
36
        final FileEditorInput rootEditorInput = (FileEditorInput) getEditorInput();
37
        
38
        int index = addPage( this.mapSourceEditor, rootEditorInput );
39
        setPageText( index, "map.xml" );
40
	}
41
42
	@Override
43
	protected IModelElement createModel() 
44
	{
45
		this.modelMap = IMap.TYPE.instantiate(new RootXmlResource(new XmlEditorResourceStore(this, this.mapSourceEditor)));
46
		return this.modelMap;
47
	}
48
49
	@Override
50
	protected void createDiagramPages() throws PartInitException
51
	{
52
		IPath path = new Path( "org.eclipse.sapphire.samples/sdef/MapEditor.sdef/diagram" );
53
		this.mapDiagramPart = new SapphireDiagramEditorPart(this, this.modelMap, path);
54
		SapphireDiagramEditor diagramEditor = this.mapDiagramPart.getDiagramEditor();
55
		SapphireDiagramEditorInput diagramEditorInput = this.mapDiagramPart.getDiagramEditorInput();
56
		addPage(0, diagramEditor, diagramEditorInput);
57
		setPageText( 0, "Diagram" );
58
		setPageId(this.pages.get(0), "Diagram");
59
	}
60
	
61
	@Override
62
	protected void createFormPages() throws PartInitException 
63
	{
64
		IPath path = new Path( "org.eclipse.sapphire.samples/sdef/MapEditor.sdef/overview" );
65
		this.mapOverviewPage = new MasterDetailsPage(this, this.modelMap, path);
66
        addPage(1, this.mapOverviewPage);
67
        setPageText(1, "Overview");
68
        setPageId(this.pages.get(1), "Overview");		
69
	}
70
71
	public IMap getMap()
72
	{
73
		return this.modelMap;
74
	}
75
	
76
	@Override
77
	public void doSave( final IProgressMonitor monitor )
78
	{
79
		super.doSave(monitor);
80
		SapphireDiagramEditor diagramEditor = this.mapDiagramPart.getDiagramEditor();
81
		diagramEditor.doSave(monitor);
82
	}
83
}
(-)META-INF/MANIFEST.MF (-2 / +11 lines)
Lines 19-25 Link Here
19
 org.eclipse.sapphire.ui.swt.renderer,
19
 org.eclipse.sapphire.ui.swt.renderer,
20
 org.eclipse.sapphire.ui.swt.renderer.actions,
20
 org.eclipse.sapphire.ui.swt.renderer.actions,
21
 org.eclipse.sapphire.ui.util,
21
 org.eclipse.sapphire.ui.util,
22
 org.eclipse.sapphire.ui.xml
22
 org.eclipse.sapphire.ui.xml,
23
 org.eclipse.sapphire.ui.diagram.editor,
24
 org.eclipse.sapphire.ui.diagram.graphiti.editor
23
Require-Bundle: org.eclipse.debug.ui,
25
Require-Bundle: org.eclipse.debug.ui,
24
 org.eclipse.jdt.ui,
26
 org.eclipse.jdt.ui,
25
 org.eclipse.ui.forms;visibility:=reexport,
27
 org.eclipse.ui.forms;visibility:=reexport,
Lines 37-42 Link Here
37
 org.eclipse.help,
39
 org.eclipse.help,
38
 org.eclipse.ui.views,
40
 org.eclipse.ui.views,
39
 org.eclipse.jface.text,
41
 org.eclipse.jface.text,
40
 org.apache.ant
42
 org.apache.ant,
43
 org.eclipse.gef,
44
 org.eclipse.graphiti,
45
 org.eclipse.graphiti.mm,
46
 org.eclipse.graphiti.pattern,
47
 org.eclipse.graphiti.ui,
48
 org.eclipse.emf.transaction,
49
 org.eclipse.ui.views.properties.tabbed
41
Bundle-ActivationPolicy: lazy
50
Bundle-ActivationPolicy: lazy
42
Bundle-RequiredExecutionEnvironment: J2SE-1.5
51
Bundle-RequiredExecutionEnvironment: J2SE-1.5
(-)plugin.properties (+4 lines)
Lines 15-17 Link Here
15
showInSourceCommandName = Show in Source
15
showInSourceCommandName = Show in Source
16
showInSourceCommandDescription = Shows the location in the source file that corresponds to the selected entity.
16
showInSourceCommandDescription = Shows the location in the source file that corresponds to the selected entity.
17
builderName = Sapphire UI Builder
17
builderName = Sapphire UI Builder
18
sapphireDiagramTypeDescription = Sapphire Diagram Type
19
sapphireDiagramTypeName = Sapphire Diagram Type
20
sapphireDiagramTypeProviderDescription = Sapphire Diagram Type Provider
21
sapphireDiagramTypeProviderName = Sapphire Diagram Type Provider
(-)plugin.xml (+19 lines)
Lines 24-27 Link Here
24
    </builder> 
24
    </builder> 
25
  </extension> 
25
  </extension> 
26
  
26
  
27
   <extension point="org.eclipse.graphiti.ui.diagramTypes">
28
      <diagramType
29
            description="%sapphireDiagramTypeDescription"
30
            id="org.eclipse.sapphire.ui.SapphireDiagramType"
31
            name="%sapphireDiagramTypeName"
32
            type="sapphireDiagram">
33
      </diagramType>
34
   </extension>
35
   
36
   <extension point="org.eclipse.graphiti.ui.diagramTypeProviders">
37
      <diagramTypeProvider
38
            class="org.eclipse.sapphire.ui.diagram.graphiti.providers.SapphireDiagramTypeProvider"
39
            description="%sapphireDiagramTypeProviderDescription"
40
            id="org.eclipse.sapphire.ui.sapphireDiagramTypeProvider"
41
            name="%sapphireDiagramTypeProviderName">
42
         <diagramType id="org.eclipse.sapphire.ui.SapphireDiagramType"/>
43
      </diagramTypeProvider>
44
   </extension>
45
     
27
</plugin>
46
</plugin>
(-)src/org/eclipse/sapphire/ui/SapphireEditor.java (+9 lines)
Lines 18-23 Link Here
18
import java.net.URI;
18
import java.net.URI;
19
import java.util.Collections;
19
import java.util.Collections;
20
import java.util.HashMap;
20
import java.util.HashMap;
21
import java.util.Iterator;
21
import java.util.Map;
22
import java.util.Map;
22
import java.util.Set;
23
import java.util.Set;
23
24
Lines 285-290 Link Here
285
                this.model = createModel();
286
                this.model = createModel();
286
                adaptModel( this.model );
287
                adaptModel( this.model );
287
                
288
                
289
                createDiagramPages();
290
                
288
                createFormPages();
291
                createFormPages();
289
    
292
    
290
                createFileChangeListener();
293
                createFileChangeListener();
Lines 330-335 Link Here
330
    protected abstract void createSourcePages() throws PartInitException;
333
    protected abstract void createSourcePages() throws PartInitException;
331
    protected abstract void createFormPages() throws PartInitException;
334
    protected abstract void createFormPages() throws PartInitException;
332
    
335
    
336
    // default impl does nothing, subclass may override it to add diagram pages
337
    protected void createDiagramPages() throws PartInitException
338
    {
339
    	
340
    }
341
    
333
    protected final void setPageId( final Object page,
342
    protected final void setPageId( final Object page,
334
                                    final String id )
343
                                    final String id )
335
    {
344
    {
(-)src/org/eclipse/sapphire/ui/def/ISapphireUiDef.java (+10 lines)
Lines 26-31 Link Here
26
import org.eclipse.sapphire.modeling.xml.annotations.XmlListBinding;
26
import org.eclipse.sapphire.modeling.xml.annotations.XmlListBinding;
27
import org.eclipse.sapphire.modeling.xml.annotations.XmlRootBinding;
27
import org.eclipse.sapphire.modeling.xml.annotations.XmlRootBinding;
28
import org.eclipse.sapphire.ui.def.internal.SapphireUiDefMethods;
28
import org.eclipse.sapphire.ui.def.internal.SapphireUiDefMethods;
29
import org.eclipse.sapphire.ui.diagram.def.IDiagramPageDef;
29
30
30
/**
31
/**
31
 * @author <a href="mailto:konstantin.komissarchik@oracle.com">Konstantin Komissarchik</a>
32
 * @author <a href="mailto:konstantin.komissarchik@oracle.com">Konstantin Komissarchik</a>
Lines 129-134 Link Here
129
    
130
    
130
    ModelElementList<IEditorPageDef> getEditorPageDefs();
131
    ModelElementList<IEditorPageDef> getEditorPageDefs();
131
    
132
    
133
    // *** DiagramPageDefs ***
134
    
135
    @Type( base = IDiagramPageDef.class )
136
    @XmlListBinding( mappings = @XmlListBinding.Mapping( element = "diagram-page", type = IDiagramPageDef.class ) )
137
    
138
    ListProperty PROP_DIAGRAM_PAGE_DEFS = new ListProperty( TYPE, "DiagramPageDefs" );
139
    
140
    ModelElementList<IDiagramPageDef> getDiagramPageDefs();
141
    
132
    // *** DialogDefs ***
142
    // *** DialogDefs ***
133
    
143
    
134
    @Type( base = ISapphireDialogDef.class )
144
    @Type( base = ISapphireDialogDef.class )
(-)src/org/eclipse/sapphire/ui/def/SapphireDiagramDefFactory.java (+51 lines)
Added Link Here
1
package org.eclipse.sapphire.ui.def;
2
3
import org.eclipse.sapphire.modeling.BundleResourceStore;
4
import org.eclipse.sapphire.modeling.ResourceStoreException;
5
import org.eclipse.sapphire.modeling.SharedModelsCache;
6
import org.eclipse.sapphire.modeling.xml.RootXmlResource;
7
import org.eclipse.sapphire.modeling.xml.XmlResourceStore;
8
import org.eclipse.sapphire.ui.diagram.def.IDiagramPageDef;
9
import org.eclipse.sapphire.ui.internal.SapphireUiFrameworkPlugin;
10
11
public class SapphireDiagramDefFactory 
12
{
13
	public static IDiagramPageDef load(final String bundleId, final String path)
14
	{
15
        try
16
        {
17
            return load( new XmlResourceStore( new BundleResourceStore( bundleId, path ) ), false );
18
        }
19
        catch( ResourceStoreException e )
20
        {
21
            SapphireUiFrameworkPlugin.log( e );
22
            return null;
23
        }		
24
	}
25
	
26
	public static IDiagramPageDef load( final XmlResourceStore resourceStore,
27
            							final boolean writable )
28
	{
29
		IDiagramPageDef model;
30
31
		if( writable )
32
		{
33
			model = IDiagramPageDef.TYPE.instantiate( new RootXmlResource( resourceStore ) );
34
		}
35
		else
36
		{
37
			final SharedModelsCache.StandardKey key = new SharedModelsCache.StandardKey( resourceStore, IDiagramPageDef.TYPE );
38
			
39
			model = (IDiagramPageDef) SharedModelsCache.retrieve( key );
40
			
41
			if( model == null )
42
			{
43
				model = IDiagramPageDef.TYPE.instantiate( new RootXmlResource( resourceStore ) );
44
				SharedModelsCache.store( key, model );			
45
			}	
46
		}
47
		return model;
48
	}
49
50
}
51
(-)src/org/eclipse/sapphire/ui/diagram/def/ConnectionEndpointType.java (+10 lines)
Added Link Here
1
package org.eclipse.sapphire.ui.diagram.def;
2
3
4
public enum ConnectionEndpointType 
5
{
6
	NONE,
7
	ARROW,
8
	CIRCLE,
9
	ELLIPSE;
10
}
(-)src/org/eclipse/sapphire/ui/diagram/def/IDiagramConnectionDef.java (+102 lines)
Added Link Here
1
package org.eclipse.sapphire.ui.diagram.def;
2
3
import org.eclipse.sapphire.modeling.ElementProperty;
4
import org.eclipse.sapphire.modeling.ModelElementHandle;
5
import org.eclipse.sapphire.modeling.ModelElementType;
6
import org.eclipse.sapphire.modeling.Value;
7
import org.eclipse.sapphire.modeling.ValueProperty;
8
import org.eclipse.sapphire.modeling.annotations.GenerateImpl;
9
import org.eclipse.sapphire.modeling.annotations.Label;
10
import org.eclipse.sapphire.modeling.annotations.Type;
11
import org.eclipse.sapphire.modeling.xml.annotations.XmlBinding;
12
import org.eclipse.sapphire.ui.def.ISapphirePartDef;
13
14
@GenerateImpl
15
16
public interface IDiagramConnectionDef 
17
	
18
	extends ISapphirePartDef 
19
	
20
{
21
	ModelElementType TYPE = new ModelElementType( IDiagramConnectionDef.class );
22
	
23
    // *** Id ***
24
    
25
    @Label( standard = "ID" )
26
    @XmlBinding( path = "id" )
27
    
28
    ValueProperty PROP_ID = new ValueProperty( TYPE, "Id" );
29
    
30
    Value<String> getId();
31
    void setId( String id );
32
    
33
    // *** Property ***
34
    
35
    @Label( standard = "property" )
36
    @XmlBinding( path = "property" )
37
    
38
    ValueProperty PROP_PROPERTY = new ValueProperty( TYPE, "Property" );
39
    
40
    Value<String> getProperty();
41
    void setProperty( String property );
42
    
43
    // *** ToolPaletteLabel ***
44
    
45
    @Label( standard = "tool palette label" )
46
    @XmlBinding( path = "tool-palette-label" )
47
    
48
    ValueProperty PROP_TOOL_PALETTE_LABEL = new ValueProperty( TYPE, "ToolPaletteLabel" );
49
    
50
    Value<String> getToolPaletteLabel();
51
    void setToolPaletteLabel( String paletteLabel );
52
    
53
    // *** ToolPaletteDesc ***
54
    
55
    @Label( standard = "tool palette description" )
56
    @XmlBinding( path = "tool-palette-desc" )
57
    
58
    ValueProperty PROP_TOOL_PALETTE_DESC = new ValueProperty( TYPE, "ToolPaletteDesc" );
59
    
60
    Value<String> getToolPaletteDesc();
61
    void setToolPaletteDesc( String paletteDesc );    
62
        
63
    // *** Label ***
64
    
65
    @Label( standard = "label" )
66
    @XmlBinding( path = "label" )
67
    
68
    ValueProperty PROP_LABEL = new ValueProperty( TYPE, "Label" );
69
    
70
    Value<String> getLabel();
71
    void setLabel( String label );
72
    
73
    // *** Endpoint1 ***
74
    
75
    @Type( base = IDiagramConnectionEndpointDef.class )
76
    @XmlBinding( path = "endpoint1" )
77
78
    ElementProperty PROP_ENDPOINT_1 = new ElementProperty( TYPE, "Endpoint1" );
79
    
80
    ModelElementHandle<IDiagramConnectionEndpointDef> getEndpoint1();
81
82
    // *** Endpoint2 ***
83
    
84
    @Type( base = IDiagramConnectionEndpointDef.class )
85
    @XmlBinding( path = "endpoint2" )
86
87
    ElementProperty PROP_ENDPOINT_2 = new ElementProperty( TYPE, "Endpoint2" );
88
    
89
    ModelElementHandle<IDiagramConnectionEndpointDef> getEndpoint2();
90
    
91
    // *** InstanceId ***
92
    
93
    @Label( standard = "instance id" )
94
    @XmlBinding( path = "instance-id" )
95
    
96
    ValueProperty PROP_INSTANCE_ID = new ValueProperty( TYPE, "InstanceId" );
97
    
98
    Value<String> getInstanceId();
99
    void setInstanceId( String instanceId );
100
    
101
    
102
}
(-)src/org/eclipse/sapphire/ui/diagram/def/IDiagramConnectionEndpointDef.java (+54 lines)
Added Link Here
1
package org.eclipse.sapphire.ui.diagram.def;
2
3
import org.eclipse.sapphire.modeling.ModelElementType;
4
import org.eclipse.sapphire.modeling.Value;
5
import org.eclipse.sapphire.modeling.ValueProperty;
6
import org.eclipse.sapphire.modeling.annotations.GenerateImpl;
7
import org.eclipse.sapphire.modeling.annotations.Label;
8
import org.eclipse.sapphire.modeling.annotations.Type;
9
import org.eclipse.sapphire.modeling.xml.annotations.XmlBinding;
10
import org.eclipse.sapphire.ui.def.ISapphirePartDef;
11
12
@GenerateImpl
13
14
public interface IDiagramConnectionEndpointDef 
15
16
	extends ISapphirePartDef 
17
	
18
{
19
	ModelElementType TYPE = new ModelElementType( IDiagramConnectionEndpointDef.class );
20
	
21
    // *** Property ***
22
    
23
    @Label( standard = "property" )
24
    @XmlBinding( path = "property" )
25
    
26
    ValueProperty PROP_PROPERTY = new ValueProperty( TYPE, "Property" );
27
    
28
    Value<String> getProperty();
29
    void setProperty( String property );
30
    
31
    // *** Value ***
32
    
33
    @Label( standard = "value" )
34
    @XmlBinding( path = "value" )
35
    
36
    ValueProperty PROP_VALUE = new ValueProperty( TYPE, "Value" );
37
    
38
    Value<String> getValue();
39
    void setValue( String value );
40
    
41
    
42
	// *** Type ***
43
	
44
	@Type( base = ConnectionEndpointType.class )
45
	@XmlBinding( path = "connectionDecoratorType" )
46
	
47
	ValueProperty PROP_TYPE = new ValueProperty( TYPE, "Type" );
48
	
49
	Value<ConnectionEndpointType> getType();
50
	void setType( String value );
51
	void setType( ConnectionEndpointType value );
52
    
53
	
54
}
(-)src/org/eclipse/sapphire/ui/diagram/def/IDiagramNodeDef.java (+79 lines)
Added Link Here
1
package org.eclipse.sapphire.ui.diagram.def;
2
3
import org.eclipse.sapphire.modeling.ModelElementType;
4
import org.eclipse.sapphire.modeling.Value;
5
import org.eclipse.sapphire.modeling.ValueProperty;
6
import org.eclipse.sapphire.modeling.annotations.GenerateImpl;
7
import org.eclipse.sapphire.modeling.annotations.Label;
8
import org.eclipse.sapphire.modeling.xml.annotations.XmlBinding;
9
import org.eclipse.sapphire.ui.def.ISapphirePartDef;
10
11
@GenerateImpl
12
13
public interface IDiagramNodeDef 
14
15
	extends ISapphirePartDef
16
	
17
{
18
	ModelElementType TYPE = new ModelElementType( IDiagramNodeDef.class );
19
	
20
    // *** Id ***
21
    
22
    @Label( standard = "ID" )
23
    @XmlBinding( path = "id" )
24
    
25
    ValueProperty PROP_ID = new ValueProperty( TYPE, "Id" );
26
    
27
    Value<String> getId();
28
    void setId( String id );
29
    
30
    // *** ToolPaletteLabel ***
31
    
32
    @Label( standard = "tool palette label" )
33
    @XmlBinding( path = "tool-palette-label" )
34
    
35
    ValueProperty PROP_TOOL_PALETTE_LABEL = new ValueProperty( TYPE, "ToolPaletteLabel" );
36
    
37
    Value<String> getToolPaletteLabel();
38
    void setToolPaletteLabel( String paletteLabel );
39
    
40
    // *** ToolPaletteDesc ***
41
    
42
    @Label( standard = "tool palette description" )
43
    @XmlBinding( path = "tool-palette-desc" )
44
    
45
    ValueProperty PROP_TOOL_PALETTE_DESC = new ValueProperty( TYPE, "ToolPaletteDesc" );
46
    
47
    Value<String> getToolPaletteDesc();
48
    void setToolPaletteDesc( String paletteDesc );
49
50
    // *** Property ***
51
    
52
    @Label( standard = "property" )
53
    @XmlBinding( path = "property" )
54
    
55
    ValueProperty PROP_PROPERTY = new ValueProperty( TYPE, "Property" );
56
    
57
    Value<String> getProperty();
58
    void setProperty( String property );
59
        
60
    // *** Label ***
61
    
62
    @Label( standard = "label" )
63
    @XmlBinding( path = "label" )
64
    
65
    ValueProperty PROP_LABEL = new ValueProperty( TYPE, "Label" );
66
    
67
    Value<String> getLabel();
68
    void setLabel( String label );
69
	
70
    // *** InstanceId ***
71
    
72
    @Label( standard = "instance id" )
73
    @XmlBinding( path = "instance-id" )
74
    
75
    ValueProperty PROP_INSTANCE_ID = new ValueProperty( TYPE, "InstanceId" );
76
    
77
    Value<String> getInstanceId();
78
    void setInstanceId( String instanceId );
79
}
(-)src/org/eclipse/sapphire/ui/diagram/def/IDiagramPageDef.java (+75 lines)
Added Link Here
1
package org.eclipse.sapphire.ui.diagram.def;
2
3
import org.eclipse.sapphire.modeling.ListProperty;
4
import org.eclipse.sapphire.modeling.ModelElementList;
5
import org.eclipse.sapphire.modeling.ModelElementType;
6
import org.eclipse.sapphire.modeling.Value;
7
import org.eclipse.sapphire.modeling.ValueProperty;
8
import org.eclipse.sapphire.modeling.annotations.DefaultValue;
9
import org.eclipse.sapphire.modeling.annotations.GenerateImpl;
10
import org.eclipse.sapphire.modeling.annotations.Label;
11
import org.eclipse.sapphire.modeling.annotations.Type;
12
import org.eclipse.sapphire.modeling.xml.annotations.XmlBinding;
13
import org.eclipse.sapphire.modeling.xml.annotations.XmlListBinding;
14
import org.eclipse.sapphire.ui.def.ISapphirePartDef;
15
16
@GenerateImpl
17
18
public interface IDiagramPageDef 
19
	
20
	extends ISapphirePartDef
21
	
22
{
23
	ModelElementType TYPE = new ModelElementType( IDiagramPageDef.class);
24
	
25
    // *** Id ***
26
    
27
    @Label( standard = "ID" )
28
    @XmlBinding( path = "id" )
29
    
30
    ValueProperty PROP_ID = new ValueProperty( TYPE, "Id" );
31
    
32
    Value<String> getId();
33
    void setId( String id );
34
    
35
    // *** PageName ***
36
    
37
    @Label( standard = "page name" )
38
    @DefaultValue( text = "Diagram" )
39
    @XmlBinding( path = "page-name" )
40
    
41
    ValueProperty PROP_PAGE_NAME = new ValueProperty( TYPE, "PageName" );
42
    
43
    Value<String> getPageName();
44
    void setPageName( String pageName );
45
    
46
    // *** PageHeaderText ***
47
    
48
    @Label( standard = "page header text" )
49
    @DefaultValue( text = "Diagram View" )
50
    @XmlBinding( path = "page-header-text" )
51
    
52
    ValueProperty PROP_PAGE_HEADER_TEXT = new ValueProperty( TYPE, "PageHeaderText" );
53
    
54
    Value<String> getPageHeaderText();
55
    void setPageHeaderText( String pageHeaderText );
56
	
57
	// *** DiagramNodeDefs ***
58
    
59
    @Type( base = IDiagramNodeDef.class )
60
    @XmlListBinding( mappings = @XmlListBinding.Mapping( element = "node", type = IDiagramNodeDef.class ) )
61
                             
62
    ListProperty PROP_DIAGRAM_NODE_DEFS = new ListProperty( TYPE, "DiagramNodeDefs" );
63
    
64
    ModelElementList<IDiagramNodeDef> getDiagramNodeDefs();
65
    
66
	// *** DiagramConnectionDefs ***
67
    
68
    @Type( base = IDiagramConnectionDef.class )
69
    @XmlListBinding( mappings = @XmlListBinding.Mapping( element = "connection", type = IDiagramConnectionDef.class ) )
70
                             
71
    ListProperty PROP_DIAGRAM_CONNECTION_DEFS = new ListProperty( TYPE, "DiagramConnectionDefs" );
72
    
73
    ModelElementList<IDiagramConnectionDef> getDiagramConnectionDefs();
74
	
75
}
(-)src/org/eclipse/sapphire/ui/diagram/editor/DiagramConnectionPart.java (+231 lines)
Added Link Here
1
package org.eclipse.sapphire.ui.diagram.editor;
2
3
import org.eclipse.sapphire.modeling.IModelElement;
4
import org.eclipse.sapphire.modeling.ModelElementType;
5
import org.eclipse.sapphire.modeling.ModelProperty;
6
import org.eclipse.sapphire.modeling.ReferenceValue;
7
import org.eclipse.sapphire.modeling.Value;
8
import org.eclipse.sapphire.modeling.ValueProperty;
9
import org.eclipse.sapphire.modeling.el.Function;
10
import org.eclipse.sapphire.ui.SapphirePart;
11
import org.eclipse.sapphire.ui.SapphireRenderingContext;
12
import org.eclipse.sapphire.ui.diagram.def.IDiagramConnectionDef;
13
import org.eclipse.sapphire.ui.diagram.def.IDiagramConnectionEndpointDef;
14
15
public class DiagramConnectionPart extends SapphirePart 
16
{
17
	private DiagramConnectionTemplate connectionTemplate;
18
	private IDiagramConnectionDef definition;
19
	private IModelElement modelElement;
20
	private IDiagramConnectionEndpointDef endpoint1Def;
21
	private IDiagramConnectionEndpointDef endpoint2Def;
22
	private IModelElement endpoint1Model;
23
	private IModelElement endpoint2Model;
24
	private Function labelFunction;
25
	private ValueProperty labelProperty;
26
	private Function idFunction;
27
	private Function endpoint1Function;
28
	private Function endpoint2Function;
29
	
30
	public DiagramConnectionPart(DiagramConnectionTemplate connectionTemplate)
31
	{
32
		this.connectionTemplate = connectionTemplate;
33
	}
34
	
35
    @Override
36
    protected void init()
37
    {
38
        super.init();
39
        
40
        this.definition = (IDiagramConnectionDef)super.definition;
41
        this.modelElement = getModelElement();
42
        this.labelFunction = initExpression
43
        ( 
44
            this.definition.getLabel().getLocalizedText(), 
45
            new Runnable()
46
            {
47
                public void run()
48
                {
49
                }
50
            }
51
        );
52
        
53
        this.labelProperty = FunctionUtil.getFunctionProperty(this.modelElement, this.labelFunction);
54
        if (this.labelProperty == null)
55
        {
56
        	throw new RuntimeException( "Label function does not contain a property.");
57
        }
58
        
59
        this.idFunction = initExpression
60
        ( 
61
            this.definition.getInstanceId().getLocalizedText(), 
62
            new Runnable()
63
            {
64
                public void run()
65
                {
66
                }
67
            }
68
        );
69
        
70
        this.endpoint1Def = this.definition.getEndpoint1().element();
71
        this.endpoint2Def = this.definition.getEndpoint2().element();
72
        this.endpoint1Model = processEndpoint(this.endpoint1Def);
73
        this.endpoint2Model = processEndpoint(this.endpoint2Def);
74
        this.endpoint1Function = initExpression
75
        (
76
        	this.endpoint1Model, 
77
        	this.endpoint1Def.getValue().getContent(), 
78
            new Runnable()
79
        	{
80
	            public void run()
81
	            {
82
	            	refreshEndpoint1();
83
	            }
84
        	}
85
        );
86
        this.endpoint2Function = initExpression
87
        (
88
        	this.endpoint2Model, 
89
        	this.endpoint2Def.getValue().getContent(), 
90
            new Runnable()
91
        	{
92
	            public void run()
93
	            {
94
	            	refreshEndpoint2();
95
	            }
96
        	}
97
        );
98
    }
99
    
100
    public DiagramConnectionTemplate getDiagramConnectionTemplate()
101
    {
102
    	return this.connectionTemplate;
103
    }
104
    
105
    public IModelElement getLocalModelElement()
106
    {
107
        return this.modelElement;
108
    }    
109
    
110
    public IModelElement getEndpoint1()
111
    {
112
    	return this.endpoint1Model;
113
    }
114
    
115
    public IModelElement getEndpoint2()
116
    {
117
    	return this.endpoint2Model;
118
    }
119
120
    public String getLabel()
121
	{
122
        String label = null;
123
        
124
        if( this.labelFunction != null )
125
        {
126
            label = (String) this.labelFunction.value();
127
        }
128
        
129
        if( label == null )
130
        {
131
            label = "#null#";
132
        }
133
        
134
        return label;
135
	}
136
    
137
    public void setLabel(String newValue)
138
    {
139
		if (this.labelProperty != null)
140
		{
141
			this.modelElement.write(this.labelProperty, newValue);
142
		}    	
143
    }
144
    
145
    public String getInstanceId()
146
	{
147
        String id = null;
148
        
149
        if( this.idFunction != null )
150
        {
151
            id = (String) this.idFunction.value();
152
        }
153
        
154
        if( id == null )
155
        {
156
            id = "#null#";
157
        }
158
        
159
        return id;
160
	}
161
162
    @Override
163
	public void render(SapphireRenderingContext context)
164
	{
165
		throw new UnsupportedOperationException();
166
	}
167
	
168
	public void refreshEndpoint1()
169
	{
170
		if (this.endpoint1Function != null)
171
		{
172
			Object value = this.endpoint1Function.value();
173
			String property = this.endpoint1Def.getProperty().getContent();
174
			setModelProperty(this.modelElement, property, value);
175
		}		
176
	}
177
	
178
	public void refreshEndpoint2()
179
	{
180
		if (this.endpoint2Function != null)
181
		{
182
			Object value = this.endpoint2Function.value();
183
			String property = this.endpoint2Def.getProperty().getContent();
184
			setModelProperty(this.modelElement, property, value);
185
		}		
186
	}
187
	
188
	private IModelElement processEndpoint(IDiagramConnectionEndpointDef endpointDef)
189
	{
190
		String propertyName = endpointDef.getProperty().getContent();
191
		ModelProperty modelProperty = resolve(this.modelElement, propertyName);
192
        if (!(modelProperty instanceof ValueProperty))
193
        {
194
        	throw new RuntimeException( "Property " + propertyName + " not a ValueProperty");
195
        }
196
        ValueProperty property = (ValueProperty)modelProperty;
197
		Value<?> valObj = this.modelElement.read(property);
198
		if (!(valObj instanceof ReferenceValue))
199
		{
200
			throw new RuntimeException( "Property " + propertyName + " value not a reference");
201
		}
202
		ReferenceValue<?> refVal = (ReferenceValue<?>)valObj;
203
		Object targetObj = refVal.resolve();
204
		if (!(targetObj instanceof IModelElement))
205
		{
206
			throw new RuntimeException( "Target not an IModelElement");
207
		}
208
		return (IModelElement)targetObj;
209
	}
210
	
211
    private void setModelProperty(final IModelElement modelElement, 
212
			String propertyName, Object value)
213
	{
214
		if (propertyName != null)
215
		{
216
			final ModelElementType type = modelElement.getModelElementType();
217
			final ModelProperty property = type.getProperty( propertyName );
218
			if( property == null )
219
			{
220
				throw new RuntimeException( "Could not find property " + propertyName + " in " + type.getQualifiedName() );
221
			}
222
			if (!(property instanceof ValueProperty))
223
			{
224
				throw new RuntimeException( "Property " + propertyName + " not a ValueProperty");
225
			}
226
		
227
			modelElement.write((ValueProperty)property, value);
228
		}    	
229
	}
230
	
231
}
(-)src/org/eclipse/sapphire/ui/diagram/editor/DiagramConnectionTemplate.java (+186 lines)
Added Link Here
1
package org.eclipse.sapphire.ui.diagram.editor;
2
3
import java.util.ArrayList;
4
import java.util.Collections;
5
import java.util.List;
6
7
import org.eclipse.sapphire.modeling.ElementProperty;
8
import org.eclipse.sapphire.modeling.IModelElement;
9
import org.eclipse.sapphire.modeling.ListProperty;
10
import org.eclipse.sapphire.modeling.ModelElementList;
11
import org.eclipse.sapphire.modeling.ModelElementType;
12
import org.eclipse.sapphire.modeling.ModelProperty;
13
import org.eclipse.sapphire.modeling.Value;
14
import org.eclipse.sapphire.modeling.ValueProperty;
15
import org.eclipse.sapphire.modeling.el.Function;
16
import org.eclipse.sapphire.modeling.el.FunctionContext;
17
import org.eclipse.sapphire.modeling.el.ModelElementFunctionContext;
18
import org.eclipse.sapphire.modeling.el.parser.ExpressionLanguageParser;
19
import org.eclipse.sapphire.ui.diagram.def.IDiagramConnectionDef;
20
import org.eclipse.sapphire.ui.diagram.def.IDiagramConnectionEndpointDef;
21
import org.eclipse.sapphire.ui.internal.SapphireUiFrameworkPlugin;
22
23
public class DiagramConnectionTemplate 
24
{
25
	private SapphireDiagramEditorPart diagramEditor;
26
	private IDiagramConnectionDef definition;
27
	private IModelElement modelElement;	
28
	private ModelProperty modelProperty;
29
	private String toolPaletteLabel;
30
	private String toolPaletteDesc;
31
	
32
	private List<DiagramConnectionPart> diagramConnections;
33
	    
34
    public DiagramConnectionTemplate(final SapphireDiagramEditorPart diagramEditor, 
35
    		IDiagramConnectionDef definition, IModelElement modelElement)
36
    {
37
    	this.diagramEditor = diagramEditor;
38
    	this.modelElement = modelElement;
39
    	this.definition = definition;
40
    	
41
        this.toolPaletteLabel = this.definition.getToolPaletteLabel().getContent();
42
        this.toolPaletteDesc = this.definition.getToolPaletteDesc().getContent();
43
        
44
        this.diagramConnections = new ArrayList<DiagramConnectionPart>();
45
        
46
        String propertyName = this.definition.getProperty().getContent();
47
        this.modelProperty = resolve(this.modelElement, propertyName);
48
        if (this.modelProperty instanceof ListProperty)
49
        {
50
        	ListProperty listProperty = (ListProperty)this.modelProperty;
51
        	ModelElementList<?> list = this.modelElement.read(listProperty);
52
            for( IModelElement listEntryModelElement : list )
53
            {
54
            	DiagramConnectionPart connection = new DiagramConnectionPart(this);
55
            	connection.init(this.diagramEditor, listEntryModelElement, definition, 
56
            			Collections.<String,String>emptyMap());
57
            	this.diagramConnections.add(connection);
58
            }
59
        }
60
        else if (this.modelProperty instanceof ElementProperty)
61
        {
62
        	ElementProperty elementProperty = (ElementProperty)this.modelProperty;
63
        	if (this.modelElement.read(elementProperty) != null)
64
        	{
65
	        	IModelElement localModelElement = this.modelElement.read(elementProperty).element();
66
	        	DiagramConnectionPart connection = new DiagramConnectionPart(this);
67
	        	connection.init(this.diagramEditor, localModelElement, definition, 
68
	        			Collections.<String,String>emptyMap());
69
	        	
70
	        	this.diagramConnections.add(connection);
71
        	}
72
        }
73
    }
74
    
75
    public List<DiagramConnectionPart> getDiagramConnections()
76
    {
77
    	return this.diagramConnections;
78
    }
79
    
80
    public String getToolPaletteLabel()
81
    {
82
    	return this.toolPaletteLabel;
83
    }
84
    
85
    public String getToolPaletteDesc()
86
    {
87
    	return this.toolPaletteDesc;
88
    }
89
    
90
    public DiagramConnectionPart createNewDiagramConnection(DiagramNodePart srcNode, 
91
    														DiagramNodePart targetNode)
92
    {
93
    	IModelElement newElement = null;
94
    	if (this.modelProperty instanceof ListProperty)
95
    	{
96
    		ListProperty listProperty = (ListProperty)this.modelProperty;
97
    		ModelElementList<?> list = this.modelElement.read(listProperty);
98
    		newElement = list.addNewElement();
99
    	}
100
    	else if (this.modelProperty instanceof ElementProperty)
101
    	{
102
    		// TODO what if the element property does exist?
103
    		ElementProperty elementProperty = (ElementProperty)this.modelProperty;
104
    		newElement = this.modelElement.read(elementProperty).element(true);
105
    	}
106
    	
107
    	IDiagramConnectionEndpointDef srcAnchorDef = this.definition.getEndpoint1().element();
108
    	String srcProperty = srcAnchorDef.getProperty().getContent();
109
    	String srcExpr = srcAnchorDef.getValue().getContent();
110
    	Function srcFunc = getNodeReferenceFunction(srcNode, srcExpr);
111
    	Object srcValue = srcFunc.value();
112
    	srcFunc.dispose();
113
    	setModelProperty(newElement, srcProperty, srcValue);
114
    	
115
    	IDiagramConnectionEndpointDef targetAnchorDef = this.definition.getEndpoint2().element();
116
    	String targetProperty = targetAnchorDef.getProperty().getContent();
117
    	String targetExpr = targetAnchorDef.getValue().getContent();
118
    	Function targetFunc = getNodeReferenceFunction(targetNode, targetExpr);
119
    	Object targetValue = targetFunc.value();
120
    	targetFunc.dispose();
121
    	setModelProperty(newElement, targetProperty, targetValue);
122
    	
123
    	DiagramConnectionPart newConn = new DiagramConnectionPart(this);
124
    	newConn.init(this.diagramEditor, newElement, this.definition, 
125
    			Collections.<String,String>emptyMap());
126
    	this.diagramConnections.add(newConn);
127
    	return newConn;
128
    }
129
    
130
    private ModelProperty resolve(final IModelElement modelElement, 
131
    		String propertyName)
132
    {
133
    	if (propertyName != null)
134
    	{
135
	        final ModelElementType type = modelElement.getModelElementType();
136
	        final ModelProperty property = type.getProperty( propertyName );
137
	        if( property == null )
138
	        {
139
	            throw new RuntimeException( "Could not find property " + propertyName + " in " + type.getQualifiedName() );
140
	        }
141
	        return property;
142
    	}    
143
        return null;
144
    }
145
146
    private void setModelProperty(final IModelElement modelElement, 
147
    								String propertyName, Object value)
148
    {
149
    	if (propertyName != null)
150
    	{
151
	        final ModelElementType type = modelElement.getModelElementType();
152
	        final ModelProperty property = type.getProperty( propertyName );
153
	        if( property == null )
154
	        {
155
	            throw new RuntimeException( "Could not find property " + propertyName + " in " + type.getQualifiedName() );
156
	        }
157
	        if (!(property instanceof ValueProperty))
158
	        {
159
	        	throw new RuntimeException( "Property " + propertyName + " not a ValueProperty");
160
	        }
161
	        		
162
	        modelElement.write((ValueProperty)property, ((Value<?>)value).getContent());
163
    	}    	
164
    }
165
    
166
    private Function getNodeReferenceFunction(final DiagramNodePart nodePart,
167
    											final String expression)
168
    {
169
        final FunctionContext context = new ModelElementFunctionContext( nodePart.getLocalModelElement() );
170
        Function result = null;
171
        
172
        if( expression != null )
173
        {
174
            try
175
            {
176
                result = ExpressionLanguageParser.parse( context, expression );
177
            }
178
            catch( Exception e )
179
            {
180
                SapphireUiFrameworkPlugin.log( e );
181
                result = null;
182
            }
183
        }
184
        return result;
185
    }
186
}
(-)src/org/eclipse/sapphire/ui/diagram/editor/DiagramGeometryWrapper.java (+266 lines)
Added Link Here
1
package org.eclipse.sapphire.ui.diagram.editor;
2
3
import java.util.ArrayList;
4
import java.util.HashMap;
5
import java.util.Iterator;
6
import java.util.List;
7
8
import org.eclipse.core.resources.IFile;
9
import org.eclipse.sapphire.modeling.ModelElementList;
10
import org.eclipse.sapphire.modeling.ResourceStoreException;
11
import org.eclipse.sapphire.modeling.WorkspaceFileResourceStore;
12
import org.eclipse.sapphire.modeling.xml.RootXmlResource;
13
import org.eclipse.sapphire.modeling.xml.XmlResourceStore;
14
import org.eclipse.sapphire.ui.diagram.geometry.IBendPoint;
15
import org.eclipse.sapphire.ui.diagram.geometry.IDiagramConnectionGeometry;
16
import org.eclipse.sapphire.ui.diagram.geometry.IDiagramGeometry;
17
import org.eclipse.sapphire.ui.diagram.geometry.IDiagramNodeGeometry;
18
19
public class DiagramGeometryWrapper 
20
{
21
	private IFile file;
22
	private IDiagramGeometry geometryModel;
23
	private SapphireDiagramEditorPart diagramPart;
24
	
25
	private HashMap<DiagramNodePart, Bounds> nodeGeometries;
26
	private HashMap<DiagramConnectionPart, List<Point>> connectionBendpoints;
27
	
28
	public DiagramGeometryWrapper(IFile file, SapphireDiagramEditorPart diagramPart)
29
		throws ResourceStoreException
30
	{
31
		this.file = file;
32
		this.diagramPart = diagramPart;
33
		this.nodeGeometries = new HashMap<DiagramNodePart, Bounds>();
34
		this.connectionBendpoints = new HashMap<DiagramConnectionPart, List<Point>>();
35
		read();
36
	}
37
	
38
	public void addNode(DiagramNodePart nodePart, int x, int y, int w, int h)
39
	{
40
		Bounds bounds = new Bounds(x, y, w, h);
41
		this.nodeGeometries.put(nodePart, bounds);
42
	}
43
	
44
	public void removeNode(DiagramNodePart nodePart)
45
	{
46
		this.nodeGeometries.remove(nodePart);
47
	}
48
	
49
	public void updateNode(DiagramNodePart nodePart, int x, int y)
50
	{
51
		Bounds bounds = this.nodeGeometries.get(nodePart);
52
		if (bounds != null)
53
		{
54
			bounds.x = x;
55
			bounds.y = y;
56
		}
57
	}
58
59
	public void updateNode(DiagramNodePart nodePart, int x, int y, int w, int h)
60
	{
61
		Bounds bounds = this.nodeGeometries.get(nodePart);
62
		if (bounds != null)
63
		{
64
			bounds.x = x;
65
			bounds.y = y;
66
			bounds.height = h;
67
			bounds.width = w;
68
		}
69
	}
70
	
71
	public Bounds getNode(DiagramNodePart nodePart)
72
	{
73
		return this.nodeGeometries.get(nodePart);
74
	}
75
	
76
	public void addConnectionBendpoint(DiagramConnectionPart connPart, int index, int x, int y)
77
	{
78
		List<Point> bendpoints = this.connectionBendpoints.get(connPart);
79
		if (bendpoints == null)
80
		{
81
			bendpoints = new ArrayList<Point>();
82
			this.connectionBendpoints.put(connPart, bendpoints);
83
		}
84
		Point newPt = new Point(x, y);
85
		bendpoints.add(index, newPt);
86
	}
87
	
88
	public void removeConnectionBendpoint(DiagramConnectionPart connPart, int index)
89
	{
90
		List<Point> bendpoints = this.connectionBendpoints.get(connPart);
91
		if (bendpoints != null)
92
		{
93
			bendpoints.remove(index);
94
		}
95
	}
96
	
97
	public boolean setConnectionBendpoint(DiagramConnectionPart connPart, int index, int x, int y)
98
	{
99
		List<Point> bendpoints = this.connectionBendpoints.get(connPart);
100
		if (bendpoints != null && index < bendpoints.size())
101
		{
102
			bendpoints.set(index, new Point(x, y));
103
			return true;
104
		}
105
		return false;
106
	}
107
	
108
109
	public List<Point> getConnectionBendpoints(DiagramConnectionPart connPart)
110
	{
111
		return this.connectionBendpoints.get(connPart);
112
	}
113
	
114
	public void removeConnectionBendpoints(DiagramConnectionPart connPart)
115
	{
116
		this.connectionBendpoints.remove(connPart);
117
	}
118
	
119
	public void removeAllConnectionBendpoints()
120
	{
121
		this.connectionBendpoints.clear();
122
	}
123
	
124
	public void read() throws ResourceStoreException
125
	{
126
		final XmlResourceStore resourceStore = new XmlResourceStore( new WorkspaceFileResourceStore(this.file ));
127
		this.geometryModel = IDiagramGeometry.TYPE.instantiate(new RootXmlResource( resourceStore ));
128
129
		ModelElementList<IDiagramNodeGeometry> nodes = this.geometryModel.getDiagramNodeGeometries();
130
		for (IDiagramNodeGeometry node : nodes)
131
		{
132
			String id = node.getNodeId().getContent();
133
			DiagramNodePart nodePart = getNodeElement(id);
134
			if (nodePart != null)
135
			{
136
				int x = node.getX().getContent() != null ? node.getX().getContent() : -1;
137
				int y = node.getY().getContent() != null ? node.getY().getContent() : -1;
138
				int width = node.getWidth().getContent() != null ? node.getWidth().getContent() : -1;
139
				int height = node.getHeight().getContent() != null ? node.getHeight().getContent() : -1;
140
				this.addNode(nodePart, x, y, width, height);
141
			}
142
		}
143
		
144
		ModelElementList<IDiagramConnectionGeometry> connList = this.geometryModel.getDiagramConnectionGeometries();
145
		for (IDiagramConnectionGeometry connBend : connList)
146
		{
147
			String connId = connBend.getConnectionId().getContent();
148
			DiagramConnectionPart connPart = getConnectionElement(connId);
149
			if (connPart != null)
150
			{
151
				ModelElementList<IBendPoint> bps = connBend.getConnectionBendpoints();
152
				int index = 0;
153
				for (IBendPoint pt : bps)
154
				{
155
					this.addConnectionBendpoint(connPart, index++, pt.getX().getContent(), pt.getY().getContent());
156
				}
157
			}			
158
		}
159
	}
160
	
161
	public void write() throws ResourceStoreException
162
	{
163
		this.geometryModel.getDiagramNodeGeometries().clear();
164
		Iterator<DiagramNodePart> it = this.nodeGeometries.keySet().iterator();
165
		while (it.hasNext())
166
		{
167
			DiagramNodePart nodePart = it.next();
168
			Bounds bounds = this.nodeGeometries.get(nodePart);
169
			String id = nodePart.getInstanceId();
170
			
171
			if (bounds != null && id != null)
172
			{
173
				IDiagramNodeGeometry diagramNode = this.geometryModel.getDiagramNodeGeometries().addNewElement();
174
				diagramNode.setNodeId(id);
175
				diagramNode.setX(bounds.x);
176
				diagramNode.setY(bounds.y);
177
				diagramNode.setWidth(bounds.width);
178
				diagramNode.setHeight(bounds.height);
179
			}
180
		}
181
		
182
		this.geometryModel.getDiagramConnectionGeometries().clear();
183
		Iterator<DiagramConnectionPart> connIt = this.connectionBendpoints.keySet().iterator();
184
		while (connIt.hasNext())
185
		{
186
			DiagramConnectionPart connPart = connIt.next();
187
			List<Point> bps = this.connectionBendpoints.get(connPart);
188
			String id = connPart.getInstanceId();
189
			
190
			if (bps != null && id != null)
191
			{
192
				IDiagramConnectionGeometry conn = this.geometryModel.getDiagramConnectionGeometries().addNewElement();
193
				conn.setConnectionId(id);
194
				for (Point pt : bps)
195
				{
196
					IBendPoint pt2 = conn.getConnectionBendpoints().addNewElement();
197
					pt2.setX(pt.x);
198
					pt2.setY(pt.y);
199
				}
200
			}
201
		}
202
		
203
		this.geometryModel.resource().save();
204
	}
205
206
	private DiagramNodePart getNodeElement(String nodeId)
207
	{
208
		for (DiagramNodeTemplate nodeTemplate : this.diagramPart.getNodeTemplates())
209
		{
210
			for (DiagramNodePart nodePart : nodeTemplate.getDiagramNodes())
211
			{
212
				String nodeId2 = nodePart.getInstanceId();
213
				if (nodeId != null && nodeId2 != null && nodeId.equals(nodeId2))
214
				{
215
					return nodePart;
216
				}
217
			}
218
		}
219
		return null;
220
	}
221
	
222
	private DiagramConnectionPart getConnectionElement(String connId)
223
	{
224
		for (DiagramConnectionTemplate connTemplate : this.diagramPart.getConnectionTemplates())
225
		{
226
			for (DiagramConnectionPart connPart : connTemplate.getDiagramConnections())
227
			{
228
				String connId2 = connPart.getInstanceId();
229
				if (connId != null && connId2 != null && connId.equals(connId2))
230
				{
231
					return connPart;
232
				}
233
			}
234
		}
235
		return null;
236
	}
237
238
	// -------------------------------------------------------------------
239
	// Inner classes
240
	// -------------------------------------------------------------------
241
	
242
	public static class Point
243
	{
244
		public int x;
245
		public int y;
246
		
247
		public Point(int x, int y)
248
		{
249
			this.x = x;
250
			this.y = y;
251
		}
252
	}
253
	
254
	public static final class Bounds extends Point
255
	{
256
		public int width;
257
		public int height;
258
		
259
		public Bounds(int x, int y, int width, int height)
260
		{
261
			super(x, y);
262
			this.width = width;
263
			this.height = height;
264
		}
265
	}
266
}
(-)src/org/eclipse/sapphire/ui/diagram/editor/DiagramNodePart.java (+118 lines)
Added Link Here
1
package org.eclipse.sapphire.ui.diagram.editor;
2
3
import org.eclipse.sapphire.modeling.IModelElement;
4
import org.eclipse.sapphire.modeling.ValueProperty;
5
import org.eclipse.sapphire.modeling.el.Function;
6
import org.eclipse.sapphire.ui.SapphirePart;
7
import org.eclipse.sapphire.ui.SapphireRenderingContext;
8
import org.eclipse.sapphire.ui.diagram.def.IDiagramNodeDef;
9
10
public class DiagramNodePart extends SapphirePart 
11
{
12
	private DiagramNodeTemplate nodeTemplate;
13
	private IDiagramNodeDef definition;
14
	private IModelElement modelElement;
15
	private Function labelFunction;
16
	private Function idFunction;
17
	private ValueProperty labelProperty;
18
	
19
	public DiagramNodePart(DiagramNodeTemplate nodeTemplate)
20
	{
21
		this.nodeTemplate = nodeTemplate;
22
	}
23
	
24
    @Override
25
    protected void init()
26
    {
27
        super.init();
28
        
29
        this.definition = (IDiagramNodeDef)super.definition;
30
        this.modelElement = getModelElement();
31
        this.labelFunction = initExpression
32
        ( 
33
            this.definition.getLabel().getLocalizedText(), 
34
            new Runnable()
35
            {
36
                public void run()
37
                {
38
                }
39
            }
40
        );
41
        this.labelProperty = FunctionUtil.getFunctionProperty(this.modelElement, this.labelFunction);
42
        if (this.labelProperty == null)
43
        {
44
        	throw new RuntimeException( "Label function does not contain a property.");
45
        }
46
        
47
        this.idFunction = initExpression
48
        ( 
49
            this.definition.getInstanceId().getLocalizedText(), 
50
            new Runnable()
51
            {
52
                public void run()
53
                {
54
                }
55
            }
56
        );
57
    }
58
    
59
    public DiagramNodeTemplate getDiagramNodeTemplate()
60
    {
61
    	return this.nodeTemplate;
62
    }
63
    
64
    public IModelElement getLocalModelElement()
65
    {
66
        return this.modelElement;
67
    }    
68
        
69
	@Override
70
	public void render(SapphireRenderingContext context)
71
	{
72
		throw new UnsupportedOperationException();
73
	}
74
75
	public String getLabel()
76
	{
77
        String label = null;
78
        
79
        if( this.labelFunction != null )
80
        {
81
            label = (String) this.labelFunction.value();
82
        }
83
        
84
        if( label == null )
85
        {
86
            label = "#null#";
87
        }
88
        
89
        return label;
90
	}
91
92
	public void setLabel(String newValue)
93
	{
94
		if (this.labelProperty != null)
95
		{
96
			this.modelElement.write(this.labelProperty, newValue);
97
		}
98
	}
99
	
100
	public String getInstanceId()
101
	{
102
        String id = null;
103
        
104
        if( this.idFunction != null )
105
        {
106
            id = (String) this.idFunction.value();
107
        }
108
        
109
        if( id == null )
110
        {
111
            id = "#null#";
112
        }
113
        
114
        return id;
115
		
116
	}
117
		
118
}
(-)src/org/eclipse/sapphire/ui/diagram/editor/DiagramNodeTemplate.java (+120 lines)
Added Link Here
1
package org.eclipse.sapphire.ui.diagram.editor;
2
3
import java.util.ArrayList;
4
import java.util.Collections;
5
import java.util.List;
6
7
import org.eclipse.sapphire.modeling.ElementProperty;
8
import org.eclipse.sapphire.modeling.IModelElement;
9
import org.eclipse.sapphire.modeling.ListProperty;
10
import org.eclipse.sapphire.modeling.ModelElementList;
11
import org.eclipse.sapphire.modeling.ModelElementType;
12
import org.eclipse.sapphire.modeling.ModelProperty;
13
import org.eclipse.sapphire.ui.diagram.def.IDiagramNodeDef;
14
15
public class DiagramNodeTemplate 
16
{
17
	private SapphireDiagramEditorPart diagramEditor;
18
	private IDiagramNodeDef definition;
19
	private IModelElement modelElement;	
20
	private ModelProperty modelProperty;
21
	private String toolPaletteLabel;
22
	private String toolPaletteDesc;
23
	
24
	private List<DiagramNodePart> diagramNodes;
25
	    
26
    public DiagramNodeTemplate(final SapphireDiagramEditorPart diagramEditor, IDiagramNodeDef definition, IModelElement modelElement)
27
    {
28
    	this.diagramEditor = diagramEditor;
29
    	this.modelElement = modelElement;
30
    	this.definition = definition;
31
    	
32
        this.toolPaletteLabel = this.definition.getToolPaletteLabel().getContent();
33
        this.toolPaletteDesc = this.definition.getToolPaletteDesc().getContent();
34
        
35
        this.diagramNodes = new ArrayList<DiagramNodePart>();
36
        
37
        String propertyName = this.definition.getProperty().getContent();
38
        this.modelProperty = resolve(this.modelElement, propertyName);
39
        if (this.modelProperty instanceof ListProperty)
40
        {
41
        	ListProperty listProperty = (ListProperty)this.modelProperty;
42
        	ModelElementList<?> list = this.modelElement.read(listProperty);
43
            for( IModelElement listEntryModelElement : list )
44
            {
45
            	DiagramNodePart node = new DiagramNodePart(this);
46
            	node.init(this.diagramEditor, listEntryModelElement, definition, 
47
            			Collections.<String,String>emptyMap());
48
            	this.diagramNodes.add(node);
49
            }
50
        }
51
        else if (this.modelProperty instanceof ElementProperty)
52
        {
53
        	ElementProperty elementProperty = (ElementProperty)this.modelProperty;
54
        	if (this.modelElement.read(elementProperty) != null)
55
        	{
56
	        	IModelElement localModelElement = this.modelElement.read(elementProperty).element();
57
	        	DiagramNodePart node = new DiagramNodePart(this);
58
	        	node.init(this.diagramEditor, localModelElement, definition, 
59
	        			Collections.<String,String>emptyMap());
60
	        	
61
	        	this.diagramNodes.add(node);
62
        	}
63
        }
64
    }
65
    
66
    public List<DiagramNodePart> getDiagramNodes()
67
    {
68
    	return this.diagramNodes;
69
    }
70
    
71
    public String getToolPaletteLabel()
72
    {
73
    	return this.toolPaletteLabel;
74
    }
75
    
76
    public String getToolPaletteDesc()
77
    {
78
    	return this.toolPaletteDesc;
79
    }
80
    
81
    public DiagramNodePart createNewDiagramNode()
82
    {
83
    	IModelElement newElement = null;
84
    	if (this.modelProperty instanceof ListProperty)
85
    	{
86
    		ListProperty listProperty = (ListProperty)this.modelProperty;
87
    		ModelElementList<?> list = this.modelElement.read(listProperty);
88
    		newElement = list.addNewElement();
89
    	}
90
    	else if (this.modelProperty instanceof ElementProperty)
91
    	{
92
    		// TODO what if the element property does exist?
93
    		ElementProperty elementProperty = (ElementProperty)this.modelProperty;
94
    		newElement = this.modelElement.read(elementProperty).element(true);
95
    	}
96
    	DiagramNodePart newNode = new DiagramNodePart(this);
97
    	newNode.init(this.diagramEditor, newElement, this.definition, 
98
    			Collections.<String,String>emptyMap());
99
    	this.diagramNodes.add(newNode);
100
    	return newNode;
101
    }
102
    
103
    private ModelProperty resolve(final IModelElement modelElement, 
104
    		String propertyName)
105
    {
106
    	if (propertyName != null)
107
    	{
108
	        final ModelElementType type = modelElement.getModelElementType();
109
	        final ModelProperty property = type.getProperty( propertyName );
110
	        
111
	        if( property == null )
112
	        {
113
	            throw new RuntimeException( "Could not find property " + propertyName + " in " + type.getQualifiedName() );
114
	        }
115
	        return property;
116
    	}    
117
        return null;
118
    }
119
    
120
}
(-)src/org/eclipse/sapphire/ui/diagram/editor/FunctionUtil.java (+45 lines)
Added Link Here
1
package org.eclipse.sapphire.ui.diagram.editor;
2
3
import java.util.List;
4
5
import org.eclipse.sapphire.modeling.IModelElement;
6
import org.eclipse.sapphire.modeling.ModelElementType;
7
import org.eclipse.sapphire.modeling.ModelProperty;
8
import org.eclipse.sapphire.modeling.ValueProperty;
9
import org.eclipse.sapphire.modeling.el.Function;
10
import org.eclipse.sapphire.modeling.el.RootPropertyAccessFunction;
11
12
public class FunctionUtil 
13
{
14
    public static ValueProperty getFunctionProperty(IModelElement modelElement, Function function)
15
    {
16
    	if (function instanceof RootPropertyAccessFunction)
17
    	{
18
    		RootPropertyAccessFunction rpaf = (RootPropertyAccessFunction)function;
19
    		if (rpaf.operand(0).value() instanceof String)
20
    		{
21
    			String propName = (String)rpaf.operand(0).value();
22
    	        final ModelElementType type = modelElement.getModelElementType();
23
    	        final ModelProperty property = type.getProperty(propName);
24
    			if (property instanceof ValueProperty)
25
    			{
26
    				return (ValueProperty)property;
27
    			}
28
    		}
29
    	}
30
    	else 
31
    	{
32
    		List<Function> subFuncs = function.operands();
33
    		for (Function subFunc : subFuncs)
34
    		{
35
    			ValueProperty property = getFunctionProperty(modelElement, subFunc);
36
    			if (property != null)
37
    			{
38
    				return property;
39
    			}
40
    		}
41
    	}
42
    	return null;
43
    }
44
45
}
(-)src/org/eclipse/sapphire/ui/diagram/editor/SapphireDiagramEditorPart.java (+206 lines)
Added Link Here
1
package org.eclipse.sapphire.ui.diagram.editor;
2
3
import java.util.ArrayList;
4
import java.util.List;
5
import java.util.Set;
6
7
import org.eclipse.core.resources.IFile;
8
import org.eclipse.core.runtime.IPath;
9
import org.eclipse.core.runtime.IStatus;
10
import org.eclipse.help.IContext;
11
import org.eclipse.sapphire.modeling.IModelElement;
12
import org.eclipse.sapphire.modeling.ModelElementList;
13
import org.eclipse.sapphire.ui.ISapphirePart;
14
import org.eclipse.sapphire.ui.SapphireAction;
15
import org.eclipse.sapphire.ui.SapphireActionGroup;
16
import org.eclipse.sapphire.ui.SapphireEditor;
17
import org.eclipse.sapphire.ui.SapphireImageCache;
18
import org.eclipse.sapphire.ui.SapphirePartListener;
19
import org.eclipse.sapphire.ui.def.ISapphirePartDef;
20
import org.eclipse.sapphire.ui.def.ISapphireUiDef;
21
import org.eclipse.sapphire.ui.def.SapphireUiDefFactory;
22
import org.eclipse.sapphire.ui.diagram.def.IDiagramConnectionDef;
23
import org.eclipse.sapphire.ui.diagram.def.IDiagramNodeDef;
24
import org.eclipse.sapphire.ui.diagram.def.IDiagramPageDef;
25
import org.eclipse.sapphire.ui.diagram.graphiti.editor.SapphireDiagramEditor;
26
import org.eclipse.sapphire.ui.diagram.graphiti.editor.SapphireDiagramEditorFactory;
27
import org.eclipse.sapphire.ui.diagram.graphiti.editor.SapphireDiagramEditorInput;
28
import org.eclipse.sapphire.ui.internal.SapphireUiFrameworkPlugin;
29
30
public class SapphireDiagramEditorPart
31
	implements ISapphirePart
32
{
33
    private final SapphireEditor editor;
34
    private final IModelElement rootModelElement;
35
    private IDiagramPageDef diagramPageDef = null;
36
    private DiagramGeometryWrapper diagramGeometry;
37
    private List<DiagramNodeTemplate> nodeTemplates;
38
    private List<DiagramConnectionTemplate> connectionTemplates;
39
    private SapphireDiagramEditorInput diagramInput;
40
    private SapphireDiagramEditor diagramEditor;
41
	
42
	public SapphireDiagramEditorPart(final SapphireEditor editor,
43
								final IModelElement rootModelElement,
44
								final IPath pageDefinitionLocation )
45
	{
46
		this(editor, rootModelElement, pageDefinitionLocation, null);
47
	}
48
49
	public SapphireDiagramEditorPart(final SapphireEditor editor,
50
								final IModelElement rootModelElement,
51
								final IPath pageDefinitionLocation,
52
								String pageName )
53
	{
54
		this.editor = editor;
55
		this.rootModelElement = rootModelElement;
56
57
        final String bundleId = pageDefinitionLocation.segment( 0 );
58
        final String pageId = pageDefinitionLocation.lastSegment();
59
        final String relPath = pageDefinitionLocation.removeFirstSegments( 1 ).removeLastSegments( 1 ).toPortableString();
60
        
61
        final ISapphireUiDef def = SapphireUiDefFactory.load( bundleId, relPath );
62
        
63
        for( IDiagramPageDef pg : def.getDiagramPageDefs() )
64
        {
65
            if( pageId.equals( pg.getId().getText() ) )
66
            {
67
                this.diagramPageDef = pg;
68
                break;
69
            }
70
        }
71
                
72
        this.nodeTemplates = new ArrayList<DiagramNodeTemplate>();
73
        ModelElementList<IDiagramNodeDef> nodeDefs = this.diagramPageDef.getDiagramNodeDefs();
74
        
75
        for (IDiagramNodeDef nodeDef : nodeDefs)
76
        {
77
        	DiagramNodeTemplate nodeTemplate = new DiagramNodeTemplate(this, nodeDef, this.rootModelElement);
78
        	this.nodeTemplates.add(nodeTemplate);
79
        }
80
        
81
        this.connectionTemplates = new ArrayList<DiagramConnectionTemplate>();
82
        ModelElementList<IDiagramConnectionDef> connectionDefs = this.diagramPageDef.getDiagramConnectionDefs();
83
        for (IDiagramConnectionDef connectionDef : connectionDefs)
84
        {
85
        	DiagramConnectionTemplate connectionTemplate = new DiagramConnectionTemplate(this, connectionDef, this.rootModelElement);
86
        	this.connectionTemplates.add(connectionTemplate);
87
        }       
88
        
89
        initializeDiagramEditor();
90
	}
91
92
	private void initializeDiagramEditor()
93
	{
94
        try
95
        {
96
        	this.diagramInput = SapphireDiagramEditorFactory.createEditorInput(this.editor.getEditorInput());
97
        	IFile npFile = this.diagramInput.getNodePositionFile();
98
        	this.diagramGeometry = new DiagramGeometryWrapper(npFile, this);
99
        }
100
        catch( Exception e )
101
        {
102
            SapphireUiFrameworkPlugin.log( e );
103
        }
104
    	this.diagramEditor = new SapphireDiagramEditor(this);
105
	}
106
	
107
	public SapphireDiagramEditorInput getDiagramEditorInput()
108
	{
109
		return this.diagramInput;
110
	}
111
	
112
	public SapphireDiagramEditor getDiagramEditor()
113
	{
114
		return this.diagramEditor;
115
	}
116
	
117
	public List<DiagramNodeTemplate> getNodeTemplates()
118
	{
119
		return this.nodeTemplates;
120
	}
121
	
122
	public List<DiagramConnectionTemplate> getConnectionTemplates()
123
	{
124
		return this.connectionTemplates;
125
	}
126
	
127
	public DiagramGeometryWrapper getDiagramGeometry()
128
	{
129
		return this.diagramGeometry;
130
	}
131
	
132
	public ISapphirePart getParentPart() 
133
	{	
134
		return this.editor;
135
	}
136
137
	public <T> T getNearestPart(Class<T> partType) {
138
		// TODO Auto-generated method stub
139
		return null;
140
	}
141
142
	public IModelElement getModelElement() 
143
	{	
144
		return this.rootModelElement;
145
	}
146
147
	public IStatus getValidationState() {
148
		// TODO Auto-generated method stub
149
		return null;
150
	}
151
152
	public IContext getDocumentationContext() {
153
		// TODO Auto-generated method stub
154
		return null;
155
	}
156
157
	public SapphireImageCache getImageCache() {
158
		// TODO Auto-generated method stub
159
		return null;
160
	}
161
162
	public void addListener(SapphirePartListener listener) {
163
		// TODO Auto-generated method stub
164
		
165
	}
166
167
	public void removeListener(SapphirePartListener listener) {
168
		// TODO Auto-generated method stub
169
		
170
	}
171
172
	public void dispose() {
173
		// TODO Auto-generated method stub
174
		
175
	}
176
177
	public ISapphirePartDef getDefinition() 
178
	{	
179
		return this.diagramPageDef;
180
	}
181
182
	public Set<String> getActionContexts() {
183
		// TODO Auto-generated method stub
184
		return null;
185
	}
186
187
	public String getMainActionContext() {
188
		// TODO Auto-generated method stub
189
		return null;
190
	}
191
192
	public SapphireActionGroup getActions() {
193
		// TODO Auto-generated method stub
194
		return null;
195
	}
196
197
	public SapphireActionGroup getActions(String context) {
198
		// TODO Auto-generated method stub
199
		return null;
200
	}
201
202
	public SapphireAction getAction(String id) {
203
		// TODO Auto-generated method stub
204
		return null;
205
	}
206
}
(-)src/org/eclipse/sapphire/ui/diagram/geometry/IBendPoint.java (+40 lines)
Added Link Here
1
package org.eclipse.sapphire.ui.diagram.geometry;
2
3
import org.eclipse.sapphire.modeling.IModelElement;
4
import org.eclipse.sapphire.modeling.ModelElementType;
5
import org.eclipse.sapphire.modeling.Value;
6
import org.eclipse.sapphire.modeling.ValueProperty;
7
import org.eclipse.sapphire.modeling.annotations.GenerateImpl;
8
import org.eclipse.sapphire.modeling.annotations.Type;
9
import org.eclipse.sapphire.modeling.xml.annotations.CustomXmlValueBinding;
10
import org.eclipse.sapphire.ui.diagram.geometry.internal.GeometryAttributeBinding;
11
12
@GenerateImpl
13
14
public interface IBendPoint extends IModelElement 
15
{
16
	ModelElementType TYPE = new ModelElementType( IBendPoint.class );
17
	
18
    // *** X ***
19
    
20
    @Type( base = Integer.class )
21
    @CustomXmlValueBinding( impl = GeometryAttributeBinding.class, params = {"x"})
22
    
23
    ValueProperty PROP_X = new ValueProperty( TYPE, "X");
24
    
25
    Value<Integer> getX();
26
    void setX(Integer value);
27
	void setX(String value);
28
29
    // *** Y ***
30
    
31
    @Type( base = Integer.class )
32
    @CustomXmlValueBinding( impl = GeometryAttributeBinding.class, params = {"y"})
33
34
    ValueProperty PROP_Y = new ValueProperty( TYPE, "Y");
35
    
36
    Value<Integer> getY();
37
    void setY(Integer value);
38
	void setY(String value);
39
	
40
}
(-)src/org/eclipse/sapphire/ui/diagram/geometry/IDiagramConnectionGeometry.java (+41 lines)
Added Link Here
1
package org.eclipse.sapphire.ui.diagram.geometry;
2
3
import org.eclipse.sapphire.modeling.IModelElement;
4
import org.eclipse.sapphire.modeling.ListProperty;
5
import org.eclipse.sapphire.modeling.ModelElementList;
6
import org.eclipse.sapphire.modeling.ModelElementType;
7
import org.eclipse.sapphire.modeling.Value;
8
import org.eclipse.sapphire.modeling.ValueProperty;
9
import org.eclipse.sapphire.modeling.annotations.GenerateImpl;
10
import org.eclipse.sapphire.modeling.annotations.NonNullValue;
11
import org.eclipse.sapphire.modeling.annotations.Type;
12
import org.eclipse.sapphire.modeling.xml.annotations.XmlBinding;
13
import org.eclipse.sapphire.modeling.xml.annotations.XmlListBinding;
14
15
@GenerateImpl
16
17
public interface IDiagramConnectionGeometry extends IModelElement 
18
{
19
	ModelElementType TYPE = new ModelElementType( IDiagramConnectionGeometry.class );
20
	
21
	// *** ConnectionId ***
22
	
23
	@XmlBinding( path = "id")
24
	@NonNullValue
25
26
	ValueProperty PROP_CONNECTION_ID = new ValueProperty( TYPE, "ConnectionId" );
27
28
    Value<String> getConnectionId();
29
    void setConnectionId( String name );
30
31
    // *** ConnectionBendpoints***
32
33
    @Type( base = IBendPoint.class )
34
    @XmlListBinding( mappings = @XmlListBinding.Mapping( element = "bendpoint", type = IBendPoint.class ) )
35
    
36
    ListProperty PROP_CONNECTION_BENDPOINTS = new ListProperty( TYPE, "ConnectionBendPoints" );
37
    
38
    ModelElementList<IBendPoint> getConnectionBendpoints();
39
    
40
    
41
}
(-)src/org/eclipse/sapphire/ui/diagram/geometry/IDiagramGeometry.java (+39 lines)
Added Link Here
1
package org.eclipse.sapphire.ui.diagram.geometry;
2
3
import org.eclipse.sapphire.modeling.IModelElement;
4
import org.eclipse.sapphire.modeling.ListProperty;
5
import org.eclipse.sapphire.modeling.ModelElementList;
6
import org.eclipse.sapphire.modeling.ModelElementType;
7
import org.eclipse.sapphire.modeling.annotations.GenerateImpl;
8
import org.eclipse.sapphire.modeling.annotations.Type;
9
import org.eclipse.sapphire.modeling.xml.annotations.XmlListBinding;
10
import org.eclipse.sapphire.modeling.xml.annotations.XmlRootBinding;
11
12
@GenerateImpl
13
@XmlRootBinding( elementName = "diagram-geometry" )
14
15
public interface IDiagramGeometry extends IModelElement 
16
{
17
	ModelElementType TYPE = new ModelElementType( IDiagramGeometry.class );
18
	
19
    // *** DiagramNodeGeometries ***
20
21
    @Type( base = IDiagramNodeGeometry.class )
22
    
23
    @XmlListBinding( mappings = @XmlListBinding.Mapping( element = "node", type = IDiagramNodeGeometry.class ) )
24
    
25
    ListProperty PROP_DIAGRAM_NODE_GEOMETRIES = new ListProperty( TYPE, "DiagramNodeGeometries" );
26
    
27
    ModelElementList<IDiagramNodeGeometry> getDiagramNodeGeometries();
28
	
29
    // *** DiagramConnectionGeometries ***
30
31
    @Type( base = IDiagramConnectionGeometry.class )
32
    
33
    @XmlListBinding( mappings = @XmlListBinding.Mapping( element = "connection", type = IDiagramConnectionGeometry.class ) )
34
    
35
    ListProperty PROP_DIAGRAM_CONNECTION_GEOMETRIES = new ListProperty( TYPE, "DiagramConnectionGeometries" );
36
    
37
    ModelElementList<IDiagramConnectionGeometry> getDiagramConnectionGeometries();
38
39
}
(-)src/org/eclipse/sapphire/ui/diagram/geometry/IDiagramNodeGeometry.java (+74 lines)
Added Link Here
1
package org.eclipse.sapphire.ui.diagram.geometry;
2
3
import org.eclipse.sapphire.modeling.IModelElement;
4
import org.eclipse.sapphire.modeling.ModelElementType;
5
import org.eclipse.sapphire.modeling.Value;
6
import org.eclipse.sapphire.modeling.ValueProperty;
7
import org.eclipse.sapphire.modeling.annotations.GenerateImpl;
8
import org.eclipse.sapphire.modeling.annotations.NonNullValue;
9
import org.eclipse.sapphire.modeling.annotations.Type;
10
import org.eclipse.sapphire.modeling.xml.annotations.CustomXmlValueBinding;
11
import org.eclipse.sapphire.modeling.xml.annotations.XmlBinding;
12
import org.eclipse.sapphire.ui.diagram.geometry.internal.GeometryAttributeBinding;
13
14
@GenerateImpl
15
16
public interface IDiagramNodeGeometry extends IModelElement
17
{
18
	ModelElementType TYPE = new ModelElementType( IDiagramNodeGeometry.class );
19
	
20
	// *** NodeId ***
21
	
22
	@XmlBinding( path = "id")
23
	@NonNullValue
24
25
	ValueProperty PROP_NODE_ID = new ValueProperty( TYPE, "NodeId" );
26
27
    Value<String> getNodeId();
28
    void setNodeId( String name );
29
30
    // *** X ***
31
    
32
    @Type( base = Integer.class )
33
    @CustomXmlValueBinding( impl = GeometryAttributeBinding.class, params = {"gemometry", "x"})
34
    
35
    ValueProperty PROP_X = new ValueProperty( TYPE, "X");
36
    
37
    Value<Integer> getX();
38
    void setX(Integer value);
39
	void setX(String value);
40
41
    // *** Y ***
42
    
43
    @Type( base = Integer.class )
44
    @CustomXmlValueBinding( impl = GeometryAttributeBinding.class, params = {"gemometry", "y"})
45
46
    ValueProperty PROP_Y = new ValueProperty( TYPE, "Y");
47
    
48
    Value<Integer> getY();
49
    void setY(Integer value);
50
	void setY(String value);
51
    
52
    // *** Width ***
53
    
54
    @Type( base = Integer.class )
55
    @CustomXmlValueBinding( impl = GeometryAttributeBinding.class, params = {"gemometry", "width"})
56
    
57
    ValueProperty PROP_WIDTH = new ValueProperty( TYPE, "Width");
58
    
59
    Value<Integer> getWidth();
60
    void setWidth(Integer value);
61
	void setWidth(String value);
62
	
63
    // *** Height ***
64
    
65
    @Type( base = Integer.class )
66
    @CustomXmlValueBinding( impl = GeometryAttributeBinding.class, params = {"gemometry", "height"})
67
    
68
    ValueProperty PROP_HEIGHT = new ValueProperty( TYPE, "Height");
69
    
70
    Value<Integer> getHeight();
71
    void setHeight(Integer value);
72
	void setHeight(String value);
73
	
74
}
(-)src/org/eclipse/sapphire/ui/diagram/geometry/internal/GeometryAttributeBinding.java (+68 lines)
Added Link Here
1
package org.eclipse.sapphire.ui.diagram.geometry.internal;
2
3
import org.eclipse.sapphire.modeling.IModelElement;
4
import org.eclipse.sapphire.modeling.ModelProperty;
5
import org.eclipse.sapphire.modeling.xml.XmlElement;
6
import org.eclipse.sapphire.modeling.xml.XmlValueBindingImpl;
7
8
public class GeometryAttributeBinding extends XmlValueBindingImpl
9
{
10
	private String modelPath = null;
11
	private String attrName = null;
12
	
13
	@Override
14
    public void init( final IModelElement element,
15
            final ModelProperty property,
16
            final String[] params )
17
	{
18
		super.init(element, property, params);
19
		if (params != null)
20
		{
21
			if (params.length > 1)
22
			{
23
				this.modelPath = params[0];
24
				this.attrName = params[1];
25
			}
26
			else if (params.length > 0) 
27
			{
28
				this.attrName = params[0];
29
			}
30
		}
31
	}
32
33
	@Override
34
	public String read() 
35
	{
36
		String attrVal = null;
37
		if (this.attrName != null)
38
		{
39
			XmlElement geometryEl = xml(false);
40
			if (geometryEl != null)
41
			{
42
				if (this.modelPath != null)
43
				{
44
					geometryEl = geometryEl.getChildElement(this.modelPath, false);
45
				}
46
				if (geometryEl != null)
47
				{
48
					attrVal = ( geometryEl.getAttributeText(this.attrName) );
49
				}
50
			}
51
		}
52
		return attrVal;
53
	}
54
55
	@Override
56
	public void write(String value) 
57
	{
58
		if (this.attrName != null)
59
		{
60
			XmlElement geometryEl = xml(true);
61
			if (this.modelPath != null)
62
			{
63
				geometryEl = geometryEl.getChildElement(this.modelPath, true);
64
			}
65
			geometryEl.setAttributeText( this.attrName, value, true );
66
		}		
67
	}
68
}
(-)src/org/eclipse/sapphire/ui/diagram/graphiti/GraphitiFileService.java (+172 lines)
Added Link Here
1
package org.eclipse.sapphire.ui.diagram.graphiti;
2
3
import java.io.IOException;
4
import java.io.PrintWriter;
5
import java.io.StringWriter;
6
import java.util.Collections;
7
import java.util.HashMap;
8
import java.util.HashSet;
9
import java.util.Map;
10
import java.util.Map.Entry;
11
import java.util.Set;
12
13
import org.eclipse.core.resources.IFile;
14
import org.eclipse.core.resources.IWorkspaceRunnable;
15
import org.eclipse.core.resources.ResourcesPlugin;
16
import org.eclipse.core.runtime.CoreException;
17
import org.eclipse.core.runtime.IProgressMonitor;
18
import org.eclipse.core.runtime.Path;
19
import org.eclipse.emf.common.command.CommandStack;
20
import org.eclipse.emf.common.util.EList;
21
import org.eclipse.emf.common.util.URI;
22
import org.eclipse.emf.common.util.WrappedException;
23
import org.eclipse.emf.ecore.resource.Resource;
24
import org.eclipse.emf.ecore.resource.ResourceSet;
25
import org.eclipse.emf.transaction.RecordingCommand;
26
import org.eclipse.emf.transaction.Transaction;
27
import org.eclipse.emf.transaction.TransactionalEditingDomain;
28
import org.eclipse.emf.transaction.impl.TransactionalEditingDomainImpl;
29
import org.eclipse.graphiti.mm.pictograms.Diagram;
30
import org.eclipse.graphiti.ui.editor.DiagramEditorFactory;
31
32
public class GraphitiFileService 
33
{
34
	public static TransactionalEditingDomain createEmfFileForDiagram(URI diagramResourceUri, final Diagram diagram) {
35
36
		// Create a resource set and EditingDomain
37
		final TransactionalEditingDomain editingDomain = DiagramEditorFactory.createResourceSetAndEditingDomain();
38
		final ResourceSet resourceSet = editingDomain.getResourceSet();
39
		// Create a resource for this file.
40
		final Resource resource = resourceSet.createResource(diagramResourceUri);
41
		final CommandStack commandStack = editingDomain.getCommandStack();
42
		commandStack.execute(new RecordingCommand(editingDomain) {
43
44
			@Override
45
			protected void doExecute() {
46
				resource.setTrackingModification(true);
47
				resource.getContents().add(diagram);
48
49
			}
50
		});
51
52
		save(editingDomain, Collections.<Resource, Map<?, ?>> emptyMap());
53
		return editingDomain;
54
	}
55
56
	private static void save(TransactionalEditingDomain editingDomain, Map<Resource, Map<?, ?>> options) {
57
		saveInWorkspaceRunnable(editingDomain, options);
58
	}
59
60
	private static void saveInWorkspaceRunnable(final TransactionalEditingDomain editingDomain, final Map<Resource, Map<?, ?>> options) {
61
62
		final Map<URI, Throwable> failedSaves = new HashMap<URI, Throwable>();
63
		final IWorkspaceRunnable wsRunnable = new IWorkspaceRunnable() {			
64
			public void run(final IProgressMonitor monitor) throws CoreException {
65
66
				final Runnable runnable = new Runnable() {
67
					
68
					public void run() {
69
						Transaction parentTx;
70
						if (editingDomain != null
71
								&& (parentTx = ((TransactionalEditingDomainImpl) editingDomain).getActiveTransaction()) != null) {
72
							do {
73
								if (!parentTx.isReadOnly()) {
74
									throw new IllegalStateException(
75
											"FileService.save() called from within a command (likely produces a deadlock)");
76
								}
77
							} while ((parentTx = ((TransactionalEditingDomainImpl) editingDomain).getActiveTransaction().getParent()) != null);
78
						}
79
80
						final EList<Resource> resources = editingDomain.getResourceSet().getResources();
81
						// Copy list to an array to prevent
82
						// ConcurrentModificationExceptions
83
						// during the saving of the dirty resources
84
						Resource[] resourcesArray = new Resource[resources.size()];
85
						resourcesArray = resources.toArray(resourcesArray);
86
						final Set<Resource> savedResources = new HashSet<Resource>();
87
						for (int i = 0; i < resourcesArray.length; i++) {
88
							// In case resource modification tracking is
89
							// switched on, we can check if a resource
90
							// has been modified, so that we only need to same
91
							// really changed resources; otherwise
92
							// we need to save all resources in the set
93
							final Resource resource = resourcesArray[i];
94
							if (resource.isModified()) {
95
								try {
96
									resource.save(options.get(resource));
97
									savedResources.add(resource);
98
								} catch (final Throwable t) {
99
									failedSaves.put(resource.getURI(), t);
100
								}
101
							}
102
						}
103
					}
104
				};
105
106
				try {
107
					editingDomain.runExclusive(runnable);
108
				} catch (final InterruptedException e) {
109
					throw new RuntimeException(e);
110
				}
111
				editingDomain.getCommandStack().flush();
112
			}
113
		};
114
		try {
115
			ResourcesPlugin.getWorkspace().run(wsRunnable, null);
116
			if (!failedSaves.isEmpty()) {
117
				throw new WrappedException(createMessage(failedSaves), new RuntimeException());
118
			}
119
		} catch (final CoreException e) {
120
			final Throwable cause = e.getStatus().getException();
121
			if (cause instanceof RuntimeException) {
122
				throw (RuntimeException) cause;
123
			}
124
			throw new RuntimeException(e);
125
		}
126
	}
127
128
	private static String createMessage(Map<URI, Throwable> failedSaves) {
129
		final StringBuilder buf = new StringBuilder("The following resources could not be saved:");
130
		for (final Entry<URI, Throwable> entry : failedSaves.entrySet()) {
131
			buf.append("\nURI: ").append(entry.getKey().toString()).append(", cause: \n").append(getExceptionAsString(entry.getValue()));
132
		}
133
		return buf.toString();
134
	}
135
136
	private static String getExceptionAsString(Throwable t) {
137
		final StringWriter stringWriter = new StringWriter();
138
		final PrintWriter printWriter = new PrintWriter(stringWriter);
139
		t.printStackTrace(printWriter);
140
		final String result = stringWriter.toString();
141
		try {
142
			stringWriter.close();
143
		} catch (final IOException e) {
144
			// $JL-EXC$ ignore
145
		}
146
		printWriter.close();
147
		return result;
148
	}
149
	
150
	public static IFile EmfResourceToIFile(Resource resource)
151
	{
152
		IFile ifile = null;
153
		
154
		URI uri = resource.getURI();
155
	    uri = resource.getResourceSet().getURIConverter().normalize(uri);
156
	    
157
	    String scheme = uri.scheme();
158
	    if ("platform".equals(scheme) && uri.segmentCount() > 1 &&
159
	    		"resource".equals(uri.segment(0)))
160
	    {
161
	    	StringBuffer platformResourcePath = new StringBuffer();
162
	    	for (int j = 1, size = uri.segmentCount(); j < size; ++j)
163
	    	{
164
	    		platformResourcePath.append('/');
165
	    		platformResourcePath.append(uri.segment(j));
166
	    	}
167
	    	return ResourcesPlugin.getWorkspace().getRoot().getFile(new Path(platformResourcePath.toString()));
168
	    }
169
		return ifile;
170
	}
171
172
}
(-)src/org/eclipse/sapphire/ui/diagram/graphiti/editor/SapphireDiagramEditor.java (+230 lines)
Added Link Here
1
package org.eclipse.sapphire.ui.diagram.graphiti.editor;
2
3
import java.util.Collection;
4
import java.util.HashSet;
5
import java.util.Iterator;
6
import java.util.List;
7
8
import org.eclipse.core.runtime.IProgressMonitor;
9
import org.eclipse.emf.transaction.RecordingCommand;
10
import org.eclipse.emf.transaction.TransactionalEditingDomain;
11
import org.eclipse.emf.transaction.util.TransactionUtil;
12
import org.eclipse.graphiti.features.IAddFeature;
13
import org.eclipse.graphiti.features.context.impl.AddConnectionContext;
14
import org.eclipse.graphiti.features.context.impl.AddContext;
15
import org.eclipse.graphiti.mm.algorithms.styles.Point;
16
import org.eclipse.graphiti.mm.pictograms.Anchor;
17
import org.eclipse.graphiti.mm.pictograms.Connection;
18
import org.eclipse.graphiti.mm.pictograms.ContainerShape;
19
import org.eclipse.graphiti.mm.pictograms.Diagram;
20
import org.eclipse.graphiti.mm.pictograms.FreeFormConnection;
21
import org.eclipse.graphiti.mm.pictograms.PictogramElement;
22
import org.eclipse.graphiti.mm.pictograms.Shape;
23
import org.eclipse.graphiti.services.Graphiti;
24
import org.eclipse.graphiti.ui.editor.DiagramEditor;
25
import org.eclipse.sapphire.modeling.IModelElement;
26
import org.eclipse.sapphire.ui.diagram.editor.DiagramConnectionPart;
27
import org.eclipse.sapphire.ui.diagram.editor.DiagramConnectionTemplate;
28
import org.eclipse.sapphire.ui.diagram.editor.DiagramGeometryWrapper;
29
import org.eclipse.sapphire.ui.diagram.editor.DiagramGeometryWrapper.Bounds;
30
import org.eclipse.sapphire.ui.diagram.editor.DiagramNodePart;
31
import org.eclipse.sapphire.ui.diagram.editor.DiagramNodeTemplate;
32
import org.eclipse.sapphire.ui.diagram.editor.SapphireDiagramEditorPart;
33
import org.eclipse.sapphire.ui.internal.SapphireUiFrameworkPlugin;
34
35
@SuppressWarnings("restriction")
36
public class SapphireDiagramEditor extends DiagramEditor 
37
{
38
	private SapphireDiagramEditorPart diagramPart;
39
	
40
	public SapphireDiagramEditor(SapphireDiagramEditorPart diagramPart)
41
	{
42
		this.diagramPart = diagramPart;
43
	}
44
	
45
	public SapphireDiagramEditorPart getDiagramEditorPart()
46
	{
47
		return this.diagramPart;
48
	}
49
	
50
	@Override
51
	public void doSave(final IProgressMonitor monitor )
52
	{
53
		super.doSave(monitor);
54
		try
55
		{
56
			this.diagramPart.getDiagramGeometry().write();
57
		}
58
        catch( Exception e )
59
        {
60
            SapphireUiFrameworkPlugin.log( e );
61
        }		
62
	}
63
	
64
	public void syncDiagramWithModel()
65
	{
66
		final Diagram diagram = getDiagramTypeProvider().getDiagram();
67
		
68
		TransactionalEditingDomain ted = TransactionUtil.getEditingDomain(diagram);
69
		ted.getCommandStack().execute(new RecordingCommand(ted) 
70
		{
71
			protected void doExecute() 
72
			{		
73
				removeConnections(diagram);
74
				removeNodes(diagram);
75
				
76
				reconstructNodes(diagram);
77
				reconstructConnections(diagram);
78
			}
79
		});
80
	}
81
	
82
	private void removeConnections(final Diagram diagram) 
83
	{
84
		List<Connection> cons = diagram.getConnections();
85
		Iterator<Connection> consIt = cons.iterator();
86
		// don't cause ConcurrentModificationException
87
		Collection<Connection> allCons = new HashSet<Connection>();
88
		while (consIt.hasNext()) 
89
		{
90
			Connection con = consIt.next();
91
			allCons.add(con);
92
		}
93
		consIt = allCons.iterator();
94
		while (consIt.hasNext()) 
95
		{
96
			Connection con = consIt.next();			
97
			Graphiti.getPeService().deletePictogramElement(con);
98
		}		
99
	}
100
	
101
	private void removeNodes(final Diagram diagram)
102
	{
103
		List<Shape> containerShapes = diagram.getChildren();
104
		Iterator<Shape> shapeIt = containerShapes.iterator();
105
		// don't cause ConcurrentModificationException
106
		Collection<Shape> allShapes = new HashSet<Shape>();
107
		while (shapeIt.hasNext())
108
		{
109
			Shape shape = shapeIt.next();
110
			allShapes.add(shape);
111
		}
112
		shapeIt = allShapes.iterator();
113
		while (shapeIt.hasNext())
114
		{
115
			Shape containerShape = shapeIt.next();
116
			Graphiti.getPeService().deletePictogramElement(containerShape);
117
		}
118
	}
119
	
120
	private void reconstructNodes(final Diagram diagram)
121
	{
122
		// Add the nodes back to the diagram thus re-establishing mapping between
123
		// pictograms and business objects
124
		
125
		int nodeX = 50;
126
		int nodeY = 50;
127
		int xInc = 100;
128
		DiagramGeometryWrapper diagramGeometry = SapphireDiagramEditor.this.diagramPart.getDiagramGeometry();
129
		
130
		for (DiagramNodeTemplate nodeTemplate : SapphireDiagramEditor.this.diagramPart.getNodeTemplates())
131
		{
132
			for (DiagramNodePart nodePart : nodeTemplate.getDiagramNodes())
133
			{
134
				Bounds bounds = diagramGeometry.getNode(nodePart);
135
				if (bounds == null)
136
				{
137
					bounds = new Bounds(nodeX, nodeY, -1, -1);
138
					nodeX += xInc;
139
				}
140
				AddContext ctx = new AddContext();
141
				ctx.setNewObject(nodePart);
142
				ctx.setTargetContainer(diagram);
143
				ctx.setWidth(bounds.width);
144
				ctx.setHeight(bounds.height);
145
				ctx.setX(bounds.x);
146
				ctx.setY(bounds.y);
147
				IAddFeature ft = getDiagramTypeProvider().getFeatureProvider().getAddFeature(ctx);						
148
				ft.add(ctx);						
149
			}
150
		}		
151
	}
152
	
153
	private void reconstructConnections(final Diagram diagram)
154
	{
155
		DiagramGeometryWrapper diagramGeometry = SapphireDiagramEditor.this.diagramPart.getDiagramGeometry();
156
		// add the connections back to the diagram
157
		for (DiagramConnectionTemplate connTemplate : SapphireDiagramEditor.this.diagramPart.getConnectionTemplates())
158
		{
159
			for (DiagramConnectionPart connPart : connTemplate.getDiagramConnections())
160
			{
161
				IModelElement endpoint1 = connPart.getEndpoint1();
162
				IModelElement endpoint2 = connPart.getEndpoint2();
163
				DiagramNodePart nodePart1 = getDiagramNode(endpoint1);
164
				DiagramNodePart nodePart2 = getDiagramNode(endpoint2);
165
				if (nodePart1 != null && nodePart2 != null)
166
				{
167
					ContainerShape sourceShape = getContainerShape(nodePart1);
168
					ContainerShape targetShape = getContainerShape(nodePart2);
169
					if (sourceShape != null && targetShape != null)
170
					{
171
						Anchor sourceAnchor = sourceShape.getAnchors().get(0);
172
						Anchor targetAnchor = targetShape.getAnchors().get(0);
173
						AddConnectionContext connCtx = new AddConnectionContext(sourceAnchor, targetAnchor);
174
						connCtx.setNewObject(connPart);
175
						IAddFeature fcfp = getDiagramTypeProvider().getFeatureProvider().getAddFeature(connCtx);
176
						
177
						Connection conn = (Connection)fcfp.add(connCtx);
178
						if (conn instanceof FreeFormConnection && 
179
								diagramGeometry.getConnectionBendpoints(connPart) != null)
180
						{
181
							FreeFormConnection freeConn = (FreeFormConnection)conn;
182
							List<org.eclipse.sapphire.ui.diagram.editor.DiagramGeometryWrapper.Point> bps = 
183
								diagramGeometry.getConnectionBendpoints(connPart);
184
							List<Point> bendpoints = freeConn.getBendpoints();
185
							int index = 0;
186
							for (org.eclipse.sapphire.ui.diagram.editor.DiagramGeometryWrapper.Point pt : bps)
187
							{
188
								Point newPoint = Graphiti.getGaService().createPoint(pt.x, pt.y);										
189
								bendpoints.add(index++, newPoint);										
190
							}
191
						}
192
					}							
193
				}
194
			}
195
		}		
196
	}
197
	
198
	private DiagramNodePart getDiagramNode(IModelElement endpoint)
199
	{
200
		for (DiagramNodeTemplate nodeTemplate : this.diagramPart.getNodeTemplates())
201
		{
202
			for (DiagramNodePart nodePart : nodeTemplate.getDiagramNodes())
203
			{
204
				IModelElement element = nodePart.getLocalModelElement();
205
				if (element == endpoint)
206
				{
207
					return nodePart;
208
				}
209
			}
210
		}		
211
		return null;	
212
	}
213
	
214
	private ContainerShape getContainerShape(Object bo)
215
	{
216
		ContainerShape containerShape = null;
217
		PictogramElement [] pictograms = 
218
			getDiagramTypeProvider().getFeatureProvider().getAllPictogramElementsForBusinessObject(bo);
219
		for (PictogramElement pictogram : pictograms)
220
		{
221
			if (pictogram instanceof ContainerShape)
222
			{
223
				containerShape = (ContainerShape)pictogram;
224
				break;
225
			}
226
		}
227
		return containerShape;
228
	}
229
	
230
}
(-)src/org/eclipse/sapphire/ui/diagram/graphiti/editor/SapphireDiagramEditorFactory.java (+119 lines)
Added Link Here
1
package org.eclipse.sapphire.ui.diagram.graphiti.editor;
2
3
import java.io.ByteArrayInputStream;
4
import java.io.IOException;
5
import java.util.Iterator;
6
7
import org.eclipse.core.resources.IFile;
8
import org.eclipse.core.resources.IFolder;
9
import org.eclipse.core.resources.IProject;
10
import org.eclipse.core.runtime.CoreException;
11
import org.eclipse.core.runtime.Path;
12
import org.eclipse.emf.common.util.EList;
13
import org.eclipse.emf.common.util.URI;
14
import org.eclipse.emf.ecore.EObject;
15
import org.eclipse.emf.ecore.resource.Resource;
16
import org.eclipse.emf.ecore.resource.ResourceSet;
17
import org.eclipse.emf.transaction.TransactionalEditingDomain;
18
import org.eclipse.graphiti.mm.pictograms.Diagram;
19
import org.eclipse.graphiti.services.Graphiti;
20
import org.eclipse.graphiti.ui.editor.DiagramEditorFactory;
21
import org.eclipse.graphiti.ui.internal.services.GraphitiUiInternal;
22
import org.eclipse.graphiti.ui.services.GraphitiUi;
23
import org.eclipse.sapphire.modeling.util.internal.FileUtil;
24
import org.eclipse.sapphire.ui.diagram.graphiti.GraphitiFileService;
25
import org.eclipse.ui.IEditorInput;
26
import org.eclipse.ui.IFileEditorInput;
27
28
public class SapphireDiagramEditorFactory 
29
{
30
	public static final String SAPPHIRE_DIAGRAM_TYPE = "sapphireDiagram";
31
	
32
	public static SapphireDiagramEditorInput createEditorInput(IEditorInput otherInput) 
33
		throws CoreException
34
	{
35
		if (otherInput instanceof SapphireDiagramEditorInput)
36
		{
37
			return (SapphireDiagramEditorInput)otherInput;
38
		}
39
		else if (otherInput instanceof IFileEditorInput)
40
		{
41
			final IFileEditorInput fileInput = (IFileEditorInput) otherInput;
42
			final IFile file = fileInput.getFile();
43
			final IProject project = file.getProject();
44
			
45
			final IFolder diagramFolder = project.getFolder(".settings/diagrams/");
46
			String fileName = file.getName();
47
			if (fileName.endsWith(".xml"))
48
			{
49
				fileName = fileName.substring(0, fileName.indexOf(".xml"));
50
			}
51
			
52
			TransactionalEditingDomain domain = null;
53
			ResourceSet resourceSet = null;
54
			Diagram diagram = null;
55
			URI diagramFileUri = null;
56
			
57
			// create diagram file if it doesn't exist
58
			final IFile diagramFile = diagramFolder.getFile(fileName + ".xmi");
59
			if (!diagramFile.exists())
60
			{
61
				FileUtil.mkdirs( diagramFile.getParent().getLocation().toFile() );
62
				diagramFile.create( new ByteArrayInputStream(new byte[0]), true, null );
63
				
64
				// Create Diagram Obj
65
				diagram = Graphiti.getPeCreateService().createDiagram(
66
						SAPPHIRE_DIAGRAM_TYPE, fileName, 10, false);
67
				diagramFileUri = URI.createPlatformResourceURI(diagramFile.getFullPath().toString(), true);
68
				domain = GraphitiFileService.createEmfFileForDiagram(diagramFileUri, diagram);
69
			}
70
			else
71
			{			
72
				domain = DiagramEditorFactory.createResourceSetAndEditingDomain();
73
				resourceSet = domain.getResourceSet();
74
			
75
				diagramFileUri = GraphitiUiInternal.getEmfService().getFileURI(diagramFile, resourceSet);
76
				if (diagramFileUri != null)
77
				{
78
					final Resource resource = resourceSet.createResource(diagramFileUri);
79
					try
80
					{
81
						resource.load(null);
82
					}
83
					catch (IOException ie)
84
					{
85
						ie.printStackTrace();
86
					}
87
					EList<EObject> objs = resource.getContents();
88
					Iterator<EObject> it = objs.iterator();					
89
					while (it.hasNext()) 
90
					{
91
						EObject obj = it.next();
92
						if ((obj == null) && !Diagram.class.isInstance(obj))
93
							continue;
94
						diagram = (Diagram)obj;
95
						break;
96
					}
97
				}				
98
			}
99
			// create diagram node position file if it doesn't exist
100
			IFile nodePosFile = diagramFile.getParent().getFile(new Path(fileName + ".np"));
101
			if (!nodePosFile.exists())
102
			{
103
				nodePosFile.create( new ByteArrayInputStream(new byte[0]), true, null );
104
			}
105
106
			// create sapphire diagram editor input
107
			if (diagram != null)
108
			{
109
				String providerId = GraphitiUi.getExtensionManager().getDiagramTypeProviderId(diagram.getDiagramTypeId());
110
				final SapphireDiagramEditorInput diagramEditorInput = 
111
					SapphireDiagramEditorInput.createEditorInput(diagram, domain, providerId, false);
112
				diagramEditorInput.setNodePositionFile(nodePosFile);
113
				return diagramEditorInput;
114
			}
115
		}
116
		return null;
117
	}
118
119
}
(-)src/org/eclipse/sapphire/ui/diagram/graphiti/editor/SapphireDiagramEditorInput.java (+91 lines)
Added Link Here
1
package org.eclipse.sapphire.ui.diagram.graphiti.editor;
2
3
import org.eclipse.core.resources.IFile;
4
import org.eclipse.emf.common.util.URI;
5
import org.eclipse.emf.ecore.EObject;
6
import org.eclipse.emf.ecore.resource.Resource;
7
import org.eclipse.emf.ecore.resource.ResourceSet;
8
import org.eclipse.emf.transaction.TransactionalEditingDomain;
9
import org.eclipse.graphiti.mm.pictograms.Diagram;
10
import org.eclipse.graphiti.ui.editor.DiagramEditorInput;
11
12
public class SapphireDiagramEditorInput extends DiagramEditorInput 
13
{
14
	private Diagram diagram;
15
	private IFile nodePositionFile;
16
	
17
	public SapphireDiagramEditorInput(Diagram diagram, String diagramUriString,
18
			TransactionalEditingDomain domain, String providerId,
19
			boolean disposeEditingDomain) 
20
	{
21
		super(diagramUriString, domain, providerId, disposeEditingDomain);
22
		this.diagram = diagram;
23
	}
24
25
	public SapphireDiagramEditorInput(Diagram diagram, URI diagramUri,
26
			TransactionalEditingDomain domain, String providerId,
27
			boolean disposeEditingDomain) 
28
	{
29
		super(diagramUri, domain, providerId, disposeEditingDomain);
30
		this.diagram = diagram;
31
	}
32
	
33
	public Diagram getDiagram() 
34
	{
35
		return this.diagram;
36
	}	
37
	
38
	public TransactionalEditingDomain getEditingDomain() 
39
	{
40
		return this.editingDomain;
41
	}
42
	
43
	@SuppressWarnings("rawtypes")
44
	public Object getAdapter(Class adapter) 
45
	{
46
		if (EObject.class.isAssignableFrom(adapter)) 
47
		{
48
			return getDiagram();
49
		} 
50
		else if (Diagram.class.isAssignableFrom(adapter)) 
51
		{
52
			return getDiagram();
53
		}
54
		else if (TransactionalEditingDomain.class.isAssignableFrom(adapter)) 
55
		{
56
			return getEditingDomain();
57
		} 
58
		else if (ResourceSet.class.isAssignableFrom(adapter)) 
59
		{
60
			return getEditingDomain().getResourceSet();
61
		}		
62
		return null;
63
	}
64
	
65
	public IFile getNodePositionFile()
66
	{
67
		return this.nodePositionFile;
68
	}
69
	
70
	public void setNodePositionFile(IFile file)
71
	{
72
		this.nodePositionFile = file;
73
	}
74
	
75
	
76
	public static SapphireDiagramEditorInput createEditorInput(Diagram diagram, 
77
			TransactionalEditingDomain domain, String providerId, boolean disposeEditingDomain) 
78
	{
79
		final Resource resource = diagram.eResource();
80
		if (resource == null) {
81
			throw new IllegalArgumentException();
82
		}
83
		final String fragment = resource.getURIFragment(diagram);
84
		final URI fragmentUri = resource.getURI().appendFragment(fragment);
85
		SapphireDiagramEditorInput diagramEditorInput;
86
		diagramEditorInput = new SapphireDiagramEditorInput(diagram, fragmentUri,
87
				domain, providerId, disposeEditingDomain);
88
		return diagramEditorInput;
89
	}	
90
	
91
}
(-)src/org/eclipse/sapphire/ui/diagram/graphiti/features/SapphireAddBendpointFeature.java (+33 lines)
Added Link Here
1
package org.eclipse.sapphire.ui.diagram.graphiti.features;
2
3
import org.eclipse.graphiti.features.IFeatureProvider;
4
import org.eclipse.graphiti.features.context.IAddBendpointContext;
5
import org.eclipse.graphiti.features.impl.DefaultAddBendpointFeature;
6
import org.eclipse.graphiti.mm.pictograms.FreeFormConnection;
7
import org.eclipse.sapphire.ui.diagram.editor.DiagramConnectionPart;
8
import org.eclipse.sapphire.ui.diagram.editor.DiagramGeometryWrapper;
9
import org.eclipse.sapphire.ui.diagram.graphiti.providers.SapphireDiagramFeatureProvider;
10
11
public class SapphireAddBendpointFeature extends DefaultAddBendpointFeature 
12
{
13
	public SapphireAddBendpointFeature(IFeatureProvider fp)
14
	{
15
		super(fp);
16
	}
17
	
18
	@Override
19
	public void addBendpoint(IAddBendpointContext context) 
20
	{
21
		super.addBendpoint(context);
22
		
23
		FreeFormConnection freeFormConnection = context.getConnection();
24
		Object bo = getBusinessObjectForPictogramElement(freeFormConnection);
25
		if (bo instanceof DiagramConnectionPart)
26
		{
27
			DiagramGeometryWrapper dg = 
28
				((SapphireDiagramFeatureProvider)getFeatureProvider()).getDiagramGeometry();
29
			dg.addConnectionBendpoint((DiagramConnectionPart)bo, 
30
					context.getBendpointIndex(), context.getX(), context.getY());
31
		}		
32
	}	
33
}
(-)src/org/eclipse/sapphire/ui/diagram/graphiti/features/SapphireAddConnectionFeature.java (+97 lines)
Added Link Here
1
package org.eclipse.sapphire.ui.diagram.graphiti.features;
2
3
import org.eclipse.graphiti.features.IDirectEditingInfo;
4
import org.eclipse.graphiti.features.IFeatureProvider;
5
import org.eclipse.graphiti.features.context.IAddConnectionContext;
6
import org.eclipse.graphiti.features.context.IAddContext;
7
import org.eclipse.graphiti.features.impl.AbstractAddFeature;
8
import org.eclipse.graphiti.mm.GraphicsAlgorithmContainer;
9
import org.eclipse.graphiti.mm.algorithms.Polygon;
10
import org.eclipse.graphiti.mm.algorithms.Polyline;
11
import org.eclipse.graphiti.mm.algorithms.Text;
12
import org.eclipse.graphiti.mm.pictograms.Connection;
13
import org.eclipse.graphiti.mm.pictograms.ConnectionDecorator;
14
import org.eclipse.graphiti.mm.pictograms.PictogramElement;
15
import org.eclipse.graphiti.services.Graphiti;
16
import org.eclipse.graphiti.services.IGaService;
17
import org.eclipse.graphiti.services.IPeCreateService;
18
import org.eclipse.graphiti.util.IColorConstant;
19
import org.eclipse.sapphire.ui.diagram.editor.DiagramConnectionPart;
20
21
public class SapphireAddConnectionFeature extends AbstractAddFeature 
22
{
23
	public SapphireAddConnectionFeature(IFeatureProvider fp)
24
	{
25
		super(fp);
26
	}
27
	
28
	public boolean canAdd(IAddContext context) 
29
	{
30
		// return true if given business object is an DiagramConnectionPart
31
		// note, that the context must be an instance of IAddConnectionContext
32
		if (context instanceof IAddConnectionContext && 
33
				context.getNewObject() instanceof DiagramConnectionPart) 
34
		{
35
			return true;
36
		}
37
		return false;
38
	}
39
40
	public PictogramElement add(IAddContext context) 
41
	{
42
		IAddConnectionContext addConContext = (IAddConnectionContext) context;
43
		DiagramConnectionPart connectionPart = (DiagramConnectionPart) context.getNewObject();
44
45
		IPeCreateService peCreateService = Graphiti.getPeCreateService();
46
		// CONNECTION WITH POLYLINE
47
		Connection connection = peCreateService.createFreeFormConnection(getDiagram());
48
		connection.setStart(addConContext.getSourceAnchor());
49
		connection.setEnd(addConContext.getTargetAnchor());
50
51
		IGaService gaService = Graphiti.getGaService();
52
		Polyline polyline = gaService.createPolyline(connection);
53
		polyline.setForeground(manageColor(IColorConstant.BLACK));
54
		polyline.setLineWidth(1);
55
       
56
		// create link and wire it
57
		link(connection, connectionPart);
58
59
		// add dynamic text decorator for the reference name
60
		ConnectionDecorator textDecorator = peCreateService.createConnectionDecorator(connection, true, 0.5, true);
61
		Text text = gaService.createDefaultText(textDecorator);
62
		text.setForeground(manageColor(IColorConstant.BLACK));
63
		gaService.setLocation(text, 10, 0);		
64
		
65
		text.setValue(connectionPart.getLabel());
66
67
		// add static graphical decorators (composition and navigable)
68
		ConnectionDecorator cd;
69
		cd = peCreateService.createConnectionDecorator(connection, false, 1.0, true);
70
		createArrow(cd);
71
72
        // provide information to support direct-editing directly 
73
74
        // after object creation (must be activated additionally)
75
        IDirectEditingInfo directEditingInfo = getFeatureProvider().getDirectEditingInfo();
76
77
        // set container shape for direct editing after object creation
78
        directEditingInfo.setMainPictogramElement(connection);
79
80
        // set shape and graphics algorithm where the editor for
81
        // direct editing shall be opened after object creation
82
        directEditingInfo.setPictogramElement(textDecorator);
83
        directEditingInfo.setGraphicsAlgorithm(text);
84
		
85
		return connection;
86
	}
87
88
	private Polygon createArrow(GraphicsAlgorithmContainer gaContainer) 
89
	{
90
		Polygon polygon = Graphiti.getGaCreateService().createPolygon(gaContainer, new int[] { -8, 4, 0, 0, -8, -4, -5, 0 });
91
		polygon.setBackground(manageColor(IColorConstant.BLACK));
92
		//polygon.setStyle(StyleUtil.getStyleForADFView(getDiagram()));
93
		polygon.setFilled(true);
94
		return polygon;
95
	}
96
	
97
}
(-)src/org/eclipse/sapphire/ui/diagram/graphiti/features/SapphireAddNodeFeature.java (+121 lines)
Added Link Here
1
package org.eclipse.sapphire.ui.diagram.graphiti.features;
2
3
import org.eclipse.graphiti.features.IDirectEditingInfo;
4
import org.eclipse.graphiti.features.IFeatureProvider;
5
import org.eclipse.graphiti.features.context.IAddContext;
6
import org.eclipse.graphiti.features.impl.AbstractAddShapeFeature;
7
import org.eclipse.graphiti.mm.algorithms.Polyline;
8
import org.eclipse.graphiti.mm.algorithms.Rectangle;
9
import org.eclipse.graphiti.mm.algorithms.Text;
10
import org.eclipse.graphiti.mm.algorithms.styles.Orientation;
11
import org.eclipse.graphiti.mm.pictograms.ContainerShape;
12
import org.eclipse.graphiti.mm.pictograms.Diagram;
13
import org.eclipse.graphiti.mm.pictograms.PictogramElement;
14
import org.eclipse.graphiti.mm.pictograms.Shape;
15
import org.eclipse.graphiti.services.Graphiti;
16
import org.eclipse.graphiti.services.IGaService;
17
import org.eclipse.graphiti.services.IPeCreateService;
18
import org.eclipse.graphiti.util.ColorConstant;
19
import org.eclipse.graphiti.util.IColorConstant;
20
import org.eclipse.sapphire.ui.diagram.editor.DiagramGeometryWrapper;
21
import org.eclipse.sapphire.ui.diagram.editor.DiagramNodePart;
22
import org.eclipse.sapphire.ui.diagram.graphiti.providers.SapphireDiagramFeatureProvider;
23
24
public class SapphireAddNodeFeature extends AbstractAddShapeFeature 
25
{
26
    private static final IColorConstant CLASS_TEXT_FOREGROUND = new ColorConstant(51, 51, 153);
27
    private static final IColorConstant CLASS_FOREGROUND = new ColorConstant(255, 102, 0);
28
    private static final IColorConstant CLASS_BACKGROUND =  new ColorConstant(255, 204, 153);
29
	
30
	public SapphireAddNodeFeature(IFeatureProvider fp)
31
	{
32
		super(fp);
33
	}
34
	
35
	public boolean canAdd(IAddContext context) 
36
	{
37
		Object newObj = context.getNewObject();
38
		if (newObj instanceof DiagramNodePart && context.getTargetContainer() instanceof Diagram)
39
		{
40
			return true;
41
		}
42
		return false;
43
	}
44
45
	public PictogramElement add(IAddContext context)
46
	{
47
		DiagramNodePart nodePart = (DiagramNodePart)context.getNewObject();
48
		final Diagram targetDiagram = (Diagram) context.getTargetContainer();
49
		
50
        // CONTAINER SHAPE WITH RECTANGLE
51
        IPeCreateService peCreateService = Graphiti.getPeCreateService();
52
        ContainerShape containerShape =  peCreateService.createContainerShape(targetDiagram, true);
53
54
        // define a default size for the shape
55
        int width = 100;
56
        int height = 50; 
57
        IGaService gaService = Graphiti.getGaService();
58
        {
59
            // create and set graphics algorithm
60
            Rectangle rectangle = gaService.createRectangle(containerShape);
61
            rectangle.setForeground(manageColor(CLASS_FOREGROUND));
62
            rectangle.setBackground(manageColor(CLASS_BACKGROUND));
63
            rectangle.setLineWidth(2);
64
            gaService.setLocationAndSize(rectangle,
65
                context.getX(), context.getY(), width, height);
66
 
67
            // create link and wire it
68
            link(containerShape, nodePart);
69
        }
70
		
71
        // SHAPE WITH LINE
72
        {
73
            // create shape for line
74
            Shape shape = peCreateService.createShape(containerShape, false);
75
 
76
            // create and set graphics algorithm
77
            Polyline polyline =
78
                gaService.createPolyline(shape, new int[] { 0, 20, width, 20 });
79
            polyline.setForeground(manageColor(CLASS_FOREGROUND));
80
            polyline.setLineWidth(2);
81
        }
82
 
83
        // SHAPE WITH TEXT
84
        {
85
            // create shape for text
86
            Shape shape = peCreateService.createShape(containerShape, false);
87
 
88
            // create and set text graphics algorithm
89
            Text text = gaService.createDefaultText(shape, nodePart.getLabel());
90
            text.setForeground(manageColor(CLASS_TEXT_FOREGROUND));
91
            text.setHorizontalAlignment(Orientation.ALIGNMENT_CENTER);
92
            text.setVerticalAlignment(Orientation.ALIGNMENT_CENTER);
93
            //text.getFont().setBold(true);
94
            gaService.setLocationAndSize(text, 0, 0, width, 20);
95
 
96
            // create link and wire it
97
            link(shape, nodePart);
98
            
99
			// provide information to support direct-editing directly
100
			// after object creation (must be activated additionally)
101
			final IDirectEditingInfo directEditingInfo = getFeatureProvider().getDirectEditingInfo();
102
			// set container shape for direct editing after object creation
103
			directEditingInfo.setMainPictogramElement(containerShape);
104
			// set shape and graphics algorithm where the editor for
105
			// direct editing shall be opened after object creation
106
			directEditingInfo.setPictogramElement(shape);
107
			directEditingInfo.setGraphicsAlgorithm(text);            
108
        }
109
        
110
        // add a chopbox anchor to the shape
111
        peCreateService.createChopboxAnchor(containerShape);
112
        
113
        // Save the node bounds
114
        DiagramGeometryWrapper diagramGeometry = 
115
        	((SapphireDiagramFeatureProvider)getFeatureProvider()).getDiagramGeometry();
116
        diagramGeometry.addNode(nodePart, context.getX(), context.getY(), width, height);
117
        
118
		return containerShape;
119
	}
120
121
}
(-)src/org/eclipse/sapphire/ui/diagram/graphiti/features/SapphireCreateConnectionFeature.java (+92 lines)
Added Link Here
1
package org.eclipse.sapphire.ui.diagram.graphiti.features;
2
3
import org.eclipse.graphiti.features.IFeatureProvider;
4
import org.eclipse.graphiti.features.context.ICreateConnectionContext;
5
import org.eclipse.graphiti.features.context.impl.AddConnectionContext;
6
import org.eclipse.graphiti.features.impl.AbstractCreateConnectionFeature;
7
import org.eclipse.graphiti.mm.pictograms.Anchor;
8
import org.eclipse.graphiti.mm.pictograms.Connection;
9
import org.eclipse.sapphire.ui.SapphirePart;
10
import org.eclipse.sapphire.ui.diagram.editor.DiagramConnectionPart;
11
import org.eclipse.sapphire.ui.diagram.editor.DiagramConnectionTemplate;
12
import org.eclipse.sapphire.ui.diagram.editor.DiagramNodePart;
13
14
public class SapphireCreateConnectionFeature extends AbstractCreateConnectionFeature 
15
{
16
	private DiagramConnectionTemplate connectionTemplate;
17
	
18
	public SapphireCreateConnectionFeature(IFeatureProvider fp, DiagramConnectionTemplate connectionTemplate)
19
	{
20
		super(fp, connectionTemplate.getToolPaletteLabel(), connectionTemplate.getToolPaletteDesc());
21
		this.connectionTemplate = connectionTemplate;		
22
	}
23
	
24
	public boolean canCreate(ICreateConnectionContext context) 
25
	{
26
		// return true if both anchors belong to an IModelElement
27
		// and those model elements are not identical
28
		SapphirePart source = getEndpoint(context.getSourceAnchor());
29
		SapphirePart target = getEndpoint(context.getTargetAnchor());
30
		if (source instanceof DiagramNodePart && 
31
				source instanceof DiagramNodePart && source != target) 
32
		{
33
			return true;
34
		}
35
		return false;
36
	}
37
38
	public Connection create(ICreateConnectionContext context) 
39
	{
40
		Connection newConnection = null;
41
42
		// get model elements which should be connected
43
		SapphirePart source = getEndpoint(context.getSourceAnchor());
44
		SapphirePart target = getEndpoint(context.getTargetAnchor());
45
46
		if (source instanceof DiagramNodePart && target instanceof DiagramNodePart) 
47
		{
48
			DiagramNodePart sourceNode = (DiagramNodePart)source;
49
			DiagramNodePart targetNode = (DiagramNodePart)target;
50
			// create new business object
51
			DiagramConnectionPart connectionPart = 
52
				this.connectionTemplate.createNewDiagramConnection(sourceNode, targetNode);
53
54
			// add connection for business object
55
			AddConnectionContext addContext = new AddConnectionContext(context.getSourceAnchor(), context.getTargetAnchor());
56
			addContext.setNewObject(connectionPart);
57
			newConnection = (Connection) getFeatureProvider().addIfPossible(addContext);
58
			
59
	        // activate direct editing after object creation
60
	        getFeatureProvider().getDirectEditingInfo().setActive(true);
61
		}
62
63
		return newConnection;
64
	}
65
66
	public boolean canStartConnection(ICreateConnectionContext context) 
67
	{
68
		// return true if start anchor belongs to an IModelElement
69
		if (getEndpoint(context.getSourceAnchor()) instanceof DiagramNodePart) 
70
		{
71
			return true;
72
		}
73
		return false;
74
	}
75
76
	/**
77
	 * Returns the SapphirePart belonging to the anchor, or null if not available.
78
	 */
79
	private SapphirePart getEndpoint(Anchor anchor) 
80
	{
81
		if (anchor != null) 
82
		{
83
			Object obj = getBusinessObjectForPictogramElement(anchor.getParent());
84
			if (obj instanceof SapphirePart) 
85
			{
86
				return (SapphirePart) obj;
87
			}
88
		}
89
		return null;
90
	}
91
	
92
}
(-)src/org/eclipse/sapphire/ui/diagram/graphiti/features/SapphireCreateNodeFeature.java (+32 lines)
Added Link Here
1
package org.eclipse.sapphire.ui.diagram.graphiti.features;
2
3
import org.eclipse.graphiti.features.IFeatureProvider;
4
import org.eclipse.graphiti.features.context.ICreateContext;
5
import org.eclipse.graphiti.features.impl.AbstractCreateFeature;
6
import org.eclipse.graphiti.mm.pictograms.Diagram;
7
import org.eclipse.sapphire.ui.diagram.editor.DiagramNodePart;
8
import org.eclipse.sapphire.ui.diagram.editor.DiagramNodeTemplate;
9
10
public class SapphireCreateNodeFeature extends AbstractCreateFeature 
11
{
12
	private DiagramNodeTemplate nodeTemplate;
13
	
14
	public SapphireCreateNodeFeature(IFeatureProvider fp, DiagramNodeTemplate nodeTemplate)
15
	{
16
		super(fp, nodeTemplate.getToolPaletteLabel(), nodeTemplate.getToolPaletteDesc());
17
		this.nodeTemplate = nodeTemplate;		
18
	}
19
20
	public boolean canCreate(ICreateContext context) 
21
	{		
22
		return context.getTargetContainer() instanceof Diagram;
23
	}
24
25
	public Object[] create(ICreateContext context) 
26
	{
27
		DiagramNodePart nodePart = this.nodeTemplate.createNewDiagramNode();
28
		
29
		addGraphicalRepresentation(context, nodePart);
30
		return new Object[] { nodePart };
31
	}
32
}
(-)src/org/eclipse/sapphire/ui/diagram/graphiti/features/SapphireDirectEditConnectionFeature.java (+45 lines)
Added Link Here
1
package org.eclipse.sapphire.ui.diagram.graphiti.features;
2
3
import org.eclipse.graphiti.features.IFeatureProvider;
4
import org.eclipse.graphiti.features.context.IDirectEditingContext;
5
import org.eclipse.graphiti.features.impl.AbstractDirectEditingFeature;
6
import org.eclipse.graphiti.mm.pictograms.PictogramElement;
7
import org.eclipse.sapphire.ui.diagram.editor.DiagramConnectionPart;
8
9
public class SapphireDirectEditConnectionFeature extends AbstractDirectEditingFeature 
10
{
11
	public SapphireDirectEditConnectionFeature(IFeatureProvider fp)
12
	{
13
		super(fp);
14
	}
15
	
16
	public int getEditingType() 
17
	{
18
		return TYPE_TEXT;
19
	}
20
21
	public String getInitialValue(IDirectEditingContext context) 
22
	{
23
		PictogramElement pe = (PictogramElement)context.getPictogramElement().eContainer();
24
		Object bo = getBusinessObjectForPictogramElement(pe);
25
		if (bo instanceof DiagramConnectionPart)
26
		{
27
			DiagramConnectionPart connPart = (DiagramConnectionPart)bo;
28
			return connPart.getLabel();
29
		}
30
		return null;
31
	}
32
33
	public void setValue(String value, IDirectEditingContext context) 
34
	{
35
		PictogramElement pe = (PictogramElement)context.getPictogramElement().eContainer();
36
		Object bo = getBusinessObjectForPictogramElement(pe);
37
		if (bo instanceof DiagramConnectionPart)
38
		{
39
			DiagramConnectionPart connPart = (DiagramConnectionPart)bo;
40
			connPart.setLabel(value);
41
			updatePictogramElement(pe);
42
		}
43
	}
44
45
}
(-)src/org/eclipse/sapphire/ui/diagram/graphiti/features/SapphireDirectEditNodeFeature.java (+103 lines)
Added Link Here
1
package org.eclipse.sapphire.ui.diagram.graphiti.features;
2
3
import java.util.Iterator;
4
5
import org.eclipse.graphiti.features.IFeatureProvider;
6
import org.eclipse.graphiti.features.context.IDirectEditingContext;
7
import org.eclipse.graphiti.features.impl.AbstractDirectEditingFeature;
8
import org.eclipse.graphiti.mm.algorithms.GraphicsAlgorithm;
9
import org.eclipse.graphiti.mm.algorithms.Text;
10
import org.eclipse.graphiti.mm.pictograms.Anchor;
11
import org.eclipse.graphiti.mm.pictograms.Connection;
12
import org.eclipse.graphiti.mm.pictograms.PictogramElement;
13
import org.eclipse.graphiti.mm.pictograms.Shape;
14
import org.eclipse.graphiti.services.Graphiti;
15
import org.eclipse.sapphire.modeling.IModelElement;
16
import org.eclipse.sapphire.ui.diagram.editor.DiagramConnectionPart;
17
import org.eclipse.sapphire.ui.diagram.editor.DiagramNodePart;
18
19
public class SapphireDirectEditNodeFeature extends AbstractDirectEditingFeature 
20
{
21
	public SapphireDirectEditNodeFeature(IFeatureProvider fp)
22
	{
23
		super(fp);
24
	}
25
	
26
	public int getEditingType() 
27
	{
28
		return TYPE_TEXT;
29
	}
30
31
	@Override
32
	public boolean canDirectEdit(IDirectEditingContext context) 
33
	{
34
		PictogramElement pe = context.getPictogramElement();
35
		Object bo = getBusinessObjectForPictogramElement(pe);
36
		GraphicsAlgorithm ga = context.getGraphicsAlgorithm();
37
		// support direct editing, if it is a DiagramNodePart, and the user clicked
38
		// directly on the text and not somewhere else in the rectangle
39
		if (bo instanceof DiagramNodePart && ga instanceof Text) 
40
		{
41
			return true;
42
		}
43
		// direct editing not supported in all other cases
44
		return false;
45
	}
46
47
	public String getInitialValue(IDirectEditingContext context) 
48
	{
49
		// return the current label of the node
50
		PictogramElement pe = context.getPictogramElement();
51
		DiagramNodePart nodePart = (DiagramNodePart)getBusinessObjectForPictogramElement(pe);
52
		return nodePart.getLabel();
53
	}
54
55
	public void setValue(String value, IDirectEditingContext context) 
56
	{
57
		PictogramElement pe = context.getPictogramElement();
58
				
59
		if (pe.eContainer() instanceof Shape)
60
		{
61
			DiagramNodePart nodePart = (DiagramNodePart)getBusinessObjectForPictogramElement(pe);
62
			nodePart.setLabel(value);
63
			IModelElement nodeElement = nodePart.getLocalModelElement();
64
			Shape nodeShape = (Shape)pe.eContainer();
65
			
66
			// go through all the connections associated with this bo and update them
67
			for (Iterator<Anchor> iter = nodeShape.getAnchors().iterator(); iter.hasNext();) 
68
			{
69
				Anchor anchor = iter.next();
70
				for (Iterator<Connection> iterator = Graphiti.getPeService().getAllConnections(anchor).iterator(); iterator.hasNext();) 
71
				{
72
					Connection connection = iterator.next();
73
					Object bo = getBusinessObjectForPictogramElement(connection);
74
					if (bo instanceof DiagramConnectionPart)
75
					{
76
						DiagramConnectionPart connectionPart = (DiagramConnectionPart)bo;
77
						IModelElement endpoint1 = connectionPart.getEndpoint1();
78
						IModelElement endpoint2 = connectionPart.getEndpoint2();
79
						if (endpoint1 == nodeElement)
80
						{
81
							connectionPart.refreshEndpoint1();
82
						}
83
						else if (endpoint2 == nodeElement)
84
						{
85
							connectionPart.refreshEndpoint2();
86
						}
87
					}
88
				}
89
			}
90
			
91
		}
92
				
93
		// Explicitly update the shape to display the new value in the diagram
94
		// Note, that this might not be necessary in future versions of Graphiti
95
		// (currently in discussion)
96
97
		// we know, that pe is the Shape of the Text, so its container is the
98
		// main shape of the node
99
		updatePictogramElement(((Shape)pe).getContainer());
100
101
	}
102
103
}
(-)src/org/eclipse/sapphire/ui/diagram/graphiti/features/SapphireMoveBendpointFeature.java (+36 lines)
Added Link Here
1
package org.eclipse.sapphire.ui.diagram.graphiti.features;
2
3
import org.eclipse.graphiti.features.IFeatureProvider;
4
import org.eclipse.graphiti.features.context.IMoveBendpointContext;
5
import org.eclipse.graphiti.features.impl.DefaultMoveBendpointFeature;
6
import org.eclipse.graphiti.mm.pictograms.FreeFormConnection;
7
import org.eclipse.sapphire.ui.diagram.editor.DiagramConnectionPart;
8
import org.eclipse.sapphire.ui.diagram.editor.DiagramGeometryWrapper;
9
import org.eclipse.sapphire.ui.diagram.graphiti.providers.SapphireDiagramFeatureProvider;
10
11
public class SapphireMoveBendpointFeature extends DefaultMoveBendpointFeature 
12
{
13
	public SapphireMoveBendpointFeature(IFeatureProvider fp)
14
	{
15
		super(fp);
16
	}
17
	
18
	@Override
19
	public boolean moveBendpoint(IMoveBendpointContext context) 
20
	{
21
		boolean ret = false;
22
		super.moveBendpoint(context);
23
		
24
		FreeFormConnection freeFormConnection = context.getConnection();
25
		Object bo = getBusinessObjectForPictogramElement(freeFormConnection);
26
		if (bo instanceof DiagramConnectionPart)
27
		{
28
			DiagramGeometryWrapper dg = 
29
				((SapphireDiagramFeatureProvider)getFeatureProvider()).getDiagramGeometry();
30
			ret = dg.setConnectionBendpoint((DiagramConnectionPart)bo, 
31
					context.getBendpointIndex(), context.getX(), context.getY());
32
		}		
33
		return ret;
34
	}
35
	
36
}
(-)src/org/eclipse/sapphire/ui/diagram/graphiti/features/SapphireMoveNodeFeature.java (+31 lines)
Added Link Here
1
package org.eclipse.sapphire.ui.diagram.graphiti.features;
2
3
import org.eclipse.graphiti.features.IFeatureProvider;
4
import org.eclipse.graphiti.features.context.IMoveShapeContext;
5
import org.eclipse.graphiti.features.impl.DefaultMoveShapeFeature;
6
import org.eclipse.graphiti.mm.pictograms.PictogramElement;
7
import org.eclipse.sapphire.ui.diagram.editor.DiagramGeometryWrapper;
8
import org.eclipse.sapphire.ui.diagram.editor.DiagramNodePart;
9
import org.eclipse.sapphire.ui.diagram.graphiti.providers.SapphireDiagramFeatureProvider;
10
11
public class SapphireMoveNodeFeature extends DefaultMoveShapeFeature 
12
{
13
	public SapphireMoveNodeFeature(IFeatureProvider fp)
14
	{
15
		super(fp);
16
	}
17
	
18
	@Override
19
	protected void internalMove(IMoveShapeContext context) 
20
	{
21
		super.internalMove(context);
22
 		PictogramElement pe = context.getPictogramElement();
23
 		Object bo = getBusinessObjectForPictogramElement(pe);
24
 		if (bo instanceof DiagramNodePart)
25
 		{
26
			DiagramGeometryWrapper dg = 
27
				((SapphireDiagramFeatureProvider)getFeatureProvider()).getDiagramGeometry();
28
			dg.updateNode((DiagramNodePart)bo, context.getX(), context.getY());
29
 		}
30
	}
31
}
(-)src/org/eclipse/sapphire/ui/diagram/graphiti/features/SapphireRemoveBendpointFeature.java (+34 lines)
Added Link Here
1
package org.eclipse.sapphire.ui.diagram.graphiti.features;
2
3
import org.eclipse.graphiti.features.IFeatureProvider;
4
import org.eclipse.graphiti.features.context.IRemoveBendpointContext;
5
import org.eclipse.graphiti.features.impl.DefaultRemoveBendpointFeature;
6
import org.eclipse.graphiti.mm.pictograms.FreeFormConnection;
7
import org.eclipse.sapphire.ui.diagram.editor.DiagramConnectionPart;
8
import org.eclipse.sapphire.ui.diagram.editor.DiagramGeometryWrapper;
9
import org.eclipse.sapphire.ui.diagram.graphiti.providers.SapphireDiagramFeatureProvider;
10
11
public class SapphireRemoveBendpointFeature extends DefaultRemoveBendpointFeature 
12
{
13
	public SapphireRemoveBendpointFeature(IFeatureProvider fp)
14
	{
15
		super(fp);
16
	}
17
	
18
	@Override
19
	public void removeBendpoint(IRemoveBendpointContext context) 
20
	{
21
		super.removeBendpoint(context);
22
		
23
		FreeFormConnection freeFormConnection = context.getConnection();
24
		Object bo = getBusinessObjectForPictogramElement(freeFormConnection);
25
		if (bo instanceof DiagramConnectionPart)
26
		{
27
			DiagramGeometryWrapper dg = 
28
				((SapphireDiagramFeatureProvider)getFeatureProvider()).getDiagramGeometry();
29
			dg.removeConnectionBendpoint((DiagramConnectionPart)bo, context.getBendpointIndex());
30
		}
31
		
32
	}
33
34
}
(-)src/org/eclipse/sapphire/ui/diagram/graphiti/features/SapphireUpdateConnectionFeature.java (+94 lines)
Added Link Here
1
package org.eclipse.sapphire.ui.diagram.graphiti.features;
2
3
import org.eclipse.graphiti.features.IFeatureProvider;
4
import org.eclipse.graphiti.features.IReason;
5
import org.eclipse.graphiti.features.context.IUpdateContext;
6
import org.eclipse.graphiti.features.impl.AbstractUpdateFeature;
7
import org.eclipse.graphiti.features.impl.Reason;
8
import org.eclipse.graphiti.mm.algorithms.Text;
9
import org.eclipse.graphiti.mm.pictograms.Connection;
10
import org.eclipse.graphiti.mm.pictograms.ConnectionDecorator;
11
import org.eclipse.graphiti.mm.pictograms.PictogramElement;
12
import org.eclipse.sapphire.ui.diagram.editor.DiagramConnectionPart;
13
14
public class SapphireUpdateConnectionFeature extends AbstractUpdateFeature 
15
{
16
	public SapphireUpdateConnectionFeature(IFeatureProvider fp)
17
	{
18
		super(fp);
19
	}
20
	
21
	public boolean canUpdate(IUpdateContext context) 
22
	{
23
		PictogramElement pe = context.getPictogramElement();
24
		Object bo = getBusinessObjectForPictogramElement(pe);
25
		return bo instanceof DiagramConnectionPart;
26
	}
27
28
	public IReason updateNeeded(IUpdateContext context) 
29
	{
30
		// retrieve name from pictogram model
31
		String pictogramName = null;
32
		PictogramElement pictogramElement = context.getPictogramElement();
33
		if (pictogramElement instanceof Connection) {
34
			Connection c = (Connection) pictogramElement;
35
			for (ConnectionDecorator cd : c.getConnectionDecorators()) {
36
				if (cd.getGraphicsAlgorithm() instanceof Text) {
37
					Text text = (Text) cd.getGraphicsAlgorithm();
38
					pictogramName = text.getValue();
39
				}
40
			}
41
		}
42
43
		// retrieve name from business model
44
		String businessName = null;
45
		Object bo = getBusinessObjectForPictogramElement(pictogramElement);
46
		if (bo instanceof DiagramConnectionPart) 
47
		{
48
			DiagramConnectionPart connPart = (DiagramConnectionPart)bo;
49
			businessName = connPart.getLabel();
50
		}
51
52
		// update needed, if names are different
53
		boolean updateNameNeeded = ((pictogramName == null && businessName != null) || (pictogramName != null && !pictogramName
54
				.equals(businessName)));
55
		if (updateNameNeeded) 
56
		{
57
			return Reason.createTrueReason("Name is out of date"); //$NON-NLS-1$
58
		} 
59
		else
60
		{
61
			return Reason.createFalseReason();
62
		}
63
	}
64
65
	public boolean update(IUpdateContext context) 
66
	{
67
		String businessName = null;
68
		PictogramElement pe = context.getPictogramElement();
69
		Object bo = getBusinessObjectForPictogramElement(pe);
70
		if (bo instanceof DiagramConnectionPart) 
71
		{
72
			DiagramConnectionPart connPart = (DiagramConnectionPart)bo;
73
			businessName = connPart.getLabel();
74
		}
75
		
76
		// Set name in pictogram model
77
		if (pe instanceof Connection) 
78
		{
79
			Connection c = (Connection) pe;
80
			for (ConnectionDecorator cd : c.getConnectionDecorators()) 
81
			{
82
				if (cd.getGraphicsAlgorithm() instanceof Text)
83
				{
84
					Text text = (Text) cd.getGraphicsAlgorithm();
85
					text.setValue(businessName);
86
					return true;
87
				}
88
			}
89
		}
90
		
91
		return false;
92
	}
93
94
}
(-)src/org/eclipse/sapphire/ui/diagram/graphiti/features/SapphireUpdateNodeFeature.java (+94 lines)
Added Link Here
1
package org.eclipse.sapphire.ui.diagram.graphiti.features;
2
3
import org.eclipse.graphiti.features.IFeatureProvider;
4
import org.eclipse.graphiti.features.IReason;
5
import org.eclipse.graphiti.features.context.IUpdateContext;
6
import org.eclipse.graphiti.features.impl.AbstractUpdateFeature;
7
import org.eclipse.graphiti.features.impl.Reason;
8
import org.eclipse.graphiti.mm.algorithms.Text;
9
import org.eclipse.graphiti.mm.pictograms.ContainerShape;
10
import org.eclipse.graphiti.mm.pictograms.PictogramElement;
11
import org.eclipse.graphiti.mm.pictograms.Shape;
12
import org.eclipse.sapphire.ui.diagram.editor.DiagramNodePart;
13
14
public class SapphireUpdateNodeFeature extends AbstractUpdateFeature 
15
{
16
	public SapphireUpdateNodeFeature(IFeatureProvider fp)
17
	{
18
		super(fp);
19
	}
20
	
21
	public boolean canUpdate(IUpdateContext context) 
22
	{
23
		Object bo = getBusinessObjectForPictogramElement(context.getPictogramElement());
24
		return bo instanceof DiagramNodePart;
25
	}
26
27
	public IReason updateNeeded(IUpdateContext context) 
28
	{
29
		// retrieve name from pictogram model
30
		String pictogramName = null;
31
		PictogramElement pictogramElement = context.getPictogramElement();
32
		if (pictogramElement instanceof ContainerShape) 
33
		{
34
			ContainerShape cs = (ContainerShape) pictogramElement;
35
			for (Shape shape : cs.getChildren()) 
36
			{
37
				if (shape.getGraphicsAlgorithm() instanceof Text) {
38
					Text text = (Text) shape.getGraphicsAlgorithm();
39
					pictogramName = text.getValue();
40
				}
41
			}
42
		}
43
		// retrieve name from business model
44
		String businessName = null;
45
		Object bo = getBusinessObjectForPictogramElement(pictogramElement);
46
		if (bo instanceof DiagramNodePart) 
47
		{
48
			DiagramNodePart nodePart = (DiagramNodePart) bo;
49
			businessName = nodePart.getLabel();
50
		}
51
		// update needed, if names are different
52
		boolean updateNameNeeded = 
53
			((pictogramName == null && businessName != null) || 
54
					(pictogramName != null && !pictogramName.equals(businessName)));
55
		if (updateNameNeeded) 
56
		{
57
			return Reason.createTrueReason("Name is out of date"); //$NON-NLS-1$
58
		} 
59
		else 
60
		{
61
			return Reason.createFalseReason();
62
		}		
63
	}
64
65
	public boolean update(IUpdateContext context) 
66
	{
67
		// retrieve name from business model
68
		String businessName = null;
69
		PictogramElement pictogramElement = context.getPictogramElement();
70
		Object bo = getBusinessObjectForPictogramElement(pictogramElement);
71
		if (bo instanceof DiagramNodePart) {
72
			DiagramNodePart nodePart = (DiagramNodePart) bo;
73
			businessName = nodePart.getLabel();
74
		}
75
76
		// Set name in pictogram model
77
		if (pictogramElement instanceof ContainerShape) 
78
		{
79
			ContainerShape cs = (ContainerShape) pictogramElement;
80
			for (Shape shape : cs.getChildren()) 
81
			{
82
				if (shape.getGraphicsAlgorithm() instanceof Text) 
83
				{
84
					Text text = (Text) shape.getGraphicsAlgorithm();
85
					text.setValue(businessName);
86
					return true;
87
				}
88
			}
89
		}
90
91
		return false;
92
	}
93
94
}
(-)src/org/eclipse/sapphire/ui/diagram/graphiti/providers/SapphireDiagramFeatureProvider.java (+184 lines)
Added Link Here
1
package org.eclipse.sapphire.ui.diagram.graphiti.providers;
2
3
import java.util.List;
4
5
import org.eclipse.graphiti.dt.IDiagramTypeProvider;
6
import org.eclipse.graphiti.features.IAddBendpointFeature;
7
import org.eclipse.graphiti.features.IAddFeature;
8
import org.eclipse.graphiti.features.ICreateConnectionFeature;
9
import org.eclipse.graphiti.features.ICreateFeature;
10
import org.eclipse.graphiti.features.IDirectEditingFeature;
11
import org.eclipse.graphiti.features.IMoveBendpointFeature;
12
import org.eclipse.graphiti.features.IMoveShapeFeature;
13
import org.eclipse.graphiti.features.IRemoveBendpointFeature;
14
import org.eclipse.graphiti.features.IUpdateFeature;
15
import org.eclipse.graphiti.features.context.IAddBendpointContext;
16
import org.eclipse.graphiti.features.context.IAddContext;
17
import org.eclipse.graphiti.features.context.IDirectEditingContext;
18
import org.eclipse.graphiti.features.context.IMoveBendpointContext;
19
import org.eclipse.graphiti.features.context.IMoveShapeContext;
20
import org.eclipse.graphiti.features.context.IRemoveBendpointContext;
21
import org.eclipse.graphiti.features.context.IUpdateContext;
22
import org.eclipse.graphiti.features.impl.IIndependenceSolver;
23
import org.eclipse.graphiti.mm.pictograms.Connection;
24
import org.eclipse.graphiti.mm.pictograms.ContainerShape;
25
import org.eclipse.graphiti.mm.pictograms.PictogramElement;
26
import org.eclipse.graphiti.ui.features.DefaultFeatureProvider;
27
import org.eclipse.sapphire.ui.diagram.editor.DiagramConnectionPart;
28
import org.eclipse.sapphire.ui.diagram.editor.DiagramConnectionTemplate;
29
import org.eclipse.sapphire.ui.diagram.editor.DiagramGeometryWrapper;
30
import org.eclipse.sapphire.ui.diagram.editor.DiagramNodePart;
31
import org.eclipse.sapphire.ui.diagram.editor.DiagramNodeTemplate;
32
import org.eclipse.sapphire.ui.diagram.editor.SapphireDiagramEditorPart;
33
import org.eclipse.sapphire.ui.diagram.graphiti.features.SapphireAddBendpointFeature;
34
import org.eclipse.sapphire.ui.diagram.graphiti.features.SapphireAddConnectionFeature;
35
import org.eclipse.sapphire.ui.diagram.graphiti.features.SapphireAddNodeFeature;
36
import org.eclipse.sapphire.ui.diagram.graphiti.features.SapphireCreateConnectionFeature;
37
import org.eclipse.sapphire.ui.diagram.graphiti.features.SapphireCreateNodeFeature;
38
import org.eclipse.sapphire.ui.diagram.graphiti.features.SapphireDirectEditConnectionFeature;
39
import org.eclipse.sapphire.ui.diagram.graphiti.features.SapphireDirectEditNodeFeature;
40
import org.eclipse.sapphire.ui.diagram.graphiti.features.SapphireMoveBendpointFeature;
41
import org.eclipse.sapphire.ui.diagram.graphiti.features.SapphireMoveNodeFeature;
42
import org.eclipse.sapphire.ui.diagram.graphiti.features.SapphireRemoveBendpointFeature;
43
import org.eclipse.sapphire.ui.diagram.graphiti.features.SapphireUpdateConnectionFeature;
44
import org.eclipse.sapphire.ui.diagram.graphiti.features.SapphireUpdateNodeFeature;
45
46
public class SapphireDiagramFeatureProvider extends DefaultFeatureProvider 
47
{
48
	private SapphireDiagramEditorPart diagramPart;
49
	
50
	public SapphireDiagramFeatureProvider(IDiagramTypeProvider dtp, IIndependenceSolver solver)
51
	{
52
		super(dtp);
53
		this.setIndependenceSolver(solver);
54
	}
55
	
56
	public void setDiagramPart(SapphireDiagramEditorPart diagramPart)
57
	{
58
		this.diagramPart = diagramPart;
59
		
60
	}
61
	
62
	public SapphireDiagramEditorPart getDiagramPart()
63
	{
64
		return this.diagramPart;
65
	}
66
	
67
	public DiagramGeometryWrapper getDiagramGeometry()
68
	{
69
		return this.diagramPart.getDiagramGeometry();
70
	}
71
	
72
	@Override
73
	public IAddFeature getAddFeature(IAddContext context) 
74
	{
75
		Object obj = context.getNewObject();
76
		if (obj instanceof DiagramNodePart)
77
		{
78
			return new SapphireAddNodeFeature(this);
79
		}
80
		else if (obj instanceof DiagramConnectionPart)
81
		{
82
			return new SapphireAddConnectionFeature(this);
83
		}
84
		
85
		return super.getAddFeature(context);
86
	}
87
	
88
	@Override
89
	public ICreateFeature[] getCreateFeatures() 
90
	{
91
		List<DiagramNodeTemplate> nodeTemplates = this.diagramPart.getNodeTemplates();
92
		ICreateFeature[] features = new ICreateFeature[nodeTemplates.size()];
93
		int i = 0;
94
		for (DiagramNodeTemplate nodeTemplate : nodeTemplates)
95
		{
96
			SapphireCreateNodeFeature createNodeFeature = 
97
				new SapphireCreateNodeFeature(this, nodeTemplate);
98
			features[i++] = createNodeFeature;
99
		}
100
		return features;
101
	}	
102
	
103
	@Override
104
    public ICreateConnectionFeature[] getCreateConnectionFeatures() 
105
	{
106
		List<DiagramConnectionTemplate> connectionTemplates = this.diagramPart.getConnectionTemplates();
107
		ICreateConnectionFeature[] features = new ICreateConnectionFeature[connectionTemplates.size()];
108
		int i = 0;
109
		for (DiagramConnectionTemplate connectionTemplate : connectionTemplates)
110
		{
111
			SapphireCreateConnectionFeature createConnectionFeature = 
112
				new SapphireCreateConnectionFeature(this, connectionTemplate);
113
			features[i++] = createConnectionFeature;
114
		}
115
		return features;
116
	}
117
	
118
	@Override
119
	public IDirectEditingFeature getDirectEditingFeature(IDirectEditingContext context) 
120
	{
121
		PictogramElement pe = context.getPictogramElement();
122
		Object bo = getBusinessObjectForPictogramElement((PictogramElement) pe.eContainer());
123
		if (bo instanceof DiagramNodePart)
124
		{
125
			return new SapphireDirectEditNodeFeature(this);
126
		}
127
		else if (bo instanceof DiagramConnectionPart)
128
		{
129
			return new SapphireDirectEditConnectionFeature(this);
130
		}
131
		return super.getDirectEditingFeature(context);		
132
	}
133
	
134
	@Override
135
	public IUpdateFeature getUpdateFeature(IUpdateContext context) 
136
	{
137
		PictogramElement pe = context.getPictogramElement();
138
		if (pe instanceof ContainerShape) 
139
		{
140
			Object bo = getBusinessObjectForPictogramElement(pe);
141
			if (bo instanceof DiagramNodePart) 
142
			{
143
				return new SapphireUpdateNodeFeature(this);
144
			}
145
		}
146
		else if (pe instanceof Connection)
147
		{
148
			Object bo = getBusinessObjectForPictogramElement(pe);
149
			if (bo instanceof DiagramConnectionPart)
150
			{
151
				return new SapphireUpdateConnectionFeature(this);
152
			}
153
		}
154
		return super.getUpdateFeature(context);
155
	}
156
	
157
	@Override
158
	public IAddBendpointFeature getAddBendpointFeature(IAddBendpointContext context) 
159
	{
160
		IAddBendpointFeature ret = new SapphireAddBendpointFeature(this);
161
		return ret;
162
	}
163
	
164
	@Override
165
	public IRemoveBendpointFeature getRemoveBendpointFeature(IRemoveBendpointContext context) 
166
	{
167
		IRemoveBendpointFeature ret = new SapphireRemoveBendpointFeature(this);
168
		return ret;
169
	}
170
171
	@Override
172
	public IMoveBendpointFeature getMoveBendpointFeature(IMoveBendpointContext context) 
173
	{
174
		IMoveBendpointFeature ret = new SapphireMoveBendpointFeature(this);
175
		return ret;
176
	}
177
	
178
    @Override
179
    public IMoveShapeFeature getMoveShapeFeature(IMoveShapeContext context) 
180
    {
181
    	return new SapphireMoveNodeFeature(this);
182
    }
183
	
184
}
(-)src/org/eclipse/sapphire/ui/diagram/graphiti/providers/SapphireDiagramSolver.java (+45 lines)
Added Link Here
1
package org.eclipse.sapphire.ui.diagram.graphiti.providers;
2
3
import java.util.HashMap;
4
5
import org.eclipse.graphiti.features.impl.IIndependenceSolver;
6
import org.eclipse.sapphire.ui.SapphirePart;
7
8
public class SapphireDiagramSolver implements IIndependenceSolver 
9
{
10
	private HashMap<String, SapphirePart> keyToBOMap;
11
	private HashMap<SapphirePart, String> bOToKeyMap;
12
	private int counter = 0;
13
14
	public SapphireDiagramSolver()
15
	{
16
		this.keyToBOMap = new HashMap<String, SapphirePart>();
17
		this.bOToKeyMap = new HashMap<SapphirePart, String>();		
18
	}
19
	
20
	public String getKeyForBusinessObject(Object bo) 
21
	{
22
		if (bo instanceof SapphirePart)			
23
		{
24
			SapphirePart part = (SapphirePart)bo;
25
			if (this.bOToKeyMap.containsKey(bo))
26
			{
27
				return this.bOToKeyMap.get(bo);
28
			}
29
			String key = Integer.toString(this.counter++);
30
			this.keyToBOMap.put(key, part);
31
			this.bOToKeyMap.put(part, key);
32
			
33
			return key;
34
		}
35
		return null;
36
	}
37
38
	public Object getBusinessObjectForKey(String key) 
39
	{
40
		if (key == null)
41
			return null;
42
		return this.keyToBOMap.get(key);
43
	}
44
45
}
(-)src/org/eclipse/sapphire/ui/diagram/graphiti/providers/SapphireDiagramToolBehaviorProvider.java (+25 lines)
Added Link Here
1
package org.eclipse.sapphire.ui.diagram.graphiti.providers;
2
3
import org.eclipse.graphiti.dt.IDiagramTypeProvider;
4
import org.eclipse.graphiti.mm.pictograms.PictogramElement;
5
import org.eclipse.graphiti.tb.DefaultToolBehaviorProvider;
6
import org.eclipse.graphiti.tb.IContextButtonPadData;
7
8
9
public class SapphireDiagramToolBehaviorProvider extends DefaultToolBehaviorProvider 
10
{
11
	public SapphireDiagramToolBehaviorProvider(IDiagramTypeProvider dtp) 
12
	{
13
		super(dtp);
14
	}
15
16
	/**
17
	 * Override super class impl to not include "Remove"
18
	 */
19
	@Override
20
	protected void setGenericContextButtons(IContextButtonPadData data, PictogramElement pe, int identifiers) 
21
	{
22
		identifiers -= CONTEXT_BUTTON_REMOVE;
23
		super.setGenericContextButtons(data, pe, identifiers);
24
	}
25
}
(-)src/org/eclipse/sapphire/ui/diagram/graphiti/providers/SapphireDiagramTypeProvider.java (+60 lines)
Added Link Here
1
package org.eclipse.sapphire.ui.diagram.graphiti.providers;
2
3
import org.eclipse.graphiti.dt.AbstractDiagramTypeProvider;
4
import org.eclipse.graphiti.mm.pictograms.Diagram;
5
import org.eclipse.graphiti.platform.IDiagramEditor;
6
import org.eclipse.graphiti.tb.IToolBehaviorProvider;
7
import org.eclipse.sapphire.ui.diagram.editor.SapphireDiagramEditorPart;
8
import org.eclipse.sapphire.ui.diagram.graphiti.editor.SapphireDiagramEditor;
9
10
public class SapphireDiagramTypeProvider extends AbstractDiagramTypeProvider 
11
{
12
	private SapphireDiagramEditorPart diagramPart;
13
	private SapphireDiagramFeatureProvider featureProvider;
14
	private IToolBehaviorProvider[] toolBehaviorProviders;
15
	
16
	public SapphireDiagramTypeProvider()
17
	{
18
		this.featureProvider = 
19
			new SapphireDiagramFeatureProvider(this, new SapphireDiagramSolver());
20
		setFeatureProvider(this.featureProvider);
21
	}
22
	
23
	public void setDiagramPart(SapphireDiagramEditorPart diagramPart)
24
	{
25
		this.diagramPart = diagramPart;
26
		this.featureProvider.setDiagramPart(diagramPart);		
27
	}
28
	
29
	public SapphireDiagramEditorPart getDiagramPart()
30
	{
31
		return this.diagramPart;
32
	}
33
	
34
	@Override
35
	public void init(Diagram diagram, IDiagramEditor diagramEditor)
36
	{
37
		super.init(diagram, diagramEditor);
38
		setDiagramPart(((SapphireDiagramEditor)diagramEditor).getDiagramEditorPart());
39
	}
40
	
41
    @Override
42
    public IToolBehaviorProvider[] getAvailableToolBehaviorProviders() 
43
    {
44
        if (this.toolBehaviorProviders == null) 
45
        {
46
            this.toolBehaviorProviders =
47
                new IToolBehaviorProvider[] { new SapphireDiagramToolBehaviorProvider(this) };
48
        }
49
        return this.toolBehaviorProviders;
50
    }
51
	
52
    @Override
53
    public void postInit() 
54
    {    	
55
    	SapphireDiagramEditor sapphireEditor = (SapphireDiagramEditor)getDiagramEditor();
56
    	sapphireEditor.syncDiagramWithModel();
57
    	sapphireEditor.doSave(null);
58
    }
59
    
60
}

Return to bug 330482