Skip to content

Commit

Permalink
Adding replacement
Browse files Browse the repository at this point in the history
  • Loading branch information
marigostra committed Nov 4, 2021
1 parent 066a749 commit 940eb85
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 65 deletions.
29 changes: 7 additions & 22 deletions src/main/java/org/luwrain/app/notepad/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,13 @@ public App(String arg)
super(Strings.NAME, Strings.class, "luwrain.notepad");
this.arg = arg;
this.corrector = new EditUtils.ActiveCorrector();
setTabProcessing(false);
}

@Override protected AreaLayout onAppInit() throws IOException
{
this.sett = Settings.create(getLuwrain().getRegistry());
this.conv = new Conversations(getLuwrain(), getStrings());
this.conv = new Conversations(this);
this.hooks = new Hooks(this);
this.mainLayout = new MainLayout(this);
this.narratingLayout = new NarratingLayout(this, ()->{});
Expand Down Expand Up @@ -107,7 +108,7 @@ boolean onSave()
setAppName(file.getName());
}
try {
save(mainLayout.getText());
save(mainLayout.editArea.getText());
}
catch(IOException e)
{
Expand Down Expand Up @@ -220,21 +221,6 @@ void save(String[] lines) throws IOException
org.luwrain.util.FileUtils.writeTextFileMultipleStrings(file, lines, charset, lineSeparator);
}

Conversations getConv()
{
return this.conv;
}

Hooks getHooks()
{
return this.hooks;
}

Settings getSett()
{
return this.sett;
}

@Override public boolean isBusy()
{
return narratingTask != null && !narratingTask.isDone();
Expand All @@ -258,9 +244,8 @@ Settings getSett()
super.closeApp();
}

@Override public void setAppName(String newName)
{
NullCheck.notEmpty(newName, "newName");
super.setAppName(newName);
}
Conversations getConv() { return this.conv; }
Hooks getHooks() { return this.hooks; }
Settings getSett() { return this.sett; }

}
24 changes: 19 additions & 5 deletions src/main/java/org/luwrain/app/notepad/Conversations.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,15 @@ enum UnsavedChangesRes {CONTINUE_SAVE, CONTINUE_UNSAVED, CANCEL};
private final Luwrain luwrain;
private final Strings strings;

Conversations(Luwrain luwrain, Strings strings)
private final Set<String>
replaceExpHistory = new HashSet<>(),
replaceWithHistory = new HashSet<>();

Conversations(App app)
{
NullCheck.notNull(luwrain, "luwrain");
NullCheck.notNull(strings, "strings");
this.luwrain = luwrain;
this.strings = strings;
NullCheck.notNull(app, "app");
this.luwrain = app.getLuwrain();
this.strings = app.getStrings();
}

//currentFile may be null
Expand Down Expand Up @@ -88,4 +91,15 @@ UnsavedChangesRes unsavedChanges()
return UnsavedChangesRes.CANCEL;
return popup.result()?UnsavedChangesRes.CONTINUE_SAVE:UnsavedChangesRes.CONTINUE_UNSAVED;
}

String replaceExp()
{
return Popups.editWithHistory(luwrain, strings.replacePopupName(), strings.replaceExpPopupPrefix(), "", replaceExpHistory);
}

