Skip to content

Commit 1911428

Browse files
committed
1、添加自动刷新索引功能;2、优化隐藏文件图标显示
1 parent bd5fc00 commit 1911428

4 files changed

Lines changed: 323 additions & 142 deletions

File tree

src/main/java/com/xwintop/xJavaFxTool/controller/littleTools/FileSearchToolController.java

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,32 @@
11
package com.xwintop.xJavaFxTool.controller.littleTools;
22

3+
import cn.hutool.core.swing.clipboard.ClipboardUtil;
34
import com.xwintop.xJavaFxTool.services.littleTools.FileSearchToolService;
5+
import com.xwintop.xJavaFxTool.utils.ImgToolUtil;
46
import com.xwintop.xJavaFxTool.utils.JavaFxViewUtil;
7+
import com.xwintop.xJavaFxTool.utils.XJavaFxSystemUtil;
58
import com.xwintop.xJavaFxTool.view.littleTools.FileSearchToolView;
69
import com.xwintop.xcore.util.javafx.FileChooserUtil;
710
import javafx.application.Platform;
811
import javafx.collections.FXCollections;
912
import javafx.collections.ObservableList;
1013
import javafx.embed.swing.SwingFXUtils;
1114
import javafx.event.ActionEvent;
15+
import javafx.event.Event;
1216
import javafx.fxml.FXML;
17+
import javafx.scene.control.ContextMenu;
18+
import javafx.scene.control.MenuItem;
1319
import javafx.scene.control.TableCell;
1420
import javafx.scene.control.TableColumn;
1521
import javafx.scene.image.Image;
1622
import javafx.scene.image.ImageView;
23+
import javafx.scene.input.MouseButton;
24+
import javafx.scene.paint.Color;
1725
import javafx.util.Callback;
1826
import lombok.Getter;
1927
import lombok.Setter;
2028
import lombok.extern.slf4j.Slf4j;
29+
import org.apache.commons.lang3.StringUtils;
2130

2231
import javax.swing.*;
2332
import javax.swing.filechooser.FileSystemView;
@@ -73,6 +82,12 @@ protected void updateItem(String item, boolean empty) {
7382
}
7483
ImageIcon icon = (ImageIcon) FileSystemView.getFileSystemView().getSystemIcon(file);
7584
Image fxImage = SwingFXUtils.toFXImage((BufferedImage) icon.getImage(), null);
85+
if (file.isHidden()) {
86+
this.setTextFill(Color.GREY);
87+
fxImage = ImgToolUtil.pixWithImage(8, fxImage);
88+
} else {
89+
this.setTextFill(Color.BLACK);
90+
}
7691
ImageView imageView = new ImageView(fxImage);
7792
this.setGraphic(imageView);
7893
} catch (Exception e) {
@@ -92,14 +107,54 @@ protected void updateItem(String item, boolean empty) {
92107
JavaFxViewUtil.setTableColumnMapValueFactory(lastModifiedTableColumn, "lastModified", false);
93108
searchResultTableVIew.setItems(searchResultTableData);
94109

95-
searchDirectoryTextField.setText("D:\\TestXf\\");
110+
// searchDirectoryTextField.setText("D:\\TestXf\\");
111+
searchDirectoryTextField.setText(StringUtils.removeEnd(new File("./").getAbsolutePath(), "."));
96112
}
97113

98114
private void initEvent() {
99115
FileChooserUtil.setOnDrag(searchDirectoryTextField, FileChooserUtil.FileType.FOLDER);
116+
117+
searchResultTableVIew.setOnMouseClicked(event -> {
118+
if (event.getButton() == MouseButton.SECONDARY) {
119+
MenuItem menuOpen = new MenuItem("打开");
120+
menuOpen.setOnAction(event1 -> {
121+
String absolutePath = searchResultTableVIew.getSelectionModel().getSelectedItem().get("absolutePath");
122+
XJavaFxSystemUtil.openDirectory(absolutePath);
123+
});
124+
MenuItem menuOpenPath = new MenuItem("打开路径");
125+
menuOpenPath.setOnAction(event1 -> {
126+
String absolutePath = searchResultTableVIew.getSelectionModel().getSelectedItem().get("absolutePath");
127+
File file = new File(absolutePath);
128+
if (!file.isDirectory()) {
129+
absolutePath = file.getParent();
130+
}
131+
XJavaFxSystemUtil.openDirectory(absolutePath);
132+
});
133+
MenuItem menuCopyFileName = new MenuItem("复制文件名");
134+
menuCopyFileName.setOnAction(event1 -> {
135+
ClipboardUtil.setStr(searchResultTableVIew.getSelectionModel().getSelectedItem().get("fileName"));
136+
});
137+
MenuItem menuCopyFilePath = new MenuItem("复制完整路径");
138+
menuCopyFilePath.setOnAction(event1 -> {
139+
ClipboardUtil.setStr(searchResultTableVIew.getSelectionModel().getSelectedItem().get("absolutePath"));
140+
});
141+
searchResultTableVIew.setContextMenu(new ContextMenu(menuOpen, menuOpenPath, menuCopyFileName, menuCopyFilePath));
142+
}
143+
});
100144
}
101145

102146
private void initService() {
147+
if (autoRefreshIndexCheckBox.isSelected()) {
148+
fileSearchToolService.autoRefreshIndexAction();
149+
}
150+
}
151+
152+
public void autoRefreshIndexAction() throws Exception {
153+
if (autoRefreshIndexCheckBox.isSelected()) {
154+
fileSearchToolService.autoRefreshIndexAction();
155+
} else {
156+
fileSearchToolService.stopAutoRefreshIndexTimer();
157+
}
103158
}
104159

105160
public void searchContentAction() throws Exception {
@@ -118,4 +173,11 @@ private void searchDirectoryAction(ActionEvent event) throws Exception {
118173
searchDirectoryTextField.setText(file.getPath());
119174
}
120175
}
176+
177+
/**
178+
* 父控件被移除前调用
179+
*/
180+
public void onCloseRequest(Event event) {
181+
fileSearchToolService.stopAutoRefreshIndexTimer();
182+
}
121183
}

