#StackBounty: #java #reflection #dependencies How to determine all runtime (but not static) dependencies of Java source by reading the …

Bounty: 50

Let’s say I wanted to assert that a given Java source folder contained all of the source code required to run any of the programs contained in the source folder. Simply showing that the whole source folder compiles would be insufficient because perhaps some of those programs use reflection to instantiate objects. As such I could search through all the code and look for invocations of newInstance() to learn what classes are expected to be present at runtime. But what about calls to Class.forName(...) that are not involved in calls to newInstance() ? Better check for those as well. But how many such things do I need to check for?

Is there any sort of exhaustive list I could consult to ensure that I am considering each way in Java that such a runtime dependency could be introduced? Restated, does there exist some list of operations such that if I can show none of the source in a source folder use those operations (and that folder compiles) that all (code) dependencies are present?

If no such list exists, could we start one in this thread and make a best effort to cover all operations that we know of?

Edit

I’d like to narrow the question a little. What I’m most interested in showing is that if a codebase compiles, all dependencies are present. It seems to me to do this I would need to first compile the codebase and then check to see if any of the code ever calls certain methods (e.g. newInstance) that could introduce a runtime dependency. If no such methods are found, I’m reasonably sure that all required code is present in source form and running the program will not generate a ClassNotFoundException.


Get this bounty!!!

#StackBounty: #java #design-patterns #mvc Is this a correct implementation of Trygve Reenskaug 's MVC specification?

Bounty: 100

So I was reading the MODELS – VIEWS – CONTROLLERS paper by Trygve Reenskaug from December 1979 and wanted to try an implementation with Java Swing from my understandings..

Notes I have taken are as follows:

A view is attached to its model (or model part) and gets the data
necessary for the presentation from the model by asking questions. It
may also update the model by sending appropriate messages. All these
questions and messages have to be in the terminology of the model, the
view will therefore have to know the semantics of the attributes of
the model it represents.

A view should never know about user input, such as mouse operations
and keystrokes. It should always be possible to write a method in a
controller that sends messages to views which exactly reproduce any
sequence of user commands.

A Controller provides means for user input by presenting the user with
menus or other means of giving commands and data. The controller
receives such user input, translates it into the appropriate messages
and pass these messages onto one or more of the views.

The application I worked on is a very simple Note Taking application. This is what the application itself looks like:

enter image description here
enter image description here
enter image description here

Moving on the code, start with Directory Layout..enter image description here

My Model is very simple and knows nothing about Views or Controllers..

package biz.tugay.swingynotes.model;

/* User: koray@tugay.biz Date: 2017/05/12 */
public class SwingyNote {

    private final String note;

    public SwingyNote(final String note) {
        this.note = note;
    }

    public String getNote() {
        return note;
    }
}

package biz.tugay.swingynotes.model;

import java.util.*;

/* User: koray@tugay.biz Date: 2017/05/12 */
public final class SwingyNoteService {

    private final Set<SwingyNote> swingyNotes = new HashSet<SwingyNote>();

    // Add some sample data..
    {
        final SwingyNote sampleNote = new SwingyNote("Sample Note!");
        addNote(sampleNote);

        final SwingyNote helloWorld = new SwingyNote("Hello World!");
        addNote(helloWorld);
    }

    public void addNote(final SwingyNote swingyNote) {
        swingyNotes.add(swingyNote);
    }

    public Collection<SwingyNote> getAllNotes() {
        return swingyNotes;
    }
}

Moving on to View..

package biz.tugay.swingynotes.view;

import javax.swing.*;
import java.awt.*;

/* User: koray@tugay.biz Date: 2017/05/12 */
public class SwingyNotesMainFrame extends JFrame {

    public SwingyNotesMainFrame(final AllNotesPanel allNotesPanel, final AddNewNotePanel addNewNotePanel) {
        super("Swingy Notes");

        // Set Layout..
        final BorderLayout borderLayout = new BorderLayout();
        final Container contentPane = getContentPane();
        contentPane.setLayout(borderLayout);

        // Add panels..
        contentPane.add(allNotesPanel, BorderLayout.CENTER);
        contentPane.add(addNewNotePanel, BorderLayout.SOUTH);

        // Adjust properties and make the frame visible..
        setSize(600, 200);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setVisible(true);
    }
}

package biz.tugay.swingynotes.view;

import biz.tugay.swingynotes.model.SwingyNote;
import biz.tugay.swingynotes.model.SwingyNoteService;

import javax.swing.*;
import java.awt.*;
import java.util.Collection;

/* User: koray@tugay.biz Date: 2017/05/12 */
public class AllNotesPanel extends JPanel {

    private final SwingyNoteService swingyNoteService;

    public AllNotesPanel(final SwingyNoteService swingyNoteService) {
        this.swingyNoteService = swingyNoteService;
        displayAllNotes();
    }

    public void displayAllNotes() {
        removeAll();
        final Collection<SwingyNote> allNotes = swingyNoteService.getAllNotes();
        for (final SwingyNote swingyNote : allNotes) {
            final JLabel swingyNoteLabel = buildLabelForSwingyNote(swingyNote);
            add(swingyNoteLabel);
        }
        validate();
        repaint();
    }

    private JLabel buildLabelForSwingyNote(final SwingyNote swingyNote) {
        final JLabel swingyNoteLabel = new JLabel(swingyNote.getNote());
        swingyNoteLabel.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2));
        swingyNoteLabel.setFont(new Font("Dialog", Font.BOLD, 27));
        swingyNoteLabel.setOpaque(true);
        swingyNoteLabel.setBackground(Color.YELLOW);
        return swingyNoteLabel;
    }
}

package biz.tugay.swingynotes.view;

import biz.tugay.swingynotes.model.SwingyNote;
import biz.tugay.swingynotes.model.SwingyNoteService;

import javax.swing.*;

/* User: koray@tugay.biz Date: 2017/05/12 */
public class AddNewNotePanel extends JPanel {

    private final SwingyNoteService swingyNoteService;
    private final JTextField newNoteInputTextField;

    public AddNewNotePanel(final SwingyNoteService swingyNoteService) {
        this.swingyNoteService = swingyNoteService;
        this.newNoteInputTextField = new JTextField(20);

        add(newNoteInputTextField);
    }

    public void addNote() {
        final SwingyNote swingyNote = new SwingyNote(newNoteInputTextField.getText());
        swingyNoteService.addNote(swingyNote);
        newNoteInputTextField.setText(null);
    }
}

and here is my Controller

package biz.tugay.swingynotes.controller;

import biz.tugay.swingynotes.view.AddNewNotePanel;
import biz.tugay.swingynotes.view.AllNotesPanel;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/* User: koray@tugay.biz Date: 2017/05/12 */
public class AddNewNoteButton extends JButton implements ActionListener {

    private final AddNewNotePanel addNewNotePanel;
    private final AllNotesPanel allNotesPanel;

    public AddNewNoteButton(final AddNewNotePanel addNewNotePanel, final AllNotesPanel allNotesPanel) {
        super("Add New Note!");
        addActionListener(this);

        this.addNewNotePanel = addNewNotePanel;
        this.allNotesPanel = allNotesPanel;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        addNewNotePanel.addNote();
        allNotesPanel.displayAllNotes();
    }
}

and this is how I start my Application:

package biz.tugay.swingynotes;

import biz.tugay.swingynotes.controller.AddNewNoteButton;
import biz.tugay.swingynotes.model.SwingyNoteService;
import biz.tugay.swingynotes.view.AllNotesPanel;
import biz.tugay.swingynotes.view.AddNewNotePanel;
import biz.tugay.swingynotes.view.SwingyNotesMainFrame;

