note
	description	: "Main window for this application"
	author		: "Generated by the New Vision2 Application Wizard."
	date		: "$Date: 2012/12/6 17:58:19 $"
	revision	: "1.0.0"

class
	MAIN_WINDOW

inherit
	EV_TITLED_WINDOW
		redefine
			initialize,
			is_in_default_state
		end

	INTERFACE_NAMES
		export
			{NONE} all
		undefine
			default_create, copy
		end

create
	default_create

feature {NONE} -- Implementation / Constants

	Window_title: STRING = "Downloader"
			-- Title of the window.

	Window_width: INTEGER = 400
			-- Initial width for this window.

	Window_height: INTEGER = 400
			-- Initial height for this window.

	BORDER_MARGIN : INTEGER = 5
	MARGIN : INTEGER = 15

	main_container: EV_FIXED
			-- Main container (contains all widgets displayed in this window)
	attribute
		create Result.default_create
	end

	standard_status_bar: EV_STATUS_BAR
			-- Standard status bar for this window
	attribute
		create Result
		Result.set_border_width (2)
	end

	standard_status_label: EV_LABEL
			-- Label situated in the standard status bar.
			--
			-- Note: Call `standard_status_label.set_text (...)' to change the text
			--       displayed in the status bar.
	attribute
		create Result.make_with_text ("Download not started...")
		Result.align_text_left
	end

	download_button : EV_BUTTON
	attribute
		create Result.make_with_text_and_action ("Start download", agent on_download_button_clicked)
	end

	cancel_button : EV_BUTTON
	attribute
		create Result.make_with_text_and_action ("Cancel download", agent on_cancel_button_clicked)
	end

	url_text :  EV_TEXT_FIELD
	attribute
		create Result.make_with_text ("http://download.thinkbroadband.com/50MB.zip")
	end

	result_text : EV_TEXT_FIELD
	attribute
		create Result.make_with_text ("")
	end

	download_started : BOOLEAN

	downloader : separate DOWNLOADER
	attribute
		create Result.make

		subscribe(Result)
	end

feature {NONE} -- Initialization

	initialize
			-- Build the interface for this window.
		do
			Precursor {EV_TITLED_WINDOW}

				-- Create and add the status bar.
			build_standard_status_bar
			lower_bar.extend (standard_status_bar)

			build_main_container
			extend (main_container)

				-- Execute `request_close_window' when the user clicks
				-- on the cross in the title bar.
			close_request_actions.extend (agent request_close_window)

				-- Set the title of the window
			set_title (Window_title)

				-- Set the initial size of the window
			set_size (Window_width, Window_height)
		end

	is_in_default_state: BOOLEAN
			-- Is the window in its default state
			-- (as stated in `initialize')
		do
			Result := true
		--	Result := (width = Window_width) and then
		--		(height = Window_height) and then
		--		(title.is_equal (Window_title))
		end


feature {NONE} -- StatusBar Implementation



	build_standard_status_bar
			-- Create and populate the standard toolbar.
		do
			standard_status_bar.extend (standard_status_label)
		end

feature {NONE} -- Implementation, Close event

	request_close_window
	do
		destroy;
		destroy_app((create {EV_ENVIRONMENT}).application)
	end

	destroy_app(l_app: detachable separate EV_APPLICATION)
	do
		l_app.destroy
	end

feature {NONE} -- Implementation



	build_main_container
			-- Create and populate `main_container'.
		do
			lock_update

			download_button.set_minimum_width (140)
			cancel_button.set_minimum_width (140)

			url_text.set_minimum_width (((window_width / 2) * 2).truncated_to_integer)

			result_text.set_minimum_width (url_text.width - download_button.width - margin)
			result_text.disable_edit

			main_container.extend (download_button)
			main_container.extend (cancel_button)
			main_container.extend (url_text)
			main_container.extend (result_text)

			main_container.set_item_y_position (url_text, 30)
			main_container.set_item_x_position (url_text, border_margin)

			main_container.set_item_y_position (download_button, url_text.y_position + url_text.height + margin)
			main_container.set_item_x_position (download_button, border_margin)

			main_container.set_item_y_position (cancel_button, download_button.y_position + download_button.height + margin)
			main_container.set_item_x_position (cancel_button, border_margin)

			main_container.set_item_y_position (result_text, download_button.y_position)
			main_container.set_item_x_position (result_text, download_button.width + download_button.x_position + margin)
			unlock_update
		end

	on_download_button_clicked
	do
		download_async(downloader)
		standard_status_label.set_text ("Download started!")
		result_text.set_text ("")
	end

	on_cancel_button_clicked
	do
		cancel_async (downloader)
	end

	subscribe(dl: separate DOWNLOADER)
	do
		dl.download_completed.subscribe (agent on_download_completed)
		dl.download_progress_changed.subscribe ( agent on_progress)
		dl.download_cancelled.subscribe(agent on_download_cancelled)
	end

	download_async(dl: separate DOWNLOADER)
	do
		set_url(dl, url_text.text.to_string_8)
		dl.download_async
	end

	set_url(a_dl: separate DOWNLOADER; a_url : STRING)
	local
		sep_url : separate STRING
	do
		create sep_url.make_from_separate (a_url)

		a_dl.set_url(sep_url)
	end

	cancel_async(dl : separate DOWNLOADER)
	do
		dl.cancel

	end

	on_download_cancelled
	do
		result_text.set_text ("Download cancelled")
		standard_status_label.set_text ("Download not started...")
	end

	on_download_completed
	do
		result_text.set_text ("Download finished")
	end

	on_progress(received_kb: INTEGER; total_kb : INTEGER)
	do
		standard_status_label.set_text ("Downloaded " + received_kb.out + " ouf of " + total_kb.out + "Kb")
	end

end
