package views;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import misc.CustomFileFilter;
import model.Model;
import application.Application;
public class FileManager implements PropertyChangeListener {
public String defaultPath = null;
private Model model = null;
private String fileType = null;
public FileManager(Model model,String defaultPath) {
this.model = model;
model.addPropertyChangeListener(this);
this.defaultPath = defaultPath;
}
/**
* This methods get the file paths for saving and opening files
*/
private String getOpenFilePath() {
JFileChooser fileChooser = new JFileChooser(this.defaultPath);
CustomFileFilter filter = new CustomFileFilter();
String tempFilePath = null;
filter.setDescription("*."+fileType);
filter.addExtension(fileType);
fileChooser.setFileFilter(filter);
if(fileChooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
tempFilePath = fileChooser.getSelectedFile().getAbsolutePath();
//Save the path as the default one
this.defaultPath = tempFilePath.substring(0, tempFilePath.lastIndexOf("\\"));
}
return tempFilePath;
}
private String getSaveFilePath() {
JFileChooser fileChooser = new JFileChooser(this.defaultPath);
CustomFileFilter filter = new CustomFileFilter();
String tempFilePath = null;
filter.setDescription("*."+fileType);
filter.addExtension(fileType);
fileChooser.setFileFilter(filter);
if(fileChooser.showSaveDialog(null) == JFileChooser.APPROVE_OPTION) {
tempFilePath = fileChooser.getSelectedFile().getAbsolutePath();
//Save the path as the default one
this.defaultPath = tempFilePath.substring(0, tempFilePath.lastIndexOf("\\"));
}
return tempFilePath;
}
/**
* askToSave will ask the user whehther he wants to save the current opened file before continuing
*/
public boolean askToSave() {
boolean cancel = false;
String errorMessage = Application.messages.getString("WARNING_UNSAVED_"+fileType.toUpperCase());
String errorTitle = Application.messages.getString("WARNING_UNSAVED_"+fileType.toUpperCase()+"_TITLE");
switch(JOptionPane.showConfirmDialog(null, errorMessage, errorTitle, JOptionPane.INFORMATION_MESSAGE)) {
case JOptionPane.YES_OPTION:
model.saveFile(getSaveFilePath(), fileType);
break;
case JOptionPane.CANCEL_OPTION:
cancel = true;
default:
break;
}
return cancel;
}
public void propertyChange(PropertyChangeEvent arg0) {
// if a file has been modified whithout being saved
if (arg0.getPropertyName() == "fileConflict") {
this.fileType = (String)arg0.getNewValue();
if (!this.askToSave())
model.openFile(getOpenFilePath(), this.fileType);
}
// if this is an open order
if (arg0.getPropertyName() == "openFile") {
this.fileType = (String)arg0.getNewValue();
model.openFile(getOpenFilePath(), this.fileType);
}
// if this is a save order
if (arg0.getPropertyName() == "saveFile") {
this.fileType = (String)arg0.getNewValue();
model.saveFile(getSaveFilePath(), this.fileType);
}
}
public void setFileType(String fileType) {
this.fileType = fileType;
}
public String getDefaultPath() {
return defaultPath;
}
public void setDefaultPath(String defaultPath) {
this.defaultPath = defaultPath;
}
}