private File showFileChooser(boolean isOpen, File fileName) { File file = null; if (!config.getOs().trim().toLowerCase().equals("weblookandfeel")) { JFileChooser jFileChooser = new JFileChooser(); jFileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES); jFileChooser.setFileFilter(new FileNameExtensionFilter("*.xml", "xml")); jFileChooser.setCurrentDirectory(new File("Setting")); if (fileName != null) { jFileChooser.setSelectedFile(fileName); } int option; JFrame fileFrame = new JFrame(); fileFrame.setIconImage(iconImage); if (isOpen) { option = jFileChooser.showOpenDialog(fileFrame); } else { option = jFileChooser.showSaveDialog(fileFrame); } if (option == JFileChooser.APPROVE_OPTION) { file = jFileChooser.getSelectedFile(); if (file.isDirectory()) { File[] files = file.listFiles(new FilenameFilterTest()); System.out.println(files.length); for (File fl : files) { System.out.println(fl.getName()); } } } } else { boolean ok = false; WebFileChooser wfc = new WebFileChooser(new JFrame()); wfc.setCurrentDirectory(new File("Setting")); wfc.setChooseFilter(new FileExtensionFilter("xml")); wfc.setSelectionMode(SelectionMode.SINGLE_SELECTION); wfc.setAvailableFilter(new FileExtensionFilter("xml")); wfc.setIconImage(iconImage); wfc.setOkListener( new ActionListener() { @Override public void actionPerformed(ActionEvent arg0) { System.out.println(arg0.getSource()); } }); wfc.setVisible(true); file = wfc.getSelectedFile(); } if (file == null) { showMessage("Operation was canceled."); } return file; }
private void setOSinConfig(String st) { config.setOs(st); showMessage("The change affects from next time you launch."); }
/** Create the frame. */ public HappyWaysCrecheApp() { addWindowListener( new WindowAdapter() { @Override public void windowClosing(WindowEvent arg0) { try { Object[] objs = {config, persons}; ofh.writeIntoFile(objs); // ofh.writeIntoFile(persons); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }); /* * Read default setting file and set current maximum id number for each subclass of person * as currentID so that new instances get id after that. */ Object[] objs = null; try { // Create "Setting" Directory if it doesn't exist File file = new File("Setting"); if (!file.exists() || !file.isDirectory()) { file.mkdir(); } // Get list of persons objs = ofh.readFromFile(); persons = (List<Person>) objs[1]; // persons = ofh.readFromFile(); PersonList list = (PersonList) persons; // Set currentID (which is variable of PersonID) for each subclass of Person int pi = list.findBorder(0, 0, list.size() - 1); int ci = list.findBorder(1, pi, list.size() - 1); // System.out.println("pi = "+pi+" ci = "+ci); if (pi != -1 && list.get(pi) instanceof Person) { PersonID.setParentCurrent(list.get(pi).getID()); } if ((ci - pi) > 0 && list.get(ci) instanceof Child) { PersonID.setChildCurrent(list.get(ci).getID()); } } catch (Exception e) { System.err.println(e); } try { config = (ConfigFile) objs[0]; } catch (Exception e) { e.printStackTrace(); config = new ConfigFile("default"); } setLookAndFeel(config.getOs()); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(100, 100, 400, 600); setTitle("HappyWaysCrecheApp"); JMenuBar menuBar = new JMenuBar(); setJMenuBar(menuBar); JMenu mnFile = new JMenu("File"); mnFile.setMnemonic('f'); menuBar.add(mnFile); JMenuItem mntmImport = new JMenuItem("Import"); mntmImport.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent arg0) { try { File file = showFileChooser(true, null); if (file != null) { persons = ofh.readFromXML(file); showMessage("Import sucessful."); } } catch (Exception e) { showMessage(e.getMessage()); // e.printStackTrace(); } // System.out.println("Parent Current = " + PersonID.getParentCurrent() + // " Child Current = " + PersonID.getChildCurrent()); } }); mnFile.add(mntmImport); JMenuItem mntmExport = new JMenuItem("Export"); mntmExport.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent arg0) { try { Date date = new Date(); File fileName = new File("Persons_" + date.getTime() + ".xml"); File file = showFileChooser(false, fileName); if (file != null) { if (!file.getName().endsWith(".xml")) { file = new File(file.getPath() + ".xml"); } boolean stop = false; if (file.exists()) { int response = JOptionPane.showConfirmDialog( null, "The file (" + file.getPath() + ") exists. " + "Would you like to overwrite it?", "Overwritting File", JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE, null); if (response == JOptionPane.CANCEL_OPTION) { showMessage("Operation canceled"); stop = true; } } if (!stop) { ObjectFileHandler ofh = new ObjectFileHandler(); ofh.writeIntoXML(persons, file); showMessage("Exported to " + file.getPath()); } } } catch (NullPointerException e) { } catch (Exception e) { showMessage(e.getMessage()); } } }); mnFile.add(mntmExport); JMenu mnOption = new JMenu("Option"); mnOption.setMnemonic('o'); menuBar.add(mnOption); JMenuItem mntmIdSetting = new JMenuItem("ID Setting"); mntmIdSetting.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent arg0) { PersonIDgui pidGui = new PersonIDgui(persons); pidGui.setIconImage(iconImage); pidGui.setVisible(true); } }); mnOption.add(mntmIdSetting); JMenuItem mntmDefragId = new JMenuItem("Defrag ID"); mntmDefragId.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent arg0) { PersonList list = (PersonList) persons; try { int response = JOptionPane.showConfirmDialog( null, "Are you sure to defrag ID of Persons", "Defragging ID", JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE, null); if (response == JOptionPane.CANCEL_OPTION) { // System.exit(0); } else { File file = new File("(|Not Saved|)"); int response2 = JOptionPane.showConfirmDialog( null, "Do you want to save custom name ID_Backup file?\n" + "This file can be used when you need to restore old ID", "Saving backup option", JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE, null); if (response2 == JOptionPane.CANCEL_OPTION) { List<IdBackup> idList = list.defragId(); file = new File("Setting/ID_Backup.xml"); ObjectFileHandler ofh = new ObjectFileHandler(); ofh.writeIdBackupXML(idList, file); } else { Date date = new Date(); File fileName = new File("ID_Backup_" + date.getTime() + ".xml"); file = showFileChooser(false, fileName); if (!file.getName().equals("(|Not Saved|)")) { if (!file.getName().endsWith(".xml")) { file = new File(file.getPath() + ".xml"); } List<IdBackup> idList = null; if (file.exists()) { int response3 = JOptionPane.showConfirmDialog( null, "The file (" + file.getPath() + ") exists. " + "Would you like to overwrite it?", "Overwritting File", JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE, null); if (response3 == JOptionPane.CANCEL_OPTION) { showMessage("Operation canceled"); } else { idList = list.defragId(); } } else { idList = list.defragId(); } if (idList != null) { ObjectFileHandler ofh = new ObjectFileHandler(); ofh.writeIdBackupXML(idList, file); showMessage( "ID was defragged and backup file was saved to " + file.getPath()); } } } showMessage("Defrag ID done. Backup file name is " + file.getName()); } } catch (IdOverflowException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (NoPrimaryKeyFoundException e) { String idList = ""; for (Integer i : e.getErrorId()) { idList += i + ","; } JOptionPane.showMessageDialog( null, "Parent ID could not be found for Children ID = " + idList + "\nTherefore Parent ID was set to 0 for these Children"); } } }); mnOption.add(mntmDefragId); JMenuItem mntmRestoreOldId = new JMenuItem("Restore Old ID"); mntmRestoreOldId.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent arg0) { int response = JOptionPane.showConfirmDialog( null, "Are you sure to restore ID of Persons to old ID?\n" + "This doesn't restore the length of ID\n" + "so please check old length of ID and configure it later.", "Defragging ID", JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE, null); if (response == JOptionPane.CANCEL_OPTION) { } else { try { PersonList list = (PersonList) persons; List<IdBackup> idList; File file = showFileChooser(true, null); if (file != null) { idList = ofh.readIdBackupXML(file); list.restorePrimaryKey(idList); showMessage("ID was restored to OLD IDs"); } } catch (JDOMException e) { showMessage(e.getMessage()); } catch (IOException e) { showMessage(e.getMessage()); } catch (NoPrimaryKeyFoundException e) { showMessage(e.getMessage()); } catch (IdOverflowException e) { showMessage(e.getMessage()); } catch (NullPointerException e) { } } } }); mnOption.add(mntmRestoreOldId); JMenu mnLookAndFeel = new JMenu("Look and Feel"); mnOption.add(mnLookAndFeel); chckbxmntmDefault = new JCheckBoxMenuItem("Default"); chckbxmntmDefault.setSelected(true); chckbxmntmDefault.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent arg0) { selectOneCheckBoxMenu(arg0); setOSinConfig("default"); } }); mnLookAndFeel.add(chckbxmntmDefault); chckbxmntmWindows = new JCheckBoxMenuItem("Windows"); chckbxmntmWindows.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { selectOneCheckBoxMenu(e); setOSinConfig("windows"); } }); mnLookAndFeel.add(chckbxmntmWindows); chckbxmntmSeaglass = new JCheckBoxMenuItem("Seaglass"); chckbxmntmSeaglass.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { selectOneCheckBoxMenu(e); setOSinConfig("mac"); } }); mnLookAndFeel.add(chckbxmntmSeaglass); chckbxmntmJavaMetal = new JCheckBoxMenuItem("Java Metal"); chckbxmntmJavaMetal.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { selectOneCheckBoxMenu(e); setOSinConfig("javametal"); } }); mnLookAndFeel.add(chckbxmntmJavaMetal); chckbxmntmWebLaf = new JCheckBoxMenuItem("Web LaF"); chckbxmntmWebLaf.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent arg0) { selectOneCheckBoxMenu(arg0); setOSinConfig("weblookandfeel"); } }); mnLookAndFeel.add(chckbxmntmWebLaf); JMenu mnHelp = new JMenu("Help"); mnHelp.setMnemonic('h'); menuBar.add(mnHelp); JMenuItem mntmAbout = new JMenuItem("About"); mntmAbout.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent arg0) { SubFrame frame = new SubFrame( new String[] {"OK", "Cancel"}, new About(persons), "About this APP", iconImage); frame.setVisible(true); } }); mnHelp.add(mntmAbout); contentPane = new JPanel(); contentPane.setBackground(Color.WHITE); contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); contentPane.setLayout(new BorderLayout(0, 0)); setContentPane(contentPane); JPanel menuPane = new JPanel(); contentPane.add(menuPane, BorderLayout.CENTER); menuPane.setLayout(new GridLayout(0, 1, 0, 0)); JButton btnRegPerson = new JButton("1. Register a Person"); btnRegPerson.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent arg0) { switchCommand(1); } }); btnRegPerson.setHorizontalAlignment(SwingConstants.LEFT); btnRegPerson.setMnemonic('1'); menuPane.add(btnRegPerson); JButton btnRegChild = new JButton("2. Register a Child with a Parent"); btnRegChild.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { switchCommand(2); } }); btnRegChild.setHorizontalAlignment(SwingConstants.LEFT); btnRegChild.setMnemonic('2'); menuPane.add(btnRegChild); JButton btnListAll = new JButton("3. List All Parents & Children"); btnListAll.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { switchCommand(3); } }); btnListAll.setHorizontalAlignment(SwingConstants.LEFT); btnListAll.setMnemonic('3'); menuPane.add(btnListAll); JButton btnListChild = new JButton("4. List All Children of Parent (based on ID)"); btnListChild.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { switchCommand(4); } }); btnListChild.setHorizontalAlignment(SwingConstants.LEFT); btnListChild.setMnemonic('4'); menuPane.add(btnListChild); JButton btnCalIncome = new JButton("5. Calculate Weekly Income for All Children"); btnCalIncome.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { switchCommand(5); } }); btnCalIncome.setHorizontalAlignment(SwingConstants.LEFT); btnCalIncome.setMnemonic('5'); menuPane.add(btnCalIncome); JButton btnSeach = new JButton("6. Search for a Person (based on First Name & Last Name)"); btnSeach.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { switchCommand(6); } }); btnSeach.setHorizontalAlignment(SwingConstants.LEFT); btnSeach.setMnemonic('6'); menuPane.add(btnSeach); JButton btnRemPerson = new JButton("7. Remove Person(s) (based on ID)"); btnRemPerson.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { switchCommand(7); } }); btnRemPerson.setHorizontalAlignment(SwingConstants.LEFT); btnRemPerson.setMnemonic('7'); menuPane.add(btnRemPerson); JButton btnExitSystem = new JButton("8. Exit System"); btnExitSystem.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { switchCommand(8); } }); btnExitSystem.setHorizontalAlignment(SwingConstants.LEFT); btnExitSystem.setMnemonic('8'); menuPane.add(btnExitSystem); JPanel commandPane = new JPanel(); contentPane.add(commandPane, BorderLayout.SOUTH); commandPane.setLayout(new BorderLayout(0, 0)); txf_Com = new JTextField(); txf_Com.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { switchCommand(txf_Com.getText()); } }); commandPane.add(txf_Com, BorderLayout.CENTER); txf_Com.setColumns(10); JButton btnOk = new JButton("OK"); btnOk.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent arg0) { switchCommand(txf_Com.getText()); } }); commandPane.add(btnOk, BorderLayout.EAST); /** Create headerImage and Icon */ try { iconImage = ImageUtility.retrieveImageWithResize("iconLogo.png", 128, 128); this.setIconImage(iconImage); headerImage = ImageUtility.retrieveImage("largeLogo.jpg"); headerImage = ImageUtility.resizeImageKeepRatioWithW(headerImage, this.getSize().width - 40); } catch (Exception e) { e.printStackTrace(); } if (headerImage != null) { JLabel lblHead = new JLabel(new ImageIcon(headerImage)); contentPane.add(lblHead, BorderLayout.NORTH); } }