/** * Returns an <tt>ObservableFeatureToggle</tt> for the given feature name. * * @param featureName Name of the feature * @return toggle for the given feature name */ public ObservableFeatureToggle getFeatureToggle(String featureName) { return getFeatureToggle(getFeatureByName(featureName)); }
/** * Deactivates the feature with the given name * * @param featureName unique name of the feature that should be deactivated */ public void deactivate(String featureName) { deactivate(getFeatureByName(featureName)); } }
private void fire(Feature f, boolean active) { for (FeatureStateListener listener : listeners) { listener.stateChanged(f, active); } }
public FeatureProperty(T feature) { this.feature = feature; feature.addListener((f, a) -> set(a)); set(feature.isActive()); }
public FeatureProperty<?> createFeatureProperty(String featureName) { return new FeatureProperty<>(getManager().getFeatureToggle(featureName)); }
/** * Defines a <tt>ObservableToggleManager</tt> that is configured by the given <tt>ObservableTogglzConfig</tt> * * @param config The configuration */ public ObservableToggleManager(ObservableTogglzConfig config) { manager = new FeatureManagerBuilder().togglzConfig(config).build(); weakFeatureStateListeners = new CopyOnWriteArrayList<>(); config.getStateRepository().addListener((feature, active) -> { for (WeakReference<FeatureStateListener> ref : weakFeatureStateListeners) { FeatureStateListener listener = ref.get(); if (listener == null) { weakFeatureStateListeners.remove(ref); } else { listener.stateChanged(feature, active); } } }); }
private ObservableToggleManager createManagerFromConfig() { try { List<Element> elements = DataFXConfiguration.getInstance().getElements("features"); if (elements == null) { throw new RuntimeException("Can't load config. Features section not specified!"); } if (elements.isEmpty()) { throw new RuntimeException("Can't load config. Features section not specified!"); } if (elements.size() > 1) { throw new RuntimeException("Can't load config. Features section must only be specified once!"); } Class<? extends Feature> featureClass = (Class<? extends Feature>) Class.forName(elements.get(0).getElementsByTagName("featureEnum").item(0).getTextContent()); return new ObservableToggleManager(featureClass); } catch (Exception e) { throw new RuntimeException("Can't generate Feature Manager", e); } }
/** * Checks if the feature with the given unique name is active * * @param featureName unique name of the feature * @return true if the feature with the given unique name is active */ public boolean isActive(String featureName) { return isActive(getFeatureByName(featureName)); }
/** * Activates the feature with the given name * * @param featureName unique name of the feature that should be activated */ public void activate(String featureName) { activate(getFeatureByName(featureName)); }
/** * Returns an <tt>ObservableFeatureToggle</tt> for the given Togglz feature. * * @param feature a Togglz feature * @return toggle for the given feature */ public ObservableFeatureToggle getFeatureToggle(Feature feature) { DefaultFeatureToggle toggle = new DefaultFeatureToggle(feature, (t) -> isActive(t)); weakFeatureStateListeners.add(new WeakReference<FeatureStateListener>(toggle)); return toggle; }
/** * Defines a <tt>ObservableToggleManager</tt> that uses a enum for all features. Internally a <tt>InMemoryObservableStateRepository</tt> and <tt>NoOpUserProvider</tt> is used to configure Togglz- * * @param featureClass The enum that defines all features */ public ObservableToggleManager(Class<? extends Feature> featureClass) { this(new SimpleTogglzConfig<>(featureClass)); }
@Override public void stateChanged(Feature feature, boolean active) { if(this.feature.equals(feature)) { fireStateChanged(active); } }
/** * Default Constructor * * @param featureClass enum class that defines all features */ public SimpleTogglzConfig(Class<T> featureClass) { super(featureClass, new InMemoryObservableStateRepository(), new NoOpUserProvider()); }
/** * Returns the registered feature with the given name * * @param featureName unique name of the feature * @return feature with the given name */ public Feature getFeatureByName(String featureName) { for (Feature feature : getManager().getFeatures()) { if (feature.name().equals(featureName)) { return feature; } } return null; }
@Override public void setFeatureState(FeatureState featureState) { super.setFeatureState(featureState); fire(featureState.getFeature(), featureState.isEnabled()); } }
public FeatureProperty(T feature) { this.feature = feature; feature.addListener((f, a) -> set(a)); set(feature.isActive()); }
public FeatureProperty<?> createFeatureProperty(String featureName) { return new FeatureProperty<>(getManager().getFeatureToggle(featureName)); }
private ObservableToggleManager createManagerFromConfig() { try { List<Element> elements = DataFXConfiguration.getInstance().getElements("features"); if (elements == null) { throw new RuntimeException("Can't load config. Features section not specified!"); } if (elements.isEmpty()) { throw new RuntimeException("Can't load config. Features section not specified!"); } if (elements.size() > 1) { throw new RuntimeException("Can't load config. Features section must only be specified once!"); } Class<? extends Feature> featureClass = (Class<? extends Feature>) Class.forName(elements.get(0).getElementsByTagName("featureEnum").item(0).getTextContent()); return new ObservableToggleManager(featureClass); } catch (Exception e) { throw new RuntimeException("Can't generate Feature Manager", e); } }
protected void fireStateChanged(boolean active) { for(FeatureStateListener listener : listeners) { listener.stateChanged(this.feature, active); } }