private void saveDataToFile(File file) { try { PrintStream out = new PrintStream(new FileOutputStream(file)); // Print header line out.print("Time"); for (Sequence seq : seqs) { out.print("," + seq.name); } out.println(); // Print data lines if (seqs.size() > 0 && seqs.get(0).size > 0) { for (int i = 0; i < seqs.get(0).size; i++) { double excelTime = toExcelTime(times.time(i)); out.print(String.format(Locale.ENGLISH, "%.6f", excelTime)); for (Sequence seq : seqs) { out.print("," + getFormattedValue(seq.value(i), false)); } out.println(); } } out.close(); JOptionPane.showMessageDialog( this, Resources.format( Messages.FILE_CHOOSER_SAVED_FILE, file.getAbsolutePath(), file.length())); } catch (IOException ex) { String msg = ex.getLocalizedMessage(); String path = file.getAbsolutePath(); if (msg.startsWith(path)) { msg = msg.substring(path.length()).trim(); } JOptionPane.showMessageDialog( this, Resources.format(Messages.FILE_CHOOSER_SAVE_FAILED_MESSAGE, path, msg), Messages.FILE_CHOOSER_SAVE_FAILED_TITLE, JOptionPane.ERROR_MESSAGE); } }
public void validateJSON(final String json) { boolean valid = false; String msg = null; try { final JsonParser parser = new ObjectMapper().getJsonFactory().createJsonParser(json); while (parser.nextToken() != null) {} valid = true; } catch (JsonParseException jpe) { msg = jpe.getLocalizedMessage(); } catch (IOException ioe) { msg = ioe.getLocalizedMessage(); } if (!valid) { JOptionPane.showMessageDialog( this, "<html><p>JSON contents is not valid. Please check:</p><p>" + msg, "Success", JOptionPane.ERROR_MESSAGE); } else { JOptionPane.showMessageDialog( this, "JSON contents is valid!", "Success", JOptionPane.INFORMATION_MESSAGE); } }
public fileBackupProgram(JFrame frame) { super(new BorderLayout()); this.frame = frame; errorDialog = new CustomDialog(frame, "Please enter a new name for the error log", this); errorDialog.pack(); moveDialog = new CustomDialog(frame, "Please enter a new name for the move log", this); moveDialog.pack(); printer = new FilePrinter(); timers = new ArrayList<>(); log = new JTextArea(5, 20); log.setMargin(new Insets(5, 5, 5, 5)); log.setEditable(false); JScrollPane logScrollPane = new JScrollPane(log); Object obj; copy = true; listModel = new DefaultListModel(); // destListModel = new DefaultListModel(); directoryList = new directoryStorage(); // Create a file chooser fc = new JFileChooser(); // Create the menu bar. menuBar = new JMenuBar(); // Build the first menu. menu = new JMenu("File"); menu.getAccessibleContext() .setAccessibleDescription("The only menu in this program that has menu items"); menuBar.add(menu); editError = new JMenuItem("Save Error Log As..."); editError .getAccessibleContext() .setAccessibleDescription("Change the name of the error log file"); editError.addActionListener(new ErrorListener()); menu.add(editError); editMove = new JMenuItem("Save Move Log As..."); editMove .getAccessibleContext() .setAccessibleDescription("Change the name of the move log file"); editMove.addActionListener(new MoveListener()); menu.add(editMove); exit = new JMenuItem("Exit"); exit.getAccessibleContext().setAccessibleDescription("Exit the Program"); exit.addActionListener(new CloseListener()); menu.add(exit); frame.setJMenuBar(menuBar); // Uncomment one of the following lines to try a different // file selection mode. The first allows just directories // to be selected (and, at least in the Java look and feel, // shown). The second allows both files and directories // to be selected. If you leave these lines commented out, // then the default mode (FILES_ONLY) will be used. // fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); // fc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES); openButton = new JButton(openString); openButton.setActionCommand(openString); openButton.addActionListener(new OpenListener()); destButton = new JButton(destString); destButton.setActionCommand(destString); destButton.addActionListener(new DestListener()); // Create the save button. We use the image from the JLF // Graphics Repository (but we extracted it from the jar). saveButton = new JButton(saveString); saveButton.setActionCommand(saveString); saveButton.addActionListener(new SaveListener()); URL imageURL = getClass().getResource(greenButtonIcon); ImageIcon greenSquare = new ImageIcon(imageURL); startButton = new JButton("Start", greenSquare); startButton.setSize(60, 20); startButton.setHorizontalTextPosition(AbstractButton.LEADING); startButton.setActionCommand("Start"); startButton.addActionListener(new StartListener()); imageURL = getClass().getResource(redButtonIcon); ImageIcon redSquare = new ImageIcon(imageURL); stopButton = new JButton("Stop", redSquare); stopButton.setSize(60, 20); stopButton.setHorizontalTextPosition(AbstractButton.LEADING); stopButton.setActionCommand("Stop"); stopButton.addActionListener(new StopListener()); copyButton = new JRadioButton("Copy"); copyButton.setActionCommand("Copy"); copyButton.setSelected(true); copyButton.addActionListener(new RadioListener()); moveButton = new JRadioButton("Move"); moveButton.setActionCommand("Move"); moveButton.addActionListener(new RadioListener()); ButtonGroup group = new ButtonGroup(); group.add(copyButton); group.add(moveButton); // For layout purposes, put the buttons in a separate panel JPanel optionPanel = new JPanel(); GroupLayout layout = new GroupLayout(optionPanel); optionPanel.setLayout(layout); layout.setAutoCreateGaps(true); layout.setAutoCreateContainerGaps(true); layout.setHorizontalGroup( layout.createSequentialGroup().addComponent(copyButton).addComponent(moveButton)); layout.setVerticalGroup( layout .createSequentialGroup() .addGroup( layout .createParallelGroup(GroupLayout.Alignment.BASELINE) .addComponent(copyButton) .addComponent(moveButton))); JPanel buttonPanel = new JPanel(); // use FlowLayout layout = new GroupLayout(buttonPanel); buttonPanel.setLayout(layout); layout.setAutoCreateGaps(true); layout.setAutoCreateContainerGaps(true); layout.setHorizontalGroup( layout .createSequentialGroup() .addGroup( layout .createParallelGroup(GroupLayout.Alignment.LEADING) .addComponent(openButton) .addComponent(optionPanel)) .addComponent(destButton) .addComponent(startButton) .addComponent(stopButton) // .addComponent(saveButton) ); layout.setVerticalGroup( layout .createSequentialGroup() .addGroup( layout .createParallelGroup(GroupLayout.Alignment.BASELINE) .addComponent(openButton) .addComponent(destButton) .addComponent(startButton) .addComponent(stopButton) // .addComponent(saveButton) ) .addComponent(optionPanel)); buttonPanel.add(optionPanel); /* buttonPanel.add(openButton); buttonPanel.add(destButton); buttonPanel.add(startButton); buttonPanel.add(stopButton); buttonPanel.add(saveButton); buttonPanel.add(listLabel); buttonPanel.add(copyButton); buttonPanel.add(moveButton); */ destButton.setEnabled(false); startButton.setEnabled(false); stopButton.setEnabled(false); // Add the buttons and the log to this panel. // add(logScrollPane, BorderLayout.CENTER); JLabel listLabel = new JLabel("Monitored Directory:"); listLabel.setLabelFor(list); // Create the list and put it in a scroll pane. list = new JList(listModel); list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); list.setSelectedIndex(0); list.addListSelectionListener(this); list.setVisibleRowCount(8); JScrollPane listScrollPane = new JScrollPane(list); JPanel listPane = new JPanel(); listPane.setLayout(new BorderLayout()); listPane.add(listLabel, BorderLayout.PAGE_START); listPane.add(listScrollPane, BorderLayout.CENTER); listSelectionModel = list.getSelectionModel(); listSelectionModel.addListSelectionListener(new SharedListSelectionHandler()); // monitored, destination, waitInt, check destination = new JLabel("Destination Directory: "); waitField = new JFormattedTextField(); // waitField.setValue(240); waitField.setEditable(false); waitField.addPropertyChangeListener(new FormattedTextListener()); waitInt = new JLabel("Wait Interval (in minutes)"); // waitInt.setLabelFor(waitField); checkField = new JFormattedTextField(); checkField.setSize(1, 10); // checkField.setValue(60); checkField.setEditable(false); checkField.addPropertyChangeListener(new FormattedTextListener()); check = new JLabel("Check Interval (in minutes)"); // check.setLabelFor(checkField); fireButton = new JButton(fireString); fireButton.setActionCommand(fireString); fireButton.addActionListener(new FireListener()); JPanel fieldPane = new JPanel(); // fieldPane.add(destField); layout = new GroupLayout(fieldPane); fieldPane.setLayout(layout); layout.setAutoCreateGaps(true); layout.setAutoCreateContainerGaps(true); layout.setHorizontalGroup( layout .createSequentialGroup() .addGroup( layout .createParallelGroup(GroupLayout.Alignment.LEADING) .addComponent(waitInt) .addComponent(check)) .addGroup( layout .createParallelGroup(GroupLayout.Alignment.LEADING) .addComponent(waitField, 60, 60, 60) .addComponent(checkField, 60, 60, 60))); layout.setVerticalGroup( layout .createSequentialGroup() .addGroup( layout .createParallelGroup(GroupLayout.Alignment.BASELINE) .addComponent(waitInt) .addComponent(waitField)) .addGroup( layout .createParallelGroup(GroupLayout.Alignment.BASELINE) .addComponent(check) .addComponent(checkField))); JPanel labelPane = new JPanel(); labelPane.setLayout(new BorderLayout()); labelPane.add(destination, BorderLayout.PAGE_START); labelPane.add(fieldPane, BorderLayout.CENTER); layout = new GroupLayout(labelPane); labelPane.setLayout(layout); layout.setAutoCreateGaps(true); layout.setAutoCreateContainerGaps(true); layout.setHorizontalGroup( layout .createSequentialGroup() .addGroup( layout .createParallelGroup(GroupLayout.Alignment.LEADING) .addComponent(destination) .addComponent(fieldPane))); layout.setVerticalGroup( layout.createSequentialGroup().addComponent(destination).addComponent(fieldPane)); // labelPane.add(destination); // labelPane.add(fieldPane); try { // Read from disk using FileInputStream FileInputStream f_in = new FileInputStream(LOG_DIRECTORY + "\\save.data"); // Read object using ObjectInputStream ObjectInputStream obj_in = new ObjectInputStream(f_in); // Read an object directoryList = (directoryStorage) obj_in.readObject(); ERROR_LOG_NAME = (String) obj_in.readObject(); MOVE_LOG_NAME = (String) obj_in.readObject(); if (ERROR_LOG_NAME instanceof String) { printer.changeErrorLogName(ERROR_LOG_NAME); } if (MOVE_LOG_NAME instanceof String) { printer.changeMoveLogName(MOVE_LOG_NAME); } if (directoryList instanceof directoryStorage) { System.out.println("found object"); // directoryList = (directoryStorage) obj; Iterator<Directory> directories = directoryList.getDirectories(); Directory d; while (directories.hasNext()) { d = directories.next(); try { listModel.addElement(d.getDirectory().toRealPath()); } catch (IOException x) { printer.printError(x.toString()); } int index = list.getSelectedIndex(); if (index == -1) { list.setSelectedIndex(0); } index = list.getSelectedIndex(); Directory dir = directoryList.getDirectory(index); destButton.setEnabled(true); checkField.setValue(dir.getInterval()); waitField.setValue(dir.getWaitInterval()); checkField.setEditable(true); waitField.setEditable(true); // directoryList.addNewDirectory(d); // try { // listModel.addElement(d.getDirectory().toString()); // } catch (IOException x) { // printer.printError(x.toString()); // } // timer = new Timer(); // timer.schedule(new CopyTask(d.directory, d.destination, d.getWaitInterval(), printer, // d.copy), 0, d.getInterval()); } } else { System.out.println("did not find object"); } obj_in.close(); } catch (ClassNotFoundException x) { printer.printError(x.getLocalizedMessage()); System.err.format("Unable to read"); } catch (IOException y) { printer.printError(y.getLocalizedMessage()); } // Layout the text fields in a panel. JPanel buttonPane = new JPanel(); buttonPane.setLayout(new BoxLayout(buttonPane, BoxLayout.LINE_AXIS)); buttonPane.add(fireButton); buttonPane.add(Box.createHorizontalStrut(5)); buttonPane.add(new JSeparator(SwingConstants.VERTICAL)); buttonPane.add(Box.createHorizontalStrut(5)); buttonPane.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5)); add(buttonPanel, BorderLayout.PAGE_START); add(listPane, BorderLayout.LINE_START); // add(destListScrollPane, BorderLayout.CENTER); add(fieldPane, BorderLayout.LINE_END); add(labelPane, BorderLayout.CENTER); add(buttonPane, BorderLayout.PAGE_END); }