All Downloads are FREE. Search and download functionalities are using the official Maven repository.

nl.cloudfarming.client.fleet.machine.windows.ListOverviewPanel Maven / Gradle / Ivy

/**
 * Copyright (C) 2008-2012 AgroSense Foundation.
 *
 * AgroSense is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * There are special exceptions to the terms and conditions of the GPLv3 as it is applied to
 * this software, see the FLOSS License Exception
 * .
 *
 * AgroSense 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 General Public License
 * along with AgroSense.  If not, see .
 */
package nl.cloudfarming.client.fleet.machine.windows;

import java.util.List;
import javax.swing.GroupLayout;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.table.DefaultTableModel;
import nl.cloudfarming.client.fleet.model.Machine;
import org.jdesktop.swingx.JXTable;
import org.openide.util.NbBundle;
import org.openide.windows.WindowManager;

/**
 * This panel contains a table that shows a list of machines with their
 * properties.
 *
 * @author Wytse Visser
 */
public class ListOverviewPanel extends JPanel {

    private JScrollPane scrollPane;
    private JXTable machineTable;

    public ListOverviewPanel() {
        initComponents();
    }

    private void initComponents() {
        machineTable = new JXTable(new MachineListModel());
        scrollPane = new JScrollPane();
        scrollPane.setViewportView(machineTable);
        scrollPane.setPreferredSize(getMaximumSize());

        setBackground(java.awt.Color.white);

        GroupLayout layout = new GroupLayout(this);
        this.setLayout(layout);

        layout.setHorizontalGroup(
                layout.createParallelGroup(GroupLayout.Alignment.LEADING).addGroup(
                layout.createSequentialGroup().addComponent(scrollPane)));

        layout.setVerticalGroup(
                layout.createParallelGroup(GroupLayout.Alignment.LEADING).addGroup(
                layout.createSequentialGroup().addComponent(scrollPane)));
    }

    /**
     * Updates the list of machines displayed in this panel with the given one.
     *
     * @param machines The machines to show
     */
    public void setMachines(List machines) {
        MachineListModel model = (MachineListModel) machineTable.getModel();
        model.setMachines(machines);
    }

    /**
     * The table model for machines.
     */
    @NbBundle.Messages({"MachineListModel name column name text=Name",
            "MachineListModel type column name text=Type",
            "MachineListModel brand column name text=Brand",
            "MachineListModel model column name text=Model",
            "MachineListModel year column name text=Year",
            "MachineListModel purchase date column name text=Purchase date",
            "MachineListModel purchase price column name text=Purchase price",
            "MachineListModel mileage column name text=Mileage"})
    private static class MachineListModel extends DefaultTableModel {

        private static final String[] COLUMN_NAMES = new String[]{
            Bundle.MachineListModel_name_column_name_text(),
            Bundle.MachineListModel_type_column_name_text(),
            Bundle.MachineListModel_brand_column_name_text(),
            Bundle.MachineListModel_model_column_name_text(),
            Bundle.MachineListModel_year_column_name_text(),
            Bundle.MachineListModel_purchase_date_column_name_text(),
            Bundle.MachineListModel_purchase_price_column_name_text(),
            Bundle.MachineListModel_mileage_column_name_text()};

        public MachineListModel() {
            initColumns();
        }

        private void initColumns() {
            for (String columnName : COLUMN_NAMES) {
                addColumn(columnName);
            }
        }

        /**
         * Updates the machines to the given machines. To prevent concurrency
         * problems, this method is performed when the UI is ready.
         *
         * @param machines The new list of machines
         */
        public void setMachines(final List machines) {
            WindowManager.getDefault().invokeWhenUIReady(
                    new Runnable() {

                        @Override
                        public void run() {
                            //calling removeRow(0) in a loop to clean model is slow.
                        	//remaining items need to be shifted, listeners are fired etc.
                        	//let model do it
                        	setRowCount(0);
                        	                            
                            for (Machine machine : machines) {
                                addRow(new Object[]{
                                            machine.getName(),
                                            machine.getType(),
                                            machine.getBrand(),
                                            machine.getModelNumber(),
                                            machine.getYear(),
                                            machine.getPurchaseDate(),
                                            machine.getPurchasePrice(),
                                            machine.getMileage()
                                        });
                            }
                        }
                    });
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy