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.DumpData;
20import com.android.preload.DumpDataIO;
21import com.android.preload.DumpTableModel;
22import com.android.preload.Main;
23
24import java.awt.event.ActionEvent;
25import java.io.File;
26import java.util.Collection;
27
28import javax.swing.AbstractAction;
29
30public class ImportAction extends AbstractAction implements Runnable {
31    private File[] lastOpenFiles;
32    private DumpTableModel dataTableModel;
33
34    public ImportAction(DumpTableModel dataTableModel) {
35        super("Import data");
36        this.dataTableModel = dataTableModel;
37    }
38
39    @Override
40    public void actionPerformed(ActionEvent e) {
41        lastOpenFiles = Main.getUI().showOpenDialog(true);
42        if (lastOpenFiles != null) {
43            new Thread(this).start();
44        }
45    }
46
47    @Override
48    public void run() {
49        Main.getUI().showWaitDialog();
50
51        try {
52            for (File f : lastOpenFiles) {
53                try {
54                    Collection<DumpData> data = DumpDataIO.deserialize(f);
55
56                    for (DumpData d : data) {
57                        dataTableModel.addData(d);
58                    }
59                } catch (Exception e) {
60                    Main.getUI().showMessageDialog("Failed reading: " + e.getMessage());
61                }
62            }
63        } finally {
64            Main.getUI().hideWaitDialog();
65        }
66
67    }
68}