4 * Created on August 28, 2004, 7:09 PM
7 package org
.tsho
.dmc2
.ui
;
9 import java
.util
.Hashtable
;
10 import java
.util
.HashSet
;
11 import java
.util
.Vector
;
13 import java
.io
.BufferedReader
;
14 import java
.io
.BufferedWriter
;
15 import java
.io
.FileReader
;
16 import java
.io
.FileWriter
;
17 import java
.io
.IOException
;
18 import javax
.swing
.JFileChooser
;
19 import javax
.swing
.JOptionPane
;
20 import javax
.swing
.JPanel
;
22 import org
.tsho
.dmc2
.core
.util
.SampleParser
;
25 * The methods of this class may corrupt a binary file
26 * if it is passed as an argument. Care should be taken to pass
27 * only text files as an argument (for example,verifying the
31 public class SamplesSupplier
{
33 private File modelFile
;
34 private Vector buffer
;//buffer which holds the "sample" lines read from the file
35 private Vector shortBuffer
;
37 private Vector menuIndex
;
38 private HashSet setOfShorts
;
39 private String restOfFile
;
40 private String currentContents
;
41 private String lineSeparator
=System
.getProperty("line.separator");
43 /** Creates a new instance of SamplesSupplier:
44 * plotType is obtained from the method getFormType of
45 * AbstractControlForm.
47 public SamplesSupplier(File modelFile
) {
49 this.modelFile
=modelFile
;
50 setOfShorts
=new HashSet();
54 shortBuffer
=new Vector();
56 menuIndex
=new Vector();
59 BufferedReader is
=new BufferedReader(new FileReader(modelFile
));
65 if (line
!=null && !line
.equals("--@@")){
66 line1
=line
+lineSeparator
;//restoring newline
68 if (line
.substring(0, min(line
.length(),3) ).equals("--%")){
69 String s
=new String(line1
);
73 s1
=" ft: "+SampleParser
.find(line
,3)+" sn: "+SampleParser
.find(line
,5);
74 shortBuffer
.addElement(s1
);
78 restOfFile
=restOfFile
+line1
;
80 currentContents
=currentContents
+line1
;
85 System
.out
.println(e
);
90 System
.out
.println(e
);
96 public void addSample(String s
, String sampleName
, String plotType
){
97 if (!buffer
.contains(s
)){
98 if (s
==null || s
.length()<3)
101 if (!s
.substring(0,3).equals("--%"))
103 }//this to avoid corruption of .lua model files
107 s1
=" ft: "+plotType
+" sn: "+sampleName
;
109 while (this.setOfShorts
.contains(s1
)){
110 s2
=JOptionPane
.showInputDialog("Name already exists, choose another:");
112 s2
=SampleParser
.toWord(s2
);
113 s1
=" ft: "+plotType
+" sn: "+s2
;
119 if (SampleParser
.numberOfWords(s1
)==4){
120 buffer
.addElement(s
);
121 shortBuffer
.addElement(s1
);
127 public void removeSample(String sampleName
,String plotType
){
129 if (sampleName
!=null){
130 int p
=menu
.indexOf(sampleName
);
131 int plong
= ((Integer
) menuIndex
.elementAt(p
)).intValue();
132 buffer
.removeElementAt(plong
);
133 shortBuffer
.removeElementAt(plong
);
134 setOfShorts
.remove(" ft: "+plotType
+" sn: "+sampleName
);
139 public Vector
getShortBuffer(){
143 private void buildMenu(String plotType
){
146 for (int i
=0;i
<shortBuffer
.size();i
++){
147 String s
=(String
) shortBuffer
.elementAt(i
);
148 if (s
.length()>5+plotType
.length()){
149 if (s
.substring(0,5+plotType
.length()).equals(" ft: "+plotType
)){
150 if (SampleParser
.find(s
,3).equals("sn:")){
151 menu
.addElement(SampleParser
.find(s
,4));
152 menuIndex
.addElement(new Integer(i
));
155 String s1
=s
.substring(5+plotType
.length(),s
.length());
157 menuIndex
.addElement(new Integer(i
));
164 /** returns a vector with names of samples corresponding to plotType given*/
165 public Vector
menuSamples(String plotType
){
171 public String
[] supplyMenu(){
172 String
[] result
=new String
[menu
.size()];
173 for (int i
=0; i
<menu
.size();i
++){
174 result
[i
]=SampleParser
.toString((String
) menu
.elementAt(i
));
179 public Vector
getBuffer(){
183 public String
longString(String sampleName
, String plotType
){
186 int pos
= shortBuffer
.indexOf(" ft: "+plotType
+" sn: "+sampleName
);
187 return (String
) buffer
.elementAt(pos
);
193 BufferedWriter os
=new BufferedWriter(new FileWriter(modelFile
));
195 for (int i
=0;i
<buffer
.size();i
++){
196 contents
=contents
+buffer
.elementAt(i
);
198 contents
=contents
+"--@@"+this.lineSeparator
;
199 contents
=contents
+restOfFile
;
201 currentContents
=contents
;
205 catch(IOException e
){
206 System
.out
.println(e
);
211 private int min(int a
,int b
){
212 if (a
<b
) return a
; else return b
;