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;
22
23import java.awt.event.ActionEvent;
24import java.io.File;
25import java.io.PrintWriter;
26
27import javax.swing.AbstractAction;
28
29public class ExportAction extends AbstractAction implements Runnable {
30    private File lastSaveFile;
31    private DumpTableModel dataTableModel;
32
33    public ExportAction(DumpTableModel dataTableModel) {
34        super("Export data");
35        this.dataTableModel = dataTableModel;
36    }
37
38    @Override
39    public void actionPerformed(ActionEvent e) {
40        lastSaveFile = Main.getUI().showSaveDialog();
41        if (lastSaveFile != null) {
42            new Thread(this).start();
43        }
44    }
45
46    @Override
47    public void run() {
48        Main.getUI().showWaitDialog();
49
50        String serialized = DumpDataIO.serialize(dataTableModel.getData());
51
52        if (serialized != null) {
53            try {
54                PrintWriter out = new PrintWriter(lastSaveFile);
55                out.println(serialized);
56                out.close();
57
58                Main.getUI().hideWaitDialog();
59            } catch (Exception e) {
60                Main.getUI().hideWaitDialog();
61                Main.getUI().showMessageDialog("Failed writing: " + e.getMessage());
62            }
63        }
64    }
65}