Add 'GC.SuppressFinalize(this)' on Dispose()
[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     /// Polygon map object
27     /// </summary>
28     public class Polygon : MapObject, IDisposable
29     {
30         internal Interop.PolygonHandle handle;
31         private List<Geocoordinates> _coordinateList;
32
33         /// <summary>
34         /// Creates a polygon visual object
35         /// </summary>
36         /// <param name="coordinates">list of geographical coordinates</param>
37         /// <param name="color">background color</param>
38         public Polygon(IEnumerable<Geocoordinates> coordinates, Color color) : base()
39         {
40             var err = Interop.ErrorCode.InvalidParameter;
41             if (coordinates == null || coordinates.Count() < 3)
42             {
43                 err.ThrowIfFailed("given coordinates list should contain at least 3 coordinates");
44             }
45             _coordinateList = coordinates.ToList();
46             var geocoordinateList = new GeocoordinatesList(_coordinateList, false);
47             handle = new Interop.PolygonHandle(geocoordinateList.handle, color);
48         }
49
50         /// <summary>
51         /// Clicked event
52         /// </summary>
53         public event EventHandler Clicked;
54
55         public override bool IsVisible
56         {
57             get { return handle.IsVisible; }
58             set { handle.IsVisible = value; }
59         }
60
61         /// <summary>
62         /// List of geographical coordinates of polygon vertices
63         /// </summary>
64         public IEnumerable<Geocoordinates> Coordinates
65         {
66             get
67             {
68                 return _coordinateList;
69             }
70             set
71             {
72                 var coordinates = value.ToList();
73                 var err = Interop.ErrorCode.InvalidParameter;
74                 if (coordinates == null || coordinates.Count() < 3)
75                 {
76                     err.ThrowIfFailed("given coordinates list should contain at least 3 coordinates");
77                 }
78
79                 var geocoordinateList = new GeocoordinatesList(coordinates, false);
80                 if (handle.SetPolygon(geocoordinateList.handle).IsSuccess())
81                 {
82                     _coordinateList = coordinates;
83                 }
84             }
85         }
86
87         /// <summary>
88         /// Background fill color
89         /// </summary>
90         public Color FillColor
91         {
92             get
93             {
94                 return handle.FillColor;
95             }
96             set
97             {
98                 handle.FillColor = value;
99             }
100         }
101
102         internal override void HandleClickedEvent()
103         {
104             Clicked?.Invoke(this, EventArgs.Empty);
105         }
106
107         internal override void InvalidateMapObject()
108         {
109             handle = null;
110         }
111
112         internal override Interop.ViewObjectHandle GetHandle()
113         {
114             return handle;
115         }
116
117         #region IDisposable Support
118         private bool _disposedValue = false;
119
120         protected virtual void Dispose(bool disposing)
121         {
122             if (!_disposedValue)
123             {
124                 if (disposing)
125                 {
126                     _coordinateList.Clear();
127                 }
128                 handle.Dispose();
129                 _disposedValue = true;
130             }
131         }
132
133         public void Dispose()
134         {
135             Dispose(true);
136             GC.SuppressFinalize(this);
137         }
138         #endregion
139     }
140 }