Skip to content

Commit ce4b087

Browse files
committed
update file search related popover to store root path changes
1 parent 4b056ea commit ce4b087

2 files changed

Lines changed: 39 additions & 23 deletions

File tree

src/main/java/gr/sqlbrowserfx/nodes/FileSearchPopOver.java

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ public interface Action {
3636
private final ListView<String> filesListView;
3737
private String rootPath = ((String) PropertiesLoader.getProperty("sqlbrowserfx.root.path", String.class, "~/"))
3838
.replaceAll("\"", "");
39+
private Label descLabel;
3940

4041
public FileSearchPopOver(Action action) {
4142
super();
@@ -88,32 +89,41 @@ protected void updateItem(String item, boolean empty) {
8889
// TODO: add open button if has any value
8990
ImageView descIcon = JavaFXUtils.createIcon("/icons/settings.png");
9091

91-
Label descLabel = new Label("File Search in: " + rootPath, descIcon);
92+
descLabel = new Label("File Search in: " + rootPath, descIcon);
9293
descLabel.setOnMouseClicked(event -> {
9394
this.hide();
94-
DirectoryChooser dirChooser = new DirectoryChooser();
95-
File selectedDir = dirChooser.showDialog(this.getOwnerWindow());
96-
this.rootPath = selectedDir.getAbsolutePath();
97-
descLabel.setText("File Search in: " + rootPath);
95+
var dirChooser = new DirectoryChooser();
96+
var initialDir = new File(this.rootPath);
97+
dirChooser.setInitialDirectory(initialDir);
98+
var selectedDir = dirChooser.showDialog(this.getOwnerWindow());
99+
if (selectedDir != null) {
100+
this.rootPath = selectedDir.getAbsolutePath();
101+
descLabel.setText("File Search in: " + rootPath);
102+
PropertiesLoader.storeProperty("./sqlbrowserfx.properties", "sqlbrowserfx.root.path", this.rootPath);
103+
}
98104
});
99105
descLabel.setTooltip(new Tooltip("Click to change root path"));
100106

101107
this.setContentNode(new CustomVBox(descLabel, searchField, filesListView));
108+
109+
this.setOnShowing(event -> {
110+
PropertiesLoader.loadProperties();
111+
rootPath = ((String) PropertiesLoader.getProperty("sqlbrowserfx.root.path", String.class, "~/"))
112+
.replaceAll("\"", "");
113+
this.descLabel.setText("File Search in: " + rootPath);
114+
});
115+
this.setOnShown(event -> searchField.requestFocus());
116+
this.setHideOnEscape(true);
102117
this.setOnHidden(event -> {
103118
if (executor != null) {
104119
executor.shutdownNow();
105120
}
106121
});
107-
108-
this.setOnShown(event -> searchField.requestFocus());
109-
this.setHideOnEscape(true);
110122

111123
filesListView.setOnKeyPressed(keyEvent -> {
112124
if (keyEvent.getCode() == KeyCode.ENTER) {
113125
String filePath = filesListView.getSelectionModel().getSelectedItem();
114126
action.run(new File(filePath));
115-
} else if (keyEvent.getCode() == KeyCode.ESCAPE) {
116-
this.hide();
117127
}
118128
});
119129
filesListView.setOnMouseClicked(mouseEvent -> {

src/main/java/gr/sqlbrowserfx/nodes/SearchInFilesPopOver.java

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,7 @@
4848

4949
public class SearchInFilesPopOver extends CustomPopOver {
5050

51-
private String rootPath = ((String) PropertiesLoader.getProperty("sqlbrowserfx.root.path", String.class, "~/"))
52-
.replaceAll("\"", "");
51+
private String rootPath = "~/";
5352

5453
private CodeArea codeArea = new CodeArea();
5554
TextField searchField;
@@ -62,9 +61,10 @@ public class SearchInFilesPopOver extends CustomPopOver {
6261

6362
private ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();
6463

64+
private Label descLabel;
65+
6566

6667
public SearchInFilesPopOver() {
67-
6868
var fileSearchBox = this.createFileSearchBox();
6969
var linesListBox = this.createLinesListBox();
7070
var hSplit = new SplitPane(fileSearchBox, new CustomVBox(new Label("Lines Matches"),linesListBox));
@@ -78,13 +78,18 @@ public SearchInFilesPopOver() {
7878
var borderPane = new BorderPane(vSplit);
7979
this.setContentNode(borderPane);
8080
this.setPrefSize(1000, 800);
81-
// Add ESC key handler to close the stage
82-
borderPane.setOnKeyPressed(event -> {
83-
if (event.getCode() == KeyCode.ESCAPE) {
84-
PropertiesLoader.storeProperty("./sqlbrowserfx.properties", "sqlbrowserfx.root.path", this.rootPath);
85-
this.hide();
86-
}
87-
});
81+
this.setHideOnEscape(true);
82+
this.setOnShowing(event -> {
83+
PropertiesLoader.loadProperties();
84+
rootPath = ((String) PropertiesLoader.getProperty("sqlbrowserfx.root.path", String.class, "~/"))
85+
.replaceAll("\"", "");
86+
this.descLabel.setText("File Search in: " + rootPath);
87+
});
88+
this.setOnHidden(event -> {
89+
if (executor != null) {
90+
executor.shutdownNow();
91+
}
92+
});
8893
}
8994

9095

@@ -142,17 +147,18 @@ private VBox createFileSearchBox() {
142147
caseInsensitiveCheckBox.setTooltip(new Tooltip("Case Insensitive"));
143148
caseInsensitiveCheckBox.setFocusTraversable(false);
144149

145-
var descLabel = new Label("File Search in: " + rootPath);
150+
descLabel = new Label("File Search in: " + rootPath);
146151

147152
var settingsButton = new Button("", JavaFXUtils.createIcon("/icons/settings.png"));
148153
settingsButton.setOnMouseClicked(event -> {
149154
var dirChooser = new DirectoryChooser();
150-
File initialDir = new File(this.rootPath);
155+
var initialDir = new File(this.rootPath);
151156
dirChooser.setInitialDirectory(initialDir);
152-
var selectedDir = dirChooser.showDialog(vbox.getScene().getWindow());
157+
var selectedDir = dirChooser.showDialog(this.getOwnerWindow());
153158
if (selectedDir != null) {
154159
this.rootPath = selectedDir.getAbsolutePath();
155160
descLabel.setText("File Search in: " + rootPath);
161+
PropertiesLoader.storeProperty("./sqlbrowserfx.properties", "sqlbrowserfx.root.path", this.rootPath);
156162
}
157163
});
158164
settingsButton.setTooltip(new Tooltip("Click to change root path"));

0 commit comments

Comments
 (0)