Imports Stax
Imports Stax.UI
Imports Stax.AppFramework

Public Class JobsForm
    Inherits ExtendedForm

#Region " Vom Windows Form Designer generierter Code "
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    ' Für Windows Form-Designer erforderlich
    Private components As System.ComponentModel.IContainer

    'HINWEIS: Die folgende Prozedur ist für den Windows Form-Designer erforderlich
    'Sie kann mit dem Windows Form-Designer modifiziert werden.
    'Verwenden Sie nicht den Code-Editor zur Bearbeitung.
    Friend WithEvents bAdd As System.Windows.Forms.Button
    Friend WithEvents bRemove As System.Windows.Forms.Button
    Friend WithEvents bLoad As System.Windows.Forms.Button
    Friend WithEvents clb As System.Windows.Forms.CheckedListBox
    Friend WithEvents bDown As System.Windows.Forms.Button
    Friend WithEvents bUp As System.Windows.Forms.Button
    Public WithEvents bStart As System.Windows.Forms.Button
    Friend WithEvents bClose As System.Windows.Forms.Button
    Friend WithEvents bHelp As System.Windows.Forms.Button
    Friend WithEvents SmartTagProvider As Stax.UI.Design.SmartTagProvider
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.clb = New System.Windows.Forms.CheckedListBox
        Me.bStart = New System.Windows.Forms.Button
        Me.bRemove = New System.Windows.Forms.Button
        Me.bAdd = New System.Windows.Forms.Button
        Me.bClose = New System.Windows.Forms.Button
        Me.bLoad = New System.Windows.Forms.Button
        Me.bDown = New System.Windows.Forms.Button
        Me.bUp = New System.Windows.Forms.Button
        Me.bHelp = New System.Windows.Forms.Button
        Me.SmartTagProvider = New Stax.UI.Design.SmartTagProvider
        Me.SuspendLayout()
        '
        'clb
        '
        Me.clb.Anchor = CType((((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom) _
                    Or System.Windows.Forms.AnchorStyles.Left) _
                    Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
        Me.clb.FormattingEnabled = True
        Me.clb.HorizontalScrollbar = True
        Me.clb.IntegralHeight = False
        Me.clb.Location = New System.Drawing.Point(13, 13)
        Me.clb.Name = "clb"
        Me.clb.Size = New System.Drawing.Size(336, 233)
        Me.clb.TabIndex = 14
        '
        'bStart
        '
        Me.bStart.Anchor = CType((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
        Me.bStart.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft
        Me.bStart.ImeMode = System.Windows.Forms.ImeMode.NoControl
        Me.bStart.Location = New System.Drawing.Point(360, 162)
        Me.bStart.Name = "bStart"
        Me.bStart.Size = New System.Drawing.Size(75, 24)
        Me.bStart.TabIndex = 13
        Me.bStart.Text = "Start"
        '
        'bRemove
        '
        Me.bRemove.Anchor = CType((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
        Me.bRemove.Image = Global.StaxRip.My.Resources.Resources.Minus16
        Me.bRemove.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft
        Me.bRemove.ImeMode = System.Windows.Forms.ImeMode.NoControl
        Me.bRemove.Location = New System.Drawing.Point(360, 42)
        Me.bRemove.Name = "bRemove"
        Me.bRemove.Size = New System.Drawing.Size(75, 24)
        Me.bRemove.TabIndex = 11
        Me.bRemove.Text = "Remove"
        Me.bRemove.TextImageRelation = System.Windows.Forms.TextImageRelation.ImageBeforeText
        '
        'bAdd
        '
        Me.bAdd.Anchor = CType((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
        Me.bAdd.Cursor = System.Windows.Forms.Cursors.Default
        Me.bAdd.Image = Global.StaxRip.My.Resources.Resources.Plus16
        Me.bAdd.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft
        Me.bAdd.ImeMode = System.Windows.Forms.ImeMode.NoControl
        Me.bAdd.Location = New System.Drawing.Point(360, 12)
        Me.bAdd.Name = "bAdd"
        Me.bAdd.Size = New System.Drawing.Size(75, 24)
        Me.bAdd.TabIndex = 10
        Me.bAdd.Text = "Add"
        '
        'bClose
        '
        Me.bClose.Anchor = CType((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
        Me.bClose.DialogResult = System.Windows.Forms.DialogResult.Cancel
        Me.bClose.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft
        Me.bClose.Location = New System.Drawing.Point(360, 222)
        Me.bClose.Name = "bClose"
        Me.bClose.Size = New System.Drawing.Size(75, 24)
        Me.bClose.TabIndex = 15
        Me.bClose.Text = "Close"
        '
        'bLoad
        '
        Me.bLoad.Anchor = CType((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
        Me.bLoad.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft
        Me.bLoad.Location = New System.Drawing.Point(360, 72)
        Me.bLoad.Name = "bLoad"
        Me.bLoad.Size = New System.Drawing.Size(75, 24)
        Me.bLoad.TabIndex = 16
        Me.bLoad.Text = "Load"
        '
        'bDown
        '
        Me.bDown.Anchor = CType((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
        Me.bDown.Image = Global.StaxRip.My.Resources.Resources.Down16
        Me.bDown.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft
        Me.bDown.Location = New System.Drawing.Point(360, 132)
        Me.bDown.Name = "bDown"
        Me.bDown.Size = New System.Drawing.Size(75, 24)
        Me.bDown.TabIndex = 17
        Me.bDown.Text = "Down"
        '
        'bUp
        '
        Me.bUp.Anchor = CType((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
        Me.bUp.Image = Global.StaxRip.My.Resources.Resources.Up16
        Me.bUp.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft
        Me.bUp.Location = New System.Drawing.Point(360, 102)
        Me.bUp.Name = "bUp"
        Me.bUp.Size = New System.Drawing.Size(75, 24)
        Me.bUp.TabIndex = 18
        Me.bUp.Text = "Up"
        '
        'bHelp
        '
        Me.bHelp.Anchor = CType((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
        Me.bHelp.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft
        Me.bHelp.Location = New System.Drawing.Point(360, 192)
        Me.bHelp.Name = "bHelp"
        Me.bHelp.Size = New System.Drawing.Size(75, 24)
        Me.bHelp.TabIndex = 21
        Me.bHelp.Text = "Help"
        Me.bHelp.TextImageRelation = System.Windows.Forms.TextImageRelation.ImageBeforeText
        '
        'JobsForm
        '
        Me.CancelButton = Me.bClose
        Me.ClientSize = New System.Drawing.Size(447, 261)
        Me.Controls.Add(Me.bHelp)
        Me.Controls.Add(Me.bUp)
        Me.Controls.Add(Me.bDown)
        Me.Controls.Add(Me.bLoad)
        Me.Controls.Add(Me.bClose)
        Me.Controls.Add(Me.clb)
        Me.Controls.Add(Me.bStart)
        Me.Controls.Add(Me.bRemove)
        Me.Controls.Add(Me.bAdd)
        Me.MaximizeBox = False
        Me.MinimizeBox = False
        Me.MinimumSize = New System.Drawing.Size(351, 295)
        Me.Name = "JobsForm"
        Me.ShowIcon = False
        Me.ShowInTaskbar = False
        Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent
        Me.Text = "Jobs"
        Me.ResumeLayout(False)

    End Sub

#End Region

    Public Start As Boolean

    Public Sub New(ByRef f As MainForm)
        MyBase.New()
        InitializeComponent()

        For Each i As KeyValuePair(Of String, Boolean) In s.Jobs
            clb.Items.Add(i.Key, i.Value)
        Next

        UpdateControls()
    End Sub

    Private Sub bNew_Click(ByVal sender As Object, ByVal e As EventArgs) Handles bAdd.Click
        If Not Paths.VerifyRequirements() Then
            Exit Sub
        End If

        If Not g.MainForm.ShowSaveDialog Then
            Exit Sub
        End If

        If clb.Items.Contains(g.ProjectPath) Then
            Msg.Show("Path already used.", "Already Used", MsgIcon.Warning)
            Exit Sub
        End If

        clb.Items.Add(g.ProjectPath, True)
        clb.SelectedIndex = clb.Items.Count - 1

        UpdateControls()
    End Sub

    Private Sub clb_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles clb.SelectedIndexChanged
        UpdateControls()
    End Sub

    Private Sub bDelete_Click(ByVal sender As Object, ByVal e As EventArgs) Handles bRemove.Click
        CheckedListBoxHelp.DeleteSelectedItem(clb)
        UpdateControls()
    End Sub

    Private Sub Start_Click(ByVal sender As Object, ByVal e As EventArgs) Handles bStart.Click
        If g.MainForm.IsSaveCanceled Then
            Exit Sub
        End If

        Start = True
        Close()
    End Sub

    Private Sub bLoad_Click(ByVal sender As Object, ByVal e As EventArgs) Handles bLoad.Click
        g.MainForm.OpenProject(clb.Text)
        UpdateControls()
    End Sub

    Private Sub bUp_Click(ByVal sender As Object, ByVal e As EventArgs) Handles bUp.Click
        CheckedListBoxHelp.MoveSelectedItemUp(clb)
        UpdateControls()
    End Sub

    Private Sub bDown_Click(ByVal sender As Object, ByVal e As EventArgs) Handles bDown.Click
        CheckedListBoxHelp.MoveSelectedItemDown(clb)
        UpdateControls()
    End Sub

    Private Sub UpdateControls()
        bAdd.Enabled = p.SourceFile <> "" OrElse p.AutoSourceFiles.Count > 0
        bRemove.Enabled = clb.SelectedIndex > -1
        bLoad.Enabled = clb.SelectedIndex > -1
        bUp.Enabled = clb.SelectedIndex > 0
        bDown.Enabled = clb.SelectedIndex > -1 AndAlso _
            clb.SelectedIndex < clb.Items.Count - 1

        bStart.Enabled = False

        For x As Integer = 0 To clb.Items.Count - 1
            If clb.GetItemChecked(x) Then
                bStart.Enabled = True
                Exit For
            End If
        Next
    End Sub

    Private Sub clb_MouseUp(ByVal sender As Object, ByVal e As MouseEventArgs) Handles clb.MouseUp
        UpdateControls()
    End Sub

    Private Sub clb_KeyUp(ByVal sender As Object, ByVal e As KeyEventArgs) Handles clb.KeyUp
        UpdateControls()
    End Sub

    Private Sub JobsForm_ActivatedFirstTime() Handles Me.ActivatedFirstTime
        If Not s.StringList.Contains(Me.GetType.FullName) Then
            bHelp.PerformClick()
            s.StringList.Add(Me.GetType.FullName)
        End If
    End Sub

    Private Sub bHelp_Click(ByVal sender As Object, ByVal e As EventArgs) Handles bHelp.Click
        Dim f As New HelpForm
        f.Document.WriteStart(Text)
        f.Document.WriteParagraph("The Jobs dialog allows to encode a batch of projects. Technically a job is a mere project file. The encoding of all checked projects in the list can be triggered by pressing the **Start** button.")
        f.Show()
    End Sub

    Private Sub clb_DoubleClick(ByVal sender As Object, ByVal e As EventArgs) Handles clb.DoubleClick
        If Not clb.SelectedItem Is Nothing Then
            bLoad.PerformClick()
        End If
    End Sub

    Private Sub JobsForm_FormClosing(ByVal sender As Object, ByVal e As FormClosingEventArgs) Handles Me.FormClosing
        s.Jobs.Clear()

        For x As Integer = 0 To clb.Items.Count - 1
            s.Jobs.Add(CStr(clb.Items(x)), clb.GetItemChecked(x))
        Next
    End Sub
End Class