001: /**
002: * LibreSource
003: * Copyright (C) 2004-2008 Artenum SARL / INRIA
004: * http://www.libresource.org - contact@artenum.com
005: *
006: * This file is part of the LibreSource software,
007: * which can be used and distributed under license conditions.
008: * The license conditions are provided in the LICENSE.TXT file
009: * at the root path of the packaging that enclose this file.
010: * More information can be found at
011: * - http://dev.libresource.org/home/license
012: *
013: * Initial authors :
014: *
015: * Guillaume Bort / INRIA
016: * Francois Charoy / Universite Nancy 2
017: * Julien Forest / Artenum
018: * Claude Godart / Universite Henry Poincare
019: * Florent Jouille / INRIA
020: * Sebastien Jourdain / INRIA / Artenum
021: * Yves Lerumeur / Artenum
022: * Pascal Molli / Universite Henry Poincare
023: * Gerald Oster / INRIA
024: * Mariarosa Penzi / Artenum
025: * Gerard Sookahet / Artenum
026: * Raphael Tani / INRIA
027: *
028: * Contributors :
029: *
030: * Stephane Bagnier / Artenum
031: * Amadou Dia / Artenum-IUP Blois
032: * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
033: */package org.libresource.so6.application.studio.history.viewer.ui;
034:
035: import org.libresource.so6.application.studio.history.viewer.util.FileHistoryPlayer;
036:
037: import java.awt.event.ActionEvent;
038: import java.awt.event.ActionListener;
039:
040: import java.io.File;
041: import java.io.FileWriter;
042: import java.io.PrintWriter;
043:
044: import javax.swing.JFileChooser;
045: import javax.swing.JMenu;
046: import javax.swing.JMenuBar;
047: import javax.swing.JMenuItem;
048: import javax.swing.JOptionPane;
049:
050: /**
051: * @author smack
052: */
053: public class Menu extends JMenuBar implements ActionListener {
054: private FileHistoryPlayer player;
055: private String basePath;
056:
057: public Menu(String basePath, FileHistoryPlayer player) {
058: this .basePath = basePath;
059: this .player = player;
060:
061: // init menu
062: JMenu file = new JMenu("File");
063: JMenuItem save = new JMenuItem("Save as...");
064: save.setActionCommand("SAVE");
065: save.addActionListener(this );
066:
067: JMenuItem exit = new JMenuItem("Exit");
068: exit.setActionCommand("EXIT");
069: exit.addActionListener(this );
070: file.add(save);
071: file.addSeparator();
072: file.add(exit);
073:
074: //
075: add(file);
076: }
077:
078: public void actionPerformed(ActionEvent e) {
079: String command = e.getActionCommand();
080:
081: if (command.equals("SAVE")) {
082: JFileChooser chooser = new JFileChooser(basePath);
083: chooser.setDialogTitle("Save current version");
084: chooser.setDialogType(JFileChooser.SAVE_DIALOG);
085:
086: int result = chooser.showSaveDialog(this );
087:
088: if (result == JFileChooser.APPROVE_OPTION) {
089: File fileToSave = chooser.getSelectedFile();
090:
091: if (fileToSave.exists()) {
092: int erase = JOptionPane
093: .showConfirmDialog(this ,
094: "The file already exists : Do you want to overwrite it ?");
095:
096: if (erase == JOptionPane.OK_OPTION) {
097: fileToSave.delete();
098:
099: try {
100: PrintWriter output = new PrintWriter(
101: new FileWriter(fileToSave));
102: output.print(player.getState());
103: output.flush();
104: output.close();
105: } catch (Exception exec) {
106: exec.printStackTrace();
107: JOptionPane.showMessageDialog(this ,
108: "Error while saving.");
109: }
110: }
111: } else {
112: try {
113: PrintWriter output = new PrintWriter(
114: new FileWriter(fileToSave));
115: output.print(player.getState());
116: output.flush();
117: output.close();
118: } catch (Exception exec) {
119: exec.printStackTrace();
120: JOptionPane.showMessageDialog(this ,
121: "Error while saving.");
122: }
123: }
124: }
125: } else if (command.equals("EXIT")) {
126: System.exit(0);
127: }
128: }
129: }
|