2015-04-15 18:32:56 +00:00
|
|
|
|
///
|
|
|
|
|
|
/// Copyright (c) 2013-2015 Sensus Metering Systems
|
|
|
|
|
|
///
|
|
|
|
|
|
using System;
|
2014-07-28 07:54:35 +00:00
|
|
|
|
using System.Windows.Forms;
|
2015-12-04 16:17:20 +00:00
|
|
|
|
using Config.Entities;
|
2014-07-28 07:54:35 +00:00
|
|
|
|
using TBF.Resources;
|
|
|
|
|
|
|
|
|
|
|
|
namespace TBF.UiControls
|
|
|
|
|
|
{
|
|
|
|
|
|
public partial class SharedButtons : UserControl
|
|
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Masks to specify the enabled state and the visibility of buttons.
|
|
|
|
|
|
/// Unlock button is hard coded in this control, cannot be set from the parent.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public enum Buttons
|
|
|
|
|
|
{
|
|
|
|
|
|
None = 0,
|
|
|
|
|
|
OK = 1, /// always visible, text = 'Close' when locked
|
|
|
|
|
|
Cancel = 2, /// always visible
|
|
|
|
|
|
Add = 4, /// optional
|
|
|
|
|
|
Remove = 8, /// optional
|
|
|
|
|
|
Up = 16, /// optional
|
|
|
|
|
|
Down = 32, /// optional
|
|
|
|
|
|
Edit = 64, /// optional
|
|
|
|
|
|
Copy = 128, /// optional
|
|
|
|
|
|
NewTab = 256, /// optional
|
|
|
|
|
|
RenameTab = 512, /// optional
|
|
|
|
|
|
RemoveTab = 1024, /// optional
|
2015-01-02 22:42:01 +00:00
|
|
|
|
More = 2048, /// optional
|
|
|
|
|
|
}
|
2014-07-28 07:54:35 +00:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Optional buttons are Add, Remove, Up, Down, Edit and Copy.
|
|
|
|
|
|
/// OK and Cancel buttons are always visible (after unlock), but not always enabled.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public Buttons OptionalButtons;
|
|
|
|
|
|
|
|
|
|
|
|
public enum LockState
|
|
|
|
|
|
{
|
|
|
|
|
|
Locked, /// 'Unlock', 'Close' (=OK) are shown, all the othere are hidden
|
|
|
|
|
|
Unlocked, /// Unlock, Ok, Cancel, Add,Remove and all optional buttons are shown
|
|
|
|
|
|
}
|
|
|
|
|
|
LockState lockState;
|
|
|
|
|
|
|
2015-01-02 22:42:01 +00:00
|
|
|
|
public bool MoreActive;
|
2016-08-01 09:22:59 +00:00
|
|
|
|
public int MoreButtonTop;
|
2015-01-02 22:42:01 +00:00
|
|
|
|
|
2014-07-28 07:54:35 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Used by controls to indicate which item is selected
|
|
|
|
|
|
/// and to appropriately update Remove, Up and Down Buttons.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public enum SelectedItemPos
|
|
|
|
|
|
{
|
|
|
|
|
|
None,
|
|
|
|
|
|
First,
|
|
|
|
|
|
Last,
|
|
|
|
|
|
FirstAndLast,
|
|
|
|
|
|
Middle,
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-03-16 20:53:12 +00:00
|
|
|
|
public Users.Grp.GID RequiredGroupMembership;
|
2014-07-28 07:54:35 +00:00
|
|
|
|
|
2017-03-16 20:53:12 +00:00
|
|
|
|
Users.IUser originalUser;
|
2014-07-28 07:54:35 +00:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Default constructor
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public SharedButtons()
|
|
|
|
|
|
{
|
2017-03-16 20:53:12 +00:00
|
|
|
|
originalUser = Users.GlobalData.CurrentUser;
|
2014-07-28 07:54:35 +00:00
|
|
|
|
lockState = LockState.Locked;
|
2015-01-02 22:42:01 +00:00
|
|
|
|
MoreActive = false;
|
2014-07-28 07:54:35 +00:00
|
|
|
|
OptionalButtons = Buttons.None;
|
2017-03-16 20:53:12 +00:00
|
|
|
|
RequiredGroupMembership = Users.Grp.GID.None;
|
2014-07-28 07:54:35 +00:00
|
|
|
|
InitializeComponent();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Initialize the buttons when the dialog is loaded
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="sender"></param>
|
|
|
|
|
|
/// <param name="e"></param>
|
|
|
|
|
|
private void SharedDlgButtons_Load(object sender, EventArgs e)
|
|
|
|
|
|
{
|
|
|
|
|
|
unlockButton.Text = Strings.UnlockBtnText;
|
|
|
|
|
|
okButton.Text = Strings.CloseBtnText;
|
|
|
|
|
|
cancelButton.Text = Strings.CancelBtnText;
|
|
|
|
|
|
addButton.Text = Strings.AddBtnText;
|
|
|
|
|
|
removeButton.Text = Strings.RemoveBtnText;
|
|
|
|
|
|
upButton.Text = Strings.UpBtnText;
|
|
|
|
|
|
downButton.Text = Strings.DownBtnText;
|
|
|
|
|
|
editButton.Text = Strings.EditBtnText;
|
|
|
|
|
|
copyButton.Text = Strings.CopyBtnText;
|
|
|
|
|
|
newTabButton.Text = Strings.NewTabBtnText;
|
|
|
|
|
|
renameTabButton.Text = Strings.RenameTabBtnText;
|
|
|
|
|
|
removeTabButton.Text = Strings.RemoveTabBtnText;
|
2015-01-02 22:42:01 +00:00
|
|
|
|
moreButton.Text = Strings.MoreBtnText;
|
2014-07-28 07:54:35 +00:00
|
|
|
|
|
|
|
|
|
|
cancelButton.Hide();
|
|
|
|
|
|
addButton.Hide();
|
|
|
|
|
|
removeButton.Hide();
|
|
|
|
|
|
upButton.Hide();
|
|
|
|
|
|
downButton.Hide();
|
|
|
|
|
|
editButton.Hide();
|
|
|
|
|
|
copyButton.Hide();
|
|
|
|
|
|
newTabButton.Hide();
|
|
|
|
|
|
renameTabButton.Hide();
|
|
|
|
|
|
removeTabButton.Hide();
|
2015-01-02 22:42:01 +00:00
|
|
|
|
moreButton.Hide();
|
2014-07-28 07:54:35 +00:00
|
|
|
|
|
|
|
|
|
|
/// Reposition 'Edit' and 'Copy" buttons in case both 'Up' and 'Down' are hidden
|
|
|
|
|
|
if (((OptionalButtons & Buttons.Up) == 0) &&
|
|
|
|
|
|
((OptionalButtons & Buttons.Down) == 0))
|
|
|
|
|
|
{
|
|
|
|
|
|
editButton.Top = upButton.Top;
|
|
|
|
|
|
copyButton.Top = downButton.Top;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// Reposition Tab-related button in case both 'Edit' and 'Copy" are hidden
|
|
|
|
|
|
if (((OptionalButtons & Buttons.Edit) == 0) &&
|
|
|
|
|
|
((OptionalButtons & Buttons.Copy) == 0))
|
|
|
|
|
|
{
|
|
|
|
|
|
removeTabButton.Top = newTabButton.Top + 15;
|
|
|
|
|
|
newTabButton.Top = editButton.Top + 15;
|
|
|
|
|
|
renameTabButton.Top = copyButton.Top + 15;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2015-01-02 22:42:01 +00:00
|
|
|
|
if ((OptionalButtons & Buttons.Add) == 0 &&
|
|
|
|
|
|
(OptionalButtons & Buttons.Remove) == 0 &&
|
|
|
|
|
|
(OptionalButtons & Buttons.Up) == 0 &&
|
|
|
|
|
|
(OptionalButtons & Buttons.Down) == 0 &&
|
|
|
|
|
|
(OptionalButtons & Buttons.Edit) == 0 &&
|
|
|
|
|
|
(OptionalButtons & Buttons.Copy) == 0 &&
|
|
|
|
|
|
(OptionalButtons & Buttons.NewTab) == 0 &&
|
|
|
|
|
|
(OptionalButtons & Buttons.RenameTab) == 0 &&
|
|
|
|
|
|
(OptionalButtons & Buttons.RemoveTab) == 0)
|
|
|
|
|
|
{
|
2016-08-01 09:22:59 +00:00
|
|
|
|
moreButton.Top = MoreButtonTop;
|
2015-01-02 22:42:01 +00:00
|
|
|
|
}
|
2014-07-28 07:54:35 +00:00
|
|
|
|
#if DEBUG
|
|
|
|
|
|
//unlockBtn_Click(sender, e);
|
|
|
|
|
|
#endif
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Update the visibility of buttons and OK button text when the dialog is unlocked
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public void UnlockButtons()
|
|
|
|
|
|
{
|
|
|
|
|
|
unlockButton.Enabled = false;
|
|
|
|
|
|
okButton.Text = Strings.OkBtnText;
|
|
|
|
|
|
cancelButton.Show();
|
|
|
|
|
|
if ((OptionalButtons & Buttons.Add) != 0) addButton.Show();
|
|
|
|
|
|
if ((OptionalButtons & Buttons.Remove) != 0) removeButton.Show();
|
|
|
|
|
|
if ((OptionalButtons & Buttons.Up) != 0) upButton.Show();
|
|
|
|
|
|
if ((OptionalButtons & Buttons.Down) != 0) downButton.Show();
|
|
|
|
|
|
if ((OptionalButtons & Buttons.Edit) != 0) editButton.Show();
|
|
|
|
|
|
if ((OptionalButtons & Buttons.Copy) != 0) copyButton.Show();
|
|
|
|
|
|
if ((OptionalButtons & Buttons.NewTab) != 0) newTabButton.Show();
|
|
|
|
|
|
if ((OptionalButtons & Buttons.RenameTab) != 0) renameTabButton.Show();
|
|
|
|
|
|
if ((OptionalButtons & Buttons.RemoveTab) != 0) removeTabButton.Show();
|
2015-01-02 22:42:01 +00:00
|
|
|
|
if ((OptionalButtons & Buttons.More) != 0) moreButton.Show();
|
2014-07-28 07:54:35 +00:00
|
|
|
|
|
|
|
|
|
|
lockState = LockState.Unlocked;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Enable specified buttons
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public void EnableButtons(Buttons buttons)
|
|
|
|
|
|
{
|
|
|
|
|
|
SetEnabled(buttons, true);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Disable specified buttons
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public void DisableButtons(Buttons buttons)
|
|
|
|
|
|
{
|
|
|
|
|
|
SetEnabled(buttons, false);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void SetEnabled(Buttons buttons, bool value)
|
|
|
|
|
|
{
|
|
|
|
|
|
if ((buttons & Buttons.OK) != 0) okButton.Enabled = value;
|
|
|
|
|
|
if ((buttons & Buttons.Cancel) != 0) cancelButton.Enabled = value;
|
|
|
|
|
|
if ((buttons & Buttons.Add) != 0) addButton.Enabled = value;
|
|
|
|
|
|
if ((buttons & Buttons.Remove) != 0) removeButton.Enabled = value;
|
|
|
|
|
|
if ((buttons & Buttons.Up) != 0) upButton.Enabled = value;
|
|
|
|
|
|
if ((buttons & Buttons.Down) != 0) downButton.Enabled = value;
|
|
|
|
|
|
if ((buttons & Buttons.Edit) != 0) editButton.Enabled = value;
|
|
|
|
|
|
if ((buttons & Buttons.Copy) != 0) copyButton.Enabled = value;
|
|
|
|
|
|
if ((buttons & Buttons.NewTab) != 0) newTabButton.Enabled = value;
|
|
|
|
|
|
if ((buttons & Buttons.RenameTab) != 0) renameTabButton.Enabled = value;
|
|
|
|
|
|
if ((buttons & Buttons.RemoveTab) != 0) removeTabButton.Enabled = value;
|
2015-01-02 22:42:01 +00:00
|
|
|
|
if ((buttons & Buttons.More) != 0) moreButton.Enabled = value;
|
2014-07-28 07:54:35 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void RestoreUser()
|
|
|
|
|
|
{
|
2017-03-16 20:53:12 +00:00
|
|
|
|
Users.GlobalData.CurrentUser = originalUser;
|
2014-07-28 07:54:35 +00:00
|
|
|
|
Program.MainWnd.UpdateUser();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Events invoked when buttons are clicked (and in case of Unlock kbutton also accepted)
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public event EventHandler Unlocked;
|
|
|
|
|
|
public event EventHandler OKClicked;
|
|
|
|
|
|
public event EventHandler CancelClicked;
|
|
|
|
|
|
public event EventHandler AddClicked;
|
|
|
|
|
|
public event EventHandler RemoveClicked;
|
|
|
|
|
|
public event EventHandler UpClicked;
|
|
|
|
|
|
public event EventHandler DownClicked;
|
|
|
|
|
|
public event EventHandler EditClicked;
|
|
|
|
|
|
public event EventHandler CopyClicked;
|
|
|
|
|
|
public event EventHandler NewTabClicked;
|
|
|
|
|
|
public event EventHandler RenameTabClicked;
|
|
|
|
|
|
public event EventHandler RemoveTabClicked;
|
2015-01-02 22:42:01 +00:00
|
|
|
|
public event EventHandler MoreClicked;
|
2014-07-28 07:54:35 +00:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 'Unlock' button handler
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="sender"></param>
|
|
|
|
|
|
/// <param name="e"></param>
|
2015-08-13 06:36:28 +00:00
|
|
|
|
public void unlockBtn_Click(object sender, EventArgs e)
|
2014-07-28 07:54:35 +00:00
|
|
|
|
{
|
|
|
|
|
|
if (lockState == LockState.Locked)
|
|
|
|
|
|
{
|
2017-03-16 20:53:12 +00:00
|
|
|
|
if (!Users.GlobalData.CurrentUser.IsMemberOf(RequiredGroupMembership))
|
2014-07-28 07:54:35 +00:00
|
|
|
|
{
|
2017-03-15 11:56:53 +00:00
|
|
|
|
if ((new Users.Forms.LoginDlg(RequiredGroupMembership)).ShowDialog() != DialogResult.OK)
|
|
|
|
|
|
return;
|
2014-07-28 07:54:35 +00:00
|
|
|
|
}
|
|
|
|
|
|
else if (Program.LocalSettings.AlwaysAskPasswdWhenUnlocking)
|
|
|
|
|
|
{
|
2017-03-16 20:53:12 +00:00
|
|
|
|
if ((new Users.Forms.LoginDlg(Users.GlobalData.CurrentUser.UserName, RequiredGroupMembership)).ShowDialog() != DialogResult.OK)
|
2017-03-15 11:56:53 +00:00
|
|
|
|
return;
|
2014-07-28 07:54:35 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
2017-03-15 11:56:53 +00:00
|
|
|
|
if (Program.MainWnd != null) Program.MainWnd.UpdateUser();
|
|
|
|
|
|
|
|
|
|
|
|
UnlockButtons();
|
2014-07-28 07:54:35 +00:00
|
|
|
|
|
|
|
|
|
|
if (Unlocked != null) Unlocked(sender, e);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 'OK'/'Close' button handler. Handled as 'Cancel' in the locked state ('Close' button).
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
private void okBtn_Click(object sender, EventArgs e)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (lockState == LockState.Locked)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (CancelClicked != null) CancelClicked(sender, e);
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
if (OKClicked != null) OKClicked(sender, e);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2015-01-02 22:42:01 +00:00
|
|
|
|
private void moreButton_Click(object sender, EventArgs e)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (MoreActive)
|
|
|
|
|
|
{
|
|
|
|
|
|
MoreActive = false;
|
|
|
|
|
|
moreButton.Text = Strings.MoreBtnText;
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
MoreActive = true;
|
|
|
|
|
|
moreButton.Text = Strings.LessBtnText;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (MoreClicked != null) MoreClicked(sender, e);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2014-07-28 07:54:35 +00:00
|
|
|
|
///
|
|
|
|
|
|
/// All remaining handlers:
|
|
|
|
|
|
///
|
|
|
|
|
|
private void cancelBtn_Click(object s, EventArgs e) { if (CancelClicked != null) CancelClicked(s, e); }
|
|
|
|
|
|
private void addBtn_Click(object s, EventArgs e) { if (AddClicked != null) AddClicked(s, e); }
|
|
|
|
|
|
private void removeBtn_Click(object s, EventArgs e) { if (RemoveClicked != null) RemoveClicked(s, e); }
|
|
|
|
|
|
private void upBtn_Click(object s, EventArgs e) { if (UpClicked != null) UpClicked(s, e); }
|
|
|
|
|
|
private void downBtn_Click(object s, EventArgs e) { if (DownClicked != null) DownClicked(s, e); }
|
|
|
|
|
|
private void editBtn_Click(object s, EventArgs e) { if (EditClicked != null) EditClicked(s, e); }
|
|
|
|
|
|
private void copyBtn_Click(object s, EventArgs e) { if (CopyClicked != null) CopyClicked(s, e); }
|
|
|
|
|
|
private void newTabButton_Click(object s, EventArgs e) { if (NewTabClicked != null) NewTabClicked(s, e); }
|
|
|
|
|
|
private void renameTabButton_Click(object s, EventArgs e) { if (RenameTabClicked != null) RenameTabClicked(s, e); }
|
|
|
|
|
|
private void removeTabButton_Click(object s, EventArgs e) { if (RemoveTabClicked != null) RemoveTabClicked(s, e); }
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|