1 /* Copyright (c) 2002-2011 The University of the West Indies
2  *
3  * Contact: robert.lancashire@uwimona.edu.jm
4  *
5  *  This library is free software; you can redistribute it and/or
6  *  modify it under the terms of the GNU Lesser General Public
7  *  License as published by the Free Software Foundation; either
8  *  version 2.1 of the License, or (at your option) any later version.
9  *
10  *  This library is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  *  Lesser General Public License for more details.
14  *
15  *  You should have received a copy of the GNU Lesser General Public
16  *  License along with this library; if not, write to the Free Software
17  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18  */
19 
20 package jspecview.source;
21 
22 import javajs.util.Lst;
23 import jspecview.common.Spectrum;
24 
25 
26 
27 /**
28  * <code>JDXSource</code> is representation of all the data in the JCAMP-DX file
29  * or source. Note: All Jdx Source are viewed as having a set of Spectra
30  *
31  * @author Debbie-Ann Facey
32  * @author Khari A. Bryan
33  * @author Prof. Robert J. Lancashire
34  */
35 public class JDXSource extends JDXHeader {
36 
37   public final static int TYPE_VIEW = -2;
38 
39   public final static int TYPE_UNKNOWN = -1;
40   public final static int TYPE_SIMPLE = 0;
41   public final static int TYPE_BLOCK = 1;
42   public final static int TYPE_NTUPLE = 2;
43 
44   public int type = TYPE_SIMPLE;
45   public boolean isCompoundSource = false;
46 
47   private Lst<Spectrum> jdxSpectra;
48   private String errors = "";
49   private String filePath;
50 
51 
dispose()52   public void dispose() {
53     headerTable = null;
54     jdxSpectra = null;
55   }
56 
57   public int peakCount;
58 
59 	public boolean isView;
60 
61   private String inlineData;
62 
JDXSource(int type, String filePath)63   public JDXSource(int type, String filePath) {
64     this.type = type;
65     setFilePath(filePath);
66     headerTable = new Lst<String[]>();
67     jdxSpectra = new Lst<Spectrum>();
68     isCompoundSource = (type != TYPE_SIMPLE);
69   }
70 
71   /**
72    * Returns the Spectrum at a given index in the list
73    *
74    * @param index
75    *        the spectrum index
76    * @return the Spectrum at a given index in the list
77    */
getJDXSpectrum(int index)78   public Spectrum getJDXSpectrum(int index) {
79     return (jdxSpectra.size() <= index ? null : jdxSpectra.get(index));
80   }
81 
82   /**
83    * Adds a Spectrum to the list
84    * @param filePath
85    *
86    * @param spectrum
87    *        the spectrum to be added
88    * @param forceSub
89    */
addJDXSpectrum(String filePath, Spectrum spectrum, boolean forceSub)90   public void addJDXSpectrum(String filePath, Spectrum spectrum, boolean forceSub) {
91     if (filePath == null)
92       filePath = this.filePath;
93     spectrum.setFilePath(filePath);
94     if (inlineData != null)
95       spectrum.setInlineData(inlineData);
96     int n = jdxSpectra.size();
97     if (n == 0 || !jdxSpectra.get(n - 1).addSubSpectrum(spectrum, forceSub))
98       jdxSpectra.addLast(spectrum);
99   }
100 
101   /**
102    * Returns the number of Spectra in this Source
103    *
104    * @return the number of Spectra in this Source
105    */
getNumberOfSpectra()106   public int getNumberOfSpectra() {
107     return jdxSpectra.size();
108   }
109 
110   /**
111    * Returns the Vector of Spectra
112    *
113    * @return the Vector of Spectra
114    */
getSpectra()115   public Lst<Spectrum> getSpectra() {
116     return jdxSpectra;
117   }
118 
119   /**
120    * Used in Android
121    *
122    * @return array of JDXpectrum
123    */
getSpectraAsArray()124   public Spectrum[] getSpectraAsArray() {
125     return (Spectrum[]) (jdxSpectra == null ? null : jdxSpectra.toArray());
126   }
127 
128   /**
129    * Returns the error log for this source
130    *
131    * @return the error log for this source
132    */
getErrorLog()133   public String getErrorLog() {
134     return errors;
135   }
136 
137   /**
138    * Sets the error log for this source
139    *
140    * @param errors
141    *        error log for this source
142    */
setErrorLog(String errors)143   public void setErrorLog(String errors) {
144     this.errors = errors;
145   }
146 
setFilePath(String filePath)147   public void setFilePath(String filePath) {
148     this.filePath = filePath;
149   }
150 
getFilePath()151   public String getFilePath() {
152     return filePath;
153   }
154 
createView(Lst<Spectrum> specs)155   public static JDXSource createView(Lst<Spectrum> specs) {
156     JDXSource source = new JDXSource(TYPE_VIEW, "view");
157     source.isView = true;
158     for (int i = 0; i < specs.size(); i++)
159       source.addJDXSpectrum(specs.get(i).getFilePath(), specs.get(i), false);
160     return source;
161   }
162 
getHeaderRowDataAsArray(boolean addDataClass, String[][] rowData)163   public String[][] getHeaderRowDataAsArray(boolean addDataClass,
164                                             String[][] rowData) {
165     if (rowData == null)
166       rowData = new String[0][0];
167     String[][] data = getHeaderRowDataAsArray(addDataClass, rowData.length);
168     for (int i = rowData.length; --i >= 0; )
169       data[data.length - rowData.length + i] = rowData[i];
170     return data;
171   }
172 
setID(String id)173 	public void setID(String id) {
174 		jdxSpectra.get(0).sourceID = id;
175 	}
176 
matchesFilePath(String filePath)177 	public boolean matchesFilePath(String filePath) {
178 		return this.filePath.equals(filePath)
179 				|| this.filePath.replace('\\', '/').equals(filePath);
180 	}
181 
setInlineData(String data)182   public void setInlineData(String data) {
183     inlineData = data;
184     if (jdxSpectra != null)
185       for (int i = jdxSpectra.size(); --i >= 0;)
186         jdxSpectra.get(i).setInlineData(data);
187   }
188 
189 }
190