-
Notifications
You must be signed in to change notification settings - Fork 3.3k
/
ResultSetViewer.java
4472 lines (4001 loc) · 179 KB
/
ResultSetViewer.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
/*
* DBeaver - Universal Database Manager
* Copyright (C) 2010-2019 Serge Rider (serge@jkiss.org)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jkiss.dbeaver.ui.controls.resultset;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.jobs.IJobChangeEvent;
import org.eclipse.core.runtime.jobs.JobChangeAdapter;
import org.eclipse.jface.action.*;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.IDialogSettings;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.resource.StringConverter;
import org.eclipse.jface.text.IFindReplaceTarget;
import org.eclipse.jface.viewers.*;
import org.eclipse.osgi.util.NLS;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.*;
import org.eclipse.swt.events.FocusEvent;
import org.eclipse.swt.events.FocusListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.layout.RowData;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.*;
import org.eclipse.ui.ISaveablePart2;
import org.eclipse.ui.IWorkbenchActionConstants;
import org.eclipse.ui.IWorkbenchCommandConstants;
import org.eclipse.ui.IWorkbenchPartSite;
import org.eclipse.ui.actions.CompoundContributionItem;
import org.eclipse.ui.internal.ide.IDEWorkbenchMessages;
import org.eclipse.ui.menus.CommandContributionItem;
import org.eclipse.ui.menus.CommandContributionItemParameter;
import org.eclipse.ui.menus.IMenuService;
import org.eclipse.ui.texteditor.ITextEditorActionDefinitionIds;
import org.jkiss.code.NotNull;
import org.jkiss.code.Nullable;
import org.jkiss.dbeaver.DBException;
import org.jkiss.dbeaver.Log;
import org.jkiss.dbeaver.ModelPreferences;
import org.jkiss.dbeaver.model.*;
import org.jkiss.dbeaver.model.data.*;
import org.jkiss.dbeaver.model.edit.DBEPersistAction;
import org.jkiss.dbeaver.model.exec.*;
import org.jkiss.dbeaver.model.impl.AbstractExecutionSource;
import org.jkiss.dbeaver.model.impl.local.StatResultSet;
import org.jkiss.dbeaver.model.messages.ModelMessages;
import org.jkiss.dbeaver.model.preferences.DBPPreferenceStore;
import org.jkiss.dbeaver.model.runtime.DBRProgressMonitor;
import org.jkiss.dbeaver.model.runtime.DBRRunnableWithProgress;
import org.jkiss.dbeaver.model.runtime.VoidProgressMonitor;
import org.jkiss.dbeaver.model.runtime.load.DatabaseLoadService;
import org.jkiss.dbeaver.model.runtime.load.ILoadService;
import org.jkiss.dbeaver.model.sql.DBSQLException;
import org.jkiss.dbeaver.model.sql.SQLQueryContainer;
import org.jkiss.dbeaver.model.sql.SQLUtils;
import org.jkiss.dbeaver.model.struct.*;
import org.jkiss.dbeaver.model.virtual.*;
import org.jkiss.dbeaver.runtime.DBWorkbench;
import org.jkiss.dbeaver.runtime.DBeaverNotifications;
import org.jkiss.dbeaver.runtime.jobs.DataSourceJob;
import org.jkiss.dbeaver.tools.transfer.registry.DataTransferNodeDescriptor;
import org.jkiss.dbeaver.tools.transfer.registry.DataTransferProcessorDescriptor;
import org.jkiss.dbeaver.tools.transfer.registry.DataTransferRegistry;
import org.jkiss.dbeaver.ui.*;
import org.jkiss.dbeaver.ui.controls.ToolbarSeparatorContribution;
import org.jkiss.dbeaver.ui.controls.VerticalButton;
import org.jkiss.dbeaver.ui.controls.VerticalFolder;
import org.jkiss.dbeaver.ui.controls.autorefresh.AutoRefreshControl;
import org.jkiss.dbeaver.ui.controls.resultset.internal.ResultSetMessages;
import org.jkiss.dbeaver.ui.controls.resultset.panel.ResultSetPanelDescriptor;
import org.jkiss.dbeaver.ui.controls.resultset.valuefilter.FilterValueEditDialog;
import org.jkiss.dbeaver.ui.controls.resultset.valuefilter.FilterValueEditPopup;
import org.jkiss.dbeaver.ui.controls.resultset.view.EmptyPresentation;
import org.jkiss.dbeaver.ui.controls.resultset.view.ErrorPresentation;
import org.jkiss.dbeaver.ui.controls.resultset.view.StatisticsPresentation;
import org.jkiss.dbeaver.ui.css.CSSUtils;
import org.jkiss.dbeaver.ui.css.DBStyles;
import org.jkiss.dbeaver.ui.data.IValueController;
import org.jkiss.dbeaver.ui.dialogs.ConfirmationDialog;
import org.jkiss.dbeaver.ui.editors.data.internal.DataEditorsMessages;
import org.jkiss.dbeaver.ui.editors.data.preferences.PrefPageDataFormat;
import org.jkiss.dbeaver.ui.editors.data.preferences.PrefPageResultSetMain;
import org.jkiss.dbeaver.ui.editors.object.struct.EditConstraintPage;
import org.jkiss.dbeaver.ui.editors.object.struct.EditDictionaryPage;
import org.jkiss.dbeaver.ui.navigator.NavigatorCommands;
import org.jkiss.dbeaver.utils.GeneralUtils;
import org.jkiss.dbeaver.utils.PrefUtils;
import org.jkiss.dbeaver.utils.RuntimeUtils;
import org.jkiss.utils.ArrayUtils;
import org.jkiss.utils.CommonUtils;
import java.lang.reflect.InvocationTargetException;
import java.text.DecimalFormat;
import java.util.*;
import java.util.List;
/**
* ResultSetViewer
*
* TODO: not-editable cells (struct owners in record mode)
* TODO: PROBLEM. Multiple occurrences of the same struct type in a single table.
* Need to make wrapper over DBSAttributeBase or something. Or maybe it is not a problem
* because we search for binding by attribute only in constraints and for unique key columns which are unique?
* But what PK has struct type?
*
*/
public class ResultSetViewer extends Viewer
implements DBPContextProvider, IResultSetController, ISaveablePart2, IAdaptable
{
private static final Log log = Log.getLog(ResultSetViewer.class);
private static final String TOOLBAR_GROUP_NAVIGATION = "navigation";
private static final String TOOLBAR_GROUP_PRESENTATIONS = "presentations";
private static final String TOOLBAR_GROUP_ADDITIONS = IWorkbenchActionConstants.MB_ADDITIONS;
private static final String SETTINGS_SECTION_PRESENTATIONS = "presentations";
private static final String TOOLBAR_CONTRIBUTION_ID = "toolbar:org.jkiss.dbeaver.ui.controls.resultset.status";
static final String EMPTY_TRANSFORMER_NAME = "Default";
static final String CONTROL_ID = ResultSetViewer.class.getSimpleName();
public static final String DEFAULT_QUERY_TEXT = "SQL";
private static final DecimalFormat ROW_COUNT_FORMAT = new DecimalFormat("###,###,###,###,###,##0");
private static final IResultSetListener[] EMPTY_LISTENERS = new IResultSetListener[0];
private IResultSetFilterManager filterManager;
@NotNull
private final IWorkbenchPartSite site;
private final Composite mainPanel;
private final Composite viewerPanel;
private final IResultSetDecorator decorator;
@Nullable
private ResultSetFilterPanel filtersPanel;
private SashForm viewerSash;
private final VerticalFolder panelSwitchFolder;
private CTabFolder panelFolder;
private ToolBarManager panelToolBar;
private final VerticalFolder presentationSwitchFolder;
private final Composite presentationPanel;
private final List<ToolBarManager> toolbarList = new ArrayList<>();
private Composite statusBar;
private StatusLabel statusLabel;
private ActiveStatusMessage rowCountLabel;
private Text resultSetSize;
private final DynamicFindReplaceTarget findReplaceTarget;
// Presentation
private IResultSetPresentation activePresentation;
private ResultSetPresentationDescriptor activePresentationDescriptor;
private List<ResultSetPresentationDescriptor> availablePresentations;
private final List<ResultSetPanelDescriptor> availablePanels = new ArrayList<>();
private final Map<ResultSetPresentationDescriptor, PresentationSettings> presentationSettings = new HashMap<>();
private final Map<String, IResultSetPanel> activePanels = new HashMap<>();
private final Map<String, ToolBarManager> activeToolBars = new HashMap<>();
@NotNull
private final IResultSetContainer container;
@NotNull
private final ResultSetDataReceiver dataReceiver;
// Current row/col number
@Nullable
private ResultSetRow curRow;
// Mode
private boolean recordMode;
private final List<IResultSetListener> listeners = new ArrayList<>();
private volatile ResultSetJobDataRead dataPumpJob;
private final ResultSetModel model = new ResultSetModel();
private HistoryStateItem curState = null;
private final List<HistoryStateItem> stateHistory = new ArrayList<>();
private int historyPosition = -1;
private AutoRefreshControl autoRefreshControl;
private boolean actionsDisabled;
private Color defaultBackground, defaultForeground;
private VerticalButton recordModeButton;
public ResultSetViewer(@NotNull Composite parent, @NotNull IWorkbenchPartSite site, @NotNull IResultSetContainer container)
{
super();
this.site = site;
this.recordMode = false;
this.container = container;
this.decorator = container.createResultSetDecorator();
this.dataReceiver = new ResultSetDataReceiver(this);
this.filterManager = GeneralUtils.adapt(this, IResultSetFilterManager.class);
if (this.filterManager == null) {
this.filterManager = new SimpleFilterManager();
}
loadPresentationSettings();
this.defaultBackground = UIStyles.getDefaultTextBackground();
this.defaultForeground = UIStyles.getDefaultTextForeground();
boolean supportsPanels = (decorator.getDecoratorFeatures() & IResultSetDecorator.FEATURE_PANELS) != 0;
this.mainPanel = UIUtils.createPlaceholder(parent, supportsPanels ? 3 : 2);
this.autoRefreshControl = new AutoRefreshControl(
this.mainPanel, ResultSetViewer.class.getSimpleName(), monitor -> refreshData(null));
if ((decorator.getDecoratorFeatures() & IResultSetDecorator.FEATURE_FILTERS) != 0) {
this.filtersPanel = new ResultSetFilterPanel(this, this.mainPanel);
GridData gd = new GridData(GridData.FILL_HORIZONTAL);
gd.horizontalSpan = ((GridLayout)mainPanel.getLayout()).numColumns;
this.filtersPanel.setLayoutData(gd);
}
this.presentationSwitchFolder = new VerticalFolder(mainPanel, SWT.LEFT);
this.presentationSwitchFolder.setLayoutData(new GridData(GridData.FILL_VERTICAL));
CSSUtils.setCSSClass(this.presentationSwitchFolder, DBStyles.COLORED_BY_CONNECTION_TYPE);
this.viewerPanel = UIUtils.createPlaceholder(mainPanel, 1);
this.viewerPanel.setLayoutData(new GridData(GridData.FILL_BOTH));
this.viewerPanel.setData(CONTROL_ID, this);
CSSUtils.setCSSClass(this.viewerPanel, DBStyles.COLORED_BY_CONNECTION_TYPE);
UIUtils.setHelp(this.viewerPanel, IHelpContextIds.CTX_RESULT_SET_VIEWER);
this.viewerPanel.setRedraw(false);
if (supportsPanels) {
this.panelSwitchFolder = new VerticalFolder(mainPanel, SWT.RIGHT);
this.panelSwitchFolder.setLayoutData(new GridData(GridData.FILL_VERTICAL));
CSSUtils.setCSSClass(this.panelSwitchFolder, DBStyles.COLORED_BY_CONNECTION_TYPE);
} else {
panelSwitchFolder = null;
}
try {
this.findReplaceTarget = new DynamicFindReplaceTarget();
this.viewerSash = UIUtils.createPartDivider(site.getPart(), this.viewerPanel, SWT.HORIZONTAL | SWT.SMOOTH);
this.viewerSash.setLayoutData(new GridData(GridData.FILL_BOTH));
this.presentationPanel = UIUtils.createPlaceholder(this.viewerSash, 1);
this.presentationPanel.setLayoutData(new GridData(GridData.FILL_BOTH));
if (supportsPanels) {
this.panelFolder = new CTabFolder(this.viewerSash, SWT.FLAT | SWT.TOP);
CSSUtils.setCSSClass(panelFolder, DBStyles.COLORED_BY_CONNECTION_TYPE);
this.panelFolder.marginWidth = 0;
this.panelFolder.marginHeight = 0;
this.panelFolder.setMinimizeVisible(true);
this.panelFolder.setMRUVisible(true);
this.panelFolder.setLayoutData(new GridData(GridData.FILL_BOTH));
this.panelFolder.addListener(SWT.MouseDoubleClick, event -> {
if (event.button != 1) {
return;
}
CTabItem selectedItem = panelFolder.getItem(new Point(event.getBounds().x, event.getBounds().y));
if (selectedItem != null && selectedItem == panelFolder.getSelection()) {
togglePanelsMaximize();
}
});
this.panelToolBar = new ToolBarManager(SWT.HORIZONTAL | SWT.RIGHT | SWT.FLAT);
ToolBar panelToolbarControl = this.panelToolBar.createControl(panelFolder);
this.panelFolder.setTopRight(panelToolbarControl, SWT.RIGHT | SWT.WRAP);
this.panelFolder.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
CTabItem activeTab = panelFolder.getSelection();
if (activeTab != null) {
setActivePanel((String) activeTab.getData());
}
}
});
this.panelFolder.addListener(SWT.Resize, event -> {
if (!viewerSash.isDisposed()) {
int[] weights = viewerSash.getWeights();
getPresentationSettings().panelRatio = weights[1];
}
});
this.panelFolder.addCTabFolder2Listener(new CTabFolder2Adapter() {
@Override
public void close(CTabFolderEvent event) {
CTabItem item = (CTabItem) event.item;
String panelId = (String) item.getData();
removePanel(panelId);
}
@Override
public void minimize(CTabFolderEvent event) {
showPanels(false, true, true);
}
@Override
public void maximize(CTabFolderEvent event) {
}
});
MenuManager panelsMenuManager = new MenuManager();
panelsMenuManager.setRemoveAllWhenShown(true);
panelsMenuManager.addMenuListener(manager -> {
for (IContributionItem menuItem : fillPanelsMenu()) {
panelsMenuManager.add(menuItem);
}
});
Menu panelsMenu = panelsMenuManager.createContextMenu(this.panelFolder);
this.panelFolder.setMenu(panelsMenu);
this.panelFolder.addDisposeListener(e -> panelsMenuManager.dispose());
}
showEmptyPresentation();
if (supportsStatusBar()) {
createStatusBar();
}
this.viewerPanel.addDisposeListener(e -> dispose());
changeMode(false);
} finally {
this.viewerPanel.setRedraw(true);
}
updateFiltersText();
}
@Override
@NotNull
public IResultSetContainer getContainer() {
return container;
}
@NotNull
@Override
public IResultSetDecorator getDecorator() {
return decorator;
}
AutoRefreshControl getAutoRefresh() {
return autoRefreshControl;
}
////////////////////////////////////////////////////////////
// Filters
private boolean supportsPanels() {
return (decorator.getDecoratorFeatures() & IResultSetDecorator.FEATURE_PANELS) != 0 &&
activePresentationDescriptor != null &&
activePresentationDescriptor.supportsPanels();
}
private boolean supportsStatusBar() {
return (decorator.getDecoratorFeatures() & IResultSetDecorator.FEATURE_STATUS_BAR) != 0;
}
boolean supportsDataFilter() {
DBSDataContainer dataContainer = getDataContainer();
return dataContainer != null &&
(dataContainer.getSupportedFeatures() & DBSDataContainer.DATA_FILTER) == DBSDataContainer.DATA_FILTER;
}
boolean supportsNavigation() {
return activePresentationDescriptor != null && activePresentationDescriptor.supportsNavigation();
}
boolean supportsEdit() {
return activePresentationDescriptor != null && activePresentationDescriptor.supportsEdit();
}
public void resetDataFilter(boolean refresh)
{
setDataFilter(model.createDataFilter(), refresh);
}
void saveDataFilter() {
DBCExecutionContext context = getExecutionContext();
if (context == null) {
log.error("Can't save data filter with null context");
return;
}
DataFilterRegistry.getInstance().saveDataFilter(getDataContainer(), model.getDataFilter());
DBeaverNotifications.showNotification(DBeaverNotifications.NT_GENERAL,
"Data filter was saved",
filtersPanel.getFilterText(),
DBPMessageType.INFORMATION, null);
}
void switchFilterFocus() {
boolean filterFocused = filtersPanel.getEditControl().isFocusControl();
if (filterFocused) {
if (activePresentation != null) {
activePresentation.getControl().setFocus();
}
} else {
filtersPanel.getEditControl().setFocus();
}
}
private void updateFiltersText()
{
updateFiltersText(true);
}
public void updateFiltersText(boolean resetFilterValue)
{
if (filtersPanel == null || this.viewerPanel.isDisposed()) {
return;
}
this.viewerPanel.setRedraw(false);
try {
boolean enableFilters = false;
DBCExecutionContext context = getExecutionContext();
if (context != null) {
if (activePresentation instanceof StatisticsPresentation) {
enableFilters = false;
} else {
StringBuilder where = new StringBuilder();
SQLUtils.appendConditionString(model.getDataFilter(), context.getDataSource(), null, where, true);
String whereCondition = where.toString().trim();
if (resetFilterValue) {
filtersPanel.setFilterValue(whereCondition);
if (!whereCondition.isEmpty()) {
filtersPanel.addFiltersHistory(whereCondition);
}
}
if (container.isReadyToRun() && !model.isUpdateInProgress()) {
enableFilters = true;
}
}
}
filtersPanel.enableFilters(enableFilters);
//presentationSwitchToolbar.setEnabled(enableFilters);
} finally {
this.viewerPanel.setRedraw(true);
}
}
public void setDataFilter(final DBDDataFilter dataFilter, boolean refreshData)
{
//if (!model.getDataFilter().equals(dataFilter))
{
//model.setDataFilter(dataFilter);
if (refreshData) {
refreshWithFilter(dataFilter);
} else {
model.setDataFilter(dataFilter);
activePresentation.refreshData(true, false, true);
updateFiltersText();
}
}
}
////////////////////////////////////////////////////////////
// Misc
@NotNull
public DBPPreferenceStore getPreferenceStore()
{
DBCExecutionContext context = getExecutionContext();
if (context != null) {
return context.getDataSource().getContainer().getPreferenceStore();
}
return DBWorkbench.getPlatform().getPreferenceStore();
}
@NotNull
@Override
public Color getDefaultBackground() {
if (filtersPanel == null) {
return defaultBackground;
}
return UIStyles.getDefaultTextBackground();
}
@NotNull
@Override
public Color getDefaultForeground() {
if (filtersPanel == null) {
return defaultForeground;
}
return UIStyles.getDefaultTextForeground();
}
void persistConfig() {
DBCExecutionContext context = getExecutionContext();
if (context != null) {
context.getDataSource().getContainer().persistConfiguration();
}
}
////////////////////////////////////////
// Presentation & panels
public List<ResultSetPresentationDescriptor> getAvailablePresentations() {
return availablePresentations;
}
@Override
@NotNull
public IResultSetPresentation getActivePresentation() {
return activePresentation;
}
@Override
public void showEmptyPresentation() {
activePresentationDescriptor = null;
setActivePresentation(new EmptyPresentation());
updatePresentationInToolbar();
}
void showErrorPresentation(String sqlText, String message, Throwable error) {
activePresentationDescriptor = null;
setActivePresentation(
new ErrorPresentation(
sqlText,
GeneralUtils.makeErrorStatus(message, error)));
updatePresentationInToolbar();
}
void updatePresentation(final DBCResultSet resultSet, boolean metadataChanged) {
if (getControl().isDisposed()) {
return;
}
boolean changed = false;
try {
if (resultSet instanceof StatResultSet) {
// Statistics - let's use special presentation for it
availablePresentations = Collections.emptyList();
setActivePresentation(new StatisticsPresentation());
activePresentationDescriptor = null;
changed = true;
} else {
// Regular results
IResultSetContext context = new ResultSetContextImpl(this, resultSet);
final List<ResultSetPresentationDescriptor> newPresentations = ResultSetPresentationRegistry.getInstance().getAvailablePresentations(resultSet, context);
changed = CommonUtils.isEmpty(this.availablePresentations) || !newPresentations.equals(this.availablePresentations);
this.availablePresentations = newPresentations;
if (!this.availablePresentations.isEmpty()) {
if (activePresentationDescriptor != null && (!metadataChanged || activePresentationDescriptor.getPresentationType().isPersistent())) {
if (this.availablePresentations.contains(activePresentationDescriptor)) {
// Keep the same presentation
return;
}
}
String defaultPresentationId = getPreferenceStore().getString(ResultSetPreferences.RESULT_SET_PRESENTATION);
ResultSetPresentationDescriptor newPresentation = null;
if (!CommonUtils.isEmpty(defaultPresentationId)) {
for (ResultSetPresentationDescriptor pd : this.availablePresentations) {
if (pd.getId().equals(defaultPresentationId)) {
newPresentation = pd;
break;
}
}
}
changed = true;
if (newPresentation == null) {
newPresentation = this.availablePresentations.get(0);
}
try {
IResultSetPresentation instance = newPresentation.createInstance();
activePresentationDescriptor = newPresentation;
setActivePresentation(instance);
} catch (Throwable e) {
DBWorkbench.getPlatformUI().showError("Presentation activate", "Can't instantiate data view '" + newPresentation.getLabel() + "'", e);
}
}
}
} finally {
if (changed && presentationSwitchFolder != null) {
updatePresentationInToolbar();
}
}
}
private void updatePresentationInToolbar() {
// Update combo
viewerPanel.setRedraw(false);
try {
boolean pVisible = activePresentationDescriptor != null;
((GridData) presentationSwitchFolder.getLayoutData()).exclude = !pVisible;
presentationSwitchFolder.setVisible(pVisible);
if (!pVisible) {
presentationSwitchFolder.setEnabled(false);
} else {
presentationSwitchFolder.setEnabled(true);
for (Control item : presentationSwitchFolder.getChildren()) {
item.dispose();
}
for (ResultSetPresentationDescriptor pd : availablePresentations) {
VerticalButton item = new VerticalButton(presentationSwitchFolder, SWT.LEFT | SWT.RADIO);
item.setImage(DBeaverIcons.getImage(pd.getIcon()));
item.setText(pd.getLabel());
item.setToolTipText(pd.getDescription());
item.setData(pd);
if (pd == activePresentationDescriptor) {
presentationSwitchFolder.setSelection(item);
}
item.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
if (e.widget != null && e.widget.getData() != null) {
switchPresentation((ResultSetPresentationDescriptor) e.widget.getData());
}
}
});
}
UIUtils.createEmptyLabel(presentationSwitchFolder, 1, 1).setLayoutData(new GridData(GridData.FILL_VERTICAL));
recordModeButton = new VerticalButton(presentationSwitchFolder, SWT.LEFT | SWT.CHECK);
recordModeButton.setAction(new ToggleModeAction(), true);
if (statusBar != null) {
((GridLayout) presentationSwitchFolder.getLayout()).marginBottom = statusBar.getSize().y;
}
}
mainPanel.layout(true, true);
} catch (Exception e) {
log.debug("Error updating presentation toolbar", e);
} finally {
// Enable redraw
viewerPanel.setRedraw(true);
}
}
private void setActivePresentation(@NotNull IResultSetPresentation presentation) {
boolean focusInPresentation = UIUtils.isParent(presentationPanel, viewerPanel.getDisplay().getFocusControl());
// Dispose previous presentation and panels
UIUtils.disposeChildControls(presentationPanel);
if (panelFolder != null) {
CTabItem curItem = panelFolder.getSelection();
for (CTabItem panelItem : panelFolder.getItems()) {
if (panelItem != curItem) {
panelItem.dispose();
}
}
if (curItem != null) {
curItem.dispose();
}
}
// Set new presentation
activePresentation = presentation;
availablePanels.clear();
activePanels.clear();
if (activePresentationDescriptor != null) {
availablePanels.addAll(ResultSetPresentationRegistry.getInstance().getSupportedPanels(
getDataSource(), activePresentationDescriptor.getId(), activePresentationDescriptor.getPresentationType()));
} else if (activePresentation instanceof StatisticsPresentation) {
// Stats presentation
availablePanels.addAll(ResultSetPresentationRegistry.getInstance().getSupportedPanels(
getDataSource(), null, IResultSetPresentation.PresentationType.COLUMNS));
}
activePresentation.createPresentation(this, presentationPanel);
// Clear panels toolbar
if (panelSwitchFolder != null) {
UIUtils.disposeChildControls(panelSwitchFolder);
}
// Activate panels
if (supportsPanels()) {
boolean panelsVisible = false;
boolean verticalLayout = false;
int[] panelWeights = new int[]{700, 300};
if (activePresentationDescriptor != null) {
PresentationSettings settings = getPresentationSettings();
panelsVisible = settings.panelsVisible;
verticalLayout = settings.verticalLayout;
if (settings.panelRatio > 0) {
panelWeights = new int[] {1000 - settings.panelRatio, settings.panelRatio};
}
activateDefaultPanels(settings);
}
viewerSash.setOrientation(verticalLayout ? SWT.VERTICAL : SWT.HORIZONTAL);
viewerSash.setWeights(panelWeights);
showPanels(panelsVisible, false, false);
if (!availablePanels.isEmpty()) {
VerticalButton panelsButton = new VerticalButton(panelSwitchFolder, SWT.RIGHT | SWT.CHECK);
{
panelsButton.setText(ResultSetMessages.controls_resultset_config_panels);
panelsButton.setImage(DBeaverIcons.getImage(UIIcon.PANEL_CUSTOMIZE));
panelsButton.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
showPanels(!isPanelsVisible(), true, true);
panelsButton.setChecked(isPanelsVisible());
updatePanelsButtons();
}
});
String toolTip = ActionUtils.findCommandDescription(ResultSetHandlerMain.CMD_TOGGLE_PANELS, getSite(), false);
if (!CommonUtils.isEmpty(toolTip)) {
panelsButton.setToolTipText(toolTip);
}
panelsButton.setChecked(panelsVisible);
}
// Add all panels
for (final ResultSetPanelDescriptor panel : availablePanels) {
VerticalButton panelButton = new VerticalButton(panelSwitchFolder, SWT.RIGHT | SWT.CHECK);
GridData gd = new GridData(GridData.VERTICAL_ALIGN_BEGINNING);
gd.verticalIndent = 2;
gd.horizontalIndent = 1;
panelButton.setLayoutData(gd);
panelButton.setData(panel);
panelButton.setImage(DBeaverIcons.getImage(panel.getIcon()));
panelButton.setToolTipText(panel.getLabel());
String toolTip = ActionUtils.findCommandDescription(
ResultSetHandlerTogglePanel.CMD_TOGGLE_PANEL, getSite(), true,
ResultSetHandlerTogglePanel.PARAM_PANEL_ID, panel.getId());
if (!CommonUtils.isEmpty(toolTip)) {
panelButton.setToolTipText(panel.getLabel() + " (" + toolTip + ")");
}
panelButton.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
boolean isPanelVisible = isPanelsVisible() && isPanelVisible(panel.getId());
if (isPanelVisible) {
closePanel(panel.getId());
} else {
activatePanel(panel.getId(), true, true);
}
panelButton.setChecked(!isPanelVisible);
panelsButton.setChecked(isPanelsVisible());
panelSwitchFolder.redraw();
}
});
panelButton.setChecked(panelsVisible && isPanelVisible(panel.getId()));
}
UIUtils.createEmptyLabel(panelSwitchFolder, 1, 1).setLayoutData(new GridData(GridData.FILL_VERTICAL));
VerticalButton.create(panelSwitchFolder, SWT.RIGHT | SWT.CHECK, getSite(), ResultSetHandlerMain.CMD_TOGGLE_LAYOUT, false);
}
} else {
if (viewerSash != null) {
viewerSash.setMaximizedControl(viewerSash.getChildren()[0]);
}
}
mainPanel.layout(true, true);
if (recordModeButton != null) {
recordModeButton.setVisible(activePresentationDescriptor != null && activePresentationDescriptor.supportsRecordMode());
}
// Update dynamic find/replace target
{
IFindReplaceTarget nested = null;
if (presentation instanceof IAdaptable) {
nested = ((IAdaptable) presentation).getAdapter(IFindReplaceTarget.class);
}
findReplaceTarget.setTarget(nested);
}
if (!toolbarList.isEmpty()) {
for (ToolBarManager tb : toolbarList) {
tb.update(true);
}
}
// Listen presentation selection change
if (presentation instanceof ISelectionProvider) {
((ISelectionProvider) presentation).addSelectionChangedListener(this::fireResultSetSelectionChange);
}
// Set focus in presentation control
// Use async exec to avoid focus switch after user UI interaction (e.g. combo)
if (focusInPresentation) {
UIUtils.asyncExec(() -> {
Control control = activePresentation.getControl();
if (control != null && !control.isDisposed()) {
control.setFocus();
}
});
}
}
private void updatePanelsButtons() {
boolean panelsVisible = isPanelsVisible();
for (Control child : panelSwitchFolder.getChildren()) {
if (child instanceof VerticalButton && child.getData() instanceof ResultSetPanelDescriptor) {
boolean newChecked = panelsVisible &&
isPanelVisible(((ResultSetPanelDescriptor) child.getData()).getId());
if (((VerticalButton) child).isChecked() != newChecked) {
((VerticalButton) child).setChecked(newChecked);
child.redraw();
}
}
}
}
/**
* Switch to the next presentation
*/
void switchPresentation() {
if (availablePresentations.size() < 2) {
return;
}
int index = availablePresentations.indexOf(activePresentationDescriptor);
if (index < availablePresentations.size() - 1) {
index++;
} else {
index = 0;
}
switchPresentation(availablePresentations.get(index));
}
public void switchPresentation(ResultSetPresentationDescriptor selectedPresentation) {
try {
IResultSetPresentation instance = selectedPresentation.createInstance();
activePresentationDescriptor = selectedPresentation;
setActivePresentation(instance);
instance.refreshData(true, false, false);
if (presentationSwitchFolder != null) {
for (VerticalButton item : presentationSwitchFolder.getItems()) {
if (item.getData() == activePresentationDescriptor) {
presentationSwitchFolder.setSelection(item);
break;
}
}
}
// Save in global preferences
if (activePresentationDescriptor.getPresentationType().isPersistent()) {
// Save current presentation (only if it is persistent)
DBWorkbench.getPlatform().getPreferenceStore().setValue(
ResultSetPreferences.RESULT_SET_PRESENTATION, activePresentationDescriptor.getId());
}
savePresentationSettings();
} catch (Throwable e1) {
DBWorkbench.getPlatformUI().showError(
"Presentation switch",
"Can't switch presentation",
e1);
}
}
private void loadPresentationSettings() {
IDialogSettings pSections = ResultSetUtils.getViewerSettings(SETTINGS_SECTION_PRESENTATIONS);
for (IDialogSettings pSection : ArrayUtils.safeArray(pSections.getSections())) {
String pId = pSection.getName();
ResultSetPresentationDescriptor presentation = ResultSetPresentationRegistry.getInstance().getPresentation(pId);
if (presentation == null) {
log.warn("Presentation '" + pId + "' not found. ");
continue;
}
PresentationSettings settings = new PresentationSettings();
String panelIdList = pSection.get("enabledPanelIds");
if (panelIdList != null) {
Collections.addAll(settings.enabledPanelIds, panelIdList.split(","));
}
settings.activePanelId = pSection.get("activePanelId");
settings.panelRatio = pSection.getInt("panelRatio");
settings.panelsVisible = pSection.getBoolean("panelsVisible");
settings.verticalLayout = pSection.getBoolean("verticalLayout");
presentationSettings.put(presentation, settings);
}
}
private PresentationSettings getPresentationSettings() {
PresentationSettings settings = this.presentationSettings.get(activePresentationDescriptor);
if (settings == null) {
settings = new PresentationSettings();
// By default panels are visible for column presentations
settings.panelsVisible = activePresentationDescriptor != null &&
(activePresentationDescriptor.getPresentationType() == IResultSetPresentation.PresentationType.COLUMNS);
this.presentationSettings.put(activePresentationDescriptor, settings);
}
return settings;
}
private void savePresentationSettings() {
if ((decorator.getDecoratorFeatures() & IResultSetDecorator.FEATURE_PANELS) != 0) {
IDialogSettings pSections = ResultSetUtils.getViewerSettings(SETTINGS_SECTION_PRESENTATIONS);
for (Map.Entry<ResultSetPresentationDescriptor, PresentationSettings> pEntry : presentationSettings.entrySet()) {
if (pEntry.getKey() == null) {
continue;
}
String pId = pEntry.getKey().getId();
PresentationSettings settings = pEntry.getValue();
IDialogSettings pSection = UIUtils.getSettingsSection(pSections, pId);
pSection.put("enabledPanelIds", CommonUtils.joinStrings(",", settings.enabledPanelIds));
pSection.put("activePanelId", settings.activePanelId);
pSection.put("panelRatio", settings.panelRatio);
pSection.put("panelsVisible", settings.panelsVisible);
pSection.put("verticalLayout", settings.verticalLayout);
}
}
}
@Override
public IResultSetPanel getVisiblePanel() {
return isPanelsVisible() ? activePanels.get(getPresentationSettings().activePanelId) : null;
}
String getActivePanelId() {
return getPresentationSettings().activePanelId;
}
void closeActivePanel() {
CTabItem activePanelItem = panelFolder.getSelection();
if (activePanelItem != null) {
activePanelItem.dispose();
}
if (panelFolder.getItemCount() <= 0) {
showPanels(false, true, true);
}
}
@Override
public IResultSetPanel[] getActivePanels() {
return activePanels.values().toArray(new IResultSetPanel[0]);
}
@Override
public boolean activatePanel(String id, boolean setActive, boolean showPanels) {
if (!supportsPanels()) {
return false;
}
if (showPanels && !isPanelsVisible()) {
showPanels(true, false, false);
}
PresentationSettings presentationSettings = getPresentationSettings();
IResultSetPanel panel = activePanels.get(id);
if (panel != null) {
CTabItem panelTab = getPanelTab(id);
if (panelTab != null) {
if (setActive) {
panelFolder.setSelection(panelTab);
presentationSettings.activePanelId = id;
}
return true;
} else {
log.debug("Panel '" + id + "' tab not found");
}
}
// Create panel
ResultSetPanelDescriptor panelDescriptor = getPanelDescriptor(id);
if (panelDescriptor == null) {
log.debug("Panel '" + id + "' not found");
return false;
}
try {
panel = panelDescriptor.createInstance();
} catch (DBException e) {
DBWorkbench.getPlatformUI().showError("Can't show panel", "Can't create panel '" + id + "'", e);
return false;
}
activePanels.put(id, panel);
// Create control and tab item
panelFolder.setRedraw(false);
try {
Control panelControl = panel.createContents(activePresentation, panelFolder);
boolean firstPanel = panelFolder.getItemCount() == 0;
CTabItem panelTab = new CTabItem(panelFolder, SWT.CLOSE);
panelTab.setData(id);
panelTab.setText(panelDescriptor.getLabel());
panelTab.setImage(DBeaverIcons.getImage(panelDescriptor.getIcon()));