From fd8a65993b528520df20ff6186d5ed7b5a051d88 Mon Sep 17 00:00:00 2001 From: UltimatePea Date: Mon, 27 Feb 2017 01:16:10 -0800 Subject: [PATCH 1/8] Update isSelectedMethod in DataFromPointDataProvider, update documentation for overriding Paint Component --- src/paintcomponents/DataFromPoint.java | 1 + .../DataInputTextfieldPaintComponent.java | 12 +++++++++++- src/paintcomponents/PaintComponent.java | 4 ++-- 3 files changed, 14 insertions(+), 3 deletions(-) diff --git a/src/paintcomponents/DataFromPoint.java b/src/paintcomponents/DataFromPoint.java index 08ef23f..ce2e0d4 100644 --- a/src/paintcomponents/DataFromPoint.java +++ b/src/paintcomponents/DataFromPoint.java @@ -60,4 +60,5 @@ public void setProvider(DataFromPointDataProvider provider) { this.provider = provider; } + } diff --git a/src/paintcomponents/DataInputTextfieldPaintComponent.java b/src/paintcomponents/DataInputTextfieldPaintComponent.java index 99d0004..0d7397f 100644 --- a/src/paintcomponents/DataInputTextfieldPaintComponent.java +++ b/src/paintcomponents/DataInputTextfieldPaintComponent.java @@ -11,7 +11,7 @@ * */ public class DataInputTextfieldPaintComponent extends DataTextPaintComponent - implements DataFromPointDataProvider { + implements DataFromPointDataProvider{ private static final int HORIZONTAL_OFFSET = 10; private DataFromPoint fromPoint; @@ -98,5 +98,15 @@ public void deselect(SelectTool selectTool) { super.deselect(selectTool); } } + + @Override + public boolean isSelected() { + //if the from point is selected, this components is considered selected + if(this.fromPoint.isSelected()) return true; + return super.isSelected(); + } + + + } diff --git a/src/paintcomponents/PaintComponent.java b/src/paintcomponents/PaintComponent.java index 9452249..2d66dd6 100644 --- a/src/paintcomponents/PaintComponent.java +++ b/src/paintcomponents/PaintComponent.java @@ -11,14 +11,14 @@ * To add functionality to translation: Override translate method if you want to * customize tranlation. * - * To add functionality to selection: Override select and deselect method to + * To add functionality to selection: Override isSelected, select and deselect method to * perform additional selections. * * * You should generally not override paint, toggleSelect as the default * implementation delegates to other methods that you have to. * - * MAKE SURE YOU CALL SUPER when overriding non-abstract methods. + * MAKE SURE YOU CALL SUPER when overriding non-abstract methods, (select, translate) * * @author chenzb * From 9a4ceb5cd5638917ee0ec7365a0c6d999e7c7518 Mon Sep 17 00:00:00 2001 From: UltimatePea Date: Mon, 27 Feb 2017 16:59:58 -0800 Subject: [PATCH 2/8] Add Data Display Paint Component --- .../DataDisplayPaintComponent.java | 112 ++++++++++++++++++ 1 file changed, 112 insertions(+) create mode 100644 src/paintcomponents/DataDisplayPaintComponent.java diff --git a/src/paintcomponents/DataDisplayPaintComponent.java b/src/paintcomponents/DataDisplayPaintComponent.java new file mode 100644 index 0000000..240b9d2 --- /dev/null +++ b/src/paintcomponents/DataDisplayPaintComponent.java @@ -0,0 +1,112 @@ +package paintcomponents; + +import java.awt.Graphics; +import java.util.NoSuchElementException; + +import painttools.tools.SelectTool; + +/** + * The data display paint component displays the data with a asoociated DataToPoint + * @author chenzb + * + */ +//TODO THIS class is a copy of DataInputTextfieldPaintComponent class, please consider abstraction +public class DataDisplayPaintComponent extends DataTextPaintComponent { + + private static final int HORIZONTAL_OFFSET = 10; + private DataToPoint toPoint; + + public DataDisplayPaintComponent(String displayingText, int x, int y) { + super(displayingText, x, y); + this.toPoint = new DataToPoint<>(x, y); + } + + @Override + protected void paintSelected(Graphics g) { + super.paintSelected(g); + updateFromPointPosition(); + toPoint.paint(g); + } + + @Override + protected void paintNotSelected(Graphics g) { + super.paintNotSelected(g); + updateFromPointPosition(); + toPoint.paint(g); + } + + /** + * This method will use the protected bounds, which will be updated in + * super.paint[Not]Selected. Make sure you've already invoked super's + * paintNotSelectedMethod before invoking this one. + */ + private void updateFromPointPosition() { + this.toPoint.setX( + (int) (getX() - HORIZONTAL_OFFSET)); + this.toPoint.setY((int) (getY() + this.bounds.getHeight() / 2)); + } + + @Override + public void translate(int i, int j) { + super.translate(i, j); + this.toPoint.translate(i, j); + } + + @Override + public boolean contains(int x, int y) { + + return toPoint.contains(x, y) || super.contains(x, y); + } + + public void inputData(String s) { + this.setDisplayingText(s); + } + + + @Override + public void select(SelectTool selectTool) { + int x = selectTool.getLastMouseEvent().getX(); + int y = selectTool.getLastMouseEvent().getY(); + if (toPoint.contains(x, y)) { + toPoint.select(selectTool); + } else { + super.select(selectTool); + } + + } + + @Override + public void deselect(SelectTool selectTool) { + int x = selectTool.getLastMouseEvent().getX(); + int y = selectTool.getLastMouseEvent().getY(); + if (toPoint.contains(x, y)) { + toPoint.deselect(selectTool); + } else { + super.deselect(selectTool); + } + } + + @Override + public boolean isSelected() { + //if the from point is selected, this components is considered selected + if(this.toPoint.isSelected()) return true; + return super.isSelected(); + } + + /** + * Update the current display. + * + * This class will try to fetch the data from the toPointClass + * @throws DataFromPointProviderCannotProvideDataException + * @throws DataFromPointNoDataProviderException + * @throws NoConnectingLineSegmentException + * @throws NoSuchElementException + * @see DataToPoint.fetchData for exception details + */ + public void displayText() throws NoSuchElementException, NoConnectingLineSegmentException, DataFromPointNoDataProviderException, DataFromPointProviderCannotProvideDataException{ + this.setDisplayingText(toPoint.fetchData()); + } + + + +} From 28ec1be210199d93612b7630fe66be5d35e7d9ae Mon Sep 17 00:00:00 2001 From: UltimatePea Date: Mon, 27 Feb 2017 17:47:22 -0800 Subject: [PATCH 3/8] Update Documentation for Paint Action --- src/actions/PaintAction.java | 36 +++++++++++++++++++++++++++++++----- 1 file changed, 31 insertions(+), 5 deletions(-) diff --git a/src/actions/PaintAction.java b/src/actions/PaintAction.java index 2039811..91a3a21 100644 --- a/src/actions/PaintAction.java +++ b/src/actions/PaintAction.java @@ -2,21 +2,47 @@ import ui.PaintPanel; +/** + * Paint Action abstracts a particular menu action + * + * Override this class to create a new Action. + * + * Perfrom action will only be called when can perform action returns true. + * + * Most of the cases, you have to call panel.repaint() as the last statement of + * perform action + * + * @author chenzb + * + */ public abstract class PaintAction { - + protected PaintPanel panel; - public PaintAction(PaintPanel panel){ + public PaintAction(PaintPanel panel) { this.panel = panel; } - + + /** + * Whether this action can perform. Subclasses generally base the return + * value on the current selection on screen + * panel.getSelectTool().getSelectedComponents + * + * @return true if the action can be performed + */ public abstract boolean canPerformAction(); + /** - * Performs this action - * Subclassess must invoke panel.repaint if the action changes the panel + * Performs this action Subclassess must invoke panel.repaint if the action + * changes the panel */ public abstract void performAction(); + /** + * The location of this item in the menu bar. + * For example, "File/Save As..", "File/Open Recent/Clear Menu" + * @return + */ public abstract String locationString(); } From 00860bb8a3f5f4282fc8a15bd88cb2143b648c3b Mon Sep 17 00:00:00 2001 From: UltimatePea Date: Mon, 27 Feb 2017 17:49:16 -0800 Subject: [PATCH 4/8] Abstract ConstructLineSegment by providing subclasses addLineSegment method to add a line segment --- src/actions/ConstructLineSegmentAction.java | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/src/actions/ConstructLineSegmentAction.java b/src/actions/ConstructLineSegmentAction.java index 78b6162..c7030a7 100644 --- a/src/actions/ConstructLineSegmentAction.java +++ b/src/actions/ConstructLineSegmentAction.java @@ -59,6 +59,18 @@ public void performAction() { //construct line segment LineSegment lineSegment = new LineSegment((SimplePoint)(items.get(0)), (SimplePoint)(items.get(1))); + + + addLineSegment(lineSegment); + } + /** + * This method updates the panel's list of paint components and selection after a line segment is added + * Subclasses should call this method to update the panel when customizing the addition of a line segment + * + * @param lineSegment the lineSegment to be added to the painting panel + */ + protected void addLineSegment(LineSegment lineSegment) { + //add to panel panel.addPaintComponent(lineSegment); @@ -66,8 +78,6 @@ public void performAction() { panel.getSelectTool().clearSelection(); panel.getSelectTool().selectComponent(lineSegment); panel.repaint(); - - } @Override From 585d342620dd33631c49b51ce80861c621ff4a8e Mon Sep 17 00:00:00 2001 From: UltimatePea Date: Mon, 27 Feb 2017 17:50:50 -0800 Subject: [PATCH 5/8] Update DataDisplayPaintComponent delete copied inputData method change name of displayText to UpdateDisplayText --- src/paintcomponents/DataDisplayPaintComponent.java | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/src/paintcomponents/DataDisplayPaintComponent.java b/src/paintcomponents/DataDisplayPaintComponent.java index 240b9d2..02e4d38 100644 --- a/src/paintcomponents/DataDisplayPaintComponent.java +++ b/src/paintcomponents/DataDisplayPaintComponent.java @@ -58,10 +58,6 @@ public boolean contains(int x, int y) { return toPoint.contains(x, y) || super.contains(x, y); } - public void inputData(String s) { - this.setDisplayingText(s); - } - @Override public void select(SelectTool selectTool) { @@ -103,7 +99,7 @@ public boolean isSelected() { * @throws NoSuchElementException * @see DataToPoint.fetchData for exception details */ - public void displayText() throws NoSuchElementException, NoConnectingLineSegmentException, DataFromPointNoDataProviderException, DataFromPointProviderCannotProvideDataException{ + public void updateDisplayText() throws NoSuchElementException, NoConnectingLineSegmentException, DataFromPointNoDataProviderException, DataFromPointProviderCannotProvideDataException{ this.setDisplayingText(toPoint.fetchData()); } From a571783a2126fb305521984cfc9fd535e0038b1b Mon Sep 17 00:00:00 2001 From: UltimatePea Date: Mon, 27 Feb 2017 17:51:39 -0800 Subject: [PATCH 6/8] Fix a bug where canProvideInformationToDataFromPoint returns the opposite result --- src/paintcomponents/DataInputTextfieldPaintComponent.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/paintcomponents/DataInputTextfieldPaintComponent.java b/src/paintcomponents/DataInputTextfieldPaintComponent.java index 0d7397f..88d271d 100644 --- a/src/paintcomponents/DataInputTextfieldPaintComponent.java +++ b/src/paintcomponents/DataInputTextfieldPaintComponent.java @@ -73,7 +73,7 @@ public String provideInformationToDataFromPoint( @Override public boolean canProvideInformationToDataFromPoint( DataFromPoint dataFromPoint) { - return displayingText == null; + return displayingText != null; } @Override From 31ddb7ca5ae9776ed1d6570feba1699066d12a4f Mon Sep 17 00:00:00 2001 From: UltimatePea Date: Mon, 27 Feb 2017 17:52:38 -0800 Subject: [PATCH 7/8] Fix a bug where the fromPoint and toPoint's line segment is not set when constructing the DataLineSegment --- src/paintcomponents/DataLineSegment.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/paintcomponents/DataLineSegment.java b/src/paintcomponents/DataLineSegment.java index 9991608..431653e 100644 --- a/src/paintcomponents/DataLineSegment.java +++ b/src/paintcomponents/DataLineSegment.java @@ -4,6 +4,8 @@ public class DataLineSegment extends LineSegment { public DataLineSegment(DataFromPoint fromPoint, DataToPoint toPoint) { super(fromPoint, toPoint); + fromPoint.setLineSegment(this); + toPoint.setLineSegment(this); } From 0721af8ffd3792b398c1a2eb0c41e808613b629e Mon Sep 17 00:00:00 2001 From: UltimatePea Date: Mon, 27 Feb 2017 17:53:53 -0800 Subject: [PATCH 8/8] Add three actions, AddDataDisplayBoxAction, ConstructDataLienSegmentAction, UpdateDataDisplayBoxAction, update the ActionMenuBar --- src/actions/AddDataDisplayBoxAction.java | 29 +++++++++++ .../ConstructDataLineSegmentAction.java | 49 +++++++++++++++++++ src/actions/UpdateDataDisplayBoxAction.java | 49 +++++++++++++++++++ src/actions/menu/ActionsMenuBar.java | 10 ++++ 4 files changed, 137 insertions(+) create mode 100644 src/actions/AddDataDisplayBoxAction.java create mode 100644 src/actions/ConstructDataLineSegmentAction.java create mode 100644 src/actions/UpdateDataDisplayBoxAction.java diff --git a/src/actions/AddDataDisplayBoxAction.java b/src/actions/AddDataDisplayBoxAction.java new file mode 100644 index 0000000..9c29858 --- /dev/null +++ b/src/actions/AddDataDisplayBoxAction.java @@ -0,0 +1,29 @@ +package actions; + +import paintcomponents.DataDisplayPaintComponent; +import ui.PaintPanel; + +public class AddDataDisplayBoxAction extends PaintAction { + + public AddDataDisplayBoxAction(PaintPanel panel) { + super(panel); + } + + @Override + public boolean canPerformAction() { + return true; + } + + @Override + public void performAction() { + DataDisplayPaintComponent comp = new DataDisplayPaintComponent("Data Display", panel.getWidth() /2, panel.getHeight()/2); + panel.addPaintComponent(comp); + panel.repaint(); + } + + @Override + public String locationString() { + return "Add/Data Display"; + } + +} diff --git a/src/actions/ConstructDataLineSegmentAction.java b/src/actions/ConstructDataLineSegmentAction.java new file mode 100644 index 0000000..3b0da13 --- /dev/null +++ b/src/actions/ConstructDataLineSegmentAction.java @@ -0,0 +1,49 @@ +package actions; + +import java.util.ArrayList; + +import paintcomponents.DataFromPoint; +import paintcomponents.DataLineSegment; +import paintcomponents.DataToPoint; +import paintcomponents.PaintComponent; +import ui.PaintPanel; + +public class ConstructDataLineSegmentAction extends ConstructLineSegmentAction { + + public ConstructDataLineSegmentAction(PaintPanel panel) { + super(panel); + // TODO Auto-generated constructor stub + } + + @Override + public boolean canPerformAction() { + if( super.canPerformAction() == false) return false; + //we must connect from a DataFromPoint to DataToPoint + //assume ConstructLineSegment is doing correctly, there is two corrently selected points + ArrayList comps = this.panel.getSelectTool().getSelectedComponents(); + //TODO IMPORTANT Generic Argument is erased, may cause unexpected behavior when types dont match in the future + if(comps.get(0) instanceof DataFromPoint && comps.get(1) instanceof DataToPoint){ + //allow connection only when no segment has no existing connections to the data + if(((DataToPoint)comps.get(1)).getLineSegment() == null){ + return true; + } + } + return false; + } + + @Override + public void performAction() { + + ArrayList comps = this.panel.getSelectTool().getSelectedComponents(); + @SuppressWarnings("rawtypes") + DataLineSegment seg = new DataLineSegment((DataFromPoint)comps.get(0), (DataToPoint)comps.get(1)); + addLineSegment(seg); + } + + @Override + public String locationString() { + // TODO Auto-generated method stub + return "Data/Construct/Line Segment"; + } + +} diff --git a/src/actions/UpdateDataDisplayBoxAction.java b/src/actions/UpdateDataDisplayBoxAction.java new file mode 100644 index 0000000..6ec710f --- /dev/null +++ b/src/actions/UpdateDataDisplayBoxAction.java @@ -0,0 +1,49 @@ +package actions; + +import java.util.NoSuchElementException; + +import javax.swing.JOptionPane; + +import paintcomponents.DataDisplayPaintComponent; +import paintcomponents.DataFromPointNoDataProviderException; +import paintcomponents.DataFromPointProviderCannotProvideDataException; +import paintcomponents.NoConnectingLineSegmentException; +import ui.PaintPanel; + +public class UpdateDataDisplayBoxAction extends PaintAction { + + public UpdateDataDisplayBoxAction(PaintPanel panel) { + super(panel); + } + + @Override + public boolean canPerformAction() { + if(panel.getSelectTool().getSelectedComponents().size() == 1){ + if(panel.getSelectTool().getSelectedComponents().get(0) instanceof DataDisplayPaintComponent){ + return true; + } + } + return false; + } + + @Override + public void performAction() { + DataDisplayPaintComponent comp = (DataDisplayPaintComponent) panel.getSelectTool().getSelectedComponents().get(0) ; + try { + comp.updateDisplayText(); + panel.repaint(); + } catch (NoSuchElementException | NoConnectingLineSegmentException + | DataFromPointNoDataProviderException + | DataFromPointProviderCannotProvideDataException e) { + e.printStackTrace(); + JOptionPane.showMessageDialog(panel, e.toString()); + } + + } + + @Override + public String locationString() { + return "Data/Display Box/Update"; + } + +} diff --git a/src/actions/menu/ActionsMenuBar.java b/src/actions/menu/ActionsMenuBar.java index 7c13d6f..3aa3c4d 100644 --- a/src/actions/menu/ActionsMenuBar.java +++ b/src/actions/menu/ActionsMenuBar.java @@ -9,12 +9,15 @@ import painttools.tools.SelectionToolListener; import ui.PaintPanel; +import actions.AddDataDisplayBoxAction; import actions.AddDataInputBoxAction; import actions.AddTextBoxAction; +import actions.ConstructDataLineSegmentAction; import actions.ConstructLineSegmentAction; import actions.GeneratePolygonSourceJava; import actions.InputDataForDataInputBoxAction; import actions.PaintAction; +import actions.UpdateDataDisplayBoxAction; public class ActionsMenuBar extends JMenuBar implements SelectionToolListener{ @@ -24,6 +27,13 @@ public ActionsMenuBar(PaintPanel panel){ addAction(new AddTextBoxAction(panel)); addAction(new AddDataInputBoxAction(panel)); addAction(new InputDataForDataInputBoxAction(panel)); + + //data display + addAction(new AddDataDisplayBoxAction(panel)); + addAction(new UpdateDataDisplayBoxAction(panel)); + + //data segments + addAction(new ConstructDataLineSegmentAction(panel)); }