private void initialize() { bindingContext = new BindingContext(createPropertySet()); panel = new PreferencesPanel(createPanel(bindingContext), bindingContext); panel.getComponent(); // trigger component initialisation configure(bindingContext); }
@Override public JComponent getComponent(Lookup lookup) { if (!isInitialised()) { initialize(); } return panel.getComponent(); }
@Override public void cancel() { if (isInitialised()) { setChanged(false); } }
@Override public void applyChanges() { if (isInitialised()) { HashSet<Preferences> set = new HashSet<>(); for (Property property : bindingContext.getPropertySet().getProperties()) { String key = property.getDescriptor().getAttribute("key").toString(); String value = property.getValueAsText(); Preferences preferences = getPreferences(property.getDescriptor()); preferences.put(key, value); set.add(preferences); SystemUtils.LOG.fine(String.format("Preferences value change: %s = %s", key, preferences.get(key, null))); } for (Preferences preferences : set) { try { preferences.flush(); } catch (BackingStoreException e) { SnapApp.getDefault().handleError("Failed to store user preferences.", e); } } setChanged(false); } }
@Override public void update() { if (isInitialised()) { for (Property property : bindingContext.getPropertySet().getProperties()) { String key = property.getDescriptor().getAttribute("key").toString(); String preferencesValue = getPreferences(property.getDescriptor()).get(key, null); if (preferencesValue != null) { try { property.setValueFromText(preferencesValue); SystemUtils.LOG.fine(String.format("Bean property value change: %s = %s", property.getName(), property.getValueAsText())); } catch (ValidationException e) { SystemUtils.LOG.severe("Failed to set bean value from preferences: " + e.getMessage()); } } } } }
@Override public boolean isChanged() { return isInitialised() && panel.isChanged(); }
@Override public void cancel() { if (isInitialised()) { final BindingContext bindingContext = getBindingContext(); final Property showSuppressedProperty = bindingContext.getPropertySet().getProperty(PREFERENCE_KEY_SHOW_SUPPRESSED); try { showSuppressedProperty.setValue(Boolean.FALSE); } catch (ValidationException e) { // ignore } } super.cancel(); }
@Override public void applyChanges() { if (isInitialised()) { final BindingContext bindingContext = getBindingContext(); final Property showSuppressedProperty = bindingContext.getPropertySet().getProperty(PREFERENCE_KEY_SHOW_SUPPRESSED); if (Boolean.parseBoolean(showSuppressedProperty.getValueAsText())) { final Preferences preferences = SnapApp.getDefault().getPreferences(); try { final String[] childrenNames = preferences.keys(); for (String childrenName : childrenNames) { if (childrenName.endsWith(Dialogs.PREF_KEY_SUFFIX_DONTSHOW)) { preferences.putBoolean(childrenName, false); } } showSuppressedProperty.setValue(Boolean.FALSE); } catch (BackingStoreException | ValidationException e) { SnapApp.getDefault().handleError("Failure while resetting suppressed dialogs.", e); } } } super.applyChanges(); }
String description = annotation.description(); Validator validator = createValidator(annotation.validatorClass()); Assert.state(StringUtils.isNotNullAndNotEmpty(label), "Label of field '" + field.getName() + "' must not be null or empty.");
/** * Create a panel that allows the user to set the parameters in the given {@link BindingContext}. Clients that want * to create their own panel representation on the given properties need to overwrite this method. * * @param context The {@link BindingContext} for the panel. * * @return A JPanel instance for the given {@link BindingContext}, never {@code null}. */ protected JPanel createPanel(BindingContext context) { Assert.state(isInitialised()); return new PreferencesPanel(null, bindingContext).getComponent(); }
@Override public boolean isValid() { if (!isInitialised()) { return false; } for (Property property : bindingContext.getPropertySet().getProperties()) { Validator validator = (Validator) property.getDescriptor().getAttribute("propertyValidator"); try { validator.validateValue(property, property.getValue()); } catch (ValidationException e) { return false; } } return true; }