-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[WIP] Convert Exporter Customization Dialog to javafx #4394
Changes from 11 commits
f120b41
12e3e7d
ec364ad
f5c89fa
6773b22
1f318f0
1a0b760
5c65b57
2de4124
a869aa2
9ae84c0
e992c5d
3a4baa3
f63d43c
1f094d6
83b7dac
99d7041
0479893
547898c
e2ae2c9
3be5672
debd2bc
7141a46
889489b
1fb234d
e17d0da
73e683a
19720cc
4e8c8df
3618f83
51a726e
1821da3
d339f78
eea34ed
d36eec2
294a367
4269ca0
25db576
3b54bc8
84f293e
00bac82
b26d592
b681ae8
be47855
9aa0887
d7277e8
7087cfb
1daf7c4
83d490a
17d20cc
40eed27
379f201
4679150
0053abf
9442025
de3bf63
19d8c62
df42676
f31bc51
d95a1aa
80ffa33
5f56c25
f1d920e
f22e97a
252ecbb
ccdbf1f
aba31a6
8cd7ff3
3e09a7b
3051022
0ef3061
7bf6dd6
93838de
a79d037
70479cb
bf22adf
c2b3489
a364e1d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
package org.jabref.gui.exporter; | ||
|
||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
import java.util.Optional; | ||
|
||
import javafx.beans.property.SimpleStringProperty; | ||
|
||
import org.jabref.gui.AbstractViewModel; | ||
import org.jabref.gui.DialogService; | ||
import org.jabref.gui.util.FileDialogConfiguration; | ||
import org.jabref.logic.exporter.SavePreferences; | ||
import org.jabref.logic.exporter.TemplateExporter; | ||
import org.jabref.logic.l10n.Localization; | ||
import org.jabref.logic.layout.LayoutFormatterPreferences; | ||
import org.jabref.logic.util.FileType; | ||
import org.jabref.logic.util.StandardFileType; | ||
import org.jabref.preferences.JabRefPreferences; //will be removed with writing of new method | ||
import org.jabref.preferences.PreferencesService; | ||
|
||
public class CreateModifyExporterDialogViewModel extends AbstractViewModel { | ||
|
||
//This could be separated into two dialogs, one for adding exporters and one for modifying them. | ||
//See the two constructors for the view | ||
|
||
//The view will return a TemplateExporter | ||
//You will have to look at Swing class CustomExportDialog when you write the View | ||
|
||
//Cancel to be implemented in View, not here | ||
|
||
private final TemplateExporter exporter; | ||
private final DialogService dialogService; | ||
private final PreferencesService preferences; | ||
|
||
private final SimpleStringProperty name; //prevent saveExporter from saving this if it's null | ||
private final SimpleStringProperty layoutFile; | ||
private final SimpleStringProperty extension; | ||
|
||
|
||
public CreateModifyExporterDialogViewModel(TemplateExporter exporter, DialogService dialogService, PreferencesService preferences) { | ||
this.exporter = exporter; | ||
this.dialogService = dialogService; | ||
this.preferences = preferences; | ||
//Set text of each of the boxes | ||
|
||
} | ||
|
||
public TemplateExporter saveExporter() {//void? | ||
Path layoutFileDir = Paths.get(layoutFile.get()).getParent(); | ||
if (layoutFileDir != null) { | ||
preferences.put(JabRefPreferences.EXPORT_WORKING_DIRECTORY, layoutFileDir.toString()); //fix to work with PreferencesService | ||
|
||
} | ||
preferences.saveExportWorkingDirectory(layoutFileDir.toString()); //See Swing class CustomExportDialog for more on how implement this | ||
// Create a new exporter to be returned to ExportCustomizationDialog, which requested it | ||
LayoutFormatterPreferences layoutPreferences = preferences.getLayoutFormatterPreferences(); | ||
SavePreferences savePreferences = preferences.LoadForExportFromPreferences(); | ||
String filename = layoutFile.get(); //change var name? | ||
String extensionString = extension.get(); //change var name? | ||
String lfFileName; | ||
//You might want to move the next few lines into logic because it also appears in JabRefPreferences | ||
if (filename.endsWith(".layout")) { | ||
lfFileName = filename.substring(0, filename.length() - ".layout".length()); | ||
} else { | ||
lfFileName = filename; | ||
} | ||
if (extensionString.contains(".")) { | ||
extension.set(extensionString.substring(extensionString.indexOf('.') + 1, extensionString.length())); | ||
} | ||
FileType fileType = StandardFileType.newFileType(extensionString); | ||
TemplateExporter format = new TemplateExporter(name.get(), name.get(), lfFileName, null, fileType, layoutPreferences, | ||
savePreferences); | ||
format.setCustomExport(true); | ||
return format; | ||
} | ||
|
||
public String getExportWorkingDirectory() {//i.e. layout dir | ||
return preferences.getExportWorkingDirectory(); | ||
} | ||
|
||
public void browse() { | ||
FileDialogConfiguration fileDialogConfiguration = new FileDialogConfiguration.Builder() | ||
.addExtensionFilter(Localization.lang("Custom layout file"), StandardFileType.LAYOUT) | ||
.withDefaultExtension(Localization.lang("Custom layout file"), StandardFileType.LAYOUT) | ||
.withInitialDirectory(getExportWorkingDirectory()).build(); | ||
dialogService.showFileOpenDialog(fileDialogConfiguration).ifPresent(f -> setText(f.toAbsolutePath().toString())); //implement setting the text | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To set the text, you would create a StringProperty here, e.g. selectedPath and then bind that property to a textfield in the View There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed it. I think I had the property, but I hadn't realized this is what I needed here. Thanks for your recommendation! |
||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
package org.jabref.gui.exporter; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import javafx.beans.property.SimpleListProperty; | ||
import javafx.collections.FXCollections; | ||
|
||
import org.jabref.gui.AbstractViewModel; | ||
import org.jabref.gui.DialogService; | ||
import org.jabref.logic.exporter.TemplateExporter; | ||
import org.jabref.logic.journals.JournalAbbreviationLoader; | ||
import org.jabref.preferences.PreferencesService; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
public class ExportCustomizationDialogViewModel extends AbstractViewModel { | ||
|
||
//The class vars might need to be reordered | ||
|
||
//exporters should probably be a JavaFX SortedList instead of SimpleListPreperty, | ||
//but not yet sure how to make SortedList into a property | ||
private final SimpleListProperty<ExporterViewModel> exporters = new SimpleListProperty<>(FXCollections.observableArrayList()); | ||
|
||
private final int size; //final? Or you don't need this and just use a a while loop | ||
|
||
//Indices within which export format information is stored within JabRefPreferences | ||
private static final int EXPORTER_NAME_INDEX = 0; | ||
private static final int EXPORTER_FILENAME_INDEX = 1; | ||
private static final int EXPORTER_EXTENSION_INDEX = 2; | ||
|
||
private final PreferencesService preferences; | ||
private final DialogService dialogService; | ||
private final JournalAbbreviationLoader loader; | ||
|
||
private static final Logger LOGGER = LoggerFactory.getLogger(CustomExportList.class); | ||
|
||
//Other variable declarations here | ||
|
||
//Also write tests for all of this if necessary | ||
|
||
public ExportCustomizationDialogViewModel(DialogService dialogService, JournalAbbreviationLoader loader) { | ||
this.dialogService = dialogService; | ||
this.loader = loader; | ||
init(); | ||
|
||
//ExporterViewModel is organized as a singular version of what now is CustomExportDialog, which | ||
//currently stores all the exporters in a class var. Each ExporterViewModel wraps an exporter, and | ||
//the class var exporters is a list of the ExporterViewModels | ||
|
||
} | ||
|
||
public void loadExporters() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would prefer if this method can be made private. |
||
List<TemplateExporter> exportersLogic = preferences.getCustomExportFormats(loader); //Var may need more descriptive name | ||
for (TemplateExporter exporter : exportersLogic) { | ||
exporters.add(new ExporterViewModel(exporter)); | ||
} | ||
} | ||
|
||
//The following method will have to be implemented to get information from the JavaFX analogue of Swing CustomExportDialog | ||
public void addExporter() { | ||
// open add Exporter dialog, set vars as dialogResult or analogous | ||
TemplateExporter exporter = new CreateModifyExporterDialogView().show(); //Not sure if this is right | ||
exporters.add(new ExporterViewModel(exporter));//var might have to be renamed | ||
|
||
} | ||
|
||
public void modifyExporter(int row) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do you really need an int here? It would be generally better if you just operate on the row object There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think I need it. I changed it to use a property |
||
// open modify Exporter dialog, which may be the same as add Exporter dialog, and set that into exporters. | ||
exporters.set(row, new ExporterViewModel(dialogResult)); //result must come from dialog | ||
} | ||
|
||
public void removeExporters(int[] rows) { | ||
if (rows.length == 0) { // Is this check necessary? Probably not | ||
return; | ||
} | ||
for (int i = 0; i < rows.length; i++) { | ||
exporters.remove(rows[i]); | ||
} | ||
} | ||
|
||
public void saveToPrefs() { | ||
List<TemplateExporter> exportersLogic; | ||
exporters.forEach(exporter -> exportersLogic.add(exporter.getLogic())); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I find |
||
preferences.storeCustomExportFormats(exportersLogic); | ||
} | ||
public void init() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. inline? |
||
loadExporters(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package org.jabref.gui.exporter; | ||
|
||
import org.jabref.logic.exporter.TemplateExporter; | ||
|
||
public class ExporterViewModel { | ||
|
||
TemplateExporter exporter; | ||
|
||
public ExporterViewModel(TemplateExporter exporter) { | ||
this.exporter = exporter; | ||
} | ||
|
||
public TemplateExporter getLogic() { | ||
return this.exporter; | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -79,7 +79,9 @@ | |
import org.jabref.logic.protectedterms.ProtectedTermsPreferences; | ||
import org.jabref.logic.remote.RemotePreferences; | ||
import org.jabref.logic.shared.prefs.SharedDatabasePreferences; | ||
import org.jabref.logic.util.FileType; | ||
import org.jabref.logic.util.OS; | ||
import org.jabref.logic.util.StandardFileType; | ||
import org.jabref.logic.util.UpdateFieldPreferences; | ||
import org.jabref.logic.util.Version; | ||
import org.jabref.logic.util.io.AutoLinkPreferences; | ||
|
@@ -2012,4 +2014,68 @@ public Map<String, SortType> getMainTableColumnSortTypes() { | |
} | ||
return map; | ||
} | ||
|
||
@Override | ||
public List<TemplateExporter> getCustomExportFormats(JournalAbbreviationLoader loader) { | ||
int i = 0; | ||
List<TemplateExporter> formats = null; | ||
String exporterName; | ||
String filename; | ||
String extension; | ||
String lfFileName; | ||
LayoutFormatterPreferences layoutPreferences = getLayoutFormatterPreferences(loader); | ||
SavePreferences savePreferences = loadForExportFromPreferences(); | ||
List<String> s; | ||
// possibly check if CUSTOM_EXPORT_FORMAT + 0 is empty too and throw error | ||
while (!((s = getStringList(CUSTOM_EXPORT_FORMAT + i)).isEmpty())) { | ||
exporterName = s.get(0); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Pleas use constants for the indicies, e.g. EXPORTER_NAME_INDEX = 0 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have fixed these issues. |
||
filename = s.get(1); // 0, 1, 2 were originally static vars | ||
extension = s.get(2); | ||
//You might want to put the following few lines in logic because it also appears in CreateModifyExporterDialogViewModel | ||
if (filename.endsWith(".layout")) { | ||
lfFileName = filename.substring(0, filename.length() - ".layout".length()); | ||
} else { | ||
lfFileName = filename; | ||
} | ||
if (extension.contains(".")) { | ||
extension = extension.substring(extension.indexOf('.') + 1, extension.length()); | ||
} | ||
FileType fileType = StandardFileType.newFileType(extension); | ||
Optional<TemplateExporter> format = Optional.of(new TemplateExporter(exporterName, exporterName, lfFileName, | ||
null, fileType, layoutPreferences, | ||
savePreferences)); | ||
format.get().setCustomExport(true); //Taken out of orig CustomExporerList | ||
if (format.isPresent()) { | ||
formats.add(format.get()); | ||
} else { | ||
String customExportFormat = get(JabRefPreferences.CUSTOM_EXPORT_FORMAT + i); | ||
LOGGER.error("Error initializing custom export format from string " + customExportFormat); | ||
} | ||
i++; | ||
} | ||
return formats; | ||
} | ||
|
||
@Override | ||
public void storeCustomExportFormats(List<TemplateExporter> exporters) { | ||
if (exporters.isEmpty()) { | ||
purgeCustomExportFormats(0); | ||
} else { | ||
for (int i = 0; i < exporters.size(); i++) { | ||
List<String> exporterData = new ArrayList<>(); | ||
exporterData.add(exporters.get(i).getName()); | ||
exporterData.add(exporters.get(i).getId()); | ||
putStringList(CUSTOM_EXPORT_FORMAT + i, exporterData); | ||
} | ||
purgeCustomExportFormats(exporters.size()); | ||
} | ||
} | ||
|
||
private void purgeCustomExportFormats(int from) { | ||
int i = from; | ||
while (!getStringList(CUSTOM_EXPORT_FORMAT + i).isEmpty()) { | ||
remove(CUSTOM_EXPORT_FORMAT + i); | ||
i++; | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am sure checkstyle will complain about too many empty lines(not more than one)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am fixing spacing and putting it in my next push. Thanks!