View Javadoc
1   /*
2   
3       dsh-eventlist-view  Views for event lists.
4       Copyright (c) 2010-2019 held jointly by the individual authors.
5   
6       This library is free software; you can redistribute it and/or modify it
7       under the terms of the GNU Lesser General Public License as published
8       by the Free Software Foundation; either version 3 of the License, or (at
9       your option) any later version.
10  
11      This library is distributed in the hope that it will be useful, but WITHOUT
12      ANY WARRANTY; with out even the implied warranty of MERCHANTABILITY or
13      FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
14      License for more details.
15  
16      You should have received a copy of the GNU Lesser General Public License
17      along with this library;  if not, write to the Free Software Foundation,
18      Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA.
19  
20      > http://www.fsf.org/licensing/licenses/lgpl.html
21      > http://www.opensource.org/licenses/lgpl-license.php
22  
23  */
24  package org.dishevelled.eventlist.view;
25  
26  import java.awt.BorderLayout;
27  
28  import java.util.List;
29  
30  import javax.swing.JPanel;
31  import javax.swing.JScrollPane;
32  import javax.swing.JTable;
33  
34  import ca.odell.glazedlists.EventList;
35  import ca.odell.glazedlists.SortedList;
36  
37  import ca.odell.glazedlists.gui.AbstractTableComparatorChooser;
38  import ca.odell.glazedlists.gui.TableFormat;
39  
40  import ca.odell.glazedlists.swing.GlazedListsSwing;
41  import ca.odell.glazedlists.swing.TableComparatorChooser;
42  
43  import org.dishevelled.identify.ContextMenuListener;
44  import org.dishevelled.identify.StripeTableCellRenderer;
45  
46  /**
47   * Elements table.
48   *
49   * @param <E> model element type
50   * @author  Michael Heuer
51   */
52  public class ElementsTable<E>
53      extends AbstractEventListView<E>
54  {
55      /** Table. */
56      private final JTable table;
57  
58  
59      /**
60       * Create a new elements table view with the specified model and table format.
61       *
62       * @param model model, must not be null
63       * @param tableFormat table format, must not be null
64       */
65      public ElementsTable(final EventList<E> model, final TableFormat<E> tableFormat)
66      {
67          super(new SortedList<E>(model, null));
68          if (tableFormat == null)
69          {
70              throw new IllegalArgumentException("tableFormat must not be null");
71          }
72          table = new JTable(GlazedListsSwing.eventTableModelWithThreadProxyList(getModel(), tableFormat));
73          TableComparatorChooser.install(table,
74                                         (SortedList<E>) getModel(),
75                                         AbstractTableComparatorChooser.MULTIPLE_COLUMN_MOUSE);
76  
77          StripeTableCellRenderer.install(table);
78          table.setSelectionModel(getListSelectionModelAdapter());
79          table.addMouseListener(new ContextMenuListener(getContextMenu()));
80  
81          setLayout(new BorderLayout());
82          add("North", createToolBarPanel());
83          add("Center", createTablePanel());
84      }
85  
86      /**
87       * Create a new elements table view with the specified model and table format.
88       *
89       * @param labelText label text
90       * @param model model, must not be null
91       * @param tableFormat table format, must not be null
92       */
93      public ElementsTable(final String labelText, final EventList<E> model, final TableFormat<E> tableFormat)
94      {
95          this(model, tableFormat);
96          getLabel().setText(labelText);
97      }
98  
99  
100     @Override
101     protected void cut(final List<E> toCut)
102     {
103         // empty
104     }
105 
106     @Override
107     protected void copy(final List<E> toCopy)
108     {
109         // empty
110     }
111 
112     @Override
113     public void add()
114     {
115         // empty
116     }
117 
118     @Override
119     public void paste()
120     {
121         // empty
122     }
123 
124     /**
125      * Return the table for this elements table.
126      *
127      * @return the table for this elements table
128      */
129     protected final JTable getTable()
130     {
131         return table;
132     }
133 
134     /**
135      * Create and return a new table panel.
136      *
137      * @return a new table panel
138      */
139     private JPanel createTablePanel()
140     {
141         JPanel panel = new JPanel();
142         panel.setOpaque(false);
143         panel.setLayout(new BorderLayout());
144         panel.add("Center", new JScrollPane(table));
145         return panel;
146     }
147 }