-
-
Notifications
You must be signed in to change notification settings - Fork 2k
/
MockForm.java
1490 lines (1320 loc) · 59 KB
/
MockForm.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
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-2017 MIT, All rights reserved
// Released under the Apache License, Version 2.0
// http://www.apache.org/licenses/LICENSE-2.0
package com.google.appinventor.client.editor.simple.components;
import static com.google.appinventor.client.Ode.MESSAGES;
import com.google.appinventor.client.editor.simple.SimpleEditor;
import com.google.appinventor.client.editor.simple.components.utils.PropertiesUtil;
import com.google.appinventor.client.editor.youngandroid.YaFormEditor;
import com.google.appinventor.client.editor.youngandroid.properties.YoungAndroidLengthPropertyEditor;
import com.google.appinventor.client.editor.youngandroid.properties.YoungAndroidVerticalAlignmentChoicePropertyEditor;
import com.google.appinventor.client.output.OdeLog;
import com.google.appinventor.client.properties.BadPropertyEditorException;
import com.google.appinventor.client.widgets.properties.EditableProperties;
import com.google.appinventor.components.common.ComponentConstants;
import com.google.appinventor.components.common.PropertyTypeConstants;
import com.google.appinventor.shared.settings.SettingsConstants;
import com.google.gwt.dom.client.DivElement;
import com.google.gwt.dom.client.Document;
import com.google.gwt.dom.client.Element;
import com.google.gwt.dom.client.NativeEvent;
import com.google.gwt.dom.client.Style;
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.AbsolutePanel;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DockPanel;
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.ScrollPanel;
import com.google.gwt.user.client.ui.TreeItem;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
/**
* Mock Form component. This implementation provides two main preview sizes corresponding to
* 'normal' and 'large' buckets (http://developer.android.com/guide/practices/screens_support.html).
* Normal size is a 1:1 with pixels on a device with dpi:160. We use that as the baseline for the
* browser too. All UI elements should be scaled to DP for buckets other than 'normal'.
*/
public final class MockForm extends MockContainer {
/*
* Widget for the mock form title bar.
*/
private class TitleBar extends Composite {
private static final int TITLEBAR_HEIGHT = 24;
private static final int ACTIONBAR_HEIGHT = 56;
private static final int NAVBAR_HEIGHT = 44;
// UI elements
private Label title;
private Button menuButton;
private AbsolutePanel bar;
private Image bookIconWhite;
private Image bookIconBlack;
private boolean actionBar;
private boolean navBar;
private String backgroundColor;
public String getTitle() {
return title.getText();
}
/*
* Creates a new title bar.
*/
TitleBar() {
title = new Label();
title.setStylePrimaryName("ode-SimpleMockFormTitle");
menuButton = new Button();
menuButton.setText("\u22ee");
menuButton.setStylePrimaryName("ode-SimpleMockFormMenuButton");
bookIconWhite = new Image(images.bookIconWhite());
bookIconWhite.setStylePrimaryName("ode-SimpleMockFormIconIOSWhite");
bookIconBlack = new Image(images.bookIconBlack());
bookIconBlack.setStylePrimaryName("ode-SimpleMockFormIconIOSBlack");
bar = new AbsolutePanel();
bar.add(title);
bar.add(menuButton);
initWidget(bar);
setStylePrimaryName("ode-SimpleMockFormTitleBar");
setSize("100%", TITLEBAR_HEIGHT + "px");
}
/*
* Changes the title in the title bar.
*/
void changeTitle(String newTitle) {
title.setText(newTitle);
}
void setActionBar(boolean actionBar , boolean navBar) {
this.actionBar = actionBar;
this.navBar = navBar;
setSize("100%", (navBar ? NAVBAR_HEIGHT : (actionBar ? ACTIONBAR_HEIGHT : TITLEBAR_HEIGHT)) + "px");
if (actionBar) {
if (navBar) {
removeStyleDependentName("ActionBar");
addStyleDependentName("NavBar");
} else {
removeStyleDependentName("NavBar");
addStyleDependentName("ActionBar");
}
MockComponentsUtil.setWidgetBackgroundColor(titleBar.bar, backgroundColor);
} else {
removeStyleDependentName("ActionBar");
MockComponentsUtil.setWidgetBackgroundColor(titleBar.bar, "&HFF696969");
}
}
void changeBookmarkIcon(boolean black) {
bar.remove(black ? bookIconWhite : bookIconBlack);
bar.add(black ? bookIconBlack : bookIconWhite);
}
void setBackgroundColor(String color) {
this.backgroundColor = color;
if (actionBar || navBar) {
MockComponentsUtil.setWidgetBackgroundColor(titleBar.bar, color);
}
}
int getHeight() {
return navBar ? NAVBAR_HEIGHT : (actionBar ? ACTIONBAR_HEIGHT : TITLEBAR_HEIGHT);
}
}
/*
* Widget for a mock phone status bar.
*/
private class PhoneBar extends Composite {
private static final int HEIGHT = 24;
private static final int IPAD_HEIGHT = 20;
private static final int IPHONEX_HEIGHT = 44;
// UI elements
private DockPanel bar;
private HorizontalPanel phoneBarLeftPanel;
private HorizontalPanel phoneBarRightPanel;
private HorizontalPanel iPadPhoneBarLeftPanel;
private HorizontalPanel iPadPhoneBarRightPanel;
private Image blackIconsLeft;
private Image blackIconsRight;
private Image whiteIconsLeft;
private Image whiteIconsRight;
private Image iPadWhiteIconsLeft;
private Image iPadWhiteIconsRight;
private Image iPadBlackIconsLeft;
private Image iPadBlackIconsRight;
private boolean visible;
/*
* Creates a new phone status bar.
*/
PhoneBar() {
Image phoneBarImage = new Image(images.phonebar());
bar = new DockPanel();
bar.setHorizontalAlignment(HorizontalPanel.ALIGN_RIGHT);
bar.add(phoneBarImage, DockPanel.EAST);
initWidget(bar);
setStylePrimaryName("ode-SimpleMockFormPhoneBarAndroidHolo");
setSize("100%", HEIGHT + "px");
}
PhoneBar(String color) {
Image phoneBarAndroidMaterial = new Image(images.phonebarAndroidMaterial());
bar = new DockPanel();
bar.setHorizontalAlignment(HorizontalPanel.ALIGN_RIGHT);
bar.add(phoneBarAndroidMaterial, DockPanel.EAST);
initWidget(bar);
MockComponentsUtil.setWidgetBackgroundColor(bar, color);
setStylePrimaryName("ode-SimpleMockFormPhoneBarAndroidMaterial");
setSize("100%", HEIGHT + "px");
}
PhoneBar(boolean blackIcons, int size, String color) {
// icons for iPhone
blackIconsLeft = new Image(images.phonebariPhoneLeftBlack());
blackIconsRight = new Image(images.phonebariPhoneRightBlack());
whiteIconsLeft = new Image(images.phonebariPhoneLeftWhite());
whiteIconsRight = new Image(images.phonebariPhoneRightWhite());
phoneBarLeftPanel = new HorizontalPanel();
phoneBarLeftPanel.setStylePrimaryName("ode-SimpleMockFormLeft");
phoneBarRightPanel = new HorizontalPanel();
phoneBarRightPanel.setStylePrimaryName("ode-SimpleMockFormRight");
//icons for iPad
iPadWhiteIconsLeft = new Image(images.phonebariPadLeftWhite());
iPadWhiteIconsRight = new Image(images.phonebariPadRightWhite());
iPadBlackIconsLeft = new Image(images.phonebariPadLeftBlack());
iPadBlackIconsRight = new Image(images.phonebariPadRightBlack());
iPadPhoneBarLeftPanel = new HorizontalPanel();
iPadPhoneBarLeftPanel.setStylePrimaryName("ode-SimpleMockFormLeftIPad");
iPadPhoneBarRightPanel = new HorizontalPanel();
iPadPhoneBarRightPanel.setStylePrimaryName("ode-SimpleMockFormRightIPad");
bar = new DockPanel();
setIconColor(blackIcons, size);
bar.add(phoneBarLeftPanel, DockPanel.WEST);
bar.add(phoneBarRightPanel, DockPanel.EAST);
bar.add(iPadPhoneBarLeftPanel, DockPanel.WEST);
bar.add(iPadPhoneBarRightPanel, DockPanel.EAST);
initWidget(bar);
MockComponentsUtil.setWidgetBackgroundColor(bar, color);
setStylePrimaryName("ode-SimpleMockFormPhoneBariOS");
setSize(size);
}
// changing Icons in Device Default and Black Title Text
void setIconColor(boolean blackIcons, int size) {
if (size == 0) {
bar.removeStyleDependentName("iPad");
bar.addStyleDependentName("iPhone");
phoneBarLeftPanel.remove(blackIcons ? whiteIconsLeft : blackIconsLeft);
phoneBarRightPanel.remove(blackIcons ? whiteIconsRight : blackIconsRight);
phoneBarLeftPanel.add(blackIcons ? blackIconsLeft : whiteIconsLeft);
phoneBarRightPanel.add(blackIcons ? blackIconsRight : whiteIconsRight);
} else {
bar.removeStyleDependentName("iPhone");
bar.addStyleDependentName("iPad");
iPadPhoneBarLeftPanel.remove(blackIcons? iPadWhiteIconsLeft : iPadBlackIconsLeft);
iPadPhoneBarRightPanel.remove(blackIcons ? iPadWhiteIconsRight : iPadBlackIconsRight);
iPadPhoneBarLeftPanel.add(blackIcons ? iPadBlackIconsLeft : iPadWhiteIconsLeft);
iPadPhoneBarRightPanel.add(blackIcons? iPadBlackIconsRight : iPadWhiteIconsRight);
}
}
//set status bar size for the iOS theme
void setSize(int size) {
setSize("100%", (size == 0 ? IPHONEX_HEIGHT : IPAD_HEIGHT) + "px");
}
void setVisibility(boolean visible) {
this.visible = visible;
}
int getHeight() {
// Adjust for CSS borders, which are not included in the height value
return visible ? PhoneBar.HEIGHT + 3 : 0;
}
}
/*
*
* Widget for a mock phone navigation bar; Shows at the bottom of the viewer
*/
private class NavigationBar extends Composite {
private static final int HEIGHT = 44;
// UI elements
private AbsolutePanel bar;
/*
* Creates a new phone navigation bar; Shows at the bottom of the viewer.
*/
NavigationBar() {
bar = new AbsolutePanel();
initWidget(bar);
setStylePrimaryName("ode-SimpleMockFormNavigationBarPortrait");
}
public int getHeight() {
return HEIGHT;
}
}
/**
* Component type name.
*/
public static final String TYPE = "Form";
private static final String VISIBLE_TYPE = "Screen";
// Currently App Inventor provides two main sizes that correspond to 'normal' and 'large'
// screens. We use phone=normal (470 x 320 DP) and tablet=large (640 x 480 DP).
// More information about 'bucket' sizes at:
// http://developer.android.com/guide/practices/screens_support.html
// The values for Phone and Tablet were decided by trial and error. The main reason is that in
// the designer we use sizes of GWT widgets, and not the sizes of the actual Android widgets.
private static final int PHONE_PORTRAIT_WIDTH = 320;
private static final int PHONE_PORTRAIT_HEIGHT = 470 + 35; // Adds 35 for the navigation bar
private static final int PHONE_LANDSCAPE_WIDTH = PHONE_PORTRAIT_HEIGHT;
private static final int PHONE_LANDSCAPE_HEIGHT = PHONE_PORTRAIT_WIDTH;
private static final int TABLET_PORTRAIT_WIDTH = 480;
private static final int TABLET_PORTRAIT_HEIGHT = 640 + 35; // Adds 35 for the navigation bar
private static final int TABLET_LANDSCAPE_WIDTH = TABLET_PORTRAIT_HEIGHT;
private static final int TABLET_LANDSCAPE_HEIGHT = TABLET_PORTRAIT_WIDTH;
private static final int PHONE_PORTRAIT_WIDTH_iPHONE = 320;
private static final int PHONE_PORTRAIT_HEIGHT_iPHONE= 650;
private static final int PHONE_LANDSCAPE_WIDTH_iPHONE = PHONE_PORTRAIT_HEIGHT_iPHONE;
private static final int PHONE_LANDSCAPE_HEIGHT_iPHONE= PHONE_PORTRAIT_WIDTH_iPHONE;
// These are default values but they can be changed in the changePreviewSize method
private int PORTRAIT_WIDTH = PHONE_PORTRAIT_WIDTH;
private int PORTRAIT_HEIGHT = PHONE_PORTRAIT_HEIGHT;
private int LANDSCAPE_WIDTH = PHONE_LANDSCAPE_WIDTH;
private int LANDSCAPE_HEIGHT = PHONE_LANDSCAPE_HEIGHT;
private boolean landscape = false;
private int idxPhoneSize = 0;
//Default values for theme style
private boolean changePreviewFlag = false;
private boolean blackIcons = false ;
private int idxPhonePreviewStyle = -1;
private String primaryDarkColor="&HFF41521C";
private String primaryColor="&HFFA5CF47";
private boolean actionBar = false;
private boolean showStatusBar = true;
// Property names
private static final String PROPERTY_NAME_TITLE = "Title";
private static final String PROPERTY_NAME_SCREEN_ORIENTATION = "ScreenOrientation";
private static final String PROPERTY_NAME_SCROLLABLE = "Scrollable";
private static final String PROPERTY_NAME_ICON = "Icon";
private static final String PROPERTY_NAME_VCODE = "VersionCode";
private static final String PROPERTY_NAME_VNAME = "VersionName";
private static final String PROPERTY_NAME_ANAME = "AppName";
private static final String PROPERTY_NAME_SIZING = "Sizing"; // Don't show except on screen1
private static final String PROPERTY_NAME_TITLEVISIBLE = "TitleVisible";
private static final String PROPERTY_NAME_SHOW_STATUS_BAR = "ShowStatusBar";
// Don't show except on screen1
private static final String PROPERTY_NAME_SHOW_LISTS_AS_JSON = "ShowListsAsJson";
private static final String PROPERTY_NAME_TUTORIAL_URL = "TutorialURL";
private static final String PROPERTY_NAME_BLOCK_SUBSET = "BlocksToolkit";
private static final String PROPERTY_NAME_ACTIONBAR = "ActionBar";
private static final String PROPERTY_NAME_PRIMARY_COLOR = "PrimaryColor";
private static final String PROPERTY_NAME_PRIMARY_COLOR_DARK = "PrimaryColorDark";
private static final String PROPERTY_NAME_ACCENT_COLOR = "AccentColor";
private static final String PROPERTY_NAME_THEME = "Theme";
private static final String PROPERTY_NAME_DEFAULTFILESCOPE = "DefaultFileScope";
// Form UI components
AbsolutePanel formWidget;
AbsolutePanel phoneWidget;
AbsolutePanel responsivePanel;
AbsolutePanel notchPanel;
ScrollPanel scrollPanel;
private TitleBar titleBar;
private PhoneBar phoneBar;
private NavigationBar navigationBar;
private List<MockComponent> selectedComponents = new ArrayList<MockComponent>(Collections.singleton(this));
private MockContainer pasteTarget = this;
int screenWidth; // TEMP: Make package visible so we can use it MockHVLayoutBase
private int screenHeight;
int usableScreenHeight; // TEMP: Make package visible so we can use it MockHVLayoutBase
int usableScreenWidth;
// Set of listeners for any changes of the form
final HashSet<FormChangeListener> formChangeListeners = new HashSet<FormChangeListener>();
// Set of listeners for DesignPreviewChanges
final HashSet<DesignPreviewChangeListener> designPreviewChangeListeners = new HashSet<DesignPreviewChangeListener>();
// Don't access the verticalScrollbarWidth field directly. Use getVerticalScrollbarWidth().
private static int verticalScrollbarWidth;
private MockFormLayout myLayout;
// flag to control attempting to enable/disable vertical
// alignment when scrollable property is changed
private boolean initialized = false;
private YoungAndroidVerticalAlignmentChoicePropertyEditor myVAlignmentPropertyEditor;
public static final String PROPERTY_NAME_HORIZONTAL_ALIGNMENT = "AlignHorizontal";
public static final String PROPERTY_NAME_VERTICAL_ALIGNMENT = "AlignVertical";
/**
* Creates a new MockForm component.
*
* @param editor editor of source file the component belongs to
*/
public MockForm(SimpleEditor editor) {
// Note(Hal): This helper thing is a kludge because I really want to write:
// myLayout = new MockHVLayout(orientation);
// super(editor, type, icon, myLayout);
// but Java won't let me do that.
super(editor, TYPE, images.form(), MockFormHelper.makeLayout());
// Note(hal): There better not be any calls to MockFormHelper before the
// next instruction. Note that the Helper methods are synchronized to avoid possible
// future problems if we ever have threads creating forms in parallel.
myLayout = MockFormHelper.getLayout();
phoneWidget = new AbsolutePanel();
phoneWidget.setStylePrimaryName("ode-SimpleMockFormPhonePortrait");
formWidget = new AbsolutePanel();
formWidget.setStylePrimaryName("ode-SimpleMockForm");
notchPanel = new AbsolutePanel();
notchPanel.setStylePrimaryName("ode-SimpleMockFormNotch");
notchPanel.getElement().getStyle().clearPosition();
notchPanel.getElement().getStyle().clearOverflow();
responsivePanel = new AbsolutePanel();
// Initialize mock form UI by adding the phone bar and title bar.
changePreview();
responsivePanel.add(phoneBar);
titleBar = new TitleBar();
responsivePanel.add(titleBar);
// Put a ScrollPanel around the rootPanel.
scrollPanel = new ScrollPanel(rootPanel);
responsivePanel.add(scrollPanel);
formWidget.add(responsivePanel);
//Add navigation bar at the bottom of the viewer.
navigationBar = new NavigationBar();
formWidget.add(navigationBar);
phoneWidget.add(formWidget);
phoneWidget.add(notchPanel);
initComponent(phoneWidget);
// Set up the initial state of the vertical alignment property editor and its dropdowns
try {
myVAlignmentPropertyEditor = PropertiesUtil.getVAlignmentEditor(properties);
} catch (BadPropertyEditorException e) {
OdeLog.log(MESSAGES.badAlignmentPropertyEditorForArrangement());
return;
}
enableAndDisableDropdowns();
initialized = true;
// Now that the default for Scrollable is false, we need to force setting the property when creating the MockForm
setScrollableProperty(getPropertyValue(PROPERTY_NAME_SCROLLABLE));
}
public void changePreviewSize(int width, int height, int idx) {
// It will definitely be modified in the future to add more options.
PORTRAIT_WIDTH = width;
PORTRAIT_HEIGHT = height;
LANDSCAPE_WIDTH = height;
LANDSCAPE_HEIGHT = width;
idxPhoneSize = idx;
setPhoneStyle();
updateScreenSize();
}
private void updateScreenSize() {
if (landscape) {
if (idxPhoneSize == 0 && idxPhonePreviewStyle == 2) {
resizePanel(PHONE_LANDSCAPE_WIDTH_iPHONE, PHONE_LANDSCAPE_HEIGHT_iPHONE);
} else {
resizePanel(LANDSCAPE_WIDTH, LANDSCAPE_HEIGHT);
}
} else {
if (idxPhoneSize == 0 && idxPhonePreviewStyle == 2) {
resizePanel(PHONE_PORTRAIT_WIDTH_iPHONE, PHONE_PORTRAIT_HEIGHT_iPHONE);
} else {
resizePanel(PORTRAIT_WIDTH, PORTRAIT_HEIGHT);
}
}
}
public void changePhonePreview(int idx, String previewName ) {
// storing the new preview style in the user settings
editor.getProjectEditor().changeProjectSettingsProperty(
SettingsConstants.PROJECT_YOUNG_ANDROID_SETTINGS,
SettingsConstants.YOUNG_ANDROID_SETTINGS_PHONE_PREVIEW, previewName);
idxPhonePreviewStyle = idx;
changePreviewFlag = true;
changePreview();
setPhoneStyle();
updateScreenSize();
}
private void setPhoneStyle() {
if (landscape) {
if (idxPhoneSize == 0) phoneWidget.setStylePrimaryName("ode-SimpleMockFormPhoneLandscape");
else if (idxPhoneSize == 1) phoneWidget.setStylePrimaryName("ode-SimpleMockFormPhoneLandscapeTablet");
else if (idxPhoneSize == 2) phoneWidget.setStylePrimaryName("ode-SimpleMockFormPhoneLandscapeMonitor");
navigationBar.setStylePrimaryName("ode-SimpleMockFormNavigationBarLandscape");
} else {
if (idxPhoneSize == 0) phoneWidget.setStylePrimaryName("ode-SimpleMockFormPhonePortrait");
else if (idxPhoneSize == 1) phoneWidget.setStylePrimaryName("ode-SimpleMockFormPhonePortraitTablet");
else if (idxPhoneSize == 2) phoneWidget.setStylePrimaryName("ode-SimpleMockFormPhonePortraitMonitor");
navigationBar.setStylePrimaryName("ode-SimpleMockFormNavigationBarPortrait");
}
if (idxPhonePreviewStyle == 2) {
setIOSPhoneStyle();
}
fireDesignPreviewChange();
}
private static String[] iosSizesLandscape = {
"ode-SimpleMockFormIOSLandscape",
"ode-SimpleMockFormIOSLandscapeTablet",
"ode-SimpleMockFormIOSLandscapeMonitor"
};
private static String[] iosSizesPortrait = {
"ode-SimpleMockFormIOSPortrait",
"ode-SimpleMockFormIOSPortraitTablet",
"ode-SimpleMockFormIOSPortraitMonitor"
};
private void setIOSPhoneStyle() {
if (landscape) {
phoneWidget.setStylePrimaryName(iosSizesLandscape[idxPhoneSize]);
} else {
phoneWidget.setStylePrimaryName(iosSizesPortrait[idxPhoneSize]);
}
navigationBar.setStylePrimaryName("ode-SimpleMockFormNavigationBarIOS");
phoneBar.setIconColor(blackIcons, idxPhoneSize);
phoneBar.setSize(idxPhoneSize);
}
/*
* Resizes the scrollPanel, responsivePanel, and formWidget based on the screen size.
*/
private void resizePanel(int newWidth, int newHeight){
screenWidth = newWidth;
screenHeight = newHeight;
if (landscape) {
String val = editor.getProjectEditor().getProjectSettingsProperty(
SettingsConstants.PROJECT_YOUNG_ANDROID_SETTINGS,
SettingsConstants.YOUNG_ANDROID_SETTINGS_PHONE_PREVIEW);
if (val.equals("iOS")) {
usableScreenWidth = screenWidth;
} else {
usableScreenWidth = screenWidth - navigationBar.getHeight();
}
usableScreenHeight = screenHeight - phoneBar.getHeight() - titleBar.getHeight();
} else {
usableScreenWidth = screenWidth;
usableScreenHeight = screenHeight - phoneBar.getHeight() - titleBar.getHeight() - navigationBar.getHeight();
}
rootPanel.setPixelSize(usableScreenWidth, usableScreenHeight);
scrollPanel.setPixelSize(usableScreenWidth + getVerticalScrollbarWidth(), usableScreenHeight);
formWidget.setPixelSize(screenWidth + getVerticalScrollbarWidth(), screenHeight);
// Store properties
changeProperty(PROPERTY_NAME_WIDTH, "" + usableScreenWidth);
boolean scrollable = Boolean.parseBoolean(getPropertyValue(PROPERTY_NAME_SCROLLABLE));
if (!scrollable) {
changeProperty(PROPERTY_NAME_HEIGHT, "" + usableScreenHeight);
}
}
/*
* Changes the preview of MockForm based on Android Holo, Android Material and iOS styles
*/
private void changePreview() {
// this condition prevents adding multiple phoneBars and titleBars
if (changePreviewFlag) {
responsivePanel.remove(phoneBar);
responsivePanel.remove(titleBar);
}
if (idxPhonePreviewStyle == -1) {
phoneBar = new PhoneBar();
formWidget.removeStyleDependentName("AndroidMaterial");
formWidget.removeStyleDependentName("iOS");
formWidget.removeStyleDependentName("AndroidHolo");
} else if (idxPhonePreviewStyle == 0) {
phoneBar = new PhoneBar(primaryDarkColor);
formWidget.removeStyleDependentName("AndroidHolo");
formWidget.removeStyleDependentName("iOS");
formWidget.addStyleDependentName("AndroidMaterial");
} else if (idxPhonePreviewStyle == 1) {
phoneBar = new PhoneBar();
formWidget.removeStyleDependentName("AndroidMaterial");
formWidget.removeStyleDependentName("iOS");
formWidget.addStyleDependentName("AndroidHolo");
} else if (idxPhonePreviewStyle == 2) {
phoneBar = new PhoneBar(blackIcons, idxPhoneSize, primaryColor);
formWidget.removeStyleDependentName("AndroidMaterial");
formWidget.removeStyleDependentName("AndroidHolo");
formWidget.addStyleDependentName("iOS");
}
// updating changes to the MockForm
if (changePreviewFlag) {
formWidget.remove(responsivePanel);
formWidget.remove(navigationBar);
///////////////////////////////////////////////////////////////////
// Always need to add the phoneBar to the designer, then set the /
// visibility accordingly! /
///////////////////////////////////////////////////////////////////
responsivePanel.add(phoneBar);
phoneBar.setVisible(showStatusBar);
phoneBar.setVisibility(showStatusBar);
responsivePanel.add(titleBar);
responsivePanel.add(scrollPanel);
formWidget.add(responsivePanel);
formWidget.add(navigationBar);
if (idxPhonePreviewStyle != 2) {
titleBar.setActionBar(actionBar, false);
} else {
titleBar.setActionBar(true, true);
titleBar.changeBookmarkIcon(blackIcons);
}
}
changePreviewFlag = false;
}
/*
* Returns the width of a vertical scroll bar, calculating it if necessary.
*/
private static int getVerticalScrollbarWidth() {
// We only calculate the vertical scroll bar width once, then we store it in the static field
// verticalScrollbarWidth. If the field is non-zero, we don't need to calculate it again.
if (verticalScrollbarWidth == 0) {
// The following code will calculate (on the fly) the width of a vertical scroll bar.
// We'll create two divs, one inside the other and add the outer div to the document body,
// but off-screen where the user won't see it.
// We'll measure the width of the inner div twice: (first) when the outer div's vertical
// scrollbar is hidden and (second) when the outer div's vertical scrollbar is visible.
// The width of inner div will be smaller when outer div's vertical scrollbar is visible.
// By subtracting the two measurements, we can calculate the width of the vertical scrollbar.
// I used code from the following websites as reference material:
// http://jdsharp.us/jQuery/minute/calculate-scrollbar-width.php
// http://www.fleegix.org/articles/2006-05-30-getting-the-scrollbar-width-in-pixels
Document document = Document.get();
// Create an outer div.
DivElement outerDiv = document.createDivElement();
Style outerDivStyle = outerDiv.getStyle();
// Use absolute positioning and set the top/left so that it is off-screen.
// We don't want the user to see anything while we do this calculation.
outerDivStyle.setProperty("position", "absolute");
outerDivStyle.setProperty("top", "-1000px");
outerDivStyle.setProperty("left", "-1000px");
// Set the width and height of the outer div to a fixed size in pixels.
outerDivStyle.setProperty("width", "100px");
outerDivStyle.setProperty("height", "50px");
// Hide the outer div's scrollbar by setting the "overflow" property to "hidden".
outerDivStyle.setProperty("overflow", "hidden");
// Create an inner div and put it inside the outer div.
DivElement innerDiv = document.createDivElement();
Style innerDivStyle = innerDiv.getStyle();
// Set the height of the inner div to be 4 times the height of the outer div so that a
// vertical scrollbar will be necessary (but hidden for now) on the outer div.
innerDivStyle.setProperty("height", "200px");
outerDiv.appendChild(innerDiv);
// Temporarily add the outer div to the document body. It's off-screen so the user won't
// actually see anything.
Element bodyElement = document.getElementsByTagName("body").getItem(0);
bodyElement.appendChild(outerDiv);
// Get the width of the inner div while the outer div's vertical scrollbar is hidden.
int widthWithoutScrollbar = innerDiv.getOffsetWidth();
// Show the outer div's vertical scrollbar by setting the "overflow" property to "auto".
outerDivStyle.setProperty("overflow", "auto");
// Now, get the width of the inner div while the vertical scrollbar is visible.
int widthWithScrollbar = innerDiv.getOffsetWidth();
// Remove the outer div from the document body.
bodyElement.removeChild(outerDiv);
// Calculate the width of the vertical scrollbar by subtracting the two widths.
verticalScrollbarWidth = widthWithoutScrollbar - widthWithScrollbar;
}
return verticalScrollbarWidth;
}
@Override
public final MockForm getForm() {
return this;
}
@Override
public boolean isForm() {
return true;
}
@Override
public String getVisibleTypeName() {
return VISIBLE_TYPE;
}
@Override
protected void addWidthHeightProperties() {
addProperty(PROPERTY_NAME_WIDTH, "" + PORTRAIT_WIDTH, null,
PropertyTypeConstants.PROPERTY_TYPE_LENGTH, null,
new YoungAndroidLengthPropertyEditor());
addProperty(PROPERTY_NAME_HEIGHT, "" + LENGTH_PREFERRED, null,
PropertyTypeConstants.PROPERTY_TYPE_LENGTH, null,
new YoungAndroidLengthPropertyEditor());
}
@Override
public boolean isPropertyPersisted(String propertyName) {
// We use the Width and Height properties to make the form appear correctly in the designer,
// but they aren't actually persisted to the .scm file.
if (propertyName.equals(PROPERTY_NAME_WIDTH) ||
propertyName.equals(PROPERTY_NAME_HEIGHT)) {
return false;
}
return super.isPropertyPersisted(propertyName);
}
@Override
protected boolean isPropertyVisible(String propertyName) {
switch (propertyName) {
case PROPERTY_NAME_WIDTH:
case PROPERTY_NAME_HEIGHT:
case PROPERTY_NAME_ACTIONBAR: {
return false;
}
// The Icon property actually applies to the application and is only visible on Screen1.
case PROPERTY_NAME_ICON:
// The VersionName property actually applies to the application and is only visible on Screen1.
case PROPERTY_NAME_VNAME:
// The VersionCode property actually applies to the application and is only visible on Screen1.
case PROPERTY_NAME_VCODE:
// The Sizing property actually applies to the application and is only visible on Screen1.
case PROPERTY_NAME_SIZING:
// The AppName property actually applies to the application and is only visible on Screen1.
case PROPERTY_NAME_ANAME:
// The ShowListsAsJson property actually applies to the application and is only visible on Screen1.
case PROPERTY_NAME_SHOW_LISTS_AS_JSON:
// The TutorialURL property actually applies to the application and is only visible on Screen1.
case PROPERTY_NAME_TUTORIAL_URL:
case PROPERTY_NAME_BLOCK_SUBSET:
case PROPERTY_NAME_PRIMARY_COLOR:
case PROPERTY_NAME_PRIMARY_COLOR_DARK:
case PROPERTY_NAME_ACCENT_COLOR:
case PROPERTY_NAME_THEME:
case PROPERTY_NAME_DEFAULTFILESCOPE: {
return editor.isScreen1();
}
default: {
return super.isPropertyVisible(propertyName);
}
}
}
/*
* Sets the form's BackgroundColor property to a new value.
*/
private void setBackgroundColorProperty(String text) {
if (MockComponentsUtil.isNoneColor(text)) {
text = "&HFF000000"; // black
} else if (MockComponentsUtil.isDefaultColor(text)) {
text = "&HFFFFFFFF"; // white
}
MockComponentsUtil.setWidgetBackgroundColor(rootPanel, text);
}
/*
* Sets the form's BackgroundImage property to a new value.
*/
private void setBackgroundImageProperty(String text) {
String url = convertImagePropertyValueToUrl(text);
if (url == null) {
// text was not recognized as an asset.
url = "";
}
MockComponentsUtil.setWidgetBackgroundImage(rootPanel, url);
}
private void setScreenOrientationProperty(String text) {
String val = editor.getProjectEditor().getProjectSettingsProperty(
SettingsConstants.PROJECT_YOUNG_ANDROID_SETTINGS,
SettingsConstants.YOUNG_ANDROID_SETTINGS_PHONE_PREVIEW);
if (hasProperty(PROPERTY_NAME_WIDTH) && hasProperty(PROPERTY_NAME_HEIGHT)
&& hasProperty(PROPERTY_NAME_SCROLLABLE)) {
if (text.equalsIgnoreCase("landscape")) {
if (val.equals("iOS") && idxPhoneSize == 0) {
screenWidth = PHONE_LANDSCAPE_WIDTH_iPHONE;
screenHeight = PHONE_LANDSCAPE_HEIGHT_iPHONE;
} else {
screenWidth = LANDSCAPE_WIDTH;
screenHeight = LANDSCAPE_HEIGHT;
}
landscape = true;
} else {
if (val.equals("iOS") && idxPhoneSize == 0) {
screenWidth = PHONE_PORTRAIT_WIDTH_iPHONE;
screenHeight = PHONE_PORTRAIT_HEIGHT_iPHONE;
} else {
screenWidth = PORTRAIT_WIDTH;
screenHeight = PORTRAIT_HEIGHT;
}
landscape = false;
}
setPhoneStyle();
if (landscape) {
if (val.equals("iOS")) {
usableScreenWidth = screenWidth;
} else {
usableScreenWidth = screenWidth - navigationBar.getHeight();
}
usableScreenHeight = screenHeight - phoneBar.getHeight() - titleBar.getHeight();
} else {
usableScreenWidth = screenWidth;
usableScreenHeight = screenHeight - phoneBar.getHeight() - titleBar.getHeight() - navigationBar.getHeight();
}
resizePanel(screenWidth, screenHeight);
changeProperty(PROPERTY_NAME_WIDTH, "" + usableScreenWidth);
boolean scrollable = Boolean.parseBoolean(getPropertyValue(PROPERTY_NAME_SCROLLABLE));
if (!scrollable) {
changeProperty(PROPERTY_NAME_HEIGHT, "" + usableScreenHeight);
}
}
}
private void setScrollableProperty(String text) {
if (hasProperty(PROPERTY_NAME_HEIGHT)) {
final boolean scrollable = Boolean.parseBoolean(text);
int heightHint = scrollable ? LENGTH_PREFERRED : usableScreenHeight;
changeProperty(PROPERTY_NAME_HEIGHT, "" + heightHint);
}
}
private void setIconProperty(String icon) {
// The Icon property actually applies to the application and is only visible on Screen1.
// When we load a form that is not Screen1, this method will be called with the default value
// for icon (empty string). We need to ignore that.
if (editor.isScreen1()) {
editor.getProjectEditor().changeProjectSettingsProperty(
SettingsConstants.PROJECT_YOUNG_ANDROID_SETTINGS,
SettingsConstants.YOUNG_ANDROID_SETTINGS_ICON, icon);
}
}
private void setVCodeProperty(String vcode) {
// The VersionCode property actually applies to the application and is only visible on Screen1.
// When we load a form that is not Screen1, this method will be called with the default value
// for VersionCode (1). We need to ignore that.
if (editor.isScreen1()) {
editor.getProjectEditor().changeProjectSettingsProperty(
SettingsConstants.PROJECT_YOUNG_ANDROID_SETTINGS,
SettingsConstants.YOUNG_ANDROID_SETTINGS_VERSION_CODE, vcode);
}
}
private void setVNameProperty(String vname) {
// The VersionName property actually applies to the application and is only visible on Screen1.
// When we load a form that is not Screen1, this method will be called with the default value
// for VersionName (1.0). We need to ignore that.
if (editor.isScreen1()) {
editor.getProjectEditor().changeProjectSettingsProperty(
SettingsConstants.PROJECT_YOUNG_ANDROID_SETTINGS,
SettingsConstants.YOUNG_ANDROID_SETTINGS_VERSION_NAME, vname);
}
}
private void setSizingProperty(String sizingProperty) {
// The Compatibility property actually applies to the application and is only visible on
// Screen1. When we load a form that is not Screen1, this method will be called with the
// default value for CompatibilityProperty (false). We need to ignore that.
if (editor.isScreen1()) {
editor.getProjectEditor().changeProjectSettingsProperty(
SettingsConstants.PROJECT_YOUNG_ANDROID_SETTINGS,
SettingsConstants.YOUNG_ANDROID_SETTINGS_SIZING, sizingProperty);
}
}
private void setShowListsAsJsonProperty(String asJson) {
// This property actually applies to the application and is only visible on
// Screen1. When we load a form that is not Screen1, this method will be called with the
// default value for ShowListsAsJsonProperty (false). We need to ignore that.
if (editor.isScreen1()) {
editor.getProjectEditor().changeProjectSettingsProperty(
SettingsConstants.PROJECT_YOUNG_ANDROID_SETTINGS,
SettingsConstants.YOUNG_ANDROID_SETTINGS_SHOW_LISTS_AS_JSON, asJson);
}
}
private void setShowStatusBarProperty(String text) {
showStatusBar = Boolean.parseBoolean(text);
phoneBar.setVisible(showStatusBar);
phoneBar.setVisibility(showStatusBar);
if (screenWidth == 0 || screenHeight == 0) { // This happens when a project is loaded
return; // so don't attempt to resize to 0,0
}
resizePanel(screenWidth,screenHeight); // update the MockForm size
}
private void setTutorialURLProperty(String asJson) {
// This property actually applies to the application and is only visible on
// Screen1. When we load a form that is not Screen1, this method will be called with the
// default value for TutorialURL (""). We need to ignore that.
if (editor.isScreen1()) {
editor.getProjectEditor().changeProjectSettingsProperty(
SettingsConstants.PROJECT_YOUNG_ANDROID_SETTINGS,
SettingsConstants.YOUNG_ANDROID_SETTINGS_TUTORIAL_URL, asJson);
}
}
private void setBlockSubsetProperty(String asJson) {
//This property applies to the application and is only visible on Screen1. When we load a form that is
//not Screen1, this method will be called with the default value for SubsetJson (""). We need to ignore that.
if (editor.isScreen1()) {
editor.getProjectEditor().changeProjectSettingsProperty(
SettingsConstants.PROJECT_YOUNG_ANDROID_SETTINGS,
SettingsConstants.YOUNG_ANDROID_SETTINGS_BLOCK_SUBSET, asJson);
if (editor.isLoadComplete()) {
((YaFormEditor)editor).reloadComponentPalette(asJson);
}
}
}
private void setANameProperty(String aname) {
// The AppName property actually applies to the application and is only visible on Screen1.
// When we load a form that is not Screen1, this method will be called with the default value
if (editor.isScreen1()) {
editor.getProjectEditor().changeProjectSettingsProperty(
SettingsConstants.PROJECT_YOUNG_ANDROID_SETTINGS,
SettingsConstants.YOUNG_ANDROID_SETTINGS_APP_NAME, aname);
}
}
private void setTitleVisibleProperty(String text) {
boolean visible = Boolean.parseBoolean(text);
titleBar.setVisible(visible);
}
private void setActionBarProperty(String actionBar) {
this.actionBar = Boolean.parseBoolean(actionBar);
if (editor.isScreen1()) {
editor.getProjectEditor().changeProjectSettingsProperty(
SettingsConstants.PROJECT_YOUNG_ANDROID_SETTINGS,
SettingsConstants.YOUNG_ANDROID_SETTINGS_ACTIONBAR, actionBar);
}
titleBar.setActionBar(this.actionBar, false);
if (initialized) {
resizePanel(screenWidth, screenHeight); // update screen due to titlebar size change.
}
}
private void setPrimaryColor(String color) {
if (editor.isScreen1()) {
editor.getProjectEditor().changeProjectSettingsProperty(
SettingsConstants.PROJECT_YOUNG_ANDROID_SETTINGS,
SettingsConstants.YOUNG_ANDROID_SETTINGS_PRIMARY_COLOR, color);
}
if (color.equals("&H00000000")) {
// Replace Default with actual default color
color = ComponentConstants.DEFAULT_PRIMARY_COLOR;
}
titleBar.setBackgroundColor(color);
primaryColor = color;
}
private void setPrimaryColorDark(String color) {
if (editor.isScreen1()) {
editor.getProjectEditor().changeProjectSettingsProperty(
SettingsConstants.PROJECT_YOUNG_ANDROID_SETTINGS,
SettingsConstants.YOUNG_ANDROID_SETTINGS_PRIMARY_COLOR_DARK, color);
}
primaryDarkColor= color;
}
private void setAccentColor(String color) {
if (editor.isScreen1()) {
editor.getProjectEditor().changeProjectSettingsProperty(
SettingsConstants.PROJECT_YOUNG_ANDROID_SETTINGS,
SettingsConstants.YOUNG_ANDROID_SETTINGS_ACCENT_COLOR, color);
}
}
private void setTheme(String theme) {
if (editor.isScreen1()) {