|
1 | 1 | package org.jabref.gui.preferences.journals;
|
2 | 2 |
|
3 |
| -import java.io.FileNotFoundException; |
4 | 3 | import java.io.IOException;
|
5 | 4 | import java.nio.file.Files;
|
| 5 | +import java.nio.file.NoSuchFileException; |
6 | 6 | import java.nio.file.Path;
|
7 | 7 | import java.util.Collection;
|
8 | 8 | import java.util.List;
|
9 | 9 | import java.util.Objects;
|
10 | 10 | import java.util.Optional;
|
11 | 11 | import java.util.stream.Collectors;
|
| 12 | +import java.util.ArrayList; |
12 | 13 |
|
| 14 | +import javafx.beans.property.BooleanProperty; |
13 | 15 | import javafx.beans.property.ReadOnlyBooleanProperty;
|
| 16 | +import javafx.beans.property.ReadOnlyBooleanWrapper; |
14 | 17 | import javafx.beans.property.SimpleBooleanProperty;
|
15 | 18 | import javafx.beans.property.SimpleListProperty;
|
| 19 | +import javafx.beans.property.SimpleStringProperty; |
16 | 20 | import javafx.collections.FXCollections;
|
| 21 | +import javafx.collections.ObservableList; |
17 | 22 |
|
18 | 23 | import org.jabref.logic.journals.Abbreviation;
|
19 | 24 | import org.jabref.logic.journals.AbbreviationWriter;
|
20 | 25 | import org.jabref.logic.journals.JournalAbbreviationLoader;
|
| 26 | +import org.jabref.logic.journals.JournalAbbreviationRepository; |
| 27 | + |
| 28 | +import org.slf4j.Logger; |
| 29 | +import org.slf4j.LoggerFactory; |
21 | 30 |
|
22 | 31 | /**
|
23 | 32 | * This class provides a model for abbreviation files. It actually doesn't save the files as objects but rather saves
|
24 | 33 | * their paths. This also allows to specify pseudo files as placeholder objects.
|
25 | 34 | */
|
26 | 35 | public class AbbreviationsFileViewModel {
|
| 36 | + private static final Logger LOGGER = LoggerFactory.getLogger(AbbreviationsFileViewModel.class); |
27 | 37 |
|
28 |
| - private final SimpleListProperty<AbbreviationViewModel> abbreviations = new SimpleListProperty<>( |
29 |
| - FXCollections.observableArrayList()); |
30 |
| - private final ReadOnlyBooleanProperty isBuiltInList; |
31 |
| - private final String name; |
32 |
| - private final Optional<Path> path; |
33 |
| - |
34 |
| - public AbbreviationsFileViewModel(Path filePath) { |
35 |
| - this.path = Optional.ofNullable(filePath); |
36 |
| - this.name = path.get().toAbsolutePath().toString(); |
37 |
| - this.isBuiltInList = new SimpleBooleanProperty(false); |
38 |
| - } |
| 38 | + private final SimpleStringProperty name = new SimpleStringProperty(); |
| 39 | + private final ReadOnlyBooleanWrapper isBuiltInList = new ReadOnlyBooleanWrapper(); |
| 40 | + private final SimpleListProperty<AbbreviationViewModel> abbreviations = new SimpleListProperty<>(FXCollections.observableArrayList()); |
| 41 | + private final Path filePath; |
| 42 | + private final SimpleBooleanProperty enabled = new SimpleBooleanProperty(true); |
39 | 43 |
|
40 | 44 | /**
|
41 |
| - * This constructor should only be called to create a pseudo abbreviation file for built in lists. This means it is |
42 |
| - * a placeholder and its path will be null meaning it has no place on the filesystem. Its isPseudoFile property |
43 |
| - * will therefore be set to true. |
| 45 | + * This creates a built in list containing the abbreviations from the given list |
| 46 | + * |
| 47 | + * @param name The name of the built in list |
44 | 48 | */
|
45 | 49 | public AbbreviationsFileViewModel(List<AbbreviationViewModel> abbreviations, String name) {
|
46 | 50 | this.abbreviations.addAll(abbreviations);
|
47 |
| - this.name = name; |
48 |
| - this.path = Optional.empty(); |
49 |
| - this.isBuiltInList = new SimpleBooleanProperty(true); |
| 51 | + this.name.setValue(name); |
| 52 | + this.isBuiltInList.setValue(true); |
| 53 | + this.filePath = null; |
50 | 54 | }
|
51 | 55 |
|
52 |
| - public void readAbbreviations() throws IOException { |
53 |
| - if (path.isPresent()) { |
54 |
| - Collection<Abbreviation> abbreviationList = JournalAbbreviationLoader.readAbbreviationsFromCsvFile(path.get()); |
55 |
| - abbreviationList.forEach(abbreviation -> abbreviations.addAll(new AbbreviationViewModel(abbreviation))); |
56 |
| - } else { |
57 |
| - throw new FileNotFoundException(); |
58 |
| - } |
| 56 | + public AbbreviationsFileViewModel(Path filePath) { |
| 57 | + this.name.setValue(filePath.getFileName().toString()); |
| 58 | + this.filePath = filePath; |
| 59 | + this.isBuiltInList.setValue(false); |
59 | 60 | }
|
60 | 61 |
|
61 |
| - /** |
62 |
| - * This method will write all abbreviations of this abbreviation file to the file on the file system. |
63 |
| - * It essentially will check if the current file is a builtin list and if not it will call |
64 |
| - * {@link AbbreviationWriter#writeOrCreate}. |
65 |
| - */ |
66 |
| - public void writeOrCreate() throws IOException { |
67 |
| - if (!isBuiltInList.get()) { |
68 |
| - List<Abbreviation> actualAbbreviations = |
69 |
| - abbreviations.stream().filter(abb -> !abb.isPseudoAbbreviation()) |
70 |
| - .map(AbbreviationViewModel::getAbbreviationObject) |
71 |
| - .collect(Collectors.toList()); |
72 |
| - AbbreviationWriter.writeOrCreate(path.get(), actualAbbreviations); |
73 |
| - } |
| 62 | + public boolean exists() { |
| 63 | + return isBuiltInList.get() || Files.exists(filePath); |
| 64 | + } |
| 65 | + |
| 66 | + public SimpleStringProperty nameProperty() { |
| 67 | + return name; |
| 68 | + } |
| 69 | + |
| 70 | + public ReadOnlyBooleanProperty isBuiltInListProperty() { |
| 71 | + return isBuiltInList.getReadOnlyProperty(); |
74 | 72 | }
|
75 | 73 |
|
76 | 74 | public SimpleListProperty<AbbreviationViewModel> abbreviationsProperty() {
|
77 | 75 | return abbreviations;
|
78 | 76 | }
|
79 | 77 |
|
80 |
| - public boolean exists() { |
81 |
| - return path.isPresent() && Files.exists(path.get()); |
| 78 | + public void readAbbreviations() throws IOException { |
| 79 | + if (isBuiltInList.get()) { |
| 80 | + return; |
| 81 | + } |
| 82 | + try { |
| 83 | + Collection<Abbreviation> abbreviationsFromFile = JournalAbbreviationLoader.readAbbreviationsFromCsvFile(filePath); |
| 84 | + |
| 85 | + List<AbbreviationViewModel> viewModels = abbreviationsFromFile.stream() |
| 86 | + .map(AbbreviationViewModel::new) |
| 87 | + .collect(Collectors.toCollection(ArrayList::new)); |
| 88 | + abbreviations.setAll(viewModels); |
| 89 | + } catch (NoSuchFileException e) { |
| 90 | + LOGGER.debug("Journal abbreviation list {} does not exist", filePath); |
| 91 | + } |
82 | 92 | }
|
83 | 93 |
|
84 |
| - public Optional<Path> getAbsolutePath() { |
85 |
| - return path; |
| 94 | + public void writeOrCreate() throws IOException { |
| 95 | + if (isBuiltInList.get()) { |
| 96 | + return; |
| 97 | + } |
| 98 | + |
| 99 | + List<Abbreviation> abbreviationList = abbreviationsProperty().stream() |
| 100 | + .map(AbbreviationViewModel::getAbbreviationObject) |
| 101 | + .collect(Collectors.toList()); |
| 102 | + AbbreviationWriter.writeOrCreate(filePath, abbreviationList); |
86 | 103 | }
|
87 | 104 |
|
88 |
| - public ReadOnlyBooleanProperty isBuiltInListProperty() { |
89 |
| - return isBuiltInList; |
| 105 | + /** |
| 106 | + * Gets the absolute path of this abbreviation file |
| 107 | + * |
| 108 | + * @return The optional absolute path of the file, or empty if it's a built-in list |
| 109 | + */ |
| 110 | + public Optional<Path> getAbsolutePath() { |
| 111 | + if (isBuiltInList.get()) { |
| 112 | + return Optional.empty(); |
| 113 | + } |
| 114 | + |
| 115 | + try { |
| 116 | + Path normalizedPath = filePath.toAbsolutePath().normalize(); |
| 117 | + return Optional.of(normalizedPath); |
| 118 | + } catch (Exception e) { |
| 119 | + return Optional.of(filePath); |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + /** |
| 124 | + * Checks if this source is enabled |
| 125 | + * |
| 126 | + * @return true if the source is enabled |
| 127 | + */ |
| 128 | + public boolean isEnabled() { |
| 129 | + return enabled.get(); |
| 130 | + } |
| 131 | + |
| 132 | + /** |
| 133 | + * Sets the enabled state of this source |
| 134 | + * |
| 135 | + * @param enabled true to enable the source, false to disable it |
| 136 | + */ |
| 137 | + public void setEnabled(boolean enabled) { |
| 138 | + this.enabled.set(enabled); |
| 139 | + } |
| 140 | + |
| 141 | + /** |
| 142 | + * Gets the enabled property for binding |
| 143 | + * |
| 144 | + * @return the enabled property |
| 145 | + */ |
| 146 | + public BooleanProperty enabledProperty() { |
| 147 | + return enabled; |
90 | 148 | }
|
91 | 149 |
|
92 | 150 | @Override
|
93 |
| - public String toString() { |
94 |
| - return name; |
| 151 | + public boolean equals(Object o) { |
| 152 | + if (this == o) { |
| 153 | + return true; |
| 154 | + } |
| 155 | + if ((o == null) || (getClass() != o.getClass())) { |
| 156 | + return false; |
| 157 | + } |
| 158 | + AbbreviationsFileViewModel viewModel = (AbbreviationsFileViewModel) o; |
| 159 | + if (isBuiltInList.get() && viewModel.isBuiltInList.get()) { |
| 160 | + return name.get().equals(viewModel.name.get()); |
| 161 | + } |
| 162 | + return !isBuiltInList.get() && !viewModel.isBuiltInList.get() && |
| 163 | + Objects.equals(filePath, viewModel.filePath); |
95 | 164 | }
|
96 | 165 |
|
97 | 166 | @Override
|
98 | 167 | public int hashCode() {
|
99 |
| - return Objects.hash(name); |
| 168 | + return Objects.hash(isBuiltInList, filePath); |
100 | 169 | }
|
101 | 170 |
|
102 | 171 | @Override
|
103 |
| - public boolean equals(Object obj) { |
104 |
| - if (obj instanceof AbbreviationsFileViewModel model) { |
105 |
| - return Objects.equals(this.name, model.name); |
106 |
| - } else { |
107 |
| - return false; |
108 |
| - } |
| 172 | + public String toString() { |
| 173 | + return name.get(); |
109 | 174 | }
|
110 | 175 | }
|
| 176 | + |
0 commit comments