1554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe/*
2554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe * Copyright (C) 2015 The Android Open Source Project
3554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe *
4554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe * Licensed under the Apache License, Version 2.0 (the "License");
5554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe * you may not use this file except in compliance with the License.
6554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe * You may obtain a copy of the License at
7554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe *
8554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe *      http://www.apache.org/licenses/LICENSE-2.0
9554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe *
10554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe * Unless required by applicable law or agreed to in writing, software
11554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe * distributed under the License is distributed on an "AS IS" BASIS,
12554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe * See the License for the specific language governing permissions and
14554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe * limitations under the License.
15554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe */
16554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe
17554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampepackage com.android.preload.actions;
18554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe
19554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampeimport com.android.preload.DumpDataIO;
20554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampeimport com.android.preload.DumpTableModel;
21554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampeimport com.android.preload.Main;
22554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampeimport java.awt.event.ActionEvent;
23554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampeimport java.io.File;
24554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampeimport java.io.PrintWriter;
25554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe
261c809a3d3240978ab393b48ec5501090ec5a0e57Andreas Gampepublic class ExportAction extends AbstractThreadedAction {
27554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe    private File lastSaveFile;
28554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe    private DumpTableModel dataTableModel;
29554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe
30554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe    public ExportAction(DumpTableModel dataTableModel) {
31554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe        super("Export data");
32554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe        this.dataTableModel = dataTableModel;
33554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe    }
34554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe
35554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe    @Override
36554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe    public void actionPerformed(ActionEvent e) {
37554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe        lastSaveFile = Main.getUI().showSaveDialog();
38554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe        if (lastSaveFile != null) {
391c809a3d3240978ab393b48ec5501090ec5a0e57Andreas Gampe            super.actionPerformed(e);
40554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe        }
41554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe    }
42554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe
43554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe    @Override
44554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe    public void run() {
45554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe        Main.getUI().showWaitDialog();
46554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe
47554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe        String serialized = DumpDataIO.serialize(dataTableModel.getData());
48554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe
49554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe        if (serialized != null) {
50554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe            try {
51554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe                PrintWriter out = new PrintWriter(lastSaveFile);
52554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe                out.println(serialized);
53554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe                out.close();
54554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe
55554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe                Main.getUI().hideWaitDialog();
56554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe            } catch (Exception e) {
57554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe                Main.getUI().hideWaitDialog();
58554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe                Main.getUI().showMessageDialog("Failed writing: " + e.getMessage());
59554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe            }
60554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe        }
61554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe    }
62554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe}