001    package swingui;
002    
003    import java.awt.BorderLayout;
004    import java.awt.Dimension;
005    import java.awt.GridLayout;
006    import java.awt.event.ActionEvent;
007    import java.awt.event.ActionListener;
008    import java.awt.event.MouseAdapter;
009    import java.awt.event.MouseEvent;
010    
011    import javax.swing.JButton;
012    import javax.swing.JLabel;
013    import javax.swing.JPanel;
014    import javax.swing.JScrollPane;
015    
016    import physics3d.Vect3;
017    
018    import components.GameObject;
019    
020    public class TargetPanel extends JPanel implements ActionListener {
021            //fields
022            /** the object whose targets we are inspecting **/
023            private GameObject obj; 
024            private MainGUI gui; 
025            
026            //components
027            JPanel targetList;
028            JButton addTargetButton;
029            JButton clearTargetsButton;
030            
031            public TargetPanel(MainGUI gui) {
032                    super();
033                    this.gui = gui;
034                
035                    //title & list of targets
036                    JLabel targetLabel = new JLabel(" Targets: ");
037                    targetList = new JPanel(new GridLayout(0, 1)); //1 column
038                    JScrollPane targetScroll = new JScrollPane(targetList,
039                                    JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
040                                    JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
041                    //targetScroll.setPreferredSize(new Dimension(20, 20));
042                    
043                    //add/clear buttons
044                    addTargetButton = new JButton("Add");
045                    addTargetButton.addActionListener(this);
046                    clearTargetsButton = new JButton("Clear");
047                    clearTargetsButton.addActionListener(this);
048    
049                    //layout
050                    JPanel targetButtonPanel = new JPanel();
051                    targetButtonPanel.add(addTargetButton);
052                    targetButtonPanel.add(clearTargetsButton);
053                    this.setLayout(new BorderLayout());
054                    add(targetLabel, BorderLayout.NORTH);
055                    add(targetScroll, BorderLayout.CENTER);
056                    add(targetButtonPanel, BorderLayout.SOUTH);
057            }
058            
059            /** set the object whose targets we are inspecting **/
060            public void setObject(GameObject obj) {
061                    this.obj = obj;
062                    if (obj != null) {
063                            updateTargetList();
064                    } else {
065                            clearTargetList();
066                    }
067            }
068            
069            /** on actionEvent:
070             * if add - set listener on gui for adding
071             * if clear - clear all targets
072             * otherwise - delete only the given target
073             */
074            public void actionPerformed(ActionEvent e) {
075                    if (e.getSource().equals(addTargetButton)) {
076                            //System.out.println("clicked add target");
077                            gui.setCurrentListener (new AddTargetListener());
078                    } else if (e.getSource().equals(clearTargetsButton)) {
079                            System.err.println("clicked clear targets");
080                            obj.clearTargets();
081                            updateTargetList();
082                    } else {
083                            System.err.println("clicked on an x");
084                            TargetButton src = (TargetButton)e.getSource();
085                            obj.removeTarget(src.target());
086                            targetList.remove(src);
087                            updateTargetList();
088                    } //else delete the given target
089            }
090    
091            /** populate target list with the targets for the current obj **/
092            private void updateTargetList() {
093                    clearTargetList();
094                    for (GameObject tgt : obj.getTargets()) {
095                            TargetButton t = new TargetButton(tgt);
096                            t.addActionListener(this);
097                            targetList.add(t);
098                    }
099                    targetList.validate();
100                    this.repaint();
101            }
102            /** clear targetlist panel **/
103            private void clearTargetList() {
104                    targetList.removeAll();
105            }
106            
107            /** add target listener is a mouselister intended to be registerd on the 
108             *  GLcanvas. the next thing clicked on is added as a target
109             */
110            private class AddTargetListener extends MouseAdapter {
111                    @Override
112                    public void mouseClicked(MouseEvent e) {
113                            ////System.out.println("adding target");
114                            Vect3 gridLoc = gui.getGridLocation(e.getX(), e.getY());
115                            GameObject tgt = gui.getObjectInGrid(gridLoc);
116                            if (tgt != null) {
117                                    obj.addTarget(tgt);
118                                    updateTargetList();
119                                    gui.getCanvas().removeMouseListener(this);
120                            }
121                    }
122            }
123            
124            public String getName() { return "Targets"; }
125            
126            /** TargetButton extends JButton and represents a target ( a GameObject ) **/
127            private class TargetButton extends JButton {
128                    private GameObject target;
129    
130                    public TargetButton(GameObject tgt) {
131                            super();
132                            this.target = tgt;
133                            
134                            this.setText(tgt.getName() + "   \u2717");
135                            this.setToolTipText("<"+tgt.getCenter()+">");
136                    }
137                    
138                    /** get the target this button represents **/
139                    public GameObject target() {
140                            return target;
141                    }
142            }
143    
144            
145    }