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);
} catch (MalformedURLException e1) {
e1.printStackTrace();
}
}});
forwardButton.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();
}
}});
tools.add(open);
tools.add(save);
tools.addSeparator();
tools.add(cut);
tools.add(copy);
tools.add(paste);
tools.addSeparator();
tools.add(backButton);
tools.add(forwardButton);
return tools;
}
public JToolBar getToolBar() {

JToolBar tools=new JToolBar();
JLabel addL=new JLabel("address");
tools.add(addL);
tools.addSeparator();
add=new JTextField();
add.setPreferredSize(new Dimension(500,20));
add.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {
JTextField t= (JTextField) e.getSource();
String s=t.getText();

try {
setPage(new URL(s));
} catch (MalformedURLException e1) {
e1.printStackTrace();
}

}});
tools.add(add);
return tools;
}
/**
* updates the forward and back buttons depending on whether or not they are empty
* */
public void updateMenus() {
if(backPages.isEmpty()) {
back.setEnabled(false);
backButton.setEnabled(false);
}
else {back.setEnabled(true);
backButton.setEnabled(true);
}
if(forwardPages.isEmpty()) {
forward.setEnabled(false);
forwardButton.setEnabled(false);
}
else {forward.setEnabled(true);
forwardButton.setEnabled(true);
}
}
/**
* returns a unditable JEditoPane
* */
public JEditorPane getEditor() {
JEditorPane ed=null;
ed=new JEditorPane();
ed.setEditable(false);
ed.setPreferredSize(new Dimension(500,300));
return ed;
}
public void open() {
JFileChooser fc = new JFileChooser();
fc.addChoosableFileFilter(new FileFilter() {

public boolean accept(File f) {
if (f.isDirectory()) {
return true;
}

String extension = getExtension(f);
if (extension != null) {
if (extension.equals("html")) {
return true;
} else {
return false;
}
}

return false;
}

public String getDescription() {
return "HTML Files";
}});
int returnVal = fc.showOpenDialog(IExplorer.this);//this is tricky.I dont
// understand it but..
if (returnVal == JFileChooser.APPROVE_OPTION) {
theView.setText(Messages.getString("NoteTab.47"));//need to show a new file.So clear the screen. //$NON-NLS-1$
File file = fc.getSelectedFile();
try {
FileReader in = new FileReader(file);
editor.read(in, theView.getDocument(), 0);
} catch (Exception ee) {
}
}
}
public void saveAs() {
JFileChooser fc = new JFileChooser();
int returnVal = fc.showSaveDialog(IExplorer.this);//this is tricky.I dont
// understand it but..
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
try {
FileOutputStream out = new FileOutputStream(file);
editor.write(out, theView.getDocument(), 0, theView.getDocument().getLength());
} catch (Exception ee) {
System.out.println(Messages.getString("NoteTab.45")); //$NON-NLS-1$
ee.printStackTrace();
}

}
}
private void createActionTable(JTextComponent textComponent) {
actions = new Hashtable();
Action[] actionsArray = textComponent.getActions();
for (int i = 0; i < actionsArray.length; i++) {
Action a = actionsArray[i];
actions.put(a.getValue(Action.NAME), a);
}
}

private Action getActionByName(String name) {
return (Action) (actions.get(name));
}
/**
* Get the file extension from that file
* */
public static String getExtension(File f) {
String ext = null;
String s = f.getName();
int i = s.lastIndexOf('.');

if (i > 0 && i < s.length() - 1) {
ext = s.substring(i+1).toLowerCase();
}
return ext;
}
protected static ImageIcon createImageIcon(String path,
String description) {
java.net.URL imgURL = NoteTab.class.getResource(path);
if (imgURL != null) {
return new ImageIcon(imgURL, description);
} else {
System.err.println("Couldn't find file: " + path);
return null;
}
}
public static void main(String[] args) {
IExplorer app=new IExplorer();
app.pack();
app.show();
}
}

And again we need the files Messages.java and Messages.properties to display our externalised strings. Here are the codes, same as for NoteTab.java

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

import java.util.MissingResourceException;
import java.util.ResourceBundle;

