edfd56f838ffdbfd4d85b4fd33096c925356063a
[jalview.git] / srcjar / fr / orsay / lri / varna / applications / newGUI / VARNAGUIModel.java
1 package fr.orsay.lri.varna.applications.newGUI;
2
3 import java.awt.datatransfer.DataFlavor;
4 import java.awt.datatransfer.Transferable;
5 import java.awt.datatransfer.UnsupportedFlavorException;
6 import java.io.File;
7 import java.io.FileFilter;
8 import java.io.FileNotFoundException;
9 import java.io.FilenameFilter;
10 import java.io.IOException;
11 import java.util.Collection;
12 import java.util.Date;
13 import java.util.regex.Pattern;
14
15 import fr.orsay.lri.varna.exceptions.ExceptionExportFailed;
16 import fr.orsay.lri.varna.exceptions.ExceptionFileFormatOrSyntax;
17 import fr.orsay.lri.varna.exceptions.ExceptionLoadingFailed;
18 import fr.orsay.lri.varna.exceptions.ExceptionPermissionDenied;
19 import fr.orsay.lri.varna.exceptions.ExceptionUnmatchedClosingParentheses;
20 import fr.orsay.lri.varna.factories.RNAFactory;
21 import fr.orsay.lri.varna.models.rna.RNA;
22
23 public class VARNAGUIModel implements Comparable<VARNAGUIModel> {
24   private Date _lastModified;
25   private boolean _outOfSync = false;
26   private RNA _r = null;
27   private String _caption = "";
28   private String _path = "";
29   private String _folder = "";
30
31   
32   public static Date lastModif(String path)
33   {
34          return new Date(new File(path).lastModified()) ;
35   }
36   
37   public VARNAGUIModel(String folder, String path)
38   {
39         this(folder,path,lastModif(path));
40   }
41   
42   public VARNAGUIModel(String folder, String path,Date lastModified)
43   {
44           _lastModified = lastModified;
45           _outOfSync = false;
46           _folder =folder;
47           _path = path;
48           String[] s = path.split(Pattern.quote(File.separator));
49           if (s.length>0)
50             _caption = s[s.length-1];
51   }
52   
53   public boolean hasChanged()
54   {
55           return _outOfSync;
56   }
57   
58   public boolean checkForModifications()
59   {
60         if (!lastModif(_path).equals(_lastModified) && !_outOfSync)
61         {
62                 _outOfSync = true;
63                   return true;
64         }
65         return false;
66   }
67   
68   
69   public RNA getRNA()
70   {
71           if (_r ==null)
72           {
73                   try {
74                         createRNA();
75                 } catch (ExceptionUnmatchedClosingParentheses 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 (FileNotFoundException e) {
82                         // TODO Auto-generated catch block
83                         e.printStackTrace();
84                 } catch (ExceptionExportFailed e) {
85                         // TODO Auto-generated catch block
86                         e.printStackTrace();
87                 } catch (ExceptionPermissionDenied e) {
88                         // TODO Auto-generated catch block
89                         e.printStackTrace();
90                 } catch (ExceptionLoadingFailed e) {
91                         // TODO Auto-generated catch block
92                         e.printStackTrace();
93                 }
94           }
95           return _r;
96   }
97   
98   private RNA createRNA() throws ExceptionUnmatchedClosingParentheses, ExceptionFileFormatOrSyntax, FileNotFoundException, ExceptionExportFailed, ExceptionPermissionDenied, ExceptionLoadingFailed
99   {
100           Collection<RNA> r = RNAFactory.loadSecStr(_path);
101           if (r.size()>0)
102           {
103                   _r = r.iterator().next();
104                   _r.drawRNARadiate();
105           }
106           else
107           {
108                   throw new ExceptionFileFormatOrSyntax("No valid RNA defined in this file.");
109           }
110           return _r;
111   }
112   
113   public String toString()
114   {
115           return _caption + (this._outOfSync?"*":"");
116   }
117   
118   public String getID()
119   {
120           return getRNA().getID();
121   }
122   
123   public String getCaption()
124   {
125           return _caption;
126   }
127
128   public String getFolder()
129   {
130           return _folder;
131   }
132
133   public static DataFlavor Flavor = new DataFlavor(VARNAGUIModel.class, "VARNA Object");
134
135
136 public int compareTo(VARNAGUIModel o) {
137         return _caption.compareTo(o._caption);
138 }
139   
140
141   
142 }