2
* Created on 08-dic-2005
4
* TODO To change the template for this generated file go to
5
* Window - Preferences - Java - Code Style - Code Templates
7
package org.herac.tuxguitar.gui.util;
10
import java.util.ArrayList;
11
import java.util.Iterator;
12
import java.util.List;
14
import org.eclipse.swt.SWT;
15
import org.eclipse.swt.widgets.FileDialog;
16
import org.eclipse.swt.widgets.Shell;
17
import org.herac.tuxguitar.gui.TuxGuitar;
18
import org.herac.tuxguitar.io.base.TGFileFormat;
19
import org.herac.tuxguitar.io.base.TGFileFormatManager;
24
* TODO To change the template for this generated type comment go to Window - Preferences - Java - Code Style - Code Templates
26
public class FileChooser {
28
public static final String DEFAULT_OPEN_FILENAME = null;
30
public static final String DEFAULT_SAVE_FILENAME = ("Untitled" + TGFileFormatManager.DEFAULT_EXTENSION);
32
public static TGFileFormat ALL_FORMATS = new TGFileFormat("All Files","*.*");
34
private static FileChooser instance;
36
public static FileChooser instance() {
38
instance = new FileChooser();
43
private List list(Object o){
44
List list = new ArrayList();
49
public String open(Shell parent,TGFileFormat format) {
50
return open(parent, list(format));
53
public String open(Shell parent,List formats) {
54
String currentPath = TuxGuitar.instance().getFileHistory().getCurrentFilePath();
55
String chooserPath = TuxGuitar.instance().getFileHistory().getOpenPath();
56
boolean localFile = TuxGuitar.instance().getFileHistory().isLocalFile();
57
boolean existentFile = (localFile && currentPath != null && chooserPath != null && currentPath.equals(chooserPath));
59
FilterList filter = new FilterList(formats);
60
FileDialog dialog = new FileDialog(parent,SWT.OPEN);
61
dialog.setFileName((existentFile ? getFileName(formats, DEFAULT_OPEN_FILENAME, false) : null ));
62
dialog.setFilterPath(chooserPath);
63
dialog.setFilterNames(filter.getFilterNames());
64
dialog.setFilterExtensions(filter.getFilterExtensions());
65
return openDialog(dialog);
68
public String save(Shell parent,TGFileFormat format) {
69
return save(parent, list(format));
72
public String save(Shell parent,List formats) {
73
String chooserPath = TuxGuitar.instance().getFileHistory().getSavePath();
75
FilterList filter = new FilterList(formats);
76
FileDialog dialog = new FileDialog(parent,SWT.SAVE);
77
dialog.setFileName(getFileName(formats, DEFAULT_SAVE_FILENAME, true));
78
dialog.setFilterPath(chooserPath);
79
dialog.setFilterNames(filter.getFilterNames());
80
dialog.setFilterExtensions(filter.getFilterExtensions());
81
return openDialog(dialog);
84
private String openDialog(FileDialog dialog){
85
String file = dialog.open();
87
TuxGuitar.instance().getFileHistory().setChooserPath( new File(file).getParent() );
92
private String getFileName(List formats, String defaultName, boolean replaceExtension){
93
if(formats == null || formats.isEmpty()){
96
String file = TuxGuitar.instance().getFileHistory().getCurrentFileName(defaultName);
97
if(file != null && file.length() > 0){
98
int index = file.lastIndexOf('.');
100
String fileName = file.substring(0,index);
101
String fileExtension = file.substring(index).toLowerCase();
102
Iterator it = formats.iterator();
104
TGFileFormat format = (TGFileFormat)it.next();
105
if(format.getSupportedFormats() != null){
106
String[] extensions = format.getSupportedFormats().split(TGFileFormat.EXTENSION_SEPARATOR);
107
if(extensions != null && extensions.length > 0){
108
for(int i = 0; i < extensions.length; i ++){
109
if(extensions[i].equals("*" + fileExtension)){
116
if( replaceExtension ){
117
TGFileFormat format = (TGFileFormat)formats.get(0);
118
if(format.getSupportedFormats() != null){
119
String[] extensions = format.getSupportedFormats().split(TGFileFormat.EXTENSION_SEPARATOR);
120
if(extensions != null && extensions.length > 0){
121
if(extensions[0].length() > 1){
122
return (fileName + extensions[0].substring(1));
132
private class FilterList{
133
private String[] filterExtensions;
134
private String[] filterNames;
136
public FilterList(List formats) {
137
int size = (formats.size() + 2);
138
this.filterNames = new String[size];
139
this.filterExtensions = new String[size];
140
this.filterNames[0] = new String("All Suported Formats");
141
this.filterExtensions[0] = new String();
142
for(int i = 1; i < (size - 1); i ++){
143
TGFileFormat format = (TGFileFormat)formats.get(i-1);
144
this.filterNames[i] = format.getName();
145
this.filterExtensions[i] = format.getSupportedFormats();
146
this.filterExtensions[0] += (i > 1)?";":"";
147
this.filterExtensions[0] += format.getSupportedFormats();
149
this.filterNames[(size - 1)] = new String("All Files");
150
this.filterExtensions[(size - 1)] = new String("*.*");
153
public String[] getFilterExtensions() {
154
return this.filterExtensions;
157
public String[] getFilterNames() {
158
return this.filterNames;
b'\\ No newline at end of file'