-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEntryDialog.java
More file actions
204 lines (171 loc) · 8.19 KB
/
Copy pathEntryDialog.java
File metadata and controls
204 lines (171 loc) · 8.19 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
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
public class EntryDialog extends JDialog {
private final Color BG_MAIN = Color.decode("#121212");
private final Color ACCENT = Color.decode("#00e676");
private final Color TEXT_WHITE = Color.decode("#ffffff");
private final Color TEXT_GRAY = Color.decode("#b0b0b0");
private final Color FIELD_BG = Color.decode("#333333");
private JTextField titleField;
private JTextField usernameField;
private JPasswordField passwordField;
private JTextField urlField;
private PasswordEntry result;
private boolean isEditMode;
public EntryDialog(JFrame parent, PasswordEntry existingEntry) {
super(parent, existingEntry == null ? "Add New Entry" : "Edit Entry", true);
this.isEditMode = existingEntry != null;
setSize(500, 500);
setLocationRelativeTo(parent);
setLayout(new BorderLayout());
JPanel mainPanel = new JPanel();
mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));
mainPanel.setBackground(BG_MAIN);
mainPanel.setBorder(new EmptyBorder(30, 40, 30, 40));
JLabel titleLabel = new JLabel(isEditMode ? "Edit Entry" : "Add New Entry");
titleLabel.setForeground(TEXT_WHITE);
titleLabel.setFont(new Font("Segoe UI", Font.BOLD, 24));
titleLabel.setAlignmentX(Component.LEFT_ALIGNMENT);
mainPanel.add(titleLabel);
mainPanel.add(Box.createRigidArea(new Dimension(0, 30)));
mainPanel.add(createDialogLabel("Title"));
mainPanel.add(Box.createRigidArea(new Dimension(0, 5)));
titleField = new JTextField();
mainPanel.add(createDialogRoundedField(titleField));
mainPanel.add(Box.createRigidArea(new Dimension(0, 20)));
mainPanel.add(createDialogLabel("Username / Email"));
mainPanel.add(Box.createRigidArea(new Dimension(0, 5)));
usernameField = new JTextField();
mainPanel.add(createDialogRoundedField(usernameField));
mainPanel.add(Box.createRigidArea(new Dimension(0, 20)));
mainPanel.add(createDialogLabel("Password"));
mainPanel.add(Box.createRigidArea(new Dimension(0, 5)));
passwordField = new JPasswordField();
passwordField.setEchoChar('\u2022');
mainPanel.add(createDialogRoundedField(passwordField));
mainPanel.add(Box.createRigidArea(new Dimension(0, 20)));
mainPanel.add(createDialogLabel("Website URL"));
mainPanel.add(Box.createRigidArea(new Dimension(0, 5)));
urlField = new JTextField();
mainPanel.add(createDialogRoundedField(urlField));
mainPanel.add(Box.createRigidArea(new Dimension(0, 30)));
if (existingEntry != null) {
titleField.setText(existingEntry.getTitle());
usernameField.setText(existingEntry.getUsername());
passwordField.setText(existingEntry.getPassword());
urlField.setText(existingEntry.getUrl());
}
JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.LEFT, 10, 0));
buttonPanel.setOpaque(false);
buttonPanel.setMaximumSize(new Dimension(400, 50));
buttonPanel.setAlignmentX(Component.LEFT_ALIGNMENT);
buttonPanel.setBorder(new EmptyBorder(0, -5, 0, 0));
JButton saveButton = new JButton("Save Entry");
styleDialogButton(saveButton, ACCENT, Color.BLACK);
saveButton.addActionListener(e -> {
String title = titleField.getText().trim();
String username = usernameField.getText().trim();
String password = new String(passwordField.getPassword()).trim();
String url = urlField.getText().trim();
if (title.isEmpty() || username.isEmpty() || password.isEmpty()) {
JOptionPane.showMessageDialog(this,
"Title, Username, and Password are required fields.",
"Validation Error",
JOptionPane.ERROR_MESSAGE);
return;
}
result = new PasswordEntry(title, username, password, url);
dispose();
});
JButton cancelButton = new JButton("Cancel");
styleDialogButton(cancelButton, FIELD_BG, TEXT_WHITE);
cancelButton.addActionListener(e -> {
result = null;
dispose();
});
buttonPanel.add(saveButton);
buttonPanel.add(cancelButton);
mainPanel.add(buttonPanel);
add(mainPanel, BorderLayout.CENTER);
}
public PasswordEntry getResult() {
return result;
}
private JLabel createDialogLabel(String text) {
JLabel label = new JLabel(text);
label.setForeground(TEXT_GRAY);
label.setFont(new Font("Segoe UI", Font.BOLD, 12));
label.setAlignmentX(Component.LEFT_ALIGNMENT);
return label;
}
private JPanel createDialogRoundedField(JTextField field) {
RoundedPanel roundedPanel = new RoundedPanel(400, 15);
roundedPanel.setLayout(new BorderLayout());
roundedPanel.setBackground(FIELD_BG);
roundedPanel.setMaximumSize(new Dimension(400, 40));
roundedPanel.setPreferredSize(new Dimension(400, 40));
roundedPanel.setBorder(new EmptyBorder(0, 15, 0, 15));
roundedPanel.setAlignmentX(Component.LEFT_ALIGNMENT);
field.setBackground(FIELD_BG);
field.setForeground(TEXT_WHITE);
field.setCaretColor(ACCENT);
field.setBorder(null);
field.setFont(new Font("Segoe UI", Font.PLAIN, 14));
if (field instanceof JPasswordField) {
JPanel fieldPanel = new JPanel(new BorderLayout());
fieldPanel.setOpaque(false);
fieldPanel.add(field, BorderLayout.CENTER);
JButton toggleButton = new JButton("👁");
toggleButton.setFont(new Font("Segoe UI", Font.PLAIN, 14));
toggleButton.setBackground(FIELD_BG);
toggleButton.setForeground(TEXT_GRAY);
toggleButton.setFocusPainted(false);
toggleButton.setBorderPainted(false);
toggleButton.setCursor(new Cursor(Cursor.HAND_CURSOR));
toggleButton.setPreferredSize(new Dimension(30, 30));
toggleButton.addActionListener(e -> {
JPasswordField passwordField = (JPasswordField) field;
if (passwordField.getEchoChar() == '\u2022') {
passwordField.setEchoChar((char) 0);
toggleButton.setText("👁");
} else {
passwordField.setEchoChar('\u2022');
toggleButton.setText("👁");
}
});
JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT, 0, 0));
buttonPanel.setOpaque(false);
buttonPanel.add(toggleButton);
fieldPanel.add(buttonPanel, BorderLayout.EAST);
roundedPanel.add(fieldPanel, BorderLayout.CENTER);
} else {
roundedPanel.add(field, BorderLayout.CENTER);
}
return roundedPanel;
}
private void styleDialogButton(JButton button, Color bg, Color fg) {
button.setBackground(bg);
button.setForeground(fg);
button.setFocusPainted(false);
button.setBorderPainted(false);
button.setFont(new Font("Segoe UI", Font.BOLD, 14));
button.setPreferredSize(new Dimension(140, 40));
button.setCursor(new Cursor(Cursor.HAND_CURSOR));
}
static class RoundedPanel extends JPanel {
private int radius;
public RoundedPanel(int width, int radius) {
this.radius = radius;
setOpaque(false);
}
@Override
protected void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2.setColor(getBackground());
g2.fillRoundRect(0, 0, getWidth(), getHeight(), radius, radius);
super.paintComponent(g);
}
}
}