Skip to content

Commit

Permalink
improve Search and remove unused dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
kryptonbutterfly committed Mar 25, 2023
1 parent d03be1a commit 1062ca6
Show file tree
Hide file tree
Showing 11 changed files with 67 additions and 46 deletions.
6 changes: 0 additions & 6 deletions .classpath
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,6 @@
<attribute name="add-opens" value="java.desktop/sun.awt.X11=de.tinycodecrank.ReflectionUtils"/>
</attributes>
</classpathentry>
<classpathentry kind="lib" path="dep/commons-text-1.9.jar" sourcepath="dep/commons-text-1.9-sources.jar">
<attributes>
<attribute name="javadoc_location" value="jar:platform:/resource/Localizer4J/dep/commons-text-1.9-javadoc.jar!/"/>
<attribute name="module" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="lib" path="dep/gson-2.10.1.jar">
<attributes>
<attribute name="javadoc_location" value="jar:platform:/resource/Localizer4J/dep/gson-2.10.1-javadoc.jar!/"/>
Expand Down
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,10 @@ A Language file Editor with special sauce for java projects.

java version | app version | jar | deb
:----------: | :---------: | :-: | :-:
18+ | 2.3.0 | [Localizer4J.jar](https://github.com/tinycodecrank/Localizer4J/releases/download/v2.3.0/Localizer4J.jar) | [Localizer4J-2.3.0.deb](https://github.com/tinycodecrank/Localizer4J/releases/download/v2.3.0/Localizer4J-2.3.0.deb)
18+ | 2.4.0 | [Localizer4J.jar](https://github.com/tinycodecrank/Localizer4J/releases/download/v2.4.0/Localizer4J.jar) | [Localizer4J-2.4.0.deb](https://github.com/tinycodecrank/Localizer4J/releases/download/v2.4.0/Localizer4J-2.4.0.deb)

## Dependencies

* [**commons-text-1.9.jar**](https://repo1.maven.org/maven2/org/apache/commons/commons-text/1.9/commons-text-1.9.jar)
* [**gson.jar**](https://search.maven.org/remotecontent?filepath=com/google/code/gson/gson/2.10.1/gson-2.10.1.jar)
* [**tinyArgsManager.jar**](https://github.com/tinycodecrank/tinyArgsManager/releases/download/v0.1.0/tinyArgsmanager.jar)
* [**BoundedValues.jar**](https://github.com/tinycodecrank/BoundedValues/releases/download/v1.0.0/BoundedValues.jar)
Expand Down
2 changes: 1 addition & 1 deletion deb-content/version.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2.3.0
2.4.0
10 changes: 0 additions & 10 deletions src/de/tinycodecrank/l4j/data/gui/Translatable.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,11 @@
import java.awt.Color;
import java.util.Objects;

import org.apache.commons.text.similarity.LevenshteinDistance;

import de.tinycodecrank.l4j.util.ColorUtils;
import de.tinycodecrank.l4j.util.Constants;

public abstract class Translatable implements Comparable<Translatable>, Constants
{
private static final LevenshteinDistance distance = new LevenshteinDistance(5);

private String key;
private TranslationState state;

Expand Down Expand Up @@ -42,12 +38,6 @@ public int compareTo(Translatable o)
}
}

public int getDistance(String key)
{
Objects.requireNonNull(key);
return distance.apply(key, this.key);
}

public final String getKey()
{
return key;
Expand Down
25 changes: 18 additions & 7 deletions src/de/tinycodecrank/l4j/ui/main/BusinessLogic.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.function.BiFunction;
import java.util.function.Consumer;

import javax.swing.JFileChooser;
Expand Down Expand Up @@ -73,7 +74,7 @@ void init(ProgramArgs args)
e ->
{},
l10n,
new SearchKeyData(gui.table, this::setSelection))));
new SearchKeyData(gui.table, this::searchKey))));

if (args.projectFile == null || args.projectFile.length <= 0)
return;
Expand Down Expand Up @@ -755,15 +756,23 @@ private Consumer<DocumentEvent> create_addKey_Search()
return e -> gui.if_(gui ->
{
final String searchText = gui.txtAddKey.getText();
if (!StringUtils.isNotEmpty(searchText))
return;

searchKey(searchText, StringUtils::camelDistance);
});
}

private void searchKey(String search, BiFunction<String, String, Opt<Integer>> distanceFunction)
{
if (!StringUtils.isNotBlank(search))
return;

gui.map(gui ->
{
int distance = Integer.MAX_VALUE;
String match = null;
for (int row : range(gui.tableModel.getRowCount()))
{
final var translatable = gui.tableModel.getTranslatable(row);
final var dist = StringUtils.camelDistance(searchText, translatable.getKey());
final var dist = distanceFunction.apply(search, translatable.getKey());
if (dist.isPresent())
{
int intDist = dist.get(() -> Integer.MAX_VALUE);
Expand All @@ -774,8 +783,10 @@ private Consumer<DocumentEvent> create_addKey_Search()
}
}
}
if (match != null)
setSelection(match);
return match;
}).if_(s ->
{
setSelection(s);
});
}

Expand Down
21 changes: 16 additions & 5 deletions src/de/tinycodecrank/l4j/ui/search/BL.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package de.tinycodecrank.l4j.ui.search;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JTextField;
import java.util.function.BiFunction;

import de.tinycodecrank.functions.UnaryOperator;
import de.tinycodecrank.l4j.startup.Localizer4J;
import de.tinycodecrank.l4j.util.StringUtils;
import de.tinycodecrank.monads.opt.Opt;
import de.tinycodecrank.util.swing.DialogLogicTemplate;

final class BL extends DialogLogicTemplate<SearchGui, SearchKeyData>
Expand All @@ -18,9 +19,19 @@ final class BL extends DialogLogicTemplate<SearchGui, SearchKeyData>
this.data = data;
}

ActionListener search(JTextField text)
void search(ActionEvent ae)
{
return ae -> data.setSelection().accept(text.getText());
gui.if_(gui ->
{
final UnaryOperator<String> transformer = gui.keyTab.chckbxCaseSensitive.isSelected()
? UnaryOperator.identity()
: String::toLowerCase;

BiFunction<String, String, Opt<Integer>> search = (left, right) -> StringUtils
.matchOffset(left, transformer.apply(right));

data.setSelection().accept(transformer.apply(gui.keyTab.txtSearch.getText()), search);
});
}

void abort(ActionEvent ae)
Expand Down
21 changes: 10 additions & 11 deletions src/de/tinycodecrank/l4j/ui/search/KeyTab.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,38 +17,37 @@

final class KeyTab
{
static JPanel buildKeyTab(
SearchGui gui,
Opt<BL> bL,
GuiPrefs guiPrefs,
BiConsumer<String, Consumer<String>> reg)
final JTextField txtSearch;
final JPanel panel;
final JCheckBox chckbxCaseSensitive;

KeyTab(SearchGui gui, Opt<BL> bL, GuiPrefs guiPrefs, BiConsumer<String, Consumer<String>> reg)
{
final var panelContent = Box.createVerticalBox();

final var txtSearch = new JTextField();
txtSearch = new JTextField();
panelContent.add(txtSearch, BorderLayout.CENTER);

final var panelSettings = Box.createVerticalBox();
final var chckbxCaseSensitive = new JCheckBox();
final var panelSettings = Box.createVerticalBox();
chckbxCaseSensitive = new JCheckBox();
chckbxCaseSensitive.setRolloverEnabled(true);
reg.accept("Search.Checkbox.Case Sensitive", chckbxCaseSensitive::setText);
panelSettings.add(chckbxCaseSensitive);

final var panel = new JPanel(new BorderLayout());
panel = new JPanel(new BorderLayout());
panel.add(panelContent, BorderLayout.CENTER);
panel.add(panelSettings, BorderLayout.EAST);

bL.if_(bl ->
{
final var applyAbortPanel = new ApplyAbortPanel(
buttonSearch,
bl.search(txtSearch),
bl::search,
buttonCancel,
bl::abort);
reg.accept(buttonSearch, applyAbortPanel.btnButton1::setText);
reg.accept(buttonCancel, applyAbortPanel.btnButton2::setText);
panel.add(applyAbortPanel, BorderLayout.SOUTH);
});
return panel;
}
}
5 changes: 4 additions & 1 deletion src/de/tinycodecrank/l4j/ui/search/SearchGui.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ public final class SearchGui extends ObservableLangDialog<BL, Void, SearchKeyDat
static final String buttonSearch = "Search.button.search";
static final String buttonCancel = "Search.button.cancel";

final KeyTab keyTab;

public SearchGui(
Window owner,
ModalityType modality,
Expand All @@ -36,7 +38,8 @@ public SearchGui(
final var tabbedPane = new JTabbedPane(JTabbedPane.TOP);
setContentPane(tabbedPane);

tabbedPane.addTab(null, KeyTab.buildKeyTab(this, businessLogic, guiPrefs, this::reg));
keyTab = new KeyTab(this, businessLogic, guiPrefs, this::reg);
tabbedPane.addTab(null, keyTab.panel);
reg("Search.Tab.Key", s -> tabbedPane.setTitleAt(0, s));
tabbedPane.setEnabledAt(0, true);
}
Expand Down
7 changes: 5 additions & 2 deletions src/de/tinycodecrank/l4j/ui/search/SearchKeyData.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package de.tinycodecrank.l4j.ui.search;

import java.util.function.Consumer;
import java.util.function.BiConsumer;
import java.util.function.BiFunction;

import javax.swing.JTable;

public record SearchKeyData(JTable table, Consumer<String> setSelection)
import de.tinycodecrank.monads.opt.Opt;

public record SearchKeyData(JTable table, BiConsumer<String, BiFunction<String, String, Opt<Integer>>> setSelection)
{}
12 changes: 12 additions & 0 deletions src/de/tinycodecrank/l4j/util/StringUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,16 @@ public static Opt<Integer> camelDistance(String search, String target)
}
return Opt.of(difference + target.length());
}

public static Opt<Integer> matchOffset(String search, String target)
{
if (search.length() > target.length())
return Opt.empty();

final int index = target.indexOf(search);
if (index == -1)
return Opt.empty();

return Opt.of(target.substring(index + search.length()).length());
}
}
1 change: 0 additions & 1 deletion src/module-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
requires transitive java.desktop;
requires java.xml;
requires de.tinycodecrank.mathUtils;
requires org.apache.commons.text;
requires de.tinycodecrank.JavaLexer;
requires com.google.gson;
requires de.tinycodecrank.Functional;
Expand Down

0 comments on commit 1062ca6

Please sign in to comment.