-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJFilePicker.java
More file actions
206 lines (167 loc) · 5.95 KB
/
JFilePicker.java
File metadata and controls
206 lines (167 loc) · 5.95 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
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
//import com.jayway.jsonpath.JsonPath;
import com.vormetric.rest.sample.VormetricCryptoServerHelper;
import com.vormetric.rest.sample.VormetricCryptoServerSettings;
import java.util.ArrayList;
import java.util.List;
//This sample code tests the VAE Rest calls to the Vormetric Crypto Server.
//It will first encrypt a file and create an output file with enc at the end of it.
//You can then select the file that you just encrypted that ends with an enc and
//the application will then decrypt it.
//The input file format should be a single entry for each line.
//This is for testing only so it will only at most process the first 10 records
//in the file.
public class JFilePicker extends JPanel {
private String textFieldLabel;
private String buttonLabel;
private JLabel label;
private JTextField textField;
private JTextField resultsField;
private JButton button;
private JFileChooser fileChooser;
private int mode;
public static final int MODE_OPEN = 1;
public static final int MODE_SAVE = 2;
String vtsdebug = "0";
public JFilePicker(String textFieldLabel, String buttonLabel) throws IOException {
this.textFieldLabel = textFieldLabel;
this.buttonLabel = buttonLabel;
fileChooser = new JFileChooser();
//setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
setLayout(new GridLayout(2,3));
// creates the GUI
label = new JLabel(textFieldLabel);
resultsField = new JTextField(10);
resultsField.setVisible(false);
resultsField.setBackground(Color.GRAY);
textField = new JTextField(30);
button = new JButton(buttonLabel);
textField.setVisible(false);
// JLabel imgLabel = new JLabel(new ImageIcon("C:\\Users\\mark.warner\\workspace\\JavaVAEUI\\src\\images\\vsign.png"));
// wPic = ImageIO.read(this.getClass().getResource("vsign.png"));
// wIcon = new JLabel(new ImageIcon(wPic));
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent evt) {
try {
buttonActionPerformed(evt);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
// add(imgLabel);
add(label);
add(textField);
add(button);
add(resultsField);
}
private void buttonActionPerformed(ActionEvent evt) throws Exception {
if (mode == MODE_OPEN) {
if (fileChooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
// System.out.println("open code" +
// fileChooser.getSelectedFile().getAbsolutePath());
textField.setText(fileChooser.getSelectedFile().getAbsolutePath());
}
} else if (mode == MODE_SAVE) {
//setSize(680, 100);
//This class uses the environment variables for necessary vcs settings.
VormetricCryptoServerSettings vcs = new VormetricCryptoServerSettings();
//System.out.println("to string " + vcs.toString());
if (fileChooser.showSaveDialog(this) == JFileChooser.APPROVE_OPTION) {
textField.setText(fileChooser.getSelectedFile().getAbsolutePath());
// System.out.println("save code" +
// fileChooser.getSelectedFile().getAbsolutePath());
File file = new File(fileChooser.getSelectedFile().getAbsolutePath());
BufferedReader reader = null;
PrintWriter writer = null;
boolean decrypt = false;
if (fileChooser.getSelectedFile().getAbsolutePath().endsWith("enc")) {
decrypt = true;
writer = new PrintWriter(fileChooser.getSelectedFile().getAbsolutePath() + "dec", "UTF-8");
} else {
writer = new PrintWriter(fileChooser.getSelectedFile().getAbsolutePath() + "enc", "UTF-8");
}
textField.setVisible(true);
resultsField.setVisible(true);
List<String> list = new ArrayList<String>();
{
try {
reader = new BufferedReader(new FileReader(file));
String text = null;
int i = 0;
String calltype = null;
while ((text = reader.readLine()) != null && i < 20) {
if (text.length() > 0) {
String results = null;
if (decrypt) {
calltype = "decrypt";
results = VormetricCryptoServerHelper.doDeCryptData(vcs.getvcstokenserver(),
vcs.getvcsuserid(), vcs.getvcspassword(), text, vcs.getvcsalg(),
vcs.getvcsivnumber(), calltype, vcs.getvcsencryptdecryptkey());
} else {
calltype = "encrypt";
results = VormetricCryptoServerHelper.doEncryptData(vcs.getvcstokenserver(),
vcs.getvcsuserid(), vcs.getvcspassword(), text, vcs.getvcsalg(),
vcs.getvcsivnumber(), calltype, vcs.getvcsencryptdecryptkey());
}
writer.println(results);
} else {
writer.println(text);
}
list.add((text));
i++;
}
resultsField.setText( calltype +" complete:" + new Integer(i));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (reader != null) {
reader.close();
}
} catch (IOException e) {
}
}
// print out the list
System.out.println("Original values" + list);
writer.close();
}
}
}
}
public void addFileTypeFilter(String extension, String description) {
FileTypeFilter filter = new FileTypeFilter(extension, description);
fileChooser.addChoosableFileFilter(filter);
}
public void setMode(int mode) {
this.mode = mode;
}
public String getSelectedFilePath() {
return textField.getText();
}
public JFileChooser getFileChooser() {
return this.fileChooser;
}
}