~ubuntu-branches/ubuntu/oneiric/tuxguitar/oneiric

« back to all changes in this revision

Viewing changes to TuxGuitar/src/org/herac/tuxguitar/gui/util/FileChooser.java

  • Committer: Bazaar Package Importer
  • Author(s): Philippe Coval
  • Date: 2008-06-19 00:30:30 UTC
  • mfrom: (1.1.1 upstream) (2.1.3 hardy)
  • Revision ID: james.westby@ubuntu.com-20080619003030-agens2gvd5m4dacu
New upstream release (Closes: #481728) also (LP: #176979, #212207)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Created on 08-dic-2005
 
3
 *
 
4
 * TODO To change the template for this generated file go to
 
5
 * Window - Preferences - Java - Code Style - Code Templates
 
6
 */
 
7
package org.herac.tuxguitar.gui.util;
 
8
 
 
9
import java.io.File;
 
10
import java.util.ArrayList;
 
11
import java.util.Iterator;
 
12
import java.util.List;
 
13
 
 
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;
 
20
 
 
21
/**
 
22
 * @author julian
 
23
 * 
 
24
 * TODO To change the template for this generated type comment go to Window - Preferences - Java - Code Style - Code Templates
 
25
 */
 
26
public class FileChooser {
 
27
        
 
28
        public static final String DEFAULT_OPEN_FILENAME = null;
 
29
        
 
30
        public static final String DEFAULT_SAVE_FILENAME = ("Untitled" + TGFileFormatManager.DEFAULT_EXTENSION);
 
31
        
 
32
        public static TGFileFormat ALL_FORMATS = new TGFileFormat("All Files","*.*");
 
33
        
 
34
        private static FileChooser instance;
 
35
        
 
36
        public static FileChooser instance() {
 
37
                if(instance == null){
 
38
                        instance = new FileChooser();
 
39
                }
 
40
                return instance;
 
41
        }
 
42
        
 
43
        private List list(Object o){
 
44
                List list = new ArrayList();
 
45
                list.add(o);
 
46
                return list;
 
47
        }
 
48
        
 
49
        public String open(Shell parent,TGFileFormat format) {
 
50
                return open(parent, list(format));
 
51
        }
 
52
        
 
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));
 
58
                
 
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);
 
66
        }
 
67
        
 
68
        public String save(Shell parent,TGFileFormat format) {
 
69
                return save(parent, list(format));
 
70
        }
 
71
        
 
72
        public String save(Shell parent,List formats) {
 
73
                String chooserPath = TuxGuitar.instance().getFileHistory().getSavePath();
 
74
                
 
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);
 
82
        }
 
83
        
 
84
        private String openDialog(FileDialog dialog){
 
85
                String file = dialog.open();
 
86
                if(file != null){
 
87
                        TuxGuitar.instance().getFileHistory().setChooserPath( new File(file).getParent() );
 
88
                }
 
89
                return file;
 
90
        }
 
91
        
 
92
        private String getFileName(List formats, String defaultName, boolean replaceExtension){
 
93
                if(formats == null || formats.isEmpty()){
 
94
                        return defaultName;
 
95
                }
 
96
                String file = TuxGuitar.instance().getFileHistory().getCurrentFileName(defaultName);
 
97
                if(file != null && file.length() > 0){
 
98
                        int index = file.lastIndexOf('.');
 
99
                        if(index > 0){
 
100
                                String fileName = file.substring(0,index);
 
101
                                String fileExtension = file.substring(index).toLowerCase();
 
102
                                Iterator it = formats.iterator();
 
103
                                while(it.hasNext()){
 
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)){
 
110
                                                                        return file;
 
111
                                                                }
 
112
                                                        }
 
113
                                                }
 
114
                                        }
 
115
                                }
 
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));
 
123
                                                        }
 
124
                                                }
 
125
                                        }
 
126
                                }
 
127
                        }
 
128
                }
 
129
                return defaultName;
 
130
        }
 
131
        
 
132
        private class FilterList{
 
133
                private String[] filterExtensions;
 
134
                private String[] filterNames;
 
135
                
 
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();
 
148
                        }
 
149
                        this.filterNames[(size - 1)] = new String("All Files");
 
150
                        this.filterExtensions[(size - 1)] = new String("*.*");
 
151
                }
 
152
                
 
153
                public String[] getFilterExtensions() {
 
154
                        return this.filterExtensions;
 
155
                }
 
156
                
 
157
                public String[] getFilterNames() {
 
158
                        return this.filterNames;
 
159
                }
 
160
                
 
161
        }
 
162
}
 
 
b'\\ No newline at end of file'