-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFileChooserExample.java
More file actions
224 lines (194 loc) · 9.3 KB
/
FileChooserExample.java
File metadata and controls
224 lines (194 loc) · 9.3 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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
import javax.swing.*;
import javax.swing.filechooser.FileNameExtensionFilter;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.util.List;
public class FileChooserExample extends JFrame {
private JButton chooseFileButton;
private JLabel fileNameLabel;
private JTextArea inputTextArea;
private JTextArea outputTextArea;
private JComboBox<String> algorithmComboBox;
private JButton compressButton;
private JButton decompressButton;
private StringBuilder fileContent = new StringBuilder();
private String compressedContent;
private List<Triple> compressedLZ77;
private List<Integer> compressedLZW;
public FileChooserExample() {
setTitle("File Chooser Example");
setSize(800, 400); // Adjusted size for better visibility
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationRelativeTo(null);
chooseFileButton = new JButton("Choose File");
fileNameLabel = new JLabel("Selected File: ");
inputTextArea = new JTextArea();
outputTextArea = new JTextArea();
outputTextArea.setLineWrap(true); // Enable line wrapping for outputTextArea
outputTextArea.setWrapStyleWord(true); // Wrap at word boundaries
outputTextArea.setEditable(false);
// ComboBox options
String[] algorithms = {"Choose an algorithm", "Huffman", "LZ77", "LZW"};
algorithmComboBox = new JComboBox<>(algorithms);
compressButton = new JButton("Compress");
compressButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String selectedAlgorithm = (String) algorithmComboBox.getSelectedItem();
if (selectedAlgorithm.equals("Choose an algorithm")) {
JOptionPane.showMessageDialog(null, "Please select an algorithm.");
return;
}
// Call the compress function with the selected algorithm
compress(selectedAlgorithm);
}
});
decompressButton = new JButton("Decompress");
decompressButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String selectedAlgorithm = (String) algorithmComboBox.getSelectedItem();
if (selectedAlgorithm.equals("Choose an algorithm")) {
JOptionPane.showMessageDialog(null, "Please select an algorithm.");
return;
}
// Call the decompress function with the selected algorithm
decompress(selectedAlgorithm);
}
});
chooseFileButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JFileChooser fileChooser = new JFileChooser();
fileChooser.setDialogTitle("Choose a File");
fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
fileChooser.setFileFilter(new FileNameExtensionFilter("Text Files", "txt"));
int returnValue = fileChooser.showOpenDialog(null);
if (returnValue == JFileChooser.APPROVE_OPTION) {
File selectedFile = fileChooser.getSelectedFile();
fileNameLabel.setText("Selected File: " + selectedFile.getName());
// Read file contents and display in the input text area
fileContent.setLength(0); // Clear existing content
String content = readFile(selectedFile);
inputTextArea.setText(content);
}
}
});
JPanel topPanel = new JPanel(new BorderLayout());
topPanel.add(fileNameLabel, BorderLayout.WEST); // Place label on the left
topPanel.add(chooseFileButton, BorderLayout.EAST); // Place button on the right
JPanel inputPanel = new JPanel(new BorderLayout());
inputPanel.add(new JScrollPane(inputTextArea), BorderLayout.CENTER);
inputPanel.setBorder(BorderFactory.createTitledBorder("Input"));
JPanel outputPanel = new JPanel(new BorderLayout());
outputPanel.add(new JScrollPane(outputTextArea), BorderLayout.CENTER);
outputPanel.setBorder(BorderFactory.createTitledBorder("Output"));
// Create bottom panel and sub-panels
JPanel bottomPanel = new JPanel(new BorderLayout());
// Create left panel for "Choose File" button
JPanel leftPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
leftPanel.add(chooseFileButton);
// Create center panel for algorithm combo box
JPanel centerPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
centerPanel.add(algorithmComboBox);
// Create right panel for "Compress" and "Decompress" buttons
JPanel rightPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
rightPanel.add(compressButton);
rightPanel.add(decompressButton);
// Add sub-panels to bottom panel
bottomPanel.add(leftPanel, BorderLayout.WEST);
bottomPanel.add(centerPanel, BorderLayout.CENTER);
bottomPanel.add(rightPanel, BorderLayout.EAST);
JPanel mainPanel = new JPanel(new GridLayout(1, 2)); // Split into two columns
mainPanel.add(inputPanel);
mainPanel.add(outputPanel);
JPanel containerPanel = new JPanel(new BorderLayout());
containerPanel.add(topPanel, BorderLayout.NORTH);
containerPanel.add(mainPanel, BorderLayout.CENTER);
containerPanel.add(bottomPanel, BorderLayout.SOUTH);
add(containerPanel);
}
// Read contents of a file and return as a single string
private String readFile(File file) {
try (BufferedReader reader = new BufferedReader(new FileReader(file))) {
String line;
while ((line = reader.readLine()) != null) {
fileContent.append(line).append("\n");
}
} catch (IOException e) {
e.printStackTrace();
}
return fileContent.toString();
}
private void compress(String algorithm) {
JOptionPane.showMessageDialog(null, "Compressing file using " + algorithm);
compression compresser = new compression(algorithm, inputTextArea.getText());
if (algorithm.equals("Huffman")) {
compressedContent = compresser.getCompressed();
outputTextArea.setText(compressedContent);
} else if (algorithm.equals("LZ77")) {
compressedLZ77 = compresser.getCompressedLZ77();
outputTextArea.setText(compressedLZ77.toString());
} else if (algorithm.equals("LZW")) {
compressedLZW = compresser.getCompressedLZW();
outputTextArea.setText(compressedLZW.toString());
} else {
JOptionPane.showMessageDialog(null, "Invalid compression algorithm.");
return;
}
// Fixed path to save the compressed file
String fixedPath = "C:\\familyfolders\\profolders\\Collage stuff\\Sem 2 project\\DSA\\output_files\\output.txt";
try {
// Create a new file
File outputFile = new File(fixedPath);
if (!outputFile.exists()) {
if (outputFile.createNewFile()) {
JOptionPane.showMessageDialog(null, "File created successfully at: " + outputFile.getAbsolutePath());
} else {
JOptionPane.showMessageDialog(null, "Failed to create the file at the specified location.");
return;
}
}
// Write the compressed content to the file
try (FileWriter writer = new FileWriter(outputFile)) {
if (algorithm.equals("Huffman")) {
writer.write(compressedContent);
} else if (algorithm.equals("LZ77")) {
writer.write(compressedLZ77.toString());
} else if (algorithm.equals("LZW")) {
writer.write(compressedLZW.toString());
}
}
} catch (IOException e) {
JOptionPane.showMessageDialog(null, "An error occurred while writing to the file.");
e.printStackTrace();
}
}
private void decompress(String algorithm) {
JOptionPane.showMessageDialog(null, "Decompressing file using " + algorithm);
decompression decompressor;
String decompressedContent;
if (algorithm.equals("Huffman")) {
decompressor = new decompression(algorithm, compressedContent);
} else if (algorithm.equals("LZ77")) {
decompressor = new decompression(algorithm, compressedLZ77);
} else if (algorithm.equals("LZW")) {
decompressor = new decompression(algorithm, compressedLZW);
} else {
JOptionPane.showMessageDialog(null, "Invalid decompression algorithm.");
return;
}
decompressedContent = decompressor.getDecompressed();
outputTextArea.setText(decompressedContent);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new FileChooserExample().setVisible(true);
}
});
}
}