String replaceWith()
{
return Popups.editWithHistory(luwrain, strings.replacePopupName(), strings.replaceWithPopupPrefix(), "", replaceWithHistory);
}

}
85 changes: 48 additions & 37 deletions src/main/java/org/luwrain/app/notepad/MainLayout.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,29 +37,29 @@ final class MainLayout extends LayoutBase
super(app);
this.app = app;
this.editArea = new EditArea(editParams((params)->{
params.name = "";
params.appearance = new Appearance(params.context){
@Override App.Mode getMode() { return app.mode; }
};
params.changeListener = ()->{app.modified = true;};
params.editFactory = (p)->{
app.corrector.setDefaultCorrector((MultilineEditCorrector)p.model);
p.model = app.corrector;
return new MultilineEdit(p);
};
params.name = "";
params.appearance = new Appearance(params.context){
@Override App.Mode getMode() { return app.mode; }
};
params.changeListener = ()->{app.modified = true;};
params.editFactory = (p)->{
app.corrector.setDefaultCorrector((MultilineEditCorrector)p.model);
p.model = app.corrector;
return new MultilineEdit(p);
};
})){
@Override public boolean onSystemEvent(SystemEvent event)
{
NullCheck.notNull(event, "event");
if (event.getType() == SystemEvent.Type.REGULAR)
switch(event.getCode())
{
case SAVE:
app.onSave();
return true;
case PROPERTIES:
return showProperties();
}
if (event.getType() == SystemEvent.Type.REGULAR)
switch(event.getCode())
{
case SAVE:
app.onSave();
return true;
case PROPERTIES:
return showProperties();
}
return super.onSystemEvent(event);
}
@Override public boolean onAreaQuery(AreaQuery query)
Expand All @@ -77,16 +77,32 @@ final class MainLayout extends LayoutBase
}
};
setAreaLayout(editArea, actions(
action("open", app.getStrings().actionOpen(), new InputEvent(InputEvent.Special.F3, EnumSet.of(InputEvent.Modifiers.SHIFT)), MainLayout.this::actOpen),
action("save-as", app.getStrings().actionSaveAs(), new InputEvent(InputEvent.Special.F2, EnumSet.of(InputEvent.Modifiers.SHIFT)), MainLayout.this::actSaveAs),
action("charset", app.getStrings().actionCharset(), new InputEvent(InputEvent.Special.F9), MainLayout .this::actCharset),
action("narrating", app.getStrings().actionNarrating(), new InputEvent(InputEvent.Special.F10), MainLayout.this::actNarrating),
action("mode-none", app.getStrings().modeNone(), new InputEvent(InputEvent.Special.F1, EnumSet.of(InputEvent.Modifiers.ALT)), MainLayout.this::actModeNone),
action("mode-natural", app.getStrings().modeNatural(), new InputEvent(InputEvent.Special.F2, EnumSet.of(InputEvent.Modifiers.ALT)), MainLayout.this::actModeNatural),
action("mode-programming", app.getStrings().modeProgramming(), new InputEvent(InputEvent.Special.F3, EnumSet.of(InputEvent.Modifiers.ALT)), MainLayout.this::actModeProgramming)
action("replace", app.getStrings().actionReplace(), new InputEvent(InputEvent.Special.F5), this::actReplace),
action("charset", app.getStrings().actionCharset(), new InputEvent(InputEvent.Special.F9), MainLayout .this::actCharset),
action("narrating", app.getStrings().actionNarrating(), new InputEvent(InputEvent.Special.F10), MainLayout.this::actNarrating),
action("open", app.getStrings().actionOpen(), new InputEvent(InputEvent.Special.F3, EnumSet.of(InputEvent.Modifiers.SHIFT)), MainLayout.this::actOpen),
action("save-as", app.getStrings().actionSaveAs(), new InputEvent(InputEvent.Special.F2, EnumSet.of(InputEvent.Modifiers.SHIFT)), MainLayout.this::actSaveAs),
action("mode-none", app.getStrings().modeNone(), new InputEvent(InputEvent.Special.F1, EnumSet.of(InputEvent.Modifiers.ALT)), MainLayout.this::actModeNone),
action("mode-natural", app.getStrings().modeNatural(), new InputEvent(InputEvent.Special.F2, EnumSet.of(InputEvent.Modifiers.ALT)), MainLayout.this::actModeNatural),
action("mode-programming", app.getStrings().modeProgramming(), new InputEvent(InputEvent.Special.F3, EnumSet.of(InputEvent.Modifiers.ALT)), MainLayout.this::actModeProgramming)
));
}

private boolean actReplace()
{
final String oldValue = app.getConv().replaceExp();
if (oldValue == null || oldValue.isEmpty())
return true;
final String newValue = app.getConv().replaceWith();
if (newValue == null)
return true;
editArea.getContent().update((lines)->{
for(int i = 0;i < lines.getLineCount();i++)
lines.setLine(i, lines.getLine(i).replaceAll(oldValue, newValue));
});
return true;
}

private boolean onDirectoryQuery(CurrentDirQuery query)
{
NullCheck.notNull(query, "query");
Expand All @@ -103,7 +119,7 @@ private boolean actOpen()
{
if (!app.everythingSaved())
return true;
final File file = app.getConv().open();
final File file = app.getConv().open();
if (file == null)
return true;
//To restore on failed reading
Expand Down Expand Up @@ -133,7 +149,7 @@ private boolean actSaveAs()
app.file = f;
onNewFile();
try {
app.save(getText());
app.save(editArea.getText());
}
catch(IOException e)
{
Expand All @@ -147,7 +163,7 @@ private boolean actSaveAs()

private boolean actCharset()
{
if (!app.everythingSaved())
if (!app.everythingSaved())
return true;
final String res = app.getConv().charset();
if (res == null)
Expand All @@ -164,7 +180,7 @@ private boolean actCharset()
return true;
}
}
return true;
return true;
}

private boolean actNarrating()
Expand Down Expand Up @@ -207,17 +223,12 @@ private boolean actModeProgramming()
return true;
}

String[] getText()
{
return editArea.getText();
}

void setText(String[] text)
{
NullCheck.notNullItems(text, "text");
editArea.getContent().setLines(text);
app.getLuwrain().onAreaNewContent(editArea);
}
editArea.refresh();
}

void onNewFile()
{
Expand Down
8 changes: 7 additions & 1 deletion src/main/java/org/luwrain/app/notepad/Strings.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public interface Strings
String actionSaveAs();
String appName();
String cancelNarratingBeforeClosing();
String charsetPopupPrefix();

String enteredPathMayNotBeDir(String fileName);
String fileIsSaved();
String initialTitle();
Expand Down Expand Up @@ -62,4 +62,10 @@ public interface Strings
String settingsFormNarratingPauseDuration();
String settingsFormNarratingSpeechPitch();
String settingsFormNarratingSpeechRate();


String charsetPopupPrefix();
String replacePopupName();
String replaceExpPopupPrefix();
String replaceWithPopupPrefix();
}

0 comments on commit 940eb85

Please sign in to comment.