1 /* 2 * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 package com.sun.swingset3.demos.table; 25 26 import java.util.ArrayList; 27 import java.util.List; 28 29 import javax.swing.table.AbstractTableModel; 30 31 /** 32 * Data model for oscar candidate data: a list of OscarCandidate beans. 33 * 34 * @author aim 35 */ 36 37 public class OscarTableModel extends AbstractTableModel { 38 public static final int CATEGORY_COLUMN = 0; 39 public static final int YEAR_COLUMN = 1; 40 public static final int WINNER_COLUMN = 2; 41 public static final int MOVIE_COLUMN = 3; 42 public static final int PERSONS_COLUMN = 4; 43 public static final int COLUMN_COUNT = 5; 44 45 private final List<OscarCandidate> candidates = new ArrayList<OscarCandidate>(); 46 add(List<OscarCandidate> newCandidates)47 public void add(List<OscarCandidate> newCandidates) { 48 int first = candidates.size(); 49 int last = first + newCandidates.size() - 1; 50 candidates.addAll(newCandidates); 51 fireTableRowsInserted(first, last); 52 } 53 add(OscarCandidate candidate)54 public void add(OscarCandidate candidate) { 55 int index = candidates.size(); 56 candidates.add(candidate); 57 fireTableRowsInserted(index, index); 58 } 59 getRowCount()60 public int getRowCount() { 61 return candidates.size(); 62 } 63 getColumnCount()64 public int getColumnCount() { 65 return COLUMN_COUNT; 66 } 67 68 @Override getColumnClass(int column)69 public Class getColumnClass(int column) { 70 return getValueAt(0, column).getClass(); 71 } 72 getCandidate(int row)73 public OscarCandidate getCandidate(int row) { 74 return candidates.get(row); 75 } 76 getValueAt(int row, int column)77 public Object getValueAt(int row, int column) { 78 OscarCandidate oscarCandidate = candidates.get(row); 79 switch (column) { 80 case CATEGORY_COLUMN: 81 return oscarCandidate.getCategory(); 82 case YEAR_COLUMN: 83 return oscarCandidate.getYear(); 84 case MOVIE_COLUMN: 85 return oscarCandidate.getMovieTitle(); 86 case WINNER_COLUMN: 87 return oscarCandidate.isWinner() ? Boolean.TRUE : Boolean.FALSE; 88 case PERSONS_COLUMN: 89 return oscarCandidate.getPersons(); 90 } 91 return null; 92 } 93 94 } 95