2014-01-29 19:09:30 -08:00
using System ;
using System.Collections.Generic ;
using MatterHackers.Agg ;
using MatterHackers.Agg.UI ;
using MatterHackers.Localizations ;
2014-06-11 14:52:58 -07:00
using MatterHackers.MatterControl.PrinterCommunication ;
2014-07-28 13:48:28 -07:00
using MatterHackers.SerialPortCommunication.FrostedSerial ;
2014-01-29 19:09:30 -08:00
namespace MatterHackers.MatterControl.PrinterControls.PrinterConnections
{
public class SetupStepComPortManual : SetupConnectionWidgetBase
{
List < SerialPortIndexRadioButton > SerialPortButtonsList = new List < SerialPortIndexRadioButton > ( ) ;
TextWidget printerComPortError ;
//GuiWidget comPortWidget;
Button nextButton ;
Button connectButton ;
Button refreshButton ;
Button printerComPortHelpLink ;
TextWidget printerComPortHelpMessage ;
bool printerComPortIsAvailable = false ;
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" ) ) ;
2014-01-29 19:09:30 -08:00
nextButton . Click + = new ButtonBase . ButtonEventHandler ( NextButton_Click ) ;
nextButton . Visible = false ;
2014-03-11 15:24:47 -07:00
connectButton = textImageButtonFactory . Generate ( LocalizedString . Get ( "Connect" ) ) ;
2014-01-29 19:09:30 -08:00
connectButton . Click + = new ButtonBase . ButtonEventHandler ( ConnectButton_Click ) ;
2014-06-11 14:52:58 -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" ) ) ;
2014-01-29 19:09:30 -08:00
refreshButton . Click + = new ButtonBase . ButtonEventHandler ( 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 ) ;
}
}
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-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 ) ;
comPortLabel . TextColor = this . defaultTextColor ;
comPortLabel . Margin = new BorderDouble ( 0 , 0 , 0 , 10 ) ;
comPortLabel . HAnchor = HAnchor . ParentLeftRight ;
FlowLayoutWidget comPortWidget = GetComPortWidget ( ) ;
comPortWidget . HAnchor = HAnchor . ParentLeftRight ;
FlowLayoutWidget comPortMessageContainer = new FlowLayoutWidget ( ) ;
comPortMessageContainer . Margin = elementMargin ;
comPortMessageContainer . HAnchor = HAnchor . ParentLeftRight ;
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 ;
2014-01-29 19:09:30 -08:00
printerComPortError . AutoExpandBoundsToText = true ;
2014-03-11 15:24:47 -07:00
printerComPortHelpLink = linkButtonFactory . Generate ( LocalizedString . Get ( "What's this?" ) ) ;
2014-01-29 19:09:30 -08:00
printerComPortHelpLink . Margin = new BorderDouble ( left : 5 ) ;
printerComPortHelpLink . VAnchor = VAnchor . ParentBottom ;
printerComPortHelpLink . Click + = new ButtonBase . ButtonEventHandler ( printerComPortHelp_Click ) ;
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 ;
2014-01-29 19:09:30 -08:00
printerComPortHelpMessage . Margin = new BorderDouble ( top : 10 ) ;
printerComPortHelpMessage . Visible = false ;
comPortMessageContainer . AddChild ( printerComPortError ) ;
comPortMessageContainer . AddChild ( printerComPortHelpLink ) ;
container . AddChild ( comPortLabel ) ;
container . AddChild ( comPortWidget ) ;
container . AddChild ( comPortMessageContainer ) ;
container . AddChild ( printerComPortHelpMessage ) ;
container . HAnchor = HAnchor . ParentLeftRight ;
return container ;
}
FlowLayoutWidget GetComPortWidget ( )
{
FlowLayoutWidget container = new FlowLayoutWidget ( FlowDirection . TopToBottom ) ;
int portIndex = 0 ;
2014-07-28 13:48:28 -07:00
foreach ( string serialPort in FrostedSerialPort . GetPortNames ( ) )
2014-01-29 19:09:30 -08:00
{
//Filter com port list based on usb type (applies to Mac mostly)
bool looks_like_mac = serialPort . StartsWith ( "/dev/tty." ) ;
bool looks_like_pc = serialPort . StartsWith ( "COM" ) ;
if ( looks_like_mac | | looks_like_pc )
{
SerialPortIndexRadioButton comPortOption = createComPortOption ( serialPort ) ;
container . AddChild ( comPortOption ) ;
portIndex + + ;
}
}
//If there are no com ports in the filtered list assume we are missing something and show the unfiltered list
if ( portIndex = = 0 )
{
2014-07-28 13:48:28 -07:00
foreach ( string serialPort in FrostedSerialPort . GetPortNames ( ) )
2014-01-29 19:09:30 -08:00
{
SerialPortIndexRadioButton comPortOption = createComPortOption ( serialPort ) ;
container . AddChild ( comPortOption ) ;
portIndex + + ;
}
}
if ( ! printerComPortIsAvailable & & this . ActivePrinter . ComPort ! = null )
{
SerialPortIndexRadioButton comPortOption = createComPortOption ( this . ActivePrinter . ComPort ) ;
comPortOption . Enabled = false ;
container . AddChild ( comPortOption ) ;
portIndex + + ;
}
//If there are still no com ports show a message to that effect
if ( portIndex = = 0 )
{
2014-03-11 15:24:47 -07:00
TextWidget comPortOption = new TextWidget ( LocalizedString . Get ( "No COM ports available" ) ) ;
2014-01-29 19:09:30 -08:00
comPortOption . Margin = new BorderDouble ( 3 , 6 , 5 , 6 ) ;
comPortOption . TextColor = this . subContainerTextColor ;
container . AddChild ( comPortOption ) ;
}
return container ;
}
public SerialPortIndexRadioButton createComPortOption ( string serialPort )
{
//Add formatting here to make port names prettier
string portName = serialPort ;
SerialPortIndexRadioButton comPortOption = new SerialPortIndexRadioButton ( portName , serialPort ) ;
SerialPortButtonsList . Add ( comPortOption ) ;
comPortOption . Margin = new BorderDouble ( 3 , 3 , 5 , 3 ) ;
comPortOption . TextColor = this . subContainerTextColor ;
if ( this . ActivePrinter . ComPort = = serialPort )
{
comPortOption . Checked = true ;
printerComPortIsAvailable = true ;
}
return comPortOption ;
}
void onPrinterStatusChanged ( object sender , EventArgs e )
{
2014-06-11 14:52:58 -07:00
if ( PrinterConnectionAndCommunication . Instance . PrinterIsConnected )
2014-01-29 19:09:30 -08:00
{
onConnectionSuccess ( ) ;
}
2014-06-11 14:52:58 -07:00
else if ( PrinterConnectionAndCommunication . Instance . CommunicationState ! = PrinterConnectionAndCommunication . CommunicationStates . AttemptingToConnect )
2014-01-29 19:09:30 -08:00
{
onConnectionFailed ( ) ;
}
}
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." ) ;
2014-01-29 19:09:30 -08:00
connectButton . Visible = true ;
nextButton . Visible = false ;
}
void onConnectionSuccess ( )
{
printerComPortHelpLink . Visible = false ;
2014-05-08 10:42:53 -07:00
printerComPortError . TextColor = this . subContainerTextColor ;
2014-03-11 15:24:47 -07:00
printerComPortError . Text = LocalizedString . Get ( "Connection succeeded!" ) ;
2014-01-29 19:09:30 -08:00
nextButton . Visible = true ;
connectButton . Visible = false ;
}
void printerComPortHelp_Click ( object sender , MouseEventArgs mouseEvent )
{
printerComPortHelpMessage . Visible = ! printerComPortHelpMessage . Visible ;
}
void MoveToNextWidget ( object state )
{
// you can call this like this
// AfterUiEvents.AddAction(new AfterUIAction(MoveToNextWidget));
if ( this . PrinterSetupStatus . DriverNeedsToBeInstalled )
{
Parent . AddChild ( new SetupStepInstallDriver ( ( ConnectionWindow ) Parent , Parent , this . PrinterSetupStatus ) ) ;
Parent . RemoveChild ( this ) ;
}
else
{
Parent . AddChild ( new SetupStepComPortOne ( ( ConnectionWindow ) Parent , Parent , this . PrinterSetupStatus ) ) ;
Parent . RemoveChild ( this ) ;
}
}
void RecreateCurrentWidget ( object state )
{
Parent . AddChild ( new SetupStepComPortManual ( ( ConnectionWindow ) Parent , Parent , this . PrinterSetupStatus ) ) ;
Parent . RemoveChild ( this ) ;
}
void RefreshButton_Click ( object sender , MouseEventArgs mouseEvent )
{
UiThread . RunOnIdle ( RecreateCurrentWidget ) ;
}
void ConnectButton_Click ( object sender , MouseEventArgs mouseEvent )
{
string serialPort ;
try
{
serialPort = GetSelectedSerialPort ( ) ;
this . ActivePrinter . ComPort = serialPort ;
this . ActivePrinter . Commit ( ) ;
printerComPortHelpLink . Visible = false ;
2014-05-08 10:42:53 -07:00
printerComPortError . TextColor = this . subContainerTextColor ;
2014-04-15 10:11:56 -07:00
string printerComPortErrorLabel = LocalizedString . Get ( "Attempting to connect" ) ;
string printerComPortErrorLabelFull = string . Format ( "{0}..." , printerComPortErrorLabel ) ;
printerComPortError . Text = printerComPortErrorLabelFull ;
2014-04-16 11:19:34 -07:00
printerComPortError . TextColor = ActiveTheme . Instance . PrimaryTextColor ;
2014-02-14 12:06:44 -08:00
ActivePrinterProfile . Instance . ActivePrinter = this . ActivePrinter ;
2014-06-11 14:52:58 -07:00
PrinterConnectionAndCommunication . Instance . ConnectToActivePrinter ( ) ;
2014-01-29 19:09:30 -08:00
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." ) ;
2014-01-29 19:09:30 -08:00
}
}
void NextButton_Click ( object sender , MouseEventArgs mouseEvent )
{
2014-04-16 11:19:34 -07:00
UiThread . RunOnIdle ( ( state ) = >
{
Parent . Close ( ) ;
} ) ;
2014-01-29 19:09:30 -08:00
}
private string GetSelectedSerialPort ( )
{
foreach ( SerialPortIndexRadioButton button in SerialPortButtonsList )
{
if ( button . Checked )
{
return button . PortValue ;
}
}
2014-03-11 15:24:47 -07:00
throw new Exception ( LocalizedString . Get ( "Could not find a selected button." ) ) ;
2014-01-29 19:09:30 -08:00
}
}
}