define_handler for JDialog

  11 posts   Feedicon  
Replies: 10 - Last Post: December 16, 2009 15:33
by: sundbp
showing 1 - 11 of 11
 
Posted: August 13, 2009 12:53 by sonorius
Hello,

I was trying the third option (nested controllers) of David's reply in

http://groups.google.com/group/monkeybars/browse_thread/thread/6f0c5e59110b696b

and it works, except that the handlers don't seem to be called.

No time to explain more details at the moment,
it's just that nothing happens when the Button is clicked.

Thanks for looking into it.

Sven
 
Posted: August 13, 2009 18:53 by Logan Barnett

If your handlers aren't set up, look at the name used in View.nest and in your Controller#addnestedcontroller. These names must match, and Monkeybars doesn't convert Symbols to Strings automatically.

 
Posted: August 13, 2009 21:18 by sonorius

Hello Logan,

well, I have tried to do everything according to the already mentioned Post of David.
I think the names do match and I even have verified, that PopupView#parent_component= gets called.

So, I'll just post the code of my little example application:

haupt_controller.rb:

require 'popup_controller'
class HauptController < ApplicationController
  set_model 'HauptModel'
  set_view 'HauptView'
  set_close_action :exit

  def start_dialog_button_action_performed 
    @dialog = PopupController.create_instance 
    add_nested_controller(:popup, @dialog) 
    @dialog.define_handler(:ok_button_action_performed)  do
      p :ok_button_handler_called
    end
    @dialog.define_handler(:window_close)  do
      p :window_close_handler_called
    end
    p :opening_dialog
    @dialog.open
    p :dialog_closed
  end
end



haupt_view.rb:

class HauptView < ApplicationView
  set_java_class 'haupt.HauptFrame'

  nest :sub_view => :popup, :using => [:define_popup_parent, nil]

  def define_popup_parent(view, component, model, transfer)
    view.parent_component = @main_view_component
  end
end



popup_controller.rb:

class PopupController < ApplicationController
  set_model 'PopupModel'
  set_view 'PopupView'
  #set_close_action :exit
end



popup_view.rb:

include_class 'popup.PopupDialog'

class PopupView < ApplicationView
  #set_java_class 

  def create_main_view_component  # This avoids error about missing @main_view_component
    42   
  end

  def parent_component=(parent)
    @main_view_component = PopupDialog.new(parent, true)
  end
end


And the symptom I get is that everything works normal, except that clicking on ok_button has no effect, and the :window_close handler does not fire.

Thank you for your help

Sven

 
Posted: August 14, 2009 14:29 by sonorius

And, for easier reading, this code as a pastie: http://pastie.org/584045

Sven

 
Posted: August 14, 2009 18:31 by Logan Barnett

Sven, :window_close is going to need some more work. See add_listener in the rdoc (it has a sample near the bottom): http://monkeybars.rubyforge.org/api/classes/Monkeybars/EventHandlerRegistrationAndDispatchMixin/ClassMethods.src/M000012.html

define_handler piggybacks the implicit handler registration that lets you make an ok_button_action_performed method that just works. However, we don't raise an error anytime a method is created that looks funny, or writing your controller would become a pain. This means we get silent failure.

Perhaps we can abuse blocks in a future version where you can set up implicit handlers, and raise anything that doesn't match.

class MyController < Monkeybars::Controller
  define_handlers do
    def ok_button_action_performed
      ...
    end

    # oh noes! error raised!
    def typo_button_performed_action
      ...
    end
  end
end