import javax.swing.*;

/* User: koray@tugay.biz Date: 2017/05/12 */
public class App {

    public static void main(String[] args) {

        // Model..
        final SwingyNoteService swingyNoteService = new SwingyNoteService();

        // View..
        final AllNotesPanel allNotesPanel = new AllNotesPanel(swingyNoteService);
        final AddNewNotePanel addNewNotePanel = new AddNewNotePanel(swingyNoteService);

        // Controller..
        final AddNewNoteButton addNewNoteButton = new AddNewNoteButton(addNewNotePanel, allNotesPanel);
        addNewNotePanel.add(addNewNoteButton);

        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new SwingyNotesMainFrame(allNotesPanel, addNewNotePanel);
            }
        });
    }
}

Do all components seem to be correct?


Get this bounty!!!

#StackBounty: #java #design-patterns #mvc Is this a correct implementation of Trygve Reenskaug 's MVC specification?

Bounty: 100

So I was reading the MODELS – VIEWS – CONTROLLERS paper by Trygve Reenskaug from December 1979 and wanted to try an implementation with Java Swing from my understandings..

Notes I have taken are as follows:

A view is attached to its model (or model part) and gets the data
necessary for the presentation from the model by asking questions. It
may also update the model by sending appropriate messages. All these
questions and messages have to be in the terminology of the model, the
view will therefore have to know the semantics of the attributes of
the model it represents.

A view should never know about user input, such as mouse operations
and keystrokes. It should always be possible to write a method in a
controller that sends messages to views which exactly reproduce any
sequence of user commands.

A Controller provides means for user input by presenting the user with
menus or other means of giving commands and data. The controller
receives such user input, translates it into the appropriate messages
and pass these messages onto one or more of the views.

The application I worked on is a very simple Note Taking application. This is what the application itself looks like:

enter image description here
enter image description here
enter image description here

Moving on the code, start with Directory Layout..enter image description here

My Model is very simple and knows nothing about Views or Controllers..

package biz.tugay.swingynotes.model;

/* User: koray@tugay.biz Date: 2017/05/12 */
public class SwingyNote {

    private final String note;

    public SwingyNote(final String note) {
        this.note = note;
    }

    public String getNote() {
        return note;
    }
}

package biz.tugay.swingynotes.model;

import java.util.*;

/* User: koray@tugay.biz Date: 2017/05/12 */
public final class SwingyNoteService {

    private final Set<SwingyNote> swingyNotes = new HashSet<SwingyNote>();

    // Add some sample data..
    {
        final SwingyNote sampleNote = new SwingyNote("Sample Note!");
        addNote(sampleNote);

        final SwingyNote helloWorld = new SwingyNote("Hello World!");
        addNote(helloWorld);
    }

    public void addNote(final SwingyNote swingyNote) {
        swingyNotes.add(swingyNote);
    }

    public Collection<SwingyNote> getAllNotes() {
        return swingyNotes;
    }
}

Moving on to View..

package biz.tugay.swingynotes.view;

import javax.swing.*;
import java.awt.*;

/* User: koray@tugay.biz Date: 2017/05/12 */
public class SwingyNotesMainFrame extends JFrame {

    public SwingyNotesMainFrame(final AllNotesPanel allNotesPanel, final AddNewNotePanel addNewNotePanel) {
        super("Swingy Notes");

        // Set Layout..
        final BorderLayout borderLayout = new BorderLayout();
        final Container contentPane = getContentPane();
        contentPane.setLayout(borderLayout);

        // Add panels..
        contentPane.add(allNotesPanel, BorderLayout.CENTER);
        contentPane.add(addNewNotePanel, BorderLayout.SOUTH);

        // Adjust properties and make the frame visible..
        setSize(600, 200);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setVisible(true);
    }
}

package biz.tugay.swingynotes.view;

import biz.tugay.swingynotes.model.SwingyNote;
import biz.tugay.swingynotes.model.SwingyNoteService;

import javax.swing.*;
import java.awt.*;
import java.util.Collection;

/* User: koray@tugay.biz Date: 2017/05/12 */
public class AllNotesPanel extends JPanel {

    private final SwingyNoteService swingyNoteService;

    public AllNotesPanel(final SwingyNoteService swingyNoteService) {
        this.swingyNoteService = swingyNoteService;
        displayAllNotes();
    }

    public void displayAllNotes() {
        removeAll();
        final Collection<SwingyNote> allNotes = swingyNoteService.getAllNotes();
        for (final SwingyNote swingyNote : allNotes) {
            final JLabel swingyNoteLabel = buildLabelForSwingyNote(swingyNote);
            add(swingyNoteLabel);
        }
        validate();
        repaint();
    }

    private JLabel buildLabelForSwingyNote(final SwingyNote swingyNote) {
        final JLabel swingyNoteLabel = new JLabel(swingyNote.getNote());
        swingyNoteLabel.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2));
        swingyNoteLabel.setFont(new Font("Dialog", Font.BOLD, 27));
        swingyNoteLabel.setOpaque(true);
        swingyNoteLabel.setBackground(Color.YELLOW);
        return swingyNoteLabel;
    }
}

package biz.tugay.swingynotes.view;

import biz.tugay.swingynotes.model.SwingyNote;
import biz.tugay.swingynotes.model.SwingyNoteService;

import javax.swing.*;

/* User: koray@tugay.biz Date: 2017/05/12 */
public class AddNewNotePanel extends JPanel {

    private final SwingyNoteService swingyNoteService;
    private final JTextField newNoteInputTextField;

    public AddNewNotePanel(final SwingyNoteService swingyNoteService) {
        this.swingyNoteService = swingyNoteService;
        this.newNoteInputTextField = new JTextField(20);

        add(newNoteInputTextField);
    }

    public void addNote() {
        final SwingyNote swingyNote = new SwingyNote(newNoteInputTextField.getText());
        swingyNoteService.addNote(swingyNote);
        newNoteInputTextField.setText(null);
    }
}

and here is my Controller

package biz.tugay.swingynotes.controller;

import biz.tugay.swingynotes.view.AddNewNotePanel;
import biz.tugay.swingynotes.view.AllNotesPanel;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/* User: koray@tugay.biz Date: 2017/05/12 */
public class AddNewNoteButton extends JButton implements ActionListener {

    private final AddNewNotePanel addNewNotePanel;
    private final AllNotesPanel allNotesPanel;

    public AddNewNoteButton(final AddNewNotePanel addNewNotePanel, final AllNotesPanel allNotesPanel) {
        super("Add New Note!");
        addActionListener(this);

        this.addNewNotePanel = addNewNotePanel;
        this.allNotesPanel = allNotesPanel;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        addNewNotePanel.addNote();
        allNotesPanel.displayAllNotes();
    }
}

and this is how I start my Application:

package biz.tugay.swingynotes;

import biz.tugay.swingynotes.controller.AddNewNoteButton;
import biz.tugay.swingynotes.model.SwingyNoteService;
import biz.tugay.swingynotes.view.AllNotesPanel;
import biz.tugay.swingynotes.view.AddNewNotePanel;
import biz.tugay.swingynotes.view.SwingyNotesMainFrame;

import javax.swing.*;

/* User: koray@tugay.biz Date: 2017/05/12 */
public class App {

