File: PlotContentsDialog.java

package info (click to toggle)
imagej 1.54g-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 6,520 kB
  • sloc: java: 132,209; sh: 286; xml: 255; makefile: 6
file content (671 lines) | stat: -rw-r--r-- 29,147 bytes parent folder | download
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
package ij.gui;
import ij.*;
import ij.process.*;
import ij.measure.CurveFitter;
import ij.measure.Minimizer;
import ij.text.TextWindow;
import ij.measure.ResultsTable;
import ij.plugin.Colors;
import ij.plugin.frame.Recorder;
import ij.util.Tools;
import java.awt.*;
import java.awt.event.*;
import java.util.Arrays;
import java.util.Vector;
import java.util.ArrayList;

/** This class implements the Plot Window's Data>"Add from Plot", "Add form Table", "Add Fit" and
 *  "More>Contents Style" dialogs
 */
public class PlotContentsDialog implements DialogListener {
	/** Types of dialog (ERROR suppresses the dialog after an invalid call of a constructor) */
	public final static int ERROR=-1, STYLE=0, ADD_FROM_PLOT=1, ADD_FROM_TABLE=2, ADD_FROM_ARRAYS=3, ADD_FIT=4;
	/** Dialog headings for each dialogType >= 0 */
	private static final String[] HEADINGS = new String[] {"Plot Contents Style", "Add From Plot", "Plot From Table", "Add Plot Data", "Add Fit"};
	private Plot          plot;                     // the plot we work on
	private int           dialogType;               // determines what to do: ADD_FORM_PLOT, etc.
	private double[]      savedLimits;              // previous plot range, for undo upon cancel
	GenericDialog         gd;
	private int currentObjectIndex = -1;
	private Choice        objectChoice;
	private Choice        symbolChoice;
	private TextField     colorField, color2Field, labelField, widthField;
	private Checkbox      visibleCheckbox;
	private boolean       creatingPlot;              // for creating plots; dialogType determines source
	private Choice        plotChoice;                // for "Add from Plot"
	private Plot[]        allPlots;
	private String[]      allPlotNames;
	private static Plot   previousPlot;
	private static int    previousPlotObjectIndex;
	private int           defaultPlotIndex, defaultObjectIndex;
	private int           currentPlotNumObjects;
	private Choice        tableChoice;               // for "Add from Table"
	final static int      N_COLUMNS = 4;             // number of data columns that we can have; x, y, xE, yE
	private int           nColumnsToUse = N_COLUMNS; // user may restrict to 2, for not having error bars
	private Choice[]      columnChoice = new Choice[N_COLUMNS];
	private final static String[] COLUMN_NAMES = new String[] {"X:", "Y:", "X Error:", "Y Error:"};
	private final static boolean[] COLUMN_ALLOW_NONE = new boolean[] {true, false, true, true}; //y data cannot be null
	private ResultsTable[] allTables;
	private String[]      allTableNames;
	private static String previousTableName;
	private static int[]  previousColumns = new int[]{1, 1, 0, 0}; //must be N_COLUMNS elements
	private static int    defaultTableIndex;
	private static int[]  defaultColumnIndex = new int[N_COLUMNS];
	private String[]      arrayHeadings;             // for "Add from Arrays"
	private ArrayList<float[]> arrayData;
	private Choice        fitDataChoice;             // for "Add Fit"
	private Choice        fitFunctionChoice;
	private Thread        fittingThread;
	private static String lastFitFunction = CurveFitter.fitList[0];
	private String        curveFitterStatusString;
	private static String previousColor="blue", previousColor2="#a0a0ff", previousSymbol="Circle";
	private static double previousLineWidth = 1;
	private static final String[] PLOT_COLORS = new String[] {"blue", "red", "black", "#00c0ff", "#00e000", "gray", "#c08060", "magenta"};


	/** Prepares a new PlotContentsDialog for an existing plot. Use showDialog thereafter.
	 *  @param dialogType may be STYLE (contents style), ADD_FROM_PLOT (add object from other plot), ADD_FROM_TABLE, and ADD_FIT */
	public PlotContentsDialog(Plot plot, int dialogType) {
		this.plot = plot;
		this.dialogType = plot == null ? ERROR : dialogType;
		if (plot != null) currentPlotNumObjects = plot.getNumPlotObjects();
	}

