Release 4.0.0-preview1-00051
[platform/core/csapi/tizenfx.git] / src / Tizen.Maps / Tizen.Maps / Polyline.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     /// Polyline map object
27     /// </summary>
28     /// <since_tizen>3</since_tizen>
29     public class Polyline : MapObject, IDisposable
30     {
31         internal Interop.PolylineHandle handle;
32         private List<Geocoordinates> _coordinateList;
33
34         /// <summary>
35         /// Creates polyline visual object.
36         /// </summary>
37         /// <since_tizen>3</since_tizen>
38         /// <param name="coordinates">List of geographical coordinates</param>
39         /// <param name="color">Line color</param>
40         /// <param name="width">The width of line [1 ~ 100] (pixels)</param>
41         /// <exception cref="ArgumentException">Thrown when input values are invalid</exception>
42         public Polyline(List<Geocoordinates> coordinates, Color color, int width) : base()
43         {
44             var err = Interop.ErrorCode.InvalidParameter;
45             if (coordinates == null || coordinates.Count() < 2)
46             {
47                 err.ThrowIfFailed("given coordinates list should contain at least 2 coordinates");
48             }
49             _coordinateList = coordinates.ToList();
50             var geocoordinateList = new GeocoordinatesList(_coordinateList);
51             handle = new Interop.PolylineHandle(geocoordinateList.handle, color, width);
52         }
53
54         /// <summary>
55         /// Adds or removes clicked event handlers.
56         /// </summary>
57         /// <since_tizen>3</since_tizen>
58         public event EventHandler Clicked;
59
60         /// <summary>
61         /// Gets or sets visibility for the polyline.
62         /// </summary>
63         /// <since_tizen>3</since_tizen>
64         public override bool IsVisible
65         {
66             get { return handle.IsVisible; }
67             set { handle.IsVisible = value; }
68         }
69
70         /// <summary>
71         /// Gets or sets a list of geographical coordinates for polyline vertices.
72         /// </summary>
73         /// <since_tizen>3</since_tizen>
74         public IEnumerable<Geocoordinates> Coordinates
75         {
76             get
77             {
78                 return _coordinateList;
79             }
80             set
81             {
82                 var coordinates = value.ToList();
83                 var err = Interop.ErrorCode.InvalidParameter;
84                 if (coordinates == null || coordinates.Count() < 2)
85                 {
86                     err.ThrowIfFailed("given coordinates list should contain at least 2 coordinates");
87                 }
88
89                 var geocoordinateList = new GeocoordinatesList(coordinates, false);
90                 if (handle.SetPolyline(geocoordinateList.handle).IsSuccess())
91                 {
92                     _coordinateList = coordinates;
93                 }
94             }
95         }
96
97         /// <summary>
98         /// Gets or sets line color.
99         /// </summary>
100         /// <since_tizen>3</since_tizen>
101         public Color LineColor
102         {
103             get
104             {
105                 return handle.LineColor;
106             }
107             set
108             {
109                 handle.LineColor = value;
110             }
111         }
112
113         /// <summary>
114         /// Gets or sets line width from 1 to 100 pixels.
115         /// </summary>
116         /// <since_tizen>3</since_tizen>
117         public int Width
118         {
119             get
120             {
121                 return handle.LineWidth;
122             }
123             set
124             {
125                 handle.LineWidth = value;
126             }
127         }
128
129         internal override void HandleClickedEvent()
130         {
131             Clicked?.Invoke(this, EventArgs.Empty);
132         }
133
134         internal override void InvalidateMapObject()
135         {
136             handle = null;
137         }
138
139         internal override Interop.ViewObjectHandle GetHandle()
140         {
141             return handle;
142         }
143
144         #region IDisposable Support
145         private bool _disposedValue = false;
146
147         protected virtual void Dispose(bool disposing)
148         {
149             if (!_disposedValue)
150             {
151                 if (disposing)
152                 {
153                     _coordinateList.Clear();
154                 }
155                 handle.Dispose();
156                 _disposedValue = true;
157             }
158         }
159
160         /// <summary>
161         /// Releases all resources used by this object.
162         /// </summary>
163         /// <since_tizen>3</since_tizen>
164         public void Dispose()
165         {
166             Dispose(true);
167             GC.SuppressFinalize(this);
168         }
169         #endregion
170     }
171 }