|
| 1 | +package paintcomponents; |
| 2 | + |
| 3 | +import java.awt.Graphics; |
| 4 | +import java.util.NoSuchElementException; |
| 5 | + |
| 6 | +import painttools.tools.SelectTool; |
| 7 | + |
| 8 | +/** |
| 9 | + * The data display paint component displays the data with a asoociated DataToPoint |
| 10 | + * @author chenzb |
| 11 | + * |
| 12 | + */ |
| 13 | +//TODO THIS class is a copy of DataInputTextfieldPaintComponent class, please consider abstraction |
| 14 | +public class DataDisplayPaintComponent extends DataTextPaintComponent { |
| 15 | + |
| 16 | + private static final int HORIZONTAL_OFFSET = 10; |
| 17 | + private DataToPoint<String> toPoint; |
| 18 | + |
| 19 | + public DataDisplayPaintComponent(String displayingText, int x, int y) { |
| 20 | + super(displayingText, x, y); |
| 21 | + this.toPoint = new DataToPoint<>(x, y); |
| 22 | + } |
| 23 | + |
| 24 | + @Override |
| 25 | + protected void paintSelected(Graphics g) { |
| 26 | + super.paintSelected(g); |
| 27 | + updateFromPointPosition(); |
| 28 | + toPoint.paint(g); |
| 29 | + } |
| 30 | + |
| 31 | + @Override |
| 32 | + protected void paintNotSelected(Graphics g) { |
| 33 | + super.paintNotSelected(g); |
| 34 | + updateFromPointPosition(); |
| 35 | + toPoint.paint(g); |
| 36 | + } |
| 37 | + |
| 38 | + /** |
| 39 | + * This method will use the protected bounds, which will be updated in |
| 40 | + * super.paint[Not]Selected. Make sure you've already invoked super's |
| 41 | + * paintNotSelectedMethod before invoking this one. |
| 42 | + */ |
| 43 | + private void updateFromPointPosition() { |
| 44 | + this.toPoint.setX( |
| 45 | + (int) (getX() - HORIZONTAL_OFFSET)); |
| 46 | + this.toPoint.setY((int) (getY() + this.bounds.getHeight() / 2)); |
| 47 | + } |
| 48 | + |
| 49 | + @Override |
| 50 | + public void translate(int i, int j) { |
| 51 | + super.translate(i, j); |
| 52 | + this.toPoint.translate(i, j); |
| 53 | + } |
| 54 | + |
| 55 | + @Override |
| 56 | + public boolean contains(int x, int y) { |
| 57 | + |
| 58 | + return toPoint.contains(x, y) || super.contains(x, y); |
| 59 | + } |
| 60 | + |
| 61 | + |
| 62 | + @Override |
| 63 | + public void select(SelectTool selectTool) { |
| 64 | + int x = selectTool.getLastMouseEvent().getX(); |
| 65 | + int y = selectTool.getLastMouseEvent().getY(); |
| 66 | + if (toPoint.contains(x, y)) { |
| 67 | + toPoint.select(selectTool); |
| 68 | + } else { |
| 69 | + super.select(selectTool); |
| 70 | + } |
| 71 | + |
| 72 | + } |
| 73 | + |
| 74 | + @Override |
| 75 | + public void deselect(SelectTool selectTool) { |
| 76 | + int x = selectTool.getLastMouseEvent().getX(); |
| 77 | + int y = selectTool.getLastMouseEvent().getY(); |
| 78 | + if (toPoint.contains(x, y)) { |
| 79 | + toPoint.deselect(selectTool); |
| 80 | + } else { |
| 81 | + super.deselect(selectTool); |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + @Override |
| 86 | + public boolean isSelected() { |
| 87 | + //if the from point is selected, this components is considered selected |
| 88 | + if(this.toPoint.isSelected()) return true; |
| 89 | + return super.isSelected(); |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * Update the current display. |
| 94 | + * |
| 95 | + * This class will try to fetch the data from the toPointClass |
| 96 | + * @throws DataFromPointProviderCannotProvideDataException |
| 97 | + * @throws DataFromPointNoDataProviderException |
| 98 | + * @throws NoConnectingLineSegmentException |
| 99 | + * @throws NoSuchElementException |
| 100 | + * @see DataToPoint.fetchData for exception details |
| 101 | + */ |
| 102 | + public void updateDisplayText() throws NoSuchElementException, NoConnectingLineSegmentException, DataFromPointNoDataProviderException, DataFromPointProviderCannotProvideDataException{ |
| 103 | + this.setDisplayingText(toPoint.fetchData()); |
| 104 | + } |
| 105 | + |
| 106 | + |
| 107 | + |
| 108 | +} |
0 commit comments