Skip to content

Commit

Permalink
Refactored to align diff Controller superclass with "straight"
Browse files Browse the repository at this point in the history
The source / target / extractor scheme now is a bit cleaner and more
flexible. And the code is easier to understand if the same pattern is
used for solving the same thing.
  • Loading branch information
PhRX committed Jan 27, 2017
1 parent d264ef7 commit 8268e0e
Show file tree
Hide file tree
Showing 8 changed files with 163 additions and 107 deletions.
Expand Up @@ -2,17 +2,18 @@


import static com.insightfullogic.honest_profiler.ports.javafx.util.FxUtil.addProfileNr; import static com.insightfullogic.honest_profiler.ports.javafx.util.FxUtil.addProfileNr;
import static com.insightfullogic.honest_profiler.ports.javafx.util.FxUtil.createColoredLabelContainer; import static com.insightfullogic.honest_profiler.ports.javafx.util.FxUtil.createColoredLabelContainer;
import static javafx.beans.binding.Bindings.createObjectBinding;
import static javafx.geometry.Pos.CENTER; import static javafx.geometry.Pos.CENTER;


import java.util.function.Function; import java.util.function.Function;


import com.insightfullogic.honest_profiler.core.aggregation.result.ItemType; import com.insightfullogic.honest_profiler.core.aggregation.result.ItemType;
import com.insightfullogic.honest_profiler.ports.javafx.model.ProfileContext; import com.insightfullogic.honest_profiler.ports.javafx.model.ProfileContext;


