1 /*
2 * Copyright (c) 2011, 2017, 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 javafx.scene.control;
27
28 import java.lang.ref.WeakReference;
29 import java.util.ArrayList;
30 import java.util.Collection;
31 import java.util.Collections;
32 import java.util.Comparator;
33 import java.util.HashMap;
34 import java.util.LinkedHashSet;
35 import java.util.List;
36 import java.util.Set;
37 import java.util.WeakHashMap;
38
39 import com.sun.javafx.scene.control.Logging;
40 import com.sun.javafx.scene.control.Properties;
41 import com.sun.javafx.scene.control.SelectedCellsMap;
42 import com.sun.javafx.scene.control.SelectedItemsReadOnlyObservableList;
43 import com.sun.javafx.scene.control.behavior.TableCellBehavior;
44 import com.sun.javafx.scene.control.behavior.TableCellBehaviorBase;
45 import javafx.beans.*;
46 import javafx.beans.Observable;
47 import javafx.beans.property.BooleanProperty;
48 import javafx.beans.property.DoubleProperty;
49 import javafx.beans.property.ObjectProperty;
50 import javafx.beans.property.ObjectPropertyBase;
51 import javafx.beans.property.Property;
52 import javafx.beans.property.ReadOnlyObjectProperty;
53 import javafx.beans.property.ReadOnlyObjectWrapper;
54 import javafx.beans.property.SimpleBooleanProperty;
55 import javafx.beans.property.SimpleObjectProperty;
56 import javafx.collections.FXCollections;
57 import javafx.collections.ListChangeListener;
58 import javafx.collections.MapChangeListener;
439 * @since JavaFX 8.0
440 */
441 public static final Callback<TableView, Boolean> DEFAULT_SORT_POLICY = new Callback<TableView, Boolean>() {
442 @Override public Boolean call(TableView table) {
443 try {
444 ObservableList<?> itemsList = table.getItems();
445 if (itemsList instanceof SortedList) {
446 // it is the responsibility of the SortedList to bind to the
447 // comparator provided by the TableView. However, we don't
448 // want to fail the sort (which would put the UI in an
449 // inconsistent state), so we return true here, but only if
450 // the SortedList has its comparator bound to the TableView
451 // comparator property.
452 SortedList sortedList = (SortedList) itemsList;
453 boolean comparatorsBound = sortedList.comparatorProperty().
454 isEqualTo(table.comparatorProperty()).get();
455
456 if (! comparatorsBound) {
457 // this isn't a good situation to be in, so lets log it
458 // out in case the developer is unaware
459 if (Logging.getControlsLogger().isEnabled()) {
460 String s = "TableView items list is a SortedList, but the SortedList " +
461 "comparator should be bound to the TableView comparator for " +
462 "sorting to be enabled (e.g. " +
463 "sortedList.comparatorProperty().bind(tableView.comparatorProperty());).";
464 Logging.getControlsLogger().info(s);
465 }
466 }
467 return comparatorsBound;
468 } else {
469 if (itemsList == null || itemsList.isEmpty()) {
470 // sorting is not supported on null or empty lists
471 return true;
472 }
473
474 Comparator comparator = table.getComparator();
475 if (comparator == null) {
476 return true;
477 }
478
479 // otherwise we attempt to do a manual sort, and if successful
|
1 /*
2 * Copyright (c) 2011, 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. 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 javafx.scene.control;
27
28 import java.lang.ref.WeakReference;
29 import java.util.ArrayList;
30 import java.util.Collection;
31 import java.util.Collections;
32 import java.util.Comparator;
33 import java.util.HashMap;
34 import java.util.LinkedHashSet;
35 import java.util.List;
36 import java.util.Set;
37 import java.util.WeakHashMap;
38
39 import com.sun.javafx.logging.PlatformLogger.Level;
40 import com.sun.javafx.scene.control.Logging;
41 import com.sun.javafx.scene.control.Properties;
42 import com.sun.javafx.scene.control.SelectedCellsMap;
43 import com.sun.javafx.scene.control.SelectedItemsReadOnlyObservableList;
44 import com.sun.javafx.scene.control.behavior.TableCellBehavior;
45 import com.sun.javafx.scene.control.behavior.TableCellBehaviorBase;
46 import javafx.beans.*;
47 import javafx.beans.Observable;
48 import javafx.beans.property.BooleanProperty;
49 import javafx.beans.property.DoubleProperty;
50 import javafx.beans.property.ObjectProperty;
51 import javafx.beans.property.ObjectPropertyBase;
52 import javafx.beans.property.Property;
53 import javafx.beans.property.ReadOnlyObjectProperty;
54 import javafx.beans.property.ReadOnlyObjectWrapper;
55 import javafx.beans.property.SimpleBooleanProperty;
56 import javafx.beans.property.SimpleObjectProperty;
57 import javafx.collections.FXCollections;
58 import javafx.collections.ListChangeListener;
59 import javafx.collections.MapChangeListener;
440 * @since JavaFX 8.0
441 */
442 public static final Callback<TableView, Boolean> DEFAULT_SORT_POLICY = new Callback<TableView, Boolean>() {
443 @Override public Boolean call(TableView table) {
444 try {
445 ObservableList<?> itemsList = table.getItems();
446 if (itemsList instanceof SortedList) {
447 // it is the responsibility of the SortedList to bind to the
448 // comparator provided by the TableView. However, we don't
449 // want to fail the sort (which would put the UI in an
450 // inconsistent state), so we return true here, but only if
451 // the SortedList has its comparator bound to the TableView
452 // comparator property.
453 SortedList sortedList = (SortedList) itemsList;
454 boolean comparatorsBound = sortedList.comparatorProperty().
455 isEqualTo(table.comparatorProperty()).get();
456
457 if (! comparatorsBound) {
458 // this isn't a good situation to be in, so lets log it
459 // out in case the developer is unaware
460 if (Logging.getControlsLogger().isLoggable(Level.INFO)) {
461 String s = "TableView items list is a SortedList, but the SortedList " +
462 "comparator should be bound to the TableView comparator for " +
463 "sorting to be enabled (e.g. " +
464 "sortedList.comparatorProperty().bind(tableView.comparatorProperty());).";
465 Logging.getControlsLogger().info(s);
466 }
467 }
468 return comparatorsBound;
469 } else {
470 if (itemsList == null || itemsList.isEmpty()) {
471 // sorting is not supported on null or empty lists
472 return true;
473 }
474
475 Comparator comparator = table.getComparator();
476 if (comparator == null) {
477 return true;
478 }
479
480 // otherwise we attempt to do a manual sort, and if successful
|