Java Code Examples for javafx.collections.FXCollections#sort()

The following examples show how to use javafx.collections.FXCollections#sort() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DataView.java    From chart-fx with Apache License 2.0 6 votes vote down vote up
public void sort() {
    if (isStandalone() || getContentPane() == null || getContentPane().getChildren().isEmpty()) {
        return;
    }

    FXCollections.sort(getContentPane().getChildren(), Comparator.comparing(n -> n.toString().toLowerCase(Locale.UK)));
}
 
Example 2
Source File: CollectionUtilsTests.java    From gef with Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void test_computePreviousList_sort() {
	// fill with unsorted values
	list.setAll(1, 9, 2, 8, 3, 7, 4, 6, 5);
	expectation[0] = list(1, 9, 2, 8, 3, 7, 4, 6, 5);

	// sort ascending
	FXCollections.sort(list);
	expectation[0] = list(1, 2, 3, 4, 5, 6, 7, 8, 9);

	// fill with unsorted values
	list.setAll(1, 9, 2, 8, 3, 7, 4, 6, 5);
	expectation[0] = list(1, 9, 2, 8, 3, 7, 4, 6, 5);

	// TODO sort descending
	FXCollections.sort(list, new Comparator<Integer>() {
		@Override
		public int compare(Integer lhs, Integer rhs) {
			return lhs.compareTo(rhs);
		}
	});
	expectation[0] = list(9, 8, 7, 6, 5, 4, 3, 2, 1);
}
 
Example 3
Source File: ResourceTree.java    From jmonkeybuilder with Apache License 2.0 5 votes vote down vote up
/**
 * Handle a created file.
 *
 * @param file the created file.
 */
@FxThread
public void notifyCreated(@NotNull Path file) {

    var editorConfig = EditorConfig.getInstance();
    var currentAsset = editorConfig.getCurrentAsset();
    var folder = file.getParent();

    if (!folder.startsWith(currentAsset)) {
        return;
    }

    var fileElement = createFor(file);

    var fileItem = findItemForValue(getRoot(), fileElement);
    if (fileItem != null) {
        return;
    }

    var element = createFor(folder);
    var folderItem = findItemForValue(getRoot(), element);

    if (folderItem == null) {
        notifyCreated(folder);
        folderItem = findItemForValue(getRoot(), folder);
    }

    if (folderItem == null) {
        return;
    }

    var newItem = new TreeItem<ResourceElement>(createFor(file));

    fill(newItem);

    var children = folderItem.getChildren();
    children.add(newItem);

    FXCollections.sort(children, ITEM_COMPARATOR);
}
 
Example 4
Source File: ResourceTree.java    From jmonkeybuilder with Apache License 2.0 5 votes vote down vote up
/**
 * Handle the moved file.
 *
 * @param prevFile the prev version.
 * @param newFile  the new version.
 */
@FxThread
public void notifyMoved(@NotNull Path prevFile, @NotNull Path newFile) {

    var prevElement = createFor(prevFile);
    var prevItem = findItemForValue(getRoot(), prevElement);
    if (prevItem == null) {
        return;
    }

    var newParentElement = createFor(newFile.getParent());
    var newParentItem = findItemForValue(getRoot(), newParentElement);
    if (newParentItem == null) {
        return;
    }

    var prevParentItem = prevItem.getParent();
    var prevParentChildren = prevParentItem.getChildren();
    prevParentChildren.remove(prevItem);

    prevItem.setValue(createFor(newFile));

    var children = UiUtils.getAllItems(prevItem);
    children.fastRemove(prevItem);

    fillChildren(prevFile, newFile, children);

    var newParentChildren = newParentItem.getChildren();
    newParentChildren.add(prevItem);

    FXCollections.sort(newParentChildren, ITEM_COMPARATOR);
}
 
Example 5
Source File: JfxTableEditor.java    From milkman with MIT License 4 votes vote down vote up
public void setItems(List<T> items, Comparator<T> comparator) {
		List<RecursiveWrapper<T>> wrappedItems = items.stream().map(i -> new RecursiveWrapper<>(i)).collect(Collectors.toList());
		obsWrappedItems = FXCollections.observableList(wrappedItems);
		if (comparator != null) {
			FXCollections.sort(obsWrappedItems, (ra, rb) -> comparator.compare(ra.getData(), rb.getData()));
		}
		
		
		obsWrappedItems.addListener(new ListChangeListener<RecursiveWrapper<T>>() {

			@Override
			public void onChanged(Change<? extends RecursiveWrapper<T>> c) {
				//forward removals:
				if (!c.next())
					return;
				
				if (c.wasRemoved()) {
					for(var ri : c.getRemoved()) {
						items.remove(ri.getData());
					}
				}
				
				if (c.wasAdded()) {
					RecursiveWrapper<T> newEntry = c.getAddedSubList().get(0);
					items.add(newEntry.getData());
				}
			}
		});
		
		final TreeItem<RecursiveWrapper<T>> root = new RecursiveTreeItem<>(obsWrappedItems, RecursiveTreeObject::getChildren); 
		table.setRoot(root);
		
		Platform.runLater(() -> {
			table.resizeColumns();
		});
		
		//register double-click listener for empty rows, to add a new instance
//		this.setRowFactory(view -> {
//		    TableRow<T> row = new TableRow<T>();
//		    row.setOnMouseClicked(event -> {
//			    if (row.isEmpty() && (event.getClickCount() == 2)) {
//			        T myItem = newItemCreator.get();
//			        getItems().add(myItem);
//			    } 
//			});
//		    return row;
//		});
		
		//we cant click on row if there is no row, so we have to register another event handler
		//for when the table is empty
//		this.setOnMouseClicked(event -> {
//		    if (getItems().isEmpty() && (event.getClickCount() == 2)) {
//		        T myItem = newItemCreator.get();
//		        getItems().add(myItem);
//		    } 
//		});
		
	}
 
Example 6
Source File: ListSelectionDialogDemo.java    From phoebus with Eclipse Public License 1.0 4 votes vote down vote up
public Boolean addSelected(String item)
{
    Boolean result = selected.add(item);
    FXCollections.sort(selected);
    return result;
}
 
Example 7
Source File: CollectionUtils.java    From gef with Eclipse Public License 2.0 3 votes vote down vote up
/**
 * Sorts the given {@link ObservableList} using the default
 * {@link Comparator} .
 *
 * @param <E>
 *            The value type of the {@link ObservableList}.
 * @param observableList
 *            The {@link ObservableList} to sort.
 */
public static <E extends Comparable<? super E>> void sort(
		ObservableList<E> observableList) {
	if (observableList instanceof ObservableListWrapperEx) {
		((ObservableListWrapperEx<? extends E>) observableList).sort();
	} else {
		FXCollections.sort(observableList);
	}
}
 
Example 8
Source File: CollectionUtils.java    From gef with Eclipse Public License 2.0 3 votes vote down vote up
/**
 * Sorts the given {@link ObservableList} using the given {@link Comparator}
 * .
 *
 * @param <E>
 *            The value type of the {@link ObservableList}.
 * @param observableList
 *            The {@link ObservableList} to sort.
 * @param comparator
 *            The {@link Comparator} to use.
 */
public static <E> void sort(ObservableList<E> observableList,
		Comparator<? super E> comparator) {
	if (observableList instanceof ObservableListWrapperEx) {
		((ObservableListWrapperEx<? extends E>) observableList)
				.sort(comparator);
	} else {
		FXCollections.sort(observableList, comparator);
	}
}