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.io.UnsupportedEncodingException; 16 import java.net.URL; 17 import java.net.URLDecoder; 18 import java.util.ArrayList; 19 import java.util.List; 20 import java.util.Properties; 21 22 import org.herac.tuxguitar.gui.TuxGuitar; 23 import org.herac.tuxguitar.gui.system.config.TGConfigKeys; 24 import org.herac.tuxguitar.gui.util.TGFileUtils; 25 26 /** 27 * @author julian 28 * 29 * TODO To change the template for this generated type comment go to 30 * Window - Preferences - Java - Code Style - Code Templates 31 */ 32 public class FileHistory { 33 34 private static final int URL_LIMIT = TuxGuitar.instance().getConfig().getIntConfigValue(TGConfigKeys.MAX_HISTORY_FILES); 35 36 private boolean changed; 37 private boolean newFile; 38 private boolean localFile; 39 private boolean unsavedFile; 40 private List urls; 41 private String chooserPath; 42 FileHistory()43 public FileHistory(){ 44 this.urls = new ArrayList(); 45 this.loadHistory(); 46 this.reset(null); 47 } 48 reset(URL url)49 public void reset(URL url) { 50 this.unsavedFile = false; 51 this.newFile = (url == null); 52 this.localFile = (url != null && isLocalFile(url)); 53 this.addURL(url); 54 } 55 isNewFile()56 public boolean isNewFile(){ 57 return this.newFile; 58 } 59 isLocalFile()60 public boolean isLocalFile(){ 61 return this.localFile; 62 } 63 isUnsavedFile()64 public boolean isUnsavedFile() { 65 return this.unsavedFile; 66 } 67 setUnsavedFile()68 public void setUnsavedFile() { 69 this.unsavedFile = true; 70 } 71 setChooserPath(String chooserPath)72 public void setChooserPath(String chooserPath){ 73 this.chooserPath = chooserPath; 74 } 75 setChooserPath(URL url)76 public void setChooserPath(URL url){ 77 String path = getFilePath(url); 78 if( path != null ){ 79 this.setChooserPath( path ); 80 } 81 } 82 getCurrentFileName(String defaultName)83 public String getCurrentFileName(String defaultName) { 84 if(!this.isNewFile()){ 85 URL url = getCurrentURL(); 86 if(url != null){ 87 return decode(new File(url.getFile()).getName()); 88 } 89 } 90 return defaultName; 91 } 92 getCurrentFilePath()93 public String getCurrentFilePath() { 94 if(!this.isNewFile()){ 95 URL url = getCurrentURL(); 96 if(url != null){ 97 String file = getFilePath(url); 98 if(file != null){ 99 return decode(file); 100 } 101 } 102 } 103 return this.chooserPath; 104 } 105 getSavePath()106 public String getSavePath() { 107 String current = getCurrentFilePath(); 108 return (current != null ? current : this.chooserPath); 109 } 110 getOpenPath()111 public String getOpenPath() { 112 return this.chooserPath; 113 } 114 getFilePath(URL url)115 protected String getFilePath(URL url){ 116 if(isLocalFile(url)){ 117 return new File(url.getFile()).getParent(); 118 } 119 return null; 120 } 121 decode(String url)122 protected String decode(String url){ 123 try { 124 return URLDecoder.decode(url, "UTF-8"); 125 } catch (UnsupportedEncodingException e) { 126 e.printStackTrace(); 127 } 128 return url; 129 } 130 isLocalFile(URL url)131 protected boolean isLocalFile(URL url){ 132 try { 133 if(url.getProtocol().equals( new File(url.getFile()).toURI().toURL().getProtocol() ) ){ 134 return true; 135 } 136 }catch(Throwable throwable){ 137 throwable.printStackTrace(); 138 } 139 return false; 140 } 141 getCurrentURL()142 protected URL getCurrentURL(){ 143 if(!this.urls.isEmpty()){ 144 return (URL)this.urls.get(0); 145 } 146 return null; 147 } 148 addURL(URL url)149 public void addURL(URL url){ 150 if(url != null){ 151 removeURL(url); 152 this.urls.add(0,url); 153 checkLimit(); 154 setChanged(true); 155 } 156 saveHistory(); 157 } 158 getURLs()159 public List getURLs(){ 160 return this.urls; 161 } 162 checkLimit()163 private void checkLimit(){ 164 while(this.urls.size() > URL_LIMIT){ 165 this.urls.remove(this.urls.size() - 1); 166 } 167 } 168 removeURL(URL url)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())){ 173 this.urls.remove(i); 174 break; 175 } 176 } 177 } 178 isChanged()179 public boolean isChanged() { 180 return this.changed; 181 } 182 setChanged(boolean changed)183 public void setChanged(boolean changed) { 184 this.changed = changed; 185 } 186 loadHistory()187 public void loadHistory() { 188 try { 189 this.urls.clear(); 190 if(new File(getHistoryFileName()).exists()){ 191 InputStream inputStream = new FileInputStream(getHistoryFileName()); 192 Properties properties = new Properties(); 193 properties.load(inputStream); 194 195 this.chooserPath = (String)properties.get("history.path"); 196 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)); 202 } 203 } 204 setChanged(true); 205 }else{ 206 this.saveHistory(); 207 } 208 } catch (Exception e) { 209 e.printStackTrace(); 210 } 211 } 212 saveHistory()213 public void saveHistory(){ 214 try { 215 Properties properties = new Properties(); 216 217 int count = this.urls.size(); 218 for(int i = 0;i < count;i ++){ 219 properties.put("history." + i,this.urls.get(i).toString()); 220 } 221 properties.put("history.count",Integer.toString(count)); 222 if(this.chooserPath != null){ 223 properties.put("history.path",this.chooserPath); 224 } 225 properties.store(new FileOutputStream(getHistoryFileName()),"History Files"); 226 } catch (FileNotFoundException e1) { 227 e1.printStackTrace(); 228 } catch (IOException e1) { 229 e1.printStackTrace(); 230 } 231 } 232 getHistoryFileName()233 private String getHistoryFileName(){ 234 return TGFileUtils.PATH_USER_CONFIG + File.separator + "history.properties"; 235 } 236 } 237