123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495 |
- /*
- * Copyright (c) 2006-2015 DMDirc Developers
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in
- * all copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
- * SOFTWARE.
- */
-
- package com.dmdirc.ui.core.profiles;
-
- import com.dmdirc.config.profiles.Profile;
- import com.dmdirc.config.profiles.ProfileManager;
- import com.dmdirc.interfaces.ui.ProfilesDialogModel;
- import com.dmdirc.interfaces.ui.ProfilesDialogModelListener;
- import com.dmdirc.util.collections.ListenerList;
- import com.dmdirc.util.validators.FileNameValidator;
- import com.dmdirc.util.validators.IdentValidator;
- import com.dmdirc.util.validators.ListNotEmptyValidator;
- import com.dmdirc.util.validators.NotEmptyValidator;
- import com.dmdirc.util.validators.PermissiveValidator;
- import com.dmdirc.util.validators.Validator;
- import com.dmdirc.util.validators.ValidatorChain;
-
- import com.google.common.collect.ImmutableList;
- import com.google.common.collect.Iterables;
- import com.google.common.collect.Lists;
-
- import java.util.Comparator;
- import java.util.List;
- import java.util.Optional;
- import java.util.SortedMap;
- import java.util.concurrent.ConcurrentSkipListMap;
-
- import javax.inject.Inject;
-
- import static com.google.common.base.Preconditions.checkArgument;
- import static com.google.common.base.Preconditions.checkNotNull;
- import static com.google.common.base.Preconditions.checkState;
-
- public class CoreProfilesDialogModel implements ProfilesDialogModel {
-
- private final ListenerList listeners;
- private final ProfileManager profileManager;
- private final SortedMap<String, MutableProfile> profiles;
- private Optional<MutableProfile> selectedProfile = Optional.empty();
- private Optional<String> selectedNickname = Optional.empty();
- private Optional<String> selectedHighlight = Optional.empty();
-
- @Inject
- public CoreProfilesDialogModel(final ProfileManager profileManager) {
- this.profileManager = profileManager;
- listeners = new ListenerList();
- profiles = new ConcurrentSkipListMap<>(Comparator.naturalOrder());
- }
-
- @Override
- public void loadModel() {
- profiles.clear();
- profileManager.getProfiles().stream().map(MutableProfile::new).forEach(mp -> {
- profiles.put(mp.getName(), mp);
- listeners.getCallable(ProfilesDialogModelListener.class).profileAdded(mp);
- });
- setSelectedProfile(Optional.ofNullable(Iterables.getFirst(profiles.values(), null)));
- }
-
- @Override
- public List<MutableProfile> getProfileList() {
- return ImmutableList.copyOf(profiles.values());
- }
-
- @Override
- public Optional<MutableProfile> getProfile(final String name) {
- checkNotNull(name, "Name cannot be null");
- return Optional.ofNullable(profiles.get(name));
- }
-
- @Override
- public void addProfile(final String name, final String realname, final String ident,
- final List<String> nicknames) {
- checkNotNull(name, "Name cannot be null");
- checkArgument(!name.isEmpty(), "Name cannot be empty");
- checkArgument(!profiles.containsKey(name), "Name cannot already exist");
- final MutableProfile profile =
- new MutableProfile(name, realname, Optional.ofNullable(ident), nicknames);
- profiles.put(name, profile);
- listeners.getCallable(ProfilesDialogModelListener.class).profileAdded(profile);
- setSelectedProfile(Optional.of(profile));
- }
-
- @Override
- public void addProfile(final String name) {
- addProfile(name, name, name, Lists.newArrayList(name));
- }
-
- @Override
- public void removeProfile(final String name) {
- checkNotNull(name, "Name cannot be null");
- checkArgument(profiles.containsKey(name), "profile must exist in list");
- removeProfile(profiles.get(name));
- }
-
- @Override
- public void removeProfile(final MutableProfile profile) {
- checkNotNull(profile, "Profile cannot be null");
- checkArgument(profiles.containsValue(profile), "profile must exist in list");
- profiles.remove(profile.getName());
- if (getSelectedProfile().isPresent() && getSelectedProfile().get().equals(profile)) {
- setSelectedProfile(Optional.ofNullable(Iterables.getFirst(profiles.values(), null)));
- setSelectedProfileIdent(Optional.empty());
- setSelectedProfileRealname(Optional.empty());
- setSelectedProfileSelectedHighlight(Optional.empty());
- setSelectedProfileName(Optional.empty());
- setSelectedProfileSelectedNickname(Optional.empty());
- setSelectedProfileHighlights(Optional.empty());
- setSelectedProfileNicknames(Optional.empty());
- }
- listeners.getCallable(ProfilesDialogModelListener.class).profileRemoved(profile);
- }
-
- @Override
- public void save() {
- final List<Profile> profileList = Lists.newArrayList(profileManager.getProfiles());
- profileList.forEach(profileManager::deleteProfile);
- profiles.values().forEach(p -> profileManager.addProfile(
- Profile.create(p.getName(), p.getRealname(), p.getIdent(),
- p.getNicknames(), p.getHighlights())));
- }
-
- @Override
- public void setSelectedProfile(final Optional<MutableProfile> profile) {
- checkNotNull(profile, "profile cannot be null");
- profile.ifPresent(mutableProfile -> checkArgument(profiles.containsValue(mutableProfile), "Profile must exist in list"));
- if (!selectedProfile.equals(profile)) {
- selectedProfile = profile;
- listeners.getCallable(ProfilesDialogModelListener.class).profileSelectionChanged(profile);
- }
- }
-
- @Override
- public Optional<MutableProfile> getSelectedProfile() {
- return selectedProfile;
- }
-
- @Override
- public Optional<String> getSelectedProfileName() {
- return selectedProfile.map(MutableProfile::getName);
- }
-
- @Override
- public void setSelectedProfileName(final Optional<String> name) {
- checkNotNull(name, "Name cannot be null");
- selectedProfile.ifPresent(p -> {
- p.setName(name.orElse(""));
- listeners.getCallable(ProfilesDialogModelListener.class).profileEdited(p);
- });
- }
-
- @Override
- public Optional<String> getSelectedProfileRealname() {
- return selectedProfile.map(MutableProfile::getRealname);
- }
-
- @Override
- public void setSelectedProfileRealname(final Optional<String> realname) {
- checkNotNull(realname, "Realname cannot be null");
- selectedProfile.ifPresent(p -> {
- p.setRealname(realname.orElse(""));
- listeners.getCallable(ProfilesDialogModelListener.class).profileEdited(p);
- });
- }
-
- @Override
- public Optional<String> getSelectedProfileIdent() {
- return selectedProfile.flatMap(MutableProfile::getIdent);
- }
-
- @Override
- public void setSelectedProfileIdent(final Optional<String> ident) {
- checkNotNull(ident, "Ident cannot be null");
- selectedProfile.ifPresent(p -> {
- p.setIdent(ident);
- listeners.getCallable(ProfilesDialogModelListener.class).profileEdited(p);
- });
- }
-
- @Override
- public Optional<List<String>> getSelectedProfileNicknames() {
- return selectedProfile.map(MutableProfile::getNicknames);
- }
-
- @Override
- public void setSelectedProfileNicknames(final Optional<List<String>> nicknames) {
- checkNotNull(nicknames, "nicknames cannot be null");
- selectedProfile.ifPresent(p -> {
- p.setNicknames(nicknames.orElse(Lists.newArrayList()));
- listeners.getCallable(ProfilesDialogModelListener.class).profileEdited(p);
- });
- }
-
- @Override
- public void setSelectedProfileHighlights(final Optional<List<String>> highlights) {
- checkNotNull(highlights, "highlights cannot be null");
- selectedProfile.ifPresent(p -> {
- p.setHighlights(highlights.orElse(Lists.newArrayList()));
- listeners.getCallable(ProfilesDialogModelListener.class).profileEdited(p);
- });
- }
-
- @Override
- public void addSelectedProfileNickname(final String nickname) {
- checkNotNull(nickname, "Nickname cannot be null");
- selectedProfile.ifPresent(p -> {
- checkArgument(!p.getNicknames().contains(nickname), "New nickname must not exist");
- p.addNickname(nickname);
- listeners.getCallable(ProfilesDialogModelListener.class)
- .selectedProfileNicknameAdded(nickname);
- });
- }
-
- @Override
- public void removeSelectedProfileNickname(final String nickname) {
- checkNotNull(nickname, "Nickname cannot be null");
- selectedProfile.ifPresent(p -> {
- checkArgument(p.getNicknames().contains(nickname), "Nickname must exist");
- p.removeNickname(nickname);
- listeners.getCallable(ProfilesDialogModelListener.class)
- .selectedProfileNicknameRemoved(nickname);
- });
- }
-
- @Override
- public void editSelectedProfileNickname(final String oldName, final String newName) {
- checkNotNull(oldName, "Nickname cannot be null");
- checkNotNull(newName, "Nickname cannot be null");
- selectedProfile.ifPresent(p -> {
- checkArgument(p.getNicknames().contains(oldName), "Old nickname must exist");
- checkArgument(!p.getNicknames().contains(newName), "New nickname must not exist");
- final int index = p.getNicknames().indexOf(oldName);
- p.setNickname(index, newName);
- selectedNickname = Optional.of(newName);
- listeners.getCallable(ProfilesDialogModelListener.class)
- .selectedProfileNicknameEdited(oldName, newName);
- });
- }
-
- @Override
- public Optional<String> getSelectedProfileSelectedNickname() {
- return selectedNickname;
- }
-
- @Override
- public Optional<String> getSelectedProfileSelectedHighlight() {
- return selectedHighlight;
- }
-
- @Override
- public void setSelectedProfileSelectedNickname(final Optional<String> selectedNickname) {
- checkNotNull(selectedNickname, "Nickname cannot be null");
- selectedProfile.ifPresent(p -> {
- selectedNickname.ifPresent(s -> checkArgument(p.getNicknames().contains(s),
- "Nickname must exist in nicknames list"));
- if (this.selectedNickname != selectedNickname) {
- this.selectedNickname = selectedNickname;
- listeners.getCallable(ProfilesDialogModelListener.class)
- .selectedNicknameChanged(selectedNickname);
- }
- });
- }
-
- @Override
- public void setSelectedProfileSelectedHighlight(final Optional<String> selectedHighlight) {
- checkNotNull(selectedHighlight, "Highlight cannot be null");
- selectedProfile.ifPresent(p -> {
- selectedHighlight.ifPresent(s -> checkArgument(p.getHighlights().contains(s),
- "Nickname must exist in nicknames list"));
- if (this.selectedHighlight != selectedHighlight) {
- this.selectedHighlight = selectedHighlight;
- listeners.getCallable(ProfilesDialogModelListener.class)
- .selectedHighlightChanged(selectedNickname);
- }
- });
- }
-
- @Override
- public void addListener(final ProfilesDialogModelListener listener) {
- checkNotNull(listener, "Listener must not be null");
- listeners.add(ProfilesDialogModelListener.class, listener);
- }
-
- @Override
- public void removeListener(final ProfilesDialogModelListener listener) {
- checkNotNull(listener, "Listener must not be null");
- listeners.remove(ProfilesDialogModelListener.class, listener);
- }
-
- @Override
- public boolean canSwitchProfiles() {
- return !selectedProfile.isPresent() ||
- isSelectedProfileIdentValid() && isSelectedProfileNameValid() &&
- isSelectedProfileNicknamesValid() && isSelectedProfileRealnameValid();
- }
-
- @Override
- public boolean isSaveAllowed() {
- return isProfileListValid() && isSelectedProfileIdentValid() &&
- isSelectedProfileNameValid() && isSelectedProfileNicknamesValid() &&
- isSelectedProfileRealnameValid();
- }
-
- @Override
- public boolean isSelectedProfileNicknamesValid() {
- return !getSelectedProfileNicknames().isPresent() || !getSelectedProfileNicknamesValidator()
- .validate(getSelectedProfileNicknames().get()).isFailure();
- }
-
- @Override
- public boolean isSelectedProfileHighlightsValid() {
- return !getSelectedProfileHighlights().isPresent() ||
- !getSelectedProfileHighlightsValidator().validate(
- getSelectedProfileHighlights().get()).isFailure();
- }
-
- @Override
- public boolean isSelectedProfileIdentValid() {
- return !getSelectedProfileIdent().isPresent() ||
- !getSelectedProfileIdentValidator().validate(getSelectedProfileIdent().get())
- .isFailure();
- }
-
- @Override
- public boolean isSelectedProfileRealnameValid() {
- return getSelectedProfileRealname().isPresent() ||
- !getSelectedProfileRealnameValidator().validate(getSelectedProfileRealname().get())
- .isFailure();
- }
-
- @Override
- public boolean isSelectedProfileNameValid() {
- return getSelectedProfileName().isPresent() &&
- !getSelectedProfileNameValidator().validate(getSelectedProfileName().get())
- .isFailure();
- }
-
- @Override
- public boolean isProfileListValid() {
- return !getProfileListValidator().validate(getProfileList()).isFailure();
- }
-
- @Override
- public Validator<List<MutableProfile>> getProfileListValidator() {
- return new ListNotEmptyValidator<>();
- }
-
- @Override
- public Validator<String> getSelectedProfileNameValidator() {
- return ValidatorChain.<String>builder()
- .addValidator(new EditSelectedProfileNameValidator(this))
- .addValidator(new FileNameValidator())
- .build();
- }
-
- @Override
- public Validator<String> getNewProfileNameValidator() {
- return ValidatorChain.<String>builder()
- .addValidator(new NewProfileNameValidator(this))
- .addValidator(new FileNameValidator())
- .build();
- }
-
- @Override
- public Validator<String> getSelectedProfileIdentValidator() {
- return new PermissiveValidator<>();
- }
-
- @Override
- public Validator<String> getSelectedProfileRealnameValidator() {
- return new NotEmptyValidator();
- }
-
- @Override
- public Validator<List<String>> getSelectedProfileNicknamesValidator() {
- return new ListNotEmptyValidator<>();
- }
-
- @Override
- public Optional<List<String>> getSelectedProfileHighlights() {
- return selectedProfile.map(MutableProfile::getHighlights);
- }
-
- @Override
- public Validator<List<String>> getSelectedProfileHighlightsValidator() {
- return new PermissiveValidator<>();
- }
-
- @Override
- public Validator<String> getSelectedProfileAddNicknameValidator() {
- return ValidatorChain.<String>builder()
- .addValidator(new NotEmptyValidator())
- .addValidator(new AddNicknameValidator(this))
- .build();
- }
-
- @Override
- public Validator<String> getSelectedProfileEditNicknameValidator() {
- return ValidatorChain.<String>builder()
- .addValidator(new NotEmptyValidator())
- .addValidator(new EditSelectedNicknameValidator(this))
- .build();
- }
-
- @Override
- public void addSelectedProfileHighlight(final String highlight) {
- checkNotNull(highlight, "highlight cannot be null");
- selectedProfile.ifPresent(p -> {
- if (!p.getHighlights().contains(highlight)) {
- p.addHighlight(highlight);
- listeners.getCallable(ProfilesDialogModelListener.class)
- .selectedProfileHighlightAdded(highlight);
- }
- });
- }
-
- @Override
- public void removeSelectedProfileHighlight(final String highlight) {
- checkNotNull(highlight, "highlight cannot be null");
- selectedProfile.ifPresent(p -> {
- if (p.getHighlights().contains(highlight)) {
- p.removeHighlight(highlight);
- listeners.getCallable(ProfilesDialogModelListener.class)
- .selectedProfileHighlightRemoved(highlight);
- }
- });
- }
-
- @Override
- public Validator<String> getSelectedProfileAddHighlightValidator() {
- return new NotEmptyValidator();
- }
-
- @Override
- public void editSelectedProfileHighlight(final String oldHighlight, final String newHighlight) {
- checkNotNull(oldHighlight, "Highlight cannot be null");
- checkNotNull(newHighlight, "Highlight cannot be null");
- checkState(selectedProfile.isPresent(), "There must be a profile selected");
- selectedProfile.ifPresent(p -> {
- checkArgument(p.getHighlights().contains(oldHighlight), "Old highlight must exist");
- checkArgument(!p.getHighlights().contains(newHighlight), "New highlight must not exist");
- final int index = p.getHighlights().indexOf(oldHighlight);
- p.setHighlight(index, newHighlight);
- selectedHighlight = Optional.of(newHighlight);
- listeners.getCallable(ProfilesDialogModelListener.class)
- .selectedProfileHighlightEdited(oldHighlight, newHighlight);
- });
- }
-
- @Override
- public Validator<String> getSelectedProfileEditHighlightValidator() {
- return new EditSelectedHighlightValidator(this);
- }
-
- @Override
- public Validator<List<String>> getNicknamesValidator() {
- return new ListNotEmptyValidator<>();
- }
-
- @Override
- public Validator<List<String>> getHighlightsValidator() {
- return new PermissiveValidator<>();
- }
-
- @Override
- public Validator<String> getRealnameValidator() {
- return new NotEmptyValidator();
- }
-
- @Override
- public Validator<String> getIdentValidator() {
- return new IdentValidator();
- }
-
- }
|