Model-View-Presenter - help

Hi,
I recently read about Model-View-Presenter on Martin Fowler's website, and elsewhere. I think I understand the idea...
The view just displays the GUI components..events fired from the View are delegated to a Presenter. The presenter then deals with the model and updates the View accordingly. Sound about right?
Ok, so I learn best by example..and I haven't found any code samples yet, so I thought I'd give it a shot. Below is my code...
Model
* the model to be displayed in a View
public class Album {
     private boolean isClassical;
     private String composer;
     public String getComposer() {
          return composer;
     public void setComposer(String composer) {
          this.composer = composer;
     public boolean isClassical() {
          return isClassical;
     public void setClassical(boolean isClassical) {
          this.isClassical = isClassical;
View - interface
import java.awt.event.ActionListener;
* An interface which defines the methods needed by a presenter
public interface View {
     public boolean isClassical();
     public void setClassical(boolean b);
     public boolean isComposerEnabled();
     public void setComposerEnabled(boolean b);
     public void addClassicalChangeListener(ActionListener al);
     public void showView();
View - implementation
import java.awt.Dimension;
import java.awt.event.ActionListener;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
* A an implementation of the View interface.  It shows a checkbox
* and a textfield.
public class MyView extends JFrame implements View {
     private JCheckBox checkboxClassical;
     private JTextField textfieldComposer;
     private MyPresenter mp;
     public MyView() {
          checkboxClassical = new JCheckBox("Classical");
          textfieldComposer = new JTextField();
          textfieldComposer.setPreferredSize(new Dimension(100, 20));
          textfieldComposer.setEnabled(false);
          JPanel p = new JPanel();
          p.add(checkboxClassical);
          p.add(textfieldComposer);
          add(p);
          setSize(600, 400);
     public void addClassicalChangeListener(ActionListener al) {
          checkboxClassical.addActionListener(al);
     public void setPresenter(MyPresenter mp) {
          this.mp = mp;
     public void setClassical(boolean b) {
          checkboxClassical.setSelected(b);
     public boolean isClassical() {
          return checkboxClassical.isSelected();
     public void setComposerEnabled(boolean b) {
          textfieldComposer.setEnabled(b);
     public boolean isComposerEnabled() {
          return textfieldComposer.isEnabled();
     public void showView() {
          setVisible(true);
Presenter
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
* A presenter for a View.  When the "classical" fires
* an ActionEvent, the "composer" component is enabled or disabled.
public class MyPresenter {
     private Album album;
     private View view;
     public MyPresenter(Album album, View viewVal) {
          this.album = album;
          this.view = viewVal;
          view.addClassicalChangeListener(new ActionListener() {
               public void actionPerformed(ActionEvent e) {
                    view.setClassical(view.isClassical());
                    view.setComposerEnabled(view.isClassical());
          view.showView();
}and here is a simple Main to tie it togehter.
* Ties together the M-V-P
public class Main {
     public static void main(String[] args) {
          Album al = new Album();
          al.setClassical(false);
          MyView myView = new MyView();
          MyPresenter mp = new MyPresenter(al, myView);
          myView.setPresenter(mp);
}Ok, the idea is this. The GUI shows a checkbox and a textfield. basically, if the checkbox is selected the textfield is enabled and can be typed into. If the checkbox is not selected, the textfield is disabled. From the model point of view, if isClassical is false, then no composer can be defined.
So, is my code following the pattern? Does anyone have any simple code to contribute as an example? For some reason is just seems weird to me that I would have an interface define a bunch of methods that the Presenter can call. And I have to create the Model and View which get passed to the presenter, and then I have to set the presenter on the view....it just seems weird to me.
Any comments, suggestions, examples??
Thanks for your time and contributions.

The other information didnt answer my questions. The response was mixing the idea of MVC instead of MVP, and then the response discussed what they thought was wrong with MVP and why they use a different model. I am looking for help on understanding the pattern more, also for someoen (who knows about MVP) to look at my sample code and help me understand what I did right/wrong.
Thanks.

Similar Messages

  • Model view presenter

    Hi,
    Does anyone have any examples of the use of the model view presenter design pattern within Swing. I am attempting to refactor a number of Swing classes based around JDialogs, JTables etc. Your help would be greatly appreciated.
    Many thanks,
    dude

    Thanks I have this example, I would like to see some other swing examples if anyone has them...

  • Model View Control help!

    Hi!
    I have a var in Model called "age". From ActionPerformed in Control I want to take the value from a JTextField in View and update age. How do I do this?
    ActionPerformed is triggered by a JButton..
    Thx!

    With MVC you should create the model, view and controller in a driver class that creates all the compoents of the MVC and merges them together. You should be passing the model as a parm to the view in the constructor.
    Example Driver Class:
    public MVCDriverClass {
    // constructor and any other methods needed
    public void initialize() {
        MyModelInterface model = new MyModel();
        MyViewInterface view = new MyView();
        MyControllerInterface controller = new MyController(mode, view);
        model.initialize();
        view.initialize();
        view.showView();
    }Now in your model you need to make sure you are using the Observer pattern to notify all observers of any changes to the data model. Java already implements this for you if you want to use its utils. You simply need to extend the Observable class on your model to make it the subject and implement the Observer interface on each view you want the Subject (Observer) to notify of changes. You will see that the Observer interface has a method named update(Observer o, Object arg). You simply place any code in here that you need to update your UI values with. Let me know if you need any examples.

  • I have 2 screens at work and 1 screen at home. I was working on a power point presentation on 2 monitors. My window is now "stuck" when I Power Point. I cannot drag it back to a display or find any way to move it back into my view. Help!!!

    I have 2 screens at work and 1 screen at home. I was working on a power point presentation on 2 monitors with the presentation on monitor externally hooked up to my computer. My window is now "stuck" when I open Power Point. I cannot drag it back to a display or find any way to move it back into my view. Help!!!

    Here is what worked for me:
      My usb hub, being usb2, was too fast. I moved the wire to a usb port directory on my pc. That is a usb1 port which is slow enough to run your snyc.

  • Presenter Help | Publishing and viewing presentations

    This question was posted in response to the following article: http://helpx.adobe.com/presenter/using/publishing-viewing-presentations.html

    Can you just make a .swf file from your presentation?

  • Mapping Variables(Object View) to Measures(Model View)

    Hi,
    In my AW, I have created a cube, And then I have created a new variable in the object view and populated it using load programs. Now. when I try to create a measure in the model view of the same cube based on this variable, I cannot see any option for assigning a variable to a measure. Can someone please help me out in doing this?
    I need the measure in the model view, because it looks like, when I create a relational view, only the measures that are defined in the model are added to the view and not the variables available in the object model.
    Thanks,
    Bharat

    Hi Bharat,
    The mapping editor is used to map a measure to a relational data source. Therefore, unless you convert your data source used by your DML program to a relational table/view (if it is a text file you can use an external table to present the data to AWM as a relational table, or if it is an Excel spreadsheet and your database is on Windows you can use the heterogenous gateway service to load directly from Excel by exposing the Excel worksheet as a relational table) you will not be able to use the mapping editor.
    To assign an OLAP variable to an measure within a cube you will need to create a custom calculated measure. This will allow you to create a measure that "maps" to your OLAP DML program. There is an Excel utility on the OLAP OTN home page that allows you to create custom calculated measures (at the moment this feature is not AWM10gR2 - but is available within AWM 11g). Click here for more information:
    http://download.oracle.com/otn/java/olap/SpreadsheetCalcs_10203.zip
    My advice would be to try and convert your data source to a relational table/view and using the mapping editor. This will allow you to benefit from the many new features within OLAP cube storage such as compressed composites, data compression, paralled processing, partitioning and aggregation maps. Of course you can code all this manually but it is much easier to let AWM do all this for you via a few mouse clicks. Please note - that everything you build outside of the AWM model view has to be managed (defined, updated, backed up etc) by you rather AWM. So while there are lots of advantages of using OLAP DML, I personally always try to load data into my OLAP cubes using AWM mappings and save OLAP DML for providing the advanced types of analysis that make OLAP such an excellent calculation engine.
    Hope this helps
    Keith Laker
    Oracle Data Warehouse Product Management
    OLAP Blog: http://oracleOLAP.blogspot.com/
    OLAP Wiki: http://wiki.oracle.com/page/Oracle+OLAP+Option
    DM Blog: http://oracledmt.blogspot.com/
    OWB Blog : http://blogs.oracle.com/warehousebuilder/
    OWB Wiki : http://wiki.oracle.com/page/Oracle+Warehouse+Builder
    DW on OTN : http://www.oracle.com/technology/products/bi/db/11g/index.html

  • Unable to Distribute Model View in BD64

    Hi Gurus,
    Model View - SR2CLNT251
    Receiver of Model View - EA1CLNT251
    i went to txcode bd64 > i clicked on model view SR2CLNT251 then went to edit > model view > distribute and clicking the logical system of EA1CLNT251. then error will be encountered.
    Model view SR2CLNT251 has not been updated
    Reason: No authorization to change model view SR2CLNT251
    .....authorization object B_ALE_MODL
    .....Parameter: activity 02, model view SR2CLNT251
    i already given correct authorization to my ID, but still the error persists.
    Please help. Thanks!
    Regards,
    Tony

    Hi Tony,
    You need  authorization for authorization object  'B_ALE_MODL'  in system I guess either in system SR2CLNT251  or in EA1CLNT251.
    Best Regards,
    Tushar

  • Java Script Error while deploying a Model with Value Help

    Hi,
    I am using EP 7.0 SP 10.
    I am trying to deploy a model which includes the Value Help for an Input field, and i am trying to deploy this model.
    The model compiles successfully, but gives a Java Script Error while deploying the model,
    ! Error on Page
    When Click on this java script error, it shows that ,
    Line:14985
    Char 1: Error
    object does n't support this property or method.
    code
    URL: <serverhost>/VCRes/WebContent/VisualComposer6.0/bin/223334.htm?24102006.1712.
    The Same model works in dev server, and it fails in the production server.
    Thanks and Regards,
    Sekar

    Hi jakob,
    Thankyou for your quick response.
    I did a basic model with the help of a documentation which i got from this forums.I created a iView and from there i used Bapi "BAPI_SALESORDER ".
    I created a Input Form and a outpot form (table view).I tested model and am able to get the output.but when i try to deploy it is giving me the error.
    And i think am not paring any formulas here.
    Please guide me.
    thanks and regrads
    Pradeep.B

  • Tcode  PFAL there is no model view for the distribution of HR Data.

    Hi friends ,
    i am facing new problem,i want to send iDOC to other system.
    previeous its working fine.
    but to day  when i open tcode PFAL.
    it give me Error:"There is no model view for the distribution of HR master data"
    what is the solution for that.
    i want to send HR Data.
    its urgent please give me any solution ithat will help me alot.
    thanks in advance

    There is already Distributed Model Created.
    can u give me annother tcode so i can  send Metarial data, by IDOC.
    please help me.
    Thanks

  • IDOC Model View Generation Issue

    Hi all,
    I am working on IDOCS and I am facing the follwing error when I try to distribute the Model View in IDOC--
    "the following ALE connection already exists in model view HR_TO_PI"
    I had deleted the above "HR_TO_PI" model view and I created a new one with new name.
    But I still get the same message.
    I don't think this is an issue with configuration.
    I am assuming it's an issue with table entries. Does this-"HR_TO_PI" value get stored in any table in the back end ?
    Kindly help. I had a look at SDN and other places but they all ask to check for partner profiles and logical systems which I believe I have configured right.
    Warm regards,
    Hari Kiran
    Edited by: HARI KIRAN REDDY on Feb 23, 2012 11:24 AM

    Hi Sravan,
    I had a look at TBD00. It shows all my current distribution models but not the one mentioned in the error.
    I had a look at some other tables in the TBD series but of no help.
    I don't know in which table  that entry has ended up.
    It's frustrating.
    Thanks for the input once again.
    Warmr egards,
    Hari Kiran

  • SQL Developer Data Modeler Viewer - domain datatypes

    I'm using the SQL Developer Data Modeler Viewer (that comes free with SQL Developer) to view a model that was created in Oracle SQL Developer Data Modeler (the chargeable product). The colums have all been defined using domain datatypes. I have an XML file defining the domains but I cannot see where in the viewer you can import a domian file into or where in the model's directory structure the file can be placed to be read by the viewer. Without this the columns are all showing as datatype 'Unknown'.
    Any help much appreciated, thanks.

    two set of domains can be maintained in Data Modeler:
    - per installation - they are in file defaultdomains.xml in "datamodeler\domains" directory
    - per design - they are in "domains" directory under your design directory -
    "Per installation" domains are used in your case - "per design" domains are "attached" to design - when you send the design to someone else, they are in design structure.
    Your solution - to get defaultdomains.xml file from installation where design is created and to put it in your "datamodeler\domains" directory.
    Philip

  • Model view distribution

    Hi ,
    I have to synchronise SAP system A and SAP System B through ALE  .If any change in the material in System A has be reflected in system B and vice versa.in this case do I need to create two model views. can I create one model view and distribute it.If i distribute will the partner profiles will be created automatically .why we distubute model view.Please reply.
    Thanks,
    Ravi

    Hi,
    You have to create only one model view..
    create it,then goto environment menu in SALE and create partner profile
    port is created by itseld.
    then distribute it to you other system.
    we distribute model view to transfer the information to ither system that which is the sending system receiving system and what data you are sending.
    Regards,
    pankaj singh
    reward points if helpful.

  • BD64-Log of Model View Error

    Hi,
    In  BD64 I have defined the model and also successfully created the partner profile.But after that when I am trying to do Edit/ModelView/Distribute I am getting following error.Can you please help me why am I getting this error?
    Target system TESTECF800                  Model view MATERIAL has not been updated
                                                             Reason: No authorization to create model view MATERIAL
                                                          ..... authorization object B_ALE_MODL
                                                    ..... Parameter: activity 01 , model view MATERIAL
    Regards,
    Bedayan.

    Hi Bedayan,
    As per the error your current profile is not having proper authorization to maintain model view. I think your basis people have revised the authorization objects.
    I donot have the authorization to distribute the model view to the target client
    Reddy

  • BD64 - Distribute Model View error?

    Hi, Expert:
    when I distribute model view using BD64. the following
    error message comes:
    "Target system is an external System. It does not support this function."
    what's the problem? thanks

    hi
    good
    go through this link, which ll give you brief idea about the model view -BD64, i hope this ll help you to solve your problem.
    http://www.supinfo-projects.com/cn/2005/idocs_en/2/
    thanks
    mrutyun^

  • Real Model View Controller with JTextField

    Hi!
    I am new to Java (so please bear with me). I am building a swing application. I already have added a JTable with a corresponding table model that extends AbstractTableModel. Rather than store the data in the table model, I have modified setValueAt and getValueAt to write and read cell data to another location. So far, everything is fine. When doing setValueAt, I have a fireTableCellUpdated statement that I use to update the edited cell. So far, things are all still fine.
    I would like to do the same thing with a JTextField. I found an example in Core Java Volume 1 for create a class that extends PlainDocument. It uses insertString to update the document in a way that ensures that only numbers are entered. I implemented this. Everything is still fine. I changed insertString to update my remote repository (a field in another class). Everything is still fine. Next, I tried to change (override) both getText methods to read from the repository. This works, but is not reflected on the screen.
    I realize that I need the equivalent of a fireTableCellUpdated statement for the class that extends PlainDocument, but do not know how to do this.
    I have looked a lot over the internet for the model view controller implementation. I know that it can be done using event and event listeners, but this seems to defeat the purpose of the model view controller - it seems like you ought to be able to directly modify the model object to access external data.
    My code is below.
    Thanks/Phil Troy
    * PlainDocument class to make it possible to:
    * - Make sure input in unsigned integer
    * - Automatically save data to appropriate locate
    * This will hopefully eventually work by overriding the insertString and getText methods
    * and creating methods that can be overridden to get and save the numerical value
    class UnsignedIntegerDocument extends PlainDocument
         TutorSchedulerPlusSettings settings;
         JTextField textField;
         public UnsignedIntegerDocument(TutorSchedulerPlusSettings settings)
         {     super();
              this.settings = settings;
         public void setTextField(JTextField textField)
         {     this.textField = textField;
         // Overridden method
         public void insertString(int offs, String str, AttributeSet a) throws BadLocationException
         {     if (str == null) return;
              String oldString = getText(0, getLength());
              String newString = oldString.substring(0, offs) + str +oldString.substring(offs);
              try
              {     setValue(Integer.parseInt(newString));
                   super.insertString(offs, str, a);
                   fireInsertUpdate(new DefaultDocumentEvent(offs, 10, DocumentEvent.EventType.INSERT));
              catch(NumberFormatException e)
         public String getText()
         {     return String.valueOf(getValue());
         public String getText(int offset, int length) throws BadLocationException
         {     String s = String.valueOf(getValue());
              if (length > 0)
                   s = s.substring(offset, length);
              return s;
         public void getText(int offset, int length, Segment txt) throws BadLocationException
         {     //super.getText(offset, length, txt);
              char[] c = new char[10];
              String s = String.valueOf(getValue());
              s.getChars(offset, length, c, 0);
              txt = new Segment(c, 0, length);
         public String getValue()
         {     int i = settings.maxStudents;
              String s = String.valueOf(i);
              return s;          
         void setValue(int i)
         {     settings.maxStudents = i;
    }

    Hi!
    Thanks for your response. Unfortunately, based on your response, I guess that I must not have clearly communicated what I am trying to do.
    I am using both JTables and JTextFields, and would like to use them both in the same way.
    When using JTable, I extend an AbstractTableModel so that it refers to another data source (in a separate class), rather than one inside of the AbstractTableModel. Thus the getValueAt method, getColumnCount method, setValueAt method, . .. all call methods in another class. The details of that other class are irrelevant, but they could be accessing data from a database (via JDBC) or from other machines via some other communication mechanism.
    I would like to do exactly the same thing with a JTextField. I wish for the data to come from a class other than an object of type PlainDocument, or of any class that implements the Document interface. Instead, I would like to use a class that implements the Document interface to call my external class using methods similar to those found in AbstractTableModel.
    You may ask why I would like to to this. I have specific reasons here, but more generally this would be helpful when saving or retrieving parameters set and displayed in a JTextField to a database, or when sharing JTextField to multiple users located on different machines.
    As to whether this is real MVC or not, I think it is but it really doesn't matter.
    I know that I can accomplish what I want for the JTextField using listeners. However, I would like my code for the JTables to be similarly structured to that of the JTextField.
    Thanks/Phil Troy

Maybe you are looking for