	/** Prepares a new PlotContentsDialog for creating a new plot using data from a ResultsTable.
	 *  Use showDialog thereafter. */
	public PlotContentsDialog(String title, ResultsTable rt) {
		creatingPlot = true;
		dialogType = ADD_FROM_TABLE;
		if (rt == null || !isValid(rt)) {
			IJ.error("Cant Create Plot","No (results) table or no data in "+title);
			dialogType = ERROR;
		}
		plot = new Plot("Plot of "+title, "x", "y");
		allTables = new ResultsTable[] {rt};
		allTableNames = new String[] {title};
	}

	/** Prepares a new PlotContentsDialog for creating a new plot using float[] arrays as data.
	 *  Each 'data' array in the ArrayList must have a corresponding element in the 'headings' array.
	 *  'defaultHeadings' may contain the headings of the items selected initially for x, y, x error and y error, respectively.
	 *  'defaultHeadings' and each of its entries may be null, and the array may have any length. */
	public PlotContentsDialog(String title, String[] headings, String[] defaultHeadings, ArrayList<float[]> data) {
		creatingPlot = true;
		dialogType = ADD_FROM_ARRAYS;
		this.arrayHeadings = headings;
		this.arrayData = data;
		plot = new Plot(title, "x", "y");
		setDefaultColumns(defaultHeadings);
	}

	/** Prepares a new PlotContentsDialog for adding data from float[] arrays to a plot.
	 *  Each 'data' array in the ArrayList must have a corresponding element in the 'headings' array.
	 *  'defaultHeadings' may contain the headings of the items selected initially for x, y, x error and y error, respectively.
	 *  'defaultHeadings' and each of its entries may be null, and the array may have any length. */
	public PlotContentsDialog(Plot plot, String[] headings, String[] defaultHeadings, ArrayList<float[]> data) {
		dialogType = ADD_FROM_ARRAYS;
		this.plot = plot;
		this.arrayHeadings = headings;
		this.arrayData = data;
		setDefaultColumns(defaultHeadings);
	}

	/** Avoids showing a selection for the error bars; must be called before showDialog. */
	public void noErrorBars() {
		nColumnsToUse = 2; // only x, y columns can be selected
	}

