[Maps] Modify diposing routines
[platform/core/csapi/tizenfx.git] / src / Tizen.Maps / Tizen.Maps / Polygon.cs
1 /*
2  * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the License);
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an AS IS BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 using System;
18 using System.Collections.Generic;
19 using System.Linq;
20
21 using Color = ElmSharp.Color;
22
23 namespace Tizen.Maps
24 {
25     /// <summary>
26     /// The polygon map object.
27     /// </summary>
28     /// <since_tizen> 3 </since_tizen>
29     public class Polygon : MapObject, IDisposable
30     {
31         internal Interop.PolygonHandle handle;
32         private List<Geocoordinates> _coordinateList;
33
34         /// <summary>
35         /// Creates a polygon visual object.
36         /// </summary>
37         /// <since_tizen> 3 </since_tizen>
38         /// <param name="coordinates">List of geographical coordinates.</param>
39         /// <param name="color">Background color.</param>
40         /// <exception cref="ArgumentException">Thrown when input values are invalid.</exception>
41         public Polygon(IEnumerable<Geocoordinates> coordinates, Color color) : base()
42         {
43             var err = Interop.ErrorCode.InvalidParameter;
44             if (coordinates == null || coordinates.Count() < 3)
45             {
46                 err.ThrowIfFailed("given coordinates list should contain at least 3 coordinates");
47             }
48             _coordinateList = coordinates.ToList();
49             var geocoordinateList = new GeocoordinatesList(_coordinateList, false);
50             handle = new Interop.PolygonHandle(geocoordinateList.handle, color);
51         }
52
53         /// <summary>
54         /// Destroy the Polygon object.
55         /// </summary>
56         ~Polygon()
57         {
58             Dispose(false);
59         }
60
61         /// <summary>
62         /// Adds or removes the clicked event handlers.
63         /// </summary>
64         /// <since_tizen> 3 </since_tizen>
65         public event EventHandler Clicked;
66
67         /// <summary>
68         /// Gets or sets visibility for the polygon.
69         /// </summary>
70         /// <since_tizen> 3 </since_tizen>
71         public override bool IsVisible
72         {
73             get { return handle.IsVisible; }
74             set { handle.IsVisible = value; }
75         }
76
77         /// <summary>
78         /// Gets or sets a list of geographical coordinates for polygon vertices.
79         /// </summary>
80         /// <since_tizen> 3 </since_tizen>
81         public IEnumerable<Geocoordinates> Coordinates
82         {
83             get
84             {
85                 return _coordinateList;
86             }
87             set
88             {
89                 var coordinates = value.ToList();
90                 var err = Interop.ErrorCode.InvalidParameter;
91                 if (coordinates == null || coordinates.Count() < 3)
92                 {
93                     err.ThrowIfFailed("given coordinates list should contain at least 3 coordinates");
94                 }
95
96                 var geocoordinateList = new GeocoordinatesList(coordinates, false);
97                 if (handle.SetPolygon(geocoordinateList.handle).IsSuccess())
98                 {
99                     _coordinateList = coordinates;
100                 }
101             }
102         }
103
104         /// <summary>
105         /// Gets or sets a background color to fill the polygon.
106         /// </summary>
107         /// <since_tizen> 3 </since_tizen>
108         public Color FillColor
109         {
110             get
111             {
112                 return handle.FillColor;
113             }
114             set
115             {
116                 handle.FillColor = value;
117             }
118         }
119
120         internal override void HandleClickedEvent()
121         {
122             Clicked?.Invoke(this, EventArgs.Empty);
123         }
124
125         internal override void InvalidateMapObject()
126         {
127             handle = null;
128         }
129
130         internal override Interop.ViewObjectHandle GetHandle()
131         {
132             return handle;
133         }
134
135         #region IDisposable Support
136         private bool _disposedValue = false;
137
138         /// <summary>
139         /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
140         /// </summary>
141         /// <param name="disposing">If true, managed and unmanaged resources can be disposed, otherwise only unmanaged resources can be disposed.</param>
142         /// <since_tizen> 3 </since_tizen>
143         protected virtual void Dispose(bool disposing)
144         {
145             if (!_disposedValue)
146             {
147                 if (disposing)
148                 {
149                     _coordinateList?.Clear();
150                 }
151                 handle?.Dispose();
152                 _disposedValue = true;
153             }
154         }
155
156         /// <summary>
157         /// Releases all the resources used by this object.
158         /// </summary>
159         /// <since_tizen> 3 </since_tizen>
160         public void Dispose()
161         {
162             Dispose(true);
163             GC.SuppressFinalize(this);
164         }
165         #endregion
166     }
167 }