-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspotlightSearch.java
More file actions
133 lines (113 loc) · 4.73 KB
/
spotlightSearch.java
File metadata and controls
133 lines (113 loc) · 4.73 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
import javax.swing.event.*;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.AttributeSet.ColorAttribute;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.IOException;
import javax.swing.SwingUtilities;
import java.nio.channels.SelectableChannel;
import java.io.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
public class spotlightSearch extends JFrame
{
static String chosenQuery;
JTextField[] results;
static JPanel centerPanel;
static Box vBox;
public spotlightSearch()
{
this.setSize(1000, 800);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLocationRelativeTo(null);
this.setResizable(true);
// Caps total # of results at 200
results = new JTextField[200];
JTextField searchBox = new JTextField();
// Set a larger font for the search box
searchBox.setFont(new Font("Arial", Font.PLAIN, 45));
searchBox.setText("");
searchBox.setPreferredSize(new Dimension(700, 80));
searchBox.setEditable(true);
// Initialize layout and components
vBox = Box.createVerticalBox();
centerPanel = new JPanel();
centerPanel.setLayout(new BorderLayout());
// Add vBox to a scrollable panel
JScrollPane scrollPane = new JScrollPane(centerPanel, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
scrollPane.getHorizontalScrollBar().setUnitIncrement(16); // Adjust scrolling speed
scrollPane.getVerticalScrollBar().setUnitIncrement(16);
JPanel contentPanel = (JPanel) getContentPane();
contentPanel.setLayout(new BorderLayout());
contentPanel.add(searchBox, BorderLayout.SOUTH);
contentPanel.add(scrollPane, BorderLayout.CENTER);
centerPanel.add(vBox, BorderLayout.NORTH); // Add vBox to the center panel
pack();
// Init vertical box layout scaling
searchBox.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
// Execute fd with text in searchBox:
try
{
ProcessBuilder builder = new ProcessBuilder("bin/fd", searchBox.getText(), "/");
builder.inheritIO().redirectOutput(ProcessBuilder.Redirect.PIPE);
Process p;
p = builder.start();
try (BufferedReader buf = new BufferedReader(new InputStreamReader(p.getInputStream()))) {
String line;
int count = 0;
while ((line = buf.readLine()) != null)
{
count++;
if (count == 200) {break;}
// Create result JTextField with larger font
results[count] = new JTextField(line);
results[count].setFont(new Font("Arial", Font.PLAIN, 45)); // Font size 45
results[count].setEditable(false); // Make result text non-editable
vBox.add(results[count]);
vBox.revalidate(); // Refresh layout of vBox
vBox.repaint(); // Repaint vBox
// Repack the entire window to adjust its size automatically
SwingUtilities.getWindowAncestor(vBox).pack();
}
} catch (Exception e1)
{
System.out.println("[1] Sorry something went wrong. ");
e1.printStackTrace();
}
p.waitFor();
} catch (Exception e2)
{
System.out.println("[2] Sorry something went wrong. ");
e2.printStackTrace();
}
}
});
}
// Provided user clicks a returned result, will open directory of their selection
public static void openDirectory(String selectedResult)
{
try
{
Process p = Runtime.getRuntime().exec(new String[] { "xdg-open", selectedResult });
} catch (IOException e2)
{
System.out.println("Error executing command. Please try restarting. ");
e2.printStackTrace();
}
}
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable()
{
@Override
public void run()
{
new spotlightSearch().setVisible(true);
}
});
}
}