Friday, November 04, 2005

Java Hindi office suite: InternetExplorer

Continuing my last post, this contains the code of a Java InternetExplorer replacement. This is a simple web browser with hindi menus and dialogs. Has no support for frames, javascript or applets. Save is not yet correctly imlemented(saves only the html part,not the embedded pictures or other resources). That said here is your code for IExplorer.java

//IExplorer.java
/*
* Created on Dec 16, 2004
*/
package name.shabda.office;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Hashtable;
import java.util.Stack;

import javax.swing.Action;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JEditorPane;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.JToolBar;
import javax.swing.event.HyperlinkEvent;
import javax.swing.event.HyperlinkListener;
import javax.swing.filechooser.FileFilter;
import javax.swing.text.DefaultEditorKit;
import javax.swing.text.JTextComponent;
import javax.swing.text.html.HTMLEditorKit;

/**
* @author dicky
*/
public class IExplorer extends JFrame {
JEditorPane theView;//the editor where the page is displayed
JTextField add;//address bar
Stack backPages;//stack which contains list of pages to which you can go back
Stack forwardPages;//list of pages to which you can forward
HTMLEditorKit editor;//editor for saving and opening
private JMenuItem back;//the back menu item
private JMenuItem forward;//the forward menu item
private JButton backButton;
private JButton forwardButton;

Hashtable actions;//stores the actions

/**
*
*/
public IExplorer() {
editor=new HTMLEditorKit();
getContentPane().setLayout(new BorderLayout());
theView=getEditor();
createActionTable(theView);//create action table from where we can add the actions to menus
JScrollPane pane=new JScrollPane(theView);
getContentPane().add(pane,BorderLayout.CENTER);
getContentPane().add(getNonEditor(),BorderLayout.NORTH);
addHtmlListener();
setJMenuBar(getMenu());
backPages=new Stack();
forwardPages=new Stack();
try {
setPage(new URL("http://www.aksharmala.com/")); //$NON-NLS-1$
} catch (MalformedURLException e) {
e.printStackTrace();
}
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}

});
}
/**
* This method registers a html listener on the document.
* */
public void addHtmlListener(){
theView.addHyperlinkListener(new HyperlinkListener() {

public void hyperlinkUpdate(HyperlinkEvent e) {
if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
URL url;
try {
url = new URL(add.getText());//the current page
backPages.push(url);//add current page to the stack so you can go back there
url=e.getURL();
forwardPages.clear();
setPage(url);
} catch (MalformedURLException e1) {
e1.printStackTrace();
}
}
}});
}
public void setPage(URL s) {
try {
theView.setPage(s);
add.setText(s.toString());
updateMenus();
} catch (IOException e) {
e.printStackTrace();
}
}
public JMenuBar getMenu() {
JMenuBar menu=new JMenuBar();
JMenu file = new JMenu(Messages.getString("NoteTab.4")); //$NON-NLS-1$
JMenuItem open = new JMenuItem(Messages.getString("NoteTab.7")); //$NON-NLS-1$
JMenuItem save = new JMenuItem(Messages.getString("NoteTab.6")); //$NON-NLS-1$
JMenuItem print = new JMenuItem(Messages.getString("NoteTab.8")); //$NON-NLS-1$
open.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {
open();

}});
save.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {
saveAs();

}});
file.add(open);
file.add(save);
file.add(print);
menu.add(file);

JMenu edit=new JMenu(Messages.getString("NoteTab.19")); //$NON-NLS-1$
JMenuItem cut = new JMenuItem(Messages.getString("NoteTab.20")); //$NON-NLS-1$
cut.addActionListener(getActionByName(DefaultEditorKit.cutAction));
JMenuItem copy = new JMenuItem(Messages.getString("NoteTab.21")); //$NON-NLS-1$
copy.addActionListener(getActionByName(DefaultEditorKit.copyAction));
JMenuItem paste = new JMenuItem(Messages.getString("NoteTab.22")); //$NON-NLS-1$
paste.addActionListener(getActionByName(DefaultEditorKit.pasteAction));
JMenuItem selectAll = new JMenuItem(Messages.getString("NoteTab.23")); //$NON-NLS-1$
selectAll
.addActionListener(getActionByName(DefaultEditorKit.selectAllAction));
edit.add(cut);
edit.add(copy);
edit.add(paste);
edit.add(selectAll);
menu.add(edit);
JMenu navigate=new JMenu(Messages.getString("IE.2")); //$NON-NLS-1$
back = new JMenuItem(Messages.getString("IE.3")); //$NON-NLS-1$
back.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {
URL url;
try {
url=new URL(add.getText());//current page
forwardPages.push(url);
url=(URL) backPages.pop();
setPage(url);
} catch (MalformedURLException e1) {
e1.printStackTrace();
}
}});
forward = new JMenuItem(Messages.getString("IE.4")); //$NON-NLS-1$
forward.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {
URL url;
try {
url=new URL(add.getText());//current page
backPages.push(url);
url=(URL) forwardPages.pop();
setPage(url);
} catch (MalformedURLException e1) {
e1.printStackTrace();
}
}});
JMenuItem home=new JMenuItem(Messages.getString("IE.5")); //$NON-NLS-1$
navigate.add(back);
navigate.add(forward);
navigate.add(home);
menu.add(navigate);
JMenu help=new JMenu(Messages.getString("NoteTab.44")); //$NON-NLS-1$
menu.add(help);
return menu;

}
public JPanel getNonEditor() {
JPanel p=new JPanel();
p.setLayout(new GridLayout(2,1));
p.add(getToolBar2());
p.add(getToolBar());
return p;
}
public JToolBar getToolBar2() {
JToolBar tools=new JToolBar();
backButton=new JButton();
forwardButton=new JButton();
ImageIcon icon=createImageIcon("../images/back.gif", null);
backButton.setIcon(icon);
icon=createImageIcon("../images/forward.gif", null);
forwardButton.setIcon(icon);
JButton open=new JButton();
icon=createImageIcon("../images/open.gif", null);
open.setIcon(icon);
JButton save=new JButton();
icon=createImageIcon("../images/save.gif", null);
save.setIcon(icon);
JButton cut=new JButton();
icon=createImageIcon("../images/cut.gif", null);
cut.setIcon(icon);
JButton copy=new JButton();
icon=createImageIcon("../images/copy.gif", null);
copy.setIcon(icon);
JButton paste=new JButton();
icon=createImageIcon("../images/paste.gif", null);
paste.setIcon(icon);
open.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {
open();

}});
save.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {
saveAs();

}});
cut.addActionListener(getActionByName(DefaultEditorKit.cutAction));
copy.addActionListener(getActionByName(DefaultEditorKit.copyAction));
paste.addActionListener(getActionByName(DefaultEditorKit.pasteAction));
backButton.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {
URL url;
try {
url=new URL(add.getText());//current page
forwardPages.push(url);
url=(URL) backPages.pop();
setPage(url);

0 Comments:

Post a Comment

<< Home