001 package org.util.html.demo; 002 003 import java.util.*; 004 import java.io.*; 005 import java.net.*; 006 import java.awt.*; 007 import java.awt.image.BufferedImage; 008 import java.awt.event.*; 009 import javax.swing.*; 010 import javax.swing.event.*; 011 012 import org.util.html.objects.*; 013 import org.util.html.render.*; 014 import org.util.html.factory.*; 015 import org.util.log.*; 016 017 018 public class Demo1 { 019 020 private JFrame frame_; 021 private JComponent main_content_; 022 private LogListener log_listener_; 023 024 private HTMLDocument document_; 025 private HTMLDocumentRenderer renderer_; 026 private HTMLDocumentFactory factory_; 027 private JButton back_, update_, go_; 028 private JTextField text_; 029 private int scroll_bar_width_ = 15; 030 031 public static void main(String[] args) throws Exception { 032 final Demo1 demo = new Demo1(); 033 SwingUtilities.invokeAndWait(new Runnable(){public void run(){ 034 demo.createAndShowGUI1(); 035 }}); 036 SwingUtilities.invokeAndWait(new Runnable(){public void run(){ 037 demo.createAndShowGUI2(); 038 }}); 039 Thread init_process = new Thread(new Runnable(){ 040 public void run(){ 041 demo.start(); 042 } 043 }, "demo process"); 044 init_process.start(); 045 init_process.join(); 046 SwingUtilities.invokeAndWait(new Runnable(){public void run(){ 047 demo.createAndShowGUI3(); 048 }}); 049 } 050 private void createAndShowGUI1() { 051 JFrame frame = new JFrame("Demo1"); 052 frame.setSize(500, 500); 053 frame.setLocationRelativeTo(null); 054 frame.setVisible(true); 055 frame_ = frame; 056 } 057 private void createAndShowGUI2() { 058 back_ = new JButton("<"); 059 update_ = new JButton("o"); 060 text_ = new JTextField(); 061 go_ = new JButton(">"); 062 063 JPanel toolpane = new JPanel(); 064 toolpane.setPreferredSize(new Dimension(500, 24)); 065 toolpane.setLayout(new BoxLayout(toolpane, BoxLayout.X_AXIS)); 066 toolpane.add(back_); 067 toolpane.add(update_); 068 toolpane.add(text_); 069 toolpane.add(go_); 070 071 JPanel contentpane = new JPanel(new BorderLayout()); 072 contentpane.add(toolpane, BorderLayout.NORTH); 073 074 frame_.setContentPane(contentpane); 075 contentpane.revalidate(); 076 } 077 private void createAndShowGUI3() { 078 079 text_.addActionListener(new ActionListener(){ 080 public void actionPerformed(ActionEvent e) { 081 openInOtherThread(text_.getText()); 082 } 083 }); 084 085 main_content_ = renderer_.getPanel(); 086 087 MouseManager mouse_manager = new MouseManager(); 088 main_content_.addMouseListener(mouse_manager); 089 main_content_.addMouseMotionListener(mouse_manager); 090 //main_content_.addMouseWheelListener(mouse_manager); 091 092 main_content_.addComponentListener(new ComponentAdapter(){ 093 public void componentResized(ComponentEvent e) { 094 Component c = e.getComponent(); 095 renderer_.resized(c.getWidth(), c.getHeight()); 096 } 097 }); 098 main_content_.setFocusable(true); 099 100 JScrollPane sp = new JScrollPane(main_content_); 101 sp.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS); 102 sp.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS); 103 renderer_.setScrollPane(sp); 104 105 frame_.getContentPane().add(sp); 106 107 frame_.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 108 Image icon = org.util.resource.ResourceManager.getImage("./data/icon/frame_icon.png", 30, 30); 109 frame_.setIconImage(icon); 110 } 111 112 private void start() { 113 HTMLDocument document = new HTMLDocument(); 114 HTMLDocumentRenderer renderer = new HTMLDocumentRenderer(document); 115 renderer.addHTMLDocumentRendererListener(new HTMLDocumentRendererListener(){ 116 public void repaint() { 117 if(main_content_!=null) 118 main_content_.repaint(); 119 } 120 }); 121 document_ = document; 122 renderer_ = renderer; 123 factory_ = new HTMLDocumentFactory(); 124 factory_.setLogListener(getLogListener()); 125 try{ 126 openSamplePage(); 127 }catch(Exception e) { 128 alertError(e); 129 } 130 } 131 132 private void openSamplePage() throws Exception { 133 open("http://sakura.meijo-u.ac.jp"); 134 } 135 136 private void openInOtherThread(final String url_text) { 137 new Thread(new Runnable(){public void run(){ 138 try{ 139 open(url_text); 140 }catch(Exception e) { 141 alertError(e); 142 } 143 }}, "open url").start(); 144 } 145 146 private void open(String url_text) throws Exception { 147 URL url = new URL(url_text); 148 frame_.setTitle(url.toString()); 149 factory_.createDocument(url, document_); 150 } 151 152 private class MouseManager implements MouseListener, MouseMotionListener, MouseWheelListener { 153 private int last_x; 154 private int last_y; 155 private int pressed_x; 156 private int pressed_y; 157 158 public void mousePressed(MouseEvent e) { 159 main_content_.requestFocus(); 160 pressed_x = e.getX(); 161 pressed_y = e.getY(); 162 } 163 public void mouseReleased(MouseEvent e) { 164 165 } 166 public void mouseClicked(MouseEvent e) { 167 168 } 169 public void mouseEntered(MouseEvent e) { 170 171 } 172 public void mouseExited(MouseEvent e) { 173 174 } 175 public void mouseMoved(MouseEvent e) { 176 int x = e.getX(); 177 int y = e.getY(); 178 179 last_x = x; 180 last_y = y; 181 } 182 public void mouseDragged(MouseEvent e) { 183 int x = e.getX(); 184 int y = e.getY(); 185 186 last_x = x; 187 last_y = y; 188 } 189 public void mouseWheelMoved(MouseWheelEvent e) { 190 //renderer_.moveView(0, -e.getWheelRotation()*20); 191 } 192 } 193 194 195 196 private LogListener getLogListener(){ 197 if(log_listener_==null){ 198 log_listener_ = new GUIAlert(main_content_); 199 } 200 return log_listener_; 201 } 202 203 public void alert(Object message) { 204 getLogListener().alert(message); 205 } 206 public void alertError(Object message) { 207 getLogListener().alertError(message); 208 } 209 public void log(Object message) { 210 getLogListener().log(message); 211 } 212 213 }