Skip to content

Instantly share code, notes, and snippets.

@dpzmick
Created October 17, 2014 04:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dpzmick/dce3b9458e8f56cc9588 to your computer and use it in GitHub Desktop.
Save dpzmick/dce3b9458e8f56cc9588 to your computer and use it in GitHub Desktop.
package htmlEditor;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
import java.awt.datatransfer.*;
import javax.swing.undo.*;
import javax.swing.event.*;
import javax.swing.text.*;
import java.util.*;
public class HtmlEdit extends JFrame implements WindowListener, ActionListener{
String data;
JTextArea main1;
int size2 = 16;
public int edited;
File fileName = new File("[Untitled].html");
JFileChooser fileChooser;
KeyListener keyListener;
String title = "Html Editor v.2.0 - ";
protected UndoAct undoAction = new UndoAct();
protected RedoAct redoAction = new RedoAct();
String htmlText = "<html>\n<head>\n\t<title></title>\n</head>\n\n<body>\n\n</body>\n</html>";
JPopupMenu popup;
MouseListener myPopListen = new PopupListener();
public HtmlEdit() {
//main frame stuff
this.setTitle(title + fileName);
JFrame.setDefaultLookAndFeelDecorated(true);
addWindowListener(this);
//design menu stuff
//h2 heading
JMenuItem iHTwo = new JMenuItem("Insert h2 Heading");
//h1 heading
JMenuItem iHOne = new JMenuItem("Insert h1 Heading");
//cut
JMenuItem cutItem = new JMenuItem("Cut", new ImageIcon("Images" + System.getProperty("file.separator") + "cut.gif"));
cutItem.setMnemonic(KeyEvent.VK_C);
cutItem.setToolTipText("Copy selected text and delete it from the TextArea");
cutItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X, ActionEvent.CTRL_MASK));
//redo
JMenuItem redoItem = new JMenuItem(redoAction);
redoItem.setMnemonic(KeyEvent.VK_R);
redoItem.setToolTipText("Redo Typing");
redoItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Y, ActionEvent.CTRL_MASK));
//undo
JMenuItem undoItem = new JMenuItem(undoAction);
undoItem.setMnemonic(KeyEvent.VK_U);
undoItem.setToolTipText("Undo Typing");
undoItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Z, ActionEvent.CTRL_MASK));
//paste
JMenuItem pasteItem = new JMenuItem("Paste", new ImageIcon("Images" + System.getProperty("file.separator") + "paste.gif"));
pasteItem.setMnemonic(KeyEvent.VK_P);
pasteItem.setToolTipText("Paste Text From Clipboard");
pasteItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V, ActionEvent.CTRL_MASK));
//copy
JMenuItem copyItem = new JMenuItem("Copy", new ImageIcon("Images" + System.getProperty("file.separator") + "copy.gif"));
copyItem.setMnemonic(KeyEvent.VK_C);
copyItem.setToolTipText("Copy selected text");
copyItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C, ActionEvent.CTRL_MASK));
//arialBold
JMenuItem arialBold = new JMenuItem("Arial Bold");
arialBold.setToolTipText("Set text to arial in Bold");
arialBold.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F2, ActionEvent.CTRL_MASK));
//arial plain
JMenuItem arialPlain = new JMenuItem("Arial Plain");
arialPlain.setToolTipText("Set the text to arial");
arialPlain.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F3, ActionEvent.CTRL_MASK));
//serif Bold
JMenuItem serifBold = new JMenuItem("Serif Bold");
serifBold.setToolTipText("Set text to Serif in Bold");
serifBold.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F4, ActionEvent.CTRL_MASK));
//Serif Plain
JMenuItem serifPlain = new JMenuItem("Serif Plain");
serifPlain.setToolTipText("Set text to Serif in plain");
serifPlain.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F5, ActionEvent.CTRL_MASK));
//button group
ButtonGroup wordWrap = new ButtonGroup();
//check box 1
JCheckBoxMenuItem wordWrapOn = new JCheckBoxMenuItem("Word Wrap On", false);
wordWrapOn.setMnemonic(KeyEvent.VK_N);
wordWrapOn.setToolTipText("Turn word wrap on");
wordWrap.add(wordWrapOn);
//Check Box 2
JCheckBoxMenuItem wordWrapOff = new JCheckBoxMenuItem("Word Wrap Off", true);
wordWrapOff.setMnemonic(KeyEvent.VK_O);
wordWrapOff.setToolTipText("Turn word wrap off");
wordWrap.add(wordWrapOff);
//new f2
JMenuItem newf2 = new JMenuItem("New Plain Text File", new ImageIcon("Images" + System.getProperty("file.separator") + "newfile.gif"));
newf2.setMnemonic(KeyEvent.VK_P);
newf2.setToolTipText("Click this button to make a new Plain Text file.");
newf2.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_2, ActionEvent.CTRL_MASK));
//new f
JMenuItem newf = new JMenuItem("New Html File", new ImageIcon("Images" + System.getProperty("file.separator") + "newfile.gif"));
newf.setMnemonic(KeyEvent.VK_H);
newf.setToolTipText("Click this button to make a new Html file.");
newf.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_1, ActionEvent.CTRL_MASK));
//open
JMenuItem open = new JMenuItem("Open", new ImageIcon("Images" + System.getProperty("file.separator") + "openfile.gif"));
open.setMnemonic(KeyEvent.VK_O);
open.setToolTipText("Click this button to open a file.");
open.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O, ActionEvent.CTRL_MASK));
//save
JMenuItem save = new JMenuItem("Save", new ImageIcon("Images" + System.getProperty("file.separator") + "savefile.gif"));
save.setMnemonic(KeyEvent.VK_S);
save.setToolTipText("Click this button to save the file.");
save.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, ActionEvent.CTRL_MASK));
//close
JMenuItem close = new JMenuItem("Close", new ImageIcon("Images" + System.getProperty("file.separator") + "close.gif"));
close.setMnemonic(KeyEvent.VK_C);
close.setToolTipText("Close");
close.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C, ActionEvent.CTRL_MASK));
//help
JMenuItem help = new JMenuItem("Help", new ImageIcon("Images" + System.getProperty("file.separator") + "help.gif"));
help.setMnemonic(KeyEvent.VK_H);
help.setToolTipText("Click to open Help Box.");
help.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F1, ActionEvent.CTRL_MASK));
//about
JMenuItem about = new JMenuItem("About", new ImageIcon("Images" + System.getProperty("file.separator") + "about.gif"));
about.setMnemonic(KeyEvent.VK_A);
about.setToolTipText("Click to open about box.");
about.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_A, ActionEvent.CTRL_MASK));
//heading menu
JMenu headings = new JMenu("Headings");
headings.setMnemonic(KeyEvent.VK_H);
//insert menu
JMenu insert = new JMenu("Insert");
insert.setMnemonic(KeyEvent.VK_I);
//text menu
JMenu text = new JMenu("Text");
text.setMnemonic(KeyEvent.VK_T);
//edit
JMenu edit = new JMenu("Edit");
edit.setMnemonic(KeyEvent.VK_E);
//m2
JMenu m2 = new JMenu("Help");
m2.setMnemonic(KeyEvent.VK_H);
//m1
JMenu m1 = new JMenu("File");
m1.setMnemonic(KeyEvent.VK_F);
//m3
JMenu m3 = new JMenu("Format");
m3.setMnemonic(KeyEvent.VK_F);
//new fm
JMenu newfm = new JMenu("New");
newfm.setMnemonic(KeyEvent.VK_N);
//Add Action listeners to menu
iHTwo.addActionListener(this);
iHOne.addActionListener(this);
serifPlain.addActionListener(this);
serifBold.addActionListener(this);
arialPlain.addActionListener(this);
arialBold.addActionListener(this);
newf.addActionListener(this);
newf2.addActionListener(this);
open.addActionListener(this);
save.addActionListener(this);
about.addActionListener(this);
help.addActionListener(this);
close.addActionListener(this);
wordWrapOff.addActionListener(this);
wordWrapOn.addActionListener(this);
copyItem.addActionListener(this);
pasteItem.addActionListener(this);
cutItem.addActionListener(this);
//add items menus
//headings
headings.add(iHOne);
headings.add(iHTwo);
//insert
insert.add(headings);
//m1
m1.add(newfm);
m1.addSeparator();
m1.add(open);
m1.add(save);
m1.addSeparator();
m1.add(close);
//m2
m2.add(help);
m2.addSeparator();
m2.add(about);
//add to m3
m3.add(wordWrapOn);
m3.add(wordWrapOff);
m3.add(text);
//add to text
text.add(arialPlain);
text.add(arialBold);
text.add(serifPlain);
text.add(serifBold);
//add to edit
edit.add(redoItem);
edit.add(undoItem);
edit.addSeparator();
edit.add(cutItem);
edit.add(copyItem);
edit.add(pasteItem);
//add to new
newfm.add(newf2);
newfm.add(newf);
//add menu items
JMenuBar jmb = new JMenuBar();
jmb.add(m1);
jmb.add(edit);
jmb.add(insert);
jmb.add(m3);
jmb.add(m2);
setJMenuBar(jmb);
// build toolbar buttons
//cut
JButton cut2 = new JButton("Cut", new ImageIcon("Images" + System.getProperty("file.separator") + "cut.gif"));
cut2.setMnemonic(KeyEvent.VK_X);
cut2.setToolTipText("Copy selected text and delete it from the TextArea");
//copy
JButton copy2 = new JButton("Copy", new ImageIcon("Images" + System.getProperty("file.separator") + "copy.gif"));
copy2.setMnemonic(KeyEvent.VK_C);
copy2.setToolTipText("Copy selected text");
//paste
JButton paste2 = new JButton("Paste", new ImageIcon("Images" + System.getProperty("file.separator") + "paste.gif"));
paste2.setMnemonic(KeyEvent.VK_C);
paste2.setToolTipText("Paste Text From Clipboard");
//new
JButton newft2 = new JButton("New Html File", new ImageIcon("Images" + System.getProperty("file.separator") + "newfile.gif"));
newft2.setMnemonic(KeyEvent.VK_N);
newft2.setToolTipText("Click this button to make a new file.");
//open
JButton open2 = new JButton("Open", new ImageIcon("Images" + System.getProperty("file.separator") + "openfile.gif"));
open2.setToolTipText("Click this button to open a file.");
open2.setMnemonic(KeyEvent.VK_O);
//save
JButton save2 = new JButton("Save", new ImageIcon("Images" + System.getProperty("file.separator") + "savefile.gif"));
save2.setToolTipText("Click this button to save the file.");
save2.setMnemonic(KeyEvent.VK_S);
//close
JButton closeAll = new JButton("Close", new ImageIcon("Images" + System.getProperty("file.separator") + "Close.gif"));
closeAll.setToolTipText("Close");
closeAll.setMnemonic(KeyEvent.VK_C);
//add action listeners to tollbar
newft2.addActionListener(this);
open2.addActionListener(this);
save2.addActionListener(this);
closeAll.addActionListener(this);
copy2.addActionListener(this);
paste2.addActionListener(this);
cut2.addActionListener(this);
// build toolbar
JToolBar bar = new JToolBar("Html Editor Toolbar");
bar.setLayout(new BoxLayout(bar,BoxLayout.Y_AXIS));
bar.add(newft2);
bar.add(open2);
bar.add(save2);
bar.add(cut2);
bar.add(copy2);
bar.add(paste2);
bar.add(closeAll);
this.getContentPane().add("West", bar);
//Text Area1
main1 = new JTextArea();
main1.setText(htmlText);
main1.setFont(new Font("Sanerif", Font.PLAIN, 16));
main1.setDragEnabled(true);
HtmlEdit.this.listenerSet();
//Scroll Pane
JScrollPane scroll = new JScrollPane(main1);
this.getContentPane().add("Center", scroll);
main1.getDocument().addUndoableEditListener(undoAction);
main1.getDocument().addUndoableEditListener(redoAction);
setExtendedState(MAXIMIZED_BOTH);
//build popup
popup = new JPopupMenu();
JMenuItem popundo = new JMenuItem(undoAction);
popundo.addActionListener(this);
JMenuItem popredo = new JMenuItem(redoAction);
popredo.addActionListener(this);
JMenuItem popcut = new JMenuItem("Cut", new ImageIcon("Images" + System.getProperty("file.separator") + "cut.gif"));
popcut.addActionListener(this);
JMenuItem popcopy = new JMenuItem("Copy", new ImageIcon("Images" + System.getProperty("file.separator") + "copy.gif"));
popcopy.addActionListener(this);
JMenuItem poppaste = new JMenuItem("Paste", new ImageIcon("Images" + System.getProperty("file.separator") + "paste.gif"));
poppaste.addActionListener(this);
popup.add(popundo);
popup.add(popredo);
popup.addSeparator();
popup.add(popcut);
popup.add(popcopy);
popup.add(poppaste);
main1.addMouseListener(myPopListen);
}
class PopupListener extends MouseAdapter{
public void mousePressed(MouseEvent e){ checkPopup(e); }
private void checkPopup(MouseEvent e){
if (e.isPopupTrigger()){
popup.show(e.getComponent(), e.getX(), e.getY());
}
}
}
public class UndoAct extends AbstractAction implements UndoableEditListener {
private UndoableEdit edit;
public UndoAct() {
super("Undo");
setEnabled(false);
}
public void updateEnabled() {
setEnabled(edit.canUndo());
}
public void undoableEditHappened(UndoableEditEvent event) {
edit = event.getEdit();
putValue(NAME, edit.getUndoPresentationName());
updateEnabled();
}
public void actionPerformed(ActionEvent ae) {
edit.undo();
updateEnabled(); // disable undo
redoAction.updateEnabled(); // enable redo
System.out.println("Undo Action Used");
}
}
public class RedoAct extends AbstractAction implements UndoableEditListener {
private UndoableEdit edit;
public RedoAct() {
super("Redo");
setEnabled(false);
}
public void updateEnabled() {
setEnabled(edit.canRedo());
}
public void undoableEditHappened(UndoableEditEvent event) {
edit = event.getEdit();
putValue(NAME, edit.getRedoPresentationName());
updateEnabled();
}
public void actionPerformed(ActionEvent ae) {
edit.redo();
updateEnabled(); // disable redo
undoAction.updateEnabled(); // enable undo
System.out.println("Redo Action Used");
}
}
public void actionPerformed(ActionEvent event){
String source = event.getActionCommand();
if (source == "Insert h2 Heading"){
main1.insert("<h2> </h2>", main1.getCaretPosition());
System.out.println("Insert h2 heading action used");
}
if (source == "Cut"){
main1.cut();
System.out.println("Cut action used");
}
if (source == "Insert h1 Heading"){
main1.insert("<h1> </h1>", main1.getCaretPosition());
System.out.println("Insert h1 heading action used");
}
if (source == "Paste"){
this.paste();
System.out.println("Paste Action Used");
}
if (source == "Copy"){
this.copy();
System.out.println("Copy Action Used");
}
if (source == "New Html File"){
this.newHtml();
System.out.println("New Html Action Used");
}
if (source == "Open"){
System.out.println("Open Action Used");
this.open();
this.setTitle(title + fileName);
}
if (source == "Save"){
System.out.println("Save Action Used");
HtmlEdit.this.save(true);
edited = 0;
}
if (source == "About"){
new about();
System.out.println("About Action Used");
}
if (source == "Help"){
new Help();
System.out.println("Help Action Used");
}
if (source == "Close"){
close();
System.out.println("Close Action Used");
}
if (source == "Word Wrap On"){
setWordWrap(true);
System.out.println("WordWrap is now ON");
}
if (source == "Word Wrap Off"){
setWordWrap(false);
System.out.println("WordWrap is now OFF");
}
if (source == "Arial Bold"){
this.main1.setFont(new Font("Arial", Font.BOLD, size2));
System.out.println("Font is Arial Bold");
}
if (source == "Arial Plain"){
this.main1.setFont(new Font("Arial", Font.PLAIN , size2));
System.out.println("Font is Arial Plain");
}
if (source == "Serif Bold"){
this.main1.setFont(new Font("Serif", Font.BOLD , size2));
System.out.println("Font is Serif Bold");
}
if (source == "Serif Plain"){
this.main1.setFont(new Font("Serif", Font.PLAIN , size2));
System.out.println("Font is Serif Plain");
}
if (source == "New Plain Text File"){
if(edited != 0){
int result2;
result2 = JOptionPane.showConfirmDialog((Component) null, "Current File Has Been Changed. Do You Want To Save?", "Save...", JOptionPane.YES_NO_CANCEL_OPTION);
if (result2 == JOptionPane.YES_OPTION){
HtmlEdit.this.save(true);
} else if (result2 == JOptionPane.CANCEL_OPTION){
return;
}else if(result2 == JOptionPane.NO_OPTION){
}
}
this.main1.setText("");
File fileName = new File("[Untitled].html");
this.setTitle(title + fileName);
edited = 0;
System.out.println("New Palin Text File Action Used");
}
}public void setWordWrap(boolean onOff){
main1.setLineWrap(onOff);
main1.setWrapStyleWord(onOff);
}
public void windowDeiconified(WindowEvent e){
}
public void windowIconified(WindowEvent e){
}
public void windowActivated(WindowEvent e){
}
public void windowDeactivated(WindowEvent e){
}
public void windowOpened(WindowEvent e){
}
public void windowClosing(WindowEvent e){
System.out.println("Window Closing!");
if(edited != 0){
int result2;
int result3;
result2 = JOptionPane.showConfirmDialog((Component) null, "Current File Has Been Changed. Do You Want To Save?", "Save...", JOptionPane.OK_OPTION);
if (result2 == JOptionPane.YES_OPTION){
HtmlEdit.this.save(true);
}else if (result2 == JOptionPane.NO_OPTION){
}
}
}
public void windowClosed(WindowEvent e){
}
protected void listenerSet(){
main1.addKeyListener
(
keyListener = new KeyListener(){
public void keyReleased(KeyEvent e)
{
}
public void keyPressed(KeyEvent e)
{
}
public void keyTyped(KeyEvent e)
{
HtmlEdit.this.edited();
}
}
);
}
protected void edited(){
edited = 1;
this.setTitle(title + fileName +"*");
}
//save + open
public void save(boolean ans){
String[] html = new String[]{"html", "htm", "shtml", "asp","css"};
String[] txt = new String[]{"text", "txt", "log"};
int result;
fileChooser = new JFileChooser(fileName);
fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
fileChooser.addChoosableFileFilter(new SimpleFileFilter(txt, "Text (.txt, .text, .log)"));
fileChooser.addChoosableFileFilter(new SimpleFileFilter(html, "HTML(,html, .htm, .shtml, .asp, .css)"));
edited = 0;
if (ans){
fileChooser.setSelectedFile(fileName);
result = fileChooser.showSaveDialog(this);
}
else{
result = fileChooser.showOpenDialog(this);
}
if (result == JFileChooser.CANCEL_OPTION){
return;
}
fileName = fileChooser.getSelectedFile();
if (fileName == null || fileName.getName().equals("")){
JOptionPane.showMessageDialog(this,"Enter a file name...","Enter a file name...",JOptionPane.ERROR_MESSAGE);
}
else{
if (ans){
try{
FileOutputStream stream = new FileOutputStream(fileName);
String watToWrite = main1.getText();
stream.write(watToWrite.getBytes());
stream.close();
}catch (IOException ioException){
JOptionPane.showMessageDialog(this,"Error Saving File. Disk may be full or protected.","Error Saving File. Disk may be full or protected.",JOptionPane.ERROR_MESSAGE);
}
}
else{
try{
FileInputStream inStream = new FileInputStream(fileName);
int max = inStream.available();
byte data[] = new byte[max];
inStream.read(data);
main1.setText(new String(data));
inStream.close();
}catch(IOException ex){
JOptionPane.showMessageDialog(this,"Error Opening File.","Open...",JOptionPane.ERROR_MESSAGE);
}
}
}
}
public void paste(){
Clipboard systemClipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
Transferable clipboardContents = systemClipboard.getContents(null);
try{
String returnText = (String) clipboardContents.getTransferData(DataFlavor.stringFlavor);
main1.replaceRange(returnText, main1.getSelectionStart() ,main1.getSelectionEnd());
}catch (UnsupportedFlavorException exr){
JOptionPane.showMessageDialog(this,"The contents on the clipboard cannot be pasted into the TextArea. The content you are trying to paste may not be text.","Error...",JOptionPane.ERROR_MESSAGE);
}catch (IOException ioe){
JOptionPane.showMessageDialog(this,"Error...","Exeption...",JOptionPane.ERROR_MESSAGE);
}
}
public void copy(){
String writeMe;
writeMe = main1.getSelectedText();
Clipboard systemClipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
Transferable transferableText = new StringSelection(writeMe);
systemClipboard.setContents(transferableText, null);
}
public void open(){
if (edited != 0){
int result;
result = JOptionPane.showConfirmDialog((Component) null, "Current File Has Been Changed. Do you want to save?","Save...",JOptionPane.YES_NO_CANCEL_OPTION);
if (result == JOptionPane.NO_OPTION){
HtmlEdit.this.save(false);
}else if (result == JOptionPane.CANCEL_OPTION){
return;
}else if (result == JOptionPane.YES_OPTION){
HtmlEdit.this.save(true);
}
}else if (edited == 0){
HtmlEdit.this.save(false);
}
}
public void newHtml(){
if(edited != 0){
int result2;
result2 = JOptionPane.showConfirmDialog((Component) null, "Current File Has Been Changed. Do You Want To Save?", "Save", JOptionPane.YES_NO_CANCEL_OPTION);
if (result2 == JOptionPane.YES_OPTION){
HtmlEdit.this.save(true);
}else if (result2 == JOptionPane.CANCEL_OPTION){
return;
}else if(result2 == JOptionPane.NO_OPTION){
}
}
this.main1.setText("<html>\n<head>\n\t<title></title>\n</head>\n\n<body>\n\n</body>\n</html>");
File fileName = new File("[Untitled].html");
this.setTitle(title + fileName);
edited = 0;
}
public void close(){
if (edited == 1){
int result = JOptionPane.showConfirmDialog((Component) null, "Current file has been changed. Would you like to save before exiting?","Save...",JOptionPane.YES_NO_CANCEL_OPTION);
if (result == JOptionPane.YES_OPTION){
HtmlEdit.this.save(true);
}
else if (result == JOptionPane.CANCEL_OPTION){
return;
}
}
this.setVisible(false);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment