-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathBlockMenu.java
More file actions
150 lines (128 loc) · 5.28 KB
/
Copy pathBlockMenu.java
File metadata and controls
150 lines (128 loc) · 5.28 KB
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
// -*- mode: java; c-basic-offset: 2; -*-
// Copyright 2009-2011 Google, All Rights reserved
// Copyright 2011-2012 MIT, All rights reserved
// Released under the MIT License https://raw.github.com/mit-cml/app-inventor/master/mitlicense.txt
package openblocks.renderable;
import openblocks.codeblocks.Block;
import openblocks.workspace.Workspace;
import openblocks.workspace.WorkspaceEvent;
import openblocks.yacodeblocks.PhoneCommManager;
import openblocks.yacodeblocks.WorkspaceControllerHolder;
import java.awt.MenuItem;
import java.awt.PopupMenu;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
/**
* BlockMenu performs operations for a block, currently in response to right clicks
*/
public class BlockMenu implements ActionListener {
private static final String ADD_COMMENT = "Add Comment";
private static final String REMOVE_COMMENT = "Remove Comment";
private static final String DEACTIVATE = "Deactivate";
private static final String REMOVE_COMPLAINT = "Remove Complaint";
private static final String ACTIVATE = "Activate";
private static final String DO_IT = "Do It";
private static final String REMOVE_REPORT = "Stop Watching";
private static final String REPORT_EXECUTIONS = "Watch";
private final PopupMenu popupMenu;
private final MenuItem commentItem;
private final MenuItem complaintItem;
private final MenuItem reportItem;
private final MenuItem ableItem;
private final MenuItem doItItem;
private final RenderableBlock renderable;
/**
* Constructs a new BlockMenu instance with the specified parent
* WorkspaceWidget and
*
* @param renderable block
*/
BlockMenu(RenderableBlock renderable){
this.renderable = renderable;
Block block = renderable.getBlock();
popupMenu = new PopupMenu();
commentItem = new MenuItem(ADD_COMMENT);
commentItem.addActionListener(this);
popupMenu.add(commentItem);
ableItem = new MenuItem(DEACTIVATE);
ableItem.addActionListener(this);
if (!renderable.getBlock().hasPlug()) {
popupMenu.add(ableItem);
}
reportItem = new MenuItem(REPORT_EXECUTIONS);
reportItem.setActionCommand(REPORT_EXECUTIONS);
reportItem.addActionListener(this);
if (!(block.isProcedureDeclBlock() || block.isEventHandlerBlock())) {
popupMenu.add(reportItem);
}
complaintItem = new MenuItem(REMOVE_COMPLAINT);
complaintItem.setActionCommand(REMOVE_COMPLAINT);
complaintItem.addActionListener(this);
popupMenu.add(complaintItem);
doItItem = new MenuItem(DO_IT);
doItItem.setActionCommand(DO_IT);
doItItem.addActionListener(this);
if (!(block.isDeclaration() || block.isArgument())) {
popupMenu.add(doItItem);
}
renderable.add(popupMenu);
}
void displayMenu(MouseEvent e) {
PhoneCommManager rcm = WorkspaceControllerHolder.get().getPhoneCommManager();
if (renderable.hasComment()) {
commentItem.setLabel(REMOVE_COMMENT);
commentItem.setActionCommand(REMOVE_COMMENT);
} else {
commentItem.setLabel(ADD_COMMENT);
commentItem.setActionCommand(ADD_COMMENT);
}
if (renderable.activated()) {
ableItem.setLabel(DEACTIVATE);
ableItem.setActionCommand(DEACTIVATE);
} else {
ableItem.setLabel(ACTIVATE);
ableItem.setActionCommand(ACTIVATE);
}
complaintItem.setEnabled(renderable.hasComplaint());
if (renderable.getBlock().shouldReceiveReport()) {
reportItem.setLabel(REMOVE_REPORT);
reportItem.setActionCommand(REMOVE_REPORT);
} else {
reportItem.setLabel(REPORT_EXECUTIONS);
reportItem.setActionCommand(REPORT_EXECUTIONS);
}
doItItem.setEnabled(rcm.connectedToPhone());
popupMenu.show(renderable, e.getX(), e.getY());
}
public void actionPerformed(ActionEvent a) {
if (a.getActionCommand() == ADD_COMMENT){
Comment comment = renderable.getComment();
comment.getBlockNoteLabel().setActive(true);
comment.setVisible(true);
comment.requestFocusInWindow();
} else if (a.getActionCommand() == REMOVE_COMMENT){
renderable.removeComment();
} else if (a.getActionCommand() == REMOVE_COMPLAINT){
renderable.removeComplaint();
} else if (a.getActionCommand() == REMOVE_REPORT){
renderable.getBlock().setShouldReceiveReport(false);
Workspace.getInstance().notifyListeners(new WorkspaceEvent(renderable.getParentWidget(),
renderable.getBlockID(), WorkspaceEvent.BLOCK_REPORT_CHANGE));
renderable.removeReport();
} else if (a.getActionCommand() == REPORT_EXECUTIONS){
renderable.getBlock().setShouldReceiveReport(true);
renderable.getReport().getBlockNoteLabel().setActive(false);
Workspace.getInstance().notifyListeners(new WorkspaceEvent(renderable.getParentWidget(),
renderable.getBlockID(), WorkspaceEvent.BLOCK_REPORT_CHANGE));
// This will trigger re-compilation and transmission to phone for enclosing definition.
} else if (a.getActionCommand() == DEACTIVATE){
renderable.setActivate(false);
} else if (a.getActionCommand() == ACTIVATE){
renderable.setActivate(true);
} else if (a.getActionCommand() == DO_IT) {
Workspace.getInstance().notifyListeners(new WorkspaceEvent(renderable.getParentWidget(),
renderable.getBlockID(), WorkspaceEvent.BLOCK_DO_IT));
}
}
}