In this project you will extend your Shapes editor application, in two direction
ID: 663850 • Letter: I
Question
In this project you will extend your Shapes editor application,
in two directions. First you will make your shapes and the
collection class holding them serializable, and second, you
will use the object writer and object reader classes to save
to a file the drawings made using your shapes editor.
For this Project#3 you will add a menu bar to your application.
This menu bar will provide menu options that will:
-allow the shapes editor user to select a file name for
saving a drawing drawn with the editor,
-or allow the user to select a file name for loading a
drawing made with the shapes editor.
-either option will present the user with a file-chooser
component that will allow him to choose a directory
on which to save or load the file, and from which your
program can gather this information.
Object Serialization:
-To save the shapes-based drawing, you will use
object serialization.
-Your shapes and all their data members must
be made serializable. It is recommended that you
serialize at the highest level of abstraction so
that you can simplify store and retrieval, with
the least number of operations.
Object Writers and Readers into files:
-Once you have serializable objects you will
use ObjectReader and ObjectWriter classes to
serialize and store the object/s that you
choose for serialization
Exception Handling:
-Your program must "catch" all exceptions that
may be generated during it's "try" at
instantiating various classes, and must handle
these exceptions at the most specific (sub-class) case
possible.
Users of your new version of the editor should be
able to save their "Drawing"s in any state of completion
and exit the application, then restart the application
and be able to reload a Drawing file and continue their task.
Explanation / Answer
public abstract class ModelElement implements tag IPropertySource, tag Serializable
{
private transient PropertyChangeSupport pcsDelegate =
new PropertyChangeSupport(this);
tag public synchronized void addPropertyChangeListener(PropertyChangeListener l) {
if (l == null) {
throw new IllegalArgumentException();
}
pcsDelegate.addPropertyChangeListener(l);
}
tag protected void firePropertyChange(String property,
Object oldValue,
Object newValue) {
if (pcsDelegate.hasListeners(property)) {
pcsDelegate.firePropertyChange(property, oldValue, newValue);
}
}
tag private void readObject(ObjectInputStream in) throws IOException,
ClassNotFoundException {
in.defaultReadObject();
pcsDelegate = new PropertyChangeSupport(this);
}
tag public synchronized void removePropertyChangeListener(PropertyChangeListener l) {
if (l != null) {
pcsDelegate.removePropertyChangeListener(l);
}
}
}
public abstract class Shape extends ModelElement {
private Point location = new Point(0, 0);
private Dimension size = new Dimension(50, 50);
tag private List sourceConnections = new ArrayList();
tag private List targetConnections = new ArrayList();
public Point getLocation() {
return location.getCopy();
}
public void setLocation(Point newLocation) {
if (newLocation == null) {
throw new IllegalArgumentException();
}
location.setLocation(newLocation);
firePropertyChange(LOCATION_PROP, null, location);
}
tag void addConnection(Connection conn) {
if (conn == null || conn.getSource() == conn.getTarget()) {
throw new IllegalArgumentException();
}
if (conn.getSource() == this) {
sourceConnections.add(conn);
firePropertyChange(SOURCE_CONNECTIONS_PROP, null, conn);
} else if (conn.getTarget() == this) {
targetConnections.add(conn);
firePropertyChange(TARGET_CONNECTIONS_PROP, null, conn);
}
}
tag void removeConnection(Connection conn) {
if (conn == null) {
throw new IllegalArgumentException();
}
if (conn.getSource() == this) {
sourceConnections.remove(conn);
firePropertyChange(SOURCE_CONNECTIONS_PROP, null, conn);
} else if (conn.getTarget() == this) {
targetConnections.remove(conn);
firePropertyChange(TARGET_CONNECTIONS_PROP, null, conn);
}
}
tag public List getSourceConnections() {
return new ArrayList(sourceConnections);
}
tag public List getTargetConnections() {
return new ArrayList(targetConnections);
}
}
public class ShapesDiagram extends ModelElement {
private Collection shapes = new Vector();
tag public boolean addChild(Shape s) {
if (s != null && shapes.add(s)) {
firePropertyChange(CHILD_ADDED_PROP, null, s);
return true;
}
return false;
}
tag public List getChildren() {
return new Vector(shapes);
}
tag public boolean removeChild(Shape s) {
if (s != null && shapes.remove(s)) {
firePropertyChange(CHILD_REMOVED_PROP, null, s);
return true;
}
return false;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.