Skip to content

Commit

Permalink
Tried to cache the export wizard
Browse files Browse the repository at this point in the history
  • Loading branch information
oowekyala committed Nov 29, 2017
1 parent ba37a53 commit cb141d7
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 33 deletions.
Expand Up @@ -107,7 +107,6 @@ public class ExportXPathWizardController implements Initializable {
// Fields
private final XPathPanelController owner;
private final DesignerRoot designerRoot;
private final ObservableRuleBuilder ruleBuilder;
private final Set<Subscription> activeSubs = new HashSet<>();
// Properties
private final Var<RulePriority> priority = Var.newSimpleVar(RulePriority.MEDIUM);
Expand Down Expand Up @@ -145,11 +144,9 @@ public class ExportXPathWizardController implements Initializable {


public ExportXPathWizardController(XPathPanelController owner,
DesignerRoot designerRoot,
ObservableRuleBuilder ruleBuilder) {
DesignerRoot designerRoot) {
this.owner = owner;
this.designerRoot = designerRoot;
this.ruleBuilder = ruleBuilder;
}


Expand All @@ -165,19 +162,13 @@ public void initialize(URL location, ResourceBundle resources) {
ObjectBinding<RulePriority> priorityBinding
= Bindings.createObjectBinding(() -> RulePriority.valueOf(prioritySlider.valueProperty().intValue()),
prioritySlider.valueProperty());

priority.bind(priorityBinding);


// Rewire the rulebuilder to be updated by the ui, initialise the values of the ui
rewire(ruleBuilder.nameProperty(), this.nameProperty(), this::setName);
rewire(ruleBuilder.descriptionProperty(), this.descriptionProperty(), this::setDescription);
rewire(ruleBuilder.languageProperty(), this.languageProperty(), this::setLanguage);
rewire(ruleBuilder.messageProperty(), this.messageProperty(), this::setMessage);
rewire(ruleBuilder.priorityProperty(), this.priorityProperty(), this::setPriority);
rewire(ruleBuilder.rulePropertiesProperty(), this.rulePropertiesProperty(), this::setRuleProperties);

bindToRuleBuilder(owner.getRuleBuilder());

templateHelper = new XmlLiveTemplateHelper(exportResultArea);

// Changes to the properties made in this dialog now reflect
// on the underlying rule in the XPath panel (and vice versa)
subscribe(templateHelper.replace(nameField.textProperty(), NAME_REPLACE_PATTERN).asAttribute());
Expand All @@ -203,14 +194,24 @@ public void initialize(URL location, ResourceBundle resources) {
}
});


// Expands required info pane
Platform.runLater(() -> infoAccordion.setExpandedPane((TitledPane) infoAccordion.getChildrenUnmodifiable().get(0)));
Platform.runLater(this::registerValidators);
Platform.runLater(() -> exportResultArea.replaceText(getBaseRuleElement()));
}


public void bindToRuleBuilder(ObservableRuleBuilder ruleBuilder) {
// Rewire the rulebuilder to be updated by the ui, initialise the values of the ui
rewire(ruleBuilder.nameProperty(), this.nameProperty(), this::setName);
rewire(ruleBuilder.descriptionProperty(), this.descriptionProperty(), this::setDescription);
rewire(ruleBuilder.languageProperty(), this.languageProperty(), this::setLanguage);
rewire(ruleBuilder.messageProperty(), this.messageProperty(), this::setMessage);
rewire(ruleBuilder.priorityProperty(), this.priorityProperty(), this::setPriority);
rewire(ruleBuilder.rulePropertiesProperty(), this.rulePropertiesProperty(), this::setRuleProperties);
}


