How do I ask the user for a file name?
Asked Answered
E

2

5

Searching for the call of a FileDialog

I would like to ask the user for a file name in Pharo 4.0

Through the spotter I found class

  FileDialogWindow

with a method

  answerFileName

Looking for the senders of #answerFileName I get to class

  UITheme

where it is called in the method

  chooseFileNameIn: aThemedMorph 
  title: title 
  extensions: exts 
  path: path preview: preview

And from there I come to class

 TEasilyThemed

with the method

 chooseFileName: title extensions: exts path: path preview: preview

From there finally I get to class

  WidgetExamples class >> exampleDialogs

And then I have the call

WidgetExamples exampleBuilder 
chooseFileName: 'Pick a file name' 
extensions: nil path: nil preview: nil.

However a print it of this expression does not give back a file name.

Question

What is the regular way of calling a file dialog?

Supplementary question after answers

Two classes are mentioned providing this service.

  • UIManager
  • UITheme

UIManager comment

UIManager is a dispatcher for various UI requests.

UITheme comment

Common superclass for User Interface themes. Provides methods for creating new morphs in a standard way, various "services" like a file dialog, message dialogs etc. and also methods for customising aspects of the appearance of various morphs. Though conceptually abstract, no code is "missing". Subclasses, therefore, should override the aspects they wish to change.

What is the difference between this two approaches?

Exon answered 25/8, 2015 at 7:55 Comment(0)
E
6

The easiest way is to use:

UIManager default chooseFileMatching: nil

You can specify patterns as:

UIManager default chooseFileMatching: #('*.jpg' '*.png')

You can also specify a label for the dialog:

UIManager default
    chooseFileMatching: #('*.jpg' '*.png')
    label: 'Please select and image to process'
Escuage answered 25/8, 2015 at 8:16 Comment(4)
how could I preselect a path?Fluid
@Fluid #chooseFileName:extensions:path:preview: where name is same as label and preview can be nil, but can also be #image or #textEscuage
thx thats working great. UIManager default chooseFileName: 'Please select a csv to process' extensions: #('csv') path: 'files/in' asPath preview: #text. only when I change folders how do I get the whole Path including the selected filenameFluid
Found the solution by directly opening the filestream using UITheme current fileOpenIn: World title: 'sometitle' extensions: #('csv') path: 'files/in' asPath preview: #text.Fluid
G
2

What I use in one of my demo applications is

MindmapNode class>>open
    |fileName|
    fileName := UITheme current chooseFileIn: World title: 'Choose file' extensions: nil path: nil preview:nil.
    fileName ifNotNil: [ (FLMaterializer materializationFromFileNamed: fileName)
        root openInWorld attachAllSubnodes detachAllSubnodes ]

MindmapNode>>saveMap
    |fileName|
    fileName := UITheme current fileSaveIn: World title: 'Choose file' extensions: nil path: nil.
    fileName ifNotNil: [ FLSerializer serialize: self toFileNamed: fileName].

The UIManager cares about being able to run headless, from a command line. Then you want to be able to provide a file name in either a parameter or an input file. In the standard in-image situation, UIManager default is a MorphicUIManager that delegates to the current theme.

So using the UIManager would probably be better

Gorey answered 25/8, 2015 at 8:32 Comment(1)
Thank you. Could you please add the link to your demo application as well? I have added a supplementary question. Depending on the answer I might put the 'accept flag' here later...Exon

© 2022 - 2024 — McMap. All rights reserved.