    public static void main(String[] args) {

        // Model..
        final SwingyNoteService swingyNoteService = new SwingyNoteService();

        // View..
        final AllNotesPanel allNotesPanel = new AllNotesPanel(swingyNoteService);
        final AddNewNotePanel addNewNotePanel = new AddNewNotePanel(swingyNoteService);

        // Controller..
        final AddNewNoteButton addNewNoteButton = new AddNewNoteButton(addNewNotePanel, allNotesPanel);
        addNewNotePanel.add(addNewNoteButton);

        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new SwingyNotesMainFrame(allNotesPanel, addNewNotePanel);
            }
        });
    }
}

Do all components seem to be correct?


Get this bounty!!!

#StackBounty: #java #design-patterns #mvc Is this a correct implementation of Trygve Reenskaug 's MVC specification?

Bounty: 100

So I was reading the MODELS – VIEWS – CONTROLLERS paper by Trygve Reenskaug from December 1979 and wanted to try an implementation with Java Swing from my understandings..

Notes I have taken are as follows:

A view is attached to its model (or model part) and gets the data
necessary for the presentation from the model by asking questions. It
may also update the model by sending appropriate messages. All these
questions and messages have to be in the terminology of the model, the
view will therefore have to know the semantics of the attributes of
the model it represents.

A view should never know about user input, such as mouse operations
and keystrokes. It should always be possible to write a method in a
controller that sends messages to views which exactly reproduce any
sequence of user commands.

A Controller provides means for user input by presenting the user with
menus or other means of giving commands and data. The controller
receives such user input, translates it into the appropriate messages
and pass these messages onto one or more of the views.

The application I worked on is a very simple Note Taking application. This is what the application itself looks like:

enter image description here
enter image description here
enter image description here

Moving on the code, start with Directory Layout..enter image description here

My Model is very simple and knows nothing about Views or Controllers..

package biz.tugay.swingynotes.model;

/* User: koray@tugay.biz Date: 2017/05/12 */
public class SwingyNote {

    private final String note;

    public SwingyNote(final String note) {
        this.note = note;
    }

    public String getNote() {
        return note;
    }
}

package biz.tugay.swingynotes.model;

import java.util.*;

/* User: koray@tugay.biz Date: 2017/05/12 */
public final class SwingyNoteService {

    private final Set<SwingyNote> swingyNotes = new HashSet<SwingyNote>();

    // Add some sample data..
    {
        final SwingyNote sampleNote = new SwingyNote("Sample Note!");
        addNote(sampleNote);

        final SwingyNote helloWorld = new SwingyNote("Hello World!");
        addNote(helloWorld);
    }

    public void addNote(final SwingyNote swingyNote) {
        swingyNotes.add(swingyNote);
    }

    public Collection<SwingyNote> getAllNotes() {
        return swingyNotes;
    }
}

Moving on to View..

package biz.tugay.swingynotes.view;

import javax.swing.*;
import java.awt.*;

/* User: koray@tugay.biz Date: 2017/05/12 */
public class SwingyNotesMainFrame extends JFrame {

    public SwingyNotesMainFrame(final AllNotesPanel allNotesPanel, final AddNewNotePanel addNewNotePanel) {
        super("Swingy Notes");

        // Set Layout..
        final BorderLayout borderLayout = new BorderLayout();
        final Container contentPane = getContentPane();
        contentPane.setLayout(borderLayout);

        // Add panels..
        contentPane.add(allNotesPanel, BorderLayout.CENTER);
        contentPane.add(addNewNotePanel, BorderLayout.SOUTH);

        // Adjust properties and make the frame visible..
        setSize(600, 200);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setVisible(true);
    }
}

package biz.tugay.swingynotes.view;

import biz.tugay.swingynotes.model.SwingyNote;
import biz.tugay.swingynotes.model.SwingyNoteService;

import javax.swing.*;
import java.awt.*;
import java.util.Collection;

/* User: koray@tugay.biz Date: 2017/05/12 */
public class AllNotesPanel extends JPanel {

    private final SwingyNoteService swingyNoteService;

    public AllNotesPanel(final SwingyNoteService swingyNoteService) {
        this.swingyNoteService = swingyNoteService;
        displayAllNotes();
    }

    public void displayAllNotes() {
        removeAll();
        final Collection<SwingyNote> allNotes = swingyNoteService.getAllNotes();
        for (final SwingyNote swingyNote : allNotes) {
            final JLabel swingyNoteLabel = buildLabelForSwingyNote(swingyNote);
            add(swingyNoteLabel);
        }
        validate();
        repaint();
    }

    private JLabel buildLabelForSwingyNote(final SwingyNote swingyNote) {
        final JLabel swingyNoteLabel = new JLabel(swingyNote.getNote());
        swingyNoteLabel.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2));
        swingyNoteLabel.setFont(new Font("Dialog", Font.BOLD, 27));
        swingyNoteLabel.setOpaque(true);
        swingyNoteLabel.setBackground(Color.YELLOW);
        return swingyNoteLabel;
    }
}

package biz.tugay.swingynotes.view;

import biz.tugay.swingynotes.model.SwingyNote;
import biz.tugay.swingynotes.model.SwingyNoteService;

import javax.swing.*;

/* User: koray@tugay.biz Date: 2017/05/12 */
public class AddNewNotePanel extends JPanel {

    private final SwingyNoteService swingyNoteService;
    private final JTextField newNoteInputTextField;

    public AddNewNotePanel(final SwingyNoteService swingyNoteService) {
        this.swingyNoteService = swingyNoteService;
        this.newNoteInputTextField = new JTextField(20);

        add(newNoteInputTextField);
    }

    public void addNote() {
        final SwingyNote swingyNote = new SwingyNote(newNoteInputTextField.getText());
        swingyNoteService.addNote(swingyNote);
        newNoteInputTextField.setText(null);
    }
}

and here is my Controller

package biz.tugay.swingynotes.controller;

import biz.tugay.swingynotes.view.AddNewNotePanel;
import biz.tugay.swingynotes.view.AllNotesPanel;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/* User: koray@tugay.biz Date: 2017/05/12 */
public class AddNewNoteButton extends JButton implements ActionListener {

    private final AddNewNotePanel addNewNotePanel;
    private final AllNotesPanel allNotesPanel;

    public AddNewNoteButton(final AddNewNotePanel addNewNotePanel, final AllNotesPanel allNotesPanel) {
        super("Add New Note!");
        addActionListener(this);

        this.addNewNotePanel = addNewNotePanel;
        this.allNotesPanel = allNotesPanel;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        addNewNotePanel.addNote();
        allNotesPanel.displayAllNotes();
    }
}

and this is how I start my Application:

package biz.tugay.swingynotes;

import biz.tugay.swingynotes.controller.AddNewNoteButton;
import biz.tugay.swingynotes.model.SwingyNoteService;
import biz.tugay.swingynotes.view.AllNotesPanel;
import biz.tugay.swingynotes.view.AddNewNotePanel;
import biz.tugay.swingynotes.view.SwingyNotesMainFrame;

import javax.swing.*;

/* User: koray@tugay.biz Date: 2017/05/12 */
public class App {

    public static void main(String[] args) {

        // Model..
        final SwingyNoteService swingyNoteService = new SwingyNoteService();

        // View..
        final AllNotesPanel allNotesPanel = new AllNotesPanel(swingyNoteService);
        final AddNewNotePanel addNewNotePanel = new AddNewNotePanel(swingyNoteService);

        // Controller..
        final AddNewNoteButton addNewNoteButton = new AddNewNoteButton(addNewNotePanel, allNotesPanel);
        addNewNotePanel.add(addNewNoteButton);

        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new SwingyNotesMainFrame(allNotesPanel, addNewNotePanel);
            }
        });
    }
}

