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.DumpTableModel;
21import com.android.preload.Main;
22
23import java.awt.BorderLayout;
24import java.awt.event.ActionEvent;
25import java.util.ArrayList;
26import java.util.Collections;
27import java.util.List;
28import java.util.Map;
29import java.util.Set;
30
31import javax.swing.AbstractAction;
32import javax.swing.JFrame;
33import javax.swing.JScrollPane;
34import javax.swing.JTextArea;
35
36public class ShowDataAction extends AbstractAction {
37    private DumpTableModel dataTableModel;
38
39    public ShowDataAction(DumpTableModel dataTableModel) {
40        super("Show data");
41        this.dataTableModel = dataTableModel;
42    }
43
44    @Override
45    public void actionPerformed(ActionEvent e) {
46        // TODO(agampe): Auto-generated method stub
47        int selRow = Main.getUI().getSelectedDataTableRow();
48        if (selRow != -1) {
49            DumpData data = dataTableModel.getData().get(selRow);
50            Map<String, Set<String>> inv = data.invertData();
51
52            StringBuilder builder = new StringBuilder();
53
54            // First bootclasspath.
55            add(builder, "Boot classpath:", inv.get(null));
56
57            // Now everything else.
58            for (String k : inv.keySet()) {
59                if (k != null) {
60                    builder.append("==================\n\n");
61                    add(builder, k, inv.get(k));
62                }
63            }
64
65            JFrame newFrame = new JFrame(data.getPackageName() + " " + data.getDate());
66            newFrame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
67            newFrame.getContentPane().add(new JScrollPane(new JTextArea(builder.toString())),
68                    BorderLayout.CENTER);
69            newFrame.setSize(800, 600);
70            newFrame.setLocationRelativeTo(null);
71            newFrame.setVisible(true);
72        }
73    }
74
75    private void add(StringBuilder builder, String head, Set<String> set) {
76        builder.append(head);
77        builder.append('\n');
78        addSet(builder, set);
79        builder.append('\n');
80    }
81
82    private void addSet(StringBuilder builder, Set<String> set) {
83        if (set == null) {
84            builder.append("  NONE\n");
85            return;
86        }
87        List<String> sorted = new ArrayList<>(set);
88        Collections.sort(sorted);
89        for (String s : sorted) {
90            builder.append(s);
91            builder.append('\n');
92        }
93    }
94}