-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathDesignToolbar.java
More file actions
678 lines (620 loc) · 27.5 KB
/
Copy pathDesignToolbar.java
File metadata and controls
678 lines (620 loc) · 27.5 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
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
// -*- 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 com.google.appinventor.client;
import static com.google.appinventor.client.Ode.MESSAGES;
import com.google.appinventor.client.editor.FileEditor;
import com.google.appinventor.client.editor.ProjectEditor;
import com.google.appinventor.client.editor.youngandroid.BlocklyPanel;
import com.google.appinventor.client.explorer.commands.AddFormCommand;
import com.google.appinventor.client.explorer.commands.BuildCommand;
import com.google.appinventor.client.explorer.commands.ChainableCommand;
import com.google.appinventor.client.explorer.commands.CopyYoungAndroidProjectCommand;
import com.google.appinventor.client.explorer.commands.DeleteFileCommand;
import com.google.appinventor.client.explorer.commands.DownloadProjectOutputCommand;
import com.google.appinventor.client.explorer.commands.GenerateYailCommand;
import com.google.appinventor.client.explorer.commands.SaveAllEditorsCommand;
import com.google.appinventor.client.explorer.commands.ShowBarcodeCommand;
import com.google.appinventor.client.explorer.commands.ShowProgressBarCommand;
import com.google.appinventor.client.explorer.commands.WaitForBuildResultCommand;
import com.google.appinventor.client.output.OdeLog;
import com.google.appinventor.client.tracking.Tracking;
import com.google.appinventor.client.widgets.Toolbar;
import com.google.appinventor.common.version.AppInventorFeatures;
import com.google.appinventor.shared.rpc.project.ProjectRootNode;
import com.google.appinventor.shared.rpc.project.youngandroid.YoungAndroidProjectNode;
import com.google.appinventor.shared.rpc.project.youngandroid.YoungAndroidSourceNode;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import java.util.List;
import java.util.LinkedList;
import java.util.Map;
/**
* The design toolbar houses command buttons in the Young Android Design
* tab (for the UI designer (a.k.a, Form Editor) and Blocks Editor).
*
*/
public class DesignToolbar extends Toolbar {
/*
* A Screen groups together the form editor and blocks editor for an
* application screen. Name is the name of the screen (form) displayed
* in the screens pull-down.
*/
private static class Screen {
private final String screenName;
private final FileEditor formEditor;
private final FileEditor blocksEditor;
public Screen(String name, FileEditor formEditor, FileEditor blocksEditor) {
this.screenName = name;
this.formEditor = formEditor;
this.blocksEditor = blocksEditor;
}
}
/*
* A project as represented in the DesignToolbar. Each project has a name
* (as displayed in the DesignToolbar on the left), a set of named screens,
* and an indication of which screen is currently being edited.
*/
private static class DesignProject {
private final String name;
private final Map<String, Screen> screens; // screen name -> Screen
private String currentScreen; // name of currently displayed screen
public DesignProject(String name, long projectId) {
this.name = name;
screens = Maps.newHashMap();
// Screen1 is initial screen by default
currentScreen = YoungAndroidSourceNode.SCREEN1_FORM_NAME;
// Let BlocklyPanel know which screen to send Yail for
BlocklyPanel.setCurrentForm(projectId + "_" + currentScreen);
}
// Returns true if we added the screen (it didn't previously exist), false otherwise.
public boolean addScreen(String name, FileEditor formEditor, FileEditor blocksEditor) {
if (!screens.containsKey(name)) {
screens.put(name, new Screen(name, formEditor, blocksEditor));
return true;
} else {
return false;
}
}
public void removeScreen(String name) {
screens.remove(name);
}
public void setCurrentScreen(String name) {
currentScreen = name;
}
}
private static final String WIDGET_NAME_SAVE = "Save";
private static final String WIDGET_NAME_SAVE_AS = "SaveAs";
private static final String WIDGET_NAME_CHECKPOINT = "Checkpoint";
private static final String WIDGET_NAME_ADDFORM = "AddForm";
private static final String WIDGET_NAME_REMOVEFORM = "RemoveForm";
private static final String WIDGET_NAME_BUILD = "Build";
private static final String WIDGET_NAME_BUILD_BARCODE = "Barcode";
private static final String WIDGET_NAME_BUILD_DOWNLOAD = "Download";
private static final String WIDGET_NAME_BUILD_YAIL = "Yail";
private static final String WIDGET_NAME_SCREENS_DROPDOWN = "ScreensDropdown";
private static final String WIDGET_NAME_SWITCH_TO_BLOCKS_EDITOR = "SwitchToBlocksEditor";
private static final String WIDGET_NAME_SWITCH_TO_FORM_EDITOR = "SwitchToFormEditor";
private static final String WIDGET_NAME_CONNECT_TO = "ConnectTo";
private static final String WIDGET_NAME_WIRELESS_BUTTON = "Wireless";
private static final String WIDGET_NAME_EMULATOR_BUTTON = "Emulator";
// Enum for type of view showing in the design tab
public enum View {
FORM, // Form editor view
BLOCKS // Blocks editor view
}
private View currentView = View.FORM;
private Label projectNameLabel;
// Project currently displayed in designer
private DesignProject currentProject;
// Map of project id to project info for all projects we've ever shown
// in the Designer in this session.
private Map<Long, DesignProject> projectMap = Maps.newHashMap();
// Whether or not the we are talking to the repl
private boolean replStarted = false;
// Stack of screens switched to from the Companion
// We implement screen switching in the Companion by having it tell us
// to switch screens. We then load into the companion the new Screen
// We save where we were because the companion can have us return from
// a screen. If we switch projects in the browser UI, we clear this
// list of screens as we are effectively running a different application
// on the device.
private static LinkedList<String> pushedScreens = Lists.newLinkedList();
/**
* Initializes and assembles all commands into buttons in the toolbar.
*/
public DesignToolbar() {
super();
projectNameLabel = new Label();
projectNameLabel.setStyleName("ya-ProjectName");
HorizontalPanel toolbar = (HorizontalPanel) getWidget();
toolbar.insert(projectNameLabel, 0);
toolbar.setCellWidth(projectNameLabel, "222px"); // width of palette minus
// cellspacing/border of buttons
addButton(new ToolbarItem(WIDGET_NAME_SAVE, MESSAGES.saveButton(),
new SaveAction()));
addButton(new ToolbarItem(WIDGET_NAME_SAVE_AS, MESSAGES.saveAsButton(),
new SaveAsAction()));
addButton(new ToolbarItem(WIDGET_NAME_CHECKPOINT, MESSAGES.checkpointButton(),
new CheckpointAction()));
if (AppInventorFeatures.allowMultiScreenApplications()) {
addButton(new ToolbarItem(WIDGET_NAME_ADDFORM, MESSAGES.addFormButton(),
new AddFormAction()));
addButton(new ToolbarItem(WIDGET_NAME_REMOVEFORM, MESSAGES.removeFormButton(),
new RemoveFormAction()));
}
List<ToolbarItem> connectToItems = Lists.newArrayList();
addDropDownButton(WIDGET_NAME_CONNECT_TO, MESSAGES.connectToButton(), connectToItems, true);
updateConnectToDropDownButton(false, false);
List<ToolbarItem> screenItems = Lists.newArrayList();
addDropDownButton(WIDGET_NAME_SCREENS_DROPDOWN, MESSAGES.screensButton(), screenItems, true);
addButton(new ToolbarItem(WIDGET_NAME_SWITCH_TO_FORM_EDITOR,
MESSAGES.switchToFormEditorButton(), new SwitchToFormEditorAction()), true);
addButton(new ToolbarItem(WIDGET_NAME_SWITCH_TO_BLOCKS_EDITOR,
MESSAGES.switchToBlocksEditorButton(), new SwitchToBlocksEditorAction()), true);
List<ToolbarItem> buildItems = Lists.newArrayList();
buildItems.add(new ToolbarItem(WIDGET_NAME_BUILD_BARCODE,
MESSAGES.showBarcodeButton(), new BarcodeAction()));
buildItems.add(new ToolbarItem(WIDGET_NAME_BUILD_DOWNLOAD,
MESSAGES.downloadToComputerButton(), new DownloadAction()));
if (AppInventorFeatures.hasYailGenerationOption() && Ode.getInstance().getUser().getIsAdmin()) {
buildItems.add(new ToolbarItem(WIDGET_NAME_BUILD_YAIL,
MESSAGES.generateYailButton(), new GenerateYailAction()));
}
addDropDownButton(WIDGET_NAME_BUILD, MESSAGES.buildButton(), buildItems, true);
toggleEditor(false); // Gray out the Designer button and enable the blocks button
}
private class SaveAction implements Command {
@Override
public void execute() {
ProjectRootNode projectRootNode = Ode.getInstance().getCurrentYoungAndroidProjectRootNode();
if (projectRootNode != null) {
ChainableCommand cmd = new SaveAllEditorsCommand(null);
cmd.startExecuteChain(Tracking.PROJECT_ACTION_SAVE_YA, projectRootNode);
}
}
}
private class SaveAsAction implements Command {
@Override
public void execute() {
ProjectRootNode projectRootNode = Ode.getInstance().getCurrentYoungAndroidProjectRootNode();
if (projectRootNode != null) {
ChainableCommand cmd = new SaveAllEditorsCommand(
new CopyYoungAndroidProjectCommand(false));
cmd.startExecuteChain(Tracking.PROJECT_ACTION_SAVE_AS_YA, projectRootNode);
}
}
}
private class CheckpointAction implements Command {
@Override
public void execute() {
ProjectRootNode projectRootNode = Ode.getInstance().getCurrentYoungAndroidProjectRootNode();
if (projectRootNode != null) {
ChainableCommand cmd = new SaveAllEditorsCommand(
new CopyYoungAndroidProjectCommand(true));
cmd.startExecuteChain(Tracking.PROJECT_ACTION_CHECKPOINT_YA, projectRootNode);
}
}
}
private class AddFormAction implements Command {
@Override
public void execute() {
ProjectRootNode projectRootNode = Ode.getInstance().getCurrentYoungAndroidProjectRootNode();
if (projectRootNode != null) {
ChainableCommand cmd = new AddFormCommand();
cmd.startExecuteChain(Tracking.PROJECT_ACTION_ADDFORM_YA, projectRootNode);
}
}
}
private class RemoveFormAction implements Command {
@Override
public void execute() {
YoungAndroidSourceNode sourceNode = Ode.getInstance().getCurrentYoungAndroidSourceNode();
if (sourceNode != null && !sourceNode.isScreen1()) {
// DeleteFileCommand handles the whole operation, including displaying the confirmation
// message dialog, closing the form editor and the blocks editor,
// deleting the files in the server's storage, and deleting the
// corresponding client-side nodes (which will ultimately trigger the
// screen deletion in the DesignToolbar).
final String deleteConfirmationMessage = MESSAGES.reallyDeleteForm(
sourceNode.getFormName());
ChainableCommand cmd = new DeleteFileCommand() {
@Override
protected boolean deleteConfirmation() {
return Window.confirm(deleteConfirmationMessage);
}
};
cmd.startExecuteChain(Tracking.PROJECT_ACTION_REMOVEFORM_YA, sourceNode);
}
}
}
private class SwitchScreenAction implements Command {
private final long projectId;
private final String name; // screen name
public SwitchScreenAction(long projectId, String screenName) {
this.projectId = projectId;
this.name = screenName;
}
@Override
public void execute() {
doSwitchScreen(projectId, name, currentView);
}
}
private void doSwitchScreen(long projectId, String screenName, View view) {
if (!projectMap.containsKey(projectId)) {
OdeLog.wlog("DesignToolbar: no project with id " + projectId
+ ". Ignoring SwitchScreenAction.execute().");
return;
}
DesignProject project = projectMap.get(projectId);
if (currentProject != project) {
// need to switch projects first. this will not switch screens.
if (!switchToProject(projectId, project.name)) {
return;
}
// currentProject == project now
}
String newScreenName = screenName;
if (!currentProject.screens.containsKey(newScreenName)) {
// Can't find the requested screen in this project. This shouldn't happen, but if it does
// for some reason, try switching to Screen1 instead.
OdeLog.wlog("Trying to switch to non-existent screen " + newScreenName +
" in project " + currentProject.name + ". Trying Screen1 instead.");
if (currentProject.screens.containsKey(YoungAndroidSourceNode.SCREEN1_FORM_NAME)) {
newScreenName = YoungAndroidSourceNode.SCREEN1_FORM_NAME;
} else {
// something went seriously wrong!
ErrorReporter.reportError("Something is wrong. Can't find Screen1 for project "
+ currentProject.name);
return;
}
}
currentView = view;
Screen screen = currentProject.screens.get(newScreenName);
ProjectEditor projectEditor = screen.formEditor.getProjectEditor();
currentProject.setCurrentScreen(newScreenName);
setDropDownButtonCaption(WIDGET_NAME_SCREENS_DROPDOWN, newScreenName);
OdeLog.log("Setting currentScreen to " + newScreenName);
if (currentView == View.FORM) {
projectEditor.selectFileEditor(screen.formEditor);
toggleEditor(false);
} else { // must be View.BLOCKS
projectEditor.selectFileEditor(screen.blocksEditor);
toggleEditor(true);
}
// Inform the Blockly Panel which project/screen (aka form) we are working on
BlocklyPanel.setCurrentForm(projectId + "_" + newScreenName);
updateButtons();
}
/**
* Start wireless connection to the AI Companion
* @author logan
*
*/
private class WirelessAction implements Command {
@Override
public void execute() {
if (currentProject == null) {
OdeLog.wlog("DesignToolbar.currentProject is null. "
+ "Ignoring WirelessAction.execute().");
return;
}
startRepl(currentProject.currentScreen, false); // false means we are *not* the emulator
}
}
private class EmulatorAction implements Command {
@Override
public void execute() {
if (currentProject == null) {
OdeLog.wlog("DesignToolbar.currentProject is null. "
+ "Ignoring WirelessAction.execute().");
return;
}
startRepl(currentProject.currentScreen, true); // true means we are the emulator
}
}
private class BarcodeAction implements Command {
@Override
public void execute() {
ProjectRootNode projectRootNode = Ode.getInstance().getCurrentYoungAndroidProjectRootNode();
if (projectRootNode != null) {
String target = YoungAndroidProjectNode.YOUNG_ANDROID_TARGET_ANDROID;
ChainableCommand cmd = new SaveAllEditorsCommand(
new GenerateYailCommand(
new BuildCommand(target,
new ShowProgressBarCommand(target,
new WaitForBuildResultCommand(target,
new ShowBarcodeCommand(target)), "BarcodeAction"))));
updateBuildButton(true);
cmd.startExecuteChain(Tracking.PROJECT_ACTION_BUILD_BARCODE_YA, projectRootNode,
new Command() {
@Override
public void execute() {
updateBuildButton(false);
}
});
}
}
}
private class DownloadAction implements Command {
@Override
public void execute() {
ProjectRootNode projectRootNode = Ode.getInstance().getCurrentYoungAndroidProjectRootNode();
if (projectRootNode != null) {
String target = YoungAndroidProjectNode.YOUNG_ANDROID_TARGET_ANDROID;
ChainableCommand cmd = new SaveAllEditorsCommand(
new GenerateYailCommand(
new BuildCommand(target,
new ShowProgressBarCommand(target,
new WaitForBuildResultCommand(target,
new DownloadProjectOutputCommand(target)), "DownloadAction"))));
updateBuildButton(true);
cmd.startExecuteChain(Tracking.PROJECT_ACTION_BUILD_DOWNLOAD_YA, projectRootNode,
new Command() {
@Override
public void execute() {
updateBuildButton(false);
}
});
}
}
}
/**
* Implements the action to generate the ".yail" file for each screen in the current project.
* It does not build the entire project. The intention is that this will be helpful for
* debugging during development, and will most likely be disabled in the production system.
*/
private class GenerateYailAction implements Command {
@Override
public void execute() {
ProjectRootNode projectRootNode = Ode.getInstance().getCurrentYoungAndroidProjectRootNode();
if (projectRootNode != null) {
String target = YoungAndroidProjectNode.YOUNG_ANDROID_TARGET_ANDROID;
ChainableCommand cmd = new SaveAllEditorsCommand(new GenerateYailCommand(null));
updateBuildButton(true);
cmd.startExecuteChain(Tracking.PROJECT_ACTION_BUILD_YAIL_YA, projectRootNode,
new Command() {
@Override
public void execute() {
updateBuildButton(false);
}
});
}
}
}
private class SwitchToBlocksEditorAction implements Command {
@Override
public void execute() {
if (currentProject == null) {
OdeLog.wlog("DesignToolbar.currentProject is null. "
+ "Ignoring SwitchToBlocksEditorAction.execute().");
return;
}
if (currentView != View.BLOCKS) {
long projectId = Ode.getInstance().getCurrentYoungAndroidProjectRootNode().getProjectId();
switchToScreen(projectId, currentProject.currentScreen, View.BLOCKS);
toggleEditor(true); // Gray out the blocks button and enable the designer button
}
}
}
private class SwitchToFormEditorAction implements Command {
@Override
public void execute() {
if (currentProject == null) {
OdeLog.wlog("DesignToolbar.currentProject is null. "
+ "Ignoring SwitchToFormEditorAction.execute().");
return;
}
if (currentView != View.FORM) {
long projectId = Ode.getInstance().getCurrentYoungAndroidProjectRootNode().getProjectId();
switchToScreen(projectId, currentProject.currentScreen, View.FORM);
toggleEditor(false); // Gray out the Designer button and enable the blocks button
}
}
}
public void addProject(long projectId, String projectName) {
if (!projectMap.containsKey(projectId)) {
projectMap.put(projectId, new DesignProject(projectName, projectId));
OdeLog.log("DesignToolbar added project " + projectName + " with id " + projectId);
} else {
OdeLog.wlog("DesignToolbar ignoring addProject for existing project " + projectName
+ " with id " + projectId);
}
}
// Switch to an existing project. Note that this does not switch screens.
// TODO(sharon): it might be better to throw an exception if the
// project doesn't exist.
private boolean switchToProject(long projectId, String projectName) {
if (projectMap.containsKey(projectId)) {
DesignProject project = projectMap.get(projectId);
if (project == currentProject) {
OdeLog.wlog("DesignToolbar: ignoring call to switchToProject for current project");
return true;
}
pushedScreens.clear(); // Effectively switching applications clear stack of screens
clearDropDownMenu(WIDGET_NAME_SCREENS_DROPDOWN);
OdeLog.log("DesignToolbar: switching to existing project " + projectName + " with id "
+ projectId);
currentProject = projectMap.get(projectId);
// TODO(sharon): add screens to drop-down menu in the right order
for (Screen screen : currentProject.screens.values()) {
addDropDownButtonItem(WIDGET_NAME_SCREENS_DROPDOWN, new ToolbarItem(screen.screenName,
screen.screenName, new SwitchScreenAction(projectId, screen.screenName)));
}
projectNameLabel.setText(projectName);
} else {
ErrorReporter.reportError("Design toolbar doesn't know about project " + projectName +
" with id " + projectId);
OdeLog.wlog("Design toolbar doesn't know about project " + projectName + " with id "
+ projectId);
return false;
}
return true;
}
/*
* Add a screen name to the drop-down for the project with id projectId.
* name is the form name, formEditor is the file editor for the form UI,
* and blocksEditor is the file editor for the form's blocks.
*/
public void addScreen(long projectId, String name, FileEditor formEditor,
FileEditor blocksEditor) {
if (!projectMap.containsKey(projectId)) {
OdeLog.wlog("DesignToolbar can't find project " + name + " with id " + projectId
+ ". Ignoring addScreen().");
return;
}
DesignProject project = projectMap.get(projectId);
if (project.addScreen(name, formEditor, blocksEditor)) {
if (currentProject == project) {
addDropDownButtonItem(WIDGET_NAME_SCREENS_DROPDOWN, new ToolbarItem(name,
name, new SwitchScreenAction(projectId, name)));
}
}
}
/*
* PushScreen -- Static method called by Blockly when the Companion requests
* That we switch to a new screen. We keep track of the Screen we were on
* and push that onto a stack of Screens which we pop when requested by the
* Companion.
*/
public static boolean pushScreen(String screenName) {
DesignToolbar designToolbar = Ode.getInstance().getDesignToolbar();
long projectId = Ode.getInstance().getCurrentYoungAndroidProjectId();
String currentScreen = designToolbar.currentProject.currentScreen;
if (!designToolbar.currentProject.screens.containsKey(screenName)) // No such screen -- can happen
return false; // because screen is user entered here.
pushedScreens.addFirst(currentScreen);
designToolbar.doSwitchScreen(projectId, screenName, View.BLOCKS);
return true;
}
public static void popScreen() {
DesignToolbar designToolbar = Ode.getInstance().getDesignToolbar();
long projectId = Ode.getInstance().getCurrentYoungAndroidProjectId();
String newScreen;
if (pushedScreens.isEmpty()) {
return; // Nothing to do really
}
newScreen = pushedScreens.removeFirst();
designToolbar.doSwitchScreen(projectId, newScreen, View.BLOCKS);
}
// Called from Javascript when Companion is disconnected
public static void clearScreens() {
pushedScreens.clear();
}
/*
* Switch to screen name in project projectId. Also switches projects if
* necessary.
*/
public void switchToScreen(long projectId, String screenName, View view) {
doSwitchScreen(projectId, screenName, view);
}
/*
* Remove screen name (if it exists) from project projectId
*/
public void removeScreen(long projectId, String name) {
if (!projectMap.containsKey(projectId)) {
OdeLog.wlog("DesignToolbar can't find project " + name + " with id " + projectId
+ " Ignoring removeScreen().");
return;
}
OdeLog.log("DesignToolbar: got removeScreen for project " + projectId
+ ", screen " + name);
DesignProject project = projectMap.get(projectId);
if (!project.screens.containsKey(name)) {
// already removed this screen
return;
}
if (currentProject == project) {
// if removing current screen, choose a new screen to show
if (currentProject.currentScreen.equals(name)) {
// TODO(sharon): maybe make a better choice than screen1, but for now
// switch to screen1 because we know it is always there
switchToScreen(projectId, YoungAndroidSourceNode.SCREEN1_FORM_NAME, View.FORM);
}
removeDropDownButtonItem(WIDGET_NAME_SCREENS_DROPDOWN, name);
}
project.removeScreen(name);
}
/**
* Enables and/or disables buttons based (mostly) on whether there is a
* current form.
*/
private void updateButtons() {
String screenName = (currentProject == null) ? null : currentProject.currentScreen;
boolean enabled = (currentProject != null);
setButtonEnabled(WIDGET_NAME_SAVE, enabled);
setButtonEnabled(WIDGET_NAME_SAVE_AS, enabled);
setButtonEnabled(WIDGET_NAME_CHECKPOINT, enabled);
setDropItemEnabled(WIDGET_NAME_BUILD, WIDGET_NAME_BUILD_BARCODE, enabled);
setDropItemEnabled(WIDGET_NAME_BUILD, WIDGET_NAME_BUILD_DOWNLOAD, enabled);
if (AppInventorFeatures.allowMultiScreenApplications()) {
setButtonEnabled(WIDGET_NAME_ADDFORM, enabled);
enabled = (currentProject != null &&
!YoungAndroidSourceNode.SCREEN1_FORM_NAME.equals(screenName));
setButtonEnabled(WIDGET_NAME_REMOVEFORM, enabled);
}
if (currentProject != null) {
setDropDownButtonCaption(WIDGET_NAME_SCREENS_DROPDOWN, screenName);
} else {
setDropDownButtonCaption(WIDGET_NAME_SCREENS_DROPDOWN, MESSAGES.screensButton());
}
}
/**
* Shows feedback on Package for Phone (Build) button while building.
*/
private void updateBuildButton(boolean isBuilding) {
setDropDownButtonEnabled(WIDGET_NAME_BUILD, !isBuilding);
setDropDownButtonCaption(WIDGET_NAME_BUILD,
isBuilding ? MESSAGES.isBuildingButton() : MESSAGES.buildButton());
}
private void startRepl(String screenName, boolean forEmulator) {
Screen screen = currentProject.screens.get(screenName);
screen.blocksEditor.startRepl(replStarted, forEmulator);
if (!replStarted) {
replStarted = true;
if (forEmulator) { // We are starting the emulator...
updateConnectToDropDownButton(true, false);
} else {
updateConnectToDropDownButton(false, true);
}
} else {
replStarted = false;
updateConnectToDropDownButton(false, false);
}
}
/**
* Indicate that we are no longer connected to the Companion, adjust
* buttons accordingly. Called from BlocklyPanel
*/
public static void indicateDisconnect() {
DesignToolbar instance = Ode.getInstance().getDesignToolbar();
instance.updateConnectToDropDownButton(false, false);
instance.replStarted = false; // This is ugly, I should really define a method to do this
// but that would just take space and time...
}
private void updateConnectToDropDownButton(boolean isEmulatorRunning, boolean isCompanionRunning){
clearDropDownMenu(WIDGET_NAME_CONNECT_TO);
if (!isEmulatorRunning && !isCompanionRunning) {
addDropDownButtonItem(WIDGET_NAME_CONNECT_TO, new ToolbarItem(WIDGET_NAME_WIRELESS_BUTTON,
MESSAGES.wirelessButton(), new WirelessAction()));
addDropDownButtonItem(WIDGET_NAME_CONNECT_TO, new ToolbarItem(WIDGET_NAME_EMULATOR_BUTTON,
MESSAGES.emulatorButton(), new EmulatorAction()));
} else if (isEmulatorRunning) {
addDropDownButtonItem(WIDGET_NAME_CONNECT_TO, new ToolbarItem(WIDGET_NAME_EMULATOR_BUTTON,
MESSAGES.emulatorButtonConnected(), new EmulatorAction()));
} else {
addDropDownButtonItem(WIDGET_NAME_CONNECT_TO, new ToolbarItem(WIDGET_NAME_WIRELESS_BUTTON,
MESSAGES.wirelessButtonConnected(), new WirelessAction()));
}
}
private void toggleEditor(boolean blocks) {
setButtonEnabled(WIDGET_NAME_SWITCH_TO_BLOCKS_EDITOR, !blocks);
setButtonEnabled(WIDGET_NAME_SWITCH_TO_FORM_EDITOR, blocks);
setDropDownButtonVisible(WIDGET_NAME_CONNECT_TO, blocks);
}
}