Do all components seem to be correct?


Get this bounty!!!

#StackBounty: #java #design-patterns #mvc Is this a correct implementation of Trygve Reenskaug 's MVC specification?

Bounty: 100

So I was reading the MODELS – VIEWS – CONTROLLERS paper by Trygve Reenskaug from December 1979 and wanted to try an implementation with Java Swing from my understandings..

Notes I have taken are as follows:

A view is attached to its model (or model part) and gets the data
necessary for the presentation from the model by asking questions. It
may also update the model by sending appropriate messages. All these
questions and messages have to be in the terminology of the model, the
view will therefore have to know the semantics of the attributes of
the model it represents.

A view should never know about user input, such as mouse operations
and keystrokes. It should always be possible to write a method in a
controller that sends messages to views which exactly reproduce any
sequence of user commands.

A Controller provides means for user input by presenting the user with
menus or other means of giving commands and data. The controller
receives such user input, translates it into the appropriate messages
and pass these messages onto one or more of the views.

The application I worked on is a very simple Note Taking application. This is what the application itself looks like:

enter image description here
enter image description here
enter image description here

Moving on the code, start with Directory Layout..enter image description here

My Model is very simple and knows nothing about Views or Controllers..

package biz.tugay.swingynotes.model;

/* User: koray@tugay.biz Date: 2017/05/12 */
public class SwingyNote {

    private final String note;

    public SwingyNote(final String note) {
        this.note = note;
    }

    public String getNote() {
        return note;
    }
}

package biz.tugay.swingynotes.model;

import java.util.*;

/* User: koray@tugay.biz Date: 2017/05/12 */
public final class SwingyNoteService {

    private final Set<SwingyNote> swingyNotes = new HashSet<SwingyNote>();

    // Add some sample data..
    {
        final SwingyNote sampleNote = new SwingyNote("Sample Note!");
        addNote(sampleNote);

        final SwingyNote helloWorld = new SwingyNote("Hello World!");
        addNote(helloWorld);
    }

    public void addNote(final SwingyNote swingyNote) {
        swingyNotes.add(swingyNote);
    }

    public Collection<SwingyNote> getAllNotes() {
        return swingyNotes;
    }
}

Moving on to View..

package biz.tugay.swingynotes.view;

import javax.swing.*;
import java.awt.*;

/* User: koray@tugay.biz Date: 2017/05/12 */
public class SwingyNotesMainFrame extends JFrame {

    public SwingyNotesMainFrame(final AllNotesPanel allNotesPanel, final AddNewNotePanel addNewNotePanel) {
        super("Swingy Notes");

        // Set Layout..
        final BorderLayout borderLayout = new BorderLayout();
        final Container contentPane = getContentPane();
        contentPane.setLayout(borderLayout);

        // Add panels..
        contentPane.add(allNotesPanel, BorderLayout.CENTER);
        contentPane.add(addNewNotePanel, BorderLayout.SOUTH);

        // Adjust properties and make the frame visible..
        setSize(600, 200);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setVisible(true);
    }
}

package biz.tugay.swingynotes.view;

import biz.tugay.swingynotes.model.SwingyNote;
import biz.tugay.swingynotes.model.SwingyNoteService;

import javax.swing.*;
import java.awt.*;
import java.util.Collection;

/* User: koray@tugay.biz Date: 2017/05/12 */
public class AllNotesPanel extends JPanel {

    private final SwingyNoteService swingyNoteService;

    public AllNotesPanel(final SwingyNoteService swingyNoteService) {
        this.swingyNoteService = swingyNoteService;
        displayAllNotes();
    }

    public void displayAllNotes() {
        removeAll();
        final Collection<SwingyNote> allNotes = swingyNoteService.getAllNotes();
        for (final SwingyNote swingyNote : allNotes) {
            final JLabel swingyNoteLabel = buildLabelForSwingyNote(swingyNote);
            add(swingyNoteLabel);
        }
        validate();
        repaint();
    }

    private JLabel buildLabelForSwingyNote(final SwingyNote swingyNote) {
        final JLabel swingyNoteLabel = new JLabel(swingyNote.getNote());
        swingyNoteLabel.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2));
        swingyNoteLabel.setFont(new Font("Dialog", Font.BOLD, 27));
        swingyNoteLabel.setOpaque(true);
        swingyNoteLabel.setBackground(Color.YELLOW);
        return swingyNoteLabel;
    }
}

package biz.tugay.swingynotes.view;

import biz.tugay.swingynotes.model.SwingyNote;
import biz.tugay.swingynotes.model.SwingyNoteService;

import javax.swing.*;

/* User: koray@tugay.biz Date: 2017/05/12 */
public class AddNewNotePanel extends JPanel {

    private final SwingyNoteService swingyNoteService;
    private final JTextField newNoteInputTextField;

    public AddNewNotePanel(final SwingyNoteService swingyNoteService) {
        this.swingyNoteService = swingyNoteService;
        this.newNoteInputTextField = new JTextField(20);

        add(newNoteInputTextField);
    }

    public void addNote() {
        final SwingyNote swingyNote = new SwingyNote(newNoteInputTextField.getText());
        swingyNoteService.addNote(swingyNote);
        newNoteInputTextField.setText(null);
    }
}

and here is my Controller

package biz.tugay.swingynotes.controller;

import biz.tugay.swingynotes.view.AddNewNotePanel;
import biz.tugay.swingynotes.view.AllNotesPanel;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/* User: koray@tugay.biz Date: 2017/05/12 */
public class AddNewNoteButton extends JButton implements ActionListener {

    private final AddNewNotePanel addNewNotePanel;
    private final AllNotesPanel allNotesPanel;

    public AddNewNoteButton(final AddNewNotePanel addNewNotePanel, final AllNotesPanel allNotesPanel) {
        super("Add New Note!");
        addActionListener(this);

        this.addNewNotePanel = addNewNotePanel;
        this.allNotesPanel = allNotesPanel;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        addNewNotePanel.addNote();
        allNotesPanel.displayAllNotes();
    }
}

and this is how I start my Application:

package biz.tugay.swingynotes;

import biz.tugay.swingynotes.controller.AddNewNoteButton;
import biz.tugay.swingynotes.model.SwingyNoteService;
import biz.tugay.swingynotes.view.AllNotesPanel;
import biz.tugay.swingynotes.view.AddNewNotePanel;
import biz.tugay.swingynotes.view.SwingyNotesMainFrame;

import javax.swing.*;

/* User: koray@tugay.biz Date: 2017/05/12 */
public class App {

    public static void main(String[] args) {

        // Model..
        final SwingyNoteService swingyNoteService = new SwingyNoteService();

        // View..
        final AllNotesPanel allNotesPanel = new AllNotesPanel(swingyNoteService);
        final AddNewNotePanel addNewNotePanel = new AddNewNotePanel(swingyNoteService);

        // Controller..
        final AddNewNoteButton addNewNoteButton = new AddNewNoteButton(addNewNotePanel, allNotesPanel);
        addNewNotePanel.add(addNewNoteButton);

        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new SwingyNotesMainFrame(allNotesPanel, addNewNotePanel);
            }
        });
    }
}

Do all components seem to be correct?


Get this bounty!!!

#StackBounty: #java #design-patterns #mvc Is this a correct implementation of Trygve Reenskaug 's MVC specification?

Bounty: 100

So I was reading the MODELS – VIEWS – CONTROLLERS paper by Trygve Reenskaug from December 1979 and wanted to try an implementation with Java Swing from my understandings..

Notes I have taken are as follows:

