forked from jbosstools/jbosstools-openshift
/
ApplicationConfigurationWizardPage.java
1312 lines (1157 loc) · 49.6 KB
/
ApplicationConfigurationWizardPage.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
/*******************************************************************************
* Copyright (c) 2012 Red Hat, Inc.
* Distributed under license by Red Hat, Inc. All rights reserved.
* This program is made available under the terms of the
* Eclipse Public License v1.0 which accompanies this distribution,
* and is available at http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Red Hat, Inc. - initial API and implementation
******************************************************************************/
package org.jboss.tools.openshift.express.internal.ui.wizard.application;
import java.lang.reflect.InvocationTargetException;
import java.util.Collection;
import java.util.List;
import java.util.regex.Pattern;
import org.eclipse.core.databinding.DataBindingContext;
import org.eclipse.core.databinding.UpdateListStrategy;
import org.eclipse.core.databinding.beans.BeanProperties;
import org.eclipse.core.databinding.conversion.Converter;
import org.eclipse.core.databinding.observable.list.IObservableList;
import org.eclipse.core.databinding.observable.list.WritableList;
import org.eclipse.core.databinding.observable.value.IObservableValue;
import org.eclipse.core.databinding.observable.value.IValueChangeListener;
import org.eclipse.core.databinding.observable.value.ValueChangeEvent;
import org.eclipse.core.databinding.validation.MultiValidator;
import org.eclipse.core.databinding.validation.ValidationStatus;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.jface.databinding.fieldassist.ControlDecorationSupport;
import org.eclipse.jface.databinding.swt.ISWTObservableValue;
import org.eclipse.jface.databinding.swt.WidgetProperties;
import org.eclipse.jface.databinding.viewers.ViewerProperties;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.fieldassist.AutoCompleteField;
import org.eclipse.jface.fieldassist.ControlDecoration;
import org.eclipse.jface.fieldassist.FieldDecoration;
import org.eclipse.jface.fieldassist.FieldDecorationRegistry;
import org.eclipse.jface.fieldassist.TextContentAdapter;
import org.eclipse.jface.layout.GridDataFactory;
import org.eclipse.jface.layout.GridLayoutFactory;
import org.eclipse.jface.layout.TableColumnLayout;
import org.eclipse.jface.viewers.ArrayContentProvider;
import org.eclipse.jface.viewers.CellLabelProvider;
import org.eclipse.jface.viewers.CheckboxTableViewer;
import org.eclipse.jface.viewers.ColumnWeightData;
import org.eclipse.jface.viewers.IElementComparer;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.TableViewerColumn;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.jface.viewers.ViewerCell;
import org.eclipse.jface.viewers.ViewerSorter;
import org.eclipse.jface.wizard.IWizard;
import org.eclipse.jface.wizard.WizardDialog;
import org.eclipse.osgi.util.NLS;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Link;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.Text;
import org.jboss.tools.common.ui.WizardUtils;
import org.jboss.tools.common.ui.databinding.InvertingBooleanConverter;
import org.jboss.tools.common.ui.databinding.ParametrizableWizardPageSupport;
import org.jboss.tools.common.ui.databinding.ValueBindingBuilder;
import org.jboss.tools.openshift.express.internal.core.EmbedCartridgeStrategy.IApplicationPropertiesProvider;
import org.jboss.tools.openshift.express.internal.core.connection.Connection;
import org.jboss.tools.openshift.express.internal.core.util.StringUtils;
import org.jboss.tools.openshift.express.internal.ui.OpenShiftUIActivator;
import org.jboss.tools.openshift.express.internal.ui.databinding.EmptyStringToNullConverter;
import org.jboss.tools.openshift.express.internal.ui.databinding.MultiConverter;
import org.jboss.tools.openshift.express.internal.ui.databinding.RequiredControlDecorationUpdater;
import org.jboss.tools.openshift.express.internal.ui.databinding.TrimmingStringConverter;
import org.jboss.tools.openshift.express.internal.ui.job.AbstractDelegatingMonitorJob;
import org.jboss.tools.openshift.express.internal.ui.utils.DataBindingUtils;
import org.jboss.tools.openshift.express.internal.ui.utils.DialogChildToggleAdapter;
import org.jboss.tools.openshift.express.internal.ui.utils.Logger;
import org.jboss.tools.openshift.express.internal.ui.utils.OpenShiftResourceUtils;
import org.jboss.tools.openshift.express.internal.ui.utils.UIUtils;
import org.jboss.tools.openshift.express.internal.ui.utils.UIUtils.IWidgetVisitor;
import org.jboss.tools.openshift.express.internal.ui.wizard.AbstractOpenShiftWizardPage;
import org.jboss.tools.openshift.express.internal.ui.wizard.OkButtonWizardDialog;
import org.jboss.tools.openshift.express.internal.ui.wizard.domain.ManageDomainsWizard;
import org.jboss.tools.openshift.express.internal.ui.wizard.domain.NewDomainWizard;
import org.jboss.tools.openshift.express.internal.ui.wizard.embed.EmbedCartridgeStrategyAdapter;
import org.jboss.tools.openshift.express.internal.ui.wizard.environment.EnvironmentVariablesWizard;
import org.jboss.tools.openshift.express.internal.ui.wizard.ssh.NoSSHKeysWizard;
import com.openshift.client.ApplicationScale;
import com.openshift.client.IApplication;
import com.openshift.client.IDomain;
import com.openshift.client.IGearProfile;
import com.openshift.client.NotFoundOpenShiftException;
import com.openshift.client.OpenShiftException;
import com.openshift.client.cartridge.IEmbeddableCartridge;
import com.openshift.client.cartridge.IStandaloneCartridge;
/**
* @author Andre Dietisheim
* @author Xavier Coulon
*/
public class ApplicationConfigurationWizardPage extends AbstractOpenShiftWizardPage {
private CheckboxTableViewer viewer;
private ApplicationConfigurationWizardPageModel pageModel;
private Button useExistingAppBtn;
private Text existingAppNameText;
private Button browseAppsButton;
private Group newAppConfigurationGroup;
private Text newAppNameText;
private Button checkAllButton;
private Button uncheckAllButton;
ApplicationConfigurationWizardPage(IWizard wizard, OpenShiftApplicationWizardModel wizardModel) {
super("New or existing OpenShift Application", "", "New or existing OpenShift Application, wizard", wizard);
this.pageModel = new ApplicationConfigurationWizardPageModel(wizardModel);
}
@Override
protected void doCreateControls(Composite container, DataBindingContext dbc) {
setWizardPageDescription(pageModel.isUseExistingApplication());
GridLayoutFactory.fillDefaults().applyTo(container);
GridDataFactory.fillDefaults().grab(true, true).align(SWT.FILL, SWT.FILL).applyTo(container);
createDomainGroup(container, dbc);
createApplicationSelectionGroup(container, dbc);
createApplicationConfigurationGroup(container, dbc);
}
private void createDomainGroup(Composite container, DataBindingContext dbc) {
Composite domainGroup = new Composite(container, SWT.NONE);
GridDataFactory.fillDefaults()
.align(SWT.LEFT, SWT.CENTER).align(SWT.FILL, SWT.CENTER).grab(true, false).applyTo(domainGroup);
GridLayoutFactory.fillDefaults()
.numColumns(3).margins(6, 6).applyTo(domainGroup);
// domain
final Label domainLabel = new Label(domainGroup, SWT.NONE);
domainLabel.setText("Domain:");
GridDataFactory.fillDefaults()
.align(SWT.FILL, SWT.CENTER).applyTo(domainLabel);
Combo domainCombo = new Combo(domainGroup, SWT.BORDER | SWT.READ_ONLY);
GridDataFactory.fillDefaults()
.align(SWT.FILL, SWT.CENTER).grab(true, false).applyTo(domainCombo);
dbc.bindList(WidgetProperties.items().observe(domainCombo),
BeanProperties.list(ApplicationConfigurationWizardPageModel.PROPERTY_DOMAINS).observe(pageModel),
new UpdateListStrategy(UpdateListStrategy.POLICY_NEVER),
new UpdateListStrategy() {
/**
* Needed to avoid buggy list update strategy in
* ListBinding. The bug appears if the model list changes
* its ordering and the strategy then tries to apply the
* move in the target (widget). It does not apply the
* conversion and ends up in a class cast exception when
* updating the target (widget) items list.
*
* @see https://issues.jboss.org/browse/JBIDE-11954/JBIDE-15813
*/
protected boolean useMoveAndReplace() {
return false;
}
}.setConverter(new DomainToStringConverter()));
final ISWTObservableValue selectedDomainIndexObservable =
WidgetProperties.singleSelectionIndex().observe(domainCombo);
final IObservableValue selectedDomainModelObservable =
BeanProperties.value(
ApplicationConfigurationWizardPageModel.PROPERTY_DOMAIN).observe(pageModel);
ValueBindingBuilder.bind(selectedDomainIndexObservable)
.converting(new DomainsIndexToDomain())
.to(selectedDomainModelObservable)
.converting(new DomainToDomainsIndex())
.in(dbc);
DataBindingUtils.addDisposableValueChangeListener(
onDomainChanged(dbc), selectedDomainModelObservable, domainCombo);
Link manageDomainsLink = new Link(domainGroup, SWT.NONE);
manageDomainsLink
.setText("<a>Manage Domains</a>");
GridDataFactory.fillDefaults()
.align(SWT.FILL, SWT.CENTER).applyTo(manageDomainsLink);
manageDomainsLink.addSelectionListener(onManageDomains());
}
private IValueChangeListener onDomainChanged(final DataBindingContext dbc) {
return new IValueChangeListener() {
@Override
public void handleValueChange(ValueChangeEvent event) {
Object value = event.getObservableValue().getValue();
if (!(value instanceof IDomain)) {
return;
}
final IDomain domain = (IDomain) value;
AbstractDelegatingMonitorJob job = new AbstractDelegatingMonitorJob(NLS.bind("Loading applications for domain {0}...", domain.getId())) {
@Override
protected IStatus doRun(IProgressMonitor monitor) {
pageModel.loadExistingApplications();
if (!pageModel.isUseExistingApplication()) {
return Status.OK_STATUS;
}
String existingApplicationName = pageModel.getExistingApplicationName();
if (!StringUtils.isEmpty(existingApplicationName)
&& !domain.hasApplicationByName(existingApplicationName)) {
pageModel.setExistingApplication(domain);
}
return Status.OK_STATUS;
}
};
try {
WizardUtils.runInWizard(job, getContainer(), dbc);
} catch (InvocationTargetException e) {
OpenShiftUIActivator.log(OpenShiftUIActivator.createErrorStatus(NLS.bind(
"Could not load applications for domain {0}.",
domain), e));
} catch (InterruptedException e) {
OpenShiftUIActivator.log(OpenShiftUIActivator.createErrorStatus(NLS.bind(
"Could not load applications for domain {0}.",
domain), e));
}
}
};
}
private Composite createApplicationSelectionGroup(Composite container, DataBindingContext dbc) {
Group existingAppSelectionGroup = new Group(container, SWT.NONE);
existingAppSelectionGroup.setText("Existing Application");
GridDataFactory.fillDefaults().align(SWT.LEFT, SWT.CENTER).align(SWT.FILL, SWT.CENTER).grab(true, false)
.applyTo(existingAppSelectionGroup);
GridLayoutFactory.fillDefaults().numColumns(3).margins(6, 6).applyTo(existingAppSelectionGroup);
// existing app checkbox
useExistingAppBtn = new Button(existingAppSelectionGroup, SWT.CHECK);
useExistingAppBtn.setText("Use existing application:");
useExistingAppBtn.setToolTipText("Select an existing application or uncheck to create a new one.");
useExistingAppBtn.setFocus();
GridDataFactory.fillDefaults()
.align(SWT.FILL, SWT.CENTER).grab(false, false).applyTo(useExistingAppBtn);
IObservableValue useExistingAppObservable = BeanProperties.value(
ApplicationConfigurationWizardPageModel.PROPERTY_USE_EXISTING_APPLICATION).observe(pageModel);
final IObservableValue useExistingAppBtnSelection = WidgetProperties.selection().observe(useExistingAppBtn);
dbc.bindValue(useExistingAppBtnSelection, useExistingAppObservable);
// existing app name
this.existingAppNameText = new Text(existingAppSelectionGroup, SWT.BORDER);
GridDataFactory.fillDefaults()
.align(SWT.FILL, SWT.CENTER).grab(true, false).applyTo(existingAppNameText);
IObservableValue existingAppNameTextObservable = WidgetProperties.text(SWT.Modify).observe(existingAppNameText);
IObservableValue existingAppNameModelObservable = BeanProperties.value(
ApplicationConfigurationWizardPageModel.PROPERTY_EXISTING_APPLICATION_NAME).observe(pageModel);
ValueBindingBuilder
.bind(existingAppNameTextObservable)
.to(existingAppNameModelObservable)
.in(dbc);
UIUtils.focusOnSelection(useExistingAppBtn, existingAppNameText);
createExistingAppNameContentAssist(existingAppNameText, pageModel.getApplicationNames());
// observe the list of application, get notified once they have been
// loaded
IObservableValue existingApplicationsLoaded =
BeanProperties.value(ApplicationConfigurationWizardPageModel.PROPERTY_EXISTING_APPLICATIONS_LOADED)
.observe(pageModel);
final ApplicationToSelectNameValidator existingAppValidator =
new ApplicationToSelectNameValidator(
useExistingAppBtnSelection, existingAppNameTextObservable, existingApplicationsLoaded);
dbc.addValidationStatusProvider(existingAppValidator);
ControlDecorationSupport.create(
existingAppValidator, SWT.LEFT | SWT.TOP, null, new RequiredControlDecorationUpdater(true));
useExistingAppBtnSelection.addValueChangeListener(
onUseExistingApplication(
newAppConfigurationGroup, existingAppNameText, browseAppsButton));
// browse button
this.browseAppsButton = new Button(existingAppSelectionGroup, SWT.NONE);
browseAppsButton.setText("Browse...");
browseAppsButton.addSelectionListener(onBrowseApps(dbc));
GridDataFactory.fillDefaults()
.align(SWT.LEFT, SWT.CENTER).hint(100, SWT.DEFAULT).grab(false, false)
.applyTo(browseAppsButton);
return existingAppSelectionGroup;
}
private void createExistingAppNameContentAssist(Text existingAppNameText, String[] applicationNames) {
ControlDecoration dec = new ControlDecoration(existingAppNameText, SWT.TOP | SWT.LEFT);
FieldDecoration contentProposalFieldIndicator =
FieldDecorationRegistry.getDefault().getFieldDecoration(FieldDecorationRegistry.DEC_CONTENT_PROPOSAL);
dec.setImage(contentProposalFieldIndicator.getImage());
dec.setDescriptionText("Auto-completion is enabled when you start typing an application name.");
dec.setShowOnlyOnFocus(true);
AutoCompleteField adapter =
new AutoCompleteField(existingAppNameText, new TextContentAdapter(), new String[] {});
adapter.setProposals(applicationNames);
}
private SelectionListener onBrowseApps(final DataBindingContext dbc) {
return new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
OpenShiftApplicationWizard wizard = (OpenShiftApplicationWizard) getWizard();
OpenShiftApplicationWizardModel wizardModel = wizard.getModel();
final ApplicationSelectionDialog appSelectionDialog =
new ApplicationSelectionDialog(wizard, wizardModel, null, getShell());
if (appSelectionDialog.open() == IDialogConstants.OK_ID) {
final IApplication selectedApplication = appSelectionDialog.getSelectedApplication();
if (selectedApplication != null) {
// This setter may be long-running
Job j = new AbstractDelegatingMonitorJob("Setting Application...") {
@Override
protected IStatus doRun(IProgressMonitor monitor) {
try {
pageModel.setExistingApplicationName(selectedApplication.getName());
} catch (OpenShiftException ex) {
OpenShiftUIActivator.log(OpenShiftUIActivator.createErrorStatus(NLS.bind(
"Could not get embedded cartridges for application {0}",
selectedApplication.getName()), ex));
}
return Status.OK_STATUS;
}
};
try {
WizardUtils.runInWizard(j, getContainer(), dbc);
} catch (InvocationTargetException ite) {
OpenShiftUIActivator.log(OpenShiftUIActivator.createErrorStatus(NLS.bind(
"Could use application {0} as existing application.",
selectedApplication.getName()), ite));
} catch (InterruptedException ie) {
OpenShiftUIActivator.log(OpenShiftUIActivator.createErrorStatus(NLS.bind(
"Could use application {0} as existing application.",
selectedApplication.getName()), ie));
}
}
}
}
};
}
private void createApplicationConfigurationGroup(Composite parent, DataBindingContext dbc) {
this.newAppConfigurationGroup = new Group(parent, SWT.NONE);
newAppConfigurationGroup.setText("New application");
GridLayoutFactory.fillDefaults()
.numColumns(3).margins(6, 6).applyTo(newAppConfigurationGroup);
GridDataFactory.fillDefaults()
.grab(true, true).align(SWT.FILL, SWT.FILL).applyTo(newAppConfigurationGroup);
// application name
final Label newAppNameLabel = new Label(newAppConfigurationGroup, SWT.NONE);
newAppNameLabel.setText("Name:");
GridDataFactory.fillDefaults().align(SWT.FILL, SWT.CENTER).applyTo(newAppNameLabel);
this.newAppNameText = new Text(newAppConfigurationGroup, SWT.BORDER);
GridDataFactory.fillDefaults().grab(true, false).span(2, 1).align(SWT.FILL, SWT.FILL).applyTo(newAppNameText);
UIUtils.selectAllOnFocus(newAppNameText);
final IObservableValue applicationNameTextObservable =
WidgetProperties.text(SWT.Modify).observe(newAppNameText);
final IObservableValue applicationNameModelObservable =
BeanProperties.value(ApplicationConfigurationWizardPageModel.PROPERTY_APPLICATION_NAME).observe(
pageModel);
ValueBindingBuilder
.bind(applicationNameTextObservable).
to(applicationNameModelObservable)
.in(dbc);
final ISWTObservableValue useExistingAppBtnSelection = WidgetProperties.selection().observe(useExistingAppBtn);
IObservableValue existingApplicationsLoaded =
BeanProperties.value(ApplicationConfigurationWizardPageModel.PROPERTY_EXISTING_APPLICATIONS_LOADED)
.observe(pageModel);
final NewApplicationNameValidator newApplicationNameValidator =
new NewApplicationNameValidator(
useExistingAppBtnSelection, existingApplicationsLoaded, applicationNameTextObservable);
dbc.addValidationStatusProvider(newApplicationNameValidator);
ControlDecorationSupport.create(
newApplicationNameValidator, SWT.LEFT | SWT.TOP, null, new RequiredControlDecorationUpdater());
// application type
final Label newAppTypeLabel = new Label(newAppConfigurationGroup, SWT.NONE);
newAppTypeLabel.setText("Type:");
GridDataFactory.fillDefaults()
.align(SWT.FILL, SWT.CENTER).applyTo(newAppTypeLabel);
Combo newAppCartridgeCombo = new Combo(newAppConfigurationGroup, SWT.BORDER | SWT.READ_ONLY);
GridDataFactory.fillDefaults()
.align(SWT.FILL, SWT.CENTER).span(2, 1).grab(true, false).applyTo(newAppCartridgeCombo);
dbc.bindList(WidgetProperties.items().observe(newAppCartridgeCombo),
BeanProperties.list(ApplicationConfigurationWizardPageModel.PROPERTY_CARTRIDGES).observe(pageModel),
new UpdateListStrategy(UpdateListStrategy.POLICY_NEVER),
new UpdateListStrategy().setConverter(new CartridgeToStringConverter()));
final ISWTObservableValue selectedCartridgeIndexObservable =
WidgetProperties.singleSelectionIndex().observe(newAppCartridgeCombo);
final IObservableValue selectedCartridgeModelObservable =
BeanProperties.value(
ApplicationConfigurationWizardPageModel.PROPERTY_SELECTED_CARTRIDGE).observe(pageModel);
ValueBindingBuilder.bind(selectedCartridgeIndexObservable)
.converting(new CartridgesIndexToCartridge())
.to(selectedCartridgeModelObservable)
.converting(new CartridgeToCartridgesIndex())
.in(dbc);
// gear profile
final Label gearProfileLabel = new Label(newAppConfigurationGroup, SWT.NONE);
gearProfileLabel.setText("Gear profile:");
GridDataFactory.fillDefaults()
.align(SWT.FILL, SWT.CENTER).applyTo(gearProfileLabel);
Combo gearProfilesCombo = new Combo(newAppConfigurationGroup, SWT.BORDER | SWT.READ_ONLY);
GridDataFactory.fillDefaults()
.align(SWT.FILL, SWT.CENTER).grab(true, false).applyTo(gearProfilesCombo);
dbc.bindList(WidgetProperties.items().observe(gearProfilesCombo),
BeanProperties.list(ApplicationConfigurationWizardPageModel.PROPERTY_GEAR_PROFILES).observe(pageModel),
new UpdateListStrategy(UpdateListStrategy.POLICY_NEVER),
new UpdateListStrategy() {
/**
* Needed to avoid buggy list update strategy in
* ListBinding. The bug appears if the model list changes
* its ordering and the strategy then tries to apply the
* move in the target (widget). It does not apply the
* conversion and ends up in a class cast exception when
* updating the target (widget) items list.
*
* @see https://issues.jboss.org/browse/JBIDE-11954
*/
protected boolean useMoveAndReplace() {
return false;
}
}.setConverter(new GearProfileToStringConverter()));
final ISWTObservableValue selectedGearProfileComboObservable =
WidgetProperties.selection().observe(gearProfilesCombo);
final IObservableValue selectedGearProfileModelObservable = BeanProperties.value(
ApplicationConfigurationWizardPageModel.PROPERTY_SELECTED_GEAR_PROFILE).observe(pageModel);
ValueBindingBuilder.bind(selectedGearProfileComboObservable)
.converting(new StringToGearProfileConverter())
.to(selectedGearProfileModelObservable)
.converting(new GearProfileToStringConverter())
.in(dbc);
// scaling
Button enableScalingButton = new Button(newAppConfigurationGroup, SWT.CHECK);
enableScalingButton.setText("Enable scaling");
IObservableValue enableScalingModelObservable =
BeanProperties.value(ApplicationConfigurationWizardPageModel.PROPERTY_APPLICATION_SCALE)
.observe(pageModel);
final IObservableValue enableScalingButtonSelection = WidgetProperties.selection().observe(enableScalingButton);
ValueBindingBuilder
.bind(enableScalingButtonSelection).converting(new BooleanToApplicationScaleConverter())
.to(enableScalingModelObservable).converting(new ApplicationScaleToBooleanConverter())
.in(dbc);
final NewApplicationTypeValidator newApplicationTypeValidator =
new NewApplicationTypeValidator(useExistingAppBtnSelection, selectedCartridgeIndexObservable);
dbc.addValidationStatusProvider(newApplicationTypeValidator);
ControlDecorationSupport.create(newApplicationTypeValidator, SWT.LEFT | SWT.TOP, null,
new RequiredControlDecorationUpdater());
// embeddable cartridges
Group newAppEmbeddableCartridgesGroup = new Group(newAppConfigurationGroup, SWT.NONE);
newAppEmbeddableCartridgesGroup.setText("Embeddable Cartridges");
GridDataFactory.fillDefaults()
.grab(true, true).align(SWT.FILL, SWT.FILL).span(3, 1).applyTo(newAppEmbeddableCartridgesGroup);
GridLayoutFactory.fillDefaults().numColumns(2).margins(6, 6).applyTo(newAppEmbeddableCartridgesGroup);
Composite tableContainer = new Composite(newAppEmbeddableCartridgesGroup, SWT.NONE);
GridDataFactory.fillDefaults()
.align(SWT.FILL, SWT.FILL).grab(true, true).span(1, 2).hint(400, 250).applyTo(tableContainer);
this.viewer = createTable(tableContainer);
dbc.bindSet(
ViewerProperties.checkedElements(IEmbeddableCartridge.class).observe(viewer),
BeanProperties.set(
ApplicationConfigurationWizardPageModel.PROPERTY_SELECTED_EMBEDDABLE_CARTRIDGES)
.observe(pageModel));
// strategy has to be attached after the binding, so that the binding
// can still add the checked cartridge and the strategy can correct
viewer.addCheckStateListener(new EmbedCartridgeStrategyAdapter(pageModel, this,
new ApplicationFormPropertiesProvider()));
this.checkAllButton = new Button(newAppEmbeddableCartridgesGroup, SWT.PUSH);
checkAllButton.setText("&Select All");
GridDataFactory.fillDefaults()
.hint(110, SWT.DEFAULT).grab(false, false).align(SWT.FILL, SWT.TOP).applyTo(checkAllButton);
checkAllButton.addSelectionListener(onCheckAll());
this.uncheckAllButton = new Button(newAppEmbeddableCartridgesGroup, SWT.PUSH);
uncheckAllButton.setText("&Deselect All");
GridDataFactory.fillDefaults()
.hint(110, SWT.DEFAULT).grab(false, false).align(SWT.FILL, SWT.TOP).applyTo(uncheckAllButton);
uncheckAllButton.addSelectionListener(onUncheckAll());
// advanced configurations
createAdvancedGroup(newAppConfigurationGroup, dbc);
}
private void createAdvancedGroup(Composite parent, DataBindingContext dbc) {
// advanced button
Button advancedButton = new Button(parent, SWT.NONE);
advancedButton.setText(" Advanced >> ");
GridDataFactory.fillDefaults()
.align(SWT.BEGINNING, SWT.CENTER).span(3, 1).applyTo(advancedButton);
// advanced composite
Composite advancedComposite = new Composite(parent, SWT.NONE);
GridData advancedCompositeGridData = GridDataFactory.fillDefaults()
.align(SWT.FILL, SWT.FILL).grab(true, false).span(3, 1).create();
advancedComposite.setLayoutData(advancedCompositeGridData);
GridLayoutFactory.fillDefaults().applyTo(advancedComposite);
// source group
Group sourceGroup = new Group(advancedComposite, SWT.NONE);
sourceGroup.setText("Source Code");
GridDataFactory.fillDefaults()
.align(SWT.FILL, SWT.FILL).grab(true, false).applyTo(sourceGroup);
GridLayoutFactory.fillDefaults()
.numColumns(2).margins(6, 6).applyTo(sourceGroup);
// use default source checkbox
Button useDefaultSourceButton = new Button(sourceGroup, SWT.CHECK);
useDefaultSourceButton.setText("Use default source code");
GridDataFactory.fillDefaults()
.align(SWT.BEGINNING, SWT.CENTER).span(2, 1).applyTo(useDefaultSourceButton);
IObservableValue defaultSourceCodeObservable = WidgetProperties.selection().observe(useDefaultSourceButton);
ValueBindingBuilder
.bind(defaultSourceCodeObservable)
.to(BeanProperties.value(
ApplicationConfigurationWizardPageModel.PROPERTY_DEFAULT_SOURCECODE).observe(pageModel))
.in(dbc);
// source code text
Label sourceCodeUrlLabel = new Label(sourceGroup, SWT.NONE);
sourceCodeUrlLabel.setText("Source code:");
GridDataFactory.fillDefaults()
.align(SWT.BEGINNING, SWT.CENTER).applyTo(sourceCodeUrlLabel);
ValueBindingBuilder
.bind(WidgetProperties.enabled().observe(sourceCodeUrlLabel))
.notUpdatingParticipant()
.to(BeanProperties.value(
ApplicationConfigurationWizardPageModel.PROPERTY_DEFAULT_SOURCECODE).observe(pageModel))
.converting(new InvertingBooleanConverter())
.in(dbc);
Text sourceUrlText = new Text(sourceGroup, SWT.BORDER);
GridDataFactory.fillDefaults()
.align(SWT.FILL, SWT.CENTER).grab(true, false).applyTo(sourceUrlText);
ValueBindingBuilder
.bind(WidgetProperties.enabled().observe(sourceUrlText))
.notUpdatingParticipant()
.to(BeanProperties.value(
ApplicationConfigurationWizardPageModel.PROPERTY_DEFAULT_SOURCECODE).observe(pageModel))
.converting(new InvertingBooleanConverter())
.in(dbc);
IObservableValue sourcecodeUrlObservable = WidgetProperties.text(SWT.Modify).observe(sourceUrlText);
ValueBindingBuilder
.bind(sourcecodeUrlObservable)
.converting(new MultiConverter(new TrimmingStringConverter(), new EmptyStringToNullConverter()))
.to(BeanProperties.value(
ApplicationConfigurationWizardPageModel.PROPERTY_INITIAL_GITURL).observe(pageModel))
.in(dbc);
MultiValidator sourceCodeUrlValidator = new SourceCodeUrlValidator(defaultSourceCodeObservable,
sourcecodeUrlObservable);
dbc.addValidationStatusProvider(sourceCodeUrlValidator);
ControlDecorationSupport.create(
sourceCodeUrlValidator, SWT.LEFT | SWT.TOP, null, new RequiredControlDecorationUpdater());
DialogChildToggleAdapter toggleAdapter = new DialogChildToggleAdapter(advancedComposite, getShell(), false);
advancedButton.addSelectionListener(onAdvancedClicked(advancedButton, toggleAdapter, sourceUrlText,
sourceCodeUrlLabel));
// explanation
Text sourceCodeExplanationText = new Text(sourceGroup, SWT.WRAP);
sourceCodeExplanationText
.setText("Your application will start with an exact copy of the code and configuration "
+ "provided in this Git repository instead of the default application.");
sourceCodeExplanationText.setEnabled(false);
UIUtils.copyBackground(sourceGroup, sourceCodeExplanationText);
GridDataFactory.fillDefaults()
.align(SWT.FILL, SWT.CENTER).grab(true, true).span(2, 1).applyTo(sourceCodeExplanationText);
// environment variables
Button environmentVariablesButton = new Button(advancedComposite, SWT.NONE);
environmentVariablesButton.setText("Environment Variables... ");
GridDataFactory.fillDefaults()
.align(SWT.BEGINNING, SWT.CENTER).applyTo(environmentVariablesButton);
environmentVariablesButton.addSelectionListener(onBrowseEnvironmentVariables(dbc));
ValueBindingBuilder
.bind(WidgetProperties.visible().observe(environmentVariablesButton))
.notUpdatingParticipant()
.to(BeanProperties.value(
ApplicationConfigurationWizardPageModel.PROPERTY_ENVIRONMENT_VARIABLES_SUPPORTED)
.observe(pageModel))
.in(dbc);
}
protected SelectionListener onManageDomains() {
return new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
Connection connection = pageModel.getConnection();
if (connection == null) {
return;
}
ManageDomainsWizard domainWizard =
new ManageDomainsWizard("Choose domain", "Please choose the domain for your new application"
, pageModel.getDomain(), connection);
if (new OkButtonWizardDialog(getShell(), domainWizard).open() == Dialog.OK) {
pageModel.setDomain(domainWizard.getDomain());
}
;
}
};
}
private SelectionListener onAdvancedClicked(final Button toggleButton, final DialogChildToggleAdapter adapter,
final Text sourceUrlText, final Label sourceCodeUrlLabel) {
return new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
if (!adapter.isVisible()) {
toggleButton.setText(" << Advanced ");
} else {
toggleButton.setText(" Advanced >> ");
}
sourceUrlText.setEnabled(!pageModel.isDefaultSourcecode());
sourceCodeUrlLabel.setEnabled(!pageModel.isDefaultSourcecode());
adapter.toggle();
}
};
}
private SelectionListener onBrowseEnvironmentVariables(final DataBindingContext dbc) {
return new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
EnvironmentVariablesWizard environmentVariablesWizard =
new EnvironmentVariablesWizard(pageModel.getEnvironmentVariables());
if (new OkButtonWizardDialog(getShell(), environmentVariablesWizard).open() == Dialog.OK) {
pageModel.setEnvironmentVariables(environmentVariablesWizard.getEnvironmentVariables());
}
}
};
}
/**
* Triggered when the user checks "use existing application". It will
* enable/disable the application widgets and reset existing values.
*
* @param applicationConfigurationGroup
* @param applicationNameText
* @param applicationBrowseButton
* @return
*/
private IValueChangeListener onUseExistingApplication(final Group applicationConfigurationGroup,
final Text applicationNameText, final Button applicationBrowseButton) {
return new IValueChangeListener() {
@Override
public void handleValueChange(ValueChangeEvent event) {
Object newValue = event.diff.getNewValue();
if (newValue instanceof Boolean) {
Boolean useExisting = (Boolean) newValue;
// if (!useExisting) {
// resetExistingApplication();
// }
enableApplicationWidgets(useExisting);
setWizardPageDescription(useExisting);
}
}
};
}
/**
* Enables/disables the given widgets based on the flag to use an existing
* app or create a new application.
*
* @param useExisting
* @param applicationConfigurationGroup
* @param applicationNameText
* @param applicationBrowseButton
*/
private void enableApplicationWidgets(final Boolean useExisting) {
existingAppNameText.setEnabled(useExisting);
browseAppsButton.setEnabled(useExisting);
UIUtils.doForAllChildren(new IWidgetVisitor() {
@Override
public void visit(Control control) {
control.setEnabled(!useExisting);
}
}, newAppConfigurationGroup);
}
private CheckboxTableViewer createTable(Composite tableContainer) {
Table table =
new Table(tableContainer, SWT.BORDER | SWT.FULL_SELECTION | SWT.V_SCROLL | SWT.H_SCROLL | SWT.CHECK);
table.setLinesVisible(true);
TableColumnLayout tableLayout = new TableColumnLayout();
tableContainer.setLayout(tableLayout);
CheckboxTableViewer viewer = new CheckboxTableViewer(table);
viewer.setSorter(new ViewerSorter() {
@Override
public int compare(Viewer viewer, Object e1, Object e2) {
if (e1 instanceof IEmbeddableCartridge
&& e2 instanceof IEmbeddableCartridge) {
return ((IEmbeddableCartridge) e1).getDisplayName().compareTo((
(IEmbeddableCartridge) e2).getDisplayName());
}
return super.compare(viewer, e1, e2);
}
});
viewer.setComparer(new EqualityComparer());
viewer.setContentProvider(new ArrayContentProvider());
createTableColumn("Name", 1, new CellLabelProvider() {
@Override
public void update(ViewerCell cell) {
IEmbeddableCartridge cartridge = (IEmbeddableCartridge) cell.getElement();
cell.setText(OpenShiftResourceUtils.toString(cartridge));
}
}, viewer, tableLayout);
return viewer;
}
private void createTableColumn(String name, int weight, CellLabelProvider cellLabelProvider, TableViewer viewer,
TableColumnLayout layout) {
TableViewerColumn column = new TableViewerColumn(viewer, SWT.LEFT);
column.getColumn().setText(name);
column.setLabelProvider(cellLabelProvider);
layout.setColumnData(column.getColumn(), new ColumnWeightData(weight, true));
}
private SelectionListener onCheckAll() {
return new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
// viewer.setAllChecked(true);
// try {
// addJenkinsCartridge(IEmbeddableCartridge.JENKINS_14);
// } catch (OpenShiftException ex) {
// OpenShiftUIActivator.log("Could not select jenkins cartridge",
// ex);
// } catch (SocketTimeoutException ex) {
// OpenShiftUIActivator.log("Could not select jenkins cartridge",
// ex);
// }
}
};
}
private SelectionListener onUncheckAll() {
return new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
viewer.setAllChecked(false);
}
};
}
private void setViewerInput(final Collection<IEmbeddableCartridge> cartridges) {
getShell().getDisplay().syncExec(new Runnable() {
@Override
public void run() {
if (viewer != null) {
viewer.setInput(cartridges);
}
}
});
}
private static final class CartridgeToStringConverter extends Converter {
private CartridgeToStringConverter() {
super(Object.class, String.class);
}
@Override
public Object convert(Object fromObject) {
if (!(fromObject instanceof IStandaloneCartridge)) {
return null;
}
return OpenShiftResourceUtils.toString((IStandaloneCartridge) fromObject);
}
}
private static final class DomainToStringConverter extends Converter {
private DomainToStringConverter() {
super(Object.class, String.class);
}
@Override
public Object convert(Object fromObject) {
if (!(fromObject instanceof IDomain)) {
return null;
}
return OpenShiftResourceUtils.toString((IDomain) fromObject);
}
}
private final class DomainsIndexToDomain extends Converter {
public DomainsIndexToDomain() {
super(Integer.class, IDomain.class);
}
@Override
public Object convert(Object fromObject) {
if (!(fromObject instanceof Integer)) {
return null;
}
int index = ((Integer) fromObject).intValue();
List<IDomain> domains = pageModel.getDomains();
if (domains == null
|| index >= domains.size()
|| index == -1) {
return null;
}
return domains.get(index);
}
}
private final class DomainToDomainsIndex extends Converter {
public DomainToDomainsIndex() {
super(IDomain.class, Integer.class);
}
@Override
public Object convert(Object fromObject) {
if (!(fromObject instanceof IDomain)) {
return null;
}
IDomain domain = ((IDomain) fromObject);
List<IDomain> domains = pageModel.getDomains();
if (domains == null
|| domains.isEmpty()) {
return -1;
}
return domains.indexOf(domain);
}
}
private final class CartridgesIndexToCartridge extends Converter {
public CartridgesIndexToCartridge() {
super(Integer.class, IStandaloneCartridge.class);
}
@Override
public Object convert(Object fromObject) {
if (!(fromObject instanceof Integer)) {
return null;
}
int index = ((Integer) fromObject).intValue();
List<IStandaloneCartridge> cartridges = pageModel.getCartridges();
if (index >= cartridges.size()
|| index == -1) {
return null;
}
return cartridges.get(index);
}
}
private final class CartridgeToCartridgesIndex extends Converter {
public CartridgeToCartridgesIndex() {
super(IStandaloneCartridge.class, Integer.class);
}
@Override
public Object convert(Object fromObject) {
if (!(fromObject instanceof IStandaloneCartridge)) {
return null;
}
IStandaloneCartridge cartridge = ((IStandaloneCartridge) fromObject);
List<IStandaloneCartridge> cartridges = pageModel.getCartridges();
return cartridges.indexOf(cartridge);
}
}
private static final class GearProfileToStringConverter extends Converter {
private GearProfileToStringConverter() {
super(Object.class, String.class);
}
@Override
public Object convert(Object fromObject) {
if (!(fromObject instanceof IGearProfile)) {
return null;
}
return ((IGearProfile) fromObject).getName();
}
}
private final class StringToGearProfileConverter extends Converter {
private StringToGearProfileConverter() {
super(String.class, IGearProfile.class);
}
@Override
public Object convert(Object fromObject) {
if (fromObject instanceof String) {
return pageModel.getGearProfileByName((String) fromObject);
}
return null;
}
}
private static final class ApplicationScaleToBooleanConverter extends Converter {
private ApplicationScaleToBooleanConverter() {
super(Object.class, Boolean.class);
}
@Override
public Object convert(Object fromObject) {
if (!(fromObject instanceof ApplicationScale)) {
return null;
}
switch ((ApplicationScale) fromObject) {
case SCALE:
return Boolean.TRUE;
default:
return Boolean.FALSE;
}
}
}
private final class BooleanToApplicationScaleConverter extends Converter {
private BooleanToApplicationScaleConverter() {
super(Boolean.class, ApplicationScale.class);
}
@Override
public Object convert(Object fromObject) {
if (fromObject instanceof Boolean) {
return ((Boolean) fromObject).booleanValue() ? ApplicationScale.SCALE : ApplicationScale.NO_SCALE;
}
return null;
}
}
/**
* Viewer element comparer based on #equals(). The default implementation in
* CheckboxTableViewer compares elements based on instance identity.
* <p>
* We need this since the available cartridges (item listed in the viewer)
* are not the same instance as the ones in the embedded application (items
* to check in the viewer).
*/
private static class EqualityComparer implements IElementComparer {
@Override
public boolean equals(Object thisObject, Object thatObject) {
if (thisObject == null) {
return thatObject != null;
}
if (thatObject == null) {
return false;
}
return thisObject.equals(thatObject);
}
@Override
public int hashCode(Object element) {
return element.hashCode();
}