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
2019-04-20 17:05:55 +01:00

161 lines
4.7 KiB
C#

using bzit.bomg.Models;
using RyzStudio.Windows.Forms;
using RyzStudio.Windows.ThemedForms;
using System;
using System.ComponentModel;
using System.Windows.Forms;
using Form = System.Windows.Forms.Form;
using Resources = bzit.bomg.Properties.Resources;
namespace bzit.bomg
{
public partial class BookmarkEditForm : Form
{
protected BackgroundWorker threadWorker = null;
protected BookmarkItemModel itemModel = null;
protected BookmarkTreeView treeView = null;
public BookmarkEditForm(BookmarkTreeView treeview) : base()
{
InitializeComponent();
treeView = treeview;
this.StartPosition = FormStartPosition.WindowsDefaultLocation;
if (threadWorker == null)
{
threadWorker = new BackgroundWorker();
threadWorker.WorkerReportsProgress = threadWorker.WorkerSupportsCancellation = true;
threadWorker.DoWork += threadWorker1_DoWork;
threadWorker.RunWorkerCompleted += threadWorker1_RunWorkerCompleted;
}
}
protected void threadWorker1_DoWork(object sender, DoWorkEventArgs e)
{
if (itemModel == null)
{
itemModel = new BookmarkItemModel();
}
itemModel.SiteAddress = textBox2.Text;
bool rv = itemModel.Update();
if (rv)
{
ThreadHelper.SetText(textBox1, itemModel.SiteName);
ThreadHelper.SetText(textBox2, itemModel.SiteAddress);
ThreadHelper.SetText(memoBox1, itemModel.SiteDescription);
ThreadHelper.SetImage(pictureBox2, itemModel.RetrieveFavicon());
}
}
protected void threadWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
this.IsBusy = false;
}
protected override void OnShown(EventArgs e)
{
base.OnShown(e);
if (treeView.SNode.GetNodeType() == BookmarkTreeView.NodeType.Page)
{
BookmarkItemViewModel bookmarkItem = (BookmarkItemViewModel)treeView.SelectedNode.Tag;
if (bookmarkItem != null)
{
if (itemModel == null)
{
itemModel = new BookmarkItemModel();
}
updateViewModel(bookmarkItem);
}
}
}
protected override void OnClosing(CancelEventArgs e)
{
base.OnClosing(e);
if (this.IsBusy)
{
e.Cancel = true;
}
}
protected bool IsBusy
{
get => threadWorker.IsBusy;
set
{
textBox1.Enabled = textBox2.Enabled = memoBox1.Enabled = !value;
pictureBox1.Image = (value) ? Resources.aniZomq2x32 : null;
}
}
protected void updateViewModel(BookmarkItemViewModel viewModel)
{
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];
}
private void button1_Click(object sender, EventArgs e)
{
if (this.IsBusy)
{
return;
}
if (string.IsNullOrWhiteSpace(textBox2.Text))
{
return;
}
this.IsBusy = true;
threadWorker.RunWorkerAsync();
}
private void button2_Click(object sender, EventArgs e)
{
if (this.IsBusy)
{
return;
}
if (string.IsNullOrWhiteSpace(textBox1.Text))
{
return;
}
if (string.IsNullOrWhiteSpace(textBox2.Text))
{
return;
}
if (itemModel == null)
{
itemModel = new BookmarkItemModel();
}
itemModel.SiteName = textBox1.Text?.Trim();
itemModel.SiteAddress = textBox2.Text?.Trim();
itemModel.SiteDescription = memoBox1.Text?.Trim();
treeView?.AddOrUpdateItem_OnSelectedNode(itemModel.ToViewModel());
this.Close();
}
}
}