Search in sources :

Example 1 with FieldChangedEvent

use of org.jabref.model.entry.event.FieldChangedEvent in project jabref by JabRef.

the class BibEntry method setField.

/**
     * Set a field, and notify listeners about the change.
     *
     * @param name        The field to set
     * @param value       The value to set
     * @param eventSource Source the event is sent from
     */
public Optional<FieldChange> setField(String name, String value, EntryEventSource eventSource) {
    Objects.requireNonNull(name, "field name must not be null");
    Objects.requireNonNull(value, "field value must not be null");
    String fieldName = toLowerCase(name);
    if (value.isEmpty()) {
        return clearField(fieldName);
    }
    String oldValue = getField(fieldName).orElse(null);
    if (value.equals(oldValue)) {
        return Optional.empty();
    }
    if (BibEntry.ID_FIELD.equals(fieldName)) {
        throw new IllegalArgumentException("The field name '" + name + "' is reserved");
    }
    changed = true;
    fields.put(fieldName, value.intern());
    invalidateFieldCache(fieldName);
    FieldChange change = new FieldChange(this, fieldName, oldValue, value);
    eventBus.post(new FieldChangedEvent(change, eventSource));
    return Optional.of(change);
}
Also used : FieldChangedEvent(org.jabref.model.entry.event.FieldChangedEvent) FieldChange(org.jabref.model.FieldChange)

Example 2 with FieldChangedEvent

use of org.jabref.model.entry.event.FieldChangedEvent in project jabref by JabRef.

the class BibEntry method setType.

/**
     * Sets this entry's type.
     */
public void setType(String type, EntryEventSource eventSource) {
    String newType;
    if (Strings.isNullOrEmpty(type)) {
        newType = DEFAULT_TYPE;
    } else {
        newType = type;
    }
    String oldType = getField(TYPE_HEADER).orElse(null);
    // We set the type before throwing the changeEvent, to enable
    // the change listener to access the new value if the change
    // sets off a change in database sorting etc.
    this.type = newType.toLowerCase(Locale.ENGLISH);
    changed = true;
    eventBus.post(new FieldChangedEvent(this, TYPE_HEADER, newType, oldType, eventSource));
}
Also used : FieldChangedEvent(org.jabref.model.entry.event.FieldChangedEvent)

Example 3 with FieldChangedEvent

use of org.jabref.model.entry.event.FieldChangedEvent in project jabref by JabRef.

the class BibEntry method setId.

/**
     * Sets this entry's ID, provided the database containing it
     * doesn't veto the change.
     *
     * @param id The ID to be used
     */
public void setId(String id) {
    Objects.requireNonNull(id, "Every BibEntry must have an ID");
    String oldId = this.id;
    eventBus.post(new FieldChangedEvent(this, BibEntry.ID_FIELD, id, oldId));
    this.id = id;
    changed = true;
}
Also used : FieldChangedEvent(org.jabref.model.entry.event.FieldChangedEvent)

Example 4 with FieldChangedEvent

use of org.jabref.model.entry.event.FieldChangedEvent in project jabref by JabRef.

the class BibEntry method clearField.

/**
     * Remove the mapping for the field name, and notify listeners about
     * the change including the {@link EntryEventSource}.
     *
     * @param name        The field to clear.
     * @param eventSource the source a new {@link FieldChangedEvent} should be posten from.
     */
public Optional<FieldChange> clearField(String name, EntryEventSource eventSource) {
    String fieldName = toLowerCase(name);
    if (BibEntry.ID_FIELD.equals(fieldName)) {
        throw new IllegalArgumentException("The field name '" + name + "' is reserved");
    }
    Optional<String> oldValue = getField(fieldName);
    if (!oldValue.isPresent()) {
        return Optional.empty();
    }
    changed = true;
    fields.remove(fieldName);
    invalidateFieldCache(fieldName);
    FieldChange change = new FieldChange(this, fieldName, oldValue.get(), null);
    eventBus.post(new FieldChangedEvent(change, eventSource));
    return Optional.of(change);
}
Also used : FieldChangedEvent(org.jabref.model.entry.event.FieldChangedEvent) FieldChange(org.jabref.model.FieldChange)

Aggregations

FieldChangedEvent (org.jabref.model.entry.event.FieldChangedEvent)4 FieldChange (org.jabref.model.FieldChange)2