JAL-3026 srcjar files for VARNA and log4j
[jalview.git] / srcjar / fr / orsay / lri / varna / applications / fragseq / FragSeqFileModel.java
1 package fr.orsay.lri.varna.applications.fragseq;
2
3 import java.awt.datatransfer.DataFlavor;
4 import java.io.File;
5 import java.io.FileNotFoundException;
6 import java.util.ArrayList;
7 import java.util.Collection;
8 import java.util.Date;
9 import java.util.Random;
10 import java.util.regex.Pattern;
11
12 import javax.swing.tree.DefaultMutableTreeNode;
13
14 import fr.orsay.lri.varna.exceptions.ExceptionExportFailed;
15 import fr.orsay.lri.varna.exceptions.ExceptionFileFormatOrSyntax;
16 import fr.orsay.lri.varna.exceptions.ExceptionLoadingFailed;
17 import fr.orsay.lri.varna.exceptions.ExceptionPermissionDenied;
18 import fr.orsay.lri.varna.exceptions.ExceptionUnmatchedClosingParentheses;
19 import fr.orsay.lri.varna.factories.RNAFactory;
20 import fr.orsay.lri.varna.models.rna.RNA;
21
22 public class FragSeqFileModel implements Comparable<FragSeqFileModel> {
23         private ArrayList<FragSeqModel> _models = new ArrayList<FragSeqModel>();
24           protected Date _lastModified;
25           protected boolean _outOfSync = false;
26           protected String _caption = "";
27           protected String _path = "";
28           protected String _folder = "";
29           protected boolean _cached = false;
30
31           
32           public static Date lastModif(String path)
33           {
34                  return new Date(new File(path).lastModified()) ;
35           }
36           
37           public FragSeqFileModel(String folder, String path)
38           {
39                 this(folder,path,lastModif(path));
40           }
41           
42           
43           private static Random _rnd = new Random();
44           
45           public FragSeqFileModel(String folder, String path,Date lastModified)
46           {
47                   _lastModified = lastModified;
48                   _outOfSync = false;
49                   _folder =folder;
50                   _path = path;
51                   String[] s = path.split(Pattern.quote(File.separator));
52                   if (s.length>0)
53                     _caption = s[s.length-1];
54           }
55           
56           public void load()
57           {
58                   ArrayList<RNA> rnas = null;
59                   try {
60                           rnas = createRNAs();
61                         for (RNA r: rnas)
62                         {  
63                                 this.addModel(new FragSeqRNASecStrModel(r));
64                                   int nb =_rnd.nextInt(5); 
65                                   for(int i=0;i<nb;i++)
66                                   {
67                                           FragSeqAnnotationDataModel data = new FragSeqAnnotationDataModel(r.getID(),""+i+"-"+r.getID());
68                                           FragSeqAnnotationDataModel.addRandomAnnotations(r,data);
69                                           addModel(data);
70                                   }               
71                         }
72                 } catch (ExceptionUnmatchedClosingParentheses e) {
73                         // TODO Auto-generated catch block
74                         e.printStackTrace();
75                 } catch (FileNotFoundException e) {
76                         // TODO Auto-generated catch block
77                         e.printStackTrace();
78                 } catch (ExceptionFileFormatOrSyntax e) {
79                         // TODO Auto-generated catch block
80                         e.printStackTrace();
81                 } catch (ExceptionExportFailed e) {
82                         // TODO Auto-generated catch block
83                         e.printStackTrace();
84                 } catch (ExceptionPermissionDenied e) {
85                         // TODO Auto-generated catch block
86                         e.printStackTrace();
87                 } catch (ExceptionLoadingFailed e) {
88                         // TODO Auto-generated catch block
89                         e.printStackTrace();
90                 }
91                   _cached = true;
92           }
93           
94           public boolean hasChanged()
95           {
96                   return _outOfSync;
97           }
98           
99           public boolean checkForModifications()
100           {
101                 if (!lastModif(_path).equals(_lastModified) && !_outOfSync)
102                 {
103                         _outOfSync = true;
104                           return true;
105                 }
106                 return false;
107           }
108           
109           
110           public String toString()
111           {
112                   return _caption + (this._outOfSync?"*":"");
113           }
114           
115           
116           public String getCaption()
117           {
118                   return _caption;
119           }
120
121           public String getFolder()
122           {
123                   return _folder;
124           }
125
126           public String getPath()
127           {
128                   return _path;
129           }
130
131
132         public int compareTo(FragSeqFileModel o) {
133                 return _caption.compareTo(o._caption);
134         }
135           
136         public ArrayList<FragSeqModel> getModels()
137         {
138                 if (!_cached)
139                 {  load();  }
140                 return _models;
141         }
142         public void addModel(FragSeqModel f)
143         {
144                 _models.add(f);
145         }
146
147         
148         private ArrayList<RNA> createRNAs() throws ExceptionUnmatchedClosingParentheses, ExceptionFileFormatOrSyntax, FileNotFoundException, ExceptionExportFailed, ExceptionPermissionDenied, ExceptionLoadingFailed
149           {
150                   Collection<RNA> r = RNAFactory.loadSecStr(_path);
151                   for (RNA r2 : r)
152                   {
153                           r2.drawRNARadiate();
154                   }
155                   return new ArrayList<RNA>(r);
156           }
157           
158 }