Merge pull request 'C2022-029 - Export Single Procedure (Overwrite/Rename/Cancel)' (#341) from C2022-029 into Development

passed code review. ready for testing phase
This commit is contained in:
John Jenko 2024-06-20 10:02:52 -04:00
commit d0474b22f9
3 changed files with 138 additions and 16 deletions

View File

@ -241,6 +241,21 @@ namespace JR.Utils.GUI.Forms
return FlexibleMessageBoxForm.Show(owner, text, caption, buttons, icon, defaultButton); return FlexibleMessageBoxForm.Show(owner, text, caption, buttons, icon, defaultButton);
} }
/// <summary>
/// Shows the specified message box.
/// </summary>
/// <param name="owner">The owner.</param>
/// <param name="text">The text.</param>
/// <param name="caption">The caption.</param>
/// <param name="buttons">The buttons.</param>
/// <param name="icon">The icon.</param>
/// <param name="defaultButton">The default button.</param>
/// <returns>The dialog result.</returns>
public static DialogResult ShowCustom(IWin32Window owner, string text, string caption, MessageBoxButtons buttons, MessageBoxIcon icon)
{
return FlexibleMessageBoxForm.ShowCustom(null, text, caption, buttons, icon);
}
#endregion #endregion
#region Internal form class #region Internal form class
@ -420,15 +435,15 @@ namespace JR.Utils.GUI.Forms
private static readonly String STANDARD_MESSAGEBOX_SEPARATOR_SPACES = " "; private static readonly String STANDARD_MESSAGEBOX_SEPARATOR_SPACES = " ";
//These are the possible buttons (in a standard MessageBox) //These are the possible buttons (in a standard MessageBox)
private enum ButtonID { OK = 0, CANCEL, YES, NO, ABORT, RETRY, IGNORE }; private enum ButtonID { OK = 0, CANCEL, YES, NO, ABORT, RETRY, IGNORE, OVERWRITE, RENAME };
//These are the buttons texts for different languages. //These are the buttons texts for different languages.
//If you want to add a new language, add it here and in the GetButtonText-Function //If you want to add a new language, add it here and in the GetButtonText-Function
private enum TwoLetterISOLanguageID { en, de, es, it }; private enum TwoLetterISOLanguageID { en, de, es, it };
private static readonly String[] BUTTON_TEXTS_ENGLISH_EN = { "OK", "Cancel", "&Yes", "&No", "&Abort", "&Retry", "&Ignore" }; //Note: This is also the fallback language private static readonly String[] BUTTON_TEXTS_ENGLISH_EN = { "OK", "Cancel", "&Yes", "&No", "&Abort", "&Retry", "&Ignore", "&Overwrite", "&Rename" }; //Note: This is also the fallback language
private static readonly String[] BUTTON_TEXTS_GERMAN_DE = { "OK", "Abbrechen", "&Ja", "&Nein", "&Abbrechen", "&Wiederholen", "&Ignorieren" }; private static readonly String[] BUTTON_TEXTS_GERMAN_DE = { "OK", "Abbrechen", "&Ja", "&Nein", "&Abbrechen", "&Wiederholen", "&Ignorieren", "&Overwrite", "&Rename" };
private static readonly String[] BUTTON_TEXTS_SPANISH_ES = { "Aceptar", "Cancelar", "&Sí", "&No", "&Abortar", "&Reintentar", "&Ignorar" }; private static readonly String[] BUTTON_TEXTS_SPANISH_ES = { "Aceptar", "Cancelar", "&Sí", "&No", "&Abortar", "&Reintentar", "&Ignorar", "&Overwrite", "&Rename" };
private static readonly String[] BUTTON_TEXTS_ITALIAN_IT = { "OK", "Annulla", "&Sì", "&No", "&Interrompi", "&Riprova", "&Ignora" }; private static readonly String[] BUTTON_TEXTS_ITALIAN_IT = { "OK", "Annulla", "&Sì", "&No", "&Interrompi", "&Riprova", "&Ignora", "&Overwrite", "&Rename" };
#endregion #endregion
@ -693,6 +708,7 @@ namespace JR.Utils.GUI.Forms
flexibleMessageBoxForm.CancelButton = flexibleMessageBoxForm.button3; flexibleMessageBoxForm.CancelButton = flexibleMessageBoxForm.button3;
break; break;
case MessageBoxButtons.OK: case MessageBoxButtons.OK:
default: default:
@ -709,16 +725,38 @@ namespace JR.Utils.GUI.Forms
flexibleMessageBoxForm.defaultButton = defaultButton; flexibleMessageBoxForm.defaultButton = defaultButton;
} }
#endregion private static void SetDialogButtonsCustom(FlexibleMessageBoxForm flexibleMessageBoxForm)
{
flexibleMessageBoxForm.visibleButtonsCount = 3;
#region Private event handlers flexibleMessageBoxForm.button1.Visible = true;
flexibleMessageBoxForm.button1.Text = flexibleMessageBoxForm.GetButtonText(ButtonID.CANCEL);
flexibleMessageBoxForm.button1.DialogResult = DialogResult.Abort;
/// <summary> flexibleMessageBoxForm.button2.Visible = true;
/// Handles the Shown event of the FlexibleMessageBoxForm control. flexibleMessageBoxForm.button2.Text = flexibleMessageBoxForm.GetButtonText(ButtonID.OVERWRITE);
/// </summary> flexibleMessageBoxForm.button2.DialogResult = DialogResult.Retry;
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param> flexibleMessageBoxForm.button3.Visible = true;
private void FlexibleMessageBoxForm_Shown(object sender, EventArgs e) flexibleMessageBoxForm.button3.Text = flexibleMessageBoxForm.GetButtonText(ButtonID.RENAME);
flexibleMessageBoxForm.button3.DialogResult = DialogResult.Ignore;
flexibleMessageBoxForm.ControlBox = false;
}
#endregion
#region Private event handlers
/// <summary>
/// Handles the Shown event of the FlexibleMessageBoxForm control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
private void FlexibleMessageBoxForm_Shown(object sender, EventArgs e)
{ {
int buttonIndexToFocus = 1; int buttonIndexToFocus = 1;
Button buttonToFocus; Button buttonToFocus;
@ -866,6 +904,46 @@ namespace JR.Utils.GUI.Forms
return flexibleMessageBoxForm.ShowDialog(owner); return flexibleMessageBoxForm.ShowDialog(owner);
} }
/// <summary>
/// Shows the specified message box.
/// </summary>
/// <param name="owner">The owner.</param>
/// <param name="text">The text.</param>
/// <param name="caption">The caption.</param>
/// <param name="buttons">The buttons.</param>
/// <param name="icon">The icon.</param>
/// <param name="defaultButton">The default button.</param>
/// <returns>The dialog result.</returns>
public static DialogResult ShowCustom(IWin32Window owner, string text, string caption, MessageBoxButtons buttons, MessageBoxIcon icon)
{
//Create a new instance of the FlexibleMessageBox form
var flexibleMessageBoxForm = new FlexibleMessageBoxForm();
flexibleMessageBoxForm.ShowInTaskbar = false;
//Bind the caption and the message text
flexibleMessageBoxForm.CaptionText = caption;
flexibleMessageBoxForm.MessageText = text;
flexibleMessageBoxForm.FlexibleMessageBoxFormBindingSource.DataSource = flexibleMessageBoxForm;
//Set the buttons visibilities and texts. Also set a default button.
SetDialogButtonsCustom(flexibleMessageBoxForm);
//Set the dialogs icon. When no icon is used: Correct placement and width of rich text box.
SetDialogIcon(flexibleMessageBoxForm, icon);
//Set the font for all controls
flexibleMessageBoxForm.Font = FONT;
flexibleMessageBoxForm.richTextBoxMessage.Font = FONT;
//Calculate the dialogs start size (Try to auto-size width to show longest text row). Also set the maximum dialog size.
SetDialogSizes(flexibleMessageBoxForm, text, caption);
//Set the dialogs start position when given. Otherwise center the dialog on the current screen.
SetDialogStartPosition(flexibleMessageBoxForm, owner);
//Show the dialog
return flexibleMessageBoxForm.ShowDialog(owner);
}
#endregion #endregion
} //class FlexibleMessageBoxForm } //class FlexibleMessageBoxForm

