/
ToolsActions.java
229 lines (191 loc) · 6.4 KB
/
ToolsActions.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
/*
This file is a part of Angry IP Scanner source code,
see http://www.angryip.org/ for more information.
Licensed under GPLv2.
*/
package net.azib.ipscan.gui.actions;
import net.azib.ipscan.config.CommandLineProcessor;
import net.azib.ipscan.config.GUIConfig;
import net.azib.ipscan.config.Labels;
import net.azib.ipscan.core.ScanningResult.ResultType;
import net.azib.ipscan.core.ScanningResultList;
import net.azib.ipscan.core.state.ScanningState;
import net.azib.ipscan.core.state.StateMachine;
import net.azib.ipscan.core.state.StateMachine.Transition;
import net.azib.ipscan.core.state.StateTransitionListener;
import net.azib.ipscan.gui.*;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Table;
import static net.azib.ipscan.core.ScanningResult.ResultType.*;
/**
* ToolsActions
*
* @author Anton Keks
*/
public class ToolsActions {
public static final class Preferences implements Listener {
private final PreferencesDialog preferencesDialog;
private final ResultTable resultTable;
private final StatusBar statusBar;
public Preferences(PreferencesDialog preferencesDialog, ResultTable resultTable, StatusBar statusBar) {
this.preferencesDialog = preferencesDialog;
this.resultTable = resultTable;
this.statusBar = statusBar;
}
public void handleEvent(Event event) {
// show the preferences dialog
preferencesDialog.open();
// refresh the results and status bar in case anything was changed
resultTable.updateResults();
resultTable.updateColumnNames();
statusBar.updateConfigText();
}
}
public static final class ChooseFetchers implements Listener {
private final SelectFetchersDialog selectFetchersDialog;
public ChooseFetchers(SelectFetchersDialog selectFetchersDialog) {
this.selectFetchersDialog = selectFetchersDialog;
}
public void handleEvent(Event event) {
selectFetchersDialog.open();
}
}
public static final class ScanStatistics implements Listener, StateTransitionListener {
private final StatisticsDialog statisticsDialog;
private final GUIConfig guiConfig;
public ScanStatistics(GUIConfig guiConfig, StatisticsDialog statisticsDialog, StateMachine stateMachine, CommandLineProcessor commandProcessor) {
this.guiConfig = guiConfig;
this.statisticsDialog = statisticsDialog;
// register for state changes
if (!commandProcessor.shouldAutoQuit())
stateMachine.addTransitionListener(this);
}
public void handleEvent(Event event) {
statisticsDialog.open();
}
public void transitionTo(ScanningState state, Transition transition) {
// switching to IDLE means the end of scanning
if (transition == Transition.COMPLETE && guiConfig.showScanStats) {
handleEvent(null);
}
}
}
/**
* This listener updates the status bar when user selects many items in the result table
*/
public static final class TableSelection implements Listener {
private final StatusBar statusBar;
private final StateMachine stateMachine;
public TableSelection(StatusBar statusBar, StateMachine stateMachine) {
this.statusBar = statusBar;
this.stateMachine = stateMachine;
}
public void handleEvent(Event event) {
if (stateMachine.inState(ScanningState.IDLE)) {
Table resultTable = (Table) event.widget;
int selectionCount = resultTable.getSelectionCount();
if (selectionCount > 1)
statusBar.setStatusText(selectionCount + Labels.getLabel("text.hostsSelected"));
else
statusBar.setStatusText(null);
}
}
}
static abstract class SelectDesired implements Listener {
private final ResultTable resultTable;
private final ScanningResultList results;
public SelectDesired(ResultTable resultTable) {
this.resultTable = resultTable;
this.results = resultTable.getScanningResults();
}
public void handleEvent(Event event) {
int count = resultTable.getItemCount();
resultTable.deselectAll();
for (int i = 0; i < count; i++) {
if (isDesired(results.getResult(i).getType())) {
resultTable.select(i);
}
}
event.widget = resultTable;
resultTable.notifyListeners(SWT.Selection, event);
resultTable.forceFocus();
}
abstract boolean isDesired(ResultType type);
}
public static final class SelectAlive extends SelectDesired {
public SelectAlive(ResultTable resultTable) {
super(resultTable);
}
boolean isDesired(ResultType type) {
return type.ordinal() >= ALIVE.ordinal();
}
}
public static final class SelectDead extends SelectDesired {
public SelectDead(ResultTable resultTable) {
super(resultTable);
}
boolean isDesired(ResultType type) {
return type == DEAD;
}
}
public static final class SelectWithPorts extends SelectDesired {
public SelectWithPorts(ResultTable resultTable) {
super(resultTable);
}
boolean isDesired(ResultType type) {
return type == WITH_PORTS;
}
}
public static final class SelectWithoutPorts extends SelectDesired {
public SelectWithoutPorts(ResultTable resultTable) {
super(resultTable);
}
boolean isDesired(ResultType type) {
return type == ALIVE;
}
}
/**
* This cannot be accessed from the menu, but it provides the Ctrl+A
* Select All functionality for Windows (other platforms implement this themselves)
*/
public static class SelectAll implements Listener {
private final ResultTable resultTable;
public SelectAll(ResultTable resultTable) {
this.resultTable = resultTable;
}
public void handleEvent(Event event) {
// Ctrl+A handler
if (event.type == SWT.KeyDown && event.keyCode == 'a' && event.stateMask == SWT.MOD1) {
resultTable.selectAll();
// update selection status
event.widget = resultTable;
resultTable.notifyListeners(SWT.Selection, event);
event.doit = false;
}
}
}
public static final class SelectInvert implements Listener {
private final ResultTable resultTable;
public SelectInvert(ResultTable resultTable) {
this.resultTable = resultTable;
}
public void handleEvent(Event event) {
int count = resultTable.getItemCount();
// the most naive implementation
resultTable.setRedraw(false);
for (int i = 0; i < count; i++) {
if (resultTable.isSelected(i))
resultTable.deselect(i);
else
resultTable.select(i);
}
resultTable.setRedraw(true);
resultTable.redraw();
event.widget = resultTable;
resultTable.notifyListeners(SWT.Selection, event);
resultTable.forceFocus();
}
}
}