A view is attached to its model (or model part) and gets the data
necessary for the presentation from the model by asking questions. It
may also update the model by sending appropriate messages. All these
questions and messages have to be in the terminology of the model, the
view will therefore have to know the semantics of the attributes of
the model it represents.

A view should never know about user input, such as mouse operations
and keystrokes. It should always be possible to write a method in a
controller that sends messages to views which exactly reproduce any
sequence of user commands.

A Controller provides means for user input by presenting the user with
menus or other means of giving commands and data. The controller
receives such user input, translates it into the appropriate messages
and pass these messages onto one or more of the views.

The application I worked on is a very simple Note Taking application. This is what the application itself looks like:

enter image description here
enter image description here
enter image description here

Moving on the code, start with Directory Layout..enter image description here

My Model is very simple and knows nothing about Views or Controllers..

package biz.tugay.swingynotes.model;

/* User: koray@tugay.biz Date: 2017/05/12 */
public class SwingyNote {

    private final String note;

    public SwingyNote(final String note) {
        this.note = note;
    }

    public String getNote() {
        return note;
    }
}

package biz.tugay.swingynotes.model;

import java.util.*;

/* User: koray@tugay.biz Date: 2017/05/12 */
public final class SwingyNoteService {

    private final Set<SwingyNote> swingyNotes = new HashSet<SwingyNote>();

    // Add some sample data..
    {
        final SwingyNote sampleNote = new SwingyNote("Sample Note!");
        addNote(sampleNote);

        final SwingyNote helloWorld = new SwingyNote("Hello World!");
        addNote(helloWorld);
    }

    public void addNote(final SwingyNote swingyNote) {
        swingyNotes.add(swingyNote);
    }

    public Collection<SwingyNote> getAllNotes() {
        return swingyNotes;
    }
}

Moving on to View..

package biz.tugay.swingynotes.view;

import javax.swing.*;
import java.awt.*;

/* User: koray@tugay.biz Date: 2017/05/12 */
public class SwingyNotesMainFrame extends JFrame {

    public SwingyNotesMainFrame(final AllNotesPanel allNotesPanel, final AddNewNotePanel addNewNotePanel) {
        super("Swingy Notes");

        // Set Layout..
        final BorderLayout borderLayout = new BorderLayout();
        final Container contentPane = getContentPane();
        contentPane.setLayout(borderLayout);

        // Add panels..
        contentPane.add(allNotesPanel, BorderLayout.CENTER);
        contentPane.add(addNewNotePanel, BorderLayout.SOUTH);

        // Adjust properties and make the frame visible..
        setSize(600, 200);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setVisible(true);
    }
}

package biz.tugay.swingynotes.view;

import biz.tugay.swingynotes.model.SwingyNote;
import biz.tugay.swingynotes.model.SwingyNoteService;

import javax.swing.*;
import java.awt.*;
import java.util.Collection;

/* User: koray@tugay.biz Date: 2017/05/12 */
public class AllNotesPanel extends JPanel {

    private final SwingyNoteService swingyNoteService;

    public AllNotesPanel(final SwingyNoteService swingyNoteService) {
        this.swingyNoteService = swingyNoteService;
        displayAllNotes();
    }

    public void displayAllNotes() {
        removeAll();
        final Collection<SwingyNote> allNotes = swingyNoteService.getAllNotes();
        for (final SwingyNote swingyNote : allNotes) {
            final JLabel swingyNoteLabel = buildLabelForSwingyNote(swingyNote);
            add(swingyNoteLabel);
        }
        validate();
        repaint();
    }

    private JLabel buildLabelForSwingyNote(final SwingyNote swingyNote) {
        final JLabel swingyNoteLabel = new JLabel(swingyNote.getNote());
        swingyNoteLabel.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2));
        swingyNoteLabel.setFont(new Font("Dialog", Font.BOLD, 27));
        swingyNoteLabel.setOpaque(true);
        swingyNoteLabel.setBackground(Color.YELLOW);
        return swingyNoteLabel;
    }
}

package biz.tugay.swingynotes.view;

import biz.tugay.swingynotes.model.SwingyNote;
import biz.tugay.swingynotes.model.SwingyNoteService;

import javax.swing.*;

/* User: koray@tugay.biz Date: 2017/05/12 */
public class AddNewNotePanel extends JPanel {

    private final SwingyNoteService swingyNoteService;
    private final JTextField newNoteInputTextField;

    public AddNewNotePanel(final SwingyNoteService swingyNoteService) {
        this.swingyNoteService = swingyNoteService;
        this.newNoteInputTextField = new JTextField(20);

        add(newNoteInputTextField);
    }

    public void addNote() {
        final SwingyNote swingyNote = new SwingyNote(newNoteInputTextField.getText());
        swingyNoteService.addNote(swingyNote);
        newNoteInputTextField.setText(null);
    }
}

and here is my Controller

package biz.tugay.swingynotes.controller;

import biz.tugay.swingynotes.view.AddNewNotePanel;
import biz.tugay.swingynotes.view.AllNotesPanel;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/* User: koray@tugay.biz Date: 2017/05/12 */
public class AddNewNoteButton extends JButton implements ActionListener {

    private final AddNewNotePanel addNewNotePanel;
    private final AllNotesPanel allNotesPanel;

    public AddNewNoteButton(final AddNewNotePanel addNewNotePanel, final AllNotesPanel allNotesPanel) {
        super("Add New Note!");
        addActionListener(this);

        this.addNewNotePanel = addNewNotePanel;
        this.allNotesPanel = allNotesPanel;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        addNewNotePanel.addNote();
        allNotesPanel.displayAllNotes();
    }
}

and this is how I start my Application:

package biz.tugay.swingynotes;

import biz.tugay.swingynotes.controller.AddNewNoteButton;
import biz.tugay.swingynotes.model.SwingyNoteService;
import biz.tugay.swingynotes.view.AllNotesPanel;
import biz.tugay.swingynotes.view.AddNewNotePanel;
import biz.tugay.swingynotes.view.SwingyNotesMainFrame;

import javax.swing.*;

/* User: koray@tugay.biz Date: 2017/05/12 */
public class App {

    public static void main(String[] args) {

        // Model..
        final SwingyNoteService swingyNoteService = new SwingyNoteService();

        // View..
        final AllNotesPanel allNotesPanel = new AllNotesPanel(swingyNoteService);
        final AddNewNotePanel addNewNotePanel = new AddNewNotePanel(swingyNoteService);

        // Controller..
        final AddNewNoteButton addNewNoteButton = new AddNewNoteButton(addNewNotePanel, allNotesPanel);
        addNewNotePanel.add(addNewNoteButton);

        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new SwingyNotesMainFrame(allNotesPanel, addNewNotePanel);
            }
        });
    }
}

Do all components seem to be correct?


Get this bounty!!!

#StackBounty: #java #design-patterns #mvc Is this a correct implementation of Trygve Reenskaug 's MVC specification?

Bounty: 100

So I was reading the MODELS – VIEWS – CONTROLLERS paper by Trygve Reenskaug from December 1979 and wanted to try an implementation with Java Swing from my understandings..

Notes I have taken are as follows:

A view is attached to its model (or model part) and gets the data
necessary for the presentation from the model by asking questions. It
may also update the model by sending appropriate messages. All these
questions and messages have to be in the terminology of the model, the
view will therefore have to know the semantics of the attributes of
the model it represents.

A view should never know about user input, such as mouse operations
and keystrokes. It should always be possible to write a method in a
controller that sends messages to views which exactly reproduce any
sequence of user commands.

