Skip to content

Commit 2b42ceb

Browse files
committed
fixing 'edit host configuration' menu item (#1456)
* fixing 'edit host configuration' menu item - due to listeners on object being disposed of when menu not open * review comments * further review comments, remove editBlockAction as instance var, return from createEditBlockLabelAndAction (cherry picked from commit f05f3dc)
1 parent e3a2e95 commit 2b42ceb

File tree

1 file changed

+18
-34
lines changed
  • base/uk.ac.stfc.isis.ibex.ui.blocks/src/uk/ac/stfc/isis/ibex/ui/blocks/groups

1 file changed

+18
-34
lines changed

base/uk.ac.stfc.isis.ibex.ui.blocks/src/uk/ac/stfc/isis/ibex/ui/blocks/groups/BlocksMenu.java

Lines changed: 18 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,10 @@
3232
import org.eclipse.jface.action.IMenuListener;
3333
import org.eclipse.jface.action.IMenuManager;
3434
import org.eclipse.jface.action.MenuManager;
35-
import org.eclipse.swt.widgets.Display;
36-
import org.eclipse.swt.widgets.Item;
37-
3835
import uk.ac.stfc.isis.ibex.configserver.Configurations;
3936
import uk.ac.stfc.isis.ibex.configserver.displaying.DisplayBlock;
4037
import uk.ac.stfc.isis.ibex.e4.ui.perspectiveswitcher.PerspectivesProvider;
4138
import uk.ac.stfc.isis.ibex.e4.ui.perspectiveswitcher.views.PerspectiveSwitcherView;
42-
import uk.ac.stfc.isis.ibex.epics.writing.OnCanWriteChangeListener;
4339
import uk.ac.stfc.isis.ibex.ui.blocks.presentation.Presenter;
4440
import uk.ac.stfc.isis.ibex.ui.blocks.views.BlocksView;
4541
import uk.ac.stfc.isis.ibex.ui.configserver.commands.EditBlockHandler;
@@ -58,31 +54,17 @@ public class BlocksMenu extends MenuManager {
5854
private static final String CONFIGURATION_SUFFIX = "configuration";
5955
private static final String DISPLAY_BLOCK_HISTORY = "Display block history...";
6056
private static final String LOGPLOTTER_ID = "uk.ac.stfc.isis.ibex.client.e4.product.perspective.logplotter";
57+
58+
private static boolean canWrite = false;
6159

62-
private IAction editBlockAction;
6360
private MenuManager logSubMenu;
6461
private MenuManager noLogPlotterSubMenu;
6562

66-
67-
/**
68-
* This is an inner anonymous class inherited from SameTypeWriter with added functionality
69-
* for modifying the command if the underlying configuration PV cannot be written to.
70-
*/
71-
protected final OnCanWriteChangeListener readOnlyListener = canWrite -> Display.getDefault().asyncExec(() -> {
72-
if (canWrite) {
73-
boolean buttonPresent = false;
74-
for (Item item : getMenuItems()) {
75-
if (item.getText().contains(EDIT_BLOCK_PREFIX)) {
76-
buttonPresent = true;
77-
}
78-
}
79-
if (find(editBlockAction.getId()) == null && buttonPresent) {
80-
appendToGroup(BLOCK_MENU_GROUP, editBlockAction);
81-
}
82-
} else {
83-
remove(editBlockAction.getId());
84-
}
85-
});
63+
static {
64+
// Set a listener for the edit host configuration/component menu item based on server write access status.
65+
Configurations.getInstance().server().setCurrentConfig().addOnCanWriteChangeListener(canWrite -> BlocksMenu.canWrite = canWrite);
66+
}
67+
8668

8769
private IAction createAddToPlotAction(String plotName) {
8870
return new Action("Add to new axis") {
@@ -116,8 +98,6 @@ public void run() {
11698
public BlocksMenu(DisplayBlock displayBlock) {
11799
this.block = displayBlock;
118100

119-
Configurations.getInstance().server().setCurrentConfig().addOnCanWriteChangeListener(readOnlyListener);
120-
121101
add(new GroupMarker(BLOCK_MENU_GROUP));
122102

123103
noLogPlotterSubMenu = new MenuManager(DISPLAY_BLOCK_HISTORY);
@@ -160,6 +140,10 @@ public void menuAboutToShow(IMenuManager manager) {
160140

161141
appendToGroup(BLOCK_MENU_GROUP, logSubMenu);
162142

143+
final var editBlockAction = createEditBlockLabelAndAction();
144+
145+
appendToGroup(BLOCK_MENU_GROUP, editBlockAction);
146+
163147
this.addMenuListener(new IMenuListener() {
164148
@Override
165149
public void menuAboutToShow(IMenuManager manager) {
@@ -170,16 +154,21 @@ public void menuAboutToShow(IMenuManager manager) {
170154
logSubMenu.setVisible(false);
171155
noLogPlotterSubMenu.setVisible(true);
172156
}
157+
editBlockAction.setEnabled(canWrite);
173158
updateAll(true);
174159
}
175160
});
176-
String editBlockLabel = EDIT_BLOCK_PREFIX;
161+
}
162+
163+
private IAction createEditBlockLabelAndAction() {
164+
String editBlockLabel = EDIT_BLOCK_PREFIX;
177165
if (this.block.inComponent()) {
178166
editBlockLabel += COMPONENT_SUFFIX;
179167
} else {
180168
editBlockLabel += CONFIGURATION_SUFFIX;
181169
}
182-
editBlockAction = new Action(editBlockLabel) {
170+
171+
return new Action(editBlockLabel) {
183172
@Override
184173
public void run() {
185174
new EditBlockHandler(block.getName()).execute(null); //TODO e4 migrate: This will be added as a command which includes a shell at that time make this correct
@@ -204,9 +193,4 @@ public static boolean canAddPlot() {
204193
}
205194
return false;
206195
}
207-
208-
@Override
209-
protected void finalize() {
210-
Configurations.getInstance().server().setCurrentConfig().removeOnCanWriteChangeListener(readOnlyListener);
211-
}
212196
}

0 commit comments

Comments
 (0)