~andre-dau/jhelioviewer/welcome

« back to all changes in this revision

Viewing changes to vso/src/org/helioviewer/vso/Manager.java

  • Committer: Helge Dietert
  • Date: 2010-07-21 18:26:09 UTC
  • mfrom: (189.1.11 vsoIntegrated)
  • Revision ID: helge@clem.local-20100721182609-qfmi23gqm0wkmm7n
Integrated VSO Client

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package org.helioviewer.vso;
 
2
 
 
3
import java.awt.event.WindowAdapter;
 
4
import java.awt.event.WindowEvent;
 
5
import java.io.EOFException;
 
6
import java.io.FileInputStream;
 
7
import java.io.FileNotFoundException;
 
8
import java.io.FileOutputStream;
 
9
import java.io.IOException;
 
10
import java.io.InputStream;
 
11
import java.io.ObjectInputStream;
 
12
import java.io.ObjectOutputStream;
 
13
import java.io.OutputStream;
 
14
import java.util.LinkedList;
 
15
import java.util.List;
 
16
 
 
17
import org.helioviewer.base.logging.Log;
 
18
import org.helioviewer.vso.gui.dialogs.RestartDialog;
 
19
import org.virtualsolar.VSO.VSOi.VSOiPort;
 
20
 
 
21
/**
 
22
 * Can be displayed as a window for a control
 
23
 * 
 
24
 * @author Helge Dietert
 
25
 */
 
26
public class Manager {
 
27
    private static final long serialVersionUID = -2521455486509690125L;
 
28
    private final String defaultDir;
 
29
    private final Icons icons;
 
30
    private final List<RequestWindow> openRequests;
 
31
    private final VSOiPort port;
 
32
    private final String restoreFilename;
 
33
 
 
34
    /**
 
35
     * Creates a window to query VSO data
 
36
     * 
 
37
     * @param _port
 
38
     *            Port to query the data
 
39
     * @param restoreFilename
 
40
     *            Filename to restore and save the status, set null to suppress
 
41
     *            the feature
 
42
     * @param defaultDir
 
43
     *            Default Dir to save the queries
 
44
     */
 
45
    public Manager(VSOiPort _port, Icons icons, String restoreFilename, String defaultDir) {
 
46
        this.port = _port;
 
47
        this.restoreFilename = restoreFilename;
 
48
        this.defaultDir = defaultDir;
 
49
        this.icons = icons;
 
50
        openRequests = new LinkedList<RequestWindow>();
 
51
 
 
52
        InputStream fis = null;
 
53
        try {
 
54
            fis = new FileInputStream(restoreFilename);
 
55
            ObjectInputStream o = new ObjectInputStream(fis);
 
56
            try {
 
57
                Query q;
 
58
                boolean askedRestore = false;
 
59
                while (true) {
 
60
                    q = (Query) o.readObject();
 
61
                    if (!askedRestore) {
 
62
                        if (!RestartDialog.askRestore()) {
 
63
                            Log.info("Do not restore from last time");
 
64
                            break;
 
65
                        }
 
66
                        askedRestore = true;
 
67
                    }
 
68
                    newQueryWindow(q);
 
69
                }
 
70
            } catch (EOFException e) {
 
71
                // Expected and fine
 
72
                Log.info("Finished restoring from last time");
 
73
            } catch (ClassNotFoundException e) {
 
74
                Log.error("Error restoring from last time", e);
 
75
            }
 
76
        } catch (FileNotFoundException e) {
 
77
            Log.info("No VSO config found to restore");
 
78
        } catch (IOException e) {
 
79
            Log.error("Error saving VSO state " + e.getMessage());
 
80
        } finally {
 
81
            try {
 
82
                fis.close();
 
83
            } catch (IOException e) {
 
84
                Log.error("Could not close file");
 
85
            } catch (NullPointerException e) {
 
86
                // If the file does not exists
 
87
            }
 
88
        }
 
89
 
 
90
        Thread hook = new Thread(new Runnable() {
 
91
            public void run() {
 
92
                OutputStream fos = null;
 
93
                try {
 
94
                    fos = new FileOutputStream(Manager.this.restoreFilename);
 
95
                    ObjectOutputStream o = new ObjectOutputStream(fos);
 
96
                    for (RequestWindow i : openRequests) {
 
97
                        i.getQueryControl().close();
 
98
                        o.writeObject(i.getQueryControl().getQuery());
 
99
                    }
 
100
                } catch (IOException e) {
 
101
                    Log.error("Error saving VSO state " + e.getMessage());
 
102
                } finally {
 
103
                    try {
 
104
                        fos.close();
 
105
                    } catch (IOException e) {
 
106
                        Log.error("Could not close file");
 
107
                    }
 
108
                }
 
109
            }
 
110
        });
 
111
        Runtime.getRuntime().addShutdownHook(hook);
 
112
    }
 
113
 
 
114
    public String getDefaultDir() {
 
115
        return defaultDir;
 
116
    }
 
117
 
 
118
    public Icons getIcons() {
 
119
        return icons;
 
120
    }
 
121
 
 
122
    public VSOiPort getPort() {
 
123
        return port;
 
124
    }
 
125
 
 
126
    /**
 
127
     * Creates a new window with the right bookkeeping and display it
 
128
     * 
 
129
     * @return The newly created window
 
130
     */
 
131
    public RequestWindow newQueryWindow() {
 
132
        // Build window and show
 
133
        final RequestWindow win = new RequestWindow(this);
 
134
        // Bookkeeping
 
135
        openRequests.add(win);
 
136
        win.addWindowListener(new WindowAdapter() {
 
137
            @Override
 
138
            public void windowClosed(WindowEvent winEvt) {
 
139
                openRequests.remove(win);
 
140
            }
 
141
        });
 
142
        // Show
 
143
        win.pack();
 
144
        win.setVisible(true);
 
145
        return win;
 
146
    }
 
147
    
 
148
    /**
 
149
     * Creates a new window corresponding to a given query
 
150
     * @param query 
 
151
     *          query to display
 
152
     *          @return The newly created window
 
153
     */
 
154
    public RequestWindow newQueryWindow(Query query) {
 
155
        RequestWindow r = newQueryWindow();
 
156
        r.getQueryControl().loadQuery(query);
 
157
        return r;
 
158
    }
 
159
}
 
 
b'\\ No newline at end of file'