indexing description: "[ A file selection dialog ]" date: "$Date$" revision: "$Revision$" class EM_FILE_DIALOG inherit EM_DIALOG redefine show end KL_SHARED_FILE_SYSTEM export {NONE} all end create make feature {NONE} -- Initialisation make is -- Initialise file dialog. local scrollpanel: EM_SCROLLPANEL do make_from_title ("Select file") set_dimension (400, 300) set_position ((Video_subsystem.video_surface_width-width)//2, (Video_subsystem.video_surface_height-height)//2) create current_directory_label.make_empty current_directory_label.set_position (2, 20) current_directory_label.set_dimension (396, 20) add_widget (current_directory_label) create directorylist.make_empty directorylist.selection_changed_event.subscribe (agent handle_directory_selected) create scrollpanel.make_from_widget (directorylist) scrollpanel.set_dimension (198, 210) scrollpanel.set_position (2, 40) add_widget (scrollpanel) create filelist.make_empty filelist.selection_changed_event.subscribe (agent handle_file_selected) create scrollpanel.make_from_widget (filelist) scrollpanel.set_dimension (198, 210) scrollpanel.set_position (200, 40) add_widget (scrollpanel) create filebox.make_empty filebox.set_position (2, 250) filebox.set_dimension (396, 20) add_widget (filebox) create ok_button.make_from_text ("OK") ok_button.set_position (width - 10 - 80, height - 25) ok_button.set_dimension (80, 20) ok_button.clicked_event.subscribe (agent handle_ok_clicked) add_widget (ok_button) create cancel_button.make_from_text ("Cancel") cancel_button.set_position (width - 10 - 80 - 5 - 80, height - 25) cancel_button.set_dimension (80, 20) cancel_button.clicked_event.subscribe (agent handle_cancel_clicked) add_widget (cancel_button) close_button_clicked_event.subscribe (agent handle_cancel_clicked) set_directory (File_system.current_working_directory) create button_clicked_event end feature -- Access directory: STRING -- Directory selected in dialog filename: STRING -- File selected in dialog absolute_filename: STRING is -- Absolute filename of selected file require is_file_selected do Result := File_system.pathname (directory, filename) end ok_button: EM_BUTTON -- Ok button cancel_button: EM_BUTTON -- Cancel button feature -- Status report was_ok_clicked: BOOLEAN -- Was ok button clicked? was_cancel_clicked: BOOLEAN -- Was cancel button clicked? feature -- Status report is_file_selected: BOOLEAN is -- Is any file selected? do Result := filename /= Void and then directory /= Void and then File_system.file_exists (File_system.pathname (directory, filename)) end feature -- Status setting show is -- Show dialog. do current_directory := File_system.absolute_pathname (File_system.current_working_directory) filename := Void was_ok_clicked := False was_cancel_clicked := False Precursor {EM_DIALOG} end feature -- Element change set_directory (a_directory_name: STRING) is -- Set require a_directory_name_not_void: a_directory_name /= Void local a_directory: KL_DIRECTORY directories: ARRAY [STRING] files: ARRAY [STRING] i: INTEGER do if File_system.directory_exists (a_directory_name) then current_directory := File_system.canonical_pathname (a_directory_name) else current_directory := File_system.canonical_pathname (File_system.current_working_directory) end current_directory_label.set_text (current_directory) directorylist.wipe_out if not File_system.is_root_directory (current_directory) then directorylist.put ("..") end create a_directory.make (current_directory) directories := a_directory.directory_names if directories /= Void then from i := directories.lower until i > directories.upper loop directorylist.put (directories.item (i)) i := i + 1 end end directorylist.sort (string_sorter) directorylist.resize_to_optimal_dimension filelist.wipe_out files := a_directory.filenames if files /= Void then from i := files.lower until i > files.upper loop filelist.put (files.item (i)) i := i + 1 end end filelist.sort (string_sorter) filelist.resize_to_optimal_dimension end feature -- Events button_clicked_event: EM_EVENT_CHANNEL [TUPLE []] -- Any button clicked event. feature {NONE} -- Implementation handle_ok_clicked is -- Handle ok button clicked. do was_ok_clicked := True filename := filebox.text if directory = Void then directory := current_directory end hide button_clicked_event.publish ([]) end handle_cancel_clicked is -- Handle cancel button clicked. do directory := Void filename := Void was_cancel_clicked := True hide button_clicked_event.publish ([]) end handle_file_selected (a_filename: STRING) is -- Handle selection of a file. do if a_filename /= Void then filebox.set_text (a_filename) directory := current_directory end end handle_directory_selected (a_directory: STRING) is -- Handle selection of a directory. do if a_directory /= Void then set_directory (File_system.pathname (current_directory, a_directory)) end end current_directory: STRING -- Current directory current_directory_label: EM_LABEL -- Label to display current directory filebox: EM_TEXTBOX -- File textbox directorylist: EM_TEXTLIST [STRING] -- Directory list filelist: EM_TEXTLIST [STRING] -- File list string_sorter: DS_SORTER [STRING] is -- String sorter once create {DS_BUBBLE_SORTER [STRING]}Result.make (create {ST_COLLATOR}) end end