/** Creates the internal frame for creating (and saving) a new site @author Travis Zimmerman @version 1.0 File Name: EditSiteInternalFrame.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 EditSiteInternalFrame extends JInternalFrame { public EditSiteInternalFrame() { super("Edit 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("Edit Site"); header.setForeground( Color.black ); header.setFont( new Font( "courier", Font.BOLD, 24 )); JPanel headerPanel = new JPanel(); headerPanel.add(header); mySites = new ArrayList(); final File sitesFile = new File("sites.tez"); //read in all the sites from the file 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) {} } //else put something in the menu about no existing sites siteChooser = new JComboBox(); siteChooser.addItem("None selected"); for (int i = 0; i < mySites.size(); i++) siteChooser.addItem( ((Site)mySites.get(i)).getAddress() ); editButton = new JButton("Edit"); JPanel choicePanel = new JPanel(); choicePanel.setLayout( new GridLayout(1,2) ); choicePanel.add(siteChooser); choicePanel.add(editButton); JPanel topPanel = new JPanel(); topPanel.setLayout( new GridLayout(2,1) ); topPanel.add(headerPanel); topPanel.add(choicePanel); getContentPane().add(topPanel, 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 EditButtonListener implements ActionListener { public void actionPerformed(ActionEvent event) { //read in all the sites from the file 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) {} } int currentSelection = 0; for (int i = 0; i < mySites.size(); i++) if ( ( ((Site)mySites.get(i)).getAddress() ).equals( (String)siteChooser.getSelectedItem() ) ) currentSelection = i; addressField.setText( ((Site)mySites.get(currentSelection)).getAddress() ); companyField.setText( ((Site)mySites.get(currentSelection)).getCompany() ); designerField.setText( ((Site)mySites.get(currentSelection)).getDesigner() ); topicField.setText( ((Site)mySites.get(currentSelection)).getTopic() ); startDateField.setText( ((Site)mySites.get(currentSelection)).getStartDate() ); webmasterEmailAddressField.setText( ((Site)mySites.get(currentSelection)).getWebmasterEmailAddress() ); hostedByField.setText( ((Site)mySites.get(currentSelection)).getHost() ); hostAddressField.setText( ((Site)mySites.get(currentSelection)).getHostAddress() ); hostUserNameField.setText( ((Site)mySites.get(currentSelection)).getHostUserName() ); hostPasswordField.setText( ((Site)mySites.get(currentSelection)).getHostPassword() ); ftpAddressField.setText( ((Site)mySites.get(currentSelection)).getFTPAddress() ); ftpUserNameField.setText( ((Site)mySites.get(currentSelection)).getFTPUserName() ); ftpPasswordField.setText( ((Site)mySites.get(currentSelection)).getFTPPassword() ); } } EditButtonListener myEditButtonListener = new EditButtonListener(); editButton.addActionListener(myEditButtonListener); class SaveButtonListener implements ActionListener { public void actionPerformed(ActionEvent event) { //read in all the sites from the file 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) {} } int currentSelection = 0; if ( !((String)siteChooser.getSelectedItem()).equals("None selected") ) { for (int i = 0; i < mySites.size(); i++) if ( ( ((Site)mySites.get(i)).getAddress() ).equals( (String)siteChooser.getSelectedItem() ) ) currentSelection = i; } Site editedSite = new Site(addressField.getText()); editedSite.setCompany(companyField.getText()); editedSite.setTopic(topicField.getText()); editedSite.setDesigner(designerField.getText()); editedSite.setStartDate(startDateField.getText()); editedSite.setWebmasterEmailAddress(webmasterEmailAddressField.getText()); editedSite.setHost(hostedByField.getText()); editedSite.setHostAddress(hostAddressField.getText()); editedSite.setHostUserName(hostUserNameField.getText()); editedSite.setHostPassword(hostPasswordField.getText()); editedSite.setFTPAddress(ftpAddressField.getText()); editedSite.setFTPUserName(ftpUserNameField.getText()); editedSite.setFTPPassword(ftpPasswordField.getText()); //replace the old site with the edited site mySites.set(currentSelection, editedSite); //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 ArrayList mySites; JComboBox siteChooser; 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 editButton; JButton saveButton; JButton cancelButton; private final int WIDTH = 450; private final int HEIGHT = 460; private final int OFFSET = 30; }