001 package org.util.xml.renderer.html; 002 003 import org.util.xml.parse.policy.*; 004 import org.util.xml.parse.*; 005 import org.util.xml.element.*; 006 import java.awt.*; 007 import java.awt.event.*; 008 import java.awt.geom.*; 009 import javax.swing.*; 010 import javax.swing.event.*; 011 import java.util.*; 012 import java.awt.font.*; 013 import java.awt.image.*; 014 015 public class HTMLText extends HTMLObject { 016 017 private String html_text_; 018 019 public HTMLText(String html_text) { 020 html_text_ = html_text; 021 } 022 public void mousePressed(MouseEvent e) { 023 setBackground(Color.blue); 024 } 025 public void mouseReleased(MouseEvent e) { 026 setBackground(Color.red); 027 } 028 public void mouseEntered(MouseEvent e) { 029 setBackground(Color.green); 030 } 031 public void mouseExited(MouseEvent e) { 032 setBackground(Color.white); 033 } 034 public boolean hit(double x, double y) { 035 if(x-x_<0 || x-x_>width_) return false; 036 if(y-y_<0 || y-y_>height_) return false; 037 return true; 038 } 039 public void paint(Graphics g) { 040 debug("draw start"); 041 g.setColor(background_); 042 g.fillRect((int)x_,(int)y_,(int)width_,(int)height_); 043 g.setColor(foreground_); 044 045 Font font = new Font("Arial", Font.PLAIN, 10); 046 FontRenderContext frc = new FontRenderContext(new AffineTransform(), true, true); 047 String text = html_text_; 048 String[] lines = text.split("\n"); 049 double now_y = margin_up_; 050 double max_width = 0; 051 052 TextLayout text_layout = null; 053 for(int i=0;i<lines.length;i++) { 054 if(lines[i].length()==0) continue; 055 text_layout = new TextLayout(lines[i], font, frc); 056 float d = text_layout.getAscent(); 057 text_layout.draw((Graphics2D)g, (float)(x_+margin_left_), (float)(y_+d+now_y)); 058 Rectangle2D rect = text_layout.getBounds(); 059 now_y += rect.getHeight(); 060 max_width = Math.max(max_width, rect.getWidth()); 061 } 062 if(!init_) { 063 if(text_layout != null) 064 now_y += text_layout.getDescent(); 065 setPreferredSize(margin_left_+max_width+margin_right_, now_y+margin_bottom_); 066 init_ = true; 067 } 068 debug("draw end"); 069 } 070 071 public Element allowElement(Element element) { 072 if(element.isTagElement()) { 073 TagElement tag = (TagElement)element; 074 System.out.println("tag["+tag+"]"); 075 } else { 076 TextElement text_element = (TextElement)element; 077 // System.out.println("text["+element+"]"); 078 html_text = text_element.getValue(); 079 } 080 return element; 081 } 082 083 }