mercredi 6 juillet 2016

How to connect several JTextFields to the setters / fields of an Observer Object?

I am creating a system that deals with orders of technical products.

My program has a GUI: on the left side is a navigation menu, at the top is the current order number and in the center is the technical data. The center has a cardlayout that can switch between different cards / pages for different aspects of an order.

I'm using the observer pattern to update the different pages, when the current orderObject changes, therefore each page implements my IOrderObserver and with it the updateCurrentOrderObject(OrderObject)-method, so it gets updated, if the current orderObject changes.

Here is a shortened UML-Diagram for my Observer Pattern. I have several other pages, but the idea is the same.

UML Diagram for the Observer Pattern

Each page has 50-80 JTextFields, which get filled with the updateCurrentOrderObject()-method:

public void updateCurrentOrderObject(OrderObject orderObject) {
    this.orderObject = orderObject;

    txtLocationAdressNameLineOne.setText(orderObject.getLocationAdressNameLineOne());
    txtLocationAdressNameLineTwo.setText(orderObject.getLocationAdressNameLineTwo());
    txtLocationAdressStreet.setText(orderObject.getLocationAdressStreet());
    txtLocationAdressZip.setText(orderObject.getLocationAdressZip());
    txtLocationAdressLocation.setText(orderObject.getLocationAdressLocation());
    txtOperatorAdressNameLineOne.setText(orderObject.getOperatorAdressNameLineOne());
    txtOperatorAdressNameLineTwo.setText(orderObject.getOperatorAdressNameLineTwo());
    txtOperatorAdressStreet.setText(orderObject.getOperatorAdressStreet());
    txtOperatorAdressZip.setText(orderObject.getOperatorAdressZip());
    txtOperatorAdressLocation.setText(orderObject.getOperatorAdressLocation());
.....

Since the user has to be able to change the content of a JTextField, I've written a focuslistener. Whenever the user has changed the content of a field and jumps to the next, the changes start to propagate into the Observer and with it into all the other pages.

// Hardcoded set-Method, since I dont know how to get the correct setter
orderObject.setCar_abbreviation(focusLostTextField.getText());

The effect: All pages get notified about the latest orderObject since a setter was used.

FocusListener highlighter = new FocusListener() {

     @Override
     public void focusGained(FocusEvent e) {
        e.getComponent().setBackground(Color.GREEN);
     }

     @Override
     public void focusLost(FocusEvent e) {
        JTextField focusLostTextField = (JTextField) e.getComponent();
        if (!focusLostTextField.getText().equals("")) {
           e.getComponent().setBackground(UIManager.getColor("TextField.background"));

           // Missing connection between the JTextField and the setter of the orderObject
           orderObject.setCar_abbreviation(focusLostTextField.getText());

           Method[] methods = orderObject.getClass().getDeclaredMethods();
           for (int i = 0; i < methods.length; i++) {
              String methodName = methods[i].getName();
              if (methodName.startsWith("set")) {
                 System.out.println(methodName);
              }
           }
        }
        if (focusLostTextField.getText().equals("")) {
           e.getComponent().setBackground(Color.RED);
        }
     }
  };

When I use Reflection, I can get all setter-methods from the orderObject:

Method[] methods = orderObject.getClass().getDeclaredMethods();
for (int i = 0; i < methods.length; i++) {
  String methodName = methods[i].getName();
  if (methodName.startsWith("set")) {
     System.out.println(methodName);
  }
}

BUT here is my problem: How can I connect the JTextField, where the user changed something to the correct setter-method of the orderObject? Or worded differently: How can I find the correct setter in the orderObject for the JTextField, where the user entered / changed something?





Aucun commentaire:

Enregistrer un commentaire