this.swing = swing;
setBorder(swing.emptyBorder5);
setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
// create the list
for(int i = 0; i < ITEMS; i++) {
model.addElement(new Integer(i));
}
listBox = new JList(model) {
public Dimension getMaximumSize() {
return new Dimension(400, super.getMaximumSize().height);
}
};
listBox.setCellRenderer(new TestCellRenderer(listBox));
// Create the controls
JPanel controlPanel = new JPanel() {
public Dimension getMaximumSize() {
return new Dimension(300, super.getMaximumSize().height);
}
};
controlPanel.setLayout(new BoxLayout(controlPanel, BoxLayout.Y_AXIS));
controlPanel.setBorder(swing.loweredBorder);
controlPanel.setAlignmentY(TOP_ALIGNMENT);
// List operations
JPanel pricePanel = swing.createHorizontalPanel(false);
pricePanel.setAlignmentY(TOP_ALIGNMENT);
pricePanel.setAlignmentX(LEFT_ALIGNMENT);
controlPanel.add(pricePanel);
purchase = new JButton("Purchase");
purchase.setToolTipText("Adds the selected item(s) to your grocery bill.");
pricePanel.add(purchase);
pricePanel.add(Box.createRigidArea(swing.hpad10));
priceLabel = new JLabel("Total: ");
pricePanel.add(priceLabel);
controlPanel.add(Box.createRigidArea(swing.vpad20));
JLabel l = new JLabel("Jump To:");
l.setFont(swing.boldFont);
controlPanel.add(l);
ButtonGroup group = new ButtonGroup();
fastfoodRadioButton = new JRadioButton("Fast Food");
fastfoodRadioButton.setToolTipText("Calls list.ensureVisible() to jump to the items.");
group.add(fastfoodRadioButton);
controlPanel.add(fastfoodRadioButton);
dessertRadioButton = new JRadioButton("Desserts");
dessertRadioButton.setToolTipText("Calls list.ensureVisible() to jump to the items.");
group.add(dessertRadioButton);
controlPanel.add(dessertRadioButton);
fruitRadioButton = new JRadioButton("Fruits");
fruitRadioButton.setToolTipText("Calls list.ensureVisible() to jump to the items.");
group.add(fruitRadioButton);
controlPanel.add(fruitRadioButton);
veggieRadioButton = new JRadioButton("Vegetables");
veggieRadioButton.setToolTipText("Calls list.ensureVisible(index) to jump to the items.");
group.add(veggieRadioButton);
controlPanel.add(veggieRadioButton);
controlPanel.add(Box.createRigidArea(swing.vpad20));
l = new JLabel("Show:");
l.setFont(swing.boldFont);
controlPanel.add(l);
fastfoodCheckbox = new JCheckBox("Fast Food");
fastfoodCheckbox.setToolTipText("Calls list.remove(index1,indexN) to remove items.");
fastfoodCheckbox.setSelected(true);
controlPanel.add(fastfoodCheckbox);
dessertCheckbox = new JCheckBox("Desserts");
dessertCheckbox.setToolTipText("Calls list.remove(index1,indexN) to remove items.");
dessertCheckbox.setSelected(true);
controlPanel.add(dessertCheckbox);
fruitCheckbox = new JCheckBox("Fruits");
fruitCheckbox.setToolTipText("Calls list.remove(index1,indexN) to remove items.");
fruitCheckbox.setSelected(true);
controlPanel.add(fruitCheckbox);
veggieCheckbox = new JCheckBox("Vegetables");
veggieCheckbox.setToolTipText("Calls list.remove(index1,indexN) to remove items.");
veggieCheckbox.setSelected(true);
controlPanel.add(veggieCheckbox);
controlPanel.add(Box.createGlue());
reset = new JButton("Reset");
reset.setToolTipText("Resets the state of the demo.");
controlPanel.add(reset);
scrollPane = new JScrollPane(listBox);
scrollPane.setAlignmentX(LEFT_ALIGNMENT);
scrollPane.setAlignmentY(TOP_ALIGNMENT);
add(scrollPane);
add(Box.createRigidArea(swing.hpad10));
add(controlPanel);
ActionListener purchaseListener = new ActionListener() {
public void actionPerformed(ActionEvent e) {
int first = listBox.getMinSelectionIndex();
int last = listBox.getMaxSelectionIndex();
if(first < 0) {
return;
}
for(int i = first; i <= last; i++) {
Integer item = (Integer) model.getElementAt(i);
listPrice += price[item.intValue()];
}
priceLabel.setText("Total: $" + ((double) listPrice)/100.0);
priceLabel.repaint();
}
};
purchase.addActionListener(purchaseListener);
ActionListener showListener = new ActionListener() {
public void actionPerformed(ActionEvent e) {
JCheckBox cb = (JCheckBox) e.getSource();
String label = cb.getText();
if(!cb.isSelected()) {
if(label.equals("Fast Food")) {
fastfoodShown = false;
for(int i = fastfoodIndex; i < fastfoodIndex+5; i++) {
model.removeElementAt(fastfoodIndex);
}
fastfoodRadioButton.setEnabled(false);
dessertIndex -= 5;
fruitIndex -= 5;
veggieIndex -= 5;
scrollPane.validate();
} else if(label.equals("Desserts")) {
for(int i = dessertIndex; i < dessertIndex+5; i++) {
model.removeElementAt(dessertIndex);
}
dessertRadioButton.setEnabled(false);
fruitIndex -= 5;
veggieIndex -= 5;
scrollPane.validate();
} else if(label.equals("Fruits")) {
for(int i = fruitIndex; i < fruitIndex+5; i++) {
model.removeElementAt(fruitIndex);
}
fruitRadioButton.setEnabled(false);
veggieIndex -= 5;
scrollPane.validate();
} else if(label.equals("Vegetables")) {
for(int i = veggieIndex; i < veggieIndex+5; i++) {
model.removeElementAt(veggieIndex);
}
veggieRadioButton.setEnabled(false);
scrollPane.validate();
}
if(model.getSize() < 1)
listBox.getParent().repaint();
} else {
if(label.equals("Fast Food")) {
model.insertElementAt(new Integer(4), 0);
model.insertElementAt(new Integer(3), 0);
model.insertElementAt(new Integer(2), 0);
model.insertElementAt(new Integer(1), 0);
model.insertElementAt(new Integer(0), 0);
dessertIndex += 5;
fruitIndex += 5;
veggieIndex += 5;
fastfoodRadioButton.setEnabled(true);
scrollPane.validate();
} else if(label.equals("Desserts")) {
model.insertElementAt(new Integer(9), dessertIndex);
model.insertElementAt(new Integer(8), dessertIndex);
model.insertElementAt(new Integer(7), dessertIndex);
model.insertElementAt(new Integer(6), dessertIndex);
model.insertElementAt(new Integer(5), dessertIndex);
fruitIndex += 5;
veggieIndex += 5;
dessertRadioButton.setEnabled(true);
scrollPane.validate();
} else if(label.equals("Fruits")) {
model.insertElementAt(new Integer(14), fruitIndex);
model.insertElementAt(new Integer(13), fruitIndex);
model.insertElementAt(new Integer(12), fruitIndex);
model.insertElementAt(new Integer(11), fruitIndex);
model.insertElementAt(new Integer(10), fruitIndex);
veggieIndex += 5;
fruitRadioButton.setEnabled(true);
scrollPane.validate();
} else if(label.equals("Vegetables")) {
model.insertElementAt(new Integer(19), veggieIndex);
model.insertElementAt(new Integer(18), veggieIndex);
model.insertElementAt(new Integer(17), veggieIndex);
model.insertElementAt(new Integer(16), veggieIndex);
model.insertElementAt(new Integer(15), veggieIndex);
veggieRadioButton.setEnabled(true);
scrollPane.validate();
}
}
}
};
fruitCheckbox.addActionListener(showListener);
veggieCheckbox.addActionListener(showListener);
dessertCheckbox.addActionListener(showListener);
fastfoodCheckbox.addActionListener(showListener);
ActionListener jumpListener = new ActionListener() {
public void actionPerformed(ActionEvent e) {
JRadioButton rb = (JRadioButton) e.getSource();
if(rb.isSelected()) {
String label = rb.getText();
if(label.equals("Fruits")) {
listBox.ensureIndexIsVisible(fruitIndex+5);
listBox.ensureIndexIsVisible(fruitIndex);
} else if(label.equals("Desserts")) {
listBox.ensureIndexIsVisible(dessertIndex+5);
listBox.ensureIndexIsVisible(dessertIndex);
} else if(label.equals("Vegetables")) {
listBox.ensureIndexIsVisible(veggieIndex+5);
listBox.ensureIndexIsVisible(veggieIndex);
} else if(label.equals("Fast Food")) {
listBox.ensureIndexIsVisible(fastfoodIndex+5);
listBox.ensureIndexIsVisible(fastfoodIndex);
}
}
}
};
fruitRadioButton.addActionListener(jumpListener);
veggieRadioButton.addActionListener(jumpListener);
dessertRadioButton.addActionListener(jumpListener);
fastfoodRadioButton.addActionListener(jumpListener);
ActionListener resetListener = new ActionListener() {
public void actionPerformed(ActionEvent e) {
resetAll();
}
};
reset.addActionListener(resetListener);