forked from CharlesSkelton/studio
-
Notifications
You must be signed in to change notification settings - Fork 12
/
QPadImportDialog.java
228 lines (188 loc) · 8.4 KB
/
QPadImportDialog.java
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
225
226
227
228
package studio.ui;
import studio.core.AuthenticationManager;
import studio.core.Credentials;
import studio.kdb.Config;
import javax.swing.*;
import java.awt.*;
import java.io.File;
import javax.swing.filechooser.FileFilter;
import static javax.swing.GroupLayout.PREFERRED_SIZE;
public class QPadImportDialog extends EscapeDialog {
private JTextField txtServers;
private JRadioButton btnOverwrite, btnAppend;
private JTextField txtRootName;
private JComboBox comboBoxAuthMechanism;
private JTextField txtUser;
private JPasswordField txtPassword;
private JFileChooser chooser;
public enum Location {Overwrite, Append}
public QPadImportDialog(JFrame parent) {
super(parent, "QPad Server List Import");
initComponents();
}
public String getServersCfgLocation() {
return txtServers.getText();
}
public String getDefaultAuthenticationMechanism() {
return comboBoxAuthMechanism.getModel().getSelectedItem().toString();
}
public Credentials getCredentials() {
return new Credentials(txtUser.getText().trim(), new String(txtPassword.getPassword()));
}
public Location getImportTo() {
if (btnOverwrite.isSelected()) return Location.Overwrite;
if (btnAppend.isSelected()) return Location.Append;
return null;
}
public String getRootName() {
return txtRootName.getText();
}
private void refreshCredentials() {
Credentials credentials = Config.getInstance().getDefaultCredentials(getDefaultAuthenticationMechanism());
txtUser.setText(credentials.getUsername());
txtPassword.setText(credentials.getPassword());
}
private void initComponents() {
JLabel lblServers = new JLabel("Select location of Servers.cfg: ");
txtServers = new JTextField();
JButton btnServers = new JButton("...");
JLabel lblAuthMechanism = new JLabel("Authentication:");
JLabel lblUser = new JLabel(" User:");
JLabel lblPassword = new JLabel(" Password:");
comboBoxAuthMechanism = new JComboBox(AuthenticationManager.getInstance().getAuthenticationMechanisms());
comboBoxAuthMechanism.getModel().setSelectedItem(Config.getInstance().getDefaultAuthMechanism());
comboBoxAuthMechanism.addItemListener(e -> refreshCredentials());
txtUser = new JTextField();
txtPassword = new JPasswordField();
JLabel lblLocation = new JLabel("Location where servers will be imported in the current Server Tree: ");
btnOverwrite = new JRadioButton("Overwrite");
btnAppend = new JRadioButton("Append");
ButtonGroup buttonGroup = new ButtonGroup();
buttonGroup.add(btnOverwrite);
buttonGroup.add(btnAppend);
JLabel lblRootName = new JLabel("Root name of importer server or leave it empty: ");
txtRootName = new JTextField();
JPanel main = new JPanel();
GroupLayout layout = new GroupLayout(main);
main.setLayout(layout);
layout.setAutoCreateGaps(true);
layout.setAutoCreateContainerGaps(true);
layout.setHorizontalGroup(
layout.createParallelGroup()
.addGroup(
layout.createSequentialGroup()
.addComponent(lblServers)
.addComponent(txtServers)
.addComponent(btnServers)
).addGroup(
layout.createSequentialGroup()
.addComponent(lblAuthMechanism)
.addComponent(comboBoxAuthMechanism, PREFERRED_SIZE, PREFERRED_SIZE, PREFERRED_SIZE)
.addComponent(lblUser)
.addComponent(txtUser, 150, 150,150)
.addComponent(lblPassword)
.addComponent(txtPassword, 150, 150, 150)
).addGroup(
layout.createSequentialGroup()
.addComponent(lblLocation)
.addComponent(btnOverwrite)
.addComponent(btnAppend)
).addGroup(
layout.createSequentialGroup()
.addComponent(lblRootName)
.addComponent(txtRootName)
)
);
layout.setVerticalGroup(
layout.createSequentialGroup()
.addGroup(
layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(lblServers)
.addComponent(txtServers)
.addComponent(btnServers)
).addGroup(
layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(lblAuthMechanism)
.addComponent(comboBoxAuthMechanism)
.addComponent(lblUser)
.addComponent(txtUser)
.addComponent(lblPassword)
.addComponent(txtPassword)
).addGroup(
layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(lblLocation)
.addComponent(btnOverwrite)
.addComponent(btnAppend)
).addGroup(
layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(lblRootName)
.addComponent(txtRootName)
)
);
JButton btnImport = new JButton("Import");
JButton btnCancel = new JButton("Cancel");
JPanel bottom = new JPanel(new FlowLayout(FlowLayout.RIGHT));
bottom.add(btnImport);
bottom.add(btnCancel);
JPanel root = new JPanel(new BorderLayout());
root.add(main, BorderLayout.CENTER);
root.add(bottom, BorderLayout.SOUTH);
setContentPane(root);
chooser = new JFileChooser();
FileFilter serversCfgFF = new ServersCfgFileFilter();
chooser.setFileHidingEnabled(false);
chooser.addChoosableFileFilter(serversCfgFF);
chooser.setFileFilter(serversCfgFF);
btnImport.addActionListener(e->checkAndAccept());
btnCancel.addActionListener(e->cancel());
btnServers.addActionListener(e->selectServersCfg());
}
private void selectServersCfg() {
int result = chooser.showOpenDialog(this);
if (result == JFileChooser.ERROR_OPTION) {
StudioOptionPane.showError(this, "Error while selecting Servers.cfg", "Error");
return;
}
if (result == JFileChooser.CANCEL_OPTION) {
return;
}
String location = chooser.getSelectedFile().getAbsoluteFile().toString();
txtServers.setText(location);
}
private void checkAndAccept() {
String location = getServersCfgLocation();
if (location.trim().length() == 0) {
StudioOptionPane.showError(this, "File to import is not selected", "Not Found Servers.cfg");
return;
}
if (! new File(location).exists()) {
StudioOptionPane.showError(this, "File to import doesn't not exist (" + location + ")", "Not Found Servers.cfg");
return;
}
if (getImportTo() == null) {
StudioOptionPane.showError(this, "Select how current Server Tree should be modified: Overwrite or Append", "Where to Import");
return;
}
if (getRootName().contains("/")) {
StudioOptionPane.showError(this, "Folder name can't contain /", "Wrong Folder Name");
return;
}
accept();
}
private static class ServersCfgFileFilter extends FileFilter {
@Override
public boolean accept(File f) {
if (f.isDirectory()) return true;
return f.getName().equals("Servers.cfg");
}
@Override
public String getDescription() {
return "Servers.cfg";
}
}
public static void main(String[] args) {
QPadImportDialog d = new QPadImportDialog(null);
d.pack();
d.setVisible(true);
}
}