Skip to content

Commit

Permalink
refactor: internal cleanup of Library (gluonhq#517)
Browse files Browse the repository at this point in the history
  • Loading branch information
AlmasB authored Feb 18, 2022
1 parent a586380 commit a05b8fd
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 20 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2016, 2020, Gluon and/or its affiliates.
* Copyright (c) 2016, 2022, Gluon and/or its affiliates.
* Copyright (c) 2012, 2014, Oracle and/or its affiliates.
* All rights reserved. Use is subject to license terms.
*
Expand Down Expand Up @@ -403,7 +403,7 @@ private void addCustomizedItem(Class<?> componentClass, String section,
private void addItem(String name, String fxmlText, String section, String iconName) {
final URL iconURL = ImageUtils.getNodeIconURL(iconName + ".png"); //NOI18N
final LibraryItem item = new LibraryItem(name, section, fxmlText, iconURL, this);
itemsProperty.add(item);
getItems().add(item);
}


Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/*
* Copyright (c) 2022, Gluon and/or its affiliates.
* Copyright (c) 2012, 2014, Oracle and/or its affiliates.
* All rights reserved. Use is subject to license terms.
*
Expand Down Expand Up @@ -39,31 +40,28 @@
import javafx.collections.ObservableList;

/**
*
*
* A collection of [LibraryItem].
*/
public abstract class Library {

protected final ObservableList<LibraryItem> itemsProperty
= FXCollections.observableArrayList();
protected final ObjectProperty<ClassLoader> classLoaderProperty
= new SimpleObjectProperty<>();
private final ObservableList<LibraryItem> itemsProperty = FXCollections.observableArrayList();
private final ObjectProperty<ClassLoader> classLoaderProperty = new SimpleObjectProperty<>();

/*
* Public
*/

public ObservableList<LibraryItem> getItems() {
return itemsProperty;
}

public ReadOnlyProperty<ClassLoader> classLoaderProperty() {
return classLoaderProperty;
}

public ClassLoader getClassLoader() {
return classLoaderProperty.getValue();
}

protected void setClassLoader(ClassLoader loader) {
classLoaderProperty.set(loader);
}

public abstract Comparator<String> getSectionComparator();
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2017, Gluon and/or its affiliates.
* Copyright (c) 2017, 2022, Gluon and/or its affiliates.
* Copyright (c) 2012, 2014, Oracle and/or its affiliates.
* All rights reserved. Use is subject to license terms.
*
Expand Down Expand Up @@ -326,17 +326,17 @@ void updateFxmlFileReports(Collection<Path> newFxmlFileReports) {

void setItems(Collection<LibraryItem> items) {
if (Platform.isFxApplicationThread()) {
itemsProperty.setAll(items);
getItems().setAll(items);
} else {
Platform.runLater(() -> itemsProperty.setAll(items));
Platform.runLater(() -> getItems().setAll(items));
}
}

void addItems(Collection<LibraryItem> items) {
if (Platform.isFxApplicationThread()) {
itemsProperty.addAll(items);
getItems().addAll(items);
} else {
Platform.runLater(() -> itemsProperty.addAll(items));
Platform.runLater(() -> getItems().addAll(items));
}
}

Expand Down Expand Up @@ -404,7 +404,7 @@ private void changeClassLoader(ClassLoader newClassLoader) {
* we invoke URLClassLoader.close() on the existing one
* so that it releases its associated jar files.
*/
final ClassLoader classLoader = classLoaderProperty.get();
final ClassLoader classLoader = getClassLoader();
if (classLoader instanceof URLClassLoader) {
final URLClassLoader urlClassLoader = (URLClassLoader) classLoader;
try {
Expand All @@ -415,7 +415,7 @@ private void changeClassLoader(ClassLoader newClassLoader) {
}

// Now moves to the new class loader
classLoaderProperty.set(newClassLoader);
setClassLoader(newClassLoader);
}

/*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* Copyright (c) 2022, Gluon and/or its affiliates.
* All rights reserved. Use is subject to license terms.
*
* This file is available and licensed under the following license:
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the distribution.
* - Neither the name of Oracle Corporation and Gluon nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package com.oracle.javafx.scenebuilder.kit.library;

import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Assertions.*;
import static org.junit.jupiter.api.Assertions.*;

public class BuiltinLibraryTest {

@Test
public void builtin_lib_has_default_items() {
var lib = BuiltinLibrary.getLibrary();

assertNull(lib.getClassLoader());
assertFalse(lib.getItems().isEmpty());

for (var item : lib.getItems()) {
assertThat(item.getLibrary()).isEqualTo(lib);
assertFalse(item.getName().isEmpty());
assertFalse(item.getSection().isEmpty());
assertFalse(item.getFxmlText().isEmpty());
}
}
}

0 comments on commit a05b8fd

Please sign in to comment.