230 lines
5.0 KiB
C#
230 lines
5.0 KiB
C#
///
|
|
/// Copyright (c) 2013-2015 Senus Slovensko a.s.
|
|
///
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Windows.Forms;
|
|
using Config.Entities;
|
|
|
|
namespace TBF.UI.Shared
|
|
{
|
|
public class BaseWithListViewEx<MyItem> : Common.Forms.ListViewEx
|
|
{
|
|
/// <summary>
|
|
/// List of items to be edited
|
|
/// </summary>
|
|
public IList<MyItem> MyItems;
|
|
|
|
/// <summary>
|
|
/// Number of items/rows with lowest ItemNr = 0,...
|
|
/// that cannot be deleted, moved down, renamed, etc.
|
|
/// </summary>
|
|
public int FixedRowsCount;
|
|
|
|
/// <summary>
|
|
/// List of removed items that have to be removed from the database
|
|
/// when completed with OK.
|
|
/// </summary>
|
|
public IList<MyItem> ToBeRemovedItems;
|
|
|
|
/// <summary>
|
|
/// true = editing was unlocked
|
|
/// </summary>
|
|
public bool Unlocked { get { return unlocked; } } /// read only
|
|
bool unlocked;
|
|
|
|
public BaseWithListViewEx()
|
|
: base()
|
|
{
|
|
ToBeRemovedItems = new List<MyItem>();
|
|
|
|
/// Initialize the base (ListViewEx)
|
|
this.SuspendLayout();
|
|
AllowColumnReorder = false;
|
|
Dock = System.Windows.Forms.DockStyle.Fill;
|
|
DoubleClickActivation = false;
|
|
FullRowSelect = true;
|
|
GridLines = true;
|
|
Location = new System.Drawing.Point(0, 0);
|
|
Name = "baseWithListViewEx";
|
|
Size = new System.Drawing.Size(150, 150);
|
|
TabIndex = 0;
|
|
UseCompatibleStateImageBehavior = false;
|
|
View = System.Windows.Forms.View.Details;
|
|
ResumeLayout(false);
|
|
}
|
|
|
|
public void Unlock()
|
|
{
|
|
unlocked = true;
|
|
|
|
if (base.SelectedItems.Count == 1)
|
|
{
|
|
int ix = SelectedIndices[0];
|
|
Focus();
|
|
Items[ix].Selected = true;
|
|
Items[ix].EnsureVisible();
|
|
}
|
|
}
|
|
|
|
protected void RedrawAll()
|
|
{
|
|
Items.Clear();
|
|
foreach (var myItem in MyItems) DrawOne(myItem);
|
|
}
|
|
|
|
public virtual void DrawOne(object myItem)
|
|
{
|
|
}
|
|
|
|
void UpdateAll()
|
|
{
|
|
if (MyItems == null || (MyItems.Count != Items.Count)) return;
|
|
|
|
foreach (ListViewItem lvi in Items)
|
|
{
|
|
UpdateOne(lvi, (MyItem)lvi.Tag);
|
|
}
|
|
}
|
|
|
|
public virtual void UpdateOne(ListViewItem lvi, object myItem)
|
|
{
|
|
}
|
|
|
|
public void OkBtnClicked()
|
|
{
|
|
UpdateAll();
|
|
}
|
|
|
|
public void CancelBtnClicked()
|
|
{
|
|
}
|
|
|
|
public void AddOne(MyItem myItem)
|
|
{
|
|
MyItems.Add(myItem);
|
|
DrawOne(myItem);
|
|
|
|
Focus();
|
|
SelectedItems.Clear();
|
|
Items[Items.Count - 1].Selected = true;
|
|
Items[Items.Count - 1].EnsureVisible();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Called when 'Remove' button was clicked.
|
|
/// </summary>
|
|
/// <returns>true when the last item was removed and 'Remove' button should be disabled</returns>
|
|
public bool RemoveSelected()
|
|
{
|
|
if (SelectedItems.Count != 1) return false;
|
|
|
|
UpdateAll();
|
|
|
|
int removeItemNr = SelectedIndices[0];
|
|
if (removeItemNr < FixedRowsCount) return false;
|
|
|
|
MyItem selectedItem = (MyItem)SelectedItems[0].Tag;
|
|
|
|
if (selectedItem is IHasItemNr && (selectedItem as IHasItemNr).Id != 0)
|
|
{
|
|
ToBeRemovedItems.Add(selectedItem);
|
|
}
|
|
MyItems.RemoveAt(removeItemNr);
|
|
if (selectedItem is IHasItemNr)
|
|
{
|
|
for (int i = removeItemNr; i < MyItems.Count; i++)
|
|
{
|
|
(MyItems[i] as IHasItemNr).ItemNr--;
|
|
}
|
|
}
|
|
|
|
RedrawAll();
|
|
|
|
if (removeItemNr < base.Items.Count)
|
|
{
|
|
Focus();
|
|
Items[removeItemNr].Selected = true;
|
|
Items[removeItemNr].EnsureVisible();
|
|
return false;
|
|
}
|
|
else
|
|
{
|
|
return true;
|
|
}
|
|
}
|
|
|
|
public void MoveUpSelected()
|
|
{
|
|
if (SelectedItems.Count != 1) return;
|
|
|
|
int ix = base.SelectedIndices[0];
|
|
if (ix <= FixedRowsCount || ix >= MyItems.Count) return;
|
|
|
|
UpdateAll();
|
|
|
|
MyItem item1 = (MyItem)(Items[ix - 1].Tag);
|
|
MyItem item2 = (MyItem)(Items[ix].Tag);
|
|
(item1 as IHasItemNr).ItemNr++;
|
|
(item2 as IHasItemNr).ItemNr--;
|
|
MyItems.RemoveAt(ix);
|
|
MyItems.Insert(ix - 1, item2);
|
|
|
|
RedrawAll();
|
|
|
|
Focus();
|
|
Items[ix - 1].Selected = true;
|
|
Items[ix - 1].EnsureVisible();
|
|
}
|
|
|
|
public void MoveDownSelected()
|
|
{
|
|
if (base.SelectedItems.Count != 1) return;
|
|
|
|
int ix = base.SelectedIndices[0];
|
|
if (ix < FixedRowsCount || ix >= MyItems.Count - 1) return;
|
|
|
|
UpdateAll();
|
|
|
|
MyItem item1 = (MyItem)(Items[ix].Tag);
|
|
MyItem item2 = (MyItem)(Items[ix + 1].Tag);
|
|
(item1 as IHasItemNr).ItemNr++;
|
|
(item2 as IHasItemNr).ItemNr--;
|
|
MyItems.RemoveAt(ix + 1);
|
|
MyItems.Insert(ix, item2);
|
|
|
|
RedrawAll();
|
|
|
|
Focus();
|
|
Items[ix + 1].Selected = true;
|
|
Items[ix + 1].EnsureVisible();
|
|
}
|
|
|
|
|
|
public IList<string> GetUsedNames(bool caseSensitive = true)
|
|
{
|
|
IList<string> usedNames = new List<string>();
|
|
|
|
if (MyItems != null)
|
|
{
|
|
if (caseSensitive)
|
|
{
|
|
foreach (var item in MyItems)
|
|
{
|
|
if (item is IHasName) usedNames.Add((item as IHasName).Name);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
foreach (var item in MyItems)
|
|
{
|
|
if (item is IHasName) usedNames.Add((item as IHasName).Name.ToLower());
|
|
}
|
|
}
|
|
}
|
|
|
|
return usedNames;
|
|
}
|
|
}
|
|
}
|