How do I ask the user for a file name?

2019-05-01 16:23发布

问题:

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?

回答1:

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'


回答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