initial import
[iDMC.git] / src / java / org / tsho / dmc2 / ui / SamplesSupplier.java
blob4283dab06f7271e74c0c0a8265c84da791ef63a1
1 /*
2 * SamplesSupplier.java
4 * Created on August 28, 2004, 7:09 PM
5 */
7 package org.tsho.dmc2.ui;
9 import java.util.Hashtable;
10 import java.util.HashSet;
11 import java.util.Vector;
12 import java.io.File;
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;
24 /**
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
28 * file extension).
29 * @author altair
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;
36 private Vector menu;
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();
51 restOfFile="";
52 currentContents="";
53 buffer=new Vector();
54 shortBuffer=new Vector();
55 menu=new Vector();
56 menuIndex=new Vector();
58 try{
59 BufferedReader is=new BufferedReader(new FileReader(modelFile));
60 String line=" ";
61 String line1="";
62 try{
63 while (line!=null){
64 line=is.readLine();
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);
70 buffer.add(s);//
71 String s1;
73 s1=" ft: "+SampleParser.find(line,3)+" sn: "+SampleParser.find(line,5);
74 shortBuffer.addElement(s1);
75 setOfShorts.add(s1);
77 else{
78 restOfFile=restOfFile+line1;
80 currentContents=currentContents+line1;
84 catch(IOException e){
85 System.out.println(e);
87 is.close();
89 catch(IOException 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)
99 s="--%";
100 else{
101 if (!s.substring(0,3).equals("--%"))
102 s="--%"+s;
103 }//this to avoid corruption of .lua model files
105 String s1;
107 s1=" ft: "+plotType+" sn: "+sampleName;
108 String s2;
109 while (this.setOfShorts.contains(s1)){
110 s2=JOptionPane.showInputDialog("Name already exists, choose another:");
111 if (s2!=null){
112 s2=SampleParser.toWord(s2);
113 s1=" ft: "+plotType+" sn: "+s2;
115 else{
116 s1="";
119 if (SampleParser.numberOfWords(s1)==4){
120 buffer.addElement(s);
121 shortBuffer.addElement(s1);
122 setOfShorts.add(s1);
127 public void removeSample(String sampleName,String plotType){
128 String t;
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);
135 this.flush();
139 public Vector getShortBuffer(){
140 return shortBuffer;
143 private void buildMenu(String plotType){
144 menu.clear();
145 menuIndex.clear();
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));
154 else{
155 String s1=s.substring(5+plotType.length(),s.length());
156 menu.addElement(s1);
157 menuIndex.addElement(new Integer(i));
164 /** returns a vector with names of samples corresponding to plotType given*/
165 public Vector menuSamples(String plotType){
166 buildMenu(plotType);
167 return menu;
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));
176 return result;
179 public Vector getBuffer(){
180 return buffer;
183 public String longString(String sampleName, String plotType){
184 String s;
186 int pos = shortBuffer.indexOf(" ft: "+plotType+" sn: "+sampleName);
187 return (String) buffer.elementAt(pos);
191 public void flush(){
192 try{
193 BufferedWriter os=new BufferedWriter(new FileWriter(modelFile));
194 String contents="";
195 for (int i=0;i<buffer.size();i++){
196 contents=contents+buffer.elementAt(i);
198 contents=contents+"--@@"+this.lineSeparator;
199 contents=contents+restOfFile;
200 os.write(contents);
201 currentContents=contents;
202 os.flush();
203 os.close();
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;