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