|
1 | 1 | package painttools.toolbar; |
| 2 | + |
2 | 3 | import java.awt.Button; |
3 | 4 | import java.awt.Component; |
4 | 5 | import java.awt.Graphics; |
5 | 6 | import java.awt.event.ActionEvent; |
6 | 7 | import java.awt.event.ActionListener; |
7 | 8 | import java.util.ArrayList; |
8 | 9 |
|
| 10 | +import javax.swing.BoxLayout; |
9 | 11 | import javax.swing.Icon; |
10 | 12 | import javax.swing.JButton; |
11 | 13 | import javax.swing.JPanel; |
12 | 14 | import javax.tools.Tool; |
13 | 15 |
|
14 | 16 | import painttools.tools.DotTool; |
15 | 17 | import painttools.tools.PaintTool; |
16 | | - |
| 18 | +import painttools.tools.SelectTool; |
17 | 19 |
|
18 | 20 | public class ToolBar extends JPanel { |
19 | | - |
20 | | - |
| 21 | + |
21 | 22 | public ArrayList<ToolBarListener> listeners; |
22 | | - |
23 | | - public ToolBar(){ |
| 23 | + |
| 24 | + /** |
| 25 | + * Creates a default toolbar and add necessary tools |
| 26 | + */ |
| 27 | + public ToolBar() { |
24 | 28 | listeners = new ArrayList<>(); |
| 29 | + |
| 30 | + //sets the box layout |
| 31 | + setLayout(new BoxLayout(this, BoxLayout.Y_AXIS)); |
| 32 | + |
25 | 33 | addTool(new DotTool()); |
| 34 | + addTool(new SelectTool()); |
26 | 35 | } |
27 | 36 |
|
| 37 | + /** |
| 38 | + * Adds a tool to the toolbar. This method will add specific tool to the |
| 39 | + * tool bar, and an action listener associated with it |
| 40 | + * |
| 41 | + * @param tool |
| 42 | + */ |
28 | 43 | private void addTool(PaintTool tool) { |
29 | 44 | JButton button = tool.getButton(); |
30 | 45 | button.addActionListener(new ActionListener() { |
31 | | - |
| 46 | + |
32 | 47 | @Override |
33 | 48 | public void actionPerformed(ActionEvent e) { |
34 | 49 | select(tool); |
35 | 50 | } |
36 | 51 | }); |
37 | 52 | add(button); |
38 | 53 | } |
39 | | - |
40 | | - public void addToolBarListener(ToolBarListener listener){ |
| 54 | + |
| 55 | + /** |
| 56 | + * Adds a ToolBarListener to this toolbar |
| 57 | + * @param listener |
| 58 | + */ |
| 59 | + public void addToolBarListener(ToolBarListener listener) { |
41 | 60 | listeners.add(listener); |
42 | 61 | } |
43 | | - |
44 | | - private void select(PaintTool tool){ |
| 62 | + |
| 63 | + /** |
| 64 | + * this method should be invoked by the actionlistener when an tool button is selected |
| 65 | + * @param tool |
| 66 | + */ |
| 67 | + private void select(PaintTool tool) { |
45 | 68 | for (ToolBarListener toolBarListener : listeners) { |
46 | 69 | toolBarListener.toolSelected(tool); |
47 | 70 | } |
|
0 commit comments