	/** Shows the dialog, with a given parent Frame (may be null) */
	public void showDialog(Frame parent) {
		if (dialogType == ERROR) return;
		if (!creatingPlot) savedLimits = plot.getLimits();
		plot.savePlotObjects();
		String[] designations = plot.getPlotObjectDesignations();
		if (dialogType == STYLE && designations.length==0) {
			IJ.error("Empty Plot");
			return;
		} else if (dialogType == ADD_FROM_PLOT) {
			prepareAddFromPlot();
			if (allPlots.length == 0) return;	//should never happen; we have at least the current plot
		} else if (dialogType == ADD_FROM_TABLE && !creatingPlot) {
			prepareAddFromTable();
			if (allTables.length == 0) return;	//should never happen; PlotWindow should not enable if no table
		}
		if ((dialogType == ADD_FROM_TABLE || dialogType == ADD_FROM_ARRAYS) && !creatingPlot)
			suggestColor();
		if (parent == null && plot.getImagePlus() != null)
			parent = plot.getImagePlus().getWindow();
		gd = parent == null ? new GenericDialog(HEADINGS[dialogType]) :
				new GenericDialog(HEADINGS[dialogType], parent);
		IJ.wait(100);			//sometimes needed to avoid hanging?
		if (dialogType == STYLE) {
			gd.addChoice("Item:", designations, designations[0]);
			objectChoice = (Choice)(gd.getChoices().get(0));
			currentObjectIndex = objectChoice.getSelectedIndex();
		} else if (dialogType == ADD_FROM_PLOT) {
			gd.addChoice("Select Plot:", allPlotNames, allPlotNames[defaultPlotIndex]);
			gd.addChoice("Item to Add:", new String[]{""}, "");  // will be set up by makeSourcePlotObjects
			Vector choices = gd.getChoices();
			plotChoice = (Choice)(choices.get(0));
			objectChoice = (Choice)(choices.get(1));
			makeSourcePlotObjects();
		} else if (dialogType == ADD_FROM_TABLE) {
			gd.addChoice("Select Table:", allTableNames, allTableNames[defaultTableIndex]);
			tableChoice = (Choice)(gd.getChoices().get(0));
			if (creatingPlot) tableChoice.setEnabled(false);     // we can't select the table, we have only one
		} else if (dialogType == ADD_FIT) {
			String[] dataSources = plot.getDataObjectDesignations();
			if (dataSources.length == 0) {
				IJ.error("No Data For Fitting");
				return;
			}
			gd.addChoice("Fit Data Set:", dataSources, dataSources[0]);
			gd.addChoice("Fit Function:", new String[0], "");
			Vector choices = gd.getChoices();
			fitDataChoice = (Choice)(choices.get(0));
			fitFunctionChoice = (Choice)(choices.get(1));
			if (dataSources.length == 1)
				fitDataChoice.setEnabled(false);
			for (int i=0; i<CurveFitter.fitList.length; i++)
				fitFunctionChoice.addItem(CurveFitter.fitList[CurveFitter.sortedTypes[i]]);
			fitFunctionChoice.select(lastFitFunction);
		}
		if (dialogType == ADD_FROM_TABLE || dialogType == ADD_FROM_ARRAYS) {
			for (int i=0; i<nColumnsToUse; i++) {
				gd.addChoice(COLUMN_NAMES[i], new String[]{""}, "");  // will set up by makeSourceColumns
				Vector choices = gd.getChoices();
				columnChoice[i] = (Choice)(choices.get(choices.size()-1));
			}
			makeSourceColumns();
			if (!creatingPlot)
				suggestNewYColumn();
		}
		gd.addStringField("Color:", previousColor, 10);
		gd.addStringField("Secondary (fill) color:", previousColor2, 10);
		gd.addNumericField("Line width: ", previousLineWidth, 1);
		gd.addChoice("Symbol:", Plot.SORTED_SHAPES, dialogType == ADD_FIT ? "line" : previousSymbol);
		gd.addStringField("Label:", "", 20);
		gd.setInsets(10, 60, 0);
		gd.addCheckbox("Visible", true);
		Vector choices = gd.getChoices();
		symbolChoice = (Choice)(choices.get(choices.size()-1));
		Vector stringFields = gd.getStringFields();
		colorField = (TextField)(stringFields.get(0));
		color2Field = (TextField)(stringFields.get(1));
		labelField = (TextField)(stringFields.get(2));
		widthField = (TextField)(gd.getNumericFields().get(0));
		visibleCheckbox = (Checkbox)(gd.getCheckboxes().get(0));
		gd.addDialogListener(this);
		IJ.wait(100);			//sometimes needed to avoid hanging?
		if (dialogType == STYLE)
			setDialogStyleFields(objectChoice.getSelectedIndex());
		else if (dialogType == ADD_FROM_PLOT)
			addObjectFromPlot();
		else if (dialogType == ADD_FROM_TABLE || dialogType == ADD_FROM_ARRAYS)
			addObjectFromTable();
		else if (dialogType == ADD_FIT)
			addFitCurve();
		plot.updateImage();
		if (creatingPlot) {
			boolean recording = Recorder.record;
			Recorder.record = false; // don't record creating the image as image selection
			plot.show();
			if (recording) Recorder.record = true;
		}

		gd.showDialog();

		if (fittingThread != null) {
			fittingThread.interrupt();
			try {
				fittingThread.join();
			} catch (InterruptedException e) {}
		}
		if (gd.wasCanceled()) {
			if (creatingPlot) {
				ImagePlus imp = plot.getImagePlus();
				if (imp != null) imp.close();
			} else {
				plot.restorePlotObjects();
				if (savedLimits != null)
					plot.setLimits(savedLimits[0], savedLimits[1], savedLimits[2], savedLimits[3]);
				plot.updateImage();
				plot.killPlotObjectsSnapshot();
			}
			return;
		}
		plot.killPlotObjectsSnapshot();
		if (dialogType == ADD_FROM_TABLE || dialogType == ADD_FROM_ARRAYS || dialogType == ADD_FIT) {
			previousColor = colorField.getText();
			previousColor2 = color2Field.getText();
			previousSymbol = symbolChoice.getSelectedItem();
			previousLineWidth = Tools.parseDouble(widthField.getText());
		}
		if (dialogType == ADD_FIT) {
			lastFitFunction = fitFunctionChoice.getSelectedItem();
			IJ.log(curveFitterStatusString);
		}
		if (Recorder.record && !Recorder.scriptMode()) {
			if (dialogType == ADD_FROM_PLOT) {
				Recorder.recordString("Plot.addFromPlot(\""+plotChoice.getSelectedItem()+"\", "+objectChoice.getSelectedIndex()+");\n");
			} else if (dialogType == ADD_FROM_TABLE) {
				if (creatingPlot)
					Recorder.recordString("Plot.create(\""+plot.getTitle()+"\", \""+plot.getLabel('x')+"\", \""+plot.getLabel('y')+"\");\n");
				String xSource = columnChoice[0].getSelectedIndex() == 0 ? "" :
						"Table.getColumn(\""+columnChoice[0].getSelectedItem()+"\", \""+tableChoice.getSelectedItem()+"\"), ";
				String ySource = "Table.getColumn(\""+columnChoice[1].getSelectedItem()+"\", \""+tableChoice.getSelectedItem()+"\")";
				Recorder.recordString("Plot.add(\""+symbolChoice.getSelectedItem()+"\", "+xSource+ySource+");\n");
				if (columnChoice[2].getSelectedIndex() > 0)
					Recorder.recordString("Plot.add(\"xerror\", Table.getColumn(\""+
							columnChoice[2].getSelectedItem()+"\", \""+tableChoice.getSelectedItem()+"\"));\n");
				if (columnChoice[3].getSelectedIndex() > 0)
					Recorder.recordString("Plot.add(\"error\", Table.getColumn(\""+
							columnChoice[3].getSelectedItem()+"\", \""+tableChoice.getSelectedItem()+"\"));\n");
			}
			Recorder.recordString("Plot.setStyle("+currentObjectIndex+", \""+getStyleString()+"\");\n");
		}
	}

