Skip to content

Commit 4fe5020

Browse files
committed
Makes the tree view of the sourceandconverter service available in headless mode
1 parent 52f7d4f commit 4fe5020

File tree

2 files changed

+87
-102
lines changed

2 files changed

+87
-102
lines changed

src/main/java/sc/fiji/bdvpg/scijava/services/SourceAndConverterService.java

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ public boolean containsMetadata(SourceAndConverter<?> sac, String key) {
237237
* @param sac source
238238
*/
239239
public void register(SourceAndConverter<?> sac) {
240-
register(sac, uiAvailable);
240+
register(sac, true);
241241
}
242242

243243
/**
@@ -496,7 +496,7 @@ else if (type instanceof ARGBType) {
496496
setupIdToSourceAndConverter.keySet().forEach(id -> {
497497
register(setupIdToSourceAndConverter.get(id));
498498
linkToSpimData(setupIdToSourceAndConverter.get(id), asd, id);
499-
if (uiAvailable) ui.update(setupIdToSourceAndConverter.get(id));
499+
ui.update(setupIdToSourceAndConverter.get(id));
500500
});
501501

502502
WrapBasicImgLoader.removeWrapperIfPresent(asd);
@@ -659,10 +659,7 @@ public synchronized void remove(SourceAndConverter<?>... sacs) {
659659
while (!flagPerformed.get()) {
660660
// busy waiting
661661
}*/
662-
663-
if (uiAvailable) {
664-
ui.remove(sac);
665-
}
662+
ui.remove(sac);
666663
}
667664
}
668665
}
@@ -704,11 +701,6 @@ public void linkToSpimData(SourceAndConverter sac, AbstractSpimData asd,
704701
*/
705702
SourceAndConverterServiceUI ui;
706703

707-
/**
708-
* Flags if the Inner UI exists
709-
*/
710-
boolean uiAvailable = false;
711-
712704
public SourceAndConverterServiceUI getUI() {
713705
return ui;
714706
}
@@ -757,9 +749,10 @@ public void initialize() {
757749
if (context().getService(UIService.class)!=null) {
758750
if (!context().getService(UIService.class).isHeadless()) {
759751
logger.debug(
760-
"uiService detected : Constructing JPanel for BdvSourceAndConverterService");
761-
ui = new SourceAndConverterServiceUI(this, context());
762-
uiAvailable = true;
752+
"GUI detected : Constructing JFrame for BdvSourceAndConverterService");
753+
ui = new SourceAndConverterServiceUI(this, context(), true);
754+
} else {
755+
ui = new SourceAndConverterServiceUI(this, context(), false);
763756
}
764757
}
765758

@@ -930,7 +923,7 @@ public String toString() {
930923

931924
public synchronized void setSpimDataName(AbstractSpimData asd, String name) {
932925
spimdataToMetadata.getIfPresent(asd).put("NAME", name);
933-
if (uiAvailable) ui.updateSpimDataName(asd, name);
926+
ui.updateSpimDataName(asd, name);
934927
}
935928

936929
@Override

src/main/java/sc/fiji/bdvpg/scijava/services/ui/SourceAndConverterServiceUI.java

Lines changed: 79 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -126,14 +126,11 @@ public class SourceAndConverterServiceUI {
126126
final SourceAndConverterService sourceAndConverterService;
127127

128128
/**
129-
* JFrame container
129+
* JFrame container, could be null, see guiAvailable
130130
*/
131131
final JFrame frame;
132132

133-
/**
134-
* JPanel container
135-
*/
136-
final JPanel panel;
133+
final boolean guiAvailable;
137134

138135
/**
139136
* Swing JTree used for displaying Sources object
@@ -145,11 +142,6 @@ public class SourceAndConverterServiceUI {
145142
*/
146143
final SourceFilterNode top;
147144

148-
/**
149-
* Scrollpane to display the JTree, if too big
150-
*/
151-
final JScrollPane treeView;
152-
153145
/**
154146
* Tree model
155147
*/
@@ -167,121 +159,121 @@ public class SourceAndConverterServiceUI {
167159
*/
168160
public SourceAndConverterServiceUI(
169161
SourceAndConverterService sourceAndConverterService,
170-
Context context)
162+
Context context, boolean makeGUI)
171163
{
172164
this.sourceAndConverterService = sourceAndConverterService;
173165

174-
frame = new JFrame("BDV Sources");
175-
panel = new JPanel(new BorderLayout());
176-
177166
// Tree view of Spimdata
178167
top = new SourceFilterNode(null, "Sources", (sac) -> true, false);
179168
model = new DefaultTreeModel(top);
180169
top.model = model;
181170

171+
// Registers the action which would inspect selected sources
172+
this.sourceAndConverterService.registerAction("Inspect Sources",
173+
this::inspectSources);
174+
182175
tree = new JTree(model);
183-
tree.setCellRenderer(new SourceAndConverterTreeCellRenderer());
176+
184177

185178
SourceFilterNode outsideSpimDataSources = new SourceFilterNode(model,
186179
"Other Sources", (sac) -> !sourceAndConverterService.containsMetadata(sac,
187180
SPIM_DATA_INFO), true);
188181
top.add(outsideSpimDataSources);
189182

190-
treeView = new JScrollPane(tree);
183+
if (makeGUI) {
184+
tree.setCellRenderer(new SourceAndConverterTreeCellRenderer());
185+
frame = new JFrame("BDV Sources");
186+
JPanel panel = new JPanel(new BorderLayout());
187+
JScrollPane treeView = new JScrollPane(tree);
188+
panel.add(treeView, BorderLayout.CENTER);
189+
// Shows Popup on right click
190+
tree.addMouseListener(new MouseAdapter() {
191191

192-
panel.add(treeView, BorderLayout.CENTER);
192+
@Override
193+
public void mouseClicked(MouseEvent e) {
194+
super.mouseClicked(e);
195+
// Right Click -> popup
196+
if (SwingUtilities.isRightMouseButton(e)) {
193197

194-
// Shows Popup on right click
195-
tree.addMouseListener(new MouseAdapter() {
198+
JPopupMenu popup = new SourceAndConverterPopupMenu(
199+
() -> getSelectedSourceAndConverters(tree)).getPopup();
196200

197-
@Override
198-
public void mouseClicked(MouseEvent e) {
199-
super.mouseClicked(e);
200-
// Right Click -> popup
201-
if (SwingUtilities.isRightMouseButton(e)) {
201+
addUISpecificActions(popup);
202202

203-
JPopupMenu popup = new SourceAndConverterPopupMenu(
204-
() -> getSelectedSourceAndConverters(tree)).getPopup();
203+
popup.show(e.getComponent(), e.getX(), e.getY());
205204

206-
addUISpecificActions(popup);
207-
208-
popup.show(e.getComponent(), e.getX(), e.getY());
209-
210-
// });
205+
// });
206+
}
211207
}
212-
}
213-
});
214-
215-
// Registers the action which would
216-
this.sourceAndConverterService.registerAction("Inspect Sources",
217-
this::inspectSources);
218-
219-
// We can drag the nodes
220-
tree.setDragEnabled(true);
221-
tree.setDropMode(DropMode.ON_OR_INSERT);
222-
// Enables:
223-
// - drag -> SourceAndConverters
224-
// - drop -> automatically import xml BDV datasets
225-
tree.setTransferHandler(new SourceAndConverterServiceUITransferHandler());
226-
227-
// get the screen size as a java dimension
228-
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
229-
230-
// get a fixed proportion of the height and of the width
231-
int height = screenSize.height * 4 / 5;
232-
int width = screenSize.width / 6;
233-
234-
// set the jFrame height and width
235-
frame.setPreferredSize(new Dimension(width, height));
236-
237-
JLabel cacheLabel = new JLabel("Cache");
208+
});
209+
// We can drag the nodes
210+
tree.setDragEnabled(true);
211+
tree.setDropMode(DropMode.ON_OR_INSERT);
212+
// Enables:
213+
// - drag -> SourceAndConverters
214+
// - drop -> automatically import xml BDV datasets
215+
tree.setTransferHandler(new SourceAndConverterServiceUITransferHandler());
238216

239-
panel.add(cacheLabel, BorderLayout.SOUTH);
217+
// get the screen size as a java dimension
218+
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
240219

241-
TimerTask periodicLogger = new TimerTask() {
220+
// get a fixed proportion of the height and of the width
221+
int height = screenSize.height * 4 / 5;
222+
int width = screenSize.width / 6;
242223

243-
@Override
244-
public void run() {
245-
SwingUtilities.invokeLater(() -> {
246-
cacheLabel.setText(sourceAndConverterService.getCache().toString());
247-
});
248-
}
249-
};
224+
// set the jFrame height and width
225+
frame.setPreferredSize(new Dimension(width, height));
250226

251-
Timer time = new Timer(); // Instantiate Timer Object
252-
time.schedule(periodicLogger, 0, 2000);
227+
JLabel cacheLabel = new JLabel("Cache");
253228

254-
cacheLabel.addMouseListener(new MouseAdapter() {
229+
panel.add(cacheLabel, BorderLayout.SOUTH);
230+
TimerTask periodicLogger = new TimerTask() {
255231

256-
@Override
257-
public void mouseClicked(MouseEvent e) {
258-
if (e.getClickCount() == 2) {
259-
sourceAndConverterService.getCache().invalidateAll();
232+
@Override
233+
public void run() {
260234
SwingUtilities.invokeLater(() -> {
261-
cacheLabel.setText("Cache cleared.");
235+
cacheLabel.setText(sourceAndConverterService.getCache().toString());
262236
});
263237
}
264-
}
265-
});
238+
};
239+
240+
Timer time = new Timer(); // Instantiate Timer Object
241+
time.schedule(periodicLogger, 0, 2000);
266242

267-
frame.add(panel);
268-
frame.pack();
269-
frame.setVisible(false);
270-
// frame.setVisible(true);
243+
cacheLabel.addMouseListener(new MouseAdapter() {
244+
245+
@Override
246+
public void mouseClicked(MouseEvent e) {
247+
if (e.getClickCount() == 2) {
248+
sourceAndConverterService.getCache().invalidateAll();
249+
SwingUtilities.invokeLater(() -> {
250+
cacheLabel.setText("Cache cleared.");
251+
});
252+
}
253+
}
254+
});
271255

272-
// TODO : read playground prefs from ij
273-
// PrefsPlaygroundPrefs.setSourceAndConverterUIVisibility();
256+
frame.add(panel);
257+
frame.pack();
258+
frame.setVisible(false);
259+
guiAvailable = true;
260+
} else {
261+
guiAvailable = false;
262+
frame = null;
263+
}
274264

275265
}
276266

277267
public void show() {
278-
if (PlaygroundPrefs.getSourceAndConverterUIVisibility()) {
268+
if ((guiAvailable)&&(PlaygroundPrefs.getSourceAndConverterUIVisibility())) {
279269
frame.setVisible(true);
280270
}
281271
}
282272

283273
public void hide() {
284-
frame.setVisible(false);
274+
if (guiAvailable) {
275+
frame.setVisible(false);
276+
}
285277
}
286278

287279
SourceFilterNode copiedNode = null;
@@ -427,7 +419,7 @@ public void inspectSources(SourceAndConverter<?>[] sacs) {
427419
* @param sac source to inspect
428420
*/
429421
public void inspectSource(SourceAndConverter sac) {
430-
if (!frame.isVisible()) {
422+
if ((guiAvailable)&&(!frame.isVisible())) {
431423
show();
432424
}
433425
DefaultMutableTreeNode parentNodeInspect = new DefaultMutableTreeNode(
@@ -457,7 +449,7 @@ public void removeBdvHandleNodes(BdvHandle bdvh) {
457449
* @param sac source which UI needs to be updated
458450
*/
459451
public void update(SourceAndConverter sac) {
460-
if (!frame.isVisible()) {
452+
if ((guiAvailable)&&(!frame.isVisible())) {
461453
show();
462454
}
463455
synchronized (tree) {
@@ -631,7 +623,7 @@ private void addEntityFilterNodes(SpimDataFilterNode nodeSpimData,
631623
* @param sac source to remove
632624
*/
633625
public void remove(SourceAndConverter sac) {
634-
if (!frame.isVisible()) {
626+
if ((guiAvailable)&&(!frame.isVisible())) {
635627
show();
636628
}
637629
synchronized (tree) {

0 commit comments

Comments
 (0)