home *** CD-ROM | disk | FTP | other *** search
- package como.commlet.draw;
-
- import java.awt.*;
- import java.util.*;
- import java.io.IOException;
- import como.sys.*;
- import como.util.*;
- import como.awt.*;
- import como.io.*;
- import como.commlet.*;
-
- /*
-
- Scheoene menues
- fontauswahl
- text akzeptieren -- semantik????
- */
-
- public class Draw extends WindowCommlet implements DrawObserver {
-
- DrawCanvas draw; // a draw canvas that knows nothing about networking, but informs <this>
- DrawControls controls;
- // whenever its objects are changed
-
- static final int MSG_DRAW_NEW_OBJECT = 1001; // arg = new GO
- static final int MSG_DRAW_DEL_OBJECT = 1002; // arg = key
-
- public String getCommletName() {
- return("Draw");
- }
-
- public String getCommletInfo() {
- return("Draw Comlet, V 1.0, Jan Kautz & Ulrich Gall");
- }
-
- public void init() {
- super.init();
- draw = new DrawCanvas(this);
- add("Center",draw);
- controls = new DrawControls(draw); // needs to tell draw what happens
- add("West",controls);
- draw.setControls(controls);
- resize(400,300);
- }
-
- public void addUser(int who) {
- Debug.msg(1,"Draw.addUser() who = "+who);
- User u = com.getUser(who);
- String name = u.get(u.NAME).toString();
- if (!u.containsKey(u.COLOR))
- {
- Color c = new Color(u.hashCode());
- if (c.getRed() + c.getBlue() + c.getGreen() < 150)
- c = new Color(255-c.getRed(),
- 255-c.getGreen(),
- 255-c.getBlue());
- u.put(u.COLOR,c);
- }
- Color color = (Color)u.get(u.COLOR);
- draw.addObject(new Integer(who),new GOGroup(name,color));
- if (com.getMyID() == who)
- draw.setCurrentGroupKey( new Integer(com.getMyID()) );
- else {
- // Somebody other than myself joined us, so I will send him the contents of my GOGroup
- Enumeration e = ((GOGroup)draw.getGroup( new Integer(com.getMyID()) ) ).elements();
- while (e.hasMoreElements()) {
- Msg m = new Msg(MSG_DRAW_NEW_OBJECT,(GraphicsObject)e.nextElement());
- m.to = who;
- com.sendTo(m);
- }
- if (draw.colormode == draw.COLORMODE_OWNER) {
- controls.updateColormodepanel();
- }
- }
- }
-
- public void userLeft(int who) {
- int i ;
- // TODO
- }
-
- public void newObject(GraphicsObject g) {
- com.sendToOthers(new Msg(MSG_DRAW_NEW_OBJECT,g));
- }
-
- public void delObject(Object key) {
- com.sendToOthers(new Msg(MSG_DRAW_DEL_OBJECT,key));
- }
-
- public boolean handleMsg(Msg msg) {
- if (super.handleMsg(msg)) return true;
- String name = com.getUserName(msg.from);
- Debug.msg(1,"Draw.handleMsg() : msg from " + name + " = "+msg);
- if (msg.type == MSG_DRAW_NEW_OBJECT)
- {
- Debug.msg(10,"Draw.handleMsg() appending " + msg.arg + " to " + msg.from);
- draw.appendToGroup(new Integer(msg.from),(GraphicsObject)msg.arg);
- // draw.paintObject((GraphicsObject)msg.arg);
- draw.repaint();
- return true;
- }
- else if (msg.type == MSG_DRAW_DEL_OBJECT)
- {
- draw.delFromGroup(new Integer(msg.from),msg.arg); // msg.arg = toDelete.hashCode()
- draw.repaint();
- return true;
- }
- else if (msg.type == Msg.NEW_USER_INFO)
- {
- User u = (User)msg.arg;
- draw.getGroup(u.get(u.ID)).name = u.get(u.NAME).toString();
- draw.getGroup(u.get(u.ID)).setColor((Color)u.get(u.COLOR));
- controls.updateColormodepanel();
- draw.repaint();
- return true;
- }
- return false;
- }
- }
-
- /**********************
- * DrawCanvas
- *
- *
- * The following methods are called by the Commlet
- *
- * addObject
- * appendToGroup
- * delFromGroup
- * getGroup
- * setCurrentGroupKey
- * setControls
- *
- * observer is informed about changes in objects.
- * User actions may only alter currentgroup
- *
- */
-
- class DrawCanvas extends Canvas
- {
- public static final int EVENT_ACCEPT = 150173;
-
- GOGroup objects;
- DrawObserver observer;
- DrawControls controls;
-
- GraphicsObject currentGO;
- Object currentGroupKey;
-
- String colormode;
-
- // Color Modes
- static String COLORMODE_OWNER = "Owner│s";
- static String COLORMODE_OBJECT = "Object│s";
-
- // keys
- static final int KEY_PASTE = 112;
- static final int KEY_CUT = 127;
- static final int KEY_COPY = 99;
- static final int KEY_DUMP = 68;
-
- public DrawCanvas(DrawObserver d)
- {
- setBackground(Color.black);
- colormode = COLORMODE_OBJECT;
- observer = d;
- objects = new GOGroup();
- currentGO = null;
- currentGroupKey = null;
- }
-
- public void setControls(DrawControls c) {
- controls = c;
- }
-
- public void setCurrentGroupKey(Object group)
- {
- currentGroupKey = group;
- }
-
- public GraphicsObject getCurrentGroup()
- {
- return (GraphicsObject)objects.get(currentGroupKey);
- }
-
- public void setCurrentGO(GraphicsObject go)
- {
- currentGO = go;
- }
-
- public void setColormode(String m)
- {
- colormode = m;
- repaint();
- }
-
- public void paint(Graphics g)
- {
- Enumeration e = objects.keys();
- while (e.hasMoreElements())
- {
- Object o = e.nextElement();
- GraphicsObject go = (GraphicsObject)objects.get(o);
- if (colormode.compareTo(COLORMODE_OBJECT) == 0) go.draw(g);
- else go.draw(g,go.getColor());
- }
- if (currentGO != null) {
- if (colormode.compareTo(COLORMODE_OBJECT) == 0) currentGO.draw(g);
- else currentGO.draw(g,getCurrentGroup().getColor());
- }
- }
-
- public void paintObject(GraphicsObject g)
- {
- if (getGraphics() != null) {
- if (g != null) {
- if (colormode.compareTo(COLORMODE_OBJECT) == 0) currentGO.draw(getGraphics());
- else currentGO.draw(getGraphics(),getCurrentGroup().getColor());
- }
- }
- }
-
- /*************************************************************
- * Adding and removing objects
- */
- public void addObject(Object key,GraphicsObject g)
- {
- objects.put(key,g);
- paintObject(g);
- }
-
- public GraphicsObject getGroup(Object key)
- {
- return (GraphicsObject)objects.get(key);
- }
-
- public void appendToGroup(Object key,GraphicsObject g)
- {
- GraphicsObject o = objects.get(key);
- if (o instanceof GOGroup) {
- GOGroup group = (GOGroup)o;
- group.put(new Integer(g.hashCode()),g);
- paintObject(g);
- }
- else {
- Debug.msg(90,"DrawPanel.appendToGroup: Not a group: " + o.toString());
- }
- }
-
- public void delFromGroup(Object gk,Object key)
- {
- GOGroup g = (GOGroup) objects.get(gk);
- g.remove(key);
- repaint();
- }
-
- /******************************************************************************************
- * Event handling
- */
- public boolean handleEvent(Event evt) {
- Debug.msg(0,"DrawCanvas.handleEvent(): evt = "+evt);
- /* if (evt.id == Event.KEY_PRESS) {
- if (evt.key == KEY_DUMP)
- {
- Debug.msg(100,"Draw: objects = "+objects);
- Debug.msg(100,"Draw: Colormode = "+colormode);
- if (currentGO == null)
- Debug.msg(100,"No currentGO");
- else
- Debug.msg(100,"currentGO = "+currentGO.toString());
- return true;
- }
- } */
- if (currentGO != null) {
- {
- if (currentGO.handleEvent(evt))
- {
- if (evt.id == EVENT_ACCEPT)
- {
- // This means we should accept the currentGO and start a new one
- appendToGroup(currentGroupKey ,currentGO);
- observer.newObject(currentGO);
- paintObject(currentGO);
- currentGO = currentGO.getNew();
- currentGO.color = controls.currentcolor;
- currentGO.filled = controls.filled;
- currentGO.moveTo(evt.x,evt.y);
- }
- paintObject(currentGO);
- return true;
- }
- }
- }
- return false;
- }
- }
-
- /*******************************************************************************************
- * DrawControls
- */
-
- class DrawControls extends Panel {
-
- // modes
- static String MODE_POLYGON = "Polygon";
- static String MODE_TEXT = "Text";
-
- DrawCanvas drawcanvas;
-
- Choice drawmodeChoice;
- Choice colormodeChoice;
-
- Panel colormodepanel;
- Panel drawmodepanel;
-
- Checkbox filledCB;
- ColorSelector colorsel;
-
- boolean filled;
- Color currentcolor;
- Color fillColor;
-
- public DrawControls(DrawCanvas dc) {
- drawcanvas = dc;
-
- setLayout(new VertLayout(VertLayout.STRETCH));
- add(new Label("Draw Mode:"));
- drawmodeChoice = new Choice();
- drawmodeChoice.addItem(MODE_POLYGON);
- drawmodeChoice.addItem(MODE_TEXT);
- add(drawmodeChoice);
-
- drawmodepanel = new Panel();
- add(drawmodepanel);
-
- currentcolor = Color.white;
- add(new Label("Current Color"));
- colorsel = new ColorSelector(currentcolor);
- add(colorsel);
-
- filled = false;
- filledCB = new Checkbox("Filled");
- filledCB.setState(filled);
- add(filledCB);
-
- add(new Label("Color Mode"));
- colormodeChoice = new Choice();
- colormodeChoice.addItem(dc.COLORMODE_OBJECT);
- colormodeChoice.addItem(dc.COLORMODE_OWNER);
- add(colormodeChoice);
- colormodepanel = new Panel();
- updateColormodepanel();
- add(colormodepanel);
-
- GraphicsObject cur = new GOPolygon();
- cur.setColor(currentcolor);
- cur.setFilled(filled);
- setCurrentGO(cur);
-
- }
-
- public void setCurrentGO(GraphicsObject cur)
- {
- cur.layoutPropPanel(drawmodepanel);
- layout();
- paintAll(getGraphics());
- drawcanvas.setCurrentGO(cur);
- }
-
- public void setCurrentColor(Color c) {
- currentcolor = c;
- if (drawcanvas.currentGO != null) drawcanvas.currentGO.setColor(c);
- }
-
- public void updateColormodepanel() {
- colormodepanel.removeAll();
- if (drawcanvas.colormode.compareTo(DrawCanvas.COLORMODE_OWNER) == 0)
- {
- colormodepanel.setLayout(new VertLayout(VertLayout.STRETCH));
- Enumeration e = drawcanvas.objects.elements();
- while (e.hasMoreElements()) {
- GraphicsObject g = (GraphicsObject)e.nextElement();
- ColorView c = new ColorView(g.getColor(),g.name);
- colormodepanel.add(c);
- }
- colormodepanel.paintAll(colormodepanel.getGraphics());
- }
- layout();
- }
-
- public boolean action(Event evt,Object arg) {
-
- if (evt.target == drawmodeChoice) {
- String mode= drawmodeChoice.getSelectedItem();
- GraphicsObject cur = null;
- if (mode == MODE_POLYGON) cur = new GOPolygon();
- if (mode == MODE_TEXT) cur = new GOText();
- cur.setColor(currentcolor);
- cur.setFilled(filled);
- setCurrentGO(cur);
- return true;
- }
- if (evt.target == colormodeChoice) {
- drawcanvas.setColormode(colormodeChoice.getSelectedItem());
- if (colormodeChoice.getSelectedItem().compareTo(DrawCanvas.COLORMODE_OWNER) == 0)
- currentcolor = drawcanvas.getCurrentGroup().getColor();
- updateColormodepanel();
- return true;
- }
- if (evt.target == filledCB) {
- filled = filledCB.getState();
- if (drawcanvas.currentGO != null) {
- drawcanvas.currentGO.setFilled(filled);
- drawcanvas.paintObject(drawcanvas.currentGO);
- }
- }
- if (evt.target == colorsel) {
- setCurrentColor(colorsel.getColor());
- }
- return false;
- }
-
- public boolean handleEvent(Event evt) {
- boolean sup = super.handleEvent(evt);
- // TODO: This is not necessarily for currentGO
- GraphicsObject cur = drawcanvas.currentGO;
- boolean b = false;
- if (cur != null) {
- b = cur.handlePropPanelEvent(evt,drawcanvas);
- }
- return (sup || b);
- }
- }
-
- class ColorView extends Canvas {
- String name;
- static final int SPACE_BOTTOM = 6;
- static final int SPACE_LEFT = 10;
-
- public ColorView(Color c, String n) {
- name = n;
- setBackground(c);
- if ((c.getRed()+c.getGreen()+c.getBlue()) < 384) setForeground(Color.white);
- else setForeground(Color.black);
- }
-
- public void paint(Graphics g) {
- g.drawString(name,SPACE_LEFT,size().height-SPACE_BOTTOM);
- }
-
- public Dimension preferredSize() {
- FontMetrics fm = getFontMetrics(getFont());
- return new Dimension(2*SPACE_LEFT+fm.stringWidth(name),
- fm.getHeight()+2*SPACE_BOTTOM);
- }
-
- public Dimension minimumSize() {
- return preferredSize();
- }
- }
-
- /******************************************************************************************/
- interface DrawObserver {
-
- void newObject(GraphicsObject g);
-
- void delObject(Object key);
- }
-