2017-07-30 11:59:34 +00:00
using System ;
using System.ComponentModel ;
using System.Windows.Forms ;
namespace bzit.bomg
{
public partial class BookmarkEditForm : Form
{
private TreeNode parentNode = null ;
private BookmarkItem bookmarkItem ;
private bool isWorking = false ;
public BookmarkEditForm ( ref TreeNode node )
{
InitializeComponent ( ) ;
parentNode = node ;
this . StartPosition = FormStartPosition . WindowsDefaultLocation ;
}
protected override void OnLoad ( EventArgs e )
{
base . OnLoad ( e ) ;
button1 . Button . Image = Properties . Resources . magnifier ;
}
protected override void OnShown ( EventArgs e )
{
base . OnShown ( e ) ;
if ( parentNode . Tag ! = null )
{
if ( parentNode . Tag is BookmarkItem )
{
bookmarkItem = ( BookmarkItem ) parentNode . Tag ;
tbxName . Text = bookmarkItem . GetName ( ) ;
2019-04-07 17:29:33 +00:00
tbxAddress . Text = bookmarkItem . SiteAddress ;
2017-07-30 11:59:34 +00:00
tbxDescription . Text = bookmarkItem . Description ;
if ( parentNode . TreeView . ImageList ! = null )
{
if ( parentNode . ImageIndex > = 0 )
{
pbxIcon . Image = parentNode . TreeView . ImageList . Images [ parentNode . ImageIndex ] ;
}
else
{
pbxIcon . Image = parentNode . TreeView . ImageList . Images [ parentNode . ImageKey ] ;
}
}
}
}
}
protected override void OnClosing ( CancelEventArgs e )
{
base . OnClosing ( e ) ;
if ( this . IsWorking )
{
e . Cancel = true ;
}
}
public bool IsWorking
{
get { return isWorking ; }
set
{
isWorking = value ;
tbxName . Enabled = tbxAddress . Enabled = tbxDescription . Enabled = ! value ;
btnSave . Enabled = ! value ;
button1 . Enabled = ! value ;
pictureBox1 . Image = ( value ) ? Properties . Resources . aniZomq2x32 : null ;
}
}
private void textBox_PreviewKeyDown ( object sender , PreviewKeyDownEventArgs e )
{
if ( this . IsWorking )
{
return ;
}
if ( e . KeyCode = = Keys . Escape )
{
this . Close ( ) ;
}
}
private void btnRetrievePage_Click ( object sender , EventArgs e )
{
if ( this . IsWorking )
{
return ;
}
if ( string . IsNullOrWhiteSpace ( tbxAddress . Text ) )
{
return ;
}
this . IsWorking = true ;
oToolTip . SetToolTip ( pictureBox1 , "" ) ;
BookmarkItem bi = new BookmarkItem ( ) ;
bi . OnRetrieveCompleted + = bookmarkItem_OnRetrieveCompleted ;
bi . RetrieveAsync ( tbxAddress . Text . Trim ( ) ) ;
}
private void btnSave_Click ( object sender , EventArgs e )
{
if ( this . IsWorking )
{
return ;
}
if ( bookmarkItem = = null )
{
bookmarkItem = new BookmarkItem ( ) ;
}
bookmarkItem . ChangeName ( tbxName . Text . Trim ( ) ) ;
2019-04-07 17:29:33 +00:00
bookmarkItem . SiteAddress = tbxAddress . Text . Trim ( ) ;
2017-07-30 11:59:34 +00:00
bookmarkItem . Description = tbxDescription . Text . Trim ( ) ;
parentNode . Text = tbxName . Text . Trim ( ) ;
//// parentNode.ImageIndex = parentNode.SelectedImageIndex = 3;
2019-04-07 17:29:33 +00:00
parentNode . ToolTipText = string . Concat ( bookmarkItem . SiteAddress , Environment . NewLine , bookmarkItem . Description ) . Trim ( ) ;
2017-07-30 11:59:34 +00:00
parentNode . Tag = bookmarkItem ;
BookmarkTreeView bookmarkTreeView = ( BookmarkTreeView ) parentNode . TreeView ;
if ( bookmarkTreeView ! = null )
{
parentNode . ImageIndex = parentNode . SelectedImageIndex = bookmarkTreeView . AddToIconList ( bookmarkItem ) ;
}
this . Close ( ) ;
}
protected void bookmarkItem_OnRetrieveCompleted ( BookmarkItem sender , bool hasError , string message )
{
2019-04-07 17:29:33 +00:00
if ( string . IsNullOrEmpty ( sender . SiteName ) )
2017-07-30 11:59:34 +00:00
{
if ( MessageBox . Show ( "The page could not be retrieved or the title is blank. Do you want to keep your original title?" , "Keep original?" , MessageBoxButtons . YesNo , MessageBoxIcon . Question ) = = DialogResult . No )
{
2019-04-07 17:29:33 +00:00
bookmarkItem . SiteName = sender . SiteName ;
2017-07-30 11:59:34 +00:00
2019-04-07 17:29:33 +00:00
tbxName . Text = bookmarkItem . SiteName ;
2017-07-30 11:59:34 +00:00
// don't replace with blank
if ( ! string . IsNullOrEmpty ( sender . Description ) )
{
bookmarkItem . Description = sender . Description ;
}
}
}
else
{
2019-04-07 17:29:33 +00:00
bookmarkItem . SiteName = sender . SiteName ;
2017-07-30 11:59:34 +00:00
2019-04-07 17:29:33 +00:00
tbxName . Text = bookmarkItem . SiteName ;
2017-07-30 11:59:34 +00:00
// don't replace with blank
if ( ! string . IsNullOrEmpty ( sender . Description ) )
{
bookmarkItem . Description = sender . Description ;
}
}
// don't replace with blank
if ( sender . IconData ! = null )
{
bookmarkItem . IconData = sender . IconData ;
}
tbxDescription . Text = bookmarkItem . Description ;
pbxIcon . Image = ( bookmarkItem . Icon = = null ) ? parentNode . TreeView . ImageList . Images [ 3 ] : bookmarkItem . Icon ;
if ( hasError )
{
oToolTip . SetToolTip ( pictureBox1 , message ) ;
}
this . IsWorking = false ;
}
}
}