-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaddFrame.java
More file actions
173 lines (149 loc) · 6.25 KB
/
addFrame.java
File metadata and controls
173 lines (149 loc) · 6.25 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
package project;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.LinkedList;
import java.util.concurrent.CountDownLatch;
class addFrame extends JFrame {
static int userChoice=-1 ;
static int idx=-1;
static int count;
static boolean isAdded = false;
CountDownLatch latch = new CountDownLatch(1);
public addFrame(Node ptr) {
latch = new CountDownLatch(1);
isAdded=false;
userChoice=-1 ;
isAdded=false;
if(ptr.child==null) {
JPanel panel = new JPanel();
JTextField usernameField = new JTextField(15);
JPasswordField passwordField = new JPasswordField(15);
panel.add(new JLabel("Topic :"));
panel.add(usernameField);
panel.add(new JLabel("Link :"));
panel.add(passwordField);
int result = JOptionPane.showConfirmDialog(null, panel, "Login",
JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);
if (result == JOptionPane.OK_OPTION) {
String topic = usernameField.getText();
String path = new String(passwordField.getPassword());
if (!topic.isEmpty() && !path.isEmpty()) {
Tree.path.put(topic,path);
JOptionPane.showMessageDialog(panel, "Successfully Added");
isAdded=true;
ptr.child=new LinkedList<>();
ptr.child.add(new Node(topic));
latch.countDown();
setVisible(false);
} else {
JOptionPane.showMessageDialog(panel, "Login failed!");
}
}
latch.countDown();
}
setTitle("Main Frame");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setExtendedState(JFrame.MAXIMIZED_BOTH);
getContentPane().setBackground(new Color(135, 206, 250)); // Set blue color
JPanel panel = new JPanel(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.insets = new Insets(10, 10, 10, 10);
int i=0;
count=0;
for (Node a: ptr.child) {
JButton b=new JButton(i+") "+a.data);
b.setPreferredSize(new Dimension(330, 40));
b.setBackground(Color.BLUE);
b.setForeground(Color.WHITE);
b.setFont(new Font("Arial", Font.PLAIN, 18));
b.setFocusPainted(false);
b.setBorder(BorderFactory.createLineBorder(Color.BLACK));
b.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
b.setBackground(new Color(0,0,0)); // Purple
JButton clickedButton = (JButton) e.getSource();
Character choice = clickedButton.getText().charAt(0);
String a = choice.toString();
int w = Integer.parseInt(a);
userChoice=w;
}
});
gbc.gridx = 5;
gbc.gridy = i++;
gbc.gridwidth = 2;
gbc.anchor = GridBagConstraints.CENTER;
panel.add(b, gbc);
count++;
}
JButton addHereButton = new JButton("Add Here");
addHereButton.setPreferredSize(new Dimension(150, 40));
addHereButton.setBackground(new Color(173, 216, 230));
addHereButton.setForeground(Color.BLACK);
addHereButton.setFont(new Font("Arial", Font.PLAIN, 18));
addHereButton.setFocusPainted(false);
addHereButton.setBorder(BorderFactory.createLineBorder(Color.BLACK));
addHereButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JPanel panel = new JPanel();
JTextField usernameField = new JTextField(15);
JPasswordField passwordField = new JPasswordField(15);
panel.add(new JLabel("Topic :"));
panel.add(usernameField);
panel.add(new JLabel("Link :"));
panel.add(passwordField);
int result = JOptionPane.showConfirmDialog(null, panel, "Login",
JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);
if (result == JOptionPane.OK_OPTION) {
String topic = usernameField.getText();
String path = new String(passwordField.getPassword());
if (!topic.isEmpty() && !path.isEmpty()) {
Tree.path.put(topic,path);
JOptionPane.showMessageDialog(panel, "Successfully Added");
isAdded=true;
ptr.child.add(new Node(topic));
latch.countDown();
setVisible(false);
} else {
JOptionPane.showMessageDialog(panel, "Login failed!");
}
}
}
});
gbc.gridx = 0;
gbc.gridy = 10;
panel.add(addHereButton, gbc);
JButton subtopicButton = new JButton("Subtopic");
subtopicButton.setPreferredSize(new Dimension(150, 40));
subtopicButton.setBackground(new Color(173, 216, 230));
subtopicButton.setForeground(Color.BLACK);
subtopicButton.setFont(new Font("Arial", Font.PLAIN, 18));
subtopicButton.setFocusPainted(false);
subtopicButton.setBorder(BorderFactory.createLineBorder(Color.BLACK));
subtopicButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(userChoice ==-1) {
JOptionPane.showMessageDialog(panel, "Select The Topic First");
}else {
setVisible(false);
latch.countDown();
}
}
});
gbc.gridx = 7;
gbc.gridy = 10;
gbc.anchor = GridBagConstraints.EAST;
panel.add(subtopicButton, gbc);
add(panel, BorderLayout.CENTER);
setVisible(true);
try {
latch.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}