import javafx.beans.binding.ObjectBinding;
import javafx.beans.property.ObjectProperty; import javafx.beans.property.ObjectProperty;
import javafx.beans.property.Property; import javafx.beans.property.Property;
import javafx.beans.property.SimpleObjectProperty; import javafx.beans.property.SimpleObjectProperty;
import javafx.beans.value.ObservableValue;
import javafx.scene.Node; import javafx.scene.Node;
import javafx.scene.control.Button; import javafx.scene.control.Button;
import javafx.scene.control.TableColumn; import javafx.scene.control.TableColumn;
Expand Down Expand Up @@ -41,12 +42,17 @@
*/ */
public abstract class AbstractProfileDiffViewController<T, U> extends AbstractViewController<U> public abstract class AbstractProfileDiffViewController<T, U> extends AbstractViewController<U>
{ {
// Instance Properties

private ProfileContext baseContext; private ProfileContext baseContext;
private ProfileContext newContext; private ProfileContext newContext;


private ObjectProperty<T> baseTarget; private ObjectProperty<T> baseTarget;
private ObjectProperty<T> newTarget; private ObjectProperty<T> newTarget;
private Function<ProfileContext, ObservableValue<T>> targetExtractor; private ObjectBinding<T> baseSourceBinding;
private ObjectBinding<T> newSourceBinding;

// FXML Implementation


/** /**
* This method must be called by subclasses in their FXML initialize(). It provides the extraction function which * This method must be called by subclasses in their FXML initialize(). It provides the extraction function which
Expand All @@ -58,18 +64,117 @@ public abstract class AbstractProfileDiffViewController<T, U> extends AbstractVi
* @param quickFilterButton the button used to apply the quick filter * @param quickFilterButton the button used to apply the quick filter
* @param quickFilterText the TextField providing the value for the quick filter * @param quickFilterText the TextField providing the value for the quick filter
*/ */
protected void initialize(Function<ProfileContext, ObservableValue<T>> targetExtractor, @Override
Button filterButton, Button quickFilterButton, TextField quickFilterText, ItemType type) protected void initialize(Button filterButton, Button quickFilterButton,
TextField quickFilterText, ItemType type)
{ {
super.initialize(filterButton, quickFilterButton, quickFilterText, type); super.initialize(filterButton, quickFilterButton, quickFilterText, type);


this.targetExtractor = targetExtractor;
baseTarget = new SimpleObjectProperty<>(); baseTarget = new SimpleObjectProperty<>();
newTarget = new SimpleObjectProperty<>(); newTarget = new SimpleObjectProperty<>();
baseTarget.addListener((property, oldValue, newValue) -> refresh()); baseTarget.addListener((property, oldValue, newValue) -> refresh());
newTarget.addListener((property, oldValue, newValue) -> refresh()); newTarget.addListener((property, oldValue, newValue) -> refresh());
} }


// Instance Accessors

/**
* Returns the {@link ProfileContext} for the baseline target. The name has been shortened to unclutter code in
* subclasses.
*
* @return the {@link ProfileContext} encapsulating the baseline target.
*/
protected ProfileContext baseCtx()
{
return baseContext;
}

/**
* Returns the {@link ProfileContext} for the target which will be compared against the baseline. The name has been
* shortened to unclutter code in subclasses.
*
* @return the {@link ProfileContext} encapsulating the target being compared against the baseline.
*/
protected ProfileContext newCtx()
{
return newContext;
}

/**
* Returns the current baseline target instance.
*
* @return the current baseline target instance
*/
protected T getBaseTarget()
{
return baseTarget.get();
}

/**
* Returns the current "new" target instance.
*
* @return the current "new" target instance
*/
protected T getNewTarget()
{
return newTarget.get();
}

/**
* Sets the {@link ProfileContext}s encapsulating the baseline target and the target being compared against it.
*
* @param profileContext the {@link ProfileContext}s encapsulating the baseline target and the target being compared
* against it
*/
public void setProfileContexts(ProfileContext baseContext, ProfileContext newContext)
{
this.baseContext = baseContext;
this.newContext = newContext;
}

// Source-Target Binding

/**
* Set the source object the target data structure T will be extracted from, and the function which extracts the
* target data structure T from the source.
*
* @param source the source providing the target data structure
* @param targetExtractor a function which extracts the target from the source object
*/
public void bind(ObjectProperty<? extends Object> baseSource,
ObjectProperty<? extends Object> newSource, Function<Object, T> targetExtractor)
{
baseSourceBinding = createObjectBinding(
() -> targetExtractor.apply(baseSource.get()),
baseSource);
newSourceBinding = createObjectBinding(
() -> targetExtractor.apply(newSource.get()),
newSource);
}

// Activation

/**
* Binds the local target {@link Property}s to the target {@link Property}s in the {@link ProfileContext}s, using
* the target extractor function. The net effect is that the controller will start tracking changes to the target
* instances in the {@link ProfileContext}.
*/
public void activate()
{
baseTarget.bind(baseSourceBinding);
newTarget.bind(newSourceBinding);
}

/**
* Unbinds the local target {@link Property}s. The controller no longer tracks changes to the target
* {@link Property}s in the {@link ProfileContext}s.
*/
public void deactivate()
{
baseTarget.unbind();
newTarget.unbind();
}

// UI Helper Methods // UI Helper Methods


@Override @Override
Expand All @@ -90,11 +195,11 @@ protected <C> void setColumnHeader(C column, String title, ProfileContext profil


if (column instanceof TreeTableColumn<?, ?>) if (column instanceof TreeTableColumn<?, ?>)
{ {
reconfigure((TreeTableColumn<?, ?>) column, null, header, width, width + 5); reconfigure((TreeTableColumn<?, ?>)column, null, header, width, width + 5);
} }
else else
{ {
reconfigure((TableColumn<?, ?>) column, null, header, width, width + 5); reconfigure((TableColumn<?, ?>)column, null, header, width, width + 5);
} }
} }


Expand Down Expand Up @@ -127,83 +232,4 @@ private double calculateWidth(HBox box)
width += box.getPadding().getLeft() + box.getPadding().getRight(); width += box.getPadding().getLeft() + box.getPadding().getRight();
return width; return width;
} }

// Activation

/**
* Binds the local target {@link Property}s to the target {@link Property}s in the {@link ProfileContext}s, using
* the target extractor function. The net effect is that the controller will start tracking changes to the target
* instances in the {@link ProfileContext}.
*/
public void activate()
{
baseTarget.bind(targetExtractor.apply(baseContext));
newTarget.bind(targetExtractor.apply(newContext));
}

