Car Designer on the NetBeans Platform (Part 4)

Let's now make the application more realistic and, in fact, simpler. This time we'll have a new window for each car, with Save functionality enabled/disabled on the Node as well as the TopComponent:

The car object is unchanged, i.e., the same as before:

public class Car {

    String type;
    String color;

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }
    
}

The DataObject is also unchanged. However, the OpenSupport class is changed, so that multiple windows will be openable, instead of a single instance:

public class CarOpenSupport extends OpenSupport implements OpenCookie, CloseCookie {

    public CarOpenSupport(CarDataObject.Entry entry) {
        super(entry);
    }

    @Override
    protected CloneableTopComponent createCloneableTopComponent() {
        CarDataObject cdo = (CarDataObject)entry.getDataObject();
        return new CarDesignTopComponent(cdo); 
//        return CarDesignTopComponent.findInstance();
    }
 
}

Finally, the TopComponent is changed a lot and much simpler, i.e., no LookupListener, because we received the DataObject via the constructor and we're not sharing the single window with multiple cars:

public CarDesignTopComponent(final CarDataObject cdo) {

    this();

    final Car car = cdo.getLookup().lookup(Car.class);

    setName(NbBundle.getMessage(CarDesignTopComponent.class, "CTL_CarDesignTopComponent"));
    setToolTipText(NbBundle.getMessage(CarDesignTopComponent.class, "HINT_CarDesignTopComponent"));

    setDisplayName(car.getType());

    typeField.setText(car.getType());
    colorField.setText(car.getColor());

    //Enable save when the editor has focus,
    //by exposing the node, which exposes the data object,
    //which has save capability via its SaveCookie:
    setActivatedNodes(new Node[]{cdo.getNodeDelegate()});

    typeField.getDocument().addDocumentListener(new DocumentListener() {
        @Override
        public void insertUpdate(DocumentEvent e) {
            updateType();
        }
        @Override
        public void removeUpdate(DocumentEvent e) {
            updateType();
        }
        @Override
        public void changedUpdate(DocumentEvent e) {
            updateType();
        }
        private void updateType() {
            car.setType(typeField.getText());
            setDisplayName(car.getType());
            cdo.setModified(true);
        }
    });

    colorField.getDocument().addDocumentListener(new DocumentListener() {
        @Override
        public void insertUpdate(DocumentEvent e) {
            updateColor();
        }
        @Override
        public void removeUpdate(DocumentEvent e) {
            updateColor();
        }
        @Override
        public void changedUpdate(DocumentEvent e) {
            updateColor();
        }
        private void updateColor() {
            car.setType(colorField.getText());
            setDisplayName(car.getColor());
            cdo.setModified(true);
        }
    });

}

public CarDesignTopComponent() {
    initComponents();
}

That's it.

Comments:

Hello Geertjan!
Possible, my question will be not fully related to this topic but, is there some working (e.g. lookup) method to find all Cars in standard (from books) schema? Of course without using some list of cars stored in some car manager.
Standard book schema looks like this:

\*Module ‘Car’ provides interface:
public interface Car {
String getColor();
void setColor (String Color);
}

\*Module ‘Car Honda’ implements interface Car:
@ServiceProvider(service = Car.class)
public class Honda implements Car {
String color = „standard”;
public Honda(){}
public void setColor(String color) {
this.color = color;
}
Public String getColor(){
return color;
}
} and e.g. ‘Car Mercedes’

\*Module ‘Car Manager’ provides interface and 2 metod
public interface Manager {
Car createCar();
void removeCar(String color);
}

\*Module ‘Car Manager 1’ implements above Car Manager interface
@ServiceProvider(service = Manager.class)
public class Manager1 implements Car {
Car car1 = null;
Car car2 = null;
Car car3 = new Honda(); // How to find this?
private List<Car> carList = new ArrayList<Car>();

public Manager1 () {
car1 = createCar(„black”); // Or how to find this?
car2 = createCar(„red”); // Or this?
}

public synchronized Car createCar(String color) {
Car car;
if(color.equals("black")) car = new Mercedes();
else car = new Honda();
C.setColor(color);
carList.add(car);
return car;
}
public synchronized void removeCar (String color) {
//...
}
}
\*Module ‘Car Explorer’ with explorerTopComponent and implementation of ExplorerManager.Provider to display Cars which have been found in this class.
So the question is on the top of this message ;]

Posted by Wojciech M. on January 06, 2011 at 11:47 PM PST #

Post a Comment:
  • HTML Syntax: NOT allowed
About

Geertjan Wielenga (@geertjanw) is a Principal Product Manager in the Oracle Developer Tools group living & working in Amsterdam. He is a Java technology enthusiast, evangelist, trainer, speaker, and writer. He blogs here daily.

The focus of this blog is mostly on NetBeans (a development tool primarily for Java programmers), with an occasional reference to NetBeans, and sometimes diverging to topics relating to NetBeans. And then there are days when NetBeans is mentioned, just for a change.

Search

Archives
« April 2014
SunMonTueWedThuFriSat
  
12
13
14
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today