Back to your problem though. Can you create vanilla handlers in your nested controller and see if you get output for them? Then we can look at making them fancy and working with define_handler. Make sure your button name matches what your handler thinks it is (:

 
Posted: August 14, 2009 19:56 by sonorius

Hello Logan,

I have not yet examined everything I could or wanted to,
yet I writing already because I found an interesting thing.

Your suggestion to first try vanilla handlers proved to be very useful.
Because I got the stunning message

Monkeybars::UndefinedComponentError - There is no component named ok_button on view Java::Popup::PopupDialog


(( At last! An error message!! -- I love diagnostic messages... ))

At first I thought: Ouch, it was a typo after all!
And I checked, and re-checked... and did not find any typo.

I tried to reduce the example, removed the main component
and tested only the popup dialog -- the error remained.

And then, trying to make the example even more vanilla,
I activated set_java_class -- and ..... it worked!!

I only realised afterwards that I had forgotten to comment out
the create_main_view_component method, and this lead me
to another interesting insight:
both ways of providing a @main_view_component seem to cooperate!
Because if I take away the create_main_view_component method,
then then view cannot be created, because the constructor needs
two parameters. So, both are necessary, and I thought they were alternatives.

Anything you can say about these findings?

Regards

Sven

 
Posted: August 14, 2009 20:33 by sonorius

Hi again,

I think it helps imagine what I was talking about, if I post some code:

class PopupController < ApplicationController
  set_model 'PopupModel'
  set_view 'PopupView'
  #set_close_action :exit
  #add_listener :type => :action, :components => [:ok_button]
  def ok_button_action_performed 
    p :ok_button_handler_called
    update_model view_model, :data
    p model.data
    close
  end
end


class PopupView < ApplicationView
  set_java_class 'popup.PopupDialog' 

  def create_main_view_component
    PopupDialog.new(nil, true)
  end

  map :model => :data, :view => "textfield.text"
end


Sven

 
Posted: August 14, 2009 22:56 by Logan Barnett

Interesting find!

I'm not sure how we can reliably detect when you use both set_java_class AND override create_main_view_component without using some real black magic. When using set_java_class, the create_main_view_component does get used, but in the case of a pure Ruby or manual constructor invocation, you can override create_main_view_component.

I'm open to suggestions, but I'd hate to make this stuff order-dependent at the class level.

 
Posted: December 16, 2009 12:23 by sundbp

I'll put some info on this thread as well (followd up on another dialog thread yesterday).

I'm trying something similar to this thread:

dialog view:

java_import "trade_filter_dialog.TradeFilterDialog"

class TradeFilterDialogView < ApplicationView
  set_java_class 'trade_filter_dialog.TradeFilterDialog'

  def create_main_view_component
    42
  end

  def parent_component=(parent)
    p "set parent component"
    @main_view_component = TradeFilterDialog.new(parent, true)
    @main_view_component.location_relative_to = parent
  end

end

dialog controller:

class TradeFilterDialogController < ApplicationController
  set_model 'TradeFilterDialogModel'
  set_view 'TradeFilterDialogView'
  #set_close_action :close

  def find_trades_button_action_performed
    p "find trades clicked"
    view_model.do_find = true
    update_model view_model, :do_find
    close
  end

end

in main controller: def tradetablekey_pressed(event) if event.keycode == KeyEvent::VKF3 @dialog = TradeFilterDialogController.create_instance addnestedcontroller(:dialog, @dialog) status = @dialog.open viewmodel.randomizetrades updatemodel viewmodel, :trades removenestedcontroller(:dialog, @dialog) update_view end

end

main view:

nest :sub_view => :dialog, :using => [:define_dialog_parent, nil]

def define_dialog_parent(view, component, model, transfer)
  view.parent_component = @main_view_component

end

if I run this and try to activate the dialog (via the F3 key press) I receive: Java::JavaLang::IllegalArgumentException - java.lang.IllegalArgumentException: Can not set javax.swing.JButton field tradefilterdialog.TradeFilterDialog.findtradesbutton to java.lang.Long java.lang.IllegalArgumentException: Can not set javax.swing.JButton field tradefilterdialog.TradeFilterDialog.findtradesbutton to java.lang.Long

If i comment out the setjavaclass in dialog view it runs and pops up the window, but the button of the window doesn't fire. My guess is that's because the button handler gets tied to the "first" component created by createmainviewcomponent (i.e. no bindings get made) and when I then switch @mainview_component it doesn't retie the actions to the new component.

It's a shame the google groups archive was removed, seems there's some threads there I'd like to read. I'd really like to figure out how to do a dialog but it's proving to be pretty hard going..

 
Posted: December 16, 2009 13:38 by Logan Barnett

Hopefully this will seem much more clear when I create an example for us to refer to.

 
Posted: December 16, 2009 15:33 by sundbp
Hope so Smile

Playing with non-modal dialog in the meantime. Thanks.
showing 1 - 11 of 11
Replies: 10 - Last Post: December 16, 2009 15:33
by: sundbp
  • Mysql
  • Glassfish
  • Jruby
  • Rails
  • Nblogo
Terms of Use; Privacy Policy;
© 2010, Oracle Corporation and/or its affiliates
(revision 20120518.3c65429)
 
 
Close
loading
Please Confirm
Close