1  /*
  2   * File         : ShowFile.java
  3   *
  4  
  5  This Java applet divides the applet area into two parts:  an area
  6  with controls such as buttons and choices, called controlPanelNorth,
  7  and an area called dataPanel which has a TextArea to display
  8  data from a file.  The dataPanel is a separate class.
  9  
 10  The data points are read from a file which has the following format:
 11     an integer which is the number of items in the file
 12     items:  each item has an integer id no., a name, and a floating
 13  		point price
 14  
 15  The data is read once from the file into a set of arrays.  Then the
 16  data can be displayed in different ways depending on the controls.
 17  
 18   */
 19  
 20  import java.awt.*;
 21  import java.io.*;
 22  import java.net.*;
 23  
 24  public class ShowFile extends java.applet.Applet
 25  {
 26    Font f = new Font("TimesRoman", Font.ITALIC, 18);
 27  
 28    DisplayPanel dataPanel;	// the display panel, from the class below
 29    String inname;		// name of the data file
 30  
 31    Choice dataChoice;		//  control for type of data
 32    Button showfile;              //  display the data under current controls
 33    Label title;			//  label of the display
 34  
 35    public void init()
 36    { InputStream instream = null;
 37      Panel controlPanelNorth = new Panel();
 38  
 39      setLayout(new BorderLayout());
 40      
 41      // initialize the input stream and data panel
 42      try
 43        { inname = getParameter("inputfile");
 44          instream = new URL(getDocumentBase(), inname).openStream();
 45  	dataPanel = new DisplayPanel(instream);
 46         }
 47      catch (IOException e)
 48        { System.err.println
 49  	  ("I/O Exception:  probably bad filename or bad connection."
 50  	         + e.getMessage());
 51        }
 52  
 53    // make the control panel
 54    controlPanelNorth.setLayout(new FlowLayout(FlowLayout.LEFT, 5, 5));
 55  
 56    title = new Label("Grocery Items");
 57    title.setFont(f);
 58  
 59    dataChoice = new Choice();
 60    dataChoice.setFont(f);
 61    dataChoice.addItem("Fruits");
 62    dataChoice.addItem("Veggies");
 63  
 64    showfile = new Button("Show Items");
 65    showfile.setFont(f);
 66  
 67    controlPanelNorth.add(title);
 68    controlPanelNorth.add(dataChoice);
 69    controlPanelNorth.add(showfile);
 70  
 71  
 72    // add the two components to the applet area
 73    add("North", controlPanelNorth);
 74    add("Center", dataPanel);
 75    }
 76  
 77    public boolean action (Event e, Object arg)
 78    {
 79      if (e.target instanceof Choice)
 80        { 
 81  	dataPanel.settype (dataChoice.getSelectedItem());
 82   	return true;
 83        }
 84      else if (e.target instanceof Button)
 85        {
 86  	dataPanel.displayfile();
 87  	return true;
 88        }
 89      else return false;
 90    }
 91  }
 92  
 93  class DisplayPanel extends Panel
 94  {
 95    String datatype = "Fruits";	// either "Fruits" or "Veggies"
 96    StringBuffer buff;
 97    Font f = new Font("Monospaced", Font.ITALIC, 18);
 98    TextArea ta;
 99    
100    // variables to hold the data from the file 
101    int numdata;
102    int itemnumbers[];
103    String itemnames[];
104    double itemprices[];
105    int maxlength;
106  
107    public DisplayPanel(InputStream instream)
108    { 
109      setLayout(new BorderLayout());
110      ta = new TextArea("Grocery Item Display Area . . .", 10, 30);
111      ta.setFont (f);
112      add("Center", ta);
113   
114      try
115      {
116        StreamTokenizer intokens = new StreamTokenizer(instream);
117        intokens.parseNumbers();  intokens.slashSlashComments(true);
118        
119        /* read the number of data points and allocate data arrays */    
120        int tok = intokens.nextToken();  
121        numdata = (int) intokens.nval;
122        
123        itemnumbers = new int[numdata];
124        itemnames = new String[numdata];
125        itemprices = new double[numdata];
126  
127        // read data items 
128        maxlength = 0;
129        for (int i=0; i<numdata; i++)
130        { tok = intokens.nextToken();  itemnumbers[i] = (int)intokens.nval;
131          tok = intokens.nextToken();  itemnames[i] = intokens.sval;
132          tok = intokens.nextToken();  itemprices[i] = intokens.nval;
133  	maxlength = Math.max(maxlength, itemnames[i].length());
134        }
135  
136     /* for debugging
137        for (int i=0; i<numdata; i++)
138        { System.out.println(itemnumbers[i]);
139          System.out.println(itemnames[i]);
140          System.out.println(itemprices[i]);
141        }
142     */
143      }
144      catch (IOException e)
145        { System.err.println
146  	  ("I/O Exception:  Reading data from file"
147  	         + e.getMessage());
148        }
149    }
150  
151    public void settype (String s)
152    { datatype = s;
153    }
154  
155    public void displayfile()
156    { 
157      buff = new StringBuffer();
158      Color fruitcolor = new Color(127, 0, 0);
159      Color vegcolor = new Color(0, 127, 127);
160  
161      for (int i=0; i<numdata; i++)
162        { if (datatype.equals("Fruits") && itemnumbers[i]<=1000)
163  	  appendtext(fruitcolor, itemnumbers[i], itemnames[i], itemprices[i]);
164          if (datatype.equals("Veggies") && itemnumbers[i]>1000)
165  	  appendtext(vegcolor, itemnumbers[i], itemnames[i], itemprices[i]);
166        }
167      ta.setText(buff.toString());
168    }
169  
170    public void appendtext (Color c, int n, String s, double d)
171    {
172      ta.setForeground (c);
173      int diff = maxlength - s.length();
174  
175      buff.append( n + "      " + s);
176      for (int i=0; i<diff; i++) buff.append(" ");
177      buff.append( "  $" + d + "\n");
178    }
179  }