View File

@ -99,6 +99,10 @@ namespace VEPROMS
MyProcedure = procedureInfo; MyProcedure = procedureInfo;
InitializeComponent(); InitializeComponent();
this.Text = mode + " Dialog for " + procedureInfo.DisplayNumber; this.Text = mode + " Dialog for " + procedureInfo.DisplayNumber;
//Preset path for single procedures.
PEIPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + @"\VEPROMS\PEI_" + Database.VEPROMS_SqlConnection.Database;
txtExport.Text = string.Format(@"{0}\{1}.pxml", PEIPath, MyProcedure.DisplayNumber.Replace("/", "_").Replace("\\", "_"));
} }
private void dlgExportImport_Load(object sender, EventArgs e) private void dlgExportImport_Load(object sender, EventArgs e)
{ {
@ -172,6 +176,7 @@ namespace VEPROMS
} }
else if (MyProcedure != null) else if (MyProcedure != null)
{ {
txtExport.Enabled = true;
txtExport.Text = string.Format(@"{0}\{1}.pxml", PEIPath, MyProcedure.DisplayNumber.Replace("/", "_").Replace("\\", "_")); txtExport.Text = string.Format(@"{0}\{1}.pxml", PEIPath, MyProcedure.DisplayNumber.Replace("/", "_").Replace("\\", "_"));
} }
} }
@ -190,6 +195,8 @@ namespace VEPROMS
private bool successfullExport = true; private bool successfullExport = true;
private void btnDoExport_Click(object sender, EventArgs e) private void btnDoExport_Click(object sender, EventArgs e)
{ {
btnExport.Enabled = false; btnExport.Enabled = false;
string msg = "Finished Exporting:\n\n"; string msg = "Finished Exporting:\n\n";
if (_MyMode.ToUpper().Contains("FORMAT")) if (_MyMode.ToUpper().Contains("FORMAT"))
@ -220,6 +227,42 @@ namespace VEPROMS
} }
else if (MyProcedure != null) else if (MyProcedure != null)
{ {
var fileLocation = txtExport.Text;
if (File.Exists(fileLocation))
{ // C2022-029 if an existing export of the same name is found, provide option to overwrite it
DialogResult ovewriteEx = FlexibleMessageBox.ShowCustom(null, "There is already another export file with the same name. You can choose to either overwrite the existing file or have the existing file renamed with the original creation date appended.\r\n\r\nSelecting 'Cancel' will cancel the export.", "What would you like to do?", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);// == DialogResult.Yes;
// Extract directory, filename, and extension
string directory = Path.GetDirectoryName(fileLocation);
string filename = Path.GetFileNameWithoutExtension(fileLocation);
string extension = Path.GetExtension(fileLocation);
fileLocation = $"{directory}\\{filename}{extension}";
if (ovewriteEx == DialogResult.Abort)
{
MessageBox.Show("Export has been cancelled", "You have chosen to cancel the export.", MessageBoxButtons.OK, MessageBoxIcon.Information); // C2020-042 changed mesage box title
btnCloseExport.Enabled = true;
return;
}
else if (ovewriteEx == DialogResult.Retry)
{
//Overwrite will occur, set msg.
msg = "The export file has been overwritten. ";
}
else if (ovewriteEx == DialogResult.Ignore)
{
// Get the modified date of the existing file, create a datestamp for use in name, set newlocation to move to
DateTime modifiedDate = File.GetLastWriteTime(fileLocation);
string datestamp = modifiedDate.ToString("yyyyMMddHHmmss");
string newFileLocation = $"{directory}\\{filename}_{datestamp}{extension}";
//Move and set msg.
File.Move(fileLocation, newFileLocation);
msg = "The previous export has been renamed, the export file has been created. ";
}
}
this.Cursor = Cursors.WaitCursor; this.Cursor = Cursors.WaitCursor;
MyStart = DateTime.Now; MyStart = DateTime.Now;
btnDoExport.Enabled = false; btnDoExport.Enabled = false;
@ -230,7 +273,7 @@ namespace VEPROMS
XmlElement xe = xd.CreateElement("formats"); XmlElement xe = xd.CreateElement("formats");
xd.DocumentElement.AppendChild(xe); xd.DocumentElement.AppendChild(xe);
ExportFormats(FormatInfoList.GetFormatInfoListUsed(), xe, "formats", false); ExportFormats(FormatInfoList.GetFormatInfoListUsed(), xe, "formats", false);
xd.Save(txtExport.Text); xd.Save(fileLocation);
TimeSpan elapsed = DateTime.Now.Subtract(MyStart); TimeSpan elapsed = DateTime.Now.Subtract(MyStart);
lblExportStatus.Text = "Export Completed in " + elapsed.ToString(); lblExportStatus.Text = "Export Completed in " + elapsed.ToString();
this.Cursor = Cursors.Default; this.Cursor = Cursors.Default;
@ -706,7 +749,7 @@ namespace VEPROMS
} }
catch (Exception ex) catch (Exception ex)
{ {
FlexibleMessageBox.Show("The import failed, check the error log for more information.", "Import Failed", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); FlexibleMessageBox.Show(null, "The import failed, check the error log for more information.", "Import Failed", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
_MyLog.Warn("Failure During Import", ex); _MyLog.Warn("Failure During Import", ex);
} }
return false; return false;

View File

@ -400,6 +400,7 @@ namespace VEPROMS
this.pnlImport.PerformLayout(); this.pnlImport.PerformLayout();
this.ResumeLayout(false); this.ResumeLayout(false);
} }
#endregion #endregion