Skip to content

Commit

Permalink
update: chapter search
Browse files Browse the repository at this point in the history
  • Loading branch information
muedsa committed Jun 4, 2021
1 parent 08e9cd7 commit 3ae61d4
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 14 deletions.
7 changes: 6 additions & 1 deletion resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<idea-plugin>
<id>com.muedsa.intellij.textReader</id>
<name>TextReaderSiderTool for intellij</name>
<version>2021.06.01</version>
<version>2021.06.04</version>
<vendor email="[email protected]" url="http://www.muedsa.com">MUEDSA</vendor>

<description><![CDATA[
Expand Down Expand Up @@ -44,6 +44,11 @@
<ul>
<li>Setting: modify paragraph space</li>
</ul>
<h3>2021.06.04<h3>
<ul>
<li>chapter search</li>
</ul>
<hr/>
]]>
</change-notes>
Expand Down
25 changes: 19 additions & 6 deletions src/com/muedsa/intellij/textReader/composes/ReaderWindow.form
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
<properties/>
<border type="none"/>
<children>
<grid id="71434" layout-manager="GridLayoutManager" row-count="1" column-count="3" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<grid id="71434" layout-manager="GridLayoutManager" row-count="1" column-count="4" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
Expand All @@ -41,19 +41,30 @@
<text value="file"/>
</properties>
</component>
<hspacer id="6f9dd">
<constraints>
<grid row="0" column="2" row-span="1" col-span="1" vsize-policy="1" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
</hspacer>
<component id="d288a" class="javax.swing.JButton" binding="clearButton">
<constraints>
<grid row="0" column="2" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
<grid row="0" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Clear"/>
</properties>
</component>
<hspacer id="6f9dd">
<component id="be37b" class="javax.swing.JTextField" binding="searchTextField">
<constraints>
<grid row="0" column="1" row-span="1" col-span="1" vsize-policy="1" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
<grid row="0" column="3" row-span="1" col-span="1" vsize-policy="0" hsize-policy="6" anchor="8" fill="1" indent="0" use-parent-layout="false">
<preferred-size width="150" height="-1"/>
</grid>
</constraints>
</hspacer>
<properties>
<enabled value="false"/>
<text value=""/>
</properties>
</component>
</children>
</grid>
<scrollpane id="7cd85">
Expand All @@ -65,7 +76,9 @@
<children>
<component id="81e74" class="com.intellij.ui.components.JBList" binding="titleList">
<constraints/>
<properties/>
<properties>
<selectionMode value="0"/>
</properties>
</component>
</children>
</scrollpane>
Expand Down
82 changes: 75 additions & 7 deletions src/com/muedsa/intellij/textReader/composes/ReaderWindow.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.intellij.openapi.project.Project;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.openapi.wm.ToolWindow;
import com.intellij.ui.DocumentAdapter;
import com.intellij.ui.components.JBList;
import com.muedsa.intellij.textReader.Chapter;
import com.muedsa.intellij.textReader.TextFile;
Expand All @@ -14,16 +15,22 @@
import com.muedsa.intellij.textReader.state.TextReaderStateService;
import com.muedsa.intellij.textReader.util.ChapterUtil;
import org.apache.commons.lang3.StringUtils;
import org.jetbrains.annotations.NotNull;

import javax.swing.*;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.text.*;
import java.awt.*;
import java.awt.event.*;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Vector;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;
import java.util.stream.Collectors;

public class ReaderWindow {
private JPanel readerPanel;
Expand All @@ -45,6 +52,7 @@ public class ReaderWindow {
private JComboBox<String> fontFamilyEl;
private JButton clearButton;
private JSpinner paragraphSpaceSpinner;
private JTextField searchTextField;

private Project project;
private ToolWindow toolWindow;
Expand All @@ -53,6 +61,8 @@ public class ReaderWindow {

private TextReaderStateService textReaderStateService;

private static Vector<Chapter> CHAPTER_LIST = new Vector<>(0);

public ReaderWindow(Project project, ToolWindow toolWindow) {
this.project = project;
this.toolWindow = toolWindow;
Expand Down Expand Up @@ -109,10 +119,11 @@ private void createUIComponents(){
updateRegex();
Pattern pattern = Pattern.compile(regexStringEl.getText().trim());
textFile = new TextFile(file);
Vector<Chapter> list = ChapterUtil.getChapters(textFile, (int)maxLineSizeSpinner.getValue(), pattern);
titleList.setListData(list);
CHAPTER_LIST = ChapterUtil.getChapters(textFile, (int)maxLineSizeSpinner.getValue(), pattern);
titleList.setListData(CHAPTER_LIST);
textReaderStateService.setFilePath(textFile.getFilePath());
textReaderStateService.setChapters(list);
textReaderStateService.setChapters(CHAPTER_LIST);
searchTextField.setEnabled(true);
}
catch (PatternSyntaxException error){
error.printStackTrace();
Expand All @@ -132,12 +143,67 @@ private void createUIComponents(){
//清除
clearButton.addActionListener(e -> {
textFile = null;
Vector<Chapter> list = new Vector<>(0);
titleList.setListData(list);
CHAPTER_LIST = new Vector<>(0);
titleList.setListData(CHAPTER_LIST);
textContent.setText("");
textContent.setCaretPosition(0);
textReaderStateService.setFilePath("");
textReaderStateService.setChapters(list);
textReaderStateService.setChapters(CHAPTER_LIST);
searchTextField.setEnabled(false);
});


searchTextField.getDocument().addDocumentListener(new DocumentAdapter() {

private List<searchThread> threadList = new ArrayList<>();

private void search(String searchText){
if(StringUtils.isEmpty(searchText)){
if(titleList.getModel().getSize() != CHAPTER_LIST.size()){
titleList.setListData(CHAPTER_LIST);
}
}else{
Vector<Chapter> tempChapterList = CHAPTER_LIST.stream().filter(c -> c.getTitle().contains(searchText)).collect(Collectors.toCollection(Vector::new));
if(tempChapterList.size() != CHAPTER_LIST.size()){
titleList.setListData(tempChapterList);
}
}
}

@Override
protected void textChanged(@NotNull DocumentEvent documentEvent){
threadList.forEach(searchThread::cancel);
String searchText = searchTextField.getText();
searchThread thread = new searchThread(searchText);
thread.start();
threadList.add(thread);
}

class searchThread extends Thread{
private boolean cancel = false;
private String searchText;

public searchThread(String searchText){
super();
this.searchText = searchText;
}

@Override
public void run(){
try{
Thread.sleep(300);
if(!cancel){
search(searchText);
}
threadList.remove(this);
} catch(InterruptedException e){
e.printStackTrace();
}
}
public void cancel(){
cancel = true;
}
}
});

//点击章节
Expand Down Expand Up @@ -186,7 +252,9 @@ private void init(){
String filePath = textReaderStateService.getFilePath();
if(StringUtils.isNotBlank(filePath)){
textFile = new TextFile(filePath);
titleList.setListData(textReaderStateService.getChapters());
CHAPTER_LIST = textReaderStateService.getChapters();
titleList.setListData(CHAPTER_LIST);
searchTextField.setEnabled(true);
}
}
catch (IOException error){
Expand Down

0 comments on commit 3ae61d4

Please sign in to comment.