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.helper;
10
import java.io.FileInputStream;
11
import java.io.FileNotFoundException;
12
import java.io.FileOutputStream;
13
import java.io.IOException;
14
import java.io.InputStream;
15
import java.io.UnsupportedEncodingException;
17
import java.net.URLDecoder;
18
import java.util.ArrayList;
19
import java.util.List;
20
import java.util.Properties;
22
import org.herac.tuxguitar.gui.TuxGuitar;
23
import org.herac.tuxguitar.gui.system.config.TGConfigKeys;
24
import org.herac.tuxguitar.gui.util.TGFileUtils;
29
* TODO To change the template for this generated type comment go to
30
* Window - Preferences - Java - Code Style - Code Templates
32
public class FileHistory {
34
private static final int URL_LIMIT = TuxGuitar.instance().getConfig().getIntConfigValue(TGConfigKeys.MAX_HISTORY_FILES);
36
private boolean changed;
37
private boolean newFile;
38
private boolean localFile;
39
private boolean unsavedFile;
41
private String chooserPath;
44
this.urls = new ArrayList();
49
public void reset(URL url) {
50
this.unsavedFile = false;
51
this.newFile = (url == null);
52
this.localFile = (url != null && isLocalFile(url));
56
public boolean isNewFile(){
60
public boolean isLocalFile(){
61
return this.localFile;
64
public boolean isUnsavedFile() {
65
return this.unsavedFile;
68
public void setUnsavedFile() {
69
this.unsavedFile = true;
72
public void setChooserPath(String chooserPath){
73
this.chooserPath = chooserPath;
76
public void setChooserPath(URL url){
77
String path = getFilePath(url);
79
this.setChooserPath( path );
83
public String getCurrentFileName(String defaultName) {
84
if(!this.isNewFile()){
85
URL url = getCurrentURL();
87
return decode(new File(url.getFile()).getName());
93
public String getCurrentFilePath() {
94
if(!this.isNewFile()){
95
URL url = getCurrentURL();
97
String file = getFilePath(url);
103
return this.chooserPath;
106
public String getSavePath() {
107
String current = getCurrentFilePath();
108
return (current != null ? current : this.chooserPath);
111
public String getOpenPath() {
112
return this.chooserPath;
115
protected String getFilePath(URL url){
116
if(isLocalFile(url)){
117
return new File(url.getFile()).getParent();
122
protected String decode(String url){
124
return URLDecoder.decode(url, "UTF-8");
125
} catch (UnsupportedEncodingException e) {
131
protected boolean isLocalFile(URL url){
133
if(url.getProtocol().equals( new File(url.getFile()).toURI().toURL().getProtocol() ) ){
136
}catch(Throwable throwable){
137
throwable.printStackTrace();
142
protected URL getCurrentURL(){
143
if(!this.urls.isEmpty()){
144
return (URL)this.urls.get(0);
149
public void addURL(URL url){
152
this.urls.add(0,url);
159
public List getURLs(){
163
private void checkLimit(){
164
while(this.urls.size() > URL_LIMIT){
165
this.urls.remove(this.urls.size() - 1);
169
private void removeURL(URL url){
170
for(int i = 0; i < this.urls.size(); i++){
171
URL old = (URL)this.urls.get(i);
172
if(old.toString().equals(url.toString())){
179
public boolean isChanged() {
183
public void setChanged(boolean changed) {
184
this.changed = changed;
187
public void loadHistory() {
190
if(new File(getHistoryFileName()).exists()){
191
InputStream inputStream = new FileInputStream(getHistoryFileName());
192
Properties properties = new Properties();
193
properties.load(inputStream);
195
this.chooserPath = (String)properties.get("history.path");
197
int count = Integer.parseInt((String)properties.get("history.count"));
198
for(int i = 0; i < count;i ++){
199
String url = (String)properties.get("history." + i);
200
if(URL_LIMIT > i && url != null && url.length() > 0){
201
this.urls.add(new URL(url));
208
} catch (Exception e) {
213
public void saveHistory(){
215
Properties properties = new Properties();
217
int count = this.urls.size();
218
for(int i = 0;i < count;i ++){
219
properties.put("history." + i,this.urls.get(i).toString());
221
properties.put("history.count",Integer.toString(count));
222
if(this.chooserPath != null){
223
properties.put("history.path",this.chooserPath);
225
properties.store(new FileOutputStream(getHistoryFileName()),"History Files");
226
} catch (FileNotFoundException e1) {
227
e1.printStackTrace();
228
} catch (IOException e1) {
229
e1.printStackTrace();
233
private String getHistoryFileName(){
234
return TGFileUtils.PATH_USER_CONFIG + File.separator + "history.properties";