A Controller provides means for user input by presenting the user with
menus or other means of giving commands and data. The controller
receives such user input, translates it into the appropriate messages
and pass these messages onto one or more of the views.

The application I worked on is a very simple Note Taking application. This is what the application itself looks like:

enter image description here
enter image description here
enter image description here

Moving on the code, start with Directory Layout..enter image description here

My Model is very simple and knows nothing about Views or Controllers..

package biz.tugay.swingynotes.model;

/* User: koray@tugay.biz Date: 2017/05/12 */
public class SwingyNote {

    private final String note;

    public SwingyNote(final String note) {
        this.note = note;
    }

    public String getNote() {
        return note;
    }
}

package biz.tugay.swingynotes.model;

import java.util.*;

/* User: koray@tugay.biz Date: 2017/05/12 */
public final class SwingyNoteService {

    private final Set<SwingyNote> swingyNotes = new HashSet<SwingyNote>();

    // Add some sample data..
    {
        final SwingyNote sampleNote = new SwingyNote("Sample Note!");
        addNote(sampleNote);

        final SwingyNote helloWorld = new SwingyNote("Hello World!");
        addNote(helloWorld);
    }

    public void addNote(final SwingyNote swingyNote) {
        swingyNotes.add(swingyNote);
    }

    public Collection<SwingyNote> getAllNotes() {
        return swingyNotes;
    }
}

Moving on to View..

package biz.tugay.swingynotes.view;

import javax.swing.*;
import java.awt.*;

/* User: koray@tugay.biz Date: 2017/05/12 */
public class SwingyNotesMainFrame extends JFrame {

    public SwingyNotesMainFrame(final AllNotesPanel allNotesPanel, final AddNewNotePanel addNewNotePanel) {
        super("Swingy Notes");

        // Set Layout..
        final BorderLayout borderLayout = new BorderLayout();
        final Container contentPane = getContentPane();
        contentPane.setLayout(borderLayout);

        // Add panels..
        contentPane.add(allNotesPanel, BorderLayout.CENTER);
        contentPane.add(addNewNotePanel, BorderLayout.SOUTH);

        // Adjust properties and make the frame visible..
        setSize(600, 200);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setVisible(true);
    }
}

package biz.tugay.swingynotes.view;

import biz.tugay.swingynotes.model.SwingyNote;
import biz.tugay.swingynotes.model.SwingyNoteService;

import javax.swing.*;
import java.awt.*;
import java.util.Collection;

/* User: koray@tugay.biz Date: 2017/05/12 */
public class AllNotesPanel extends JPanel {

    private final SwingyNoteService swingyNoteService;

    public AllNotesPanel(final SwingyNoteService swingyNoteService) {
        this.swingyNoteService = swingyNoteService;
        displayAllNotes();
    }

    public void displayAllNotes() {
        removeAll();
        final Collection<SwingyNote> allNotes = swingyNoteService.getAllNotes();
        for (final SwingyNote swingyNote : allNotes) {
            final JLabel swingyNoteLabel = buildLabelForSwingyNote(swingyNote);
            add(swingyNoteLabel);
        }
        validate();
        repaint();
    }

    private JLabel buildLabelForSwingyNote(final SwingyNote swingyNote) {
        final JLabel swingyNoteLabel = new JLabel(swingyNote.getNote());
        swingyNoteLabel.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2));
        swingyNoteLabel.setFont(new Font("Dialog", Font.BOLD, 27));
        swingyNoteLabel.setOpaque(true);
        swingyNoteLabel.setBackground(Color.YELLOW);
        return swingyNoteLabel;
    }
}

package biz.tugay.swingynotes.view;

import biz.tugay.swingynotes.model.SwingyNote;
import biz.tugay.swingynotes.model.SwingyNoteService;

import javax.swing.*;

/* User: koray@tugay.biz Date: 2017/05/12 */
public class AddNewNotePanel extends JPanel {

    private final SwingyNoteService swingyNoteService;
    private final JTextField newNoteInputTextField;

    public AddNewNotePanel(final SwingyNoteService swingyNoteService) {
        this.swingyNoteService = swingyNoteService;
        this.newNoteInputTextField = new JTextField(20);

        add(newNoteInputTextField);
    }

    public void addNote() {
        final SwingyNote swingyNote = new SwingyNote(newNoteInputTextField.getText());
        swingyNoteService.addNote(swingyNote);
        newNoteInputTextField.setText(null);
    }
}

and here is my Controller

package biz.tugay.swingynotes.controller;

import biz.tugay.swingynotes.view.AddNewNotePanel;
import biz.tugay.swingynotes.view.AllNotesPanel;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/* User: koray@tugay.biz Date: 2017/05/12 */
public class AddNewNoteButton extends JButton implements ActionListener {

    private final AddNewNotePanel addNewNotePanel;
    private final AllNotesPanel allNotesPanel;

    public AddNewNoteButton(final AddNewNotePanel addNewNotePanel, final AllNotesPanel allNotesPanel) {
        super("Add New Note!");
        addActionListener(this);

        this.addNewNotePanel = addNewNotePanel;
        this.allNotesPanel = allNotesPanel;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        addNewNotePanel.addNote();
        allNotesPanel.displayAllNotes();
    }
}

and this is how I start my Application:

package biz.tugay.swingynotes;

import biz.tugay.swingynotes.controller.AddNewNoteButton;
import biz.tugay.swingynotes.model.SwingyNoteService;
import biz.tugay.swingynotes.view.AllNotesPanel;
import biz.tugay.swingynotes.view.AddNewNotePanel;
import biz.tugay.swingynotes.view.SwingyNotesMainFrame;

import javax.swing.*;

/* User: koray@tugay.biz Date: 2017/05/12 */
public class App {

    public static void main(String[] args) {

        // Model..
        final SwingyNoteService swingyNoteService = new SwingyNoteService();

        // View..
        final AllNotesPanel allNotesPanel = new AllNotesPanel(swingyNoteService);
        final AddNewNotePanel addNewNotePanel = new AddNewNotePanel(swingyNoteService);

        // Controller..
        final AddNewNoteButton addNewNoteButton = new AddNewNoteButton(addNewNotePanel, allNotesPanel);
        addNewNotePanel.add(addNewNoteButton);

        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new SwingyNotesMainFrame(allNotesPanel, addNewNotePanel);
            }
        });
    }
}

Do all components seem to be correct?


Get this bounty!!!

#StackBounty: #java #jpa #docker #keycloak Bottleneck when using auth/admin/realms/myrealm/users in my app

Bounty: 350

I am running a script which requests the master token before requesting the list of users for a realm. I perform this request in a tight loop 100 times and output the time it takes to perform the “auth/admin/realms/mine/users?first=0&max=1000” request. When testing with a locally installed keycloak docker, a single request is taking 4.0 seconds. If I run 5 instances of this script at the same time, a single request takes around 10 seconds. And if I run 10 instances, it jumps to 20 seconds before a reply is received. Therefore, I have a serious bottleneck in my webapp on the page where I list all user information and was wondering how I might go about solving this problem. I am running keycloak 2.5.0.Final. My java home is /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.111-2.b15.el7_3.x86_64/jre. authorizationPersister = jpa.

My script:

import requests

keyCloakUrl = "http://foo.bar"

for i in range(100):

    session = requests.Session()
    r = session.post(
        keyCloakUrl +"auth/realms/master/protocol/openid-connect/token", 
        data={
            "grant_type":"password",
            "client_id":"admin-cli",
            "username":"admin",
            "password":"admin"
        }
    )
    master_token = json.loads(r.content)['access_token']

    r = s.get(
        keyCloakUrl + "auth/admin/realms/mine/users?first=0&max=1000",
        headers={'Authorization':'Bearer '+master_token}
    )
    print(r.elapsed.total_seconds())