/**
* Unbinds the local target {@link Property}s. The controller no longer tracks changes to the target
* {@link Property}s in the {@link ProfileContext}s.
*/
public void deactivate()
{
baseTarget.unbind();
newTarget.unbind();
}

// Accessors

/**
* Returns the {@link ProfileContext} for the baseline target. The name has been shortened to unclutter code in
* subclasses.
*
* @return the {@link ProfileContext} encapsulating the baseline target.
*/
protected ProfileContext baseCtx()
{
return baseContext;
}

/**
* Returns the {@link ProfileContext} for the target which will be compared against the baseline. The name has been
* shortened to unclutter code in subclasses.
*
* @return the {@link ProfileContext} encapsulating the target being compared against the baseline.
*/
protected ProfileContext newCtx()
{
return newContext;
}

/**
* Returns the current baseline target instance.
*
* @return the current baseline target instance
*/
protected T getBaseTarget()
{
return baseTarget.get();
}

/**
* Returns the current "new" target instance.
*
* @return the current "new" target instance
*/
protected T getNewTarget()
{
return newTarget.get();
}

/**
* Sets the {@link ProfileContext}s encapsulating the baseline target and the target being compared against it.
*
* @param profileContext the {@link ProfileContext}s encapsulating the baseline target and the target being compared
* against it
*/
public void setProfileContexts(ProfileContext baseContext, ProfileContext newContext)
{
this.baseContext = baseContext;
this.newContext = newContext;
}
} }
Expand Up @@ -42,7 +42,7 @@ public abstract class AbstractProfileViewController<T, U> extends AbstractViewCo


private ObjectBinding<T> sourceBinding; private ObjectBinding<T> sourceBinding;


// Class Methods // FXML Implementation


