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

161 lines
4.6 KiB
C#
Raw Normal View History

using bzit.bomg.Models;
using RyzStudio.Windows.Forms;
using RyzStudio.Windows.ThemedForms;
2017-07-30 11:59:34 +00:00
using System;
using System.ComponentModel;
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
{
2019-04-19 02:51:55 +00:00
protected BackgroundWorker threadWorker = null;
2017-07-30 11:59:34 +00:00
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;
2019-04-19 02:51:55 +00:00
if (threadWorker == null)
{
2019-04-19 02:51:55 +00:00
threadWorker = new BackgroundWorker();
threadWorker.WorkerReportsProgress = threadWorker.WorkerSupportsCancellation = true;
threadWorker.DoWork += threadWorker1_DoWork;
threadWorker.RunWorkerCompleted += threadWorker1_RunWorkerCompleted;
}
2017-07-30 11:59:34 +00:00
}
protected void threadWorker1_DoWork(object sender, DoWorkEventArgs e)
2017-07-30 11:59:34 +00:00
{
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());
}
}
2017-07-30 11:59:34 +00:00
protected void threadWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
this.IsBusy = false;
2017-07-30 11:59:34 +00:00
}
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
{
2019-04-19 02:51:55 +00:00
get => threadWorker.IsBusy;
2017-07-30 11:59:34 +00:00
set
{
textBox1.Enabled = textBox2.Enabled = memoBox1.Enabled = !value;
2017-07-30 11:59:34 +00:00
pictureBox1.Image = (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 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
2019-04-19 02:51:55 +00:00
threadWorker.RunWorkerAsync();
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)
2017-07-30 11:59:34 +00:00
{
return;
}
2019-04-20 16:05:55 +00:00
if (string.IsNullOrWhiteSpace(textBox1.Text))
{
return;
}
if (string.IsNullOrWhiteSpace(textBox2.Text))
{
return;
}
if (itemModel == null)
2017-07-30 11:59:34 +00:00
{
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();
}
}
}