Skip to content

Commit 85cfa2c

Browse files
author
kasemir
committed
NavTabs: Better handling of invalid active_tab
If the active tab is outside of the range of defined tabs, it would show disfunctional tab content without macros and log an ArrayIndexOutOfBounds exception. Now it will log something like "Widget 'XXXX' (navtabs) active tab 5 needs to be within 0 ... 4" and fall back to the highest valid tab index.
1 parent 14cf185 commit 85cfa2c

File tree

2 files changed

+18
-9
lines changed

2 files changed

+18
-9
lines changed

app/display/model/src/main/java/org/csstudio/display/builder/model/widgets/NavigationTabsWidget.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2017-2023 Oak Ridge National Laboratory.
2+
* Copyright (c) 2017-2025 Oak Ridge National Laboratory.
33
* All rights reserved. This program and the accompanying materials
44
* are made available under the terms of the Eclipse Public License v1.0
55
* which accompanies this distribution, and is available at
@@ -45,7 +45,6 @@
4545
/** Widget with tabs to select amongst several embedded displays
4646
* @author Kay Kasemir
4747
*/
48-
@SuppressWarnings("nls")
4948
public class NavigationTabsWidget extends VisibleWidget
5049
{
5150
/** Widget descriptor */
@@ -210,7 +209,7 @@ public Macros getEffectiveMacros()
210209
{
211210
final Macros base = super.getEffectiveMacros();
212211
// Join macros of active tab
213-
final int index;
212+
int index;
214213

215214
// To fetch the effective macros, we want to add those of the selected tab.
216215
// But if we get the tab index via active.getValue(), AND 'active' itself contains macros,
@@ -228,7 +227,13 @@ public Macros getEffectiveMacros()
228227
return base;
229228
}
230229

231-
if (index >= 0 && index < tabs.size())
230+
if (index >= tabs.size())
231+
{
232+
logger.log(Level.WARNING, this + " active tab " + index + " needs to be within 0 ... " + (tabs.size() - 1));
233+
index = tabs.size()-1;
234+
}
235+
236+
if (index >= 0)
232237
try
233238
{
234239
final Macros selected = tabs.getElement(index).macros().getValue();

app/display/representation-javafx/src/main/java/org/csstudio/display/builder/representation/javafx/widgets/NavigationTabsRepresentation.java

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2017-2018 Oak Ridge National Laboratory.
2+
* Copyright (c) 2017-2025 Oak Ridge National Laboratory.
33
* All rights reserved. This program and the accompanying materials
44
* are made available under the terms of the Eclipse Public License v1.0
55
* which accompanies this distribution, and is available at
@@ -51,13 +51,16 @@
5151
*
5252
* @author Kay Kasemir
5353
*/
54-
@SuppressWarnings("nls")
5554
public class NavigationTabsRepresentation extends RegionBaseRepresentation<NavigationTabs, NavigationTabsWidget>
5655
{
5756
private final DirtyFlag dirty_sizes = new DirtyFlag();
5857
private final DirtyFlag dirty_tabs = new DirtyFlag();
5958
private final DirtyFlag dirty_tab_look = new DirtyFlag();
6059
private final DirtyFlag dirty_active_tab = new DirtyFlag();
60+
61+
// Track the "active tab" of navigation tabs _inside_ this one.
62+
// As user toggles between tabs, this will allow restoring the active tab
63+
// of nested instances, which would otherwise start over at their default tab
6164
private class SelectedNavigationTabs extends MutablePair<Integer, HashMap<Pair<Integer, String>, HashMap<String, SelectedNavigationTabs>>> {
6265
public SelectedNavigationTabs(int activeTab) {
6366
left = activeTab;
@@ -244,9 +247,10 @@ private synchronized void updatePendingDisplay(final JobMonitor monitor)
244247
});
245248
checkCompletion(model_widget, completion, "timeout representing new content");
246249

247-
int tabNumber = model_widget.propActiveTab().getValue();
248-
String tabName = model_widget.propTabs().getValue().get(model_widget.propActiveTab().getValue()).name().getValue();
249-
Pair<Integer, String> tabNumberAndTabName = new Pair<>(tabNumber, tabName);
250+
final List<TabProperty> tabs = model_widget.propTabs().getValue();
251+
final int tabNumber = Math.min(model_widget.propActiveTab().getValue(), tabs.size()-1);
252+
final String tabName = tabs.get(tabNumber).name().getValue();
253+
final Pair<Integer, String> tabNumberAndTabName = new Pair<>(tabNumber, tabName);
250254

251255
if (!selectedNavigationTabs.right.containsKey(tabNumberAndTabName)) {
252256
selectedNavigationTabs.right.put(tabNumberAndTabName, new HashMap<>());

0 commit comments

Comments
 (0)