1 | using System.Drawing;
|
---|
2 | using System.Drawing.Drawing2D;
|
---|
3 | using System.Windows.Forms;
|
---|
4 | namespace Netron.Diagramming.Core {
|
---|
5 | // ----------------------------------------------------------------------
|
---|
6 | /// <summary>
|
---|
7 | /// Abstract base class for a connector.
|
---|
8 | /// </summary>
|
---|
9 | // ----------------------------------------------------------------------
|
---|
10 | public abstract partial class ConnectorBase :
|
---|
11 | DiagramEntityBase,
|
---|
12 | IConnector {
|
---|
13 | #region Enums
|
---|
14 | public enum ConnectorNameLocation {
|
---|
15 | Bottom,
|
---|
16 | Left,
|
---|
17 | Right,
|
---|
18 | Top
|
---|
19 | }
|
---|
20 | #endregion
|
---|
21 |
|
---|
22 | #region Fields
|
---|
23 |
|
---|
24 | // ------------------------------------------------------------------
|
---|
25 | /// <summary>
|
---|
26 | /// Implementation of IVersion - the current version of
|
---|
27 | /// ConnectorBase.
|
---|
28 | /// </summary>
|
---|
29 | // ------------------------------------------------------------------
|
---|
30 | protected const double connectorBaseVersion = 1.0;
|
---|
31 |
|
---|
32 | // ------------------------------------------------------------------
|
---|
33 | /// <summary>
|
---|
34 | /// the location of this connector
|
---|
35 | /// </summary>
|
---|
36 | // ------------------------------------------------------------------
|
---|
37 | protected Point mPoint;
|
---|
38 |
|
---|
39 | // ------------------------------------------------------------------
|
---|
40 | /// <summary>
|
---|
41 | /// the connectors attached to this connector
|
---|
42 | /// </summary>
|
---|
43 | // ------------------------------------------------------------------
|
---|
44 | protected CollectionBase<IConnector> mAttachedConnectors;
|
---|
45 |
|
---|
46 | // ------------------------------------------------------------------
|
---|
47 | /// <summary>
|
---|
48 | /// the connector, if any, to which this connector is attached to
|
---|
49 | /// </summary>
|
---|
50 | // ------------------------------------------------------------------
|
---|
51 | protected IConnector mAttachedTo;
|
---|
52 |
|
---|
53 | // ------------------------------------------------------------------
|
---|
54 | /// <summary>
|
---|
55 | /// Specifies if the connectors name is drawn.
|
---|
56 | /// </summary>
|
---|
57 | // ------------------------------------------------------------------
|
---|
58 | protected bool mShowName = false;
|
---|
59 |
|
---|
60 | // ------------------------------------------------------------------
|
---|
61 | /// <summary>
|
---|
62 | /// Specifies where the name is drawn relative to the connector's
|
---|
63 | /// bounds.
|
---|
64 | /// </summary>
|
---|
65 | // ------------------------------------------------------------------
|
---|
66 | protected ConnectorNameLocation mNameLocation =
|
---|
67 | ConnectorNameLocation.Top;
|
---|
68 |
|
---|
69 | // ------------------------------------------------------------------
|
---|
70 | /// <summary>
|
---|
71 | /// The font used to draw the name with.
|
---|
72 | /// </summary>
|
---|
73 | // ------------------------------------------------------------------
|
---|
74 | protected Font mFont = new Font("Arial", 12, FontStyle.Bold);
|
---|
75 |
|
---|
76 | // ------------------------------------------------------------------
|
---|
77 | /// <summary>
|
---|
78 | /// The color used to draw the name with.
|
---|
79 | /// </summary>
|
---|
80 | // ------------------------------------------------------------------
|
---|
81 | protected Color mForeColor = Color.Black;
|
---|
82 |
|
---|
83 | #endregion
|
---|
84 |
|
---|
85 | #region Properties
|
---|
86 |
|
---|
87 | // ------------------------------------------------------------------
|
---|
88 | /// <summary>
|
---|
89 | /// Gets the current version.
|
---|
90 | /// </summary>
|
---|
91 | // ------------------------------------------------------------------
|
---|
92 | public override double Version {
|
---|
93 | get {
|
---|
94 | return connectorBaseVersion;
|
---|
95 | }
|
---|
96 | }
|
---|
97 |
|
---|
98 | // ------------------------------------------------------------------
|
---|
99 | /// <summary>
|
---|
100 | /// Gets or sets the font used to draw the name with.
|
---|
101 | /// </summary>
|
---|
102 | // ------------------------------------------------------------------
|
---|
103 | public Font Font {
|
---|
104 | get {
|
---|
105 | return mFont;
|
---|
106 | }
|
---|
107 | set {
|
---|
108 | mFont = value;
|
---|
109 | }
|
---|
110 | }
|
---|
111 |
|
---|
112 | // ------------------------------------------------------------------
|
---|
113 | /// <summary>
|
---|
114 | /// Gets or sets the color used to draw the name with.
|
---|
115 | /// </summary>
|
---|
116 | // ------------------------------------------------------------------
|
---|
117 | public Color ForeColor {
|
---|
118 | get {
|
---|
119 | return mForeColor;
|
---|
120 | }
|
---|
121 | set {
|
---|
122 | mForeColor = value;
|
---|
123 | }
|
---|
124 | }
|
---|
125 |
|
---|
126 | // ------------------------------------------------------------------
|
---|
127 | /// <summary>
|
---|
128 | /// Gets or sets where the name is drawn relative to the connector's
|
---|
129 | /// bounds.
|
---|
130 | /// </summary>
|
---|
131 | // ------------------------------------------------------------------
|
---|
132 | public ConnectorNameLocation NameLocation {
|
---|
133 | get {
|
---|
134 | return mNameLocation;
|
---|
135 | }
|
---|
136 | set {
|
---|
137 | mNameLocation = value;
|
---|
138 | }
|
---|
139 | }
|
---|
140 |
|
---|
141 | // ------------------------------------------------------------------
|
---|
142 | /// <summary>
|
---|
143 | /// Gets or sets if the connectors name is drawn.
|
---|
144 | /// </summary>
|
---|
145 | // ------------------------------------------------------------------
|
---|
146 | public bool ShowName {
|
---|
147 | get {
|
---|
148 | return mShowName;
|
---|
149 | }
|
---|
150 | set {
|
---|
151 | mShowName = value;
|
---|
152 | }
|
---|
153 | }
|
---|
154 |
|
---|
155 | // ------------------------------------------------------------------
|
---|
156 | /// <summary>
|
---|
157 | /// Gets the attached connectors to this connector
|
---|
158 | /// </summary>
|
---|
159 | // ------------------------------------------------------------------
|
---|
160 | public CollectionBase<IConnector> AttachedConnectors {
|
---|
161 | get {
|
---|
162 | return mAttachedConnectors;
|
---|
163 | }
|
---|
164 | }
|
---|
165 |
|
---|
166 | // ------------------------------------------------------------------
|
---|
167 | /// <summary>
|
---|
168 | /// If the connector is attached to another connector
|
---|
169 | /// </summary>
|
---|
170 | // ------------------------------------------------------------------
|
---|
171 | public IConnector AttachedTo {
|
---|
172 | get {
|
---|
173 | return mAttachedTo;
|
---|
174 | }
|
---|
175 | set {
|
---|
176 | mAttachedTo = value;
|
---|
177 | }
|
---|
178 | }
|
---|
179 |
|
---|
180 | // ------------------------------------------------------------------
|
---|
181 | /// <summary>
|
---|
182 | /// The location of this connector
|
---|
183 | /// </summary>
|
---|
184 | // ------------------------------------------------------------------
|
---|
185 | public Point Point {
|
---|
186 | get {
|
---|
187 | return mPoint;
|
---|
188 | }
|
---|
189 | set {
|
---|
190 | //throw new NotSupportedException("Use the Move() method instead.");
|
---|
191 |
|
---|
192 | mPoint = value;
|
---|
193 | //foreach(IConnector con in attachedConnectors)
|
---|
194 | //{
|
---|
195 | // con.Point = value;
|
---|
196 | //}
|
---|
197 | }
|
---|
198 | }
|
---|
199 |
|
---|
200 | #endregion
|
---|
201 |
|
---|
202 | #region Constructor
|
---|
203 |
|
---|
204 | // ------------------------------------------------------------------
|
---|
205 | /// <summary>
|
---|
206 | /// Default connector.
|
---|
207 | /// </summary>
|
---|
208 | // ------------------------------------------------------------------
|
---|
209 | protected ConnectorBase(IModel model)
|
---|
210 | : base(model) {
|
---|
211 | mAttachedConnectors = new CollectionBase<IConnector>();
|
---|
212 | this.InitializeConnector();
|
---|
213 | }
|
---|
214 |
|
---|
215 | // ------------------------------------------------------------------
|
---|
216 | /// <summary>
|
---|
217 | /// Constructs a connector, passing its location
|
---|
218 | /// </summary>
|
---|
219 | /// <param name="p">Point: The location of the conector.</param>
|
---|
220 | /// <param name="model">IModel: The model.</param>
|
---|
221 | // ------------------------------------------------------------------
|
---|
222 | protected ConnectorBase(Point p, IModel model)
|
---|
223 | : base(model) {
|
---|
224 | mAttachedConnectors = new CollectionBase<IConnector>();
|
---|
225 | mPoint = p;
|
---|
226 | this.InitializeConnector();
|
---|
227 | }
|
---|
228 |
|
---|
229 | // ------------------------------------------------------------------
|
---|
230 | /// <summary>
|
---|
231 | /// Initializes a new instance of the <see cref="T:ConnectorBase"/>
|
---|
232 | /// class.
|
---|
233 | /// </summary>
|
---|
234 | /// <param name="p">Point: The location of the conector.</param>
|
---|
235 | // ------------------------------------------------------------------
|
---|
236 | protected ConnectorBase(Point p)
|
---|
237 | : base() {
|
---|
238 | mAttachedConnectors = new CollectionBase<IConnector>();
|
---|
239 | this.mPoint = p;
|
---|
240 | this.InitializeConnector();
|
---|
241 | }
|
---|
242 |
|
---|
243 | #endregion
|
---|
244 |
|
---|
245 | #region Methods
|
---|
246 |
|
---|
247 | // ------------------------------------------------------------------
|
---|
248 | /// <summary>
|
---|
249 | /// Initializes the default settings of the connector. Actions
|
---|
250 | /// performed in the ConnectorBase are:
|
---|
251 | /// * Sets the default PaintStyle to be
|
---|
252 | /// 'SolidPaintStyle' with a transparent color.
|
---|
253 | /// * Sets the default PenStyle to be a LinePenStyle whose color
|
---|
254 | /// is Transparent and DashStyle is 'Solid'.
|
---|
255 | /// </summary>
|
---|
256 | // ------------------------------------------------------------------
|
---|
257 | protected virtual void InitializeConnector() {
|
---|
258 | this.mPaintStyle = new SolidPaintStyle(Color.Transparent);
|
---|
259 | LinePenStyle lineStyle = new LinePenStyle();
|
---|
260 | lineStyle.Color = Color.Transparent;
|
---|
261 | lineStyle.DashStyle = DashStyle.Solid;
|
---|
262 | }
|
---|
263 |
|
---|
264 | // ------------------------------------------------------------------
|
---|
265 | /// <summary>
|
---|
266 | /// The custom menu to be added to the base menu of this connector.
|
---|
267 | /// 'null' is returned here.
|
---|
268 | /// </summary>
|
---|
269 | /// <returns>ToolStripItem[]</returns>
|
---|
270 | // ------------------------------------------------------------------
|
---|
271 | public override ToolStripItem[] Menu() {
|
---|
272 | return null;
|
---|
273 | }
|
---|
274 |
|
---|
275 | // ------------------------------------------------------------------
|
---|
276 | /// <summary>
|
---|
277 | /// Attaches the given connector to this connector.
|
---|
278 | /// <remarks>The method will remove a previous binding, if any,
|
---|
279 | /// before creating the attachment. This method prohibits mutliple
|
---|
280 | /// identical connections; you can attach a connector only once.
|
---|
281 | /// </remarks>
|
---|
282 | /// </summary>
|
---|
283 | /// <param name="connector">IConnector</param>
|
---|
284 | // ------------------------------------------------------------------
|
---|
285 | public virtual void AttachConnector(IConnector connector) {
|
---|
286 | if (connector == null || !Enabled)
|
---|
287 | return;
|
---|
288 | //only attach'm if not already present and not the parent
|
---|
289 | if ((!mAttachedConnectors.Contains(connector)) &&
|
---|
290 | (connector != mAttachedTo)) {
|
---|
291 | connector.DetachFromParent();
|
---|
292 | mAttachedConnectors.Add(connector);
|
---|
293 | // Make sure the attached connector is centered at this
|
---|
294 | // connector.
|
---|
295 | connector.Point = this.mPoint;
|
---|
296 | connector.AttachedTo = this;
|
---|
297 | }
|
---|
298 |
|
---|
299 | }
|
---|
300 |
|
---|
301 | // ------------------------------------------------------------------
|
---|
302 | /// <summary>
|
---|
303 | /// Detaches the given connector from this connector. No exception
|
---|
304 | /// will be thrown if the given connector is not a child.
|
---|
305 | /// </summary>
|
---|
306 | /// <param name="connector">IConnector</param>
|
---|
307 | // ------------------------------------------------------------------
|
---|
308 | public virtual void DetachConnector(IConnector connector) {
|
---|
309 | if (connector == null || !Enabled)
|
---|
310 | return;
|
---|
311 |
|
---|
312 | if (mAttachedConnectors.Contains(connector)) {
|
---|
313 | mAttachedConnectors.Remove(connector);
|
---|
314 | connector.AttachedTo = null;
|
---|
315 | }
|
---|
316 | }
|
---|
317 |
|
---|
318 | // ------------------------------------------------------------------
|
---|
319 | /// <summary>
|
---|
320 | /// Detaches from its parent (if any). No exception will be thrown
|
---|
321 | /// if this connector is not attached to any parent.
|
---|
322 | /// </summary>
|
---|
323 | // ------------------------------------------------------------------
|
---|
324 | public virtual void DetachFromParent() {
|
---|
325 | if (this.AttachedTo != null && Enabled) {
|
---|
326 | this.AttachedTo.AttachedConnectors.Remove(this);
|
---|
327 | this.AttachedTo = null;
|
---|
328 | }
|
---|
329 |
|
---|
330 | }
|
---|
331 |
|
---|
332 | // ------------------------------------------------------------------
|
---|
333 | /// <summary>
|
---|
334 | /// Attaches to another connector.
|
---|
335 | /// <remarks>If this connector is already attached it will be
|
---|
336 | /// detached first and no exception will be thrown. This method does
|
---|
337 | /// not allow mutliple connections; you cannot attach to a parent if
|
---|
338 | /// it already does
|
---|
339 | /// </remarks>
|
---|
340 | /// </summary>
|
---|
341 | /// <param name="parent">The new parent connector.</param>
|
---|
342 | // ------------------------------------------------------------------
|
---|
343 | public virtual void AttachTo(IConnector parent) {
|
---|
344 |
|
---|
345 | if (parent == null || !Enabled)
|
---|
346 | return;
|
---|
347 | // Do not re-attach and the parent cannot be an already attached
|
---|
348 | // child.
|
---|
349 | if ((this.AttachedTo != parent) &&
|
---|
350 | (!AttachedConnectors.Contains(parent))) {
|
---|
351 | //remove any other binding
|
---|
352 | DetachFromParent();
|
---|
353 | //attach to the given parent
|
---|
354 | parent.AttachedConnectors.Add(this);
|
---|
355 | this.AttachedTo = parent;
|
---|
356 | }
|
---|
357 | }
|
---|
358 |
|
---|
359 | #endregion
|
---|
360 |
|
---|
361 | }
|
---|
362 | }
|
---|