This repository has been archived on 2022-09-30. You can view files and clone it, but cannot push or open issues or pull requests.
bookmark-manager/BookmarkEditForm.cs

132 lines
3.9 KiB
C#
Raw Normal View History

using bzit.bomg.Models;
using RyzStudio.Windows.Forms;
2017-07-30 11:59:34 +00:00
using System;
using System.ComponentModel;
using System.Threading.Tasks;
2017-07-30 11:59:34 +00:00
using System.Windows.Forms;
using Form = System.Windows.Forms.Form;
using Resources = bzit.bomg.Properties.Resources;
2017-07-30 11:59:34 +00:00
namespace bzit.bomg
{
public partial class BookmarkEditForm : Form
{
protected bool isBusy = false;
protected BookmarkItemModel itemModel = null;
protected BookmarkTreeView treeView = null;
public BookmarkEditForm(BookmarkTreeView treeview) : base()
2017-07-30 11:59:34 +00:00
{
InitializeComponent();
treeView = treeview;
2017-07-30 11:59:34 +00:00
this.StartPosition = FormStartPosition.WindowsDefaultLocation;
}
protected override void OnShown(EventArgs e)
{
base.OnShown(e);
if (treeView.SNode.GetNodeType() == BookmarkTreeView.NodeType.Page)
2017-07-30 11:59:34 +00:00
{
BookmarkItemViewModel bookmarkItem = (BookmarkItemViewModel)treeView.SelectedNode.Tag;
if (bookmarkItem != null)
2017-07-30 11:59:34 +00:00
{
if (itemModel == null)
2017-07-30 11:59:34 +00:00
{
itemModel = new BookmarkItemModel();
2017-07-30 11:59:34 +00:00
}
updateViewModel(bookmarkItem);
2017-07-30 11:59:34 +00:00
}
}
}
protected override void OnClosing(CancelEventArgs e)
{
base.OnClosing(e);
if (this.IsBusy)
2017-07-30 11:59:34 +00:00
{
e.Cancel = true;
}
2017-07-30 11:59:34 +00:00
}
protected bool IsBusy
2017-07-30 11:59:34 +00:00
{
get => isBusy;
2017-07-30 11:59:34 +00:00
set
{
isBusy = value;
textBox1.Enabled = textBox2.Enabled = memoBox1.Enabled = !value;
2017-07-30 11:59:34 +00:00
ThreadControl.SetImage(pictureBox1, (value) ? Resources.aniZomq2x32 : null);
2017-07-30 11:59:34 +00:00
}
}
protected void updateViewModel(BookmarkItemViewModel viewModel)
2017-07-30 11:59:34 +00:00
{
itemModel.SiteName = viewModel.SiteName?.Trim();
itemModel.SiteAddress = viewModel.SiteAddress?.Trim();
itemModel.SiteDescription = viewModel.SiteDescription?.Trim();
itemModel.FaviconAddress = viewModel.FaviconAddress?.Trim();
textBox1.Text = itemModel.SiteName;
textBox2.Text = itemModel.SiteAddress;
memoBox1.Text = itemModel.SiteDescription;
pictureBox2.Image = treeView.ImageList.Images[treeView.SelectedNode.ImageIndex];
2017-07-30 11:59:34 +00:00
}
private async void button1_Click(object sender, EventArgs e)
2017-07-30 11:59:34 +00:00
{
if (this.IsBusy)
2017-07-30 11:59:34 +00:00
{
return;
}
if (string.IsNullOrWhiteSpace(textBox2.Text))
2017-07-30 11:59:34 +00:00
{
return;
}
this.IsBusy = true;
2017-07-30 11:59:34 +00:00
await Task.Run(() =>
{
if (itemModel == null) itemModel = new BookmarkItemModel();
itemModel.SiteAddress = textBox2.Text;
bool rv = itemModel.Update();
if (rv)
{
ThreadControl.SetText(textBox1, itemModel.SiteName);
ThreadControl.SetText(textBox2, itemModel.SiteAddress);
ThreadControl.SetText(memoBox1, itemModel.SiteDescription);
ThreadControl.SetImage(pictureBox2, itemModel.RetrieveFavicon());
}
});
this.IsBusy = false;
2017-07-30 11:59:34 +00:00
}
private void button2_Click(object sender, EventArgs e)
2017-07-30 11:59:34 +00:00
{
if (this.IsBusy) return;
if (string.IsNullOrWhiteSpace(textBox1.Text)) return;
if (string.IsNullOrWhiteSpace(textBox2.Text)) return;
2019-04-20 16:05:55 +00:00
if (itemModel == null) itemModel = new BookmarkItemModel();
2017-07-30 11:59:34 +00:00
itemModel.SiteName = textBox1.Text?.Trim();
itemModel.SiteAddress = textBox2.Text?.Trim();
itemModel.SiteDescription = memoBox1.Text?.Trim();
2017-07-30 11:59:34 +00:00
2019-04-23 20:57:35 +00:00
treeView?.SNode.AddOrUpdateItem(itemModel.ToViewModel());
2017-07-30 11:59:34 +00:00
this.Close();
}
2017-07-30 11:59:34 +00:00
}
}