Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
ToolTrackerAction |
|
| 1.5;1.5 |
1 | /* | |
2 | * Copyright (c) 2009, Swiss Federal Department of Defence Civil Protection and Sport | |
3 | * (http://www.vbs.admin.ch) | |
4 | * Copyright (c) 2009, Ergon Informatik AG (http://www.ergon.ch) | |
5 | * All rights reserved. | |
6 | * | |
7 | * Licensed under the Open Permis License which accompanies this distribution, | |
8 | * and is available at http://www.openpermis.org/BSDlicenceKent.txt | |
9 | */ | |
10 | package org.openpermis.editor.policy.view; | |
11 | ||
12 | import java.awt.event.ActionEvent; | |
13 | ||
14 | import org.openpermis.editor.policy.gui.SelectableAction; | |
15 | ||
16 | /** | |
17 | * Action that tracks the state of a tool. | |
18 | * <p>Opens and closes the tool if the action is executed.</p> | |
19 | * @since 0.1.0 | |
20 | */ | |
21 | @SuppressWarnings("serial") | |
22 | public class ToolTrackerAction | |
23 | extends SelectableAction | |
24 | implements ViewContextListener | |
25 | { | |
26 | ||
27 | //---- State | |
28 | ||
29 | /** | |
30 | * The tool to track. | |
31 | * @since 0.1.0 | |
32 | */ | |
33 | private final Tool tool; | |
34 | ||
35 | /** | |
36 | * The view context of the tool to track. | |
37 | * @since 0.1.0 | |
38 | */ | |
39 | private final ViewContext viewContext; | |
40 | ||
41 | //---- Constructors | |
42 | ||
43 | /** | |
44 | * Creates a new tool tracker for the specified tool and view context. | |
45 | * @note The tool must not be opened at the time the tracker is created! | |
46 | * @param tool the tool to track. | |
47 | * @param viewContext the view context of the tool to track. | |
48 | * @since 0.1.0 | |
49 | */ | |
50 | public ToolTrackerAction (Tool tool, ViewContext viewContext) { | |
51 | 0 | super(tool.getTitle(), tool.getIcon()); |
52 | 0 | this.tool = tool; |
53 | 0 | this.viewContext = viewContext; |
54 | 0 | this.viewContext.addViewContextListener(this); |
55 | 0 | } |
56 | ||
57 | //---- SelectableAction | |
58 | ||
59 | /** | |
60 | * @since 0.1.0 | |
61 | */ | |
62 | @Override | |
63 | public void actionPerformed (ActionEvent e) { | |
64 | 0 | if (isSelected()) { |
65 | 0 | this.viewContext.openView(this.tool); |
66 | } else { | |
67 | 0 | setSelected(!this.viewContext.closeView(this.tool, false)); |
68 | } | |
69 | 0 | } |
70 | ||
71 | //---- ViewContextListener | |
72 | ||
73 | /** | |
74 | * @since 0.1.0 | |
75 | */ | |
76 | public void viewActivated (View view) { | |
77 | // Nop. | |
78 | 0 | } |
79 | ||
80 | /** | |
81 | * @since 0.1.0 | |
82 | */ | |
83 | public void viewDeactivated (View view) { | |
84 | // Nop. | |
85 | 0 | } |
86 | ||
87 | /** | |
88 | * @since 0.1.0 | |
89 | */ | |
90 | public void viewOpened (View view) { | |
91 | 0 | if (view == this.tool) { |
92 | 0 | setSelected(true); |
93 | } | |
94 | 0 | } |
95 | ||
96 | /** | |
97 | * @since 0.1.0 | |
98 | */ | |
99 | public void viewClosed (View view) { | |
100 | 0 | if (view == this.tool) { |
101 | 0 | setSelected(false); |
102 | } | |
103 | 0 | } |
104 | ||
105 | } |