home *** CD-ROM | disk | FTP | other *** search
/ The Net: Ultimate Internet Guide / WWLCD1.ISO / pc / java / in4wjcxu / src / como / commlet / draw / gotext.java < prev    next >
Encoding:
Java Source  |  1996-08-14  |  2.7 KB  |  125 lines

  1.  
  2. package como.commlet.draw;
  3.  
  4. import como.io.*;
  5. import como.util.*;
  6. import java.awt.*;
  7. import java.io.*;
  8. import java.util.*; import java.lang.*;
  9. /******
  10. * GOText
  11. */
  12. public class GOText extends GraphicsObject
  13. {
  14.     Font font;
  15.     private StringBuffer text;
  16.     TextField textTF;
  17.     int nextx, nexty;
  18.     public GOText()
  19.     {
  20.         super();
  21.         font = new Font("Courier",Font.PLAIN,24);
  22.         text = new StringBuffer();
  23.         nextx = x;
  24.         nexty = y + 20;
  25.     }
  26.     public GraphicsObject getNew() {
  27.         GOText go = new GOText();
  28.         go.color = color;
  29.         go.x = nextx;
  30.         go.y = nexty; // TODO
  31.         go.name = name;
  32.         go.font = font;
  33.         go.textTF = textTF;
  34.         textTF = null;
  35.         go.updatePropPanel();
  36.         return go;
  37.     }
  38.  
  39.     public void draw(Graphics g,Color c)
  40.     {
  41.         lastgraphics = g;
  42.         g.setColor(c);
  43.         g.setFont(font);
  44.         g.drawString(text.toString(),getX(),getY());
  45.     }
  46.     public void layoutPropPanel(Panel p) {
  47.         p.removeAll();
  48.         p.setLayout(new BorderLayout());
  49.         if (textTF == null) textTF = new TextField(text.toString());
  50.         p.add("North",new Label("Enter text below"));
  51.         p.add("Center",textTF);
  52.         updatePropPanel();
  53.     }
  54.     public void updatePropPanel() {
  55.         if (textTF != null) {
  56.             if (text != null) textTF.setText(text.toString());
  57.             if (textTF.getParent() != null) textTF.getParent().repaint();            
  58.         }
  59.     }
  60.     public boolean handlePropPanelEvent(Event evt,DrawCanvas d) {
  61.         if (evt.target instanceof TextField) {
  62.             if (evt.id == Event.ACTION_EVENT) {
  63.                 nextx = x;
  64.                 nexty = y+20;
  65.                 d.postEvent(new Event(d,DrawCanvas.EVENT_ACCEPT,null));
  66.             }
  67.             text= new StringBuffer(textTF.getText()); 
  68.             d.repaint();
  69.             return false;
  70.         }
  71.         return false;
  72.     }
  73.  
  74.     public boolean handleEvent(Event evt)
  75.     {
  76.         if (evt.id == Event.MOUSE_DOWN)
  77.         {
  78.             if (text.length() > 0)
  79.             {
  80.                 evt.id = DrawCanvas.EVENT_ACCEPT;
  81.                 nextx = evt.x;
  82.                 nexty = evt.y;
  83.                 return true;
  84.             }
  85.             moveTo(evt.x,evt.y);
  86.             return true;
  87.         }
  88.         else if (evt.id == Event.KEY_PRESS)
  89.         {
  90.             if ((evt.key == 127) || (evt.key == '\b')) {
  91.                 if (text.length()>0) text.setLength(text.length()-1);
  92.                 if (evt.target instanceof DrawCanvas) {
  93.                     ((DrawCanvas)evt.target).repaint();
  94.                 }
  95.             }
  96.             else if (evt.key == '\n') {
  97.                 evt.id = DrawCanvas.EVENT_ACCEPT;
  98.                 nextx = x;
  99.                 nexty = y+20;
  100.                 return true;
  101.             }
  102.             else text.append((char)evt.key);
  103.             updatePropPanel();
  104.             return true;
  105.         }                           
  106.         return false;        
  107.     }
  108.     
  109.     public String toString()
  110.     {
  111.         return super.toString() + font.getName()+" "+ font.getSize()+" "+font.getStyle()+" "
  112.         +text.toString();
  113.     }
  114.     public void  save(ObjectOutputStream s) throws IOException {
  115.         super.save(s);
  116.         s.writeObject(font);
  117.         s.writeString(text.toString());
  118.     }
  119.     public void  load(ObjectInputStream s) throws IOException {
  120.         super.load(s);
  121.         font = (Font)s.readObject();
  122.         text = new StringBuffer(s.readString());
  123.     }
  124. }
  125.