Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

217
LINES

< > BotCompany Repo | #1004040 // Tiny Browser

JavaX source code [tags: use-pretranspiled] - run with: x30.jar

Libraryless. Click here for Pure Java version (622L/5K/17K).

!759

import java.awt.BorderLayout;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInput;
import java.io.ObjectInputStream;
import java.io.ObjectOutput;
import java.io.ObjectOutputStream;
import java.net.URL;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.ComboBoxModel;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JComboBox;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.event.HyperlinkEvent;
import javax.swing.event.HyperlinkListener;

p {
  new Browser;
}

sclass Browser extends JFrame {

  protected JEditorPane m_browser;

  protected MemComboBox m_locator = new MemComboBox();

  public Browser() {
    super("HTML Browser");
    setSize(500, 300);
    getContentPane().setLayout(new BorderLayout());

    JPanel p = new JPanel();
    p.setLayout(new BoxLayout(p, BoxLayout.X_AXIS));
    p.add(new JLabel("Address"));

    m_locator.load("addresses.dat");
    BrowserListener lst = new BrowserListener();
    m_locator.addActionListener(lst);

    MemComboAgent agent = new MemComboAgent(m_locator);

    p.add(m_locator);

    getContentPane().add(p, BorderLayout.NORTH);

    m_browser = new JEditorPane();
    m_browser.setEditable(false);
    // XXX m_browser.addHyperlinkListener(lst);

    JScrollPane sp = new JScrollPane();
    sp.getViewport().add(m_browser);
    getContentPane().add(sp, BorderLayout.CENTER);

    WindowListener wndCloser = new WindowAdapter() {
      public void windowClosing(WindowEvent e) {
        m_locator.save("addresses.dat");
      }
    };
    addWindowListener(wndCloser);

    setVisible(true);
    m_locator.grabFocus();
  }

  class BrowserListener implements ActionListener, HyperlinkListener {
    public void actionPerformed(ActionEvent evt) {
      //printStackTrace();
      String sUrl = (String) m_locator.getSelectedItem();
      if (sUrl == null || sUrl.length() == 0 )
        return;
      sUrl = addHTTP(sUrl);

      BrowserLoader loader = new BrowserLoader(sUrl);
      loader.start();
    }

    public void hyperlinkUpdate(HyperlinkEvent e) {
      URL url = e.getURL();
      if (url == null )
        return;
      BrowserLoader loader = new BrowserLoader(url.toString());
      loader.start();
    }
  }

  class BrowserLoader extends Thread {
    protected String m_sUrl;

    public BrowserLoader(String sUrl) {
      m_sUrl = sUrl;
    }

    public void run() {
      setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));

      try {
        URL source = new URL(m_sUrl);
        m_browser.setPage(source);
        m_locator.add(m_sUrl);
      } catch (Exception e) {
        JOptionPane.showMessageDialog(Browser.this, "Error: "
            + e.toString(), "Warning", JOptionPane.WARNING_MESSAGE);
      }
      setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
    }
  }
}

sclass MemComboAgent extends KeyAdapter {
  protected JComboBox m_comboBox;

  protected JTextField m_editor;

  public MemComboAgent(JComboBox comboBox) {
    m_comboBox = comboBox;
    m_editor = (JTextField) comboBox.getEditor().getEditorComponent();
    m_editor.addKeyListener(this);
  }

  public void keyReleased(KeyEvent e) {
    char ch = e.getKeyChar();
    if (ch == KeyEvent.CHAR_UNDEFINED || Character.isISOControl(ch))
      return;
    int pos = m_editor.getCaretPosition();
    String str = m_editor.getText();
    if (str.length() == 0)
      return;

    for (int k = 0; k < m_comboBox.getItemCount(); k++) {
      String item = m_comboBox.getItemAt(k).toString();
      if (item.startsWith(str)) {
        m_editor.setText(item);
        m_editor.setCaretPosition(item.length());
        m_editor.moveCaretPosition(pos);
        break;
      }
    }
  }
}

sclass MemComboBox extends JComboBox {
  public static final int MAX_MEM_LEN = 30;

  public MemComboBox() {
    super();
    setEditable(true);
  }

  public void add(String item) {
    if (eq(getItemAt(0), item)) ret;
    //main.print("MemComboBox.add " + item);
    //printStackTrace();
    removeItem(item);
    insertItemAt(item, 0);
    setSelectedItem(item);
    if (getItemCount() > MAX_MEM_LEN)
      removeItemAt(getItemCount() - 1);
  }

  public void load(String fName) {
    try {
      if (getItemCount() > 0)
        removeAllItems();
      File f = getProgramFile(fName);
      if (!f.exists())
        return;
      FileInputStream fStream = new FileInputStream(f);
      ObjectInput stream = new ObjectInputStream(fStream);

      Object obj = stream.readObject();
      if (obj instanceof ComboBoxModel)
        setModel((ComboBoxModel) obj);

      stream.close();
      fStream.close();
    } catch (Exception e) {
      System.err.println("Serialization error: " + e.toString());
    }
  }

  public void save(String fName) {
    try {
      FileOutputStream fStream = new FileOutputStream(getProgramFile(fName));
      ObjectOutput stream = new ObjectOutputStream(fStream);

      stream.writeObject(getModel());

      stream.flush();
      stream.close();
      fStream.close();
    } catch (Exception e) {
      System.err.println("Serialization error: " + e.toString());
    }
  }
}

Author comment

from http://www.java2s.com/Code/Java/Tiny-Application/BrowerbasedonJEditorPane.htm

download  show line numbers  debug dex  old transpilations   

Travelled to 16 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, cysqohhbtkwd, ddnzoavkxhuk, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, onxytkatvevr, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt

1 comment(s) hidden. show

Snippet ID: #1004040
Snippet name: Tiny Browser
Eternal ID of this version: #1004040/1
Text MD5: 97141f4a521a6bf32738eb2814a3fab5
Transpilation MD5: 31d210e835b4bfaa30f8fa97fa1ae75c
Author: stefan
Category: javax / gui
Type: JavaX source code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2016-08-04 16:16:50
Source code size: 5893 bytes / 217 lines
Pitched / IR pitched: No / No
Views / Downloads: 783 / 880
Referenced in: [show references]