// TODO: Put in ui package: //package ui; package startLab7; //import shapes.CompositeShape; //import shapes.ShapeManager; //import shapes.Shape; import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.util.ArrayList; import java.util.List; import java.util.Observable; /** * @author hornick * ShapeListUI - implements the UI that displays a list of shapes in a collection */ public class ShapeListUI extends JFrame /*implements java.util.Observer*/ { private JList jlContents; private String cmdCombine = "Combine"; private String cmdDecompose = "Decompose"; /** * Class constructor: creates the UI */ public ShapeListUI() { EventHandler eventHandler = new EventHandler(); //Window setTitle("SE2811 Shape List"); // initial title setSize(250, 400); setLocation(200, 200); setResizable(true); setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE); setVisible(true); Container contentPane = getContentPane(); BorderLayout bl = new BorderLayout(0,0); contentPane.setLayout(bl); // contentPane.setBackground(Color.BLACK); //Panel that contains the add, cancel, and save buttons JPanel jp = new JPanel(); jp.setLayout(new FlowLayout(FlowLayout.CENTER)); jp.setBackground(new Color(113, 142, 169)); //"Add" button JButton btnCombine = new JButton(cmdCombine); //btnAdd.setBounds(20, 400, 139, 50); btnCombine.addActionListener(eventHandler); jp.add(btnCombine); //"Remove" button JButton btnDecompose = new JButton(cmdDecompose); //btnAdd.setBounds(20, 400, 139, 50); btnDecompose.addActionListener(eventHandler); jp.add(btnDecompose); contentPane.add(jp, BorderLayout.SOUTH); jlContents = new JList(new DefaultListModel() ); // jlContents.setBackground(Color.BLACK); // jlContents.setForeground(Color.WHITE); Font font = new Font("Arial", Font.PLAIN, 12 ); jlContents.setFont(font); JScrollPane spPLContents = new JScrollPane(jlContents, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS); // CCT lab 6 spPLContents.setBounds(20, 40, 458, 340); contentPane.add(spPLContents, BorderLayout.CENTER); //"Contents" label JLabel lblContents = new JLabel(); lblContents.setBounds(20, 20, 458, 20); lblContents.setText("Shape contents:"); contentPane.add(lblContents, BorderLayout.NORTH); contentPane.validate(); contentPane.repaint(); //TODO: observe ShapeManager } //TODO: @Override? /** * override of java.util.Observable's update() method. * Regenerates the list of shapes contained in the ShapeManager's collection */ public void update(Observable o, Object arg) { DefaultListModel list = (DefaultListModel)jlContents.getModel(); list.clear(); //TODO: complete } /** * gathers the selected shapes from the list and calls * the appropriate ShapeManager methods to composite the selected shapes */ private void handleCombine() { Object[] selectedShapes = jlContents.getSelectedValues(); // TODO: complete } /** * gathers the selected shapes from the list and calls * the appropriate ShapeManager methods to decompose the selected composites; * ignores non-composite selections */ private void handleDecompose() { Object[] selectedComposites = jlContents.getSelectedValues(); //TODO: complete } /** * ShapeHierarchyUI event handler * @author hornick * */ private class EventHandler implements ActionListener { /* (non-Javadoc) * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent) */ public void actionPerformed(ActionEvent event) { String cmd = event.getActionCommand(); if( cmd.equals(cmdCombine)) handleCombine(); if( cmd.equals(cmdDecompose)) handleDecompose(); } } }