Skip to content

Commit

Permalink
Merge pull request #9 from UCSDOalads/addIOComponentsbyUltimatePea
Browse files Browse the repository at this point in the history
Add io componentsby ultimate pea, Complete DataInput and DataDisplay PaintComponent
  • Loading branch information
Ultimate Pea authored Feb 28, 2017
2 parents 7355018 + 0721af8 commit 491ee77
Show file tree
Hide file tree
Showing 11 changed files with 305 additions and 11 deletions.
29 changes: 29 additions & 0 deletions src/actions/AddDataDisplayBoxAction.java
Original file line number Diff line number Diff line change
@@ -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";
}

}
49 changes: 49 additions & 0 deletions src/actions/ConstructDataLineSegmentAction.java
Original file line number Diff line number Diff line change
@@ -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<PaintComponent> 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<PaintComponent> 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";
}

}
14 changes: 12 additions & 2 deletions src/actions/ConstructLineSegmentAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,25 @@ 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);

//change selection
panel.getSelectTool().clearSelection();
panel.getSelectTool().selectComponent(lineSegment);
panel.repaint();


}

@Override
Expand Down
36 changes: 31 additions & 5 deletions src/actions/PaintAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
* <code>panel.getSelectTool().getSelectedComponents</code>
*
* @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();

}
49 changes: 49 additions & 0 deletions src/actions/UpdateDataDisplayBoxAction.java
Original file line number Diff line number Diff line change
@@ -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";
}

}
10 changes: 10 additions & 0 deletions src/actions/menu/ActionsMenuBar.java
Original file line number Diff line number Diff line change
Expand Up @@ -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{

Expand All @@ -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));

}

Expand Down
108 changes: 108 additions & 0 deletions src/paintcomponents/DataDisplayPaintComponent.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
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<String> 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);
}


@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 updateDisplayText() throws NoSuchElementException, NoConnectingLineSegmentException, DataFromPointNoDataProviderException, DataFromPointProviderCannotProvideDataException{
this.setDisplayingText(toPoint.fetchData());
}



}
1 change: 1 addition & 0 deletions src/paintcomponents/DataFromPoint.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,5 @@ public void setProvider(DataFromPointDataProvider<T> provider) {
this.provider = provider;
}


}
14 changes: 12 additions & 2 deletions src/paintcomponents/DataInputTextfieldPaintComponent.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
*
*/
public class DataInputTextfieldPaintComponent extends DataTextPaintComponent
implements DataFromPointDataProvider<String> {
implements DataFromPointDataProvider<String>{

private static final int HORIZONTAL_OFFSET = 10;
private DataFromPoint<String> fromPoint;
Expand Down Expand Up @@ -73,7 +73,7 @@ public String provideInformationToDataFromPoint(
@Override
public boolean canProvideInformationToDataFromPoint(
DataFromPoint<String> dataFromPoint) {
return displayingText == null;
return displayingText != null;
}

@Override
Expand All @@ -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();
}




}
2 changes: 2 additions & 0 deletions src/paintcomponents/DataLineSegment.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ public class DataLineSegment<T> extends LineSegment {

public DataLineSegment(DataFromPoint<T> fromPoint, DataToPoint<T> toPoint) {
super(fromPoint, toPoint);
fromPoint.setLineSegment(this);
toPoint.setLineSegment(this);

}

Expand Down
Loading

0 comments on commit 491ee77

Please sign in to comment.