SiteFrame.java Source code

Java tutorial

Introduction

Here is the source code for SiteFrame.java

Source

/*
Java Swing, 2nd Edition
By Marc Loy, Robert Eckstein, Dave Wood, James Elliott, Brian Cole
ISBN: 0-596-00408-7
Publisher: O'Reilly 
*/
// SiteFrame.java
//A simple extension of the JInternalFrame class that contains a list
//object. Elements of the list represent HTML pages for a web site.
//

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.Vector;

import javax.swing.AbstractAction;
import javax.swing.ImageIcon;
import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JLayeredPane;
import javax.swing.JList;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JToolBar;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

public class SiteFrame extends JInternalFrame {

    JList nameList;

    SiteManager parent;

    // Hardcode the pages of our "site" to keep things simple
    String[] pages = { "index.html", "page1.html", "page2.html" };

    public SiteFrame(String name, SiteManager sm) {
        super("Site: " + name, true, true, true);
        parent = sm;
        setBounds(50, 50, 250, 100);

        nameList = new JList(pages);
        nameList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        nameList.addListSelectionListener(new ListSelectionListener() {
            public void valueChanged(ListSelectionEvent lse) {
                // We know this is the list, so pop up the page.
                if (!lse.getValueIsAdjusting()) {
                    parent.addPageFrame((String) nameList.getSelectedValue());
                }
            }
        });
        Container contentPane = getContentPane();
        contentPane.add(nameList, BorderLayout.CENTER);
    }
}

//A sample Swing application that manages several internal frames. This
//is the main class for working with the SiteFrame and PageFrame classes.
//

class SiteManager extends JFrame {

    JLayeredPane desktop;

    Vector popups = new Vector();

    public SiteManager() {
        super("Web Site Manager");
        setSize(450, 250);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        Container contentPane = getContentPane();

        JToolBar jtb = new JToolBar();
        jtb.add(new CutAction(this));
        jtb.add(new CopyAction(this));
        jtb.add(new PasteAction(this));
        contentPane.add(jtb, BorderLayout.NORTH);

        // Add our LayeredPane object for the internal frames.
        desktop = new JDesktopPane();
        contentPane.add(desktop, BorderLayout.CENTER);
        addSiteFrame("Sample");
    }

    public static void main(String args[]) {
        SiteManager mgr = new SiteManager();
        mgr.setVisible(true);
    }

    // Methods to create our internal frames
    public void addSiteFrame(String name) {
        SiteFrame sf = new SiteFrame(name, this);
        popups.addElement(sf);
        desktop.add(sf, new Integer(2)); // Keep sites on top for now
        sf.setVisible(true);
    }

    public void addPageFrame(String name) {
        PageFrame pf = new PageFrame(name, this);
        desktop.add(pf, new Integer(1));
        pf.setVisible(true);
        pf.setIconifiable(true);
        popups.addElement(pf);
    }

    public JInternalFrame getCurrentFrame() {
        for (int i = 0; i < popups.size(); i++) {
            JInternalFrame currentFrame = (JInternalFrame) popups.elementAt(i);
            if (currentFrame.isSelected()) {
                return currentFrame;
            }
        }
        return null;
    }
}

class PasteAction extends AbstractAction {
    SiteManager manager;

    public PasteAction(SiteManager sm) {
        super("", new ImageIcon("paste.gif"));
        manager = sm;
    }

    public void actionPerformed(ActionEvent ae) {
        JInternalFrame currentFrame = manager.getCurrentFrame();
        if (currentFrame == null) {
            return;
        }
        // cannot cut or paste sites
        if (currentFrame instanceof SiteFrame) {
            return;
        }
        ((PageFrame) currentFrame).pasteText();
    }
}

class PageFrame extends JInternalFrame implements ActionListener {

    SiteManager parent;

    String filename;

    JTextArea ta;

    public PageFrame(String name, SiteManager sm) {
        super("Page: " + name, true, true, true, true);
        parent = sm;
        setBounds(50, 50, 300, 150);

        Container contentPane = getContentPane();

        // Create a text area to display the contents of our file in
        // and stick it in a scrollable pane so we can see everything
        ta = new JTextArea();
        JScrollPane jsp = new JScrollPane(ta);
        contentPane.add(jsp, BorderLayout.CENTER);

        JMenuBar jmb = new JMenuBar();
        JMenu fileMenu = new JMenu("File");
        JMenuItem saveItem = new JMenuItem("Save");
        saveItem.addActionListener(this);
        fileMenu.add(saveItem);
        jmb.add(fileMenu);
        setJMenuBar(jmb);

        filename = name;
        loadContent();
    }

    public void actionPerformed(ActionEvent ae) {
        // Can only be the save menu
        saveContent();
    }

    public void loadContent() {
        try {
            FileReader fr = new FileReader(filename);
            ta.read(fr, null);
            fr.close();
        } catch (Exception e) {
            System.err.println("Could not load page: " + filename);
        }
    }

    public void saveContent() {
        try {
            FileWriter fw = new FileWriter(filename);
            ta.write(fw);
            fw.close();
        } catch (Exception e) {
            System.err.println("Could not save page: " + filename);
        }
    }

    public void cutText() {
        ta.cut();
    }

    public void copyText() {
        ta.copy();
    }

    public void pasteText() {
        ta.paste();
    }
}

class CutAction extends AbstractAction {
    SiteManager manager;

    public CutAction(SiteManager sm) {
        super("", new ImageIcon("cut.gif"));
        manager = sm;
    }

    public void actionPerformed(ActionEvent ae) {
        JInternalFrame currentFrame = manager.getCurrentFrame();
        if (currentFrame == null) {
            return;
        }
        // cannot cut or paste sites
        if (currentFrame instanceof SiteFrame) {
            return;
        }
        ((PageFrame) currentFrame).cutText();
    }
}

class CopyAction extends AbstractAction {
    SiteManager manager;

    public CopyAction(SiteManager sm) {
        super("", new ImageIcon("copy.gif"));
        manager = sm;
    }

    public void actionPerformed(ActionEvent ae) {
        JInternalFrame currentFrame = manager.getCurrentFrame();
        if (currentFrame == null) {
            return;
        }
        // can't cut or paste sites
        if (currentFrame instanceof SiteFrame) {
            return;
        }
        ((PageFrame) currentFrame).copyText();
    }
}