-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
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] Auto clean up url links(https://github.com/koppor/jabref/issues/254) #3394
Changes from all commits
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 |
---|---|---|
|
@@ -3,15 +3,20 @@ | |
import javafx.event.ActionEvent; | ||
import javafx.fxml.FXML; | ||
import javafx.scene.Parent; | ||
import javafx.scene.control.MenuItem; | ||
import javafx.scene.layout.HBox; | ||
|
||
import org.jabref.gui.DialogService; | ||
import org.jabref.gui.autocompleter.AutoCompleteSuggestionProvider; | ||
import org.jabref.gui.fieldeditors.contextmenu.EditorMenus; | ||
import org.jabref.gui.util.ControlHelper; | ||
import org.jabref.logic.formatter.bibtexfields.CleanupURLFormatter; | ||
import org.jabref.logic.integrity.FieldCheckers; | ||
import org.jabref.model.entry.BibEntry; | ||
import org.jabref.preferences.JabRefPreferences; | ||
|
||
import java.util.List; | ||
import java.util.function.Supplier; | ||
|
||
public class UrlEditor extends HBox implements FieldEditorFX { | ||
|
||
@FXML private UrlEditorViewModel viewModel; | ||
|
@@ -23,6 +28,15 @@ public UrlEditor(String fieldName, DialogService dialogService, AutoCompleteSugg | |
ControlHelper.loadFXMLForControl(this); | ||
|
||
textArea.textProperty().bindBidirectional(viewModel.textProperty()); | ||
Supplier<List<MenuItem>> contextMenuSupplier = EditorMenus.getCleanupURLMenu(textArea); | ||
textArea.addToContextMenu(contextMenuSupplier); | ||
MenuItem cleanupURLMenuItem = contextMenuSupplier.get().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. Thats a bit ugly: you need to know that the cleanup url menu item is the first one returned. Thus the code fails if somebody changes the |
||
textArea.setOnContextMenuRequested(event ->cleanupURLMenuItem.setDisable("".equals(textArea.getSelectedText()))); | ||
|
||
// init paste handler for URLEditor to format pasted url link in textArea | ||
textArea.setPasteActionHandler(()->{ | ||
textArea.setText(new CleanupURLFormatter().format(textArea.getText())); | ||
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. If it's a one liner lambda expression, you don't need to add curly braces |
||
}); | ||
|
||
new EditorValidator(preferences).configureValidation(viewModel.getFieldValidator().getValidationStatus(), textArea); | ||
} | ||
|
@@ -45,4 +59,5 @@ public Parent getNode() { | |
private void openExternalLink(ActionEvent event) { | ||
viewModel.openExternalLink(); | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,23 @@ | ||
package org.jabref.gui.fieldeditors.contextmenu; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.function.Supplier; | ||
|
||
import javax.swing.AbstractAction; | ||
import javax.swing.Action; | ||
|
||
import javafx.scene.control.CustomMenuItem; | ||
import javafx.scene.control.Label; | ||
import javafx.scene.control.MenuItem; | ||
import javafx.scene.control.SeparatorMenuItem; | ||
import javafx.scene.control.TextArea; | ||
import javafx.scene.control.Tooltip; | ||
|
||
import javafx.scene.control.*; | ||
import org.jabref.gui.actions.CopyDoiUrlAction; | ||
import org.jabref.gui.fieldeditors.EditorTextArea; | ||
import org.jabref.logic.formatter.bibtexfields.CleanupURLFormatter; | ||
import org.jabref.logic.formatter.bibtexfields.NormalizeNamesFormatter; | ||
import org.jabref.logic.l10n.Localization; | ||
|
||
import javax.swing.*; | ||
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. Please no general imports with a star, because that loads all classes in the package. |
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.function.Supplier; | ||
|
||
/** | ||
* Provides context menus for the text fields of the entry editor. Note that we use {@link Supplier} to prevent an early | ||
* instantiation of the menus. Therefore, they are attached to each text field but instantiation happens on the first | ||
* right-click of the user in that field. The late instantiation is done by {@link | ||
* EditorTextArea#addToContextMenu(java.util.function.Supplier)}. | ||
*/ | ||
|
||
public class EditorMenus { | ||
|
||
/** | ||
|
@@ -84,4 +77,26 @@ public static Supplier<List<MenuItem>> getDOIMenu(TextArea textArea) { | |
return menuItems; | ||
}; | ||
} | ||
/** | ||
* The default context menu with a specific menu item to cleanup URL. | ||
* | ||
* @param textArea text-area that this menu will be connected to | ||
* @return menu containing items of the default menu and an item to cleanup a URL | ||
*/ | ||
public static Supplier<List<MenuItem>> getCleanupURLMenu(TextArea textArea){ | ||
return () -> { | ||
|
||
CustomMenuItem cleanupURL = new CustomMenuItem(new Label(Localization.lang("Cleanup URL Link"))); | ||
//cleanupURL.setDisable(true); | ||
cleanupURL.setOnAction(event -> textArea.setText(new CleanupURLFormatter().format(textArea.getText()))); | ||
|
||
List<MenuItem> menuItems = new ArrayList<>(); | ||
menuItems.add(cleanupURL); | ||
menuItems.add(new SeparatorMenuItem()); | ||
menuItems.addAll(getDefaultMenu(textArea).get()); | ||
|
||
return menuItems; | ||
}; | ||
} | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package org.jabref.logic.formatter.bibtexfields; | ||
|
||
import org.apache.commons.logging.Log; | ||
import org.apache.commons.logging.LogFactory; | ||
import org.jabref.logic.l10n.Localization; | ||
import org.jabref.model.cleanup.Formatter; | ||
|
||
import java.io.UnsupportedEncodingException; | ||
import java.net.URLDecoder; | ||
import java.nio.charset.StandardCharsets; | ||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
|
||
/** | ||
* Cleanup URL link | ||
*/ | ||
|
||
public class CleanupURLFormatter implements Formatter { | ||
|
||
private static final Log LOGGER = LogFactory.getLog(CleanupURLFormatter.class); | ||
|
||
@Override | ||
public String getName() { | ||
return Localization.lang("Cleanup URL Link"); | ||
} | ||
|
||
@Override | ||
public String getKey() { | ||
return "cleanup_url"; | ||
} | ||
|
||
@Override | ||
public String format(String value) { | ||
URLDecoder urlDecoder = new URLDecoder(); | ||
|
||
String decodedLink = value; | ||
String toDecode = value; | ||
|
||
// This regexp find "url=" or "to=" parameter in full link and get text after them | ||
String urlRegexp = "(?:url|to)=([^&]*)"; | ||
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. Extract the pattern to a static Pattern and use Pattern.compile |
||
Pattern urlExtruder = Pattern.compile(urlRegexp); | ||
Matcher matcher = urlExtruder.matcher(value); | ||
if(matcher.find()) | ||
toDecode = matcher.group(1); | ||
|
||
try { | ||
decodedLink = URLDecoder.decode(toDecode, StandardCharsets.UTF_8.name()); | ||
} | ||
catch (UnsupportedEncodingException e){ | ||
LOGGER.warn("Used unsupported character encoding", e); | ||
} | ||
return decodedLink; | ||
} | ||
|
||
@Override | ||
public String getDescription() { | ||
return "Cleanup URL Link by removing special symbols and extracting simple link"; | ||
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. Description should be localized, too |
||
} | ||
|
||
@Override | ||
public String getExampleInput() { | ||
return "https://www.google.de/url?sa=t&rct=j&q=&esrc=s&source=web&cd=11&cad=" + | ||
"rja&uact=8&ved=0ahUKEwjg3ZrB_ZPXAhVGuhoKHYdOBOg4ChAWCCYwAA&url=" + | ||
"http%3A%2F%2Fwww.focus.de%2Fgesundheit%2Fratgeber%2Fherz%2Ftest%2" + | ||
"Flebenserwartung-werden-sie-100-jahre-alt_aid_363828.html" + "&usg=AOvVaw1G6m2jf-pTHYkXceii4hXU"; | ||
} | ||
} |
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.
Avoid the non null annotation and use Objects.requireNonNulll