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

« back to all changes in this revision

Viewing changes to src/org/herac/tuxguitar/gui/helper/FileHistory.java

  • Committer: Bazaar Package Importer
  • Author(s): Philippe Coval
  • Date: 2008-06-19 00:30:30 UTC
  • mto: (5.1.2 sid)
  • mto: This revision was merged to the branch mainline in revision 3.
  • Revision ID: james.westby@ubuntu.com-20080619003030-h719szrhsngou7c6
Tags: upstream-1.0
ImportĀ upstreamĀ versionĀ 1.0

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.helper;
8
 
 
9
 
import java.io.File;
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.util.ArrayList;
16
 
import java.util.List;
17
 
import java.util.Properties;
18
 
 
19
 
import org.herac.tuxguitar.gui.TuxGuitar;
20
 
import org.herac.tuxguitar.gui.system.config.ConfigKeys;
21
 
import org.herac.tuxguitar.gui.util.TuxGuitarFileUtils;
22
 
 
23
 
/**
24
 
 * @author julian
25
 
 *
26
 
 * TODO To change the template for this generated type comment go to
27
 
 * Window - Preferences - Java - Code Style - Code Templates
28
 
 */
29
 
public class FileHistory {
30
 
        private static final int PATH_LIMIT = TuxGuitar.instance().getConfig().getIntConfigValue(ConfigKeys.MAX_HISTORY_FILES);
31
 
        private boolean pathChanges;
32
 
        private boolean isNewFile;      
33
 
        private List paths;
34
 
 
35
 
    public FileHistory(){
36
 
        this.clearFile();
37
 
        this.loadHistory();
38
 
    }
39
 
        
40
 
        public void clearFile() {
41
 
                this.isNewFile = true;
42
 
        }           
43
 
 
44
 
        public String getFileName() {
45
 
                if(!this.isNewFile() && !this.paths.isEmpty()){
46
 
                        return new File((String)paths.get(0)).getName();
47
 
                }
48
 
                return "Untitled.gp4";
49
 
        }
50
 
 
51
 
        public String getFilePath() {
52
 
                if(!this.paths.isEmpty()){
53
 
                        return new File((String)paths.get(0)).getParent();
54
 
                }
55
 
                return null;
56
 
        }
57
 
        
58
 
        public boolean isNewFile(){
59
 
                return isNewFile;
60
 
        }
61
 
        
62
 
        public void setFile(String fileName) {
63
 
                this.isNewFile = false;
64
 
                this.addPath(fileName);
65
 
        }       
66
 
 
67
 
    public void addPath(String path){
68
 
        if(path != null && path.length() > 0){
69
 
                removePath(path);
70
 
                paths.add(0,path);              
71
 
                checkLimit();
72
 
                saveHistory();
73
 
                setPathChanges(true);
74
 
        }
75
 
    }
76
 
    
77
 
    public List getPaths(){
78
 
        return this.paths;
79
 
    }
80
 
 
81
 
    private void checkLimit(){
82
 
        while(paths.size() > PATH_LIMIT){
83
 
                paths.remove(paths.size() - 1); 
84
 
        }
85
 
    }
86
 
 
87
 
    private void removePath(String path){               
88
 
        for(int i = 0; i < paths.size(); i++){
89
 
                String old = (String)paths.get(i);
90
 
                if(old.equals(path)){
91
 
                        paths.remove(i);
92
 
                        break;
93
 
                }
94
 
        }
95
 
    }
96
 
    
97
 
    public boolean hasPathChanges() {
98
 
                return pathChanges;
99
 
        }
100
 
 
101
 
        public void setPathChanges(boolean pathChanges) {
102
 
                this.pathChanges = pathChanges;
103
 
        }
104
 
 
105
 
        public void loadHistory() {       
106
 
        try {              
107
 
                this.paths = new ArrayList();
108
 
                if(new File(getHistoryFileName()).exists()){            
109
 
                        InputStream inputStream = new FileInputStream(getHistoryFileName());                        
110
 
                        Properties properties = new Properties();
111
 
                        properties.load(inputStream);
112
 
                        
113
 
                        int count = Integer.parseInt((String)properties.get("history.count"));
114
 
                        for(int i = 0; i < count;i ++){
115
 
                                String path = (String)properties.get("history." + i);
116
 
                                if(PATH_LIMIT > i && path != null && path.length() > 0){
117
 
                                        paths.add((String)properties.get("history." + i));
118
 
                                }
119
 
                        }
120
 
                        setPathChanges(true);
121
 
                }else{
122
 
                        this.saveHistory();
123
 
                }
124
 
        } catch (Exception e) {
125
 
            e.printStackTrace();
126
 
        }
127
 
    }
128
 
    
129
 
    public void saveHistory(){
130
 
        try {
131
 
                Properties properties = new Properties();
132
 
                
133
 
                int count = paths.size();
134
 
                for(int i = 0;i < count;i ++){
135
 
                        properties.put("history." + i,paths.get(i));    
136
 
                }
137
 
                properties.put("history.count",Integer.toString(count));
138
 
 
139
 
                properties.store(new FileOutputStream(getHistoryFileName()),"History Files");
140
 
        } catch (FileNotFoundException e1) {
141
 
            e1.printStackTrace();
142
 
        } catch (IOException e1) {
143
 
            e1.printStackTrace();
144
 
        }
145
 
    }
146
 
    
147
 
    private String getHistoryFileName(){
148
 
        return TuxGuitarFileUtils.USER_CONFIG_PREFIX + File.separator + "history.properties";
149
 
    }
150
 
}