/*
Chapter 7:
Programming Assignment 2
Programmer: Brad Shedd
Date:
March 16, 2004
Filename:
MusicXXX.java
Purpose:
This program creates a Swing interface
for sorting Music.
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.text.*;
public
class
MusicXXX
extends JFrame
implements
ActionListener
{
//construct
components
JTextPane
textPane =
new
JTextPane();
//initialize
data in arrays
String artist[]
= {"The
Beatles",
"Elvis Presley",
"Mariah Carey",
"Michael
Jackson"};
String genre[] = {"Rock",
"Rock",
"Pop",
"R&B"};
String greatestHit[] = {"Hey
Jude",
"Don't Be Cruel",
"One Sweet Day",
"Billie Jean"};
String recordLabel[] = {"Capitol",
"RCA Victor",
"Columbia",
"Epic"};
//construct
instance of Music
public
MusicXXX()
{
super("Music");
}
//create the
menu system
public
JMenuBar createMenuBar()
{
//create an
instance of the menu
JMenuBar mnuBar =
new
JMenuBar();
setJMenuBar(mnuBar);
//construct
and populate the File menu
JMenu mnuFile =
new
JMenu ("File",
true);
mnuFile.setMnemonic(KeyEvent.VK_F);
mnuFile.setDisplayedMnemonicIndex(0);
mnuBar.add(mnuFile);
JMenuItem
mnuFileExit =
new
JMenuItem ("Exit");
mnuFileExit.setMnemonic(KeyEvent.VK_X);
mnuFileExit.setDisplayedMnemonicIndex(1);
mnuFile.add(mnuFileExit);
mnuFileExit.setActionCommand("Exit");
mnuFileExit.addActionListener(this);
//construct
and populate the Edit menu
JMenu mnuEdit =
new
JMenu ("Edit",
true);
mnuEdit.setMnemonic(KeyEvent.VK_E);
mnuEdit.setDisplayedMnemonicIndex(0);
mnuBar.add(mnuEdit);
JMenuItem
mnuEditInsert =
new
JMenuItem ("Insert New DVD");
mnuEditInsert.setMnemonic(KeyEvent.VK_I);
mnuEditInsert.setDisplayedMnemonicIndex(0);
mnuEdit.add(mnuEditInsert);
mnuEditInsert.setActionCommand("Insert");
mnuEditInsert.addActionListener(this);
JMenu
mnuEditSort =
new
JMenu ("Sort",
true);
mnuEditSort.setMnemonic(KeyEvent.VK_R);
mnuEditSort.setDisplayedMnemonicIndex(3);
mnuEdit.add(mnuEditSort);
JMenuItem
mnuEditSortByArtist =
new
JMenuItem ("by Artist");
mnuEditSortByArtist.setMnemonic(KeyEvent.VK_A);
mnuEditSortByArtist.setDisplayedMnemonicIndex(3);
mnuEditSort.add(mnuEditSortByArtist);
mnuEditSortByArtist.setActionCommand("artists");
mnuEditSortByArtist.addActionListener(this);
JMenuItem
mnuEditSortByGenre =
new
JMenuItem ("by Genre");
mnuEditSortByGenre.setMnemonic(KeyEvent.VK_G);
mnuEditSortByGenre.setDisplayedMnemonicIndex(3);
mnuEditSort.add(mnuEditSortByGenre);
mnuEditSortByGenre.setActionCommand("genre");
mnuEditSortByGenre.addActionListener(this);
JMenuItem
mnuEditSortByGreatestHits =
new JMenuItem ("by
Greatest Hit");
mnuEditSortByGreatestHits.setMnemonic(KeyEvent.VK_H);
mnuEditSortByGreatestHits.setDisplayedMnemonicIndex(3);
mnuEditSort.add(mnuEditSortByGreatestHits);
mnuEditSortByGreatestHits.setActionCommand("greatest
hits");
mnuEditSortByGreatestHits.addActionListener(this);
JMenuItem
mnuEditSortByRecordLabel =
new JMenuItem ("by
Record Label");
mnuEditSortByRecordLabel.setMnemonic(KeyEvent.VK_R);
mnuEditSortByRecordLabel.setDisplayedMnemonicIndex(3);
mnuEditSort.add(mnuEditSortByRecordLabel);
mnuEditSortByRecordLabel.setActionCommand("record
label");
mnuEditSortByRecordLabel.addActionListener(this);
return
mnuBar;
}
//create the
content pane
public
Container createContentPane()
{
//create the
JTextPane and center panel
JPanel centerPanel
=
new
JPanel();
setTabsAndStyles(textPane);
textPane =
addTextToTextPane();
JScrollPane
scrollPane =
new
JScrollPane(textPane);
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
scrollPane.setPreferredSize(new
Dimension(700, 400));
centerPanel.add(scrollPane);
//create
Container and set attributes
Container c = getContentPane();
c.setLayout(new
BorderLayout(10, 10));
c.add(centerPanel, BorderLayout.CENTER);
return
c;
}
//method to
create tab stops and set font styles
protected
void setTabsAndStyles(JTextPane
textPane)
{
//create Tab
Stops
TabStop[] tabs =
new
TabStop[3];
tabs[0] =
new
TabStop(250, TabStop.ALIGN_LEFT, TabStop.LEAD_NONE);
tabs[1] =
new
TabStop(350, TabStop.ALIGN_LEFT, TabStop.LEAD_NONE);
tabs[2] =
new
TabStop(550, TabStop.ALIGN_LEFT, TabStop.LEAD_NONE);
TabSet
tabset =
new
TabSet(tabs);
//set Tab
Style
StyleContext tabStyle = StyleContext.getDefaultStyleContext();
AttributeSet aset =
tabStyle.addAttribute(SimpleAttributeSet.EMPTY, StyleConstants.TabSet,
tabset);
textPane.setParagraphAttributes(aset,
false);
//set Font
Style
Style fontStyle =
StyleContext.getDefaultStyleContext().getStyle(StyleContext.DEFAULT_STYLE);
Style
regular = textPane.addStyle("regular",
fontStyle);
StyleConstants.setFontFamily(fontStyle,
"SansSerif");
Style s =
textPane.addStyle("italic",
regular);
StyleConstants.setItalic(s,
true);
s =
textPane.addStyle("bold",
regular);
StyleConstants.setBold(s,
true);
s =
textPane.addStyle("large",
regular);
StyleConstants.setFontSize(s, 16);
}
//method to
add new text to the JTextPane
public
JTextPane addTextToTextPane()
{
Document
doc = textPane.getDocument();
try
{
//clear previous text
doc.remove(0, doc.getLength());
//insert title
doc.insertString(0,
"ARTIST\tGENRE\tGREATEST HIT\tRECORD LABEL\n",textPane.getStyle("large"));
//insert detail
for
(int j = 0; j<artist.length; j++)
{
doc.insertString(doc.getLength(), artist[j] +
"\t",
textPane.getStyle("bold"));
doc.insertString(doc.getLength(), genre[j] +
"\t",
textPane.getStyle(
"italic"));
doc.insertString(doc.getLength(), greatestHit[j] +
"\t",
textPane.getStyle("regular"));
doc.insertString(doc.getLength(),
recordLabel[j] +
"\n",
textPane.getStyle("regular"));
}
}
catch
(BadLocationException ble)
{
System.err.println("Couldn't
insert text.");
}
return
textPane;
}
//event to
process user clicks
public
void actionPerformed(ActionEvent e)
{
String arg
= e.getActionCommand();
//user
clicks Exit on the File menu
if(arg
==
"Exit")
System.exit(0);
//user clicks Insert New DVD on the Edit menu
if
(arg ==
"Insert")
{
//accept new data
String newArtist = JOptionPane.showInputDialog(null,
"Please enter the artists");
String newGenre = JOptionPane.showInputDialog(null,
"Please enter the genre");
String newGreatestHit = JOptionPane.showInputDialog(null,
"Please enter the greatest hits");
String newRecordLabel = JOptionPane.showInputDialog(null,
"Please enter the record label");
//enlarge arrays
artist = enlargeArray(artist);
genre = enlargeArray(genre);
greatestHit = enlargeArray(greatestHit);
recordLabel = enlargeArray(recordLabel);
//add new data to arrays
artist[artist.length-1] = newArtist;
genre[genre.length-1] = newGenre;
greatestHit[greatestHit.length-1] = newGreatestHit;
recordLabel[recordLabel.length-1] = newRecordLabel;
//call sort method
sort(artist);
}
//user clicks Artists on the Search submenu
if(arg
==
"artist")
sort(artist);
//user clicks Genre on the Search submenu
if(arg
==
"genre")
sort(genre);
//user clicks Greatest Hits on the Search submenu
if
(arg ==
"greatestHit")
sort(greatestHit);
//user clicks Record Label on the Search submenu
if
(arg ==
"recordLabel")
sort(recordLabel);
}
//method to enlarge an array by 1
public
String[] enlargeArray(String[] currentArray)
{
String[] newArray =
new
String[currentArray.length + 1];
for(int
i = 0; i<currentArray.length; i++)
newArray[i] = currentArray[i];
return
newArray;
}
//method to sort arrays
public
void sort(String tempArray[])
{
//loop to control number of passes
for
(int pass = 1; pass <
tempArray.length; pass++)
{
for
(int element = 0;
element<tempArray.length - 1; element ++)
if
(tempArray[element].compareTo(tempArray[element +1])>0)
{
swap(artist, element, element + 1);
swap(genre, element, element + 1);
swap(greatestHit, element, element + 1);
swap(recordLabel, element, element + 1);
}
}
addTextToTextPane();
}
//method to swap two elements of an array
public
void swap(String swapArray[],
int
first,
int second)
{
String hold;
//temporary holding area for swap
hold = swapArray[first];
swapArray[first] = swapArray[second];
swapArray[second] = hold;
}
public
void search(String searchField,
String searchArray[])
{
try
{
Document doc = textPane.getDocument();
// assign text to document
object
doc.remove(0,doc.getLength());
//clear
previous text
//display column titles
doc.insertString(0,"ARTIST\tGENRE\tGREATEST_HIT\tRECORD_LABEL\n",textPane.getStyle("large"));
//prompt user for search data
String search = JOptionPane.showInputDialog(null,
"Please enter the "+
searchField);
boolean
found =
false;
//search arrays
for
(int i = 0; i<artist.length; i++)
{
if
(search.compareTo(searchArray[i])==0)
{
doc.insertString(doc.getLength(), artist[i] +
"\t",
textPane.getStyle("bold"));
doc.insertString(doc.getLength(), genre[i] +
"\t",
textPane.getStyle("italic"));
doc.insertString(doc.getLength(), greatestHit[i] +
"\t",
textPane.getStyle("regular"));
doc.insertString(doc.getLength(), recordLabel[i] +
"\t",
textPane.getStyle("regular"));
found =
true;
}
}
if
(found ==
false)
{
JOptionPane.showMessageDialog(null,
"Your search produced no results.",
"No results found",JOptionPane.INFORMATION_MESSAGE);
sort(artist);
}
}
catch
(BadLocationException ble)
{
System.err.println("Couldn't
insert text.");
}
}
//main method executes at run time
public
static
void
main(String args[])
{
JFrame.setDefaultLookAndFeelDecorated(true);
MusicXXX f =
new
MusicXXX();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setJMenuBar(f.createMenuBar());
f.setContentPane(f.createContentPane());
f.setSize(800,475);
f.setVisible(true);
}
}