private void initialiseLanguageChoiceBox() {
languageChoiceBox.getItems().addAll(DesignerUtil.getSupportedLanguageVersions().stream()
.map(LanguageVersion::getLanguage)
Expand Down Expand Up @@ -258,6 +259,7 @@ private void popEditPropertyDialog(PropertyDescriptorSpec edited) throws IOExcep
dialog.initModality(Modality.WINDOW_MODAL);

Parent root = loader.load();

Platform.runLater(() -> wizard.bindToDescriptor(edited));

Scene scene = new Scene(root);
Expand Down
Expand Up @@ -5,6 +5,7 @@
package net.sourceforge.pmd.util.fxdesigner.controllers;

import java.io.IOException;
import java.lang.ref.SoftReference;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -78,6 +79,8 @@ public class XPathPanelController implements Initializable, SettingsOwner {

private ChoiceBox<String> xpathVersionChoiceBox;

private SoftReference<Stage> exportWizardStageCache;


public XPathPanelController(DesignerRoot owner, MainDesignerController mainController) {
this.designerRoot = owner;
Expand All @@ -100,14 +103,6 @@ public void initialize(URL location, ResourceBundle resources) {
}


// Binds the underlying rule parameters to the parent UI, disconnecting it from the wizard if need be
public void bindToParent() {
ObjectBinding<Language> parentLang = Bindings.createObjectBinding(() -> parent.getLanguageVersion().getLanguage(), parent.languageVersionProperty());
ruleBuilder.languageProperty().unbind();
ruleBuilder.languageProperty().bind(parentLang);
}


public void initialiseVersionChoiceBox(ChoiceBox<String> choiceBox) {
this.xpathVersionChoiceBox = choiceBox;

Expand Down Expand Up @@ -163,34 +158,49 @@ public void shutdown() {


public void showExportXPathToRuleWizard() throws IOException {
// TODO probably no need to reload the dialog, just keep it around with a SoftReference.
// The wizard is not yet fully reusable though
ExportXPathWizardController wizard = new ExportXPathWizardController(this, designerRoot, ruleBuilder);
if (exportWizardStageCache == null || exportWizardStageCache.get() == null) {
exportWizardStageCache = new SoftReference<>(createWizard());
}

Stage dialog = exportWizardStageCache.get();
ExportXPathWizardController wizard = (ExportXPathWizardController) dialog.getUserData();
this.bindToExportWizard(wizard);
dialog.setOnCloseRequest(e -> {
wizard.shutdown();
this.bindToParent();
});
}


// Creates the wizard if needed. The wizard is reused via a soft reference.
private Stage createWizard() throws IOException {
System.out.println("Wizard instantiation");
ExportXPathWizardController wizard = new ExportXPathWizardController(this, designerRoot);

FXMLLoader loader = new FXMLLoader(DesignerUtil.getFxml("xpath-export-wizard.fxml"));
loader.setController(wizard);

final Stage dialog = new Stage();
dialog.initOwner(designerRoot.getMainStage());
dialog.setOnCloseRequest(e -> {
wizard.shutdown();
this.bindToParent();
});
dialog.initModality(Modality.WINDOW_MODAL);

Parent root = loader.load();
wizard.bindToRuleBuilder(ruleBuilder);

Scene scene = new Scene(root);
dialog.setTitle("Export XPath expression to rule");
dialog.setScene(scene);
dialog.show();
dialog.setUserData(wizard); // Store the controller for future retrieval

return dialog;
}


@Override
public List<AppSetting> getSettings() {
public List<AppSetting> getSettings() {
List<AppSetting> settings = new ArrayList<>();
settings.add(new AppSetting("xpathVersion", () -> xpathEvaluator.xpathVersionProperty().getValue(),
// @formatter:off
// @formatter:off
v -> {
if (!"".equals(v)) {
xpathEvaluator.xpathVersionProperty().setValue(v);
Expand All @@ -210,6 +220,8 @@ public List<AppSetting> getSettings() {
* <p>Note that we only need unidirectional binding (wizard changes propagate to the rest) because the wizard's
* window is modal.
*
* <p>{@link #bindToParent()} resets the original bindings.
*
* @param exportWizard The caller
*/
public void bindToExportWizard(ExportXPathWizardController exportWizard) {
Expand All @@ -225,6 +237,16 @@ public void bindToExportWizard(ExportXPathWizardController exportWizard) {
}


/**
* Binds the underlying rule parameters to the parent UI, disconnecting it from the wizard if need be.
*/
public void bindToParent() {
ObjectBinding<Language> parentLang = Bindings.createObjectBinding(() -> parent.getLanguageVersion().getLanguage(), parent.languageVersionProperty());
ruleBuilder.languageProperty().unbind();
ruleBuilder.languageProperty().bind(parentLang);
}


public String getXpathVersion() {
return xpathVersionProperty().getValue();
}
Expand All @@ -245,4 +267,7 @@ public ObservableValue<String> xpathExpressionProperty() {
}


public ObservableRuleBuilder getRuleBuilder() {
return ruleBuilder;
}
}

0 comments on commit cb141d7

Please sign in to comment.