-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
237 lines (221 loc) · 8.33 KB
/
Main.java
File metadata and controls
237 lines (221 loc) · 8.33 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
225
226
227
228
229
230
231
232
233
234
235
236
237
import javax.swing.*;
import java.io.*;
import java.util.ArrayList;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
try {
UIManager.setLookAndFeel("javax.swing.plaf.nimbus.NimbusLookAndFeel");
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (UnsupportedLookAndFeelException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
UserFrame userFrame = new UserFrame();
while (true) {
if (userFrame.isTmpCheck())
break;
}
MainFrame frame = new MainFrame();
frame.setUserName(userFrame.getText());
int userAlive = 0;
UserNames userNamesFromFile = null;
String nameOfLibrary, nameOfPlaylists, nameOfAlbum;
nameOfLibrary = userFrame.getText() + "library.txt";
nameOfAlbum = userFrame.getText() + "album.txt";
nameOfPlaylists = userFrame.getText() + "playlists.txt";
File file0 = new File("UeserNames.txt");
if (file0.exists()) {
ObjectInputStream objectInputStream =
null;
try {
objectInputStream = new ObjectInputStream(new FileInputStream("UeserNames.txt"));
} catch (IOException e) {
e.printStackTrace();
}
try {
try {
userNamesFromFile = (UserNames) objectInputStream.readObject();
} catch (IOException e) {
e.printStackTrace();
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
try {
objectInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (userNamesFromFile != null) {
int i;
for (i = 0; i < userNamesFromFile.getUserNameArraylist().size(); i++) {
frame.userNames.setUserNameArraylist(userNamesFromFile.getUserNameArraylist().get(i));
if (userNamesFromFile.getUserNameArraylist().get(i).equals(userFrame.getText())) {
userAlive = 1;
}
}
if (userAlive == 0)
frame.userNames.setUserNameArraylist(userFrame.getText());
} else {
frame.userNames.setUserNameArraylist(userFrame.getText());
}
Album library = null;
File file = new File(nameOfLibrary);
if (file.exists() && (file.length() > 122)) {
ObjectInputStream objectInputStream =
null;
try {
objectInputStream = new ObjectInputStream(new FileInputStream(nameOfLibrary));
} catch (IOException e) {
e.printStackTrace();
}
try {
try {
library = (Album) objectInputStream.readObject();
} catch (IOException e) {
e.printStackTrace();
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
try {
objectInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (library != null) {
for (int i = 0; i < library.getSongs().size(); i++) {
frame.library.setSong(library.getSongs().get(i));
}
}
Albums albums = null;
File file1 = new File(nameOfAlbum);
if (file1.exists() && (file1.length() > 122)) {
ObjectInputStream objectInputStream =
null;
try {
objectInputStream = new ObjectInputStream(new FileInputStream(nameOfAlbum));
} catch (IOException e) {
e.printStackTrace();
}
try {
try {
albums = (Albums) objectInputStream.readObject();
} catch (IOException e) {
e.printStackTrace();
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
try {
objectInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (albums != null) {
for (int i = 0; i < albums.getAlbumArrayList().size(); i++) {
frame.albumArrayList.setAlbum(albums.getAlbumArrayList().get(i));
}
}
Albums albums1 = null;
File file2 = new File(nameOfPlaylists);
if (file2.exists() && (file2.length() > 122)) {
ObjectInputStream objectInputStream =
null;
try {
objectInputStream = new ObjectInputStream(new FileInputStream(nameOfPlaylists));
} catch (IOException e) {
e.printStackTrace();
}
try {
try {
albums1 = (Albums) objectInputStream.readObject();
} catch (IOException e) {
e.printStackTrace();
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
try {
objectInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
int favoriteIsAlive = 0, sharedPlaylistIsAlive = 0;
if (albums1 != null) {
for (int i = 0; i < albums1.getAlbumArrayList().size(); i++) {
frame.playLists.setAlbum(albums1.getAlbumArrayList().get(i));
if (albums1.getAlbumArrayList().get(i).getName().equals("Favorites")) {
frame.leftPanel.getFavoriteSongs().addActionListener(frame.actionListenerForPlayList);
favoriteIsAlive = 1;
} else if (albums1.getAlbumArrayList().get(i).getName().equals("SharedPlaylist")) {
frame.leftPanel.getSharedPlaylist().addActionListener(frame.actionListenerForPlayList);
sharedPlaylistIsAlive = 1;
} else {
frame.leftPanel.getPlaylists().clear();
frame.leftPanel.addLableOfplaylist(frame.playLists.getAlbumArrayList().get(i).getName());
frame.leftPanel.getPlaylists().get(frame.leftPanel.getPlaylists().size() - 1).addActionListener(frame.actionListenerForPlayList);
}
frame.leftPanel.setVisible(false);
frame.leftPanel.setVisible(true);
}
}
if (favoriteIsAlive == 0) {
frame.creatFavorite();
}
if (sharedPlaylistIsAlive == 0) {
frame.creatSharedPlayList();
}
String myUsername = frame.getUserName();
FileReader fileReader = null;
try {
fileReader = new FileReader("IPList.txt");
} catch (FileNotFoundException e) {
e.printStackTrace();
}
Scanner scanner = new Scanner(fileReader);
ArrayList<String> IPList = new ArrayList<>(100);
while (scanner.hasNext())
IPList.add(scanner.next());
try {
fileReader.close();
} catch (IOException e) {
e.printStackTrace();
}
String myIpHost = "127.0.0.1";
int myPort = 4000;
String friendIp = "127.0.0.1";
int friendPort = 5000;
boolean [] allowAccess=new boolean[IPList.size()];
for (int i = 0; i <IPList.size() ; i++) {
allowAccess[i]=false;
}
for (int i = 0; i <IPList.size() ; i++) {
if (IPList.get(i).equals(friendIp))
allowAccess[i]=true;
}
Friend friend=null;
for (int i = 0; i <IPList.size() ; i++) {
if (allowAccess[i])
friend = new Friend(frame.getUserName(), myIpHost, myPort, friendIp, friendPort);
}
while (true) {
if (frame.isClicked() == true) {
try {
friend.sendData(myUsername);
frame.setClicked(false);
} catch (IOException e) {
}
}
}
}
}