Blame view

src/jsky/navigator/NavigatorImageDisplay.java 13.4 KB
fe0fb87e   Elodie Bourrec   First push to cre...
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
/*
 * $Id: NavigatorImageDisplay.java,v 1.7 2009/04/21 13:31:17 abrighto Exp $
 */

package jsky.navigator;

import java.awt.event.ActionEvent;
import java.awt.geom.AffineTransform;
import java.util.HashSet;
import java.util.Set;

import javax.swing.AbstractAction;
import javax.swing.JFrame;

import jsky.catalog.Catalog;
import jsky.catalog.CatalogDirectory;
import jsky.catalog.TableQueryResult;
import jsky.catalog.QueryResult;
import jsky.catalog.gui.CatalogNavigatorOpener;
import jsky.catalog.gui.TablePlotter;
import jsky.image.fits.codec.FITSImage;
import jsky.image.fits.gui.FITSKeywordsFrame;
import jsky.image.gui.DivaMainImageDisplay;
import jsky.image.gui.PickObjectStatistics;
import jsky.image.gui.ImageDisplayControlFrame;
import jsky.util.I18N;
import jsky.util.Resources;
import jsky.util.gui.DialogUtil;
import jsky.util.gui.SwingUtil;
import jsky.catalog.gui.CatalogNavigator;

/**
 * Extends the DivaMainImageDisplay class by adding support for
 * browsing catalogs and plotting catalog symbols on the image.
 *
 * @author Allan Brighton
 * @version $Revision: 1.7 $
 * 
 * Imported from Jsky library to resolve some catalogs problems.
 * 
 */