My dockerfile:

FROM jboss/keycloak-mysql:2.5.0.Final

ARG db2

ADD deps/ /opt/jboss/keycloak/
RUN /opt/jboss/keycloak/import_utbud_realm.sh 

CMD ["-b", "0.0.0.0", "-bmanagement", "0.0.0.0", "-Djboss.socket.binding.port-offset=2"]

UPDATE

I also tried measuring the time for the “auth/realms/master/protocol/openid-connect/token” request. This is 0.1 seconds per request with only one instance of the script running and 0.8 seconds with 10 instances running.


Get this bounty!!!

#StackBounty: #java #design-patterns #mvc Is this a correct implementation of Trygve Reenskaug 's MVC specification?

Bounty: 100

So I was reading the MODELS – VIEWS – CONTROLLERS paper by Trygve Reenskaug from December 1979 and wanted to try an implementation with Java Swing from my understandings..

Notes I have taken are as follows:

A view is attached to its model (or model part) and gets the data
necessary for the presentation from the model by asking questions. It
may also update the model by sending appropriate messages. All these
questions and messages have to be in the terminology of the model, the
view will therefore have to know the semantics of the attributes of
the model it represents.

A view should never know about user input, such as mouse operations
and keystrokes. It should always be possible to write a method in a
controller that sends messages to views which exactly reproduce any
sequence of user commands.

A Controller provides means for user input by presenting the user with
menus or other means of giving commands and data. The controller
receives such user input, translates it into the appropriate messages
and pass these messages onto one or more of the views.

The application I worked on is a very simple Note Taking application. This is what the application itself looks like:

enter image description here
enter image description here
enter image description here

Moving on the code, start with Directory Layout..enter image description here

My Model is very simple and knows nothing about Views or Controllers..

package biz.tugay.swingynotes.model;

/* User: koray@tugay.biz Date: 2017/05/12 */
public class SwingyNote {

    private final String note;

    public SwingyNote(final String note) {
        this.note = note;
    }

    public String getNote() {
        return note;
    }
}

package biz.tugay.swingynotes.model;

import java.util.*;

/* User: koray@tugay.biz Date: 2017/05/12 */
public final class SwingyNoteService {

    private final Set<SwingyNote> swingyNotes = new HashSet<SwingyNote>();

    // Add some sample data..
    {
        final SwingyNote sampleNote = new SwingyNote("Sample Note!");
        addNote(sampleNote);

        final SwingyNote helloWorld = new SwingyNote("Hello World!");
        addNote(helloWorld);
    }

    public void addNote(final SwingyNote swingyNote) {
        swingyNotes.add(swingyNote);
    }

    public Collection<SwingyNote> getAllNotes() {
        return swingyNotes;
    }
}

Moving on to View..

package biz.tugay.swingynotes.view;

import javax.swing.*;
import java.awt.*;

/* User: koray@tugay.biz Date: 2017/05/12 */
public class SwingyNotesMainFrame extends JFrame {

    public SwingyNotesMainFrame(final AllNotesPanel allNotesPanel, final AddNewNotePanel addNewNotePanel) {
        super("Swingy Notes");

        // Set Layout..
        final BorderLayout borderLayout = new BorderLayout();
        final Container contentPane = getContentPane();
        contentPane.setLayout(borderLayout);

        // Add panels..
        contentPane.add(allNotesPanel, BorderLayout.CENTER);
        contentPane.add(addNewNotePanel, BorderLayout.SOUTH);

        // Adjust properties and make the frame visible..
        setSize(600, 200);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setVisible(true);
    }
}

package biz.tugay.swingynotes.view;

import biz.tugay.swingynotes.model.SwingyNote;
import biz.tugay.swingynotes.model.SwingyNoteService;

import javax.swing.*;
import java.awt.*;
import java.util.Collection;

/* User: koray@tugay.biz Date: 2017/05/12 */
public class AllNotesPanel extends JPanel {

    private final SwingyNoteService swingyNoteService;

    public AllNotesPanel(final SwingyNoteService swingyNoteService) {
        this.swingyNoteService = swingyNoteService;
        displayAllNotes();
    }

    public void displayAllNotes() {
        removeAll();
        final Collection<SwingyNote> allNotes = swingyNoteService.getAllNotes();
        for (final SwingyNote swingyNote : allNotes) {
            final JLabel swingyNoteLabel = buildLabelForSwingyNote(swingyNote);
            add(swingyNoteLabel);
        }
        validate();
        repaint();
    }

    private JLabel buildLabelForSwingyNote(final SwingyNote swingyNote) {
        final JLabel swingyNoteLabel = new JLabel(swingyNote.getNote());
        swingyNoteLabel.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2));
        swingyNoteLabel.setFont(new Font("Dialog", Font.BOLD, 27));
        swingyNoteLabel.setOpaque(true);
        swingyNoteLabel.setBackground(Color.YELLOW);
        return swingyNoteLabel;
    }
}

package biz.tugay.swingynotes.view;

import biz.tugay.swingynotes.model.SwingyNote;
import biz.tugay.swingynotes.model.SwingyNoteService;

import javax.swing.*;

/* User: koray@tugay.biz Date: 2017/05/12 */
public class AddNewNotePanel extends JPanel {

    private final SwingyNoteService swingyNoteService;
    private final JTextField newNoteInputTextField;

    public AddNewNotePanel(final SwingyNoteService swingyNoteService) {
        this.swingyNoteService = swingyNoteService;
        this.newNoteInputTextField = new JTextField(20);

        add(newNoteInputTextField);
    }

    public void addNote() {
        final SwingyNote swingyNote = new SwingyNote(newNoteInputTextField.getText());
        swingyNoteService.addNote(swingyNote);
        newNoteInputTextField.setText(null);
    }
}

and here is my Controller

package biz.tugay.swingynotes.controller;

import biz.tugay.swingynotes.view.AddNewNotePanel;
import biz.tugay.swingynotes.view.AllNotesPanel;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/* User: koray@tugay.biz Date: 2017/05/12 */
public class AddNewNoteButton extends JButton implements ActionListener {

    private final AddNewNotePanel addNewNotePanel;
    private final AllNotesPanel allNotesPanel;

    public AddNewNoteButton(final AddNewNotePanel addNewNotePanel, final AllNotesPanel allNotesPanel) {
        super("Add New Note!");
        addActionListener(this);

        this.addNewNotePanel = addNewNotePanel;
        this.allNotesPanel = allNotesPanel;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        addNewNotePanel.addNote();
        allNotesPanel.displayAllNotes();
    }
}

and this is how I start my Application:

package biz.tugay.swingynotes;

import biz.tugay.swingynotes.controller.AddNewNoteButton;
import biz.tugay.swingynotes.model.SwingyNoteService;
import biz.tugay.swingynotes.view.AllNotesPanel;
import biz.tugay.swingynotes.view.AddNewNotePanel;
import biz.tugay.swingynotes.view.SwingyNotesMainFrame;

import javax.swing.*;

/* User: koray@tugay.biz Date: 2017/05/12 */
public class App {

