/** Creates the internal frame for creating (and saving) a new site @author Travis Zimmerman @version 1.0 File Name: NewSiteInternalFrame.java */ import java.awt.*; import javax.swing.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.*; import java.util.ArrayList; public class NewSiteInternalFrame extends JInternalFrame { public NewSiteInternalFrame() { super("New Site", true, //resizable true, //closable true, //maximizable true);//iconifiable try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); } catch(Exception e) { System.out.println("Error setting native LAF: " + e); } JLabel header = new JLabel("New Site"); header.setForeground( Color.black ); header.setFont( new Font( "courier", Font.BOLD, 24 )); JPanel headerPanel = new JPanel(); headerPanel.add(header); getContentPane().add(headerPanel, BorderLayout.NORTH); JPanel addressPanel = new JPanel(); addressPanel.setLayout( new GridLayout(2,1) ); addressPanel.add(addressLabel); addressPanel.add(addressField); JPanel companyPanel = new JPanel(); companyPanel.setLayout( new GridLayout(2,1) ); companyPanel.add(companyLabel); companyPanel.add(companyField); JPanel topicPanel = new JPanel(); topicPanel.setLayout( new GridLayout(2,1) ); topicPanel.add(topicLabel); topicPanel.add(topicField); JPanel designerPanel = new JPanel(); designerPanel.setLayout( new GridLayout(2,1) ); designerPanel.add(designerLabel); designerPanel.add(designerField); JPanel startDatePanel = new JPanel(); startDatePanel.setLayout( new GridLayout(2,1) ); startDatePanel.add(startDateLabel); startDatePanel.add(startDateField); JPanel webmasterEmailAddressPanel = new JPanel(); webmasterEmailAddressPanel.setLayout( new GridLayout(2,1) ); webmasterEmailAddressPanel.add(webmasterEmailAddressLabel); webmasterEmailAddressPanel.add(webmasterEmailAddressField); JPanel hostedByPanel = new JPanel(); hostedByPanel.setLayout( new GridLayout(2,1) ); hostedByPanel.add(hostedByLabel); hostedByPanel.add(hostedByField); JPanel hostAddressPanel = new JPanel(); hostAddressPanel.setLayout( new GridLayout(2,1) ); hostAddressPanel.add(hostAddressLabel); hostAddressPanel.add(hostAddressField); JPanel hostUserNamePanel = new JPanel(); hostUserNamePanel.setLayout( new GridLayout(2,1) ); hostUserNamePanel.add(hostUserNameLabel); hostUserNamePanel.add(hostUserNameField); JPanel hostPasswordPanel = new JPanel(); hostPasswordPanel.setLayout( new GridLayout(2,1) ); hostPasswordPanel.add(hostPasswordLabel); hostPasswordPanel.add(hostPasswordField); JPanel ftpAddressPanel = new JPanel(); ftpAddressPanel.setLayout( new GridLayout(2,1) ); ftpAddressPanel.add(ftpAddressLabel); ftpAddressPanel.add(ftpAddressField); JPanel ftpUserNamePanel = new JPanel(); ftpUserNamePanel.setLayout( new GridLayout(2,1) ); ftpUserNamePanel.add(ftpUserNameLabel); ftpUserNamePanel.add(ftpUserNameField); JPanel ftpPasswordPanel = new JPanel(); ftpPasswordPanel.setLayout( new GridLayout(2,1) ); ftpPasswordPanel.add(ftpPasswordLabel); ftpPasswordPanel.add(ftpPasswordField); JPanel blankPanel = new JPanel(); blankPanel.setLayout( new GridLayout(2,1) ); blankPanel.add( new JLabel(" ") ); blankPanel.add( new JLabel(" ") ); JPanel leftPanel = new JPanel(); leftPanel.setLayout( new GridLayout(7,1) ); leftPanel.add(addressPanel); leftPanel.add(topicPanel); leftPanel.add(startDatePanel); leftPanel.add(hostedByPanel); leftPanel.add(hostAddressPanel); leftPanel.add(hostUserNamePanel); leftPanel.add(hostPasswordPanel); JPanel rightPanel = new JPanel(); rightPanel.setLayout( new GridLayout(7,1) ); rightPanel.add(companyPanel); rightPanel.add(designerPanel); rightPanel.add(webmasterEmailAddressPanel); rightPanel.add(ftpAddressPanel); rightPanel.add(ftpUserNamePanel); rightPanel.add(ftpPasswordPanel); rightPanel.add(blankPanel); JPanel mainPanel = new JPanel(); mainPanel.setLayout( new GridLayout(1,2) ); mainPanel.add(leftPanel); mainPanel.add(rightPanel); getContentPane().add(mainPanel, BorderLayout.CENTER); saveButton = new JButton("Save"); cancelButton = new JButton("Cancel"); JPanel buttonPanel = new JPanel(); buttonPanel.setLayout( new GridLayout(1,4) ); buttonPanel.add(blankPanel); buttonPanel.add(saveButton); buttonPanel.add(cancelButton); getContentPane().add(buttonPanel, BorderLayout.SOUTH); setSize(WIDTH, HEIGHT); setLocation(OFFSET, OFFSET); //-----Button Listeners & Handlers----- class SaveButtonListener implements ActionListener { public void actionPerformed(ActionEvent event) { Site newSite = new Site(addressField.getText()); newSite.setCompany(companyField.getText()); newSite.setTopic(topicField.getText()); newSite.setDesigner(designerField.getText()); newSite.setStartDate(startDateField.getText()); newSite.setWebmasterEmailAddress(webmasterEmailAddressField.getText()); newSite.setHost(hostedByField.getText()); newSite.setHostAddress(hostAddressField.getText()); newSite.setHostUserName(hostUserNameField.getText()); newSite.setHostPassword(hostPasswordField.getText()); newSite.setFTPAddress(ftpAddressField.getText()); newSite.setFTPUserName(ftpUserNameField.getText()); newSite.setFTPPassword(ftpPasswordField.getText()); ArrayList mySites = new ArrayList(); File sitesFile = new File("sites.tez"); //read in all sites (if they exist) if (sitesFile.exists()) { try { ObjectInputStream in = new ObjectInputStream(new FileInputStream(sitesFile)); mySites = (ArrayList)in.readObject(); in.close(); } catch (java.io.FileNotFoundException e) {} catch (java.io.IOException e) {} catch (java.lang.ClassNotFoundException e) {} } //add the new site to the ArrayList mySites.add(newSite); //save all the sites back to the file try { ObjectOutputStream out = new ObjectOutputStream( new FileOutputStream(sitesFile) ); out.writeObject(mySites); out.close(); } catch (java.io.FileNotFoundException e) {} catch (java.io.IOException e) {} try { setClosed(true); } catch (java.beans.PropertyVetoException e) {} } } SaveButtonListener mySaveButtonListener = new SaveButtonListener(); saveButton.addActionListener(mySaveButtonListener); class CancelButtonListener implements ActionListener { public void actionPerformed(ActionEvent event) { try { setClosed(true); } catch (java.beans.PropertyVetoException e) {} } }; CancelButtonListener myCancelButtonListener = new CancelButtonListener(); cancelButton.addActionListener(myCancelButtonListener); } //instance fields JLabel addressLabel = new JLabel("Address (URL): "); JLabel companyLabel = new JLabel("Company: "); JLabel designerLabel = new JLabel("Designer: "); JLabel topicLabel = new JLabel("Topic: "); JLabel startDateLabel = new JLabel("Launch Date: "); JLabel webmasterEmailAddressLabel = new JLabel("Webmaster Email Address: "); JLabel hostedByLabel = new JLabel("Hosted by: "); JLabel hostAddressLabel = new JLabel("Host address (URL): "); JLabel hostUserNameLabel = new JLabel("Host user name: "); JLabel hostPasswordLabel = new JLabel("Host password: "); JLabel ftpAddressLabel = new JLabel("FTP address (URL): "); JLabel ftpUserNameLabel = new JLabel("FTP user name: "); JLabel ftpPasswordLabel = new JLabel("FTP password: "); JTextField addressField = new JTextField("*Required*"); JTextField companyField = new JTextField("Not specified"); JTextField designerField = new JTextField("Not specified"); JTextField topicField = new JTextField("Not specified"); JTextField startDateField = new JTextField("Not specified"); JTextField webmasterEmailAddressField = new JTextField("Not specified"); JTextField hostedByField = new JTextField("Not specified"); JTextField hostAddressField = new JTextField("Not specified"); JTextField hostUserNameField = new JTextField("Not specified"); JTextField hostPasswordField = new JTextField("Not specified"); JTextField ftpAddressField = new JTextField("Not specified"); JTextField ftpUserNameField = new JTextField("Not specified"); JTextField ftpPasswordField = new JTextField("Not specified"); JButton saveButton; JButton cancelButton; private final int WIDTH = 450; private final int HEIGHT = 460; private final int OFFSET = 30; }