	public boolean dialogItemChanged(GenericDialog gd, AWTEvent e) {
		if (e==null)
			return true;	//gets called with e=null upon OK
		boolean setStyle = false;
		if (dialogType == STYLE) {
			int objectIndex = objectChoice.getSelectedIndex();  // no getNextChoice since Choices depend on dialog type
			setStyle = (e.getSource() != objectChoice);
			if (e.getSource() == objectChoice) {
				setDialogStyleFields(objectIndex);
				currentObjectIndex = objectIndex;
			} else
				setStyle = true;
		} else if (dialogType == ADD_FROM_PLOT) {
			if (e.getSource() == plotChoice) {
				makeSourcePlotObjects();
				addObjectFromPlot();
			} else if (e.getSource() == objectChoice) {
				addObjectFromPlot();
			} else
				setStyle = true;
		} else if (dialogType == ADD_FROM_TABLE || dialogType == ADD_FROM_ARRAYS) {
			if (e.getSource() == tableChoice) {
				makeSourceColumns();
				addObjectFromTable();
			} else {
				boolean columnChanged = false;
				for (int c=0; c<nColumnsToUse; c++)
					if (e.getSource() == columnChoice[c]) {
						columnChanged = true;
						break;
					}
				if (columnChanged)
					addObjectFromTable();
				else
					setStyle = true;
			}
		} else if (dialogType == ADD_FIT) {
			if (e.getSource() == fitDataChoice || e.getSource() == fitFunctionChoice)
				addFitCurve();
			else
				setStyle = true;
		}
		if (setStyle)
			setPlotObjectStyle();
		return true;
	}

	private void setPlotObjectStyle() {
		if (currentObjectIndex < 0) return;
		String style = getStyleString();
		plot.setPlotObjectStyle(currentObjectIndex, style);
		if (labelField.isEnabled()) {
			String label = labelField.getText();
			plot.setPlotObjectLabel(currentObjectIndex, label.length() > 0 ? label : null);
		}
	}