src/main/java/com/xwintop/xJavaFxTool/services/littleTools/FileSearchToolService.java

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,7 @@
2323
import java.nio.file.Files;
2424
import java.nio.file.Path;
2525
import java.nio.file.Paths;
26-
import java.util.Date;
27-
import java.util.HashMap;
28-
import java.util.Iterator;
29-
import java.util.Map;
26+
import java.util.*;
3027

3128
/**
3229
* @ClassName: FileSearchToolService
@@ -40,7 +37,7 @@
4037
@Slf4j
4138
public class FileSearchToolService {
4239
private FileSearchToolController fileSearchToolController;
43-
40+
private static Timer autoRefreshIndexTimer = null;
4441
private static final String searchIndexDir = ConfigureUtil.getConfigurePath("searchIndexDir/");
4542

4643
private static Directory directory;
@@ -234,10 +231,25 @@ public void addIndexDocument(File file) throws Exception {
234231
}
235232

236233
public void autoRefreshIndexAction() {
237-
File[] listRoots = File.listRoots();
238-
for (File listRoot : listRoots) {
239-
System.out.println("加载目录: " + listRoot.getAbsolutePath());
240-
addSearchIndexFile(listRoot.toPath());
234+
if (autoRefreshIndexTimer == null) {
235+
autoRefreshIndexTimer = new Timer();
236+
autoRefreshIndexTimer.schedule(new TimerTask() {
237+
@Override
238+
public void run() {
239+
File[] listRoots = File.listRoots();
240+
for (File listRoot : listRoots) {
241+
System.out.println("加载目录: " + listRoot.getAbsolutePath());
242+
addSearchIndexFile(listRoot.toPath());
243+
}
244+
}
245+
}, 5000, 600000);
246+
}
247+
}
248+
249+
public void stopAutoRefreshIndexTimer() {
250+
if (autoRefreshIndexTimer != null) {
251+
autoRefreshIndexTimer.cancel();
252+
autoRefreshIndexTimer = null;
241253
}
242254
}
243255

0 commit comments

Comments
 (0)