Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: ADD Refresh library menu item #391

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1361,12 +1361,12 @@ public void onManageJarFxml(ActionEvent event) {
libraryDialogController.openWindow();
}

public void onImportJarFxml(Window owner) {
libraryPanelController.performImportJarFxml(owner);
public boolean onImportJarFxml(Window owner) {
return libraryPanelController.performImportJarFxml(owner);
}

public void onImportFromFolder(Window owner) {
libraryPanelController.performImportFromFolder(owner);
public boolean onImportFromFolder(Window owner) {
return libraryPanelController.performImportFromFolder(owner);
}

@FXML
Expand Down Expand Up @@ -1411,6 +1411,15 @@ void onLibraryImportSelection(ActionEvent event) {
}
}

@FXML
void onLibraryRefresh(ActionEvent event) {

UserLibrary userLibrary = SceneBuilderApp.getSingleton().getUserLibrary();

userLibrary.stopExplorer();
userLibrary.startExplorer();
}

@FXML
void onLibraryRevealCustomFolder(ActionEvent event) {
String userLibraryPath = ((UserLibrary) getEditorController().getLibrary()).getPath();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,7 @@ public void handleLaunch(List<String> files) {

userLibrary.explorationCountProperty().addListener((ChangeListener<Number>) (ov, t, t1) -> userLibraryExplorationCountDidChange());

userLibrary.startWatching();
userLibrary.startExplorer();

sendTrackingStartupInfo();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@
<RadioMenuItem fx:id="libraryViewAsSections" mnemonicParsing="false" onAction="#onLibraryViewAsSections" text="%library.panel.menu.view.sections" toggleGroup="$libraryDisplayOptionTG" />
<SeparatorMenuItem mnemonicParsing="false" />
<MenuItem mnemonicParsing="false" onAction="#onManageJarFxml" text="%library.panel.menu.manage.jar.fxml" />
<MenuItem mnemonicParsing="false" onAction="#onLibraryRefresh" text="%library.panel.menu.refresh" />
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please update the license header in line 4 from Copyright (c) 2016, 2019, Gluon and/or its affiliates. to Copyright (c) 2016, 2022, Gluon and/or its affiliates..

<MenuItem fx:id="libraryImportSelection" mnemonicParsing="false" onAction="#onLibraryImportSelection" text="%library.panel.menu.import.selection" />
<SeparatorMenuItem mnemonicParsing="false" />
<Menu fx:id="customLibraryMenu" mnemonicParsing="false" text="%library.panel.menu.custom">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,7 @@ library = Library
library.exploring = Exploring Library..
library.panel.menu.manage.jar.fxml = JAR/FXML Manager
library.panel.menu.import.selection = Import Selection
library.panel.menu.refresh = Refresh Library
# Messages below are temporarily unused
#library.panel.menu.category.view = View Library Category
#library.panel.menu.category.create = Create Library Category
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,21 +168,22 @@ public void setSearchPattern(String searchPattern) {

/**
* @param owner
* @return
* @treatAsPrivate Perform the import jar action.
*/
public void performImportJarFxml(Window owner) {
public boolean performImportJarFxml(Window owner) {
// Open file chooser and get user selection
final List<File> importedFiles = performSelectJarOrFxmlFile(owner);
processImportJarFxml(importedFiles);
return processImportJarFxml(importedFiles);
}

/**
* @param owner
* @treatAsPrivate Perform the import jar action.
*/
public void performImportFromFolder(Window owner) {
public boolean performImportFromFolder(Window owner) {
File folder = performSelectFolder(owner);
processImportFolder(folder);
return processImportFolder(folder);
}

/**
Expand Down Expand Up @@ -679,8 +680,6 @@ private void processInternalImport(List<FXOMObject> objects) {
if (hasDependencies) {
userLibraryUpdateRejected();
} else {
((UserLibrary) getEditorController().getLibrary()).stopWatching();

try {
// The selection can be multiple, in which case each asset is
// processed separately.
Expand All @@ -705,8 +704,6 @@ private void processInternalImport(List<FXOMObject> objects) {
if (currentDisplayMode.equals(DISPLAY_MODE.SECTIONS)) {
sectionNameToKeepOpened = UserLibrary.TAG_USER_DEFINED;
}

((UserLibrary) getEditorController().getLibrary()).startWatching();
}
}
}
Expand Down Expand Up @@ -744,7 +741,7 @@ private File getUniqueFxmlFileName(String prefix, String libDir) {
return file;
}

private void processImportJarFxml(List<File> importedFiles) {
private boolean processImportJarFxml(List<File> importedFiles) {
if (importedFiles != null && !importedFiles.isEmpty()) {
sectionNameToKeepOpened = getExpandedSectionName();
Path libPath = Paths.get(((UserLibrary)getEditorController().getLibrary()).getPath());
Expand Down Expand Up @@ -782,12 +779,16 @@ private void processImportJarFxml(List<File> importedFiles) {
if (userChoice.equals(ButtonID.OK) && currentDisplayMode.equals(DISPLAY_MODE.SECTIONS)) {
sectionNameToKeepOpened = UserLibrary.TAG_USER_DEFINED;
}

return true;
}
}
}

return false;
}

private void processImportFolder(File folder) {
private boolean processImportFolder(File folder) {
if (folder != null && folder.exists() && folder.isDirectory()) {
Path libPath = Paths.get(((UserLibrary)getEditorController().getLibrary()).getPath());
if (createUserLibraryDir(libPath)) {
Expand All @@ -809,8 +810,12 @@ private void processImportFolder(File folder) {
if (userChoice.equals(ButtonID.OK) && currentDisplayMode.equals(DISPLAY_MODE.SECTIONS)) {
sectionNameToKeepOpened = UserLibrary.TAG_USER_DEFINED;
}

return true;
}
}

return false;
}

private List<File> getSubsetOfFiles(String pattern, List<File> files) {
Expand Down Expand Up @@ -874,10 +879,6 @@ void copyFilesToUserLibraryDir(List<File> files) {
Path tempTargetPath = null;
setUserLibraryPathString();

// Here we deactivate the UserLib so that it unlocks the files contained
// in the lib dir in the file system meaning (especially on Windows).
((UserLibrary) getEditorController().getLibrary()).stopWatching();

try {
for (File file : files) {
savedFileName = file.getName();
Expand All @@ -901,8 +902,6 @@ void copyFilesToUserLibraryDir(List<File> files) {
}
}

((UserLibrary) getEditorController().getLibrary()).startWatching();

if (errorCount > 0) {
final ErrorDialog errorDialog = new ErrorDialog(null);
errorDialog.setTitle(I18N.getString("error.copy.title"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,11 @@
import com.oracle.javafx.scenebuilder.kit.preferences.MavenPreferences;
import com.oracle.javafx.scenebuilder.kit.preferences.PreferencesControllerBase;
import com.oracle.javafx.scenebuilder.kit.preferences.PreferencesRecordArtifact;
import com.oracle.javafx.scenebuilder.kit.util.ReturningRunnable;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please change line 2 from * Copyright (c) 2016, 2021, Gluon and/or its affiliates. into * Copyright (c) 2016, 2022, Gluon and/or its affiliates..


import javafx.beans.InvalidationListener;
import javafx.beans.Observable;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.collections.transformation.SortedList;
Expand Down Expand Up @@ -91,12 +93,13 @@ public class LibraryDialogController extends AbstractFxmlWindowController {

private ObservableList<DialogListItem> listItems;

private Runnable onAddJar;
private Runnable onAddFolder;
private ReturningRunnable<Boolean> onAddJar;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please consider replacing the ReturningRunnable with JDKs default java.util.function.Supplier interface.

private ReturningRunnable<Boolean> onAddFolder;
private Consumer<Path> onEditFXML;

private String userM2Repository;
private String tempM2Repository;
private SimpleBooleanProperty changedProperty;

private final PreferencesControllerBase preferencesControllerBase;

Expand All @@ -109,13 +112,16 @@ public LibraryDialogController(EditorController editorController, String userM2R
this.userM2Repository = userM2Repository;
this.tempM2Repository = tempM2Repository;
this.preferencesControllerBase = preferencesController;
this.changedProperty = new SimpleBooleanProperty(false);
}

@Override
protected void controllerDidLoadFxml() {
super.controllerDidLoadFxml();

this.classesLink.setTooltip(new Tooltip(I18N.getString("library.dialog.hyperlink.tooltip")));

userLibrary.stopExplorer(); // we stop an eventually running explorer before letting the user do something on this dialog.
}

@Override
Expand All @@ -139,11 +145,10 @@ public void onCloseRequest(WindowEvent event) {
public void openWindow() {
super.openWindow();
super.getStage().setTitle(I18N.getString("library.dialog.title"));
loadLibraryList();

loadLibraryList(false);
}

void loadLibraryList() {
void loadLibraryList(boolean changed) {
if (listItems == null) {
listItems = FXCollections.observableArrayList();
}
Expand Down Expand Up @@ -181,10 +186,15 @@ void loadLibraryList() {

libraryListView.getSelectionModel().selectFirst();
libraryListView.requestFocus();

changedProperty.set(changed);
}

@FXML
private void close() {
if (changedProperty.get())
userLibrary.startExplorer();

listItems.clear();
closeWindow();
}
Expand All @@ -200,17 +210,19 @@ private void manage() {
private void addJar() {
// documentWindowController.onImportJarFxml(getStage());
if (onAddJar != null) {
onAddJar.run();
Boolean added = onAddJar.run();
if (Boolean.TRUE.equals(added))
loadLibraryList(true);
}
loadLibraryList();
}

@FXML
private void addFolder() {
if (onAddFolder != null) {
onAddFolder.run();
Boolean added = onAddFolder.run();
if (Boolean.TRUE.equals(added))
loadLibraryList(true);
}
loadLibraryList();
}

@FXML
Expand All @@ -222,7 +234,8 @@ private void addRelease() {
@Override
public void invalidated(Observable observable) {
if (!mavenDialogController.getStage().isShowing()) {
loadLibraryList();
if (mavenDialogController.isConfirmed())
loadLibraryList(true);
mavenDialogController.getStage().showingProperty().removeListener(this);
}
}
Expand All @@ -238,34 +251,19 @@ private void addManually() {
@Override
public void invalidated(Observable observable) {
if (!mavenDialogController.getStage().isShowing()) {
loadLibraryList();
if (mavenDialogController.isConfirmed())
loadLibraryList(true);
mavenDialogController.getStage().showingProperty().removeListener(this);
}
}
});
}

/*
If the file is an fxml, we don't need to stop the library watcher.
Else we have to stop it first:
1) We stop the library watcher, so that all related class loaders will be closed and the jar can be deleted.
2) Then, if the file exists, the jar or fxml file will be deleted from the library.
3) After the jar or fxml is removed, the library watcher is started again.
* We can simply delete the item since the library explorer is shut down when opening the dialog.
*/
public void processJarFXMLFolderDelete(DialogListItem dialogListItem) {
if (dialogListItem instanceof LibraryDialogListItem &&
LibraryUtil.isFxmlPath(((LibraryDialogListItem) dialogListItem).getFilePath())) {
deleteFile(dialogListItem);
} else {
//1)
userLibrary.stopWatching();

//2)
deleteFile(dialogListItem);

//3)
userLibrary.startWatching();
}
deleteFile(dialogListItem);
}

private void deleteFile(DialogListItem dialogListItem) {
Expand Down Expand Up @@ -305,7 +303,7 @@ private void deleteFile(DialogListItem dialogListItem) {
} catch (IOException x) {
Logger.getLogger(LibraryDialogController.class.getName()).log(Level.SEVERE, "Error while deleting the file.", x);
}
loadLibraryList();
loadLibraryList(true);
}

public void processJarFXMLFolderEdit(DialogListItem dialogListItem) {
Expand All @@ -322,6 +320,7 @@ public void processJarFXMLFolderEdit(DialogListItem dialogListItem) {
AbstractModalDialog.ButtonID userChoice = iwc.showAndWait();
if (userChoice == AbstractModalDialog.ButtonID.OK) {
logInfoMessage("log.user.maven.updated", item);
changedProperty.set(true);
}
} else {
// if (SceneBuilderApp.getSingleton().lookupUnusedDocumentWindowController() != null) {
Expand All @@ -331,6 +330,7 @@ public void processJarFXMLFolderEdit(DialogListItem dialogListItem) {
// item.getFilePath().toFile());
if (onEditFXML != null) {
onEditFXML.accept(item.getFilePath());
changedProperty.set(true);
}
}
}
Expand All @@ -352,6 +352,7 @@ public void processJarFXMLFolderEdit(DialogListItem dialogListItem) {
mavenArtifact.setFilter(iwc.getNewExcludedItems());
updatePreferences(mavenArtifact);
logInfoMessage("log.user.maven.updated", mavenArtifact.getCoordinates());
changedProperty.set(true);
}
}
}
Expand All @@ -365,26 +366,21 @@ private void updatePreferences(MavenArtifact mavenArtifact) {
return;
}

userLibrary.stopWatching();

// Update record artifact
final PreferencesRecordArtifact recordArtifact = preferencesControllerBase.
getRecordArtifact(mavenArtifact);
recordArtifact.writeToJavaPreferences();

userLibrary.startWatching();

}

public void setOnAddJar(Runnable onAddJar) {
public void setOnAddJar(ReturningRunnable<Boolean> onAddJar) {
this.onAddJar = onAddJar;
}

public void setOnEditFXML(Consumer<Path> onEditFXML) {
this.onEditFXML = onEditFXML;
}

public void setOnAddFolder(Runnable onAddFolder) {
public void setOnAddFolder(ReturningRunnable<Boolean> onAddFolder) {
this.onAddFolder = onAddFolder;
}
}
Loading