home *** CD-ROM | disk | FTP | other *** search
/ Symantec Visual Cafe for Java 2.5 / symantec-visual-cafe-2.5-database-dev-edition.iso / VCafe / main.bin / WrappingLabel.class (.txt) < prev    next >
Encoding:
Java Class File  |  1997-08-04  |  3.5 KB  |  149 lines

  1. package symantec.itools.awt;
  2.  
  3. import java.awt.Canvas;
  4. import java.awt.Component;
  5. import java.awt.Dimension;
  6. import java.awt.FontMetrics;
  7. import java.awt.Graphics;
  8.  
  9. public strictfp class WrappingLabel extends Canvas implements AlignStyle {
  10.    protected String text;
  11.    protected int align;
  12.    protected int baseline;
  13.    // $FF: renamed from: fm java.awt.FontMetrics
  14.    protected FontMetrics field_0;
  15.  
  16.    public WrappingLabel() {
  17.       this("");
  18.    }
  19.  
  20.    public WrappingLabel(String s) {
  21.       this(s, 0);
  22.    }
  23.  
  24.    public WrappingLabel(String s, int a) {
  25.       this.setText(s);
  26.       this.setAlignStyle(a);
  27.    }
  28.  
  29.    public int getAlignStyle() {
  30.       return this.align;
  31.    }
  32.  
  33.    public void setAlignStyle(int a) {
  34.       this.align = a;
  35.       ((Component)this).invalidate();
  36.    }
  37.  
  38.    public String getText() {
  39.       return this.text;
  40.    }
  41.  
  42.    public void setText(String s) {
  43.       this.text = s;
  44.       ((Component)this).repaint();
  45.    }
  46.  
  47.    public String paramString() {
  48.       return "";
  49.    }
  50.  
  51.    public synchronized void reshape(int x, int y, int width, int height) {
  52.       super.reshape(x, y, width, height);
  53.       ((Component)this).invalidate();
  54.       ((Component)this).validate();
  55.       ((Component)this).repaint();
  56.    }
  57.  
  58.    public synchronized void resize(int width, int height) {
  59.       super.resize(width, height);
  60.       ((Component)this).invalidate();
  61.       ((Component)this).validate();
  62.       ((Component)this).repaint();
  63.    }
  64.  
  65.    public void paint(Graphics g) {
  66.       if (this.text != null) {
  67.          int fromIndex = 0;
  68.          int pos = 0;
  69.          this.field_0 = ((Component)this).getToolkit().getFontMetrics(((Component)this).getFont());
  70.          this.baseline = this.field_0.getMaxAscent();
  71.          Dimension d = ((Component)this).size();
  72.          int boundx = d.width;
  73.          int boundy = d.height;
  74.          int x = 0;
  75.  
  76.          for(int y = 0; y + this.field_0.getHeight() <= boundy && fromIndex != -1; y += this.field_0.getHeight()) {
  77.             while(fromIndex < this.text.length() && this.text.charAt(fromIndex) == ' ') {
  78.                ++fromIndex;
  79.                if (fromIndex >= this.text.length()) {
  80.                   break;
  81.                }
  82.             }
  83.  
  84.             pos = fromIndex;
  85.             int bestpos = -1;
  86.  
  87.             String largestString;
  88.             for(largestString = null; pos >= fromIndex; ++pos) {
  89.                pos = this.text.indexOf(32, pos);
  90.                String s;
  91.                if (pos == -1) {
  92.                   s = this.text.substring(fromIndex);
  93.                } else {
  94.                   s = this.text.substring(fromIndex, pos);
  95.                }
  96.  
  97.                if (this.field_0.stringWidth(s) >= boundx) {
  98.                   break;
  99.                }
  100.  
  101.                largestString = s;
  102.                bestpos = pos;
  103.                if (pos == -1) {
  104.                   break;
  105.                }
  106.             }
  107.  
  108.             if (largestString != null) {
  109.                this.drawAlignedString(g, largestString, x, y, boundx);
  110.                fromIndex = bestpos;
  111.             } else {
  112.                int totalWidth = 0;
  113.                int oneCharWidth = 0;
  114.  
  115.                for(pos = fromIndex; pos < this.text.length(); ++pos) {
  116.                   oneCharWidth = this.field_0.charWidth(this.text.charAt(pos));
  117.                   if (totalWidth + oneCharWidth >= boundx) {
  118.                      break;
  119.                   }
  120.  
  121.                   totalWidth += oneCharWidth;
  122.                }
  123.  
  124.                this.drawAlignedString(g, this.text.substring(fromIndex, pos), x, y, boundx);
  125.                fromIndex = pos;
  126.             }
  127.          }
  128.  
  129.          this.field_0 = null;
  130.       }
  131.  
  132.    }
  133.  
  134.    protected void drawAlignedString(Graphics g, String s, int x, int y, int width) {
  135.       int drawx = x;
  136.       int drawy = y + this.baseline;
  137.       if (this.align != 0) {
  138.          int sw = this.field_0.stringWidth(s);
  139.          if (this.align == 1) {
  140.             drawx = x + (width - sw) / 2;
  141.          } else if (this.align == 2) {
  142.             drawx = x + width - sw;
  143.          }
  144.       }
  145.  
  146.       g.drawString(s, drawx, drawy);
  147.    }
  148. }
  149.