a43939f1dc679e7852b1ec134a9a3f72029545a8
[platform/core/csapi/tizenfx.git] / test / Tizen.NUI.Samples / Tizen.NUI.Samples / Samples / CollectionViewDemo / CollectionViewLinearSample.cs
1 using System.Collections.Generic;
2 using System.Collections.ObjectModel;
3 using Tizen.NUI.BaseComponents;
4 using Tizen.NUI.Components;
5 using Tizen.NUI.Binding;
6 using System.ComponentModel;
7 using System;
8
9 namespace Tizen.NUI.Samples
10 {
11     public class CollectionViewLinearSample : IExample
12     {
13         CollectionView colView;
14         int itemCount = 500;
15         string selectedItem;
16         ItemSelectionMode selMode;
17         ObservableCollection<Gallery> gallerySource;
18         Gallery insertMenu = new Gallery(999, "Insert item to 3rd");
19         Gallery deleteMenu = new Gallery(999, "Delete item at 3rd");
20         Gallery moveMenu = new Gallery(999, "Move last item to 3rd");
21
22
23         public void Activate()
24         {
25             Window window = NUIApplication.GetDefaultWindow();
26
27             var myViewModelSource = gallerySource = new GalleryViewModel(itemCount);
28             // Add test menu options.
29             gallerySource.Insert(0, moveMenu);
30             gallerySource.Insert(0, deleteMenu);
31             gallerySource.Insert(0, insertMenu);
32
33             selMode = ItemSelectionMode.SingleSelection;
34             DefaultTitleItem myTitle = new DefaultTitleItem();
35             myTitle.Text = "Linear Sample Count["+itemCount+"]";
36             //Set Width Specification as MatchParent to fit the Item width with parent View.
37             myTitle.WidthSpecification = LayoutParamPolicies.MatchParent;
38
39             colView = new CollectionView()
40             {
41                 ItemsSource = myViewModelSource,
42                 ItemsLayouter = new LinearLayouter(),
43                 ItemTemplate = new DataTemplate(() =>
44                 {
45                     var rand = new Random();
46                     DefaultLinearItem item = new DefaultLinearItem();
47                     //Set Width Specification as MatchParent to fit the Item width with parent View.
48                     item.WidthSpecification = LayoutParamPolicies.MatchParent;
49
50                     //Decorate Label
51                     item.Label.SetBinding(TextLabel.TextProperty, "ViewLabel");
52                     item.Label.HorizontalAlignment = HorizontalAlignment.Begin;
53
54                     //Decorate SubLabel
55                     if ((rand.Next() % 2) == 0)
56                     {
57                         item.SubLabel.SetBinding(TextLabel.TextProperty, "Name");
58                         item.SubLabel.HorizontalAlignment = HorizontalAlignment.Begin;
59                     }
60
61                     //Decorate Icon
62                     item.Icon.SetBinding(ImageView.ResourceUrlProperty, "ImageUrl");
63                     item.Icon.WidthSpecification = 48;
64                     item.Icon.HeightSpecification = 48;
65
66                     //Decorate Extra RadioButton.
67                     //[NOTE] This is sample of RadioButton usage in CollectionView.
68                     // RadioButton change their selection by IsSelectedProperty bindings with
69                     // SelectionChanged event with SingleSelection ItemSelectionMode of CollectionView.
70                     // be aware of there are no RadioButtonGroup.
71                     item.Extra = new RadioButton();
72                     //FIXME : SetBinding in RadioButton crashed as Sensitive Property is disposed.
73                     //item.Extra.SetBinding(RadioButton.IsSelectedProperty, "Selected");
74                     item.Extra.WidthSpecification = 48;
75                     item.Extra.HeightSpecification = 48;
76
77                     return item;
78                 }),
79                 Header = myTitle,
80                 ScrollingDirection = ScrollableBase.Direction.Vertical,
81                 WidthSpecification = LayoutParamPolicies.MatchParent,
82                 HeightSpecification = LayoutParamPolicies.MatchParent,
83                                 SelectionMode = selMode
84             };
85             colView.SelectionChanged += SelectionEvt;
86
87             window.Add(colView);
88
89         }
90
91         public void SelectionEvt(object sender, SelectionChangedEventArgs ev)
92         {
93             //Tizen.Log.Debug("NUI", "LSH :: SelectionEvt called");
94
95             //SingleSelection Only have 1 or nil object in the list.
96             foreach (object item in ev.PreviousSelection)
97             {
98                 if (item == null) break;
99                 Gallery unselItem = (Gallery)item;
100
101                 unselItem.Selected = false;
102                 selectedItem = null;
103                 //Tizen.Log.Debug("NUI", "LSH :: Unselected: {0}", unselItem.ViewLabel);
104             }
105             foreach (object item in ev.CurrentSelection)
106             {
107                 if (item == null) break;
108                 Gallery selItem = (Gallery)item;
109                 //selItem.Selected = true;
110                 selectedItem = selItem.Name;
111
112                 // Check test menu options.
113                 if (selItem == insertMenu)
114                 {
115                     // Insert new item to index 3.
116                     Random rand = new Random();
117                     int idx = rand.Next(1000);
118                     gallerySource.Insert(3, new Gallery(idx, "Inserted Item"));
119                 }
120                 else if (selItem == deleteMenu)
121                 {
122                     // Remove item in index 3.
123                     gallerySource.RemoveAt(3);
124                 }
125                 else if (selItem == moveMenu)
126                 {
127                     // Move last indexed item to index 3.
128                     gallerySource.Move(gallerySource.Count - 1, 3);                    
129                 }
130                 //Tizen.Log.Debug("NUI", "LSH :: Selected: {0}", selItem.ViewLabel);
131             }
132             if (colView.Header != null && colView.Header is DefaultTitleItem)
133             {
134                 DefaultTitleItem title = (DefaultTitleItem)colView.Header;
135                 title.Text = "Linear Sample Count[" + gallerySource.Count + (selectedItem != null ? "] Selected [" + selectedItem + "]" : "]");
136             }
137         }
138
139         public void Deactivate()
140         {
141             if (colView != null)
142             {
143                 colView.Dispose();
144             }
145         }
146     }
147 }