	/** Returns the style String for Plot.setPlotObjectStyle from the dialog fields */
	private String getStyleString() {
		String color = colorField.getText();
		String color2 = color2Field.getText();
		double width = Tools.parseDouble(widthField.getText());
		String symbol = symbolChoice.getSelectedItem();
		Boolean visible = visibleCheckbox.getState();
		String style = color.trim()+","+color2.trim()+","+(float)width+","+ symbol+(visible?"":"hidden");
		return style;
	}

	/** Sets the style fields of the dialog according to the style of the PlotObject having the
	 *  index given. Does nothing with index < 0 */
	private void setDialogStyleFields(int index) {
		if (index < 0) return;
		Checkbox visibleC = (Checkbox)gd.getCheckboxes().get(0);
		String styleString = plot.getPlotObjectStyle(index);
		String designation = plot.getPlotObjectDesignations()[index].toLowerCase();
		boolean isData = designation.startsWith("data");
		boolean isText = designation.startsWith("text");
		boolean isBox = designation.startsWith("shapes") &&
				(designation.contains("boxes") || designation.contains("rectangles"));
		boolean isGrid = designation.startsWith("shapes") && designation.contains("redraw_grid");

		String[] items = styleString.split(",");
		colorField.setText(items[0]);
		color2Field.setText(items[1]);
		widthField.setText(items[2]);
		if (items.length >= 4)
			symbolChoice.select(items[3]);
		labelField.setText(isData ? plot.getPlotObjectLabel(index) : "");
		visibleC.setState(!styleString.contains("hidden"));

		colorField.setEnabled(!isGrid);	        //grid color is fixed
		color2Field.setEnabled(isData || isBox);//only (some) data symbols and boxes have secondary (fill) color
		widthField.setEnabled(!isText  && !isGrid); //all non-Text types have line width
		// visibleC.setEnabled(!isGrid);        //allow to hide everything
		symbolChoice.setEnabled(isData);        //only data have a symbol to choose
		labelField.setEnabled(isData);          //only data have a label in the legend
	}


	/** Prepare the lists 'allPlots', 'allPlotNames' for the "Add from Plot" dialog.
	 *  Also sets 'defaultPlotIndex', 'defaultObjectIndex' */
	private void prepareAddFromPlot() {
		int[] windowIDlist = WindowManager.getIDList();
		ArrayList<ImagePlus> plotImps = new ArrayList<ImagePlus>();
		ImagePlus currentPlotImp = null;
		for (int windowID : windowIDlist) {
			ImagePlus imp = WindowManager.getImage(windowID);
			if (imp == null || imp.getWindow() == null) continue;
			Plot thePlot = (Plot)(imp.getProperty(Plot.PROPERTY_KEY));
			if (thePlot != null) {
				if (thePlot == plot)
					currentPlotImp = imp;
				else
					plotImps.add(imp);
			}
		}
		if (currentPlotImp != null)
			plotImps.add(currentPlotImp);     // add current plot as the last one (usually not used)
		if (plotImps.size() == 0) return;     // should never happen; we have at least the current plot

		allPlots = new Plot[plotImps.size()];
		allPlotNames = new String[plotImps.size()];
		defaultPlotIndex = 0;
		for (int i=0; i<allPlots.length; i++) {
			ImagePlus imp = plotImps.get(i);
			allPlots[i] = (Plot)(imp.getProperty(Plot.PROPERTY_KEY));
			if (allPlots[i] == previousPlot)
				defaultPlotIndex = i;
			allPlotNames[i] = imp.getWindow().getTitle();
			if (imp == currentPlotImp)
				allPlotNames[i] = "THIS PLOT: " + allPlotNames[i];
		}
	}

	/** Set up the Choice of Plot objects for the source plot after selecting that plot */
	private void makeSourcePlotObjects() {
		int plotIndex = plotChoice.getSelectedIndex();
		String[] plotObjectNames = allPlots[plotIndex].getPlotObjectDesignations();
		objectChoice.removeAll();
		int nPlotObjects = allPlots[plotIndex] == plot ? currentPlotNumObjects : plotObjectNames.length; //for the own plot, the number of objects may have changed in the meanwhile
		for (int i=0; i<nPlotObjects; i++)
			objectChoice.addItem(plotObjectNames[i]);
		objectChoice.select(Math.min(nPlotObjects-1, previousPlotObjectIndex));
	}

	/** For "Add from Plot", adds item to the plot according to the current Choice settings
	 *  and sets the Style fields for it. */
	private void addObjectFromPlot() {
		int plotIndex = plotChoice.getSelectedIndex();
		int objectIndex = objectChoice.getSelectedIndex();
		plot.restorePlotObjects();
		if (savedLimits != null)
			plot.setLimits(savedLimits);
		currentObjectIndex = plot.addObjectFromPlot(allPlots[plotIndex], objectIndex); //no updateImage; will be done later
		plot.fitRangeToLastPlotObject();
		setDialogStyleFields(currentObjectIndex);
		previousPlot = allPlots[plotIndex];
		previousPlotObjectIndex = objectIndex;
	}

	/** Prepare the lists 'allTables', "allTableNames' for the "Add from Table" dialog.
	 *  Also sets 'defaultTableIndex' */
	private void prepareAddFromTable() {
		ArrayList<TextWindow> tableWindows = new ArrayList<TextWindow>();
		Frame[] windows = WindowManager.getNonImageWindows();
		for (Frame win : windows) {
			if (!(win instanceof TextWindow)) continue;
			ResultsTable rt = ((TextWindow)win).getResultsTable();
			if (isValid(rt))
				tableWindows.add((TextWindow)win);
		}
		allTables = new ResultsTable[tableWindows.size()];
		allTableNames = new String[tableWindows.size()];
		defaultTableIndex =  0;
		for (int i=0; i<allTables.length; i++) {
			TextWindow tw = tableWindows.get(i);
			allTables[i] = tw.getResultsTable();
			if (allTableNames[i]!=null && allTableNames[i].equals(previousTableName))
				defaultTableIndex = i;
			allTableNames[i] = tw.getTitle();
		}
	}

	/** Returns whether there is at least one table that can be used for "Add from Table" */
	public static boolean tableWindowExists() {
		Frame[] windows = WindowManager.getNonImageWindows();
		for (Frame win : windows) {
			if (win instanceof TextWindow) {
				ResultsTable rt = ((TextWindow)win).getResultsTable();
				if (isValid(rt)) return true;
			}
		}
		return false;
	}

	/** Set up the Choices for the source columns for "Add from Table" and "Add from Arrays" */
	private void makeSourceColumns() {
		String[] columnHeadings = null;
		if (dialogType == ADD_FROM_TABLE) {
			int tableIndex = tableChoice.getSelectedIndex();
			ResultsTable rt = allTables[tableIndex];
			String columnHeadingStr = rt.getColumnHeadings();
			if (!columnHeadingStr.startsWith(" \t"))
				columnHeadingStr = " \t"+columnHeadingStr;	//add empty field at beginning (if we don't have one)
			columnHeadings = columnHeadingStr.split("\t");
			int nBadColumns = 0;
			for (int i=1; i<columnHeadings.length; i++) {	//check for columns with no data
				int index = rt.getColumnIndex(columnHeadings[i]);
				if (!rt.columnExists(index)) {
					columnHeadings[i] = null;
					nBadColumns++;
				}
			}
			if (nBadColumns > 0) {
				String[] newHeadings = new String[columnHeadings.length - nBadColumns];
				int i=0;
				for (String heading : columnHeadings)		//copy 'good' headings
					if (heading != null)
						newHeadings[i++] = heading;
				columnHeadings = newHeadings;
			}
		} else {   // if (dialogType == ADD_FROM_ARRAYS)
			columnHeadings = new String[arrayHeadings.length+1];
			System.arraycopy(arrayHeadings, 0, columnHeadings, 1, arrayHeadings.length);
		}
		columnHeadings[0] = "---";
		for (int c=0; c<nColumnsToUse; c++) {
			columnChoice[c].removeAll();
			for (int i=COLUMN_ALLOW_NONE[c] ? 0 : 1; i<columnHeadings.length; i++)
					columnChoice[c].addItem(columnHeadings[i]);
			if (columnChoice[c].getItemCount() > 0 && previousColumns[c] >= 0)
				columnChoice[c].select(Math.min(columnChoice[c].getItemCount()-1, previousColumns[c]));
		}
	}

	/** For "Add from Table" and "Add from Arrays" adds item to the plot according to the current Choice settings
	 *  and sets the Style fields for it. */
	private void addObjectFromTable() {
		float[][] data = getDataArrays();
		if (data[1] == null) return;  //no y data? then can't plot
		String label = columnChoice[1].getSelectedItem();	//take label from y
		int shape = Plot.toShape(symbolChoice.getSelectedItem());
		float lineWidth = (float)(Tools.parseDouble(widthField.getText()));
		if (lineWidth > 0)
			plot.setLineWidth(lineWidth);
		plot.restorePlotObjects();
		if (savedLimits != null)
			plot.setLimits(savedLimits);
		plot.setColor(colorField.getText(), color2Field.getText());
		plot.addPoints(data[0], data[1], data[3], shape, label);
		if (data[2] != null)
			plot.addHorizontalErrorBars(data[2]);
		if (creatingPlot) {
			plot.setXYLabels(data[0]==null ? "x" : columnChoice[0].getSelectedItem(), columnChoice[1].getSelectedItem());
			plot.setLimitsToFit(false);
		} else
			plot.fitRangeToLastPlotObject();
		currentObjectIndex = plot.getNumPlotObjects()-1;
		setDialogStyleFields(currentObjectIndex);
		if (dialogType == ADD_FROM_TABLE)
			previousTableName = allTableNames[tableChoice.getSelectedIndex()];
	}

	/** For "Add from Table" and "Add from Arrays", tries to set a 'y' data column that has not been plotted yet. */
	private void suggestNewYColumn() {
		int nYcolumns = columnChoice[1].getItemCount();
		int currentIndex = columnChoice[1].getSelectedIndex();
		for (int i=0; i<nYcolumns; i++) {
			if (columnChoice[0].getSelectedIndex()==0 ||    //if x is a data column, don't suggest it as y
					!columnChoice[1].getSelectedItem().equals(columnChoice[0].getSelectedItem())) {
				float[][] data = getDataArrays();
				data = new float[][] {data[0], data[1]};	//only compare x & y, not the error bars
				if (plot.getPlotObjectIndex(data) < 0)
					return; //current data are ok (not duplicate)
			}
			columnChoice[1].select((currentIndex+i+1)%nYcolumns); //try the next one
		}
	}

	/** For "Add from Table" and "Add from Arrays", retrieves the data arrays according to the columnChoices */
	private float[][] getDataArrays() {
		float[][] data = new float[N_COLUMNS][];
		if (dialogType == ADD_FROM_TABLE) {
			ResultsTable rt = allTables[tableChoice.getSelectedIndex()];
			for (int c=0; c<N_COLUMNS; c++) {
				String heading = columnChoice[c].getSelectedItem();
				int index = rt.getColumnIndex(heading);
				if (index >= 0)
					data[c] = rt.getColumn(index);
				previousColumns[c] = columnChoice[c].getSelectedIndex();
			}
		} else { //if (dialogType == ADD_FROM_ARRAYS)
			for (int c=0; c<nColumnsToUse; c++) {
				String heading = columnChoice[c].getSelectedItem();
				int index = getIndex(arrayHeadings, heading);
				if (index >= 0)
					data[c] = arrayData.get(index);
				previousColumns[c] = columnChoice[c].getSelectedIndex();
			}
		}
		return data;
	}

	/** Does the curve fit and adds the fit curve to the plot */
	private void addFitCurve() {
		plot.restorePlotObjects();
		if (savedLimits != null)
			plot.setLimits(savedLimits);
		int dataIndex = fitDataChoice.getSelectedIndex();
		float[][] data = plot.getDataObjectArrays(dataIndex);
		String fitName = fitFunctionChoice.getSelectedItem();
		int fitType = getIndex(CurveFitter.fitList, fitName);
		CurveFitter cf = new CurveFitter(Tools.toDouble(data[0]), Tools.toDouble(data[1]));
		cf.doFit(fitType);
		String statusString = "Fit: "+Minimizer.STATUS_STRING[cf.getStatus()];
		if (cf.getStatus() == Minimizer.SUCCESS)
			statusString += ", sum residuals ^2 = "+(float)(cf.getSumResidualsSqr());
		IJ.showStatus(statusString);
		curveFitterStatusString = "Fit for "+plot.getTitle()+": "+fitDataChoice.getSelectedItem()+cf.getResultString(); //will be shown in Log when done

		double[] plotMinMax = plot.getLimits();
		double[] dataMinMax = Tools.getMinMax(data[0]);
		double min = Math.min(plotMinMax[0], dataMinMax[0]);
		double max = Math.max(plotMinMax[1], dataMinMax[1]);
		double plotSpan = Math.abs(plotMinMax[1] - plotMinMax[0]);
		double dataSpan = Math.abs(dataMinMax[1] - dataMinMax[0]);
		double rangeFactor = Math.max(plotSpan/dataSpan, dataSpan/plotSpan);
		if (rangeFactor > 20) rangeFactor = 20;
		int nPoints = (int)(1000*rangeFactor); //finer data point spacing if we will want to zoom out or in
		float[] xFit = new float[nPoints];
		float[] yFit = new float[nPoints];
		for (int i=0; i<nPoints; i++) {
			xFit[i] = (float)(min + i*((max-min)/(nPoints-1)));
			yFit[i] = (float)(cf.f(xFit[i]));
		}
		plot.addPoints(xFit, yFit, Plot.LINE);
		currentObjectIndex = plot.getNumPlotObjects()-1;
		labelField.setText("Fit: "+fitName);
		setPlotObjectStyle();
	}

	/** Returns whether the table is non-null and has at least one non-trivial data column */
	private static boolean isValid(ResultsTable rt) {
		if (rt == null) return false;
		String columnHeadingStr = rt.getColumnHeadings();
		if (columnHeadingStr.startsWith(" \t"))
			return columnHeadingStr.length() >=3;
		else
			return columnHeadingStr.length() >= 1;

	}

	/** For "Add from Arrays", sets default columns from the colums titles */
	private void setDefaultColumns(String[] defaultHeadings) {
		if (defaultHeadings == null) return;
		for (int i=0; i<Math.min(defaultHeadings.length, nColumnsToUse); i++) {
			if (defaultHeadings[i] == null || defaultHeadings[i].length()==0)
				previousColumns[i] = 0;
			else
				previousColumns[i] = Math.max(0,  //default column index; index=0 means 'none' for all except y
						getIndex(arrayHeadings, defaultHeadings[i]) + (COLUMN_ALLOW_NONE[i] ? 1 : 0));
		}
	}

	/** Finds the index of a searchTerm in the String array; returns -1 if not found */
	private static int getIndex(String[] strArray, String searchTerm) {
		for (int i=0; i<strArray.length; i++)
			if (strArray[i].equals(searchTerm)) return i;
		return -1;
	}

	/** Sets the 'previousColor', 'previousColor2' with a suggestion for new colors */
	private void suggestColor() {
		boolean[] colorUsed = new boolean[PLOT_COLORS.length];
		String[] designations = plot.getPlotObjectDesignations();
		for (int i=0; i<designations.length; i++) {
			if (!(designations[i].toLowerCase().startsWith("data"))) continue;
			String styleString = plot.getPlotObjectStyle(i);
			for (int j=0; j<PLOT_COLORS.length; j++)
				if (styleString.startsWith(PLOT_COLORS[j]))
					colorUsed[j] = true;
		}
		String newColor = previousColor;
		for (int j=0; j<PLOT_COLORS.length; j++)
			if (!colorUsed[j]) {
				newColor = PLOT_COLORS[j];
				break;
			}
		if (previousColor2 != null && previousColor2.equals(previousColor))  //if fill color was main color, keep it such
			previousColor2 = newColor;
		else if (Colors.decode(previousColor2, null) != null) {              //if we had a separate fill color, make main color brighter
			Color newC = Colors.decode(newColor,Color.BLACK);
			Color newC2 = new Color(makeBrighter(newC.getRed()), makeBrighter(newC.getGreen()), makeBrighter(newC.getBlue()));
			previousColor2 = Colors.colorToString(newC2);
		}
		previousColor = newColor;
	}

	/** Creates an 8-bit color value closer to 255 */
	private static int makeBrighter(int v) {
		return v> 190 ? 255 : 255 - (int)(0.4*(255-v));
	}
}