2015-04-08 15:20:10 -07:00
using MatterHackers.Agg ;
2014-01-29 19:09:30 -08:00
using MatterHackers.Agg.UI ;
using MatterHackers.Localizations ;
2014-06-11 14:52:58 -07:00
using MatterHackers.MatterControl.PrinterCommunication ;
2015-05-01 13:28:31 -07:00
2015-04-08 15:20:10 -07:00
using System ;
2014-01-29 19:09:30 -08:00
namespace MatterHackers.MatterControl.PrinterControls.PrinterConnections
{
2015-04-08 15:20:10 -07:00
public class SetupStepComPortManual : SetupConnectionWidgetBase
{
private TextWidget printerComPortError ;
//GuiWidget comPortWidget;
private Button nextButton ;
private Button connectButton ;
private Button refreshButton ;
private Button printerComPortHelpLink ;
private TextWidget printerComPortHelpMessage ;
public SetupStepComPortManual ( ConnectionWindow windowController , GuiWidget containerWindowToClose , PrinterSetupStatus setupPrinterStatus )
: base ( windowController , containerWindowToClose , setupPrinterStatus )
{
linkButtonFactory . fontSize = 8 ;
FlowLayoutWidget printerComPortContainer = createComPortContainer ( ) ;
contentRow . AddChild ( printerComPortContainer ) ;
{
//Construct buttons
2014-03-11 15:24:47 -07:00
nextButton = textImageButtonFactory . Generate ( LocalizedString . Get ( "Done" ) ) ;
2015-04-08 15:20:10 -07:00
nextButton . Click + = new EventHandler ( NextButton_Click ) ;
nextButton . Visible = false ;
2014-01-29 19:09:30 -08:00
2014-03-11 15:24:47 -07:00
connectButton = textImageButtonFactory . Generate ( LocalizedString . Get ( "Connect" ) ) ;
2015-04-08 15:20:10 -07:00
connectButton . Click + = new EventHandler ( ConnectButton_Click ) ;
2014-01-29 19:09:30 -08:00
2015-04-08 15:20:10 -07:00
PrinterConnectionAndCommunication . Instance . CommunicationStateChanged . RegisterEvent ( onPrinterStatusChanged , ref unregisterEvents ) ;
2014-01-29 19:09:30 -08:00
2014-03-11 15:24:47 -07:00
refreshButton = textImageButtonFactory . Generate ( LocalizedString . Get ( "Refresh" ) ) ;
2015-04-08 15:20:10 -07:00
refreshButton . Click + = new EventHandler ( RefreshButton_Click ) ;
GuiWidget hSpacer = new GuiWidget ( ) ;
hSpacer . HAnchor = HAnchor . ParentLeftRight ;
//Add buttons to buttonContainer
footerRow . AddChild ( nextButton ) ;
footerRow . AddChild ( connectButton ) ;
footerRow . AddChild ( refreshButton ) ;
footerRow . AddChild ( hSpacer ) ;
footerRow . AddChild ( cancelButton ) ;
}
}
private event EventHandler unregisterEvents ;
public override void OnClosed ( EventArgs e )
{
if ( unregisterEvents ! = null )
{
unregisterEvents ( this , null ) ;
}
base . OnClosed ( e ) ;
}
private FlowLayoutWidget createComPortContainer ( )
{
FlowLayoutWidget container = new FlowLayoutWidget ( FlowDirection . TopToBottom ) ;
container . Margin = new BorderDouble ( 0 ) ;
container . VAnchor = VAnchor . ParentBottomTop ;
BorderDouble elementMargin = new BorderDouble ( top : 3 ) ;
2014-01-29 19:09:30 -08:00
2014-03-11 15:24:47 -07:00
string serialPortLabel = LocalizedString . Get ( "Serial Port" ) ;
2014-01-29 19:09:30 -08:00
string serialPortLabelFull = string . Format ( "{0}:" , serialPortLabel ) ;
TextWidget comPortLabel = new TextWidget ( serialPortLabelFull , 0 , 0 , 12 ) ;
2015-04-08 15:20:10 -07:00
comPortLabel . TextColor = this . defaultTextColor ;
comPortLabel . Margin = new BorderDouble ( 0 , 0 , 0 , 10 ) ;
comPortLabel . HAnchor = HAnchor . ParentLeftRight ;
2014-01-29 19:09:30 -08:00
2015-05-01 13:28:31 -07:00
FlowLayoutWidget serialPortContainer = new FlowLayoutWidget ( FlowDirection . TopToBottom ) ;
CreateSerialPortControls ( serialPortContainer , null ) ;
2014-01-29 19:09:30 -08:00
2015-04-08 15:20:10 -07:00
FlowLayoutWidget comPortMessageContainer = new FlowLayoutWidget ( ) ;
comPortMessageContainer . Margin = elementMargin ;
comPortMessageContainer . HAnchor = HAnchor . ParentLeftRight ;
2014-01-29 19:09:30 -08:00
2014-03-11 15:24:47 -07:00
printerComPortError = new TextWidget ( LocalizedString . Get ( "Currently available serial ports." ) , 0 , 0 , 10 ) ;
2014-03-24 14:44:43 -07:00
printerComPortError . TextColor = ActiveTheme . Instance . PrimaryTextColor ;
2015-04-08 15:20:10 -07:00
printerComPortError . AutoExpandBoundsToText = true ;
2014-01-29 19:09:30 -08:00
2014-03-11 15:24:47 -07:00
printerComPortHelpLink = linkButtonFactory . Generate ( LocalizedString . Get ( "What's this?" ) ) ;
2015-04-08 15:20:10 -07:00
printerComPortHelpLink . Margin = new BorderDouble ( left : 5 ) ;
printerComPortHelpLink . VAnchor = VAnchor . ParentBottom ;
printerComPortHelpLink . Click + = new EventHandler ( printerComPortHelp_Click ) ;
2014-01-29 19:09:30 -08:00
2014-03-11 15:24:47 -07:00
printerComPortHelpMessage = new TextWidget ( LocalizedString . Get ( "The 'Serial Port' identifies which connected device is\nyour printer. Changing which usb plug you use may\nchange the associated serial port.\n\nTip: If you are uncertain, plug-in in your printer and hit\nrefresh. The new port that appears should be your\nprinter." ) , 0 , 0 , 10 ) ;
2014-03-24 14:44:43 -07:00
printerComPortHelpMessage . TextColor = ActiveTheme . Instance . PrimaryTextColor ;
2015-04-08 15:20:10 -07:00
printerComPortHelpMessage . Margin = new BorderDouble ( top : 10 ) ;
printerComPortHelpMessage . Visible = false ;
2014-01-29 19:09:30 -08:00
2015-04-08 15:20:10 -07:00
comPortMessageContainer . AddChild ( printerComPortError ) ;
comPortMessageContainer . AddChild ( printerComPortHelpLink ) ;
2014-01-29 19:09:30 -08:00
2015-04-08 15:20:10 -07:00
container . AddChild ( comPortLabel ) ;
2015-05-01 13:28:31 -07:00
container . AddChild ( serialPortContainer ) ;
2015-04-08 15:20:10 -07:00
container . AddChild ( comPortMessageContainer ) ;
container . AddChild ( printerComPortHelpMessage ) ;
2014-01-29 19:09:30 -08:00
2015-04-08 15:20:10 -07:00
container . HAnchor = HAnchor . ParentLeftRight ;
return container ;
}
2014-01-29 19:09:30 -08:00
2015-04-08 15:20:10 -07:00
private void onPrinterStatusChanged ( object sender , EventArgs e )
{
if ( PrinterConnectionAndCommunication . Instance . PrinterIsConnected )
{
onConnectionSuccess ( ) ;
}
else if ( PrinterConnectionAndCommunication . Instance . CommunicationState ! = PrinterConnectionAndCommunication . CommunicationStates . AttemptingToConnect )
{
onConnectionFailed ( ) ;
}
}
private void onConnectionFailed ( )
{
printerComPortHelpLink . Visible = false ;
printerComPortError . TextColor = RGBA_Bytes . Red ;
2014-03-11 15:24:47 -07:00
printerComPortError . Text = LocalizedString . Get ( "Uh-oh! Could not connect to printer." ) ;
2015-04-08 15:20:10 -07:00
connectButton . Visible = true ;
nextButton . Visible = false ;
}
private void onConnectionSuccess ( )
{
printerComPortHelpLink . Visible = false ;
printerComPortError . TextColor = this . subContainerTextColor ;
2014-03-11 15:24:47 -07:00
printerComPortError . Text = LocalizedString . Get ( "Connection succeeded!" ) ;
2015-04-08 15:20:10 -07:00
nextButton . Visible = true ;
connectButton . Visible = false ;
}
private void printerComPortHelp_Click ( object sender , EventArgs mouseEvent )
{
printerComPortHelpMessage . Visible = ! printerComPortHelpMessage . Visible ;
}
private void MoveToNextWidget ( object state )
{
// you can call this like this
// AfterUiEvents.AddAction(new AfterUIAction(MoveToNextWidget));
if ( this . currentPrinterSetupStatus . DriversToInstall . Count > 0 )
{
Parent . AddChild ( new SetupStepInstallDriver ( ( ConnectionWindow ) Parent , Parent , this . currentPrinterSetupStatus ) ) ;
Parent . RemoveChild ( this ) ;
}
else
{
Parent . AddChild ( new SetupStepComPortOne ( ( ConnectionWindow ) Parent , Parent , this . currentPrinterSetupStatus ) ) ;
Parent . RemoveChild ( this ) ;
}
}
2015-06-11 12:06:40 -07:00
private void RecreateCurrentWidget ( )
2015-04-08 15:20:10 -07:00
{
Parent . AddChild ( new SetupStepComPortManual ( ( ConnectionWindow ) Parent , Parent , this . currentPrinterSetupStatus ) ) ;
Parent . RemoveChild ( this ) ;
}
private void RefreshButton_Click ( object sender , EventArgs mouseEvent )
{
UiThread . RunOnIdle ( RecreateCurrentWidget ) ;
}
private void ConnectButton_Click ( object sender , EventArgs mouseEvent )
{
string serialPort ;
try
{
serialPort = GetSelectedSerialPort ( ) ;
this . ActivePrinter . ComPort = serialPort ;
this . ActivePrinter . Commit ( ) ;
printerComPortHelpLink . Visible = false ;
printerComPortError . TextColor = this . subContainerTextColor ;
2014-04-15 10:11:56 -07:00
string printerComPortErrorLabel = LocalizedString . Get ( "Attempting to connect" ) ;
2015-04-08 15:20:10 -07:00
string printerComPortErrorLabelFull = string . Format ( "{0}..." , printerComPortErrorLabel ) ;
2014-04-15 10:11:56 -07:00
printerComPortError . Text = printerComPortErrorLabelFull ;
2015-04-08 15:20:10 -07:00
printerComPortError . TextColor = ActiveTheme . Instance . PrimaryTextColor ;
ActivePrinterProfile . Instance . ActivePrinter = this . ActivePrinter ;
PrinterConnectionAndCommunication . Instance . ConnectToActivePrinter ( ) ;
connectButton . Visible = false ;
refreshButton . Visible = false ;
}
catch
{
printerComPortHelpLink . Visible = false ;
printerComPortError . TextColor = RGBA_Bytes . Red ;
2014-03-11 15:24:47 -07:00
printerComPortError . Text = LocalizedString . Get ( "Oops! Please select a serial port." ) ;
2015-04-08 15:20:10 -07:00
}
}
private void NextButton_Click ( object sender , EventArgs mouseEvent )
{
2015-06-11 12:06:40 -07:00
UiThread . RunOnIdle ( Parent . Close ) ;
2015-04-08 15:20:10 -07:00
}
}
}