forked from janbodnar/Java-Advanced
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSearchingImagesEx.java
More file actions
181 lines (132 loc) · 4.49 KB
/
SearchingImagesEx.java
File metadata and controls
181 lines (132 loc) · 4.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package com.zetcode;
import javax.swing.AbstractAction;
import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.SwingWorker;
import java.awt.Container;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.io.IOException;
import java.nio.file.FileSystems;
import java.nio.file.FileVisitResult;
import java.nio.file.FileVisitor;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.PathMatcher;
import java.nio.file.Paths;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.List;
import static javax.swing.GroupLayout.Alignment.BASELINE;
import static javax.swing.GroupLayout.Alignment.CENTER;
/*
* This program initiates a background search for
* various image files in a user's home directory.
*/
public class SearchingImagesEx extends JFrame {
private JTextArea area;
private JLabel lbl;
private JButton btn;
public SearchingImagesEx() {
initUI();
}
private void initUI() {
area = new JTextArea(20, 40);
area.setEditable(false);
var scrollPane = new JScrollPane(area);
btn = new JButton("Start");
btn.addActionListener(new StartSearchAction());
lbl = new JLabel("Files found: ");
createLayout(scrollPane, btn, lbl);
setTitle("Searching for files");
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
private void createLayout(JComponent... arg) {
Container pane = getContentPane();
GroupLayout gl = new GroupLayout(pane);
pane.setLayout(gl);
gl.setAutoCreateContainerGaps(true);
gl.setAutoCreateGaps(true);
gl.setHorizontalGroup(gl.createParallelGroup(CENTER)
.addComponent(arg[0])
.addGroup(gl.createSequentialGroup()
.addComponent(arg[1])
.addComponent(arg[2]))
);
gl.setVerticalGroup(gl.createSequentialGroup()
.addComponent(arg[0])
.addGroup(gl.createParallelGroup(BASELINE)
.addComponent(arg[1])
.addComponent(arg[2]))
);
pack();
}
private class StartSearchAction extends AbstractAction {
@Override
public void actionPerformed(ActionEvent e) {
doStartSearch();
}
private void doStartSearch() {
btn.setEnabled(false);
var tw = new FileTreeWalker();
tw.execute();
}
}
public static void main(String[] args) {
EventQueue.invokeLater(() -> {
var ex = new SearchingImagesEx();
ex.setVisible(true);
});
}
private class FileTreeWalker extends SwingWorker<Void, Path>
implements FileVisitor<Path> {
private final PathMatcher matcher;
private int count = 0;
public FileTreeWalker() {
var str = "glob:**.{png,PNG,gif,GIF,jpg,jpeg,JPG,JPEG}";
matcher = FileSystems.getDefault().getPathMatcher(str);
}
@Override
protected Void doInBackground() throws IOException {
Path path = Paths.get(System.getProperty("user.home"));
Files.walkFileTree(path, this);
return null;
}
@Override
protected void process(List<Path> chunks) {
for (Path path : chunks) {
area.append(path.toString() + "\n");
count++;
lbl.setText(String.format("Files found: %d", count));
}
}
@Override
protected void done() {
btn.setEnabled(true);
}
@Override
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs){
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) {
if (matcher.matches(file)) {
publish(file);
}
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult visitFileFailed(Path file, IOException exc) {
return FileVisitResult.TERMINATE;
}
@Override
public FileVisitResult postVisitDirectory(Path dir, IOException exc) {
return FileVisitResult.CONTINUE;
}
}
}