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 HTMLObject implements ParserPolicy {
016        
017        protected boolean init_ = false;
018        private boolean changed_view_ = true;
019        private boolean changed_layout_ = true;
020        protected double preferred_width_ = 100;
021        protected double preferred_height_ = 100;
022        protected double preferred_x_ = 100;
023        protected double preferred_y_ = 100;
024        protected double offset_x_ = 0;
025        protected double offset_y_ = 0;
026        protected boolean preferred_location_ = false;
027        protected double width_ = 100;
028        protected double height_ = 100;
029        protected double x_ = 100;
030        protected double y_ = 100;
031        protected Color background_ = Color.white;
032        protected Color foreground_ = Color.black;
033        public boolean is_mouse_over_;
034        private ArrayList<ChangeListener> listener_list_ = new ArrayList<ChangeListener>();
035        private ArrayList<HTMLObject> html_object_list_ = new ArrayList<HTMLObject>();
036        protected double write_point_x_ = 0;
037        protected double write_point_y_ = 0;
038        protected double margin_up_ = 1;
039        protected double margin_bottom_ = 1;
040        protected double margin_right_ = 5;
041        protected double margin_left_ = 5;
042        //  private ArrayList<HTMLObject> children_ = new ArrayList<HTMLObject>();
043        private BufferedImage buf_;
044        protected String html_text = "";
045        protected double now_y_ = 0;
046        
047        public Rectangle2D rect_;
048        
049        public HTMLObject() {
050        }
051        public static void alert(Object message) {
052            alert(null, message);
053        }
054        public static void alert(JComponent parent, Object message) {
055            JOptionPane.showMessageDialog(parent, message);
056        }
057        public boolean hit(double x, double y) {
058            /*
059              System.out.println(x_+","+y_+","+width_+","+height_);
060              System.out.println(x+"x"+y);
061              if(x-x_<0 || x-x_>width_) return false;
062              if(y-y_<0 || y-y_>height_) return false;
063            */
064            return true;
065        }
066        
067        public void addChangeListener(ChangeListener listener) {
068            listener_list_.add(listener);
069        }
070        public void fireChanged() {
071            ChangeEvent event = new ChangeEvent(this);
072            for(int i=0;i<listener_list_.size();i++)
073                listener_list_.get(i).stateChanged(event);
074        }
075        
076        private Thread test_thread = null;
077        public void mousePressed(MouseEvent e) {
078            /*              
079                            if(false)
080                            if(test_thread == null) {
081                            final double[] c = new double[1];
082                            test_thread = new Thread(new Runnable(){
083                            public void run() {
084                            while(true) {
085                            try{Thread.sleep(10);}catch(Exception e){}
086                            long time = System.currentTimeMillis();
087                            c[0] = (double)time/1000;
088                            setPreferredLocation(100+50*Math.cos(c[0]), 100+50*Math.sin(c[0]));
089                            }
090                            }
091                            });
092                            test_thread.start();
093                            }
094            */
095            int x = e.getX();
096            int y = e.getY();
097            for(int i=0;i<html_object_list_.size();i++) {
098                HTMLObject htmlo = html_object_list_.get(i);
099                if(htmlo.hit(x, y))
100                    htmlo.mousePressed(e);
101            }
102            setBackground(Color.blue);
103        }
104        
105        public void mouseReleased(MouseEvent e) {
106            int x = e.getX();
107            int y = e.getY();
108            for(int i=0;i<html_object_list_.size();i++) {
109                HTMLObject htmlo = html_object_list_.get(i);
110                if(htmlo.hit(x, y))
111                    htmlo.mouseReleased(e);
112            }
113            setBackground(Color.red);
114        }
115        public void mouseClicked(MouseEvent e) {
116        }
117        public void mouseEntered(MouseEvent e) {
118            setBackground(Color.red);
119        }
120        public void mouseExited(MouseEvent e) {
121            setBackground(Color.white);
122        }
123        int count = 0;
124        public void mouseMoved(MouseEvent e) {
125            for(int i=0;i<html_object_list_.size();i++) {
126                HTMLObject htmlo = html_object_list_.get(i);
127                if(htmlo.hit(e.getX(), e.getY())) {
128                    htmlo.mouseMoved(e);
129                    if(!htmlo.is_mouse_over_) {
130                        htmlo.is_mouse_over_ = true;
131                        htmlo.mouseEntered(e);
132                    }
133                } else {
134                    if(htmlo.is_mouse_over_) {
135                        htmlo.is_mouse_over_ = false;
136                        htmlo.mouseExited(e);
137                    }
138                }
139            }
140        }
141        public void mouseWheelMoved(MouseWheelEvent e) {
142            offset_y_ -= 30*e.getWheelRotation();
143            fireChanged();
144        }
145        public void mouseDragged(MouseEvent e) {}
146        
147        public void setBackground(Color background) {
148            background_ = background;
149            changed_view_ = true;
150            fireChanged();
151        }
152        public void setForeground(Color foreground) {
153            foreground_ = foreground;
154        }
155        public void setPreferredSize(double width, double height) {
156            preferred_width_ = width;
157            preferred_height_ = height;
158            fireChanged();
159        }
160        public void setPreferredLocation(double x, double y) {
161            preferred_x_ = x;
162            preferred_y_ = y;
163            fireChanged();
164        }
165        public double getPreferredWidth() {
166            return preferred_width_;
167        }
168        public double getPreferredHeight() {
169            return preferred_height_;
170        }
171        public double getPreferredX() {
172            return preferred_x_;
173        }
174        public double getPreferredY() {
175            return preferred_y_;
176        }
177        public double getX() {
178            return x_;
179        }
180        public double getY() {
181            return y_;
182        }
183        public double getWidth() {
184            return width_;
185        }
186        public double getHeight() {
187            return height_;
188        }
189        public void setLocation(double x, double y) {
190            x_ = x;
191            y_ = y;
192        }
193        public void setSize(double width, double height) {
194            width_ = width;
195            height_ = height;
196        }
197        
198        private int paint_count_ = 0;
199        public void paint(final Graphics g) {
200            debug("paint method start"+(paint_count_++));
201            if(buf_ == null) {
202                g.drawString("now rendering...", 10, 15);
203            }
204            if(changed_view_ || changed_layout_) {
205                //                  new Thread(new Runnable(){public void run(){
206                debug("thread start");
207                doLayout();
208                doRenderer();
209                for(int i=0;i<html_object_list_.size();i++) {
210                    HTMLObject htmlo = html_object_list_.get(i);
211            htmlo.paint(g);
212                }
213                //                          g.drawImage(buf_, (int)x_, (int)y_, null);
214                debug("thread end");
215                //                  }}).start();
216            } else {
217                debug("copy start");
218                g.drawImage(buf_, (int)x_, (int)y_, null);
219                debug("copy end");
220            }
221            debug("paint method end");
222        }
223        public void doLayout() {
224            now_y_ = offset_y_;
225            for(int i=0;i<html_object_list_.size();i++) {
226                HTMLObject htmlo = html_object_list_.get(i);
227                double x = htmlo.getPreferredX();
228                double y = htmlo.getPreferredY();
229                double w = htmlo.getPreferredWidth();
230                double h = htmlo.getPreferredHeight();
231                htmlo.setSize(w, h);
232                if(htmlo.preferred_location_) {
233                    htmlo.setLocation(x, y);
234                } else {
235                    //                      System.out.println("tset");
236                    htmlo.setLocation(0, now_y_);
237                    now_y_ += htmlo.getHeight();
238                }
239            }
240            //              changed_layout_ = false;
241        }
242        public void doRenderer() {
243            if(changed_view_) {
244                debug("rendering start");
245                clearTmpImage();
246                Graphics g = tmp_image_.getGraphics();
247                draw(g);
248                buf_ = tmp_image_.getSubimage((int)x_, (int)y_, (int)preferred_width_, (int)preferred_height_);
249                debug("rendering end");
250            }
251            changed_view_ = false;
252        }
253        public void write(HTMLObject html_object) {
254            html_object_list_.add(html_object);
255        }
256        private static void clearTmpImage() {
257            for(int i=0;i<tmp_image_.getHeight();i++)
258                for(int j=0;j<tmp_image_.getWidth();j++)
259                    tmp_image_.setRGB(i, j, 0x00000000);
260        }
261        private static BufferedImage tmp_image_ = new BufferedImage(500, 500, BufferedImage.TYPE_INT_ARGB);
262        public void draw(Graphics g) {
263            debug("draw start");
264            for(int i=0;i<html_object_list_.size();i++) {
265                HTMLObject htmlo = html_object_list_.get(i);
266                htmlo.paint(g);
267            }
268            
269            //g.drawRect((int)(rect.getX()+x_),(int)(rect.getY()+y_+d),(int)rect.getWidth(),(int)rect.getHeight());
270            g.drawRect((int)x_,(int)(y_),(int)(width_-1),(int)(height_-1));
271            debug("draw end");
272        }
273        
274        public void debug(String message) {
275            //System.out.println(message);
276        }
277    
278    
279    
280    
281    
282    
283        public boolean throwExceptionIfDocumentHasError() {
284            return false;
285        }
286        public boolean checkEndTag() {
287            return false;
288        }
289        public ParserPolicy getInnerPolicy(Element element) {
290            
291            return this;
292        }
293        public String selectEncoding(String last_tag_key) {
294            return "shift-jis";//"utf-8";
295        }
296        public boolean forceEmptyTag(String key) {
297            return false;
298        }
299        public Element startElement(Element element) {
300            return element;
301        }
302        @Override public boolean finished() {
303            return false;
304        }
305        public Element allowElement(Element element) {
306            
307            if(element.isTagElement()) {
308                TagElement tag = (TagElement)element;
309                if(tag.getKey().equals("img")) {
310                    HTMLImage hi = new HTMLImage(tag);
311                    write(hi);
312                    //                              alert("image");
313                } else if(tag.getKey().equals("script")) {
314                    //                              alert(tag.getValue());
315                    return null;
316                } else if(tag.getKey().equals("link")) {
317                    //                              alert(tag.getValue());
318                    return null;
319                } else {
320                    System.out.println("skip tag["+tag.getKey()+"]");
321                }
322            } else {
323                TextElement text_element = (TextElement)element;
324                //                  System.out.println("text["+element+"]");
325                HTMLText ht = new HTMLText(text_element.getValue());
326                ht.addChangeListener(new ChangeListener(){
327                        public void stateChanged(ChangeEvent e){
328                            fireChanged();
329                        }
330                    });
331                write(ht);
332                return null;
333            }
334            return element;
335        }
336    }