diff options
author | wisberg <wisberg> | 2002-12-16 17:58:19 +0000 |
---|---|---|
committer | wisberg <wisberg> | 2002-12-16 17:58:19 +0000 |
commit | d842c4f1139629c1f062b74ba818d233b2c31043 (patch) | |
tree | 842d3871620bc0eb60edcd95e55804d67e0f61fa /ajde/testdata/examples/figures-demo | |
parent | 3ce247199704eae6b2c92c6e38c69584e3250c52 (diff) | |
download | aspectj-d842c4f1139629c1f062b74ba818d233b2c31043.tar.gz aspectj-d842c4f1139629c1f062b74ba818d233b2c31043.zip |
initial version
Diffstat (limited to 'ajde/testdata/examples/figures-demo')
15 files changed, 725 insertions, 0 deletions
diff --git a/ajde/testdata/examples/figures-demo/.cvsignore b/ajde/testdata/examples/figures-demo/.cvsignore new file mode 100644 index 000000000..ad69a1aa9 --- /dev/null +++ b/ajde/testdata/examples/figures-demo/.cvsignore @@ -0,0 +1 @@ +figures-demo-all.ajsym diff --git a/ajde/testdata/examples/figures-demo/figures/Box.java b/ajde/testdata/examples/figures-demo/figures/Box.java new file mode 100644 index 000000000..4db7f439d --- /dev/null +++ b/ajde/testdata/examples/figures-demo/figures/Box.java @@ -0,0 +1,55 @@ +/* +Copyright (c) 2002 Palo Alto Research Center Incorporated. All Rights Reserved. + */ + +package figures; + +import java.awt.*; +import java.awt.geom.*; + +public class Box extends ShapeFigureElement { + private Point _p0; + private Point _p1; + private Point _p2; + private Point _p3; + + public Box(int x0, int y0, int width, int height) { + _p0 = new Point(x0, y0); + _p1 = new Point(x0+width, y0); + _p2 = new Point(x0+width, y0+height); + _p3 = new Point(x0, y0+height); + } + + public Point getP0() { return _p0; } + public Point getP1() { return _p1; } + public Point getP2() { return _p2; } + public Point getP3() { return _p3; } + + public void move(int dx, int dy) { + _p0.move(dx, dy); + _p1.move(dx, dy); + _p2.move(dx, dy); + _p3.move(dx, dy); + } + + public void checkBoxness() { + if ((_p0.getX() == _p3.getX()) && + (_p1.getX() == _p2.getX()) && + (_p0.getY() == _p1.getY()) && + (_p2.getY() == _p3.getY())) + return; + throw new IllegalStateException("This is not a square."); + } + + public String toString() { + return "Box(" + _p0 + ", " + _p1 + ", " + _p2 + ", " + _p3 + ")"; + } + + public Shape getShape() { + return new Rectangle(getP1().getX(), + getP1().getY(), + getP3().getX() - getP1().getX(), + getP3().getY() - getP1().getY()); + } +} + diff --git a/ajde/testdata/examples/figures-demo/figures/Canvas.java b/ajde/testdata/examples/figures-demo/figures/Canvas.java new file mode 100644 index 000000000..e5491d7cb --- /dev/null +++ b/ajde/testdata/examples/figures-demo/figures/Canvas.java @@ -0,0 +1,11 @@ +/* +Copyright (c) 2002 Palo Alto Research Center Incorporated. All Rights Reserved. + */ + +package figures; + +public class Canvas { + public static void updateHistory() { + // not implemented + } +} diff --git a/ajde/testdata/examples/figures-demo/figures/ColorControl.java b/ajde/testdata/examples/figures-demo/figures/ColorControl.java new file mode 100644 index 000000000..46d1ba428 --- /dev/null +++ b/ajde/testdata/examples/figures-demo/figures/ColorControl.java @@ -0,0 +1,20 @@ +/* +Copyright (c) 2002 Palo Alto Research Center Incorporated. All Rights Reserved. + */ + +package figures; + +import java.awt.Color; +import figures.FigureElement; + +public aspect ColorControl { + public static void setFillColor(FigureElement fe, Color color) { + // fill in here + } + + public static void setLineColor(FigureElement fe, Color color) { + // fill in here + } + + // fill in here +} diff --git a/ajde/testdata/examples/figures-demo/figures/FigureElement.java b/ajde/testdata/examples/figures-demo/figures/FigureElement.java new file mode 100644 index 000000000..ae06c132b --- /dev/null +++ b/ajde/testdata/examples/figures-demo/figures/FigureElement.java @@ -0,0 +1,21 @@ +/* +Copyright (c) 2001-2002 Palo Alto Research Center Incorporated. All Rights Reserved. + */ + +package figures; + +import java.awt.*; +import java.awt.geom.*; + +public interface FigureElement { + public static final int MIN_VALUE = 0; + public static final int MAX_VALUE = 500; + + public abstract void move(int dx, int dy); + + public abstract Rectangle getBounds(); + + public abstract boolean contains(Point2D p); + + public abstract void paint(Graphics2D g2); +} diff --git a/ajde/testdata/examples/figures-demo/figures/Group.java b/ajde/testdata/examples/figures-demo/figures/Group.java new file mode 100644 index 000000000..59c1a17cf --- /dev/null +++ b/ajde/testdata/examples/figures-demo/figures/Group.java @@ -0,0 +1,88 @@ +/* +Copyright (c) 2002 Palo Alto Research Center Incorporated. All Rights Reserved. + */ + +package figures; + +import java.util.*; +import java.awt.*; +import java.awt.geom.*; + +public class Group implements FigureElement { + private Collection _members; + private String _identifier; + + public Group(FigureElement first) { + this._members = new ArrayList(); + add(first); + } + + public void add(FigureElement fe) { + _members.add(fe); + } + + public Iterator members() { + return _members.iterator(); + } + + public void move(int dx, int dy) { + for (Iterator i = _members.iterator(); i.hasNext(); ) { + FigureElement fe = (FigureElement)i.next(); + fe.move(dx, dy); + } + } + + public void resetIdentifier(String identifier) { + resetIdentifier(identifier); + } + + public String toString() { + if (_identifier != null) { + return _identifier; + } + + StringBuffer buf = new StringBuffer("Group("); + for (Iterator i = _members.iterator(); i.hasNext(); ) { + buf.append(i.next().toString()); + if (i.hasNext()) { + buf.append(", "); + } + } + buf.append(")"); + return buf.toString(); + } + + public Rectangle getBounds() { + Rectangle previous = null; + for (Iterator i = _members.iterator(); i.hasNext(); ) { + FigureElement fe = (FigureElement)i.next(); + Rectangle rect = fe.getBounds(); + if (previous != null) { + previous = previous.union(rect); + } else { + previous = rect; + } + } + return previous; + } + + public boolean contains(Point2D p) { + for (Iterator i = _members.iterator(); i.hasNext(); ) { + FigureElement fe = (FigureElement)i.next(); + if (fe.contains(p)) return true; + } + return false; + } + + public void paint(Graphics2D g2) { + for (Iterator i = _members.iterator(); i.hasNext(); ) { + FigureElement fe = (FigureElement)i.next(); + fe.paint(g2); + } + } + + public int size() { + return _members.size(); + } +} + diff --git a/ajde/testdata/examples/figures-demo/figures/Line.java b/ajde/testdata/examples/figures-demo/figures/Line.java new file mode 100644 index 000000000..45324646e --- /dev/null +++ b/ajde/testdata/examples/figures-demo/figures/Line.java @@ -0,0 +1,73 @@ +/* +Copyright (c) 2001-2002 Palo Alto Research Center Incorporated. All Rights Reserved. + */ + +package figures; + +import java.awt.*; +import java.awt.geom.*; + +public class Line extends ShapeFigureElement { + private Point _p1; + private Point _p2; + + public Line(Point p1, Point p2) { + _p1 = p1; + _p2 = p2; + } + + public Point getP1() { + return _p1; + } + + public void setP1(Point p1) { + _p1 = p1; + Canvas.updateHistory(); + } + + public Point getP2() { + return _p2; + } + + public void setP2(Point p2) { + _p2 = p2; + Canvas.updateHistory(); + } + + public void move(int dx, int dy) { + //_x = dx; + //_y = dy; + + //_p1.move(dx, dy); + //_p2.move(dx, dy); + } + + public String toString() { + return "Line(" + _p1 + ", " + _p2 + ")"; + } + + /** + * Used to determine if this line {@link contains(Point2D)} a point. + */ + final static int THRESHHOLD = 5; + + /** + * Returns <code>true</code> if the point segment distance is less than + * {@link THRESHHOLD}. + */ + public boolean contains(Point2D p) { + return getLine2D().ptLineDist(p) < THRESHHOLD; + } + + private Line2D getLine2D() { + return new Line2D.Float((float)getP1().getX(), + (float)getP1().getY(), + (float)getP2().getX(), + (float)getP2().getY()); + } + + public Shape getShape() { + return getLine2D(); + } +} + diff --git a/ajde/testdata/examples/figures-demo/figures/Point.java b/ajde/testdata/examples/figures-demo/figures/Point.java new file mode 100644 index 000000000..e8783a560 --- /dev/null +++ b/ajde/testdata/examples/figures-demo/figures/Point.java @@ -0,0 +1,59 @@ +/* +Copyright (c) 2001-2002 Palo Alto Research Center Incorporated. All Rights Reserved. + */ + +package figures; + +import java.awt.*; +import java.awt.geom.*; + +public class Point extends ShapeFigureElement { + private int _x; + private int _y; + + public Point(int x, int y) { + _x = x; + _y = y; + } + + public int getX() { + return _x; + } + + public void setX(int x) { + _x = x; + //Canvas.updateHistory(); + } + + public int getY() { + return _y; + } + + public void setY(int y) { + _y = y; + //Canvas.updateHistory(); + } + + public void move(int dx, int dy) { + setX(_x + dx); + setY(_y + dy); + } + + public String toString() { + return "Point(" + _x + ", " + _y + ")"; + } + + /** The height of displayed {@link Point}s. */ + private final static int HEIGHT = 10; + + /** The width of displayed {@link Point}s. -- same as {@link HEIGHT}. */ + private final static int WIDTH = Point.HEIGHT; + + public Shape getShape() { + return new Ellipse2D.Float((float)getX()-Point.WIDTH/2, + (float)getY()-Point.HEIGHT/2, + (float)Point.HEIGHT, + (float)Point.WIDTH); + } +} + diff --git a/ajde/testdata/examples/figures-demo/figures/ShapeFigureElement.java b/ajde/testdata/examples/figures-demo/figures/ShapeFigureElement.java new file mode 100644 index 000000000..29a4a89ad --- /dev/null +++ b/ajde/testdata/examples/figures-demo/figures/ShapeFigureElement.java @@ -0,0 +1,38 @@ +/* +Copyright (c) 2002 Palo Alto Research Center Incorporated. All Rights Reserved. + */ + +package figures; + +import java.awt.*; +import java.awt.geom.*; + +public abstract class ShapeFigureElement implements FigureElement { + public abstract void move(int dx, int dy); + + public abstract Shape getShape(); + + public Rectangle getBounds() { + return getShape().getBounds(); + } + + public boolean contains(Point2D p) { + return getShape().contains(p); + } + + public Color getLineColor() { + return Color.black; + } + + public Color getFillColor() { + return Color.red; + } + + public final void paint(Graphics2D g2) { + Shape shape = getShape(); + g2.setPaint(getFillColor()); + g2.fill(shape); + g2.setPaint(getLineColor()); + g2.draw(shape); + } +} diff --git a/ajde/testdata/examples/figures-demo/figures/SlothfulPoint.java b/ajde/testdata/examples/figures-demo/figures/SlothfulPoint.java new file mode 100644 index 000000000..35c8fb635 --- /dev/null +++ b/ajde/testdata/examples/figures-demo/figures/SlothfulPoint.java @@ -0,0 +1,42 @@ +/* +Copyright (c) 2002 Palo Alto Research Center Incorporated. All Rights Reserved. + */ + +package figures; + +import java.awt.*; +import java.awt.geom.*; + +/** + * This class makes mistakes to be caught by invariant checkers. + */ +public class SlothfulPoint extends ShapeFigureElement { + private int _x; + private int _y; + + public SlothfulPoint(int x, int y) { + } + + public void setX(int x) { + _x = x; + } + + public void setY(int y) { + _y = y; + } + + public void move(int dx, int dy) { + //_x += dx; + //_y += dy; + } + + public String toString() { + return "SlothfulPoint"; + } + + public Shape getShape() { + return new Ellipse2D.Float((float)_x, + (float)_y, 1.0f, 1.0f); + } +} + diff --git a/ajde/testdata/examples/figures-demo/figures/gui/FigurePanel.java b/ajde/testdata/examples/figures-demo/figures/gui/FigurePanel.java new file mode 100644 index 000000000..8e777c3df --- /dev/null +++ b/ajde/testdata/examples/figures-demo/figures/gui/FigurePanel.java @@ -0,0 +1,172 @@ +/* +Copyright (c) 2001-2002 Palo Alto Research Center Incorporated. All Rights Reserved. + */ + +package figures.gui; + +import figures.Point; +import figures.Line; +import figures.FigureElement; +import figures.Group; + + +import java.awt.*; +import java.awt.geom.*; +import java.awt.event.*; +import java.io.*; +import java.util.*; +import javax.swing.*; +import javax.swing.text.*; +import javax.swing.border.*; + +public class FigurePanel extends JComponent { + + ButtonsPanel bp = new ButtonsPanel(); + FigureSurface fs = new FigureSurface(); + ConsolePanel cp = new ConsolePanel(); + + + public FigurePanel() { + setLayout(new BorderLayout()); + JPanel panel = new JPanel(); + panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS)); + panel.add(fs); + panel.add(bp); + JSplitPane sp = new JSplitPane(JSplitPane.VERTICAL_SPLIT, panel, cp); + sp.setPreferredSize(new Dimension(500, 400)); + sp.setDividerLocation(250); + add(BorderLayout.CENTER, sp); + } + + class ButtonsPanel extends JPanel { + private JLabel msgs = new JLabel("click to add a point or line"); + public ButtonsPanel() { + setLayout(new FlowLayout(FlowLayout.LEFT)); + add(new JButton(new AbstractAction("Main") { + public void actionPerformed(ActionEvent e) { + Main.main(new String[]{}); + fs.repaint(); + } + })); + add(msgs); + } + + public void log(String msg) { + msgs.setText(msg); + } + } + + static class ConsolePanel extends JPanel { + + JTextArea text = new JTextArea(); + + public ConsolePanel() { + super(new BorderLayout()); + text.setFont(StyleContext.getDefaultStyleContext().getFont("SansSerif", Font.PLAIN, 10)); + JScrollPane scroller = new JScrollPane(text); + scroller.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); + add(BorderLayout.CENTER, scroller); + } + + public void println(String msg) { + text.append(msg + '\n'); + } + } + + final static Color BACKGROUND = Color.white; + + static class FigureSurface extends JPanel implements MouseListener, MouseMotionListener { + private Group canvas; + + public FigureSurface() { + canvas = new Group(new Point(250, 250)); + addMouseMotionListener(this); + addMouseListener(this); + setPreferredSize(new Dimension(500,500)); + } + + private Point addPoint(int x, int y) { + Point p = new Point(x, y); + canvas.add(p); + repaint(); + return p; + } + + private Line addLine(Point p1, Point p2) { + if (Math.abs(p1.getX()-p2.getX()) < 5 || + Math.abs(p1.getY()-p2.getY()) < 5) { + return null; + } + + Line line = null; + if (p1 != null && p2 != null) { + line = new Line(p1, p2); + canvas.add(line); + } + repaint(); + return line; + } + + public void paint(Graphics g) { + Graphics2D g2 = (Graphics2D) g; + g2.setPaint(BACKGROUND); + g2.fill(new Rectangle2D.Float(0f, 0f, (float)g2.getClipBounds().width, (float)g2.getClipBounds().height)); + g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); + canvas.paint(g2); + } + + + int lastX, lastY; + int pressX, pressY; + + FigureElement first = null; + Point point1 = null; + + public void mousePressed(MouseEvent e){ + int x = e.getX(), y = e.getY(); + pressX = lastX = x; pressY = lastY = y; + first = findFigureElement(x, y); + if (first == null) { + point1 = addPoint(x, y); + } + } + + public void mouseDragged(MouseEvent e) { + int x = e.getX(), y = e.getY(), dx = lastX-x, dy = lastY-y; + lastX = x; + lastY = y; + if (first == null) { + Line line = addLine(point1, new Point(x, y)); + if (line != null) { + canvas.add(line.getP2()); + first = line.getP2(); + canvas.add(line); + } + } else { + first.move(-dx, -dy); + } + repaint(); + } + + public void mouseReleased(MouseEvent e){ + mouseDragged(e); + first = null; + point1 = null; + } + + + public void mouseMoved(MouseEvent e){} + public void mouseClicked(MouseEvent e){} + public void mouseExited(MouseEvent e){} + public void mouseEntered(MouseEvent e){} + + private FigureElement findFigureElement(int x, int y) { + Point2D p = new Point2D.Float((float)x, (float)y); + for (Iterator i = canvas.members(); i.hasNext(); ) { + FigureElement fe = (FigureElement)i.next(); + if (fe.contains(p)) return fe; + } + return null; + } + } +} diff --git a/ajde/testdata/examples/figures-demo/figures/gui/LogAdapter.java b/ajde/testdata/examples/figures-demo/figures/gui/LogAdapter.java new file mode 100644 index 000000000..078ab01cb --- /dev/null +++ b/ajde/testdata/examples/figures-demo/figures/gui/LogAdapter.java @@ -0,0 +1,16 @@ +/* +Copyright (c) 2001-2002 Palo Alto Research Center Incorporated. All Rights Reserved. + */ + +package figures.gui; + +import support.Log; + +aspect LogAdapter { + + before(String s): call(void Log.log(String)) && args(s) { + if (Main.panel != null) { + Main.panel.cp.println(s); + } + } +} diff --git a/ajde/testdata/examples/figures-demo/figures/gui/Main.java b/ajde/testdata/examples/figures-demo/figures/gui/Main.java new file mode 100644 index 000000000..15fac91a0 --- /dev/null +++ b/ajde/testdata/examples/figures-demo/figures/gui/Main.java @@ -0,0 +1,26 @@ +/* +Copyright (c) 2001-2002 Palo Alto Research Center Incorporated. All Rights Reserved. + */ + +package figures.gui; + +import javax.swing.*; +import support.Log; +import figures.Point; + +public class Main { + static FigurePanel panel; + + public static void main(String[] args) { + JFrame figureFrame = new JFrame("Figure Editor"); + panel = new FigurePanel(); + figureFrame.setContentPane(panel); + figureFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + figureFrame.pack(); + figureFrame.setVisible(true); + + // for testing--remove! + //Point p = new Point(0, 0); + //p.setX(-10); + } +} diff --git a/ajde/testdata/examples/figures-demo/figures/support/Enforcement.java b/ajde/testdata/examples/figures-demo/figures/support/Enforcement.java new file mode 100644 index 000000000..a7c806ac6 --- /dev/null +++ b/ajde/testdata/examples/figures-demo/figures/support/Enforcement.java @@ -0,0 +1,67 @@ +/* + * (c) Copyright 2001 MyCorporation. + * All Rights Reserved. + */ + +package figures.support; + +import figures.*; + +public aspect Enforcement { + + before(int newValue): set(int Point.*) && args(newValue) { + if (newValue < 0) { + throw new IllegalArgumentException("> val: " + newValue + " is too small"); + } + } + + declare warning: call(void Canvas.updateHistory(..)) && !within(Enforcement): ""; + + after() returning: call(void FigureElement+.set*(..)) { + //Canvas.updateHistory(); + } + + declare error: + set(private * FigureElement+.*) && + !(withincode(* FigureElement+.set*(..)) || withincode(FigureElement+.new(..))): + "should only assign to fileds from set methods"; + +} + + + + + + + + + + + + + + + + + + + + + +// before(int newValue): set(int Point.*) && args(newValue) { +// if (newValue < 0) { +// throw new IllegalArgumentException("> value: " + newValue + " too small"); +// } +// } +// +// declare warning: call(void Canvas.updateHistory(..)) && !within(Enforcement): +// "found call"; +// +// after() returning: call(void FigureElement+.set*(..)) { +// Canvas.updateHistory(); +// } +// +// declare error: +// set(private * FigureElement+.*) && +// !(withincode(* FigureElement+.set*(..)) || withincode(FigureElement+.new(..))): +// "should only assign to fields from set methods"; diff --git a/ajde/testdata/examples/figures-demo/figures/support/Log.java b/ajde/testdata/examples/figures-demo/figures/support/Log.java new file mode 100644 index 000000000..ed395696a --- /dev/null +++ b/ajde/testdata/examples/figures-demo/figures/support/Log.java @@ -0,0 +1,36 @@ +/* +Copyright (c) 2002 Palo Alto Research Center Incorporated. All Rights Reserved. + */ + +package support; + +public class Log { + private static StringBuffer data = new StringBuffer(); + + public static void traceObject(Object o) { + throw new UnsupportedOperationException(); + } + + public static void log(String s) { + data.append(s); + data.append(';'); + } + + public static void logClassName(Class _class) { + String name = _class.getName(); + int dot = name.lastIndexOf('.'); + if (dot == -1) { + log(name); + } else { + log(name.substring(dot+1, name.length())); + } + } + + public static String getString() { + return data.toString(); + } + + public static void clear() { + data.setLength(0); + } +} |