/*
* Created on 2004/09/27
* Author aki@www.xucker.jpn.org
* License Apache2.0 or Common Public License
*/
package org.jpn.xucker.commons.util;
import java.io.File;
/**
*
*
*/
public abstract class AbstractBasicFileApplication {
private boolean needSave;
private File file;
private String fileExtension;
public String getFileExtension() {
return fileExtension;
}
public void setFileExtension(String defaultExtension) {
this.fileExtension = defaultExtension;
}
public String fixFileExtension(String path){
if(getFileExtension()==null){
return path;
}else{
if(path.toLowerCase().endsWith("."+getFileExtension().toLowerCase())){
return path;
}else{
return path+"."+getFileExtension();
}
}
}
public String getDefaultFileName() {
return defaultFileName;
}
public void setDefaultFileName(String defaultFileName) {
this.defaultFileName = defaultFileName;
}
public File getFile() {
return file;
}
public void setFile(File file) {
this.file = file;
}
public String getFileSaveTitle() {
return fileSaveTitle;
}
public void setFileSaveTitle(String fileSaveTitle) {
this.fileSaveTitle = fileSaveTitle;
}
public String getHeadFileSaveMessage() {
return headFileSaveMessage;
}
public void setHeadFileSaveMessage(String headFileSaveMessage) {
this.headFileSaveMessage = headFileSaveMessage;
}
public String getTailFileSaveMessage() {
return tailFileSaveMessage;
}
public void setTailFileSaveMessage(String tailFileSaveMessage) {
this.tailFileSaveMessage = tailFileSaveMessage;
}
protected String defaultFileName="無題";
protected String headFileSaveMessage="ファイル ";
protected String tailFileSaveMessage=" は保存されていません。保存しますか?";
protected String fileSaveTitle="ファイル保存の確認";
public static final int YES=0;
public static final int NO=1;
public static final int CANCEL=2;
public boolean isNeedSave(){
return needSave;
}
public void setNeedSave(boolean bool){
this.needSave=bool;
}
public void openFile(){
if(isNeedSave()){
int result=askSave();
if(result==YES){
if(!saveFile()){
return; //cancel
}
}else if(result==CANCEL){
return;
}
}
file=execChooseOpenFile();
if(file!=null){
execOpen();
needSave=false;
}
updateTitle();
}
/**
*
*/
protected abstract void execOpen();
protected abstract File execChooseOpenFile();
/**
*
*/
protected abstract void execSave();
protected abstract void updateTitle();
protected abstract File execChooseSaveFile();
public String getFileName(){
if(file==null){
return defaultFileName;
}else{
return file.getName();
}
}
public int askSave(){
return executeAskSave(fileSaveTitle,headFileSaveMessage+getFileName()+tailFileSaveMessage);
}
protected abstract int executeAskSave(String title,String message);
public boolean saveFile(){
if(file==null){
file=execChooseSaveFile();
}
if(file!=null){
execSave();
needSave=false;
updateTitle();
return true;
}else{
return false;
}
}
public boolean saveAsFile(){
File tmpFile=execChooseSaveFile();
if(tmpFile!=null){
file=tmpFile;
execSave();
needSave=false;
updateTitle();
return true;
}else{
return false;
}
}
public void newFile(){
if(isNeedSave()){
int result=askSave();
if(result==YES){
if(!saveFile()){
return; //cancel
}
}else if(result==CANCEL){
return;
}
}
execNew();
file=null;
needSave=false;
updateTitle();
}
/**
*
*/
protected abstract void execNew();
/*
public void exit(){
if(isNeedSave()){
int result=askSave();
if(result==YES){
if(!saveFile()){
return; //cancel
}
}else if(result==CANCEL){
return;
}
}
execExit();
}*/
/**
*
*/
protected abstract void execExit();
}
|