PageRenderTime 21ms CodeModel.GetById 2ms app.highlight 13ms RepoModel.GetById 1ms app.codeStats 0ms

/src/AddIns/Misc/PackageManagement/Project/Src/Pages.cs

https://github.com/ajadex/SharpDevelop
C# | 170 lines | 126 code | 23 blank | 21 comment | 20 complexity | 974389b4711d30a39d2af05c5e863445 MD5 | raw file
  1// Copyright (c) 2014 AlphaSierraPapa for the SharpDevelop Team
  2// 
  3// Permission is hereby granted, free of charge, to any person obtaining a copy of this
  4// software and associated documentation files (the "Software"), to deal in the Software
  5// without restriction, including without limitation the rights to use, copy, modify, merge,
  6// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
  7// to whom the Software is furnished to do so, subject to the following conditions:
  8// 
  9// The above copyright notice and this permission notice shall be included in all copies or
 10// substantial portions of the Software.
 11// 
 12// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
 13// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
 14// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
 15// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
 16// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 17// DEALINGS IN THE SOFTWARE.
 18
 19using System;
 20using System.Collections.ObjectModel;
 21
 22namespace ICSharpCode.PackageManagement
 23{
 24	public class Pages : ObservableCollection<Page>
 25	{
 26		public const int DefaultPageSize = 10;
 27		public const int DefaultMaximumSelectablePages = 5;
 28		
 29		int pageSize = DefaultPageSize;
 30		int selectedPageNumber = 1;
 31		int maximumSelectablePages = DefaultMaximumSelectablePages;
 32		int totalItems = 0;
 33		int itemsOnSelectedPage = 0;
 34		
 35		public int TotalItems {
 36			get { return totalItems; }
 37			set {
 38				if (totalItems != value) {
 39					totalItems = value;
 40					UpdatePages();
 41				}
 42			}
 43		}
 44		
 45		public int SelectedPageNumber {
 46			get { return selectedPageNumber; }
 47			set {
 48				if (selectedPageNumber != value) {
 49					selectedPageNumber = value;
 50					UpdatePages();
 51				}
 52			}
 53		}
 54		
 55		public int MaximumSelectablePages {
 56			get { return maximumSelectablePages; }
 57			set {
 58				if (maximumSelectablePages != value) {
 59					maximumSelectablePages = value;
 60					UpdatePages();
 61				}
 62			}
 63		}
 64		
 65		public int ItemsBeforeFirstPage {
 66			get {
 67				return (selectedPageNumber - 1) * pageSize;
 68			}
 69		}
 70
 71		public bool IsPaged {
 72			get { return totalItems > pageSize; }
 73		}
 74		
 75		public bool HasPreviousPage {
 76			get { return IsPaged && !IsFirstPageSelected; }
 77		}
 78		
 79		bool IsFirstPageSelected {
 80			get { return selectedPageNumber == 1; }
 81		}
 82		
 83		public bool HasNextPage {
 84			get { return IsPaged && !IsLastPageSelected; }
 85		}
 86		
 87		bool IsLastPageSelected {
 88			get { return selectedPageNumber == TotalPages; }
 89		}
 90		
 91		public int TotalPages {
 92			get { return (totalItems + pageSize - 1) / pageSize; }
 93		}
 94		
 95		public int PageSize {
 96			get { return pageSize; }
 97			set {
 98				if (pageSize != value) {
 99					pageSize = value;
100					UpdatePages();
101				}
102			}
103		}
104		
105		void UpdatePages()
106		{
107			Clear();
108			
109			int startPage = GetStartPage();
110			for (int pageNumber = startPage; pageNumber <= TotalPages; ++pageNumber) {
111				if (Count >= maximumSelectablePages) {
112					break;
113				}
114				Page page = CreatePage(pageNumber);
115				Add(page);
116			}
117		}
118		
119		int GetStartPage()
120		{
121			// Less pages than can be selected?
122			int totalPages = TotalPages;
123			if (totalPages <= maximumSelectablePages) {
124				return 1;
125			}
126			
127			// First choice for start page.
128			int startPage = selectedPageNumber - (maximumSelectablePages / 2);
129			if (startPage <= 0) {
130				return 1;
131			}
132			
133			// Do we have enough pages?
134			int totalPagesBasedOnStartPage = totalPages - startPage + 1;
135			if (totalPagesBasedOnStartPage >= maximumSelectablePages) {
136				return startPage;
137			}
138			
139			// Ensure we have enough pages.
140			startPage -= maximumSelectablePages - totalPagesBasedOnStartPage;
141			if (startPage > 0) {
142				return startPage;
143			}
144			return 1;
145		}
146		
147		Page CreatePage(int pageNumber)
148		{
149			var page = new Page();
150			page.Number = pageNumber;
151			page.IsSelected = IsSelectedPage(pageNumber);
152			return page;
153		}
154		
155		bool IsSelectedPage(int pageNumber)
156		{
157			return pageNumber == selectedPageNumber;
158		}
159		
160		public int TotalItemsOnSelectedPage {
161			get { return itemsOnSelectedPage; }
162			set {
163				itemsOnSelectedPage = value;
164				if (itemsOnSelectedPage < pageSize) {
165					TotalItems = (selectedPageNumber - 1) * pageSize + itemsOnSelectedPage;
166				}
167			}
168		}
169	}
170}