summaryrefslogtreecommitdiff
path: root/src/com/delwink/icebox/swing/ReportResultsDialog.java
blob: 6bdd83f46a4554bbf38aa4a8c68cda1312a7ffb5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/*
 * IceBox - inventory management software for restaurants
 * Copyright (C) 2016 Delwink, LLC
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, version 3 only.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.delwink.icebox.swing;

import com.delwink.icebox.lang.Lang;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;

/**
 * A dialog box for showing table-based report results.
 * @author David McMackins II
 */
public class ReportResultsDialog extends JDialog {
    private final JButton OK_BUTTON;
    private final JTable TABLE;
    
    public ReportResultsDialog(Frame parent, Model model) {
        this(parent, Lang.get("Report.results"), model);
    }
    
    public ReportResultsDialog(Frame parent, String title, Model model) {
        super(parent, title);
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        setModal(true);
        
        TABLE = new JTable(model);
        JScrollPane tablePane = new JScrollPane(TABLE);
        
        OK_BUTTON = new JButton(Lang.get("ok"));
        OK_BUTTON.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                dispose();
            }
        });
        
        JPanel buttonBox = new JPanel(new FlowLayout(FlowLayout.RIGHT));
        buttonBox.add(OK_BUTTON);
        
        setLayout(new BorderLayout());
        add(tablePane, BorderLayout.CENTER);
        add(buttonBox, BorderLayout.SOUTH);
        
        pack();
        centorOnParent();
    }
    
    /**
     * A table model for table-based report results.
     */
    public static final class Model extends AbstractTableModel {
        private final List<String> COLUMN_NAMES;
        private final List<List<Object>> DATA;
        
        /**
         * Creates a new model.
         * @param columnNames A list of the column names.
         * @param data The data of the report arranged by row and column.
         */
        public Model(List<String> columnNames, List<List<Object>> data) {
            COLUMN_NAMES = columnNames;
            DATA = data;
        }
        
        @Override
        public int getRowCount() {
            return DATA.size();
        }

        @Override
        public int getColumnCount() {
            return COLUMN_NAMES.size();
        }
        
        @Override
        public String getColumnName(int columnIndex) {
            return COLUMN_NAMES.get(columnIndex);
        }
        
        @Override
        public Class<?> getColumnClass(int columnIndex) {
            return (DATA.isEmpty() || DATA.get(0).isEmpty()) ? String.class : DATA.get(0).get(columnIndex).getClass();
        }

        @Override
        public Object getValueAt(int row, int col) {
            return DATA.get(row).get(col);
        }
    }
}