1/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.preload.actions;
18
19import com.android.preload.DumpDataIO;
20import com.android.preload.DumpTableModel;
21import com.android.preload.Main;
22import java.awt.event.ActionEvent;
23import java.io.File;
24import java.io.PrintWriter;
25
26public class ExportAction extends AbstractThreadedAction {
27    private File lastSaveFile;
28    private DumpTableModel dataTableModel;
29
30    public ExportAction(DumpTableModel dataTableModel) {
31        super("Export data");
32        this.dataTableModel = dataTableModel;
33    }
34
35    @Override
36    public void actionPerformed(ActionEvent e) {
37        lastSaveFile = Main.getUI().showSaveDialog();
38        if (lastSaveFile != null) {
39            super.actionPerformed(e);
40        }
41    }
42
43    @Override
44    public void run() {
45        Main.getUI().showWaitDialog();
46
47        String serialized = DumpDataIO.serialize(dataTableModel.getData());
48
49        if (serialized != null) {
50            try {
51                PrintWriter out = new PrintWriter(lastSaveFile);
52                out.println(serialized);
53                out.close();
54
55                Main.getUI().hideWaitDialog();
56            } catch (Exception e) {
57                Main.getUI().hideWaitDialog();
58                Main.getUI().showMessageDialog("Failed writing: " + e.getMessage());
59            }
60        }
61    }
62}