// SortingDemoGUI1.java
// Demonstrates selection sort using file of short integers.
// Demonstrates use of a simple GUI to display the numbers.
//
// Quits with an error message to the console if the
// array numbers is too small to hold the numbers from
// the input text file.
//
// Uses, in this directory:
// SortingDemoExceptions.java
// TextFileInput.java
// TextFileOutput.java
//
// To compile this program in Java 1.4, type:
// javac -source 1.4 SortingDemoGUI1.java
// (Compile it normally in Java 1.5 or later.)
//
// To run this program with assertions enabled, type:
// java -ea SortingDemoGUI1
// (Otherwise, assertions are disabled.)
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.GridLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
/**
* Sorts the numbers in a text file containing
* short integers, one number per line, and
* then displays, in text areas, both the original
* list of numbers and the sorted version.
*
* @author Dorothy L. Nixon
*/
public class SortingDemoGUI1 extends JFrame
{
final static int MAX_NUMBER_OF_NUMBERS = 40;
private short[] numbers = new short[MAX_NUMBER_OF_NUMBERS];
private int lengthFilled = 0;
private JTextArea textAreaOriginal;
private JTextArea textAreaSorted;
private JTextField messageField;
/**
* Creates this window
*/
public SortingDemoGUI1()
{
setSize(450, 300);
setLocation(100, 100);
setTitle("Sorts file of short integers");
Container contentPane = getContentPane();
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(1, 2));
contentPane.add(panel, BorderLayout.CENTER);
textAreaOriginal = new JTextArea();
textAreaOriginal.setEditable(false);
panel.add(new JScrollPane(textAreaOriginal));
textAreaSorted = new JTextArea();
textAreaSorted.setEditable(false);
panel.add(new JScrollPane(textAreaSorted));
messageField = new JTextField();
messageField.setEditable(false);
contentPane.add(messageField, BorderLayout.SOUTH);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
} // constructor
/**
* Main method.
*
* @param args command-line arguments.
* One command-line argument is
* needed, for the input file.
*/
public static void main(String[] args)
{
if ( args.length == 0 )
{
System.out.println("This program sorts short integers");
System.out.println("from the file specified by a");
System.out.println("command-line argument.");
System.exit(0);
} // if
SortingDemoGUI1 window = new SortingDemoGUI1();
final String inputFilename = args[0];
if ( window.open(inputFilename) )
{
window.sort();
window.saveAs("sorted-" + inputFilename);
} // if
} // method main(String[])
/**
* Reads a list of numbers from the specified
* text file, displays them to a text area,
* and holds the list of numbers for further
* use by this window.
*
* @param filename filename of input text file
* @return <code>true</code> if all the numbers
* in the file were read successfully,
* <code>false</code> otherwise.
*/
private boolean open(String filename)
{
// Open file for reading:
TextFileInput in = new TextFileInput(filename);
// Read numbers into array:
String line = in.readLine(); // read first line in file
while ( lengthFilled < numbers.length && line != null )
{
numbers[lengthFilled] = Short.parseShort(line);
line = in.readLine(); // read next line in file
lengthFilled++;
} // while
// Close input file:
in.close();
// Display the numbers to the text area intended
// for display of as-yet unsorted list of numbers:
displayNumbers(textAreaOriginal);
// Check to see if all the numbers in the file were read.
// If not, then the array wasn't big enough to hold them all.
// In that case, print an error message.
if ( line != null ) // i.e. if end-of-file not reached
{
messageField.setText("Too many numbers in file."
+ " Exceeds capacity "
+ numbers.length + ".");
return false;
} // if
return true;
} // method open
/**
* Sorts this window's list of numbers and
* displays the sorted list to a text area.
*/
private void sort()
{
SortingDemoExceptions.selectionSort(numbers, lengthFilled);
displayNumbers(textAreaSorted);
} // method sortAndDisplay
/**
* Outputs this window's list of numbers to
* the specified text file
*
* @param filename text file for output of
* list of numbers
*/
private void saveAs(String filename)
{
// Create file for output:
TextFileOutput out = new TextFileOutput(filename);
// Print numbers to output text file:
for ( int i = 0; i < lengthFilled; i++ )
out.println(numbers[i]);
// Make sure the output finds its way to the file,
// and close it:
out.flush();
out.close();
// Print message indicating successful completion:
messageField.setText("File " + filename
+ " has been created.");
} // method saveAs
/**
* Displays this window's list of numbers
* to the specified text area.
*
* @param textArea text area where numbers will be displayed.
*/
private void displayNumbers(JTextArea textArea)
{
String lineBreak = System.getProperty("line.separator");
for ( int i = 0; i < lengthFilled; i++ )
{
textArea.append(Double.toString(numbers[i]));
textArea.append(lineBreak);
} // for
} // method displayNumbers
} // class SortingDemoGUI1