-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathOde.java
More file actions
1017 lines (890 loc) · 33.8 KB
/
Copy pathOde.java
File metadata and controls
1017 lines (890 loc) · 33.8 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
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// -*- 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.boxes.AssetListBox;
import com.google.appinventor.client.boxes.BlockSelectorBox;
import com.google.appinventor.client.boxes.MessagesOutputBox;
import com.google.appinventor.client.boxes.OdeLogBox;
import com.google.appinventor.client.boxes.PaletteBox;
import com.google.appinventor.client.boxes.ProjectListBox;
import com.google.appinventor.client.boxes.PropertiesBox;
import com.google.appinventor.client.boxes.SourceStructureBox;
import com.google.appinventor.client.boxes.ViewerBox;
import com.google.appinventor.client.editor.EditorManager;
import com.google.appinventor.client.editor.FileEditor;
import com.google.appinventor.client.editor.youngandroid.BlocklyPanel;
import com.google.appinventor.client.explorer.commands.CommandRegistry;
import com.google.appinventor.client.explorer.project.Project;
import com.google.appinventor.client.explorer.project.ProjectChangeAdapter;
import com.google.appinventor.client.explorer.project.ProjectManager;
import com.google.appinventor.client.explorer.project.ProjectManagerEventAdapter;
import com.google.appinventor.client.explorer.youngandroid.ProjectToolbar;
import com.google.appinventor.client.jsonp.JsonpConnection;
import com.google.appinventor.client.output.OdeLog;
import com.google.appinventor.client.settings.user.UserSettings;
import com.google.appinventor.client.tracking.Tracking;
import com.google.appinventor.client.widgets.boxes.Box;
import com.google.appinventor.client.widgets.boxes.ColumnLayout;
import com.google.appinventor.client.widgets.boxes.ColumnLayout.Column;
import com.google.appinventor.client.widgets.boxes.WorkAreaPanel;
import com.google.appinventor.common.version.AppInventorFeatures;
import com.google.appinventor.shared.rpc.GetMotdService;
import com.google.appinventor.shared.rpc.GetMotdServiceAsync;
import com.google.appinventor.shared.rpc.ServerLayout;
import com.google.appinventor.shared.rpc.help.HelpService;
import com.google.appinventor.shared.rpc.help.HelpServiceAsync;
import com.google.appinventor.shared.rpc.launch.LaunchService;
import com.google.appinventor.shared.rpc.launch.LaunchServiceAsync;
import com.google.appinventor.shared.rpc.project.FileNode;
import com.google.appinventor.shared.rpc.project.ProjectRootNode;
import com.google.appinventor.shared.rpc.project.ProjectService;
import com.google.appinventor.shared.rpc.project.ProjectServiceAsync;
import com.google.appinventor.shared.rpc.project.youngandroid.YoungAndroidSourceNode;
import com.google.appinventor.shared.rpc.user.User;
import com.google.appinventor.shared.rpc.user.UserInfoService;
import com.google.appinventor.shared.rpc.user.UserInfoServiceAsync;
import com.google.appinventor.shared.settings.SettingsConstants;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.logical.shared.ResizeEvent;
import com.google.gwt.event.logical.shared.ResizeHandler;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.http.client.Response;
import com.google.gwt.resources.client.ImageResource;
import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.DeferredCommand;
import com.google.gwt.user.client.Event;
import com.google.gwt.user.client.History;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.rpc.StatusCodeException;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.DeckPanel;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.DockPanel;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.PushButton;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
/**
* Main entry point for Ode. Defines the startup UI elements in
* {@link #onModuleLoad()}.
*
*/
public class Ode implements EntryPoint {
// I18n messages
public static final OdeMessages MESSAGES = GWT.create(OdeMessages.class);
/**
* The base URL for App Inventor documentation.
*/
public static final String APP_INVENTOR_DOCS_URL = "";
// Global instance of the Ode object
private static Ode instance;
// Application level image bundle
private static final Images IMAGES = GWT.create(Images.class);
// ProjectEditor registry
private static final ProjectEditorRegistry EDITORS = new ProjectEditorRegistry();
// Command registry
private static final CommandRegistry COMMANDS = new CommandRegistry();
// User settings
private static UserSettings userSettings;
private MotdFetcher motdFetcher;
// User information
private User user;
// Collection of projects
private ProjectManager projectManager;
// Collection of editors
private EditorManager editorManager;
// Currently active file editor, could be a YaFormEditor or a YaBlocksEditor or null.
private FileEditor currentFileEditor;
private AssetManager assetManager;
// Remembers the current View
private static final int DESIGNER = 0;
private static final int PROJECTS = 1;
private static int currentView = DESIGNER;
/*
* The following fields define the general layout of the UI as seen in the following diagram:
*
* +-- mainPanel --------------------------------+
* |+-- topPanel -------------------------------+|
* || ||
* |+-------------------------------------------+|
* |+-- deckPanel -------------------------------+|
* || ||
* |+-------------------------------------------+|
* |+-- statusPanel ----------------------------+|
* || ||
* |+-------------------------------------------+|
* +---------------------------------------------+
*/
private DeckPanel deckPanel;
private int projectsTabIndex;
private int designTabIndex;
private int debuggingTabIndex;
private TopPanel topPanel;
private StatusPanel statusPanel;
private HorizontalPanel workColumns;
private VerticalPanel structureAndAssets;
private ProjectToolbar projectToolbar;
private DesignToolbar designToolbar;
// Popup that indicates that an asynchronous request is pending. It is visible
// initially, and will be hidden automatically after the first RPC completes.
private static RpcStatusPopup rpcStatusPopup;
// Web service for help information
private final HelpServiceAsync helpService = GWT.create(HelpService.class);
// Web service for project related information
private final ProjectServiceAsync projectService = GWT.create(ProjectService.class);
// Web service for user related information
private final UserInfoServiceAsync userInfoService = GWT.create(UserInfoService.class);
// Web service for launch related services
private final LaunchServiceAsync launchService = GWT.create(LaunchService.class);
// Web service for get motd information
private final GetMotdServiceAsync getMotdService = GWT.create(GetMotdService.class);
private boolean windowClosing;
/**
* Returns global instance of Ode.
*
* @return global Ode instance
*/
public static Ode getInstance() {
return instance;
}
/**
* Returns instance of the aggregate image bundle for the application.
*
* @return image bundle
*/
public static Images getImageBundle() {
return IMAGES;
}
/**
* Returns the editor registry.
*
* @return the editor registry
*/
public static ProjectEditorRegistry getProjectEditorRegistry() {
return EDITORS;
}
/**
* Returns the command registry.
*
* @return the command registry
*/
public static CommandRegistry getCommandRegistry() {
return COMMANDS;
}
/**
* Returns the user settings.
*
* @return user settings
*/
public static UserSettings getUserSettings() {
return userSettings;
}
/**
* Returns the asset manager.
*
* @return asset manager
*/
public AssetManager getAssetManager() {
return assetManager;
}
/**
* Returns true if we have received the window closing event.
*/
public static boolean isWindowClosing() {
return getInstance().windowClosing;
}
/**
* Switch to the Projects tab
*/
public void switchToProjectsView() {
currentView = PROJECTS;
deckPanel.showWidget(projectsTabIndex);
}
/**
* Switch to the Designer tab. Shows an error message if there is no currentFileEditor.
*/
public void switchToDesignView() {
// Only show designer if there is a current editor.
// ***** THE DESIGNER TAB DOES NOT DISPLAY CORRECTLY IF THERE IS NO CURRENT EDITOR. *****
currentView = DESIGNER;
if (currentFileEditor != null) {
deckPanel.showWidget(designTabIndex);
} else {
OdeLog.wlog("No current file editor to show in designer");
ErrorReporter.reportInfo(MESSAGES.chooseProject());
}
}
/**
* Switch to the Debugging tab
*/
public void switchToDebuggingView() {
deckPanel.showWidget(debuggingTabIndex);
// NOTE(lizlooney) - Calling resizeWorkArea for debuggingTab prevents the
// boxes from overlapping each other.
resizeWorkArea((WorkAreaPanel) deckPanel.getWidget(debuggingTabIndex));
}
public void openPreviousProject() {
if (userSettings == null) {
OdeLog.wlog("Ignoring openPreviousProject() since userSettings is null");
return;
}
OdeLog.log("Ode.openPreviousProject called");
String value = userSettings.getSettings(SettingsConstants.USER_GENERAL_SETTINGS).
getPropertyValue(SettingsConstants.GENERAL_SETTINGS_CURRENT_PROJECT_ID);
openProject(value);
}
private void openProject(String projectIdString) {
OdeLog.log("Ode.openProject called for " + projectIdString);
if (projectIdString.equals("")) {
openPreviousProject();
} else if (!projectIdString.equals("0")) {
final long projectId = Long.parseLong(projectIdString);
Project project = projectManager.getProject(projectId);
if (project != null) {
openYoungAndroidProjectInDesigner(project);
} else {
// The project hasn't been added to the ProjectManager yet.
// Add a ProjectManagerEventListener so we'll be notified when it has been added.
// Alternatively, it is an invalid projectId. In which case,
// nothing happens since if the listener eventually fires
// it will not match the projectId.
projectManager.addProjectManagerEventListener(new ProjectManagerEventAdapter() {
@Override
public void onProjectAdded(Project project) {
if (project.getProjectId() == projectId) {
projectManager.removeProjectManagerEventListener(this);
openYoungAndroidProjectInDesigner(project);
}
}
});
}
}
// else projectIdString == 0; do nothing
}
public void openYoungAndroidProjectInDesigner(final Project project) {
ProjectRootNode projectRootNode = project.getRootNode();
if (projectRootNode == null) {
// The project nodes haven't been loaded yet.
// Add a ProjectChangeListener so we'll be notified when they have been loaded.
project.addProjectChangeListener(new ProjectChangeAdapter() {
@Override
public void onProjectLoaded(Project projectLoaded) {
project.removeProjectChangeListener(this);
openYoungAndroidProjectInDesigner(project);
}
});
project.loadProjectNodes();
} else {
// The project nodes have been loaded. Tell the viewer to open
// the project. This will cause the projects source files to be fetched
// asynchronously, and loaded into file editors.
ViewerBox.getViewerBox().show(projectRootNode);
// Note: we can't call switchToDesignView until the Screen1 file editor
// finishes loading. We leave that to setCurrentFileEditor(), which
// will get called at the appropriate time.
String projectIdString = Long.toString(project.getProjectId());
if (!History.getToken().equals(projectIdString)) {
// insert token into history but do not trigger listener event
History.newItem(projectIdString, false);
}
if (assetManager == null) {
assetManager = AssetManager.getInstance();
}
assetManager.loadAssets(project.getProjectId());
}
}
/**
* Returns i18n compatible messages
* @return messages
*/
public static OdeMessages getMessages() {
return MESSAGES;
}
/**
* Returns the rpcStatusPopup object.
* @return RpcStatusPopup
*/
public static RpcStatusPopup getRpcStatusPopup() {
return rpcStatusPopup;
}
/**
* Main entry point for Ode. Setting up the UI and the web service
* connections.
*/
@Override
public void onModuleLoad() {
Tracking.trackPageview();
// Handler for any otherwise unhandled exceptions
GWT.setUncaughtExceptionHandler(new GWT.UncaughtExceptionHandler() {
@Override
public void onUncaughtException(Throwable e) {
OdeLog.xlog(e);
if (AppInventorFeatures.sendBugReports()) {
if (Window.confirm(MESSAGES.internalErrorReportBug())) {
Window.open(BugReport.getBugReportLink(e), "_blank", "");
}
} else {
// Display a confirm dialog with error msg and if 'ok' open the debugging view
if (Window.confirm(MESSAGES.internalErrorClickOkDebuggingView())) {
Ode.getInstance().switchToDebuggingView();
}
}
}
});
// Define bridge methods to Javascript
JsonpConnection.defineBridgeMethod();
// Initialize global Ode instance
instance = this;
// Get user information.
OdeAsyncCallback<User> callback = new OdeAsyncCallback<User>(
// failure message
MESSAGES.serverUnavailable()) {
@Override
public void onSuccess(User result) {
// If user hasn't accepted terms of service, ask them to.
if (!result.getUserTosAccepted()) {
// We expect that the redirect to the TOS page should be handled
// by the onFailure method below. The server should return a
// "forbidden" error if the TOS wasn't accepted.
ErrorReporter.reportError(MESSAGES.serverUnavailable());
return;
}
user = result;
userSettings = new UserSettings(user);
// Here we call userSettings.loadSettings, but the settings are actually loaded
// asynchronously, so this loadSettings call will return before they are loaded.
// After the user settings have been loaded, openPreviousProject will be called.
userSettings.loadSettings();
// Initialize project and editor managers
projectManager = new ProjectManager();
editorManager = new EditorManager();
// Initialize UI
initializeUi();
topPanel.showUserEmail(user.getUserEmail());
}
@Override
public void onFailure(Throwable caught) {
if (caught instanceof StatusCodeException) {
StatusCodeException e = (StatusCodeException) caught;
int statusCode = e.getStatusCode();
switch (statusCode) {
case Response.SC_UNAUTHORIZED:
// unauthorized => not on whitelist
// getEncodedResponse() gives us the message that we wrote in
// OdeAuthFilter.writeWhitelistErrorMessage().
Window.alert(e.getEncodedResponse());
return;
case Response.SC_FORBIDDEN:
// forbidden => need tos accept
Window.open("/" + ServerLayout.YA_TOS_FORM, "_self", null);
return;
}
}
super.onFailure(caught);
}
};
// TODO(user): ODE makes too many RPC requests at startup time. Currently
// we do 3 RPCs + 1 per project + 1 per open file. We should bundle some of
// those with each other or with the initial HTML transfer.
userInfoService.getUserInformation(callback);
History.addValueChangeHandler(new ValueChangeHandler<String>() {
@Override
public void onValueChange(ValueChangeEvent<String> event) {
openProject(event.getValue());
}
});
// load project based on current url
// TODO(sharon): Seems like a possible race condition here if the onValueChange
// handler defined above gets called before the getUserInformation call sets
// userSettings.
// The following line causes problems with GWT debugging, and commenting
// it out doesn't seem to break things.
//History.fireCurrentHistoryState();
}
/*
* Initializes all UI elements.
*/
private void initializeUi() {
BlocklyPanel.initUi();
rpcStatusPopup = new RpcStatusPopup();
// Register services with RPC status popup
rpcStatusPopup.register((ExtendedServiceProxy<?>) helpService);
rpcStatusPopup.register((ExtendedServiceProxy<?>) projectService);
rpcStatusPopup.register((ExtendedServiceProxy<?>) userInfoService);
Window.setTitle(MESSAGES.titleYoungAndroid());
Window.enableScrolling(true);
topPanel = new TopPanel();
statusPanel = new StatusPanel();
DockPanel mainPanel = new DockPanel();
mainPanel.add(topPanel, DockPanel.NORTH);
// Create tab panel for subsequent tabs
deckPanel = new DeckPanel() {
@Override
public final void onBrowserEvent(Event event) {
switch (event.getTypeInt()) {
case Event.ONCONTEXTMENU:
event.preventDefault();
break;
}
}
};
deckPanel.sinkEvents(Event.ONCONTEXTMENU);
deckPanel.setStyleName("ode-DeckPanel");
// Projects tab
VerticalPanel pVertPanel = new VerticalPanel();
pVertPanel.setWidth("100%");
pVertPanel.setSpacing(0);
HorizontalPanel projectListPanel = new HorizontalPanel();
projectListPanel.setWidth("100%");
projectListPanel.add(ProjectListBox.getProjectListBox());
projectToolbar = new ProjectToolbar();
pVertPanel.add(projectToolbar);
pVertPanel.add(projectListPanel);
projectsTabIndex = deckPanel.getWidgetCount();
deckPanel.add(pVertPanel);
// Design tab
VerticalPanel dVertPanel = new VerticalPanel();
dVertPanel.setWidth("100%");
designToolbar = new DesignToolbar();
dVertPanel.add(designToolbar);
workColumns = new HorizontalPanel();
workColumns.setWidth("100%");
Box palletebox = PaletteBox.getPaletteBox();
palletebox.setWidth("225px");
workColumns.add(palletebox);
Box viewerbox = ViewerBox.getViewerBox();
workColumns.add(viewerbox);
workColumns.setCellWidth(viewerbox, "97%");
structureAndAssets = new VerticalPanel();
structureAndAssets.setVerticalAlignment(VerticalPanel.ALIGN_TOP);
// Only one of the SourceStructureBox and the BlockSelectorBox is visible
// at any given time, according to whether we are showing the form editor
// or the blocks editor. They share the same screen real estate.
structureAndAssets.add(SourceStructureBox.getSourceStructureBox());
structureAndAssets.add(BlockSelectorBox.getBlockSelectorBox()); // initially not visible
structureAndAssets.add(AssetListBox.getAssetListBox());
workColumns.add(structureAndAssets);
Box propertiesbox = PropertiesBox.getPropertiesBox();
propertiesbox.setWidth("210px");
workColumns.add(propertiesbox);
dVertPanel.add(workColumns);
designTabIndex = deckPanel.getWidgetCount();
deckPanel.add(dVertPanel);
// Debugging tab
if (AppInventorFeatures.hasDebuggingView()) {
Button dismissButton = new Button(MESSAGES.dismissButton());
dismissButton.addClickHandler(new ClickHandler() {
@Override
public void onClick(ClickEvent event) {
if (currentView == DESIGNER)
switchToDesignView();
else
switchToProjectsView();
}
});
ColumnLayout defaultLayout = new ColumnLayout("Default");
Column column = defaultLayout.addColumn(100);
column.add(MessagesOutputBox.class, 300, false);
column.add(OdeLogBox.class, 300, false);
final WorkAreaPanel debuggingTab = new WorkAreaPanel(new OdeBoxRegistry(), defaultLayout);
debuggingTab.add(dismissButton);
debuggingTabIndex = deckPanel.getWidgetCount();
deckPanel.add(debuggingTab);
// Hook the window resize event, so that we can adjust the UI.
Window.addResizeHandler(new ResizeHandler() {
@Override
public void onResize(ResizeEvent event) {
resizeWorkArea(debuggingTab);
}
});
// Call the window resized handler to get the initial sizes setup. Doing this in a deferred
// command causes it to occur after all widgets' sizes have been computed by the browser.
DeferredCommand.addCommand(new Command() {
@Override
public void execute() {
resizeWorkArea(debuggingTab);
}
});
resizeWorkArea(debuggingTab);
}
// We do not select the designer tab here because at this point there is no current project.
// Instead, we select the projects tab. If the user has a previously opened project, we will
// open it and switch to the designer after the user settings are loaded.
// Remember, the user may not have any projects at all yet.
// Or, the user may have deleted their previously opened project.
// ***** THE DESIGNER TAB DOES NOT DISPLAY CORRECTLY IF THERE IS NO CURRENT PROJECT. *****
deckPanel.showWidget(projectsTabIndex);
mainPanel.add(deckPanel, DockPanel.CENTER);
mainPanel.setCellHeight(deckPanel, "100%");
mainPanel.setCellWidth(deckPanel, "100%");
//Commenting out for now to gain more space for the blocks editor
//mainPanel.add(statusPanel, DockPanel.SOUTH);
mainPanel.setSize("100%", "100%");
RootPanel.get().add(mainPanel);
// There is no sure-fire way of preventing people from accidentally navigating away from ODE
// (e.g. by hitting the Backspace key). What we do need though is to make sure that people will
// not lose any work because of this. We hook into the window closing event to detect the
// situation.
Window.addWindowClosingHandler(new Window.ClosingHandler() {
@Override
public void onWindowClosing(Window.ClosingEvent event) {
onClosing();
}
});
if (AppInventorFeatures.showSplashScreen()) {
createWelcomeDialog(true);
} else {
getProjectService().getProjects(new AsyncCallback<long[]>() {
@Override
public void onSuccess(long [] projectIds) {
if (projectIds.length == 0) {
createNoProjectsDialog(true);
}
}
@Override
public void onFailure(Throwable projectIds) {
OdeLog.elog("Could not get project list");
}
});
}
setupMotd();
}
private void setupMotd() {
AsyncCallback<Integer> callback = new AsyncCallback<Integer>() {
@Override
public void onFailure(Throwable caught) {
OdeLog.log(MESSAGES.getMotdFailed());
}
@Override
public void onSuccess(Integer intervalSecs) {
if (intervalSecs > 0) {
topPanel.showMotd();
motdFetcher = new MotdFetcher(intervalSecs);
motdFetcher.register((ExtendedServiceProxy<?>) projectService);
motdFetcher.register((ExtendedServiceProxy<?>) userInfoService);
}
}
};
getGetMotdService().getCheckInterval(callback);
}
/**
* Returns the editor manager.
*
* @return {@link EditorManager}
*/
public EditorManager getEditorManager() {
return editorManager;
}
/**
* Returns the project manager.
*
* @return {@link ProjectManager}
*/
public ProjectManager getProjectManager() {
return projectManager;
}
/**
* Returns the project tool bar.
*
* @return {@link ProjectToolbar}
*/
public ProjectToolbar getProjectToolbar() {
return projectToolbar;
}
/**
* Returns the structureAndAssets panel.
*
* @return {@link VerticalPanel}
*/
public VerticalPanel getStructureAndAssets() {
return structureAndAssets;
}
/**
* Returns the workColumns panel.
*
* @return {@link HorizontalPanel}
*/
public HorizontalPanel getWorkColumns() {
return workColumns;
}
/**
* Returns the design tool bar.
*
* @return {@link DesignToolbar}
*/
public DesignToolbar getDesignToolbar() {
return designToolbar;
}
/**
* Get an instance of the project information web service.
*
* @return project web service instance
*/
public ProjectServiceAsync getProjectService() {
return projectService;
}
/**
* Get an instance of the user information web service.
*
* @return user information web service instance
*/
public UserInfoServiceAsync getUserInfoService() {
return userInfoService;
}
/**
* Get an instance of the motd web service.
*
* @return motd web service instance
*/
public GetMotdServiceAsync getGetMotdService() {
return getMotdService;
}
/**
* Get an instance of the help web service.
*
* @return help service instance
*/
public HelpServiceAsync getHelpService() {
return helpService;
}
/**
* Get an instance of the launch RPC service.
*
* @return launch service instance
*/
public LaunchServiceAsync getLaunchService() {
return launchService;
}
/**
* Set the current file editor.
*
* @param fileEditor the file editor, can be null.
*/
public void setCurrentFileEditor(FileEditor fileEditor) {
currentFileEditor = fileEditor;
if (currentFileEditor == null) {
// nothing more we can do
OdeLog.log("Setting current file editor to null");
return;
}
OdeLog.log("Ode: Setting current file editor to " + currentFileEditor.getFileId());
switchToDesignView();
if (!windowClosing) {
userSettings.getSettings(SettingsConstants.USER_GENERAL_SETTINGS).
changePropertyValue(SettingsConstants.GENERAL_SETTINGS_CURRENT_PROJECT_ID,
"" + getCurrentYoungAndroidProjectId());
userSettings.saveSettings(null);
}
}
/**
* @return currently open FileEditor, or null if none
*/
public FileEditor getCurrentFileEditor() {
return currentFileEditor;
}
/**
* Returns the project root node for the current project, or null if there is no current project.
*
* @return project root node corresponding to current project
*/
public ProjectRootNode getCurrentYoungAndroidProjectRootNode() {
if (currentFileEditor != null) {
return currentFileEditor.getProjectRootNode();
}
return null;
}
/**
* Updates the modification date for the requested projected in the local
* cached data structure based on the date received from the server.
* @param date the date to update it to
*/
public void updateModificationDate(long projectId, long date) {
Project project = getProjectManager().getProject(projectId);
if (project != null) {
project.setDateModified(date);
}
}
/**
* Returns the current project id, or 0 if there is no current project.
*
* @return the current project id
*/
public long getCurrentYoungAndroidProjectId() {
if (currentFileEditor != null) {
return currentFileEditor.getProjectId();
}
return 0;
}
/**
* Returns the current source node, or null if there is no current source node.
*
* @return the current source node
*/
public YoungAndroidSourceNode getCurrentYoungAndroidSourceNode() {
if (currentFileEditor != null) {
FileNode fileNode = currentFileEditor.getFileNode();
if (fileNode instanceof YoungAndroidSourceNode) {
return (YoungAndroidSourceNode) fileNode;
}
}
return null;
}
/**
* Returns user account information.
*
* @return user account information
*/
public User getUser() {
return user;
}
/**
* Helper method to create push buttons.
*
* @param img image to shown on face of push button
* @param tip text to show in tooltip
* @return newly created push button
*/
public static PushButton createPushButton(ImageResource img, String tip,
ClickHandler handler) {
PushButton pb = new PushButton(new Image(img));
pb.addClickHandler(handler);
pb.setTitle(tip);
return pb;
}
private void resizeWorkArea(WorkAreaPanel workArea) {
// Subtract 16px from width to account for vertical scrollbar FF3 likes to add
workArea.onResize(Window.getClientWidth() - 16, Window.getClientHeight());
}
private void onClosing() {
// At this point, we aren't allowed to do any UI.
windowClosing = true;
if (motdFetcher != null) {
motdFetcher.unregister((ExtendedServiceProxy<?>) projectService);
motdFetcher.unregister((ExtendedServiceProxy<?>) userInfoService);
}
// Unregister services with RPC status popup.
rpcStatusPopup.unregister((ExtendedServiceProxy<?>) helpService);
rpcStatusPopup.unregister((ExtendedServiceProxy<?>) projectService);
rpcStatusPopup.unregister((ExtendedServiceProxy<?>) userInfoService);
// Save the user settings.
userSettings.saveSettings(null);
// Save all unsaved editors.
editorManager.saveDirtyEditors(null);
}
/**
* Creates, visually centers, and optionally displays the dialog box
* that informs the user how to start learning about using App Inventor
* or create a new project.
* @param showDialog Convenience variable to show the created DialogBox.
* @return The created and optionally displayed Dialog box.
*/
public DialogBox createNoProjectsDialog(boolean showDialog) {
// Create the UI elements of the DialogBox
final DialogBox dialogBox = new DialogBox(true);
dialogBox.setStylePrimaryName("ode-DialogBox");
dialogBox.setText("Welcome to App Inventor!");
Grid mainGrid = new Grid(2, 2);
mainGrid.getCellFormatter().setAlignment(0,
0,
HasHorizontalAlignment.ALIGN_CENTER,
HasVerticalAlignment.ALIGN_MIDDLE);
mainGrid.getCellFormatter().setAlignment(0,
1,
HasHorizontalAlignment.ALIGN_CENTER,
HasVerticalAlignment.ALIGN_MIDDLE);
mainGrid.getCellFormatter().setAlignment(1,
1,
HasHorizontalAlignment.ALIGN_RIGHT,
HasVerticalAlignment.ALIGN_MIDDLE);
Image dialogImage = new Image(Ode.getImageBundle().androidGreenSmall());
Grid messageGrid = new Grid(2, 1);
messageGrid.getCellFormatter().setAlignment(0,
0,
HasHorizontalAlignment.ALIGN_JUSTIFY,
HasVerticalAlignment.ALIGN_MIDDLE);
messageGrid.getCellFormatter().setAlignment(1,
0,
HasHorizontalAlignment.ALIGN_LEFT,
HasVerticalAlignment.ALIGN_MIDDLE);
Label messageChunk1 = new Label("You don't have any projects yet."
+ " To learn how to use App Inventor, click the \"Learn\" item"
+ " at the top of the window; or to start your first project, click "
+ " the \"New\" button at the upper left of the window.");
messageChunk1.setWidth("23em");
Label messageChunk2 = new Label("Happy Inventing!");
// Add the elements to the grids and DialogBox.
messageGrid.setWidget(0, 0, messageChunk1);
messageGrid.setWidget(1, 0, messageChunk2);
mainGrid.setWidget(0, 0, dialogImage);
mainGrid.setWidget(0, 1, messageGrid);
dialogBox.setWidget(mainGrid);
dialogBox.center();
if (showDialog) {
dialogBox.show();
}
return dialogBox;
}
/**
* Creates, visually centers, and optionally displays the dialog box
* that informs the user how to start learning about using App Inventor
* or create a new project.
* @param showDialog Convenience variable to show the created DialogBox.
* @return The created and optionally displayed Dialog box.
*/
public DialogBox createWelcomeDialog(boolean showDialog) {
// Create the UI elements of the DialogBox
final DialogBox dialogBox = new DialogBox(false, true); // DialogBox(autohide, modal)
dialogBox.setStylePrimaryName("ode-DialogBox");
dialogBox.setText("Welcome to App Inventor!");
dialogBox.setHeight("400px");
dialogBox.setWidth("400px");
dialogBox.setGlassEnabled(true);
dialogBox.setAnimationEnabled(true);
dialogBox.center();
VerticalPanel DialogBoxContents = new VerticalPanel();
HTML message = new HTML("<h2>This is the Splash Screen. Make this an iframe to your splash screen.</h2>");
message.setStyleName("DialogBox-message");
SimplePanel holder = new SimplePanel();
Button ok = new Button("Continue");
ok.addClickListener(new ClickListener() {
public void onClick(Widget sender) {
dialogBox.hide();
getProjectService().getProjects(new AsyncCallback<long[]>() {
@Override
public void onSuccess(long [] projectIds) {
if (projectIds.length == 0) {
createNoProjectsDialog(true);
}
}