/** /**
* This method must be called by subclasses in their FXML initialize(). It passes on the controller-local UI nodes * This method must be called by subclasses in their FXML initialize(). It passes on the controller-local UI nodes
Expand Down
Expand Up @@ -43,9 +43,9 @@
import static com.insightfullogic.honest_profiler.ports.javafx.util.ResourceUtil.INFO_TABLE_FLATDIFF; import static com.insightfullogic.honest_profiler.ports.javafx.util.ResourceUtil.INFO_TABLE_FLATDIFF;
import static com.insightfullogic.honest_profiler.ports.javafx.util.report.ReportUtil.writeFlatProfileDiffCsv; import static com.insightfullogic.honest_profiler.ports.javafx.util.report.ReportUtil.writeFlatProfileDiffCsv;


import com.insightfullogic.honest_profiler.core.aggregation.AggregationProfile;
import com.insightfullogic.honest_profiler.core.aggregation.result.diff.DiffEntry; import com.insightfullogic.honest_profiler.core.aggregation.result.diff.DiffEntry;
import com.insightfullogic.honest_profiler.core.aggregation.result.diff.FlatDiff; import com.insightfullogic.honest_profiler.core.aggregation.result.diff.FlatDiff;
import com.insightfullogic.honest_profiler.core.aggregation.result.straight.Flat;
import com.insightfullogic.honest_profiler.ports.javafx.model.ProfileContext; import com.insightfullogic.honest_profiler.ports.javafx.model.ProfileContext;
import com.insightfullogic.honest_profiler.ports.javafx.util.report.ReportUtil; import com.insightfullogic.honest_profiler.ports.javafx.util.report.ReportUtil;
import com.insightfullogic.honest_profiler.ports.javafx.view.cell.MethodNameTableCell; import com.insightfullogic.honest_profiler.ports.javafx.view.cell.MethodNameTableCell;
Expand All @@ -58,7 +58,7 @@
import javafx.scene.control.TextField; import javafx.scene.control.TextField;


public class FlatDiffViewController public class FlatDiffViewController
extends AbstractProfileDiffViewController<AggregationProfile, DiffEntry<String>> extends AbstractProfileDiffViewController<Flat<String>, DiffEntry<String>>
{ {
@FXML @FXML
private Button filterButton; private Button filterButton;
Expand Down Expand Up @@ -130,7 +130,6 @@ protected void initialize()
diff = new FlatDiff<>(); diff = new FlatDiff<>();


super.initialize( super.initialize(
profileContext -> profileContext.profileProperty(),
filterButton, filterButton,
quickFilterButton, quickFilterButton,
quickFilterText, quickFilterText,
Expand Down Expand Up @@ -184,11 +183,11 @@ private void initializeTable()
cfgTimeDiffCol(totalTimeDiff, "totalTimeDiff", getText(COLUMN_TOTAL_TIME_DIFF)); cfgTimeDiffCol(totalTimeDiff, "totalTimeDiff", getText(COLUMN_TOTAL_TIME_DIFF));
} }


private void updateDiff(AggregationProfile profile, boolean base) private void updateDiff(Flat<String> flat, boolean base)
{ {
if (profile != null) if (flat != null)
{ {
diff.set(profile.getFlat(), base); diff.set(flat, base);
diffTable.getItems().clear(); diffTable.getItems().clear();
diffTable.getItems().addAll(diff.filter(getFilterSpecification()).getData()); diffTable.getItems().addAll(diff.filter(getFilterSpecification()).getData());
} }
Expand Down
Expand Up @@ -87,6 +87,8 @@ public class FlatViewController extends AbstractProfileViewController<Flat<Strin


private ObservableList<Entry<String>> flatProfile; private ObservableList<Entry<String>> flatProfile;


// FXML Implementation

@Override @Override
@FXML @FXML
protected void initialize() protected void initialize()
Expand All @@ -102,6 +104,8 @@ protected void initialize()
initializeTable(); initializeTable();
} }


// Accessors

@Override @Override
public void setProfileContext(ProfileContext profileContext) public void setProfileContext(ProfileContext profileContext)
{ {
Expand Down
Expand Up @@ -20,6 +20,8 @@


import static com.insightfullogic.honest_profiler.ports.javafx.ViewType.FLAT; import static com.insightfullogic.honest_profiler.ports.javafx.ViewType.FLAT;
import static com.insightfullogic.honest_profiler.ports.javafx.ViewType.TREE; import static com.insightfullogic.honest_profiler.ports.javafx.ViewType.TREE;
import static com.insightfullogic.honest_profiler.ports.javafx.util.BindUtil.FLAT_EXTRACTOR;
import static com.insightfullogic.honest_profiler.ports.javafx.util.BindUtil.TREE_EXTRACTOR;
import static com.insightfullogic.honest_profiler.ports.javafx.util.ConversionUtil.getStringConverterForType; import static com.insightfullogic.honest_profiler.ports.javafx.util.ConversionUtil.getStringConverterForType;
import static com.insightfullogic.honest_profiler.ports.javafx.util.ResourceUtil.INFO_CHOICE_VIEWTYPE; import static com.insightfullogic.honest_profiler.ports.javafx.util.ResourceUtil.INFO_CHOICE_VIEWTYPE;
import static com.insightfullogic.honest_profiler.ports.javafx.util.ResourceUtil.INFO_LABEL_BASESOURCE; import static com.insightfullogic.honest_profiler.ports.javafx.util.ResourceUtil.INFO_LABEL_BASESOURCE;
Expand Down Expand Up @@ -73,7 +75,12 @@ public void setProfileContexts(ProfileContext baseContext, ProfileContext newCon
newSourceLabel.setText(newContext.getName()); newSourceLabel.setText(newContext.getName());


flatController.setProfileContexts(baseContext, newContext); flatController.setProfileContexts(baseContext, newContext);
flatController
.bind(baseContext.profileProperty(), newContext.profileProperty(), FLAT_EXTRACTOR);

treeController.setProfileContexts(baseContext, newContext); treeController.setProfileContexts(baseContext, newContext);
treeController
.bind(baseContext.profileProperty(), newContext.profileProperty(), TREE_EXTRACTOR);


viewChoice.setConverter(getStringConverterForType(ViewType.class)); viewChoice.setConverter(getStringConverterForType(ViewType.class));
viewChoice.getItems().addAll(FLAT, TREE); viewChoice.getItems().addAll(FLAT, TREE);
Expand Down
Expand Up @@ -20,6 +20,9 @@


import static com.insightfullogic.honest_profiler.ports.javafx.ViewType.FLAT; import static com.insightfullogic.honest_profiler.ports.javafx.ViewType.FLAT;
import static com.insightfullogic.honest_profiler.ports.javafx.model.ProfileContext.ProfileMode.LIVE; import static com.insightfullogic.honest_profiler.ports.javafx.model.ProfileContext.ProfileMode.LIVE;
import static com.insightfullogic.honest_profiler.ports.javafx.util.BindUtil.FLAME_EXTRACTOR;
import static com.insightfullogic.honest_profiler.ports.javafx.util.BindUtil.FLAT_EXTRACTOR;
import static com.insightfullogic.honest_profiler.ports.javafx.util.BindUtil.TREE_EXTRACTOR;
import static com.insightfullogic.honest_profiler.ports.javafx.util.ConversionUtil.getStringConverterForType; import static com.insightfullogic.honest_profiler.ports.javafx.util.ConversionUtil.getStringConverterForType;
import static com.insightfullogic.honest_profiler.ports.javafx.util.ResourceUtil.CONTENT_LABEL_PROFILESAMPLECOUNT; import static com.insightfullogic.honest_profiler.ports.javafx.util.ResourceUtil.CONTENT_LABEL_PROFILESAMPLECOUNT;
import static com.insightfullogic.honest_profiler.ports.javafx.util.ResourceUtil.INFO_BUTTON_COMPARE; import static com.insightfullogic.honest_profiler.ports.javafx.util.ResourceUtil.INFO_BUTTON_COMPARE;
Expand All @@ -35,8 +38,6 @@


import java.util.List; import java.util.List;


import com.insightfullogic.honest_profiler.core.aggregation.AggregationProfile;
import com.insightfullogic.honest_profiler.core.profiles.FlameGraph;
import com.insightfullogic.honest_profiler.ports.javafx.ViewType; import com.insightfullogic.honest_profiler.ports.javafx.ViewType;
import com.insightfullogic.honest_profiler.ports.javafx.model.ApplicationContext; import com.insightfullogic.honest_profiler.ports.javafx.model.ApplicationContext;
import com.insightfullogic.honest_profiler.ports.javafx.model.ProfileContext; import com.insightfullogic.honest_profiler.ports.javafx.model.ProfileContext;
Expand Down Expand Up @@ -99,17 +100,13 @@ public void setProfileContext(ProfileContext prCtx)
this.profileContext = prCtx; this.profileContext = prCtx;


flatController.setProfileContext(prCtx); flatController.setProfileContext(prCtx);
flatController.bind( flatController.bind(prCtx.profileProperty(), FLAT_EXTRACTOR);
prCtx.profileProperty(),
o -> o == null ? null : ((AggregationProfile)o).getFlat());


treeController.setProfileContext(prCtx); treeController.setProfileContext(prCtx);
treeController.bind( treeController.bind(prCtx.profileProperty(), TREE_EXTRACTOR);
prCtx.profileProperty(),
o -> o == null ? null : ((AggregationProfile)o).getTree());


flameController.setProfileContext(prCtx); flameController.setProfileContext(prCtx);
flameController.bind(prCtx.flameGraphProperty(), o -> (FlameGraph)o); flameController.bind(prCtx.flameGraphProperty(), FLAME_EXTRACTOR);


prCtx.profileProperty().addListener( prCtx.profileProperty().addListener(
(property, oldValue, newValue) -> profileSampleCount.setText( (property, oldValue, newValue) -> profileSampleCount.setText(
Expand Down

0 comments on commit 8268e0e

Please sign in to comment.