• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

mvp - problems understanding passive view

 
Ranch Hand
Posts: 187
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have an application which i am currently working on which is like a ftp downloader. It gets information from an .xml file where an ftp server, user credentials etc. are defined and of course the filename. When a user clicks to load such a file, i send an event out "FileAdded".
I have a "mainview" which is a frame. This frame exists of 3 panels, so i made 3 mvp packages. One contains just 2 buttons "Start all downloads" and "stop all downloads". Another one contains a JTable (a specialized one with a AbstractTableModel modified for my needs) which represents the download state of each of those files, added as .xml.
Another one just shows general status informations (file added, current overall download speed etc.).

[DELETED]

The problem is now the downloadMvp. [DELETED]



Thats the part which looks so horribly wrong to me. I am pretty sure this is not the "best" way to achieve that. The problem is the downloadModel uses a "construct" i wrote to handle the download (more or less a simple ftp client) for each file. Inside this i send the downloadChangedEvent, and i guess thats already wrong.
Maybe someone can bring some light in this. I would love to have something like this in my presenters constructor:



Is it wrong to use the downloadModel to include business logic like i did? If not, i guess i have to completly rewrite the ftp engine which works behind the scenes...
 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can't pass a method reference like that, you have to leave out the parameter part. If you want to pass a method reference, you'll need to define a functional interface to match the method signature. Then declare your formal parameter to be an instance of the functional interface.

Declaring the parameter as a Consumer<File> should do it. You'll have to massage your code some more so it will work with what you already have.

And yes, that if-else with lots of instanceof is horrible.
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you go with my suggestion, you'll have to change your registerEvent method to take the class and the consumer. The class will be a key and the consumer the value in a Map entry. When you handle an event, just look up the event.getClass() in your map to find the appropriate consumer.

Caveat: this is just off the top of my head so there may some details that are missing or need adjustment. I hope it gives you a basic idea for a more streamlined approach though. You'll have as many different methods as you have cases in your current if-else with instanceof, basically. Then your if-else goes away and just becomes a bunch of calls to registerEvent(eventClass, consumerMethodReference)
 
olze oli
Ranch Hand
Posts: 187
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, in that case i will go with the map (class/consumer)
reply
    Bookmark Topic Watch Topic
  • New Topic