Andreas Gampe | 554d7ee | 2015-09-15 08:57:12 -0700 | [diff] [blame] | 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 | |
| 17 | package com.android.preload.actions; |
| 18 | |
| 19 | import com.android.preload.DumpData; |
| 20 | import com.android.preload.DumpDataIO; |
| 21 | import com.android.preload.DumpTableModel; |
| 22 | import com.android.preload.Main; |
| 23 | |
| 24 | import java.awt.event.ActionEvent; |
| 25 | import java.io.File; |
| 26 | import java.util.Collection; |
| 27 | |
| 28 | import javax.swing.AbstractAction; |
| 29 | |
Andreas Gampe | 1c809a3 | 2016-11-28 15:04:25 -0800 | [diff] [blame] | 30 | public class ImportAction extends AbstractThreadedAction { |
Andreas Gampe | 554d7ee | 2015-09-15 08:57:12 -0700 | [diff] [blame] | 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) { |
Andreas Gampe | 1c809a3 | 2016-11-28 15:04:25 -0800 | [diff] [blame] | 43 | super.actionPerformed(e); |
Andreas Gampe | 554d7ee | 2015-09-15 08:57:12 -0700 | [diff] [blame] | 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 | } |