/**
* @author dicky
*/
public class Messages {
private static final String BUNDLE_NAME = "name.shabda.office.messages";//$NON-NLS-1$

private static final ResourceBundle RESOURCE_BUNDLE = ResourceBundle
.getBundle(BUNDLE_NAME);

private Messages() {
}

public static String getString(String key) {
// TODO Auto-generated method stub
try {
return RESOURCE_BUNDLE.getString(key);
} catch (MissingResourceException e) {
return '!' + key + '!';
}
}
}
And the hindi localised Messages.properties:
NoteTab.0=\u0939\u093e\u0901
NoteTab.1=\u0928\u0939\u0940\u0902
NoteTab.2=\u0906\u092a\u0915\u0940 \u090f\u0915 \u095e\u093e\u0907\u0932 \u091c\u092e\u093e \u0928\u0939\u0940\u0902 \u0939\u0948\u0964 \u0915\u094d\u092f\u093e \u0909\u0938\u0947 \u091c\u092e\u093e \u0915\u0930 \u0926\u0942\u0901 ?\n
NoteTab.3=File not saved!
NoteTab.4=\u095e\u093e\u0907\u0932
NoteTab.5=\u0928\u092f\u093e
NoteTab.6=\u091c\u092e\u093e \u0915\u0930\u094b
NoteTab.7=\u0916\u094b\u0932\u094b
NoteTab.8=\u091b\u093e\u092a\u094b
NoteTab.9=\u0939\u093e\u0901
NoteTab.10=\u0928\u0939\u0940\u0902
NoteTab.11=Cancel
NoteTab.12=\u0906\u092a\u0915\u0940 \u090f\u0915 \u095e\u093e\u0907\u0932 \u091c\u092e\u093e \u0928\u0939\u0940\u0902 \u0939\u0948\u0964 \u0915\u094d\u092f\u093e \u0909\u0938\u0947 \u091c\u092e\u093e \u0915\u0930 \u0926\u0942\u0901 ?\n
NoteTab.13=File not saved!
NoteTab.14=\u0939\u093e\u0901
NoteTab.15=\u0928\u0939\u0940\u0902
NoteTab.16=Cancel
NoteTab.17=\u0906\u092a\u0915\u0940 \u090f\u0915 \u095e\u093e\u0907\u0932 \u091c\u092e\u093e \u0928\u0939\u0940\u0902 \u0939\u0948\u0964 \u0915\u094d\u092f\u093e \u0909\u0938\u0947 \u091c\u092e\u093e \u0915\u0930 \u0926\u0942\u0901 ?\n
NoteTab.18=File not saved!
NoteTab.19=\u090f\u0921\u093f\u091f
NoteTab.20=\u0915\u093e\u091f\u094b
NoteTab.21=\u0928\u0915\u0932 \u0915\u0930\u094b
NoteTab.22=\u091a\u093f\u092a\u0915\u093e\u0913
NoteTab.23=\u0938\u092c\u0915\u094b \u091a\u0941\u0928\u094b
NoteTab.24=\u0935\u093e\u092a\u0938 \u0915\u0930\u094b
NoteTab.25=\u095e\u093f\u0930 \u0938\u0947 \u0915\u0930\u094b
NoteTab.26=\u095e\u094b\u0930\u094d\u092e\u0948\u091f
NoteTab.27=\u0932\u093e\u0932
NoteTab.28=\u0915\u093e\u0932\u093e
NoteTab.29=\u0928\u0940\u0932\u093e
NoteTab.30=\u0906\u0915\u093e\u0930
NoteTab.31=\u092c\u0921\u093e
NoteTab.32=\u092e\u0927\u094d\u092f\u092e
NoteTab.33=\u091b\u094b\u091f\u093e
NoteTab.34=\u0938\u094d\u091f\u093e\u0907\u0932
NoteTab.35=\u092e\u094b\u091f\u093e
NoteTab.36=\u0928\u0940\u091a\u0947 \u0932\u093e\u0907\u0928
NoteTab.37=\u0924\u093f\u0930\u091b\u093e
NoteTab.38=Red
NoteTab.39=Black
NoteTab.40=Blue
NoteTab.41=
NoteTab.42=
NoteTab.43=
NoteTab.44=\u092e\u0926\u0926
NoteTab.45=exeption\!
NoteTab.46=exeption\!
NoteTab.47=
NoteTab.48=
NoteTab.49=Undo
NoteTab.50=Unable to undo:
NoteTab.51=Redo
NoteTab.52=Unable to redo:
NoteTab.53=\u092f\u0939\u093e\u0901 \u091c\u092e\u093e \u0915\u0930\u094b
NoteTab.54=paste.gif
IE.2=\u0918\u0942\u092e\u094b
IE.3=\u0935\u093e\u092a\u0938 \u091c\u093e\u0913
IE.4=\u0906\u0917\u0947 \u091c\u093e\u0913
IE.5=\u0918\u0930 \u091c\u093e\u0913
PaintIt.12=About
PaintIt.14=\u092c\u0929\u094d\u0926
PaintIt.16=\u0930\u0929\u094d\u0917
PaintIt.17=\u0914\u091c\u093e\u0930
PaintIt.18=\u092f\u0902\u0924\u094d\u0930
PaintIt.19=\u092f\u0902\u0924\u094d\u0930

0 Comments:

Post a Comment

<< Home