    public static void main(String[] args) {

        // Model..
        final SwingyNoteService swingyNoteService = new SwingyNoteService();

        // View..
        final AllNotesPanel allNotesPanel = new AllNotesPanel(swingyNoteService);
        final AddNewNotePanel addNewNotePanel = new AddNewNotePanel(swingyNoteService);

        // Controller..
        final AddNewNoteButton addNewNoteButton = new AddNewNoteButton(addNewNotePanel, allNotesPanel);
        addNewNotePanel.add(addNewNoteButton);

        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new SwingyNotesMainFrame(allNotesPanel, addNewNotePanel);
            }
        });
    }
}

Do all components seem to be correct?


Get this bounty!!!

#StackBounty: #java #design-patterns #mvc Is this a correct implementation of Trygve Reenskaug 's MVC specification?

Bounty: 100

So I was reading the MODELS – VIEWS – CONTROLLERS paper by Trygve Reenskaug from December 1979 and wanted to try an implementation with Java Swing from my understandings..

Notes I have taken are as follows:

A view is attached to its model (or model part) and gets the data
necessary for the presentation from the model by asking questions. It
may also update the model by sending appropriate messages. All these
questions and messages have to be in the terminology of the model, the
view will therefore have to know the semantics of the attributes of
the model it represents.

A view should never know about user input, such as mouse operations
and keystrokes. It should always be possible to write a method in a
controller that sends messages to views which exactly reproduce any
sequence of user commands.

A Controller provides means for user input by presenting the user with
menus or other means of giving commands and data. The controller
receives such user input, translates it into the appropriate messages
and pass these messages onto one or more of the views.

The application I worked on is a very simple Note Taking application. This is what the application itself looks like:

enter image description here
enter image description here
enter image description here

Moving on the code, start with Directory Layout..enter image description here

My Model is very simple and knows nothing about Views or Controllers..

package biz.tugay.swingynotes.model;

/* User: koray@tugay.biz Date: 2017/05/12 */
public class SwingyNote {

    private final String note;

    public SwingyNote(final String note) {
        this.note = note;
    }

    public String getNote() {
        return note;
    }
}

package biz.tugay.swingynotes.model;

import java.util.*;

/* User: koray@tugay.biz Date: 2017/05/12 */
public final class SwingyNoteService {

    private final Set<SwingyNote> swingyNotes = new HashSet<SwingyNote>();

    // Add some sample data..
    {
        final SwingyNote sampleNote = new SwingyNote("Sample Note!");
        addNote(sampleNote);

        final SwingyNote helloWorld = new SwingyNote("Hello World!");
        addNote(helloWorld);
    }

    public void addNote(final SwingyNote swingyNote) {
        swingyNotes.add(swingyNote);
    }

    public Collection<SwingyNote> getAllNotes() {
        return swingyNotes;
    }
}

Moving on to View..

package biz.tugay.swingynotes.view;

import javax.swing.*;
import java.awt.*;

/* User: koray@tugay.biz Date: 2017/05/12 */
public class SwingyNotesMainFrame extends JFrame {

    public SwingyNotesMainFrame(final AllNotesPanel allNotesPanel, final AddNewNotePanel addNewNotePanel) {
        super("Swingy Notes");

        // Set Layout..
        final BorderLayout borderLayout = new BorderLayout();
        final Container contentPane = getContentPane();
        contentPane.setLayout(borderLayout);

        // Add panels..
        contentPane.add(allNotesPanel, BorderLayout.CENTER);
        contentPane.add(addNewNotePanel, BorderLayout.SOUTH);

        // Adjust properties and make the frame visible..
        setSize(600, 200);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setVisible(true);
    }
}

package biz.tugay.swingynotes.view;

import biz.tugay.swingynotes.model.SwingyNote;
import biz.tugay.swingynotes.model.SwingyNoteService;

import javax.swing.*;
import java.awt.*;
import java.util.Collection;

/* User: koray@tugay.biz Date: 2017/05/12 */
public class AllNotesPanel extends JPanel {

    private final SwingyNoteService swingyNoteService;

    public AllNotesPanel(final SwingyNoteService swingyNoteService) {
        this.swingyNoteService = swingyNoteService;
        displayAllNotes();
    }

    public void displayAllNotes() {
        removeAll();
        final Collection<SwingyNote> allNotes = swingyNoteService.getAllNotes();
        for (final SwingyNote swingyNote : allNotes) {
            final JLabel swingyNoteLabel = buildLabelForSwingyNote(swingyNote);
            add(swingyNoteLabel);
        }
        validate();
        repaint();
    }

    private JLabel buildLabelForSwingyNote(final SwingyNote swingyNote) {
        final JLabel swingyNoteLabel = new JLabel(swingyNote.getNote());
        swingyNoteLabel.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2));
        swingyNoteLabel.setFont(new Font("Dialog", Font.BOLD, 27));
        swingyNoteLabel.setOpaque(true);
        swingyNoteLabel.setBackground(Color.YELLOW);
        return swingyNoteLabel;
    }
}

package biz.tugay.swingynotes.view;

import biz.tugay.swingynotes.model.SwingyNote;
import biz.tugay.swingynotes.model.SwingyNoteService;

import javax.swing.*;

/* User: koray@tugay.biz Date: 2017/05/12 */
public class AddNewNotePanel extends JPanel {

    private final SwingyNoteService swingyNoteService;
    private final JTextField newNoteInputTextField;

    public AddNewNotePanel(final SwingyNoteService swingyNoteService) {
        this.swingyNoteService = swingyNoteService;
        this.newNoteInputTextField = new JTextField(20);

        add(newNoteInputTextField);
    }

    public void addNote() {
        final SwingyNote swingyNote = new SwingyNote(newNoteInputTextField.getText());
        swingyNoteService.addNote(swingyNote);
        newNoteInputTextField.setText(null);
    }
}

and here is my Controller

package biz.tugay.swingynotes.controller;

import biz.tugay.swingynotes.view.AddNewNotePanel;
import biz.tugay.swingynotes.view.AllNotesPanel;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/* User: koray@tugay.biz Date: 2017/05/12 */
public class AddNewNoteButton extends JButton implements ActionListener {

    private final AddNewNotePanel addNewNotePanel;
    private final AllNotesPanel allNotesPanel;

    public AddNewNoteButton(final AddNewNotePanel addNewNotePanel, final AllNotesPanel allNotesPanel) {
        super("Add New Note!");
        addActionListener(this);

        this.addNewNotePanel = addNewNotePanel;
        this.allNotesPanel = allNotesPanel;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        addNewNotePanel.addNote();
        allNotesPanel.displayAllNotes();
    }
}

and this is how I start my Application:

package biz.tugay.swingynotes;

import biz.tugay.swingynotes.controller.AddNewNoteButton;
import biz.tugay.swingynotes.model.SwingyNoteService;
import biz.tugay.swingynotes.view.AllNotesPanel;
import biz.tugay.swingynotes.view.AddNewNotePanel;
import biz.tugay.swingynotes.view.SwingyNotesMainFrame;

import javax.swing.*;

/* User: koray@tugay.biz Date: 2017/05/12 */
public class App {

    public static void main(String[] args) {

        // Model..
        final SwingyNoteService swingyNoteService = new SwingyNoteService();

        // View..
        final AllNotesPanel allNotesPanel = new AllNotesPanel(swingyNoteService);
        final AddNewNotePanel addNewNotePanel = new AddNewNotePanel(swingyNoteService);

        // Controller..
        final AddNewNoteButton addNewNoteButton = new AddNewNoteButton(addNewNotePanel, allNotesPanel);
        addNewNotePanel.add(addNewNoteButton);

        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new SwingyNotesMainFrame(allNotesPanel, addNewNotePanel);
            }
        });
    }
}

Do all components seem to be correct?


Get this bounty!!!