- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainFrame.java
More file actions
Latest commit
735 lines (634 loc) · 32.2 KB
/
Copy pathMainFrame.java
File metadata and controls
735 lines (634 loc) · 32.2 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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
importjavax.swing.JFrame;
importjavax.swing.JPanel;
importjavax.swing.JLabel;
importjavax.swing.JButton;
importjavax.swing.JOptionPane;
importjavax.swing.JTextField;
importjavax.swing.ImageIcon;
importjavax.swing.border.TitledBorder;
importjavax.swing.border.EtchedBorder;
importtwitter4j.Status;
importtwitter4j.Twitter;
importtwitter4j.TwitterException;
importtwitter4j.TwitterFactory;
importtwitter4j.ResponseList;
importtwitter4j.auth.AccessToken;
importtwitter4j.auth.RequestToken;
importtwitter4j.conf.ConfigurationBuilder;
importtwitter4j.conf.Configuration;
importtwitter4j.media.ImageUpload;
importtwitter4j.media.ImageUploadFactory;
importtwitter4j.media.MediaProvider;
importtwitter4j.User;
importjava.awt.BorderLayout;
importjava.awt.Color;
importjava.awt.Dimension;
importjava.awt.Font;
importjava.awt.GridLayout;
importjava.awt.List;
importjava.awt.event.ActionListener;
importjava.awt.event.ActionEvent;
importjava.awt.event.KeyEvent;
importjava.awt.event.KeyListener;
importjava.awt.event.MouseListener;
importjava.awt.event.MouseEvent;
importjava.awt.image.BufferedImage;
importjava.net.URL;
importjava.net.MalformedURLException;
importjava.util.ArrayList;
importjava.util.GregorianCalendar;
importjava.util.Calendar;
importjava.awt.GridBagLayout;
importjava.awt.GridBagConstraints;
importjava.awt.FlowLayout;
importjava.awt.Insets;
/*
* username - nishand@gmail.com or Twit4Java (email preferred)
*
*/
/**
* MainPanel
*
* @author William Chern & Nishan D'Souza
* @version 1.0
*/
publicclassMainFrame
{
privatefinalFontdefaultUIFont = newFont("Arial", Font.PLAIN, 14); // the default UI font (Arial) for Twit4Java
privatefinalFontdefaultUIFontBold = newFont("Arial", Font.BOLD, 15); // the default BOLD style UI font
// instance field variables
privateJFramefr;
privateJPaneloverallPanel;
privateStringnewTweetString;
privateJButtontweetButton;
privateJTextFieldnewTweetTextField;
privateJLabelcurrentUserHandle;
privateJLabelcurrentUserAccountImage;
privateTwittertwitter;
privateUseruser;
privateURLurl;
privateImageIconfavoritePic;
privateImageIconfavoriteHover;
privateImageIconretweet;
privateImageIconretweetHover;
privateJLabelprofileViewUserAccountImage;
privateJLabelprofileViewUserHandle;
privateJLabelprofileViewNumTweetsLabel;
privateJLabelprofileViewNumFollowersLabel;
privateJLabelprofileViewNumFollowingLabel;
privateJLabelprofileTweet1;
privateJLabelprofileTweet2;
privateJLabelprofileTweet3;
privateJLabelprofileTweet4;
privateJLabeltweetTitleLabel;
privateJPanelrightPanel;
publicMainFrame () {
favoritePic = createImageIcon("heart_button_default.png"); // invoke createImageIcon method (defined below) to assign ImageIcon object reference from the path
favoriteHover = createImageIcon("heart_button_hover.png");
retweet = createImageIcon("retweet_button_default.png");
retweetHover = createImageIcon("retweet_button_hover.png");
fr = newJFrame("Twit4Java"); // initialize JFrame object reference
// OAuth authentication
StringconsumerKey = "LqFDdgq7SurJdoQeAtBiDmC8p";//saving various consumer and access keys, secrets, and tokens for Oauth
StringconsumerSecret = "GDdnMzYJyLddVFgdeXET9I0sHzQFYMGgozIrcTiJzDcTSflogo";
StringaccessToken = "729714377562030082-FtbarB7pQ6BbMK8589vPpoiVgFBMV0i";
StringaccessSecret = "o1GgPnihASydGh7jgMQEFncw7DQp0hGfHC9BcvpXL4A0a";
ConfigurationBuildercb = newConfigurationBuilder();//constructs a configuration builder, a class which is used to configure the twitter api
cb.setDebugEnabled(true);
cb.setOAuthConsumerKey(consumerKey);
cb.setOAuthConsumerSecret(consumerSecret);
cb.setOAuthAccessToken(accessToken);
cb.setOAuthAccessTokenSecret(accessSecret);//invokes setter methods on the ConfigurationBuilder to allow it to configure a twitter object
TwitterFactorytf = newTwitterFactory(cb.build());//constructs a new TwitterFactory with a parameter of the return of the invoked method, .build()
twitter = tf.getInstance();//invokes the getInstance on the TwitterFactory and sets the return to a new Twitter object referenced by twitter
System.setProperty("twitter4j.http.useSSL", "false");//THIS THING TOOK SO LONG HARDEST PART HANDS DOWN. So apparently twitter doesn't support ssl and instead wants https
AccessTokena = newAccessToken(accessToken, accessSecret);//I therefore needed to invoke the setProperty on the System itself to change the useSLL property to false
twitter.setOAuthAccessToken(a);//Creates a new AccessToken object which contains the token and secret, and invokes the setOAuthAccessToken method to allow our twitter object to do commands
newTweetString = "";
currentUserAccountImage = newJLabel();
try {
user = twitter.showUser(twitter.getId());//gets the current user
}
catch (TwitterExceptionte) {}
Stringu = user.getProfileImageURL();
try {
url = newURL(u);//gets the url for the user's profile pic
}
catch (MalformedURLExceptionmu){}
overallPanel = newJPanel(newBorderLayout());
currentUserAccountImage.setIcon(newImageIcon(url));//invokes setIcon with new image icon from the url
newTweetTextField = newJTextField();
currentUserHandle = newJLabel("@Twit4Java");
tweetButton = newJButton("Tweet");
profileViewUserAccountImage = newJLabel();
profileViewUserHandle = newJLabel("@user");
profileViewNumTweetsLabel = newJLabel("[X] tweets");
profileViewNumFollowersLabel = newJLabel("[X] followers");
profileViewNumFollowingLabel = newJLabel("[X] following");
profileTweet1 = newJLabel("");
profileTweet2 = newJLabel("");
profileTweet3 = newJLabel("");
profileTweet4 = newJLabel("");
tweetTitleLabel = newJLabel("User Tweets:");
}
publicvoiddisplayInterface () {
fr.setSize(1300, 650); // invoke setSize mutator method on JFrame object referenced by fr
fr.setResizable(false); // do not allow resizing of window
fr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // automatically reset JVM upon closing
// invoke methods to add left (new Tweet), center (Timeline), and right (Profile view) panels
addLeftPanel();
addCenterPanel();
addRightPanel();
// invoke mutator methods on fr to add overallPanel and display it in the window frame
fr.add(overallPanel);
fr.setVisible(true);
}
privatevoidaddLeftPanel () {
/*
* Commands for bot
* DM + message + recipientHandle
* Follow + handle
* Favorite recent of + Handle
* Retween recent of + Handle
*/
JPanelleftPanel = newJPanel(newGridLayout(10, 1)); // declare and initialize new leftPanel object reference
// set font for New Tweet controls
newTweetTextField.setFont(defaultUIFont);
tweetButton.setFont(newFont("Arial", Font.BOLD, 16));
// add New Tweet controls to leftPanel
leftPanel.add(newTweetTextField);
leftPanel.add(tweetButton);
JPanelcurrentUserAccountPanel = newJPanel(newBorderLayout(2, 2)); // initialize panel for displaying information about the current user's account
currentUserHandle.setFont(defaultUIFontBold);
for(inti = 0; i < 3; i++) {
leftPanel.add(newJPanel());
}
JPanelbotHeadingPanel = newJPanel(newGridLayout(1,2));
JLabelbotHeadingLabel = newJLabel("Twitter Bot");
botHeadingLabel.setFont(defaultUIFontBold);
JLabelbotHeadingHelpPanel = newJLabel();
ImageIconhelp = createImageIcon("help_button.png");
botHeadingHelpPanel.setIcon(help);
JTextFieldbotText = newJTextField();
botHeadingPanel.add(botHeadingLabel);
botHeadingPanel.add(botHeadingHelpPanel);
JButtonbotDoneButton = newJButton("Request");
botDoneButton.setFont(newFont("Arial", Font.BOLD, 16));
leftPanel.add(botHeadingPanel);
leftPanel.add(botText);
// add current user info to currentUserAccountPanel
currentUserAccountPanel.add(currentUserHandle, BorderLayout.EAST);
currentUserAccountPanel.add(currentUserAccountImage, BorderLayout.WEST);
// add currentUserAccountPanel to the entire leftPanel; will be below New Tweet controls
leftPanel.add(currentUserAccountPanel);
classTweetButtonListenerimplementsActionListener
{
publicvoidactionPerformed (ActionEvente) { // implement actionPerformed() method defined in ActionListener interface
newTweetString = newTweetTextField.getText(); // get text from the New Tweet JTextField
if (newTweetString.length() > 131) {
// if user's new tweet exceeds 140 characters (after Twit4Java's added timestamp), show pop-up warning (and prevent tweeting)
JOptionPanecharacterLengthWarningPane = newJOptionPane();
characterLengthWarningPane.showMessageDialog(null, "Tweet cannot exceed 140 characters!", "Tweet Length", JOptionPane.WARNING_MESSAGE);
}
elseif (newTweetString==null) {
// if user's new tweet is null, display a warning (and prevent tweeting)
JOptionPanenullTweetWarningPane = newJOptionPane();
nullTweetWarningPane.showMessageDialog(null, "You must enter some text to tweet!", "Tweet Length", JOptionPane.WARNING_MESSAGE);
}
else {
// Twit4Java appends a timestamp at the end of new tweet and then tweets the user's text
GregorianCalendarcalendar = newGregorianCalendar();
try {
Statusstatus = twitter.updateStatus(newTweetString + " " + calendar.get(Calendar.HOUR) + ":" + calendar.get(Calendar.MINUTE) + ":" + calendar.get(Calendar.SECOND));
newTweetTextField.setText(""); // clear the new tweet JTextField
}
catch (TwitterExceptionte) {}
}
}
}
tweetButton.addActionListener(newTweetButtonListener()); // add new instance of TweetButtonListener to the TweetButton
classHelpButtonListenerimplementsMouseListener {
publicvoidmouseClicked (MouseEvente) {
}
publicvoidmouseEntered (MouseEvente) {
}
publicvoidmouseExited (MouseEvente) {
}
publicvoidmousePressed (MouseEvente) {
}
publicvoidmouseReleased (MouseEvente) {
}
}
botHeadingHelpPanel.addMouseListener(newHelpButtonListener());
classBotButtonListenerimplementsActionListener {
publicvoidactionPerformed (ActionEvente) {}
}
botDoneButton.addActionListener(newBotButtonListener());
leftPanel.setBorder(newTitledBorder(newEtchedBorder())); // set border of entire left panel, make it visible (etched)
overallPanel.add(leftPanel, BorderLayout.WEST); // add the leftPanel to the overall panel on the left (west) side
/*
* Bot Outline
* 1. Create UI (Similar to tweet functionality above)
* 2. Add a help button w/instructions
* 3. Create series of switches to check if a real instruction is given
* 4. Implement logic into each switch statement
*/
}
privatevoidaddCenterPanel () {
JPanelcenterPanel = newJPanel(newGridBagLayout()); // declare and initialize new centerPanel object reference
GridBagConstraintsconstraints = newGridBagConstraints();
constraints.anchor = GridBagConstraints.WEST;
ResponseList<Status> statusList; // List to hold latest Timeline data retrieved from Twitter
ArrayList<TweetData> tweetDataList = newArrayList<TweetData>(); // ArrayList to hold data for the latest 5 tweets from Timeline
try {
statusList = twitter.getHomeTimeline(); // invoke getHomeTimeline accessor method and retreive latest Timeline tweets
// System.out.println("@" + statusList.get(0).getUser().getScreenName() + " — " + statusList.get(0).getText());
for (inti=0; i<5; i++) {
// add data for the latest 5 tweets from Timeline to new instances of TweetData class
Useru = statusList.get(i).getUser();
StringstrUrl = u.getProfileImageURL();
URLpicURL;
ImageIconprofileImg;
try{
picURL = newURL(strUrl);
profileImg = newImageIcon(picURL);
longtweetID = statusList.get(i).getId();
longuserID = statusList.get(i).getUser().getId();
tweetDataList.add(newTweetData("@" + statusList.get(i).getUser().getScreenName(), statusList.get(i).getText(), profileImg, statusList.get(i).getRetweetCount(), statusList.get(i).getFavoriteCount(), statusList.get(i).getUser().getStatusesCount(), statusList.get(i).getUser().getFollowersCount(), statusList.get(i).getUser().getFriendsCount(), tweetID, userID));
} catch(MalformedURLExceptionte) {
}
}
for (inti=0; i<5; i++) {
switch(i) {
case0:
constraints.anchor = GridBagConstraints.WEST;
constraints.weightx = 1;
constraints.gridx = 0;
constraints.gridy = 0;
break;
case1:
constraints.anchor = GridBagConstraints.WEST;
constraints.weightx = 1;
constraints.gridx = 0;
constraints.gridy = 1;
break;
case2:
constraints.anchor = GridBagConstraints.WEST;
constraints.weightx = 1;
constraints.gridx = 0;
constraints.gridy = 2;
break;
case3:
constraints.anchor = GridBagConstraints.WEST;
constraints.weightx = 1;
constraints.gridx = 0;
constraints.gridy = 3;
break;
case4:
constraints.anchor = GridBagConstraints.WEST;
constraints.weightx = 1;
constraints.gridx = 0;
constraints.gridy = 4;
break;
default: break;
}
constraints.anchor = GridBagConstraints.WEST;
constraints.weightx = 1;
constraints.insets = newInsets(30,30,30,30);
displayTweet(tweetDataList.get(i), centerPanel, constraints, rightPanel); // invoke displayTweet method to display Tweet and its data and add to center Timeline panel
}
}
catch (TwitterExceptionte) {
System.out.println("COULD NOT GET TIMELINE STATUS");
}
overallPanel.add(centerPanel, BorderLayout.CENTER); // add centerPanel to the overall panel in the center
}
privatevoidaddRightPanel () {
rightPanel = newJPanel(newGridBagLayout()); // declare and initialize new rightPanel object reference
GridBagConstraintsgbc = newGridBagConstraints();
gbc.anchor = GridBagConstraints.NORTHWEST;
gbc.weightx = 1;
gbc.insets = newInsets(12, 12, 12, 12);
// set font to default UI fonts
profileViewUserHandle.setFont(defaultUIFontBold);
profileViewNumTweetsLabel.setFont(defaultUIFont);
profileViewNumFollowersLabel.setFont(defaultUIFont);
profileViewNumFollowingLabel.setFont(defaultUIFont);
tweetTitleLabel.setFont(defaultUIFont);
profileTweet1.setFont(defaultUIFont);
profileTweet2.setFont(defaultUIFont);
profileTweet3.setFont(defaultUIFont);
profileTweet4.setFont(defaultUIFont);
// add profileView components to rightPanel
gbc.gridx = 0;
gbc.gridy = 0;
rightPanel.add(profileViewUserAccountImage, gbc);
gbc.gridx = 0;
gbc.gridy = 1;
rightPanel.add(profileViewUserHandle, gbc);
gbc.gridx = 0;
gbc.gridy = 2;
rightPanel.add(profileViewNumTweetsLabel, gbc);
gbc.gridx = 0;
gbc.gridy = 3;
rightPanel.add(profileViewNumFollowersLabel, gbc);
gbc.gridx = 0;
gbc.gridy = 4;
rightPanel.add(profileViewNumFollowingLabel, gbc);
gbc.gridx = 0;
gbc.gridy = 5;
rightPanel.add(tweetTitleLabel, gbc);
gbc.gridx = 0;
gbc.gridy = 6;
rightPanel.add(profileTweet1, gbc);
gbc.gridx = 0;
gbc.gridy = 7;
rightPanel.add(profileTweet2, gbc);
gbc.gridx = 0;
gbc.gridy = 8;
rightPanel.add(profileTweet3, gbc);
gbc.gridx = 0;
gbc.gridy = 9;
rightPanel.add(profileTweet4, gbc);
rightPanel.setBorder(newTitledBorder(newEtchedBorder(), "Profile")); // set border of entire right panel, make it visible (etched)
overallPanel.add(rightPanel, BorderLayout.EAST); // add rightPanel to overallPanel on the right (east) side
}
privatevoiddisplayTweet(TweetDatat, JPanelp, GridBagConstraintsconstraints, JPanelprofilePanel) {
JPaneltweetPanel = newJPanel(newGridBagLayout()); // declare and initialize new JPanel to hold components for tweet and its data
JPaneltweetTextPanel = newJPanel(newGridBagLayout()); // declare and initialize new JPanel specifically to hold text and username of Tweet
GridBagConstraintstextConstraints = newGridBagConstraints();
textConstraints.anchor = GridBagConstraints.WEST;
textConstraints.weightx = 1;
GridBagConstraintsgbc = newGridBagConstraints();//in order to lay the tweets correctly, we use GridBagLayout to fix spacing between text and images
JLabeluserIconImage = newJLabel(); // declare and initialize new JLabel to hold account avatar of user
ImageIconuImageIcon = t.getUserIcon();
userIconImage.setIcon(uImageIcon);
JLabeluserHandleLabel = newJLabel(t.getUserHandle()); // declare and initialize new JLabel to display user's handle
userHandleLabel.setFont(defaultUIFontBold);
intspaces2add = 110 - (t.getTweetText().length());
StringtweetText = t.getTweetText();
booleansecondLine = false;
StringsecondText = "";
if(tweetText.length() > 120) {
secondText = tweetText.substring(105);
tweetText = tweetText.substring(0,105);
secondLine = true;
}
elsesecondLine = false;
JLabeltweetTextLabel = newJLabel(tweetText); // declare and initialize new JLabel with tweetText passed in as a parameter in the JLabel constructor
tweetTextLabel.setFont(defaultUIFont);
JLabelsecondTextLabel = newJLabel(secondText);
secondTextLabel.setFont(defaultUIFont);
// add user handle and tweet text itself to the tweetTextPanel within a panel
textConstraints.gridx = 0;
textConstraints.gridy = 0;
tweetTextPanel.add(userHandleLabel, textConstraints);
textConstraints.gridx = 0;
textConstraints.gridy = 1;
tweetTextPanel.add(tweetTextLabel, textConstraints);
if(secondLine == true) {
textConstraints.gridx = 0;
textConstraints.gridy = 2;
tweetTextPanel.add(secondTextLabel, textConstraints);
}
// Retweet and Favorite buttons: declare, initialize, set icons, and add to dedicated actionPanel
JLabelretweetLabel = newJLabel();
retweetLabel.setIcon(retweet);
classRetweetListenerimplementsMouseListener {
// implementations of mouseClicked, mouseEntered, mouseExited, mousePressed, mouseReleased methods defined in MouseListener interface
publicvoidmouseClicked (MouseEvente) {
}
publicvoidmouseEntered (MouseEvente) {
retweetLabel.setIcon(retweetHover);
}
publicvoidmouseExited (MouseEvente) {
retweetLabel.setIcon(retweet);
}
publicvoidmousePressed (MouseEvente) {
// retweet logic
try {
twitter.retweetStatus(t.getID()); // invoke retweetStatus on twitter object reference, pass in ID of the tweet as parameter
System.out.println("Successfully retweeted");
}
catch (TwitterExceptionte) {}
}
publicvoidmouseReleased (MouseEvente) {
}
}
JLabelfavoriteLabel = newJLabel();
favoriteLabel.setIcon(favoritePic);
classFavoriteListenerimplementsMouseListener {
// implementations of mouseClicked, mouseEntered, mouseExited, mousePressed, mouseReleased methods defined in MouseListener interface
publicvoidmouseClicked (MouseEvente) {
}
publicvoidmouseEntered (MouseEvente) {
favoriteLabel.setIcon(favoriteHover);
}
publicvoidmouseExited (MouseEvente) {
favoriteLabel.setIcon(favoritePic);
}
publicvoidmousePressed (MouseEvente) {
// favorite logic
try {
StatusrT = twitter.createFavorite(t.getID()); // invoke createFavorite, pass in ID of the tweet as parameter
System.out.println("Successfully favorited");
}
catch (TwitterExceptionte) {}
}
publicvoidmouseReleased (MouseEvente) {
}
}
retweetLabel.addMouseListener(newRetweetListener());
favoriteLabel.addMouseListener(newFavoriteListener());
JPanelactionPanel = newJPanel(newGridBagLayout());
GridBagConstraintsactionConstraints = newGridBagConstraints();
actionConstraints.anchor = GridBagConstraints.WEST;
actionConstraints.gridx = 0;
actionConstraints.gridy = 0;
actionPanel.add(favoriteLabel, actionConstraints);
actionConstraints.gridx = 1;
actionConstraints.gridy = 0;
actionPanel.add(retweetLabel, actionConstraints);
if(secondLine == true) {
textConstraints.gridx = 0;
textConstraints.gridy = 3;
}
else {
textConstraints.gridx = 0;
textConstraints.gridy = 2;
}
tweetTextPanel.add(actionPanel, textConstraints);
gbc.anchor = GridBagConstraints.WEST;//using GridBagContraints values, anchors the text and images in the Layout
gbc.gridx = 0;//sets the x and y of the grid to 0,0 the upper left corner
gbc.gridy = 0;
tweetPanel.add(userIconImage, gbc);//adds profile pic to the position in the gridbag layout
gbc.gridx = 1;//sets the x and y to 1,0 in the upper right corner
gbc.gridy = 0;
gbc.gridwidth = 4;//sets the width of this to 4, 4 times that of the picture
tweetPanel.add(tweetTextPanel, gbc);//adds the text for the tweet in the upper right in the gridbaglayout
p.add(tweetPanel, constraints);
classUserLabelListenerimplementsMouseListener {
// implementations of mouseClicked, mouseEntered, mouseExited, mousePressed, mouseReleased methods defined in MouseListener interface
publicvoidmouseClicked (MouseEvente) {
}
publicvoidmouseEntered (MouseEvente) {
}
publicvoidmouseExited (MouseEvente) {
}
publicvoidmousePressed (MouseEvente) {
// when mouse is pressed on component, it will take that tweet's user and display basic info on the profile view on the right side
// updates components of Profile view with info about user
profileViewUserAccountImage.setIcon(t.getUserIcon());
profileViewUserHandle.setText(t.getUserHandle());
profileViewNumTweetsLabel.setText(t.getUserNumTweets() + " Tweets");//uses various accessor methods of tweetData to set the text of the labels to those values
profileViewNumFollowersLabel.setText(t.getUserFollowers() + " Followers");
profileViewNumFollowingLabel.setText(t.getUserFollowing() + " Following");
ResponseList<Status> userTweets;
ArrayList<TweetData> userTweetDataList = newArrayList<TweetData>();
try {
userTweets = twitter.getUserTimeline(t.getUserID());
for(inti = 0; i < 4; i++) {
Stringtext = userTweets.get(i).getText();
userTweetDataList.add(newTweetData(t.getUserHandle(), text, t.getUserIcon(), userTweets.get(i).getRetweetCount(), userTweets.get(i).getFavoriteCount(), t.getUserNumTweets(), t.getUserFollowers(), t.getUserFollowing(), t.getID(), t.getUserID()));
}
} catch(TwitterExceptionte) {}
for(inti = 0; i < 4; i++) {
switch(i) {
case0: displayUserTweets(userTweetDataList.get(0), profileTweet1);
case1: displayUserTweets(userTweetDataList.get(1), profileTweet2);
case2: displayUserTweets(userTweetDataList.get(2), profileTweet3);
case3: displayUserTweets(userTweetDataList.get(3), profileTweet4);
default: break;
}
}
}
publicvoidmouseReleased (MouseEvente) {
}
}
// add a new UserLabelListener to both the userHandleLabel and userIconImage (if either is pressed, Profile view on the right will update with info about user)
UserLabelListeneruLL = newUserLabelListener();
userHandleLabel.addMouseListener(uLL);
userIconImage.addMouseListener(uLL);
}
publicImageIconcreateImageIcon (Stringpath) {
URLimgURL = getClass().getResource(path);//constructs a url using the path parameter variable
if (imgURL != null) {
returnnewImageIcon(imgURL);//if the image is not null, a newly contructed ImageIcon with the url as the parameter is returned
} else {
System.err.println("Could not find file: " + path);
returnnull;
}
}
publicvoiddisplayUserTweets(TweetDatat, JLabeltweetTextLabel) {
Stringtext = t.getTweetText();
StringshortText = " '" + text.substring(0,35) + "'";
tweetTextLabel.setText(shortText);
classtweetTextListenerimplementsMouseListener {
publicvoidmouseClicked (MouseEvente) {
}
publicvoidmouseEntered (MouseEvente) {
}
publicvoidmouseExited (MouseEvente) {
}
publicvoidmousePressed (MouseEvente) {
JFrametweetFr = newJFrame();
tweetFr.setSize(825, 100);
JPanelcompleteTweetPanel = newJPanel(newGridBagLayout());
GridBagConstraintscompleteGBC = newGridBagConstraints();
completeGBC.anchor = GridBagConstraints.WEST;
completeGBC.weightx = 1;
completeGBC.insets = newInsets(0,0,0,0);
JLabeluserIcon = newJLabel();
userIcon.setIcon(t.getUserIcon());
completeGBC.gridx = 0;
completeGBC.gridy = 0;
completeTweetPanel.add(userIcon, completeGBC);
JPaneltweetP = newJPanel(newGridBagLayout());
GridBagConstraintsbigPC = newGridBagConstraints();
bigPC.anchor = GridBagConstraints.WEST;
bigPC.weightx = 1;
JLabelhandleLabel = newJLabel(t.getUserHandle());
handleLabel.setFont(defaultUIFontBold);
JLabeltextLabel = newJLabel(t.getTweetText());
textLabel.setFont(defaultUIFont);
JPanelbuttonPanel = newJPanel(newGridBagLayout());
GridBagConstraintsbuttonC = newGridBagConstraints();
buttonC.anchor = GridBagConstraints.WEST;
buttonC.weightx = 1;
ImageIconfavoriteIcon = createImageIcon("heart_button_default.png");
JLabelfavoriteLabel = newJLabel();
favoriteLabel.setIcon(favoriteIcon);
buttonC.gridx = 0;
buttonC.gridy = 0;
buttonPanel.add(favoriteLabel, buttonC);
ImageIconretweetIcon = createImageIcon("retweet_button_default.png");
JLabelretweetLabel = newJLabel();
retweetLabel.setIcon(retweetIcon);
buttonC.gridx = 1;
buttonC.gridy = 0;
buttonPanel.add(retweetLabel, buttonC);
bigPC.gridx = 0;
bigPC.gridy = 0;
tweetP.add(handleLabel, bigPC);
bigPC.gridx = 0;
bigPC.gridy = 1;
tweetP.add(textLabel, bigPC);
bigPC.gridx = 0;
bigPC.gridy = 2;
classretweetLabelListenerimplementsMouseListener {
publicvoidmouseClicked (MouseEvente) {
}
publicvoidmouseEntered (MouseEvente) {
retweetLabel.setIcon(retweetHover);
}
publicvoidmouseExited (MouseEvente) {
retweetLabel.setIcon(retweet);
}
publicvoidmousePressed (MouseEvente) {
// retweet logic
try {
twitter.retweetStatus(t.getID()); // invoke retweetStatus on twitter object reference, pass in ID of the tweet as parameter
System.out.println("Successfully retweeted");
}
catch (TwitterExceptionte) {}
}
publicvoidmouseReleased (MouseEvente) {
}
}
retweetLabel.addMouseListener(newretweetLabelListener());
classfavoriteLabelListenerimplementsMouseListener {
// implementations of mouseClicked, mouseEntered, mouseExited, mousePressed, mouseReleased methods defined in MouseListener interface
publicvoidmouseClicked (MouseEvente) {
}
publicvoidmouseEntered (MouseEvente) {
favoriteLabel.setIcon(favoriteHover);
}
publicvoidmouseExited (MouseEvente) {
favoriteLabel.setIcon(favoritePic);
}
publicvoidmousePressed (MouseEvente) {
// favorite logic
try {
StatusrT = twitter.createFavorite(t.getID()); // invoke createFavorite, pass in ID of the tweet as parameter
System.out.println("Successfully favorited");
}
catch (TwitterExceptionte) {}
}
publicvoidmouseReleased (MouseEvente) {
}
}
favoriteLabel.addMouseListener(newfavoriteLabelListener());
tweetP.add(buttonPanel, bigPC);
completeGBC.gridx = 1;
completeGBC.gridy = 0;
completeTweetPanel.add(tweetP, completeGBC);
tweetFr.add(completeTweetPanel);
completeTweetPanel.setBorder(newTitledBorder(newEtchedBorder(), t.getUserHandle() + "'s tweet"));
tweetFr.setVisible(true);
}
publicvoidmouseReleased (MouseEvente) {
}
}
tweetTextLabel.addMouseListener(newtweetTextListener());
}
}