@SuppressWarnings("serial")
public class NavigatorImageDisplay extends DivaMainImageDisplay
        implements CatalogNavigatorOpener {

    // Used to access internationalized strings (see i18n/gui*.proprties)
    private static final I18N _I18N = I18N.getInstance(NavigatorImageDisplay.class);

    /**
     * The instance of the catalog navigator to use with this image display.
     */
    private Navigator _navigator;

    /**
     * The Diva pane containing the added catalog symbol layer.
     */
    private NavigatorPane _navigatorPane;

    /**
     * The catalog navigator frame
     */
    private JFrame _navigatorFrame;

    /**
     * Set of filenames: Used to keep track of the files visited in this session.
     */
    private Set<String> _filesVisited = new HashSet<String>();

    /**
     * Action to use to show the catalog window (Browse catalogs)
     */
    private AbstractAction _catalogBrowseAction = new AbstractAction(
            _I18N.getString("browse") + "...",
            Resources.getIcon("Catalog24.gif")) {
        {
            putValue(SHORT_DESCRIPTION, _I18N.getString("showCatalogWindow"));
        }
        @Override
		public void actionPerformed(ActionEvent evt) {
            try {
                openCatalogWindow();
            } catch (Exception e) {
                DialogUtil.error(e);
            }
        }
    };

    /**
     * Action to use to show or hide the WCS grid
     */
    private AbstractAction _gridAction = new AbstractAction(
            _I18N.getString("grid") + "...",
            Resources.getIcon("grid.gif")
    ) {
        {
            putValue(SHORT_DESCRIPTION, _I18N.getString("showGrid"));
        }
        @Override
		public void actionPerformed(ActionEvent evt) {
            try {
                toggleGrid();
            } catch (Exception e) {
                DialogUtil.error(e);
            }
        }
    };

    /**
     * Construct a NavigatorImageDisplay widget.
     */
    public NavigatorImageDisplay() {
        super(new NavigatorPane());
        _navigatorPane = (NavigatorPane) getCanvasPane();
        _navigatorPane.setImageDisplay(this);
    }

    /**
     * @return the Diva pane containing the added catalog symbol layer.
     */
    public NavigatorPane getNavigatorPane() {
        return _navigatorPane;
    }

    /**
     * Open up another window like this one and return a reference to it.
     */
    @Override
	public ImageDisplayControlFrame newWindow() {
        NavigatorImageDisplayFrame f = new NavigatorImageDisplayFrame();
        f.getImageDisplayControl().getImageDisplay().setTitle(getTitle());
        f.setVisible(true);
        return f;
    }

    /**
     * Set the instance of the catalog navigator to use with this image display.
     *
     * @param navigator the instance
     */
    public void setNavigator(Navigator navigator) {
        _navigator = navigator;
        _navigatorFrame = SwingUtil.getFrame(navigator);
    }

    /**
     * @return the instance of the catalog navigator used with this image display.
     */
    public Navigator getNavigator() {
        return _navigator;
    }

    /**
     * Display or hide the WCS grid
     */
    public void toggleGrid() {
        setGridVisible(!isGridVisible());
    }

    /**
     * @return true if the WCS grid is visible
     */
    public boolean isGridVisible() {
        return _navigatorPane.getGridLayer().isVisible();
    }

    /**
     * Sets the visibility of the WCS grid
     *
     * @param visible set to true to show the grid
     */
    public void setGridVisible(boolean visible) {
        if (visible != isGridVisible()) {
            _navigatorPane.getGridLayer().setVisible(visible);
            // Use this if you want to change icons depending on the state of the grid
//            _gridAction.putValue(Action.SMALL_ICON, Resources.getIcon(isGridVisible() ? "gridon.gif" : "gridoff.gif"));
            repaint();
        }
    }

    /**
     * Open the catalog navigator window.
     */
    @Override
	public void openCatalogWindow() {
        if (_navigatorFrame == null) {
            makeNavigatorFrame();
        }
        showNavigatorFrame(null);
    }

    /**
     * Display the interface for the given catalog, if not null, otherwise just
     * open the catalog navigator window.
     */
    @Override
	public void openCatalogWindow(Catalog cat) {
        if (_navigatorFrame == null) {
            makeNavigatorFrame();
        }
        showNavigatorFrame(cat);
    }

    /**
     * Open a catalog window for the named catalog, if found.
     */
    @Override
	public void openCatalogWindow(String name) {
        CatalogDirectory dir = CatalogNavigator.getCatalogDirectory(false);
        Catalog cat = dir.findCatalog(name);
        if (cat != null) {
            openCatalogWindow(cat);
        }
    }

    /**
     * Pop up a file browser to select a local catalog file to open.
     */
    @Override
	public void openLocalCatalog() {
        openCatalogWindow();
        _navigator.open();
    }


    /**
     * Display the FITS table at the given HDU index.
     */
    @Override
	public void displayFITSTable(int hduIndex) {
        FITSImage fitsImage = getFitsImage();
        if (fitsImage != null && hduIndex > 0 && hduIndex < fitsImage.getNumHDUs()) {
            try {
                TableQueryResult table = NavigatorFITSTable.getTable(getFilename(), fitsImage.getFits(), hduIndex);
                showNavigatorFrame(null);
                openCatalogWindow(table.getCatalog());

                // update the FITS header display, if needed
                FITSKeywordsFrame fitsKeywordsFrame = getFitsKeywordsFrame();
                if (fitsKeywordsFrame != null) {
                    fitsKeywordsFrame.getFITSKeywords().updateDisplay(hduIndex);
                }
            } catch (Exception e) {
                DialogUtil.error(this, e);
            }
        }
    }

    /**
     * Deletes the FITS table at the given HDU index.
     */
    @Override
	public void deleteFITSTable(int hduIndex) {
        FITSImage fitsImage = getFitsImage();
        if (fitsImage != null && hduIndex > 0 && hduIndex < fitsImage.getNumHDUs()) {
            try {
                NavigatorFITSTable.deleteTable(fitsImage.getFits(), hduIndex);
                setSaveNeeded(true);
            } catch (Exception e) {
                DialogUtil.error(e);
            }
        }
    }

    /**
     * Save (or update) the given table as a FITS table in the current FITS image.
     *
     * @param table the table to save in the FITS image in a FITS table HDU
     */
    public void saveFITSTable(TableQueryResult table) {
        FITSImage fitsImage = getFitsImage();
        if (fitsImage == null) {
            DialogUtil.error(this, "This operation is only supported on FITS files.");
            return;
        }

        try {
            TableQueryResult newTable = NavigatorFITSTable.saveWithImage(getFilename(), fitsImage.getFits(), table);
            if (newTable == null) {
                return;
            }

            setSaveNeeded(true);
            checkExtensions(true);

            // unplot the original table, since the FITS table will be plotted from now on
            TablePlotter plotter = _navigator.getPlotter();
            if (plotter != null) {
                plotter.unplot(table);
                //plotter.plot(newTable);
                _navigator.setQueryResult(newTable.getCatalog());
            }
        } catch (Exception e) {
            DialogUtil.error(this, e);
        }
    }


    /**
     * If the given catalog argument is null, display the catalog window ("Browse" mode),
     * otherwise query the catalog using the default arguments for the current image.
     *
     * @param cat the catalog
     */
    protected void showNavigatorFrame(Catalog cat) {
        if (cat != null) {
            _navigator.setAutoQuery(true);
            _navigator.setQueryResult(cat);
        } else {
            _navigator.setAutoQuery(false);
            SwingUtil.showFrame(_navigatorFrame);
        }
    }


    /**
     * Makes a NavigatorFrame
     */
    protected void makeNavigatorFrame() {
        _navigator = NavigatorManager.create();
        _navigatorFrame = SwingUtil.getFrame(_navigator);
        _navigator.setImageDisplay(this);
    }


    /**
     * This method is called before and after a new image is loaded, each time
     * with a different argument.
     *
     * @param before set to true before the image is loaded and false afterwards
     */
    @Override
	protected void newImage(boolean before) {
        super.newImage(before);

        if (!before) {
            if (_navigatorFrame == null) {
                makeNavigatorFrame();
            }

            if (_navigatorFrame != null) {
                // Replot any previously plotted catalogs (from any image)
                // in coordinate system of the new image
                TablePlotter plotter = _navigator.getPlotter();
                if (plotter != null) {
                    plotter.replotAll();
                }

                // If this is the first time this image is being visited this session,
                // plot any catalog tables stored as FITS tables
                String filename = getFilename();
                FITSImage fitsImage = getFitsImage();
                if (fitsImage != null && filename != null) {
                    if (!_filesVisited.contains(filename)) {
                        _filesVisited.add(filename);
                        try {
                            NavigatorFITSTable.plotTables(filename, fitsImage.getFits(), _navigator);
                        } catch (Exception e) {
                            DialogUtil.error(this, e);
                        }
                    }
                }
            }
        }
    }


    /**
     * Cleanup when the window is no longer needed.
     */
    @Override
	public void dispose() {
        super.dispose();

        if (_navigatorFrame != null) {
            _navigatorFrame.dispose();
        }
    }

    /**
     * Transform the image graphics using the given AffineTransform.
     */
    @Override
	protected void transformGraphics(AffineTransform trans) {
        super.transformGraphics(trans);
        if (_navigator != null) {
            TablePlotter plotter = _navigator.getPlotter();
            if (plotter != null) {
                plotter.transformGraphics(trans);
            }
        }
    }


    /**
     * Save any current catalog overlays as a FITS table in the image file.
     */
    public void saveCatalogOverlaysWithImage() {
        if (_navigator != null) {
            TablePlotter plotter = _navigator.getPlotter();
            if (plotter != null) {
                TableQueryResult[] tables = plotter.getTables();
                if (tables != null) {
                    for (TableQueryResult table : tables) {
                        saveFITSTable(table);
                    }
                }
            }
        }
    }

    /**
     * Called when an object is selected in the Pick Object window.
     * <p/>
     * Add the currently selected object in the "Pick Object" window to the currently
     * displayed table, or create a new table if none is being displayed.
     */
    @Override
	protected void pickedObject() {
        if (_navigatorFrame == null) {
            makeNavigatorFrame();
        }
        if (_navigator == null) {
            return;
        }
        PickObjectStatistics stats = getPickObjectPanel().getStatistics();
        if (stats == null) {
            DialogUtil.error("No object was selected");
            return;
        }
        _navigator.addPickedObjectToTable(stats, getPickObjectPanel().isUpdate());
    }


    // Other menu and toolbar actions
    public AbstractAction getCatalogBrowseAction() {
        return _catalogBrowseAction;
    }

    public AbstractAction getGridAction() {
        return _gridAction;
    }

    /**
     * Can be overridden in a derived class to filter the result of a catalog query.
     *
     * @param queryResult the result of a catalog query
     * @return the possibly filtered result
     */
    public QueryResult filterQueryResult(QueryResult queryResult) {
        return queryResult;
    }
    
    public static void main(String[] args) {
    	NavigatorImageDisplay display = new NavigatorImageDisplay();
    	display.clear();
//    	final String name = "Digitized Sky at ESO";
    	display.openCatalogWindow();
//    	display.openCatalogWindow(name);
    	
	}
}