forked from ome/bioformats
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ConfigWindow.java
679 lines (577 loc) · 21.5 KB
/
ConfigWindow.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
/*
* #%L
* Bio-Formats Plugins for ImageJ: a collection of ImageJ plugins including the
* Bio-Formats Importer, Bio-Formats Exporter, Bio-Formats Macro Extensions,
* Data Browser and Stack Slicer.
* %%
* Copyright (C) 2006 - 2017 Open Microscopy Environment:
* - Board of Regents of the University of Wisconsin-Madison
* - Glencoe Software, Inc.
* - University of Dundee
* %%
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 2 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program. If not, see
* <http://www.gnu.org/licenses/gpl-2.0.html>.
* #L%
*/
package loci.plugins.config;
import ij.Prefs;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Properties;
import javax.imageio.spi.IIORegistry;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.SpringLayout;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;
import javax.swing.border.EmptyBorder;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import loci.common.Constants;
import loci.common.DateTools;
import loci.formats.FormatTools;
import loci.plugins.util.LociPrefs;
import loci.plugins.util.WindowTools;
/**
* A window for managing configuration of the Bio-Formats plugins.
*
* @author Curtis Rueden ctrueden at wisc.edu
*/
public class ConfigWindow extends JFrame
implements ActionListener, ItemListener, ListSelectionListener, Runnable
{
// -- Constants --
private static final int PAD = 5;
private static final String UPGRADE_CHECK_KEY = "bioformats.upgradeCheck";
// -- Fields --
private DefaultListModel formatsListModel;
private JList formatsList;
private JPanel formatInfo;
private JTextField extensions, sliceLabel;
private JCheckBox enabledBox, windowlessBox, upgradeBox;
private DefaultListModel libsListModel;
private JList libsList;
private JTextField type, status, version, path, url, license;
private JTextArea notes;
private JButton installButton;
private JButton setSliceLabelButton;
private JButton resetSliceLabelButton;
private PrintWriter log;
// -- Constructor --
public ConfigWindow() {
setTitle("Bio-Formats Plugins Configuration");
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
// build UI
final String loading = "Loading...";
JTabbedPane tabs = new JTabbedPane();
tabs.setBorder(new EmptyBorder(PAD, PAD, PAD, PAD));
setContentPane(tabs);
JPanel bfOptionsPanel = new JPanel(new SpringLayout());
tabs.addTab("General", bfOptionsPanel);
JPanel upgradePanel = new JPanel(new SpringLayout());
JLabel upgradeLabel =
new JLabel("Automatically check for new versions of the Bio-Formats plugins");
upgradePanel.add(upgradeLabel);
final boolean checkForUpgrades = Prefs.get(UPGRADE_CHECK_KEY, true);
upgradeBox = new JCheckBox("", checkForUpgrades);
upgradeBox.addItemListener(this);
upgradePanel.add(upgradeBox);
SpringUtilities.makeCompactGrid(upgradePanel,1, 2, PAD, PAD, PAD, PAD);
JPanel sliceNamePanel = new JPanel(new SpringLayout());
JLabel sliceNameLabel = new JLabel("Slice Label Pattern");
sliceNamePanel.add(sliceNameLabel);
final String sliceLabelPattern = LociPrefs.getSliceLabelPattern();
sliceLabel = new JTextField(sliceLabelPattern);
int prefHeight = sliceLabel.getPreferredSize().height;
sliceLabel.setMaximumSize(new Dimension(Integer.MAX_VALUE, prefHeight));
sliceNamePanel.add(sliceLabel);
setSliceLabelButton = new JButton("Set");
resetSliceLabelButton = new JButton("Reset");
setSliceLabelButton.addActionListener(this);
resetSliceLabelButton.addActionListener(this);
sliceNamePanel.add(setSliceLabelButton);
sliceNamePanel.add(resetSliceLabelButton);
SpringUtilities.makeCompactGrid(sliceNamePanel,1, 4, PAD, PAD, PAD, PAD);
bfOptionsPanel.add(upgradePanel);
bfOptionsPanel.add(sliceNamePanel);
JLabel sliceDescription = new JLabel("<html>Customize the slice label by specifying a pattern string:"
+ "<br>%s - series index"
+ "<br>%n - series name"
+ "<br>%c - channel index"
+ "<br>%w - channel name"
+ "<br>%z - Z index"
+ "<br>%t - T index"
+ "<br>%A - acquisition timestamp</html>");
bfOptionsPanel.add(sliceDescription);
SpringUtilities.makeCompactGrid(bfOptionsPanel,3, 1, PAD, PAD, PAD, PAD);
JPanel installPanel = new JPanel();
//tabs.addTab("Install", installPanel);
installButton = new JButton(
"<html><center><br><font size=\"+1\">Check my system,<br>" +
"install missing libraries,<br>" +
"and upgrade old files</font><br>" +
"<font size=\"-1\"> <br>" +
"(We'll ask before we install or upgrade anything.)" +
"</font><br> ");
installButton.addActionListener(this);
installPanel.add(installButton);
formatsListModel = new DefaultListModel();
formatsListModel.addElement(loading);
formatsList = makeList(formatsListModel);
formatInfo = new JPanel();
tabs.addTab("Formats", makeSplitPane(formatsList, formatInfo));
extensions = makeTextField();
enabledBox = new JCheckBox("", false);
enabledBox.addItemListener(this);
windowlessBox = new JCheckBox("", false);
windowlessBox.addItemListener(this);
doFormatLayout(null);
libsListModel = new DefaultListModel();
libsListModel.addElement(loading);
libsList = makeList(libsListModel);
JPanel libInfo = new JPanel();
tabs.addTab("Libraries", makeSplitPane(libsList, libInfo));
libInfo.setLayout(new SpringLayout());
libInfo.add(makeLabel("Type", false));
type = makeTextField();
libInfo.add(type);
libInfo.add(makeLabel("Status", false));
status = makeTextField();
libInfo.add(status);
libInfo.add(makeLabel("Version", false));
version = makeTextField();
libInfo.add(version);
// TODO: install/upgrade button, if applicable
// - can upgrade any JAR from the repository
// + upgrade button for "ImageJ" just launches ImageJ upgrade plugin
// - can install native libs by downloading installer from its web site
// + QuickTime for Java
// + Nikon ND2 plugin
// + ImageIO Tools
libInfo.add(makeLabel("Path", false));
path = makeTextField();
libInfo.add(path);
libInfo.add(makeLabel("URL", false));
url = makeTextField();
libInfo.add(url);
libInfo.add(makeLabel("License", false));
license = makeTextField();
libInfo.add(license);
libInfo.add(makeLabel("Notes", true));
notes = new JTextArea();
notes.setEditable(false);
notes.setWrapStyleWord(true);
notes.setLineWrap(true);
libInfo.add(new JScrollPane(notes));
// TODO: "How to install" for each library?
JPanel logPanel = new JPanel();
tabs.addTab("Log", logPanel);
logPanel.setLayout(new java.awt.BorderLayout());
JTextArea logArea = new JTextArea();
logArea.setEditable(false);
logArea.setRows(10);
logPanel.add(new JScrollPane(logArea));
SpringUtilities.makeCompactGrid(libInfo, 7, 2, PAD, PAD, PAD, PAD);
pack();
TextAreaWriter taw = new TextAreaWriter(logArea);
log = new PrintWriter(taw);
new Thread(this, "ConfigWindow-Loader").start();
}
// -- ActionListener API methods --
@Override
public void actionPerformed(ActionEvent e) {
Object src = e.getSource();
if (src == installButton) {
new LociInstaller().run(null);
dispose();
}
else if (src == setSliceLabelButton) {
Prefs.set(LociPrefs.PREF_SLICE_LABEL_PATTERN, sliceLabel.getText());
return;
}
else if (src == resetSliceLabelButton) {
Prefs.set(LociPrefs.PREF_SLICE_LABEL_PATTERN, null);
sliceLabel.setText(LociPrefs.getSliceLabelPattern());
return;
}
}
// -- ItemListener API methods --
@Override
public void itemStateChanged(ItemEvent e) {
Object src = e.getSource();
if (src == upgradeBox) {
Prefs.set(UPGRADE_CHECK_KEY, upgradeBox.isSelected());
return;
}
Object value = formatsList.getSelectedValue();
if (!(value instanceof FormatEntry)) return;
FormatEntry entry = (FormatEntry) value;
if (src == enabledBox) {
setReaderEnabled(entry, enabledBox.isSelected());
}
else if (src == windowlessBox) {
setReaderWindowless(entry, windowlessBox.isSelected());
}
}
// -- ListSelectionListener API methods --
@Override
public void valueChanged(ListSelectionEvent e) {
Object src = e.getSource();
if (src == formatsList) {
Object value = formatsList.getSelectedValue();
if (!(value instanceof FormatEntry)) return;
FormatEntry entry = (FormatEntry) formatsList.getSelectedValue();
doFormatLayout(entry);
}
else if (src == libsList) {
Object value = libsList.getSelectedValue();
if (!(value instanceof LibraryEntry)) return;
LibraryEntry entry = (LibraryEntry) libsList.getSelectedValue();
type.setText(entry.type);
status.setText(entry.status);
version.setText(entry.version);
path.setText(entry.path);
url.setText(entry.url);
license.setText(entry.license);
notes.setText(entry.notes);
}
}
// -- Runnable API methods --
/** Populate configuration information in a separate thread. */
@Override
public void run() {
log.println("Bio-Formats Plugins configuration - " + DateTools.getTimestamp());
// list system properties
log.println();
log.println("-- System properties --");
Properties sysProps = System.getProperties();
for (Object name : sysProps.keySet()) {
log.println(name + " = " + sysProps.getProperty(name.toString()));
}
// generate list of formats
log.println();
log.println("-- Formats --");
try {
Class<?> irClass = Class.forName("loci.formats.ImageReader");
Object ir = irClass.newInstance();
Method getClasses = irClass.getMethod("getReaders");
Object[] readers = (Object[]) getClasses.invoke(ir);
for (int i=0; i<readers.length; i++) {
FormatEntry entry = new FormatEntry(log, readers[i]);
addEntry(entry, formatsListModel);
}
}
catch (Throwable t) {
log.println("Could not generate list of supported formats:");
t.printStackTrace(log);
}
log.println();
log.println("-- Libraries --");
// enumerate list of libraries
String javaVersion = System.getProperty("java.version") +
" (" + System.getProperty("java.vendor") + ")";
String bfVersion = FormatTools.DATE;
// Ant replaces date token with datestamp of the build
if (bfVersion.equals("@" + "date" + "@")) bfVersion = "Internal build";
String qtVersion = null;
try {
Class<?> qtToolsClass = Class.forName("loci.formats.gui.LegacyQTTools");
Object qtTools = qtToolsClass.newInstance();
Method getQTVersion = qtToolsClass.getMethod("getQTVersion");
qtVersion = (String) getQTVersion.invoke(qtTools);
}
catch (Throwable t) {
log.println("Could not determine QuickTime version:");
t.printStackTrace(log);
}
String clibIIOVersion = null;
try {
Class<?> jpegSpi = Class.forName(
"com.sun.media.imageioimpl.plugins.jpeg.CLibJPEGImageReaderSpi");
IIORegistry registry = IIORegistry.getDefaultInstance();
Object jpeg = registry.getServiceProviderByClass(jpegSpi);
if (jpeg == null) clibIIOVersion = LibraryEntry.MISSING_VERSION_CODE;
}
catch (Throwable t) {
if (t instanceof ClassNotFoundException) {
// JAI Image I/O Tools library not available
}
else {
log.println("Error determining native Image I/O Tools version:");
t.printStackTrace(log);
}
}
String matlabVersion = null;
try {
Class<?> matlabClass = Class.forName("com.mathworks.jmi.Matlab");
Object matlab = matlabClass.newInstance();
Method eval = matlabClass.getMethod("eval", new Class[] {String.class});
String ans = (String) eval.invoke(matlab, new Object[] {"version"});
if (ans.startsWith("ans =")) ans = ans.substring(5);
matlabVersion = ans.trim();
}
catch (Throwable t) {
if (t instanceof ClassNotFoundException) {
// MATLAB library not available
}
else {
log.println("Error determining MATLAB version:");
t.printStackTrace(log);
}
}
HashMap<String, String> versions = new HashMap<String, String>();
versions.put("javaVersion", javaVersion);
versions.put("bfVersion", bfVersion);
if (qtVersion != null) versions.put("qtVersion", qtVersion);
if (clibIIOVersion != null) versions.put("clibIIOVersion", clibIIOVersion);
if (matlabVersion != null) versions.put("matlabVersion", matlabVersion);
// parse libraries
HashMap<String, String> props = null;
String propKey = null;
StringBuffer propValue = new StringBuffer();
String resource = "libraries.txt";
BufferedReader in = null;
try {
in = new BufferedReader(new InputStreamReader(
ConfigWindow.class.getResourceAsStream(resource), Constants.ENCODING));
}
catch (UnsupportedEncodingException e) {
log.println("UTF-8 encoding is not supported. Something is very wrong.");
e.printStackTrace(log);
}
while (true) {
String line = null;
try {
line = in.readLine();
}
catch (IOException exc) {
log.println("Error parsing " + resource + ":");
exc.printStackTrace(log);
break;
}
if (line == null) break;
// ignore characters following # sign (comments)
int ndx = line.indexOf('#');
if (ndx >= 0) line = line.substring(0, ndx);
boolean space = line.startsWith(" ");
line = line.trim();
if (line.equals("")) continue;
// parse key/value pairs
int equals = line.indexOf('=');
if (line.startsWith("[")) {
// new entry
if (props == null) props = new HashMap<String, String>();
else {
addProp(props, propKey, propValue.toString(), versions);
LibraryEntry entry = new LibraryEntry(log, props);
addEntry(entry, libsListModel);
}
props.clear();
props.put("name", line.substring(1, line.length() - 1));
propKey = null;
}
else if (space) {
// append to previous property value
propValue.append(" ");
propValue.append(line);
}
else if (equals >= 0) {
addProp(props, propKey, propValue.toString(), versions);
propKey = line.substring(0, equals - 1).trim();
propValue.setLength(0);
propValue.append(line.substring(equals + 1).trim());
}
}
try {
in.close();
}
catch (IOException exc) {
log.println("Error closing " + resource + ":");
exc.printStackTrace(log);
}
}
// -- Utility methods --
public static JTextField makeTextField() {
JTextField textField = new JTextField(48);
int prefHeight = textField.getPreferredSize().height;
textField.setMaximumSize(new Dimension(Integer.MAX_VALUE, prefHeight));
textField.setEditable(false);
return textField;
}
public static void addEntry(final Comparable<Object> c,
final DefaultListModel listModel)
{
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
// remove "Loading..." message
if (listModel.size() == 1 && listModel.get(0) instanceof String) {
listModel.remove(0);
}
// binary search for proper location
int min = 0, max = listModel.size();
while (min < max) {
int mid = (min + max) / 2;
Object o = listModel.get(mid);
int result = c.compareTo(o);
if (result > 0) min = mid + 1;
else max = mid;
}
listModel.add(max, c);
}
});
}
// -- Helper methods --
private void addProp(HashMap<String, String> props,
String key, String value, HashMap<String, String> versions)
{
if (key == null) return;
// replace \n sequence with newlines
value = value.replaceAll("\\\\n *", "\n");
if (key.equals("version")) {
// get actual value from versions hashtable
value = versions.get(value);
}
if (value != null) props.put(key, value);
}
private JLabel makeLabel(String text, boolean top) {
JLabel label = new JLabel(text);
label.setHorizontalAlignment(SwingConstants.RIGHT);
if (top) label.setVerticalAlignment(SwingConstants.TOP);
return label;
}
private JList makeList(DefaultListModel listModel) {
JList list = new JList(listModel);
list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
list.setVisibleRowCount(25);
list.addListSelectionListener(this);
return list;
}
private JSplitPane makeSplitPane(JList list, JPanel info) {
JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,
new JScrollPane(list), info);
splitPane.setDividerLocation(260);
return splitPane;
}
private void doFormatLayout(FormatEntry entry) {
if (entry != null) {
// build list of extensions
StringBuffer sb = new StringBuffer();
for (int i=0; i<entry.suffixes.length; i++) {
if (i > 0) sb.append(", ");
sb.append(entry.suffixes[i]);
}
extensions.setText(sb.toString());
}
enabledBox.setSelected(isReaderEnabled(entry));
windowlessBox.setSelected(isReaderWindowless(entry));
formatInfo.removeAll();
formatInfo.setLayout(new SpringLayout());
formatInfo.add(makeLabel("Extensions", false));
formatInfo.add(extensions);
formatInfo.add(makeLabel("Enabled", false));
formatInfo.add(enabledBox);
formatInfo.add(makeLabel("Windowless", false));
formatInfo.add(windowlessBox);
// format-specific widgets
int rows = entry == null ? 0 : entry.widgets.length;
for (int i=0; i<rows; i++) {
formatInfo.add(makeLabel(entry.labels[i], false));
formatInfo.add(entry.widgets[i]);
}
SpringUtilities.makeCompactGrid(formatInfo,
3 + rows, 2, PAD, PAD, PAD, PAD);
formatInfo.validate();
formatInfo.repaint();
}
private boolean isReaderEnabled(FormatEntry entry) {
return isReaderPref("ENABLED", entry, true);
}
private void setReaderEnabled(FormatEntry entry, boolean value) {
setReaderPref("ENABLED", entry, value);
}
private boolean isReaderWindowless(FormatEntry entry) {
return isReaderPref("WINDOWLESS", entry, false);
}
private void setReaderWindowless(FormatEntry entry, boolean value) {
setReaderPref("WINDOWLESS", entry, value);
}
private boolean isReaderPref(String pref, FormatEntry entry,
boolean defaultValue)
{
if (entry == null) return false;
try {
Boolean result = (Boolean)
invokeMethod("PREF_READER_" + pref, "get", entry, defaultValue);
return result.booleanValue();
}
catch (Throwable t) {
t.printStackTrace();
log.println("Error querying property '" + pref.toLowerCase() +
"' for reader '" + entry.readerName + "':");
t.printStackTrace(log);
return false;
}
}
private void setReaderPref(String pref, FormatEntry entry, boolean value) {
if (entry == null) return;
try {
invokeMethod("PREF_READER_" + pref, "set", entry, value);
}
catch (Throwable t) {
t.printStackTrace();
log.println("Error setting property '" + pref.toLowerCase() +
"' for reader '" + entry.readerName + "':");
t.printStackTrace(log);
}
}
private static final Class<?>[] PARAMS = {String.class, boolean.class};
private Object invokeMethod(String fieldName, String methodName,
FormatEntry entry, boolean value) throws ClassNotFoundException,
NoSuchFieldException, IllegalAccessException, NoSuchMethodException,
InvocationTargetException
{
Class<?> utilClass = Class.forName("loci.plugins.util.LociPrefs");
Field field = utilClass.getField(fieldName);
String key = field.get(null) + "." + entry.readerName;
Class<?> prefsClass = Class.forName("ij.Prefs");
Method method = prefsClass.getMethod(methodName, PARAMS);
Object[] args = {key, value ? Boolean.TRUE : Boolean.FALSE};
return method.invoke(null, args);
}
}