1554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe/*
2554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe * Copyright (C) 2015 The Android Open Source Project
3554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe *
4554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe * Licensed under the Apache License, Version 2.0 (the "License");
5554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe * you may not use this file except in compliance with the License.
6554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe * You may obtain a copy of the License at
7554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe *
8554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe *      http://www.apache.org/licenses/LICENSE-2.0
9554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe *
10554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe * Unless required by applicable law or agreed to in writing, software
11554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe * distributed under the License is distributed on an "AS IS" BASIS,
12554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe * See the License for the specific language governing permissions and
14554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe * limitations under the License.
15554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe */
16554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe
17554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampepackage com.android.preload.actions;
18554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe
19554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampeimport com.android.preload.DumpData;
20554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampeimport com.android.preload.DumpTableModel;
21554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampeimport com.android.preload.Main;
22554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe
23554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampeimport java.awt.BorderLayout;
24554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampeimport java.awt.event.ActionEvent;
25554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampeimport java.util.ArrayList;
26554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampeimport java.util.Collections;
27554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampeimport java.util.List;
28554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampeimport java.util.Map;
29554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampeimport java.util.Set;
30554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe
31554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampeimport javax.swing.AbstractAction;
32554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampeimport javax.swing.JFrame;
33554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampeimport javax.swing.JScrollPane;
34554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampeimport javax.swing.JTextArea;
35554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe
36554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampepublic class ShowDataAction extends AbstractAction {
37554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe    private DumpTableModel dataTableModel;
38554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe
39554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe    public ShowDataAction(DumpTableModel dataTableModel) {
40554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe        super("Show data");
41554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe        this.dataTableModel = dataTableModel;
42554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe    }
43554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe
44554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe    @Override
45554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe    public void actionPerformed(ActionEvent e) {
46554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe        // TODO(agampe): Auto-generated method stub
47554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe        int selRow = Main.getUI().getSelectedDataTableRow();
48554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe        if (selRow != -1) {
49554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe            DumpData data = dataTableModel.getData().get(selRow);
50554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe            Map<String, Set<String>> inv = data.invertData();
51554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe
52554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe            StringBuilder builder = new StringBuilder();
53554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe
54554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe            // First bootclasspath.
55554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe            add(builder, "Boot classpath:", inv.get(null));
56554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe
57554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe            // Now everything else.
58554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe            for (String k : inv.keySet()) {
59554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe                if (k != null) {
60554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe                    builder.append("==================\n\n");
61554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe                    add(builder, k, inv.get(k));
62554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe                }
63554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe            }
64554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe
65554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe            JFrame newFrame = new JFrame(data.getPackageName() + " " + data.getDate());
66554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe            newFrame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
67554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe            newFrame.getContentPane().add(new JScrollPane(new JTextArea(builder.toString())),
68554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe                    BorderLayout.CENTER);
69554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe            newFrame.setSize(800, 600);
70554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe            newFrame.setLocationRelativeTo(null);
71554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe            newFrame.setVisible(true);
72554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe        }
73554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe    }
74554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe
75554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe    private void add(StringBuilder builder, String head, Set<String> set) {
76554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe        builder.append(head);
77554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe        builder.append('\n');
78554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe        addSet(builder, set);
79554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe        builder.append('\n');
80554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe    }
81554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe
82554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe    private void addSet(StringBuilder builder, Set<String> set) {
83554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe        if (set == null) {
84554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe            builder.append("  NONE\n");
85554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe            return;
86554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe        }
87554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe        List<String> sorted = new ArrayList<>(set);
88554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe        Collections.sort(sorted);
89554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe        for (String s : sorted) {
90554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe            builder.append(s);
91554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe            builder.append('\n');
92554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe        }
93554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe    }
94554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe}