Accessing Display component from a Shape

Oct 15, 2012 at 1:09 PM
Edited Oct 15, 2012 at 1:10 PM

Hi,

Is there any way to access Display component from my Shape's code?

Shape has a Diagram property, so I can access the Diagram. But the Diagram provides no further access to its Display component :( Diagram has the DisplayService property but it's not what I need.

I need to be able to delete Shape from its own code. This task is related to my other thread ("Preventing connection to a given connection point"): when [under certain conditions] I prevent connection in MyLineShape.Connect() method, I want also to delete this instance of MyLineShape - otherwise it will remain on the Diagram (although it remains not connected, it looks confusing).

My application has only one Form and only one Display component, so I can simply store reference to this component in some application-wide static field, but it's not very nice.

Thank You.

Coordinator
Oct 15, 2012 at 2:21 PM

No, you cannot access the display from the shape, the shape only has access to the diagram's IDisplayService which provides all a shape should know about it's presentation component.
Shapes are not supposed to remove other shapes (not even their children) and they are not supposed to know anything of how they are presented.

The cleanest approach for your problem (regarding the NShape architecture) would be in fact deriving a custom SelectionTool (which is UI independent and therefore not so low-level):
Preventing the user to connect lines to special points of special shapes is a UI task. There's no real reason for a shape to restrict this as the shape *can* handle multiple connections to connection points.
The tool has access to the IDisplayPresenter and its properties, it can execute commands as it has access to the project. In addition to that, the tool can prevent connections before they are established. If you override the LinearShapeCreationTool, you can also prevent the user from creating the line shapes connected to these specific points.

An alternative would be to remove the consistency checks of the CachedRepository by removing the conditional compilation symbol "REPOSITORY_CHECK" (or simply using the release assemblies). But that's only another 'quick and dirty' workaround.