1 /*
   2  * Copyright (c) 2011, 2015, 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.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package com.sun.javafx.scene.control.skin;
  27 
  28 import java.util.*;
  29 
  30 import javafx.beans.InvalidationListener;
  31 import javafx.beans.WeakInvalidationListener;
  32 import javafx.beans.property.BooleanProperty;
  33 import javafx.beans.property.SimpleBooleanProperty;
  34 import javafx.beans.property.StringProperty;
  35 import javafx.collections.ListChangeListener;
  36 import javafx.collections.WeakListChangeListener;
  37 import javafx.geometry.HPos;
  38 import javafx.geometry.Insets;
  39 import javafx.geometry.Side;
  40 import javafx.geometry.VPos;
  41 import javafx.scene.control.CheckMenuItem;
  42 import javafx.scene.control.ContextMenu;
  43 import javafx.scene.control.Control;
  44 import javafx.scene.control.Label;
  45 import javafx.scene.control.TableColumn;
  46 import javafx.scene.control.TableColumnBase;
  47 import javafx.scene.layout.Pane;
  48 import javafx.scene.layout.Region;
  49 import javafx.scene.layout.StackPane;
  50 import javafx.scene.shape.Rectangle;
  51 
  52 import com.sun.javafx.scene.control.skin.resources.ControlResources;
  53 
  54 /**
  55  * Region responsible for painting the entire row of column headers.
  56  */
  57 public class TableHeaderRow extends StackPane {
  58 
  59     /***************************************************************************
  60      *                                                                         *
  61      * Static Fields                                                           *
  62      *                                                                         *
  63      **************************************************************************/
  64 
  65     private static final String MENU_SEPARATOR = 
  66             ControlResources.getString("TableView.nestedColumnControlMenuSeparator");
  67 
  68 
  69 
  70     /***************************************************************************
  71      *                                                                         *
  72      * Private Fields                                                          *
  73      *                                                                         *
  74      **************************************************************************/
  75 
  76     private final VirtualFlow flow;
  77 
  78     private final TableViewSkinBase tableSkin;
  79 
  80     private Map<TableColumnBase, CheckMenuItem> columnMenuItems = new HashMap<TableColumnBase, CheckMenuItem>();
  81 
  82     // Vertical line that is shown when columns are being reordered
  83 //    private Region columnReorderLine;
  84 
  85     private double scrollX;
  86 
  87     private double tableWidth;
  88 
  89     private Rectangle clip;
  90 
  91     private TableColumnHeader reorderingRegion;
  92 
  93     /**
  94      * This is the ghosted region representing the table column that is being
  95      * dragged. It moves along the x-axis but is fixed in the y-axis.
  96      */
  97     private StackPane dragHeader;
  98     private final Label dragHeaderLabel = new Label();
  99 
 100     /*
 101      * The header row is actually just one NestedTableColumnHeader that spans
 102      * the entire width. Nested within this is the TableColumnHeader's and
 103      * NestedTableColumnHeader's, as necessary. This makes it nice and clean
 104      * to handle column reordering - we basically enforce the rule that column
 105      * reordering only occurs within a single NestedTableColumnHeader, and only
 106      * at that level.
 107      */
 108     private final NestedTableColumnHeader header;
 109 
 110     private Region filler;
 111 
 112     /**
 113      * This is the region where the user can interact with to show/hide columns.
 114      * It is positioned in the top-right hand corner of the TableHeaderRow, and
 115      * when clicked shows a PopupMenu consisting of all leaf columns.
 116      */
 117     private Pane cornerRegion;
 118 
 119     /**
 120      * PopupMenu shown to users to allow for them to hide/show columns in the
 121      * table.
 122      */
 123     private ContextMenu columnPopupMenu;
 124 
 125     private BooleanProperty reordering = new SimpleBooleanProperty(this, "reordering", false) {
 126         @Override protected void invalidated() {
 127             TableColumnHeader r = getReorderingRegion();
 128             if (r != null) {
 129                 double dragHeaderHeight = r.getNestedColumnHeader() != null ?
 130                         r.getNestedColumnHeader().getHeight() :
 131                         getReorderingRegion().getHeight();
 132 
 133                 dragHeader.resize(dragHeader.getWidth(), dragHeaderHeight);
 134                 dragHeader.setTranslateY(getHeight() - dragHeaderHeight);
 135             }
 136             dragHeader.setVisible(isReordering());
 137         }
 138     };
 139 
 140 
 141 
 142     /***************************************************************************
 143      *                                                                         *
 144      * Constructor                                                             *
 145      *                                                                         *
 146      **************************************************************************/
 147     
 148     public TableHeaderRow(final TableViewSkinBase skin) {
 149         this.tableSkin = skin;
 150         this.flow = skin.flow;
 151 
 152         getStyleClass().setAll("column-header-background");
 153 
 154         // clip the header so it doesn't show outside of the table bounds
 155         clip = new Rectangle();
 156         clip.setSmooth(false);
 157         clip.heightProperty().bind(heightProperty());
 158         setClip(clip);
 159 
 160         // listen to table width to keep header in sync
 161         updateTableWidth();
 162         tableSkin.getSkinnable().widthProperty().addListener(weakTableWidthListener);
 163         tableSkin.getSkinnable().paddingProperty().addListener(weakTablePaddingListener);
 164         skin.getVisibleLeafColumns().addListener(weakVisibleLeafColumnsListener);
 165 
 166         // popup menu for hiding/showing columns
 167         columnPopupMenu = new ContextMenu();
 168         updateTableColumnListeners(tableSkin.getColumns(), Collections.<TableColumnBase<?,?>>emptyList());
 169         tableSkin.getVisibleLeafColumns().addListener(weakTableColumnsListener);
 170         tableSkin.getColumns().addListener(weakTableColumnsListener);
 171 
 172         // drag header region. Used to indicate the current column being reordered
 173         dragHeader = new StackPane();
 174         dragHeader.setVisible(false);
 175         dragHeader.getStyleClass().setAll("column-drag-header");
 176         dragHeader.setManaged(false);
 177         dragHeader.getChildren().add(dragHeaderLabel);
 178 
 179         // the header lives inside a NestedTableColumnHeader
 180         header = createRootHeader();
 181         header.setFocusTraversable(false);
 182         header.setTableHeaderRow(this);
 183 
 184         // The 'filler' area that extends from the right-most column to the edge
 185         // of the tableview, or up to the 'column control' button
 186         filler = new Region();
 187         filler.getStyleClass().setAll("filler");
 188 
 189         // Give focus to the table when an empty area of the header row is clicked.
 190         // This ensures the user knows that the table has focus.
 191         setOnMousePressed(e -> {
 192             skin.getSkinnable().requestFocus();
 193         });
 194 
 195         // build the corner region button for showing the popup menu
 196         final StackPane image = new StackPane();
 197         image.setSnapToPixel(false);
 198         image.getStyleClass().setAll("show-hide-column-image");
 199         cornerRegion = new StackPane() {
 200             @Override protected void layoutChildren() {
 201                 double imageWidth = image.snappedLeftInset() + image.snappedRightInset();
 202                 double imageHeight = image.snappedTopInset() + image.snappedBottomInset();
 203                 
 204                 image.resize(imageWidth, imageHeight);
 205                 positionInArea(image, 0, 0, getWidth(), getHeight() - 3, 
 206                         0, HPos.CENTER, VPos.CENTER);
 207             }
 208         };
 209         cornerRegion.getStyleClass().setAll("show-hide-columns-button");
 210         cornerRegion.getChildren().addAll(image);
 211         cornerRegion.setVisible(tableSkin.tableMenuButtonVisibleProperty().get());
 212         tableSkin.tableMenuButtonVisibleProperty().addListener(valueModel -> {
 213             cornerRegion.setVisible(tableSkin.tableMenuButtonVisibleProperty().get());
 214             requestLayout();
 215         });
 216         cornerRegion.setOnMousePressed(me -> {
 217             // show a popupMenu which lists all columns
 218             columnPopupMenu.show(cornerRegion, Side.BOTTOM, 0, 0);
 219             me.consume();
 220         });
 221 
 222         // the actual header
 223         // the region that is anchored above the vertical scrollbar
 224         // a 'ghost' of the header being dragged by the user to force column
 225         // reordering
 226         getChildren().addAll(filler, header, cornerRegion, dragHeader);
 227     }
 228     
 229 
 230 
 231     /***************************************************************************
 232      *                                                                         *
 233      * Listeners                                                               *
 234      *                                                                         *
 235      **************************************************************************/    
 236     
 237     private InvalidationListener tableWidthListener = valueModel -> {
 238         updateTableWidth();
 239     };
 240 
 241     private InvalidationListener tablePaddingListener = valueModel -> {
 242         updateTableWidth();
 243     };
 244     
 245     private ListChangeListener visibleLeafColumnsListener = new ListChangeListener<TableColumn<?,?>>() {
 246         @Override public void onChanged(ListChangeListener.Change<? extends TableColumn<?,?>> c) {
 247             // This is necessary for RT-20300 (but was updated for RT-20840)
 248             header.setHeadersNeedUpdate();
 249         }
 250     };
 251     
 252     private final ListChangeListener tableColumnsListener = c -> {
 253         while (c.next()) {
 254             updateTableColumnListeners(c.getAddedSubList(), c.getRemoved());
 255         }
 256     };
 257     
 258     private final InvalidationListener columnTextListener = observable -> {
 259         TableColumnBase<?,?> column = (TableColumnBase<?,?>) ((StringProperty)observable).getBean();
 260         CheckMenuItem menuItem = columnMenuItems.get(column);
 261         if (menuItem != null) {
 262             menuItem.setText(getText(column.getText(), column));
 263         }
 264     };
 265     
 266     private final WeakInvalidationListener weakTableWidthListener = 
 267             new WeakInvalidationListener(tableWidthListener);
 268 
 269     private final WeakInvalidationListener weakTablePaddingListener =
 270             new WeakInvalidationListener(tablePaddingListener);
 271 
 272     private final WeakListChangeListener weakVisibleLeafColumnsListener =
 273             new WeakListChangeListener(visibleLeafColumnsListener);
 274     
 275     private final WeakListChangeListener weakTableColumnsListener =
 276             new WeakListChangeListener(tableColumnsListener);
 277     
 278     private final WeakInvalidationListener weakColumnTextListener = 
 279             new WeakInvalidationListener(columnTextListener);
 280 
 281 
 282 
 283     /***************************************************************************
 284      *                                                                         *
 285      * Public Methods                                                          *
 286      *                                                                         *
 287      **************************************************************************/
 288 
 289     /** {@inheritDoc} */
 290     @Override protected void layoutChildren() {
 291         double x = scrollX;
 292         double headerWidth = snapSize(header.prefWidth(-1));
 293         double prefHeight = getHeight() - snappedTopInset() - snappedBottomInset();
 294         double cornerWidth = snapSize(flow.getVbar().prefWidth(-1));
 295 
 296         // position the main nested header
 297         header.resizeRelocate(x, snappedTopInset(), headerWidth, prefHeight);
 298         
 299         // position the filler region
 300         final Control control = tableSkin.getSkinnable();
 301         if (control == null) {
 302             return;
 303         }
 304 
 305         final double controlInsets = control.snappedLeftInset() + control.snappedRightInset();
 306         double fillerWidth = tableWidth - headerWidth + filler.getInsets().getLeft() - controlInsets;
 307         fillerWidth -= tableSkin.tableMenuButtonVisibleProperty().get() ? cornerWidth : 0;
 308         filler.setVisible(fillerWidth > 0);
 309         if (fillerWidth > 0) {
 310             filler.resizeRelocate(x + headerWidth, snappedTopInset(), fillerWidth, prefHeight);
 311         }
 312 
 313         // position the top-right rectangle (which sits above the scrollbar)
 314         cornerRegion.resizeRelocate(tableWidth - cornerWidth, snappedTopInset(), cornerWidth, prefHeight);
 315     }
 316 
 317     /** {@inheritDoc} */
 318     @Override protected double computePrefWidth(double height) {
 319         return header.prefWidth(height);
 320     }
 321 
 322     /** {@inheritDoc} */
 323     @Override protected double computeMinHeight(double width) {
 324         return computePrefHeight(width);
 325     }
 326 
 327     /** {@inheritDoc} */
 328     @Override protected double computePrefHeight(double width) {
 329         // we hardcode 24.0 here to avoid RT-37616, where the
 330         // entire header row would disappear when all columns were hidden. 
 331         double headerPrefHeight = header.prefHeight(width);
 332         headerPrefHeight = headerPrefHeight == 0.0 ? 24.0 : headerPrefHeight;
 333         return snappedTopInset() + headerPrefHeight + snappedBottomInset();
 334     }
 335 
 336     // protected to allow subclasses to provide a custom root header
 337     protected NestedTableColumnHeader createRootHeader() {
 338         return new NestedTableColumnHeader(tableSkin, null);
 339     }
 340 
 341     // protected to allow subclasses access to the TableViewSkinBase instance
 342     protected TableViewSkinBase<?,?,?,?,?,?> getTableSkin() {
 343         return this.tableSkin;
 344     }
 345 
 346     // protected to allow subclasses to modify the horizontal scrolling
 347     protected void updateScrollX() {
 348         scrollX = flow.getHbar().isVisible() ? -flow.getHbar().getValue() : 0.0F;
 349         requestLayout();
 350 
 351         // Fix for RT-36392: without this call even though we call requestLayout()
 352         // we don't seem to ever see the layoutChildren() method above called,
 353         // which means the layout is not always updated to use the latest scrollX.
 354         layout();
 355     }
 356 
 357     public final void setReordering(boolean value) {
 358         this.reordering.set(value);
 359     }
 360 
 361     public final boolean isReordering() {
 362         return reordering.get();
 363     }
 364 
 365     public final BooleanProperty reorderingProperty() {
 366         return reordering;
 367     }
 368 
 369     public TableColumnHeader getReorderingRegion() {
 370         return reorderingRegion;
 371     }
 372 
 373     public void setReorderingColumn(TableColumnBase rc) {
 374         dragHeaderLabel.setText(rc == null ? "" : rc.getText());
 375     }
 376 
 377     public void setReorderingRegion(TableColumnHeader reorderingRegion) {
 378         this.reorderingRegion = reorderingRegion;
 379 
 380         if (reorderingRegion != null) {
 381             dragHeader.resize(reorderingRegion.getWidth(), dragHeader.getHeight());
 382         }
 383     }
 384 
 385     public void setDragHeaderX(double dragHeaderX) {
 386         dragHeader.setTranslateX(dragHeaderX);
 387     }
 388 
 389     public NestedTableColumnHeader getRootHeader() {
 390         return header;
 391     }
 392 
 393     // protected to allow subclass to customise the width, to allow for features
 394     // such as row headers
 395     protected void updateTableWidth() {
 396         // snapping added for RT-19428
 397         final Control c = tableSkin.getSkinnable();
 398         if (c == null) {
 399             this.tableWidth = 0;
 400         } else {
 401             Insets insets = c.getInsets() == null ? Insets.EMPTY : c.getInsets();
 402             double padding = snapSize(insets.getLeft()) + snapSize(insets.getRight());
 403             this.tableWidth = snapSize(c.getWidth()) - padding;
 404         }
 405 
 406         clip.setWidth(tableWidth);
 407     }
 408 
 409     public TableColumnHeader getColumnHeaderFor(final TableColumnBase<?,?> col) {
 410         if (col == null) return null;
 411         List<TableColumnBase<?,?>> columnChain = new ArrayList<>();
 412         columnChain.add(col);
 413 
 414         TableColumnBase<?,?> parent = col.getParentColumn();
 415         while (parent != null) {
 416             columnChain.add(0, parent);
 417             parent = parent.getParentColumn();
 418         }
 419 
 420         // we now have a list from top to bottom of a nested column hierarchy,
 421         // and we can now navigate down to retrieve the header with ease
 422         TableColumnHeader currentHeader = getRootHeader();
 423         for (int depth = 0; depth < columnChain.size(); depth++) {
 424             // this is the column we are looking for at this depth
 425             TableColumnBase<?,?> column = columnChain.get(depth);
 426 
 427             // and now we iterate through the nested table column header at this
 428             // level to get the header
 429             currentHeader = getColumnHeaderFor(column, currentHeader);
 430         }
 431         return currentHeader;
 432     }
 433 
 434     public TableColumnHeader getColumnHeaderFor(final TableColumnBase<?,?> col, TableColumnHeader currentHeader) {
 435         if (currentHeader instanceof NestedTableColumnHeader) {
 436             List<TableColumnHeader> headers = ((NestedTableColumnHeader)currentHeader).getColumnHeaders();
 437 
 438             for (int i = 0; i < headers.size(); i++) {
 439                 TableColumnHeader header = headers.get(i);
 440                 if (header.getTableColumn() == col) {
 441                     return header;
 442                 }
 443             }
 444         }
 445 
 446         return null;
 447     }
 448 
 449 
 450     /***************************************************************************
 451      *                                                                         *
 452      * Private Implementation                                                  *
 453      *                                                                         *
 454      **************************************************************************/
 455 
 456     private void updateTableColumnListeners(List<? extends TableColumnBase<?,?>> added, List<? extends TableColumnBase<?,?>> removed) {
 457         // remove binding from all removed items
 458         for (TableColumnBase tc : removed) {
 459             remove(tc);
 460         }
 461 
 462         rebuildColumnMenu();
 463     }
 464 
 465     private void remove(TableColumnBase<?,?> col) {
 466         if (col == null) return;
 467 
 468         CheckMenuItem item = columnMenuItems.remove(col);
 469         if (item != null) {
 470             col.textProperty().removeListener(weakColumnTextListener);
 471             item.selectedProperty().unbindBidirectional(col.visibleProperty());
 472 
 473             columnPopupMenu.getItems().remove(item);
 474         }
 475 
 476         if (! col.getColumns().isEmpty()) {
 477             for (TableColumnBase tc : col.getColumns()) {
 478                 remove(tc);
 479             }
 480         }
 481     }
 482 
 483     private void rebuildColumnMenu() {
 484         columnPopupMenu.getItems().clear();
 485 
 486         for (TableColumnBase<?,?> col : getTableSkin().getColumns()) {
 487             // we only create menu items for leaf columns, visible or not
 488             if (col.getColumns().isEmpty()) {
 489                 createMenuItem(col);
 490             } else {
 491                 List<TableColumnBase<?,?>> leafColumns = getLeafColumns(col);
 492                 for (TableColumnBase<?,?> _col : leafColumns) {
 493                     createMenuItem(_col);
 494                 }
 495             }
 496         }
 497     }
 498 
 499     private List<TableColumnBase<?,?>> getLeafColumns(TableColumnBase<?,?> col) {
 500         List<TableColumnBase<?,?>> leafColumns = new ArrayList<>();
 501 
 502         for (TableColumnBase<?,?> _col : col.getColumns()) {
 503             if (_col.getColumns().isEmpty()) {
 504                 leafColumns.add(_col);
 505             } else {
 506                 leafColumns.addAll(getLeafColumns(_col));
 507             }
 508         }
 509 
 510         return leafColumns;
 511     }
 512 
 513     private void createMenuItem(TableColumnBase<?,?> col) {
 514         CheckMenuItem item = columnMenuItems.get(col);
 515         if (item == null) {
 516             item = new CheckMenuItem();
 517             columnMenuItems.put(col, item);
 518         }
 519 
 520         // bind column text and isVisible so that the menu item is always correct
 521         item.setText(getText(col.getText(), col));
 522         col.textProperty().addListener(weakColumnTextListener);
 523         item.selectedProperty().bindBidirectional(col.visibleProperty());
 524 
 525         columnPopupMenu.getItems().add(item);
 526     }
 527 
 528     /*
 529      * Function used for building the strings in the popup menu
 530      */
 531     private String getText(String text, TableColumnBase col) {
 532         String s = text;
 533         TableColumnBase parentCol = col.getParentColumn();
 534         while (parentCol != null) {
 535             if (isColumnVisibleInHeader(parentCol, tableSkin.getColumns())) {
 536                 s = parentCol.getText() + MENU_SEPARATOR + s;
 537             }
 538             parentCol = parentCol.getParentColumn();
 539         }
 540         return s;
 541     }
 542     
 543     // We need to show strings properly. If a column has a parent column which is
 544     // not inserted into the TableView columns list, it effectively doesn't have
 545     // a parent column from the users perspective. As such, we shouldn't include
 546     // the parent column text in the menu. Fixes RT-14482.
 547     private boolean isColumnVisibleInHeader(TableColumnBase col, List columns) {
 548         if (col == null) return false;
 549         
 550         for (int i = 0; i < columns.size(); i++) {
 551             TableColumnBase column = (TableColumnBase) columns.get(i);
 552             if (col.equals(column)) return true;
 553             
 554             if (! column.getColumns().isEmpty()) {
 555                 boolean isVisible = isColumnVisibleInHeader(col, column.getColumns());
 556                 if (isVisible) return true;
 557             }
 558         }
 559         
 560         return false;
 561     }
 562 }