How do I ask the user for a file name?

邮差的信 提交于 2019-12-22 07:24:11

问题


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



来源:https://stackoverflow.com/questions/32198476/how-do-i-ask-the-user-for-a-file-name

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!