在面向行为的设计中在类之间传递信息

时间:2016-04-30 22:17:19

标签: vala genie

我决定让我的小文本编辑器适应之前question中建议的行为设计模式。通过允许添加组合键来调用开头,保存操作等,行为设计很快就可以帮助扩展我的小项目。

但是,这个设计主要使用类,我不知道如何让类进行通信。添加了一个新类来处理保存当前文件(SaveFile),但是我无法捕获当前文件的URI(使用DocumentFileSelector类获取)。

我怀疑这样做的方法是通过类属性,但是我无法让document_selector变量继承该属性,所以我可以将它传递给SaveDocument类。

以下是代码的相关部分:

uses
    Gtk

init
    Intl.setlocale()
    Gtk.init (ref args)

    var document = new Text( "Lorem Ipsum" )

    var header = new Header ( "My text editor" )
    var body = new DocumentView( document )
    var editor = new EditorWindow (header,body )

    var document_selector = new DocumentFileSelector( editor )
    var load_new_content_command = new Load( document, document_selector )
    var create_new = new CreateNew( document )
    var save_file = new SaveFile( document )

    header.add_item( new OpenButton( load_new_content_command ) )
    header.add_item( new CreateNewButton ( create_new ) )
    header.add_item( new SaveFileButton( save_file ))

    editor.show_all ()
    Gtk.main ()

class EditorWindow:Window

    construct( header:Header, body:DocumentView )
        this.window_position = WindowPosition.CENTER
        this.set_default_size( 400, 400 )
        this.destroy.connect( Gtk.main_quit)
        this.set_titlebar(header)
        var box  = new Box (Gtk.Orientation.VERTICAL, 1)
        box.pack_start(body, true, true, 0)
        this.add(box)

class Header:HeaderBar
    construct( title:string = "" )
        this.show_close_button = true
        this.set_title( title )

    def add_item( item:Widget )
        this.pack_start( item )

class SaveFileButton:ToolButton
    construct( command:Command )
        this.icon_widget = new Image.from_icon_name(
                                                 "document-save",
                                                 IconSize.SMALL_TOOLBAR
                                                 )
        this.clicked.connect( command.execute )

class OpenButton:ToolButton
    construct( command:Command )
        this.icon_widget = new Image.from_icon_name(
                                                 "document-open",
                                                 IconSize.SMALL_TOOLBAR
                                                 )
        this.clicked.connect( command.execute )

class CreateNewButton:ToolButton
    construct( command:Command )
        this.icon_widget = new Image.from_icon_name(
                                                 "document-new",
                                                 IconSize.SMALL_TOOLBAR
                                                 )
        this.clicked.connect( command.execute )

class DocumentView:ScrolledWindow
    construct( document:TextBuffer )
        var view = new TextView.with_buffer( document )
        view.set_wrap_mode( Gtk.WrapMode.WORD )
        this.add( view )

interface Command:Object
    def abstract execute()

interface DocumentSelector:Object
    def abstract select():bool
    def abstract get_document():string

class Text:TextBuffer
    construct ( initial:string = "" )
        this.text = initial

class SaveFile:Object implements Command

    _receiver:TextBuffer

    construct( receiver:TextBuffer )
        _receiver = receiver

    def execute()
        start, end : Gtk.TextIter
        _receiver.get_start_iter(out start)
        _receiver.get_end_iter(out end)
        try
            FileUtils.set_contents (_filename, _receiver.get_text(start, end,
            false))
        except ex : FileError
            print "%s\n", ex.message


class DocumentFileSelector:Object implements DocumentSelector

    _parent:Window
    _uri:string = ""

    construct( parent:Window )
        _parent = parent

    def select():bool
        var dialog = new FileChooserDialog( "Open file",
                                            _parent,
                                            FileChooserAction.OPEN,
                                            dgettext( "gtk30", "_OK"),
                                            ResponseType.ACCEPT,
                                            dgettext( "gtk30", "_Cancel" ),
                                            ResponseType.CANCEL
                                           )

        selected:bool = false
        var response = dialog.run()
        case response
            when ResponseType.ACCEPT
                _uri = dialog.get_uri()
                selected = true

        dialog.destroy()
        return selected

    def get_document():string
        return "Reading the text from a URI is not implemented\n%s".printf(_uri)

class Load:Object implements Command

    _receiver:TextBuffer
    _document_selector:DocumentSelector

    construct( receiver:TextBuffer, document_selector:DocumentSelector )
        _receiver = receiver
        _document_selector = document_selector

    def execute()
        if _document_selector.select()
            _receiver.text = _document_selector.get_document()


class CreateNew:Object implements Command

    _receiver:TextBuffer

    construct( receiver:TextBuffer )
        _receiver = receiver

    def execute()
        var should_I_save=new MessageDialog (null, Gtk.DialogFlags.MODAL,
        Gtk.MessageType.INFO, Gtk.ButtonsType.YES_NO, "Hello world!")
        should_I_save.format_secondary_text (
        "This will delete the contets. Are you sure?")

        case should_I_save.run()
            when ResponseType.YES
                _receiver.set_text("")
                should_I_save.destroy ()
            when ResponseType.NO
                should_I_save.destroy ()

问题

  • 如何将URI信息从最后打开的文件传递到SaveDocument类?

    • 作为一个额外的问题,每个类构造后的第一行是做什么的?这些行如下:

      构造(父:窗口)

          _parent = parent
      

修改

仍然无法解决问题,我最近尝试的是在DocumentFileSelector类中创建另一个名为whichFile()的方法。这种方法只会返回uri。我在执行时遇到错误:FileUtils.set_contents ( DocumentFileSelector.whichFile(), _receiver.get_text(start, end,false))

以下是对代码的修改:

class SaveFile:Object implements Command

    _receiver:TextBuffer

    construct( receiver:TextBuffer )
        _receiver = receiver

    def execute()
        start, end : Gtk.TextIter
        _receiver.get_start_iter(out start)
        _receiver.get_end_iter(out end)
        try
            FileUtils.set_contents ( DocumentFileSelector.whichFile(), _receiver.get_text(start, end,false))
        except ex : FileError
            print "%s\n", ex.message


class DocumentFileSelector:Object implements DocumentSelector

    _parent:Window
    _uri:string = ""

    construct( parent:Window )
        _parent = parent

    def select():bool
        var dialog = new FileChooserDialog( "Open file",
                                            _parent,
                                            FileChooserAction.OPEN,
                                            dgettext( "gtk30", "_OK"),
                                            ResponseType.ACCEPT,
                                            dgettext( "gtk30", "_Cancel" ),
                                            ResponseType.CANCEL
                                           )

        selected:bool = false
        var response = dialog.run()
        case response
            when ResponseType.ACCEPT
                _uri = dialog.get_uri()
                selected = true

        dialog.destroy()
        return selected

    def whichFile():string
        return _uri

    def get_document():string
        return "Reading the text from a URI is not implemented\n%s".printf(_uri)

1 个答案:

答案 0 :(得分:1)

你快到了......

只需在wmic命令中添加对DocumentSelector的引用,就像对SaveFile命令所做的那样:

LoadFile

然后,您可以在已保存的class SaveFile:Object implements Command _receiver:TextBuffer _document_selector:DocumentSelector construct( receiver:TextBuffer, document_selector:DocumentSelector) _receiver = receiver _document_selector = document_selector 上调用新的whichFile()方法:

DocumentSelector