Coverage Report - org.openpermis.editor.policy.gui.checklist.CheckList
 
Classes in this File Line Coverage Branch Coverage Complexity
CheckList
0%
0/106
0%
0/44
2.034
CheckList$1
0%
0/6
0%
0/2
2.034
 
 1  
 /*
 2  
  * Copyright (c) 2009, Swiss Federal Department of Defence Civil Protection and Sport
 3  
  *                     (http://www.vbs.admin.ch)
 4  
  * Copyright (c) 2009, Ergon Informatik AG (http://www.ergon.ch)
 5  
  * All rights reserved.
 6  
  *
 7  
  * Licensed under the Open Permis License which accompanies this distribution,
 8  
  * and is available at http://www.openpermis.org/BSDlicenceKent.txt
 9  
  */
 10  
 package org.openpermis.editor.policy.gui.checklist;
 11  
 
 12  
 import java.awt.Component;
 13  
 import java.awt.Container;
 14  
 import java.awt.Dimension;
 15  
 import java.awt.EventQueue;
 16  
 import java.awt.Rectangle;
 17  
 import java.awt.event.ActionEvent;
 18  
 import java.awt.event.ComponentEvent;
 19  
 import java.awt.event.ComponentListener;
 20  
 import java.awt.event.MouseEvent;
 21  
 import java.util.Collections;
 22  
 import java.util.EventObject;
 23  
 
 24  
 import javax.swing.Action;
 25  
 import javax.swing.JScrollPane;
 26  
 import javax.swing.JTable;
 27  
 import javax.swing.JViewport;
 28  
 import javax.swing.UIManager;
 29  
 import javax.swing.border.Border;
 30  
 import javax.swing.event.AncestorEvent;
 31  
 import javax.swing.event.AncestorListener;
 32  
 import javax.swing.plaf.UIResource;
 33  
 import javax.swing.table.TableCellRenderer;
 34  
 import javax.swing.table.TableColumn;
 35  
 import javax.swing.table.TableModel;
 36  
 
 37  
 import org.jdesktop.observablecollections.ObservableCollections;
 38  
 import org.jdesktop.observablecollections.ObservableList;
 39  
 
 40  
 /**
 41  
  * Editor for two observable lists.
 42  
  * @param <T> type of the items in the check list.
 43  
  * @since 0.1.0
 44  
  */
 45  0
 public class CheckList<T>
 46  
         extends JTable
 47  
         implements AncestorListener, ComponentListener
 48  
 {
 49  
 
 50  
         //---- Static
 51  
 
 52  
         /**
 53  
          * @since 0.1.0
 54  
          */
 55  
         private static final long serialVersionUID = -4074036755955844894L;
 56  
 
 57  
         /**
 58  
          * Additional spacing for the first row.
 59  
          * @since 0.1.0
 60  
          */
 61  
         private static final int FIRST_ROW_SPACING = 8;
 62  
 
 63  
         /**
 64  
          * Minimum column width for empty columns.
 65  
          * @since 0.1.0
 66  
          */
 67  
         private static final int MIN_COLUMN_WIDTH = 16;
 68  
 
 69  
         //---- State
 70  
 
 71  
         /**
 72  
          * The action to trigger on a double click.
 73  
          * @since 0.1.0
 74  
          */
 75  
         private Action doubleClickAction;
 76  
         
 77  
         /**
 78  
          * The item renderer for this checklist.
 79  
          * @since 0.1.0
 80  
          */
 81  
         private TableCellRenderer customCellRenderer;
 82  
         
 83  
         //---- Constructors
 84  
 
 85  
         /**
 86  
          * Creates a new unbound check list.
 87  
          * @see #bind(ObservableList, ObservableList)
 88  
          * @since 0.1.0
 89  
          */
 90  
         public CheckList () {
 91  0
                 this(
 92  
                         ObservableCollections.observableList(Collections.<T>emptyList()),
 93  
                         ObservableCollections.observableList(Collections.<T>emptyList())
 94  
                 );
 95  0
         }
 96  
 
 97  
         /**
 98  
          * Creates a new checklist for the specified pool and selection list.
 99  
          * @param pool the available list items shown.
 100  
          * @param selection the list of selected items from the pool.
 101  
          * @since 0.1.0
 102  
          */
 103  
         public CheckList (ObservableList<T> pool, ObservableList<T> selection) {
 104  0
                 super(new CheckListModel<T>(pool, selection));
 105  0
                 addAncestorListener(this);
 106  0
                 setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
 107  0
                 setIntercellSpacing(new Dimension(0, 0));
 108  0
                 setShowGrid(false);
 109  0
                 setColumnSelectionAllowed(false);
 110  0
         }
 111  
 
 112  
         //---- Methods
 113  
         
 114  
         /**
 115  
          * Sets the renderer to be used for check list items.
 116  
          * @param itemRenderer the item renderer.
 117  
          * @since 0.1.0
 118  
          */
 119  
         public void setItemRenderer (TableCellRenderer itemRenderer) {
 120  0
                 this.customCellRenderer = itemRenderer;
 121  0
         }
 122  
 
 123  
         /**
 124  
          * Sets the action to be executed if a double click is performed on a non-check column.
 125  
          * @param action the action to be executed.
 126  
          * @since 0.1.0
 127  
          */
 128  
         public void setDoubleClickAction (Action action) {
 129  0
                 this.doubleClickAction = action;
 130  0
         }
 131  
 
 132  
         /**
 133  
          * Binds the check list to the specified pool and selection list.
 134  
          * @param pool the available list items shown.
 135  
          * @param selection the list of selected items from the pool.
 136  
          * @since 0.1.0
 137  
          */
 138  
         public void bind (ObservableList<T> pool, ObservableList<T> selection) {
 139  0
                 setModel(new CheckListModel<T>(pool, selection));
 140  0
         }
 141  
 
 142  
         /**
 143  
          * Sets the number of visible rows that the list shows by preference.
 144  
          * @param rows the number of rows requested.
 145  
          * @since 0.1.0
 146  
          */
 147  
         public void setVisibleRowCount (int rows) {
 148  0
                 final int width = getPreferredSize().width;
 149  0
                 final int height = rows * getRowHeight();
 150  0
                 setPreferredScrollableViewportSize(new Dimension(width, height));
 151  0
         }
 152  
 
 153  
         /**
 154  
          * Adds listeners to the model that should be removed if the list disappears.
 155  
          * @since 0.1.0
 156  
          */
 157  
         private void addModelListeners () {
 158  0
                 getModel().configureListeners();
 159  0
         }
 160  
 
 161  
         /**
 162  
          * Removes listeners that should only be active while the list is visible.
 163  
          * @since 0.1.0
 164  
          */
 165  
         private void removeModelListeners () {
 166  0
                 getModel().unconfigureListeners();
 167  0
         }
 168  
 
 169  
         /**
 170  
          * Calculates the maximum width of the specified column.
 171  
          * @param column the column for which to determine the maximum width.
 172  
          * @return the width requested.
 173  
          * @since 0.1.0
 174  
          */
 175  
         private int getColumnWidth (int column) {
 176  0
                 int width = MIN_COLUMN_WIDTH;
 177  0
                 final TableCellRenderer renderer = getDefaultRenderer(getColumnClass(column));
 178  0
                 for (int row = 0 ; row < getRowCount() ; row++) {
 179  0
                         final Component component = renderer.getTableCellRendererComponent(
 180  
                                 this, getValueAt(row, column), false, false, row, column
 181  
                         );
 182  0
                         width = Math.max(width, component.getPreferredSize().width);
 183  
                 }
 184  0
                 return width;
 185  
         }
 186  
 
 187  
         /**
 188  
          * Returns the view of the enclosing viewport or the width of this table.
 189  
          * @return the width currently showing.
 190  
          * @since 0.1.0
 191  
          */
 192  
         private int getViewportWidth () {
 193  0
                 final Container p = getParent();
 194  0
                 if (p instanceof JViewport) {
 195  0
                         return p.getWidth();
 196  
                 }
 197  0
                 return getWidth();
 198  
         }
 199  
 
 200  
         /**
 201  
          * Modifies the column widths according to the data in the table.
 202  
          * @since 0.1.0
 203  
          */
 204  
         private void layoutColumns () {
 205  0
                 final int viewportWidth = getViewportWidth();
 206  0
                 int totalWidth = 0;
 207  0
                 for (int i = 0 ; i < getColumnModel().getColumnCount() ; i++) {
 208  0
                         final TableColumn column = getColumnModel().getColumn(i);
 209  0
                         int width = getColumnWidth(i);
 210  0
                         if (i == 0) {
 211  
                                 // First column gets extra slack space.
 212  0
                                 width += FIRST_ROW_SPACING;
 213  0
                         } else if (i == getColumnModel().getColumnCount() - 1) {
 214  
                                 // Last column fills to viewport width if necessary.
 215  0
                                 if (totalWidth + width < viewportWidth) {
 216  0
                                         width = viewportWidth - totalWidth;
 217  
                                 }
 218  
                         }
 219  0
                         column.setPreferredWidth(width);
 220  0
                         column.setWidth(width);
 221  0
                         totalWidth += width;
 222  
                 }
 223  0
         }
 224  
 
 225  
         /**
 226  
          * @since 0.1.0
 227  
          */
 228  
         @SuppressWarnings("unchecked")
 229  
         public T getActivePart () {
 230  0
                 if (this.getSelectedRow() != -1) {
 231  
                         // keep this:
 232  
                         // same as:
 233  
                         // getPresenter().getTargetPool().getPoolList().get(this.targetList.getSelectedRow());
 234  0
                         return (T) this.getValueAt(
 235  
                                 this.getSelectionModel().getAnchorSelectionIndex(), 1);
 236  
                 }
 237  0
                 return null;
 238  
         }
 239  
 
 240  
         /**
 241  
          * Toggles the selection state of the specified row.
 242  
          * <p>Toggling occurs at the end of all actions that are currently on the dispatch queue.</p>
 243  
          * @param row the row to toggle.
 244  
          * @since 0.3.0
 245  
          */
 246  
         private void toggle (final int row) {
 247  0
                 EventQueue.invokeLater(
 248  
                         new Runnable() {
 249  0
                                 public void run () {
 250  0
                                         final Boolean value = (Boolean) getModel().getValueAt(row, 0);
 251  0
                                         getModel().setValueAt(Boolean.valueOf(!value.booleanValue()), row, 0);
 252  0
                                         getSelectionModel().setSelectionInterval(row, row);
 253  0
                                         repaint();
 254  0
                                 }
 255  
                         }
 256  
                 );
 257  0
         }
 258  
         
 259  
         //---- JTable
 260  
 
 261  
         /**
 262  
          * @since 0.1.0
 263  
          */
 264  
         @Override
 265  
         public void setModel (TableModel dataModel) {
 266  0
                 if (dataModel instanceof CheckListModel) {
 267  0
                         super.setModel(dataModel);
 268  0
                         return;
 269  
                 }
 270  0
                 throw new IllegalStateException("Change of model not allowed.");
 271  
         }
 272  
 
 273  
         /**
 274  
          * @since 0.1.0
 275  
          */
 276  
         @SuppressWarnings("unchecked")
 277  
         @Override
 278  
         public CheckListModel<T> getModel () {
 279  0
                 return (CheckListModel<T>) super.getModel();
 280  
         }
 281  
 
 282  
         /**
 283  
          * @since 0.1.0
 284  
          */
 285  
         @Override
 286  
         public TableCellRenderer getCellRenderer (int row, int column) {
 287  0
                 if (this.customCellRenderer != null && getModel().isValueColumn(column)) {
 288  0
                         return this.customCellRenderer;
 289  
                 }
 290  0
                 return super.getCellRenderer(row, column);
 291  
         }
 292  
 
 293  
         /**
 294  
          * @since 0.1.0
 295  
          */
 296  
         @Override
 297  
         public void doLayout () {
 298  0
                 super.doLayout();
 299  0
                 layoutColumns();
 300  0
         }
 301  
 
 302  
         /**
 303  
          * @since 0.1.0
 304  
          */
 305  
         @Override
 306  
         public boolean editCellAt (int row, int column, EventObject e) {
 307  0
                 if (column == 0) {
 308  0
                         toggle(row);
 309  0
                         return false;
 310  0
                 } else if (e instanceof MouseEvent && ((MouseEvent) e).getClickCount() == 2) {
 311  0
                         if (this.doubleClickAction != null) {
 312  0
                                 this.doubleClickAction.actionPerformed(
 313  
                                         new ActionEvent(this, ActionEvent.ACTION_PERFORMED, "")
 314  
                                 );
 315  0
                                 return false;
 316  
                         }
 317  
                 }
 318  0
                 return super.editCellAt(row, column, e);
 319  
         }
 320  
 
 321  
         /**
 322  
          * Disables the header view of this table.
 323  
          * @since 0.1.0
 324  
          */
 325  
         @Override
 326  
         protected void configureEnclosingScrollPane () {
 327  0
                 final Container p = getParent();
 328  0
                 if (p instanceof JViewport) {
 329  0
                         p.addComponentListener(this);
 330  0
                         final Container gp = p.getParent();
 331  0
                         if (gp instanceof JScrollPane) {
 332  0
                                 final JScrollPane scrollPane = (JScrollPane) gp;
 333  0
                                 final JViewport viewport = scrollPane.getViewport();
 334  0
                                 if (viewport == null || viewport.getView() != this) {
 335  0
                                         return;
 336  
                                 }
 337  0
                                 viewport.setBackground(getBackground());
 338  0
                                 final Border border = scrollPane.getBorder();
 339  0
                                 if (border == null || border instanceof UIResource) {
 340  0
                                         scrollPane.setBorder(UIManager.getBorder("Table.scrollPaneBorder"));
 341  
                                 }
 342  
                         }
 343  
                 }
 344  0
         }
 345  
 
 346  
         /**
 347  
          * @since 0.1.0
 348  
          */
 349  
         @Override
 350  
         protected void unconfigureEnclosingScrollPane () {
 351  0
                 Container p = getParent();
 352  0
                 if (p instanceof JViewport) {
 353  0
                         p.removeComponentListener(this);
 354  
                 }
 355  0
         }
 356  
 
 357  
         /**
 358  
          * @since 0.1.0
 359  
          */
 360  
         @Override
 361  
         public void scrollRectToVisible (Rectangle rect) {
 362  
                 final Rectangle scrollRect;
 363  0
                 if (rect.x > 0) {
 364  0
                         scrollRect = new Rectangle(0, rect.y, rect.width + rect.x, rect.height);
 365  
                 } else {
 366  0
                         scrollRect = rect;
 367  
                 }
 368  0
                 super.scrollRectToVisible(scrollRect);
 369  0
         }
 370  
 
 371  
         //---- AncestorListener
 372  
 
 373  
         /**
 374  
          * @since 0.1.0
 375  
          */
 376  
         public void ancestorAdded (AncestorEvent event) {
 377  0
                 addModelListeners();
 378  0
         }
 379  
 
 380  
         /**
 381  
          * @since 0.1.0
 382  
          */
 383  
         public void ancestorMoved (AncestorEvent event) {
 384  
                 // Nop.
 385  0
         }
 386  
 
 387  
         /**
 388  
          * @since 0.1.0
 389  
          */
 390  
         public void ancestorRemoved (AncestorEvent event) {
 391  0
                 removeModelListeners();
 392  0
         }
 393  
 
 394  
         //---- ComponentListener
 395  
 
 396  
         /**
 397  
          * @since 0.1.0
 398  
          */
 399  
         public void componentHidden (ComponentEvent e) {
 400  
                 // Nop.
 401  0
         }
 402  
 
 403  
         /**
 404  
          * @since 0.1.0
 405  
          */
 406  
         public void componentMoved (ComponentEvent e) {
 407  
                 // Nop.
 408  0
         }
 409  
 
 410  
         /**
 411  
          * @since 0.1.0
 412  
          */
 413  
         public void componentResized (ComponentEvent e) {
 414  0
                 doLayout();
 415  0
                 resizeAndRepaint();
 416  0
         }
 417  
 
 418  
         /**
 419  
          * @since 0.1.0
 420  
          */
 421  
         public void componentShown (ComponentEvent e) {
 422  
                 // Nop.
 423  0
         }
 424  
 
 425  
 }