Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package controller; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import models.MainComponent; import models.Shared; import models.User; import msgtypes.FriendRequest; import org.apache.commons.lang3.ArrayUtils; import view.AddFriendUI; import view.ChatUI; import view.FriendInfoUI; import view.FriendRequestUI; import view.MainUI; /** * * @author Jonny */ public class FriendController { private MainUI view; private ChatUI chat; private Shared shared; private User[] globalUserlist = null; private User[] personalFriendlist = null; private FriendInfoUI info; private AddFriendUI addView; private FriendController self; private FriendRequestUI friendRequest; public FriendController(MainUI view) { self = this; this.view = view; view.setFriendController(this); shared = Shared.getInstance(); shared.setFriendController(this); shared.getdHandler().requestGlobalUserlist(); shared.getdHandler().requestFriendlist(); view.setUsername(shared.getClient().name); } public void setFriendList() { personalFriendlist = shared.getPersonalFriendlist(); if (personalFriendlist != null) { view.setFriendList(personalFriendlist); } else { System.out.println("ERROR: Personal friendlist not retrieved yet."); } } public void selectedFriend() { User friend = view.getSelectedFriend(); if (friend != null) { view.clear(); view.deselectGamelist(); view.setMainTitle(friend.name); // add more maincomponents here List<MainComponent> comps = new ArrayList<MainComponent>(); chat = new ChatUI(view, friend); chat.setChatController(Shared.getInstance().getChatController()); info = new FriendInfoUI(friend); comps.add(info); comps.add(chat); view.setMainContent((ArrayList<MainComponent>) comps); } } public void addFriend() { if (globalUserlist != null) { addView = new AddFriendUI(view, true, this, globalUserlist); addView.setVisible(true); } else { System.out.println("ERROR: Global userlist not retrieved yet."); } } public void receivedFriendRequest(String requestSender) { if (globalUserlist != null) { User reqUser = null; for (User user : globalUserlist) { if (user.userID.equals(requestSender)) { reqUser = user; } } if (reqUser != null) { friendRequest = new FriendRequestUI(view, true, self, reqUser); friendRequest.setVisible(true); } else { System.out.println("ERROR: Could not find requesting user. - " + requestSender); } } else { System.out.println("ERROR: Global userlist not retrieved yet."); } } public void answerFriendRequest(User requestRecipient, Boolean accepted) { friendRequest.setVisible(false); friendRequest.dispose(); if (accepted) { addFriendToList(requestRecipient.userID); shared.getdHandler().friendRequest(shared.getUserID(), requestRecipient.userID, FriendRequest.Status.ACCEPTED); } else { shared.getdHandler().friendRequest(shared.getUserID(), requestRecipient.userID, FriendRequest.Status.DECLINED); } } public void sendFriendRequest(String requestRecipient) { shared.getdHandler().friendRequest(shared.getUserID(), requestRecipient, FriendRequest.Status.WAITING); addView.setVisible(false); addView.dispose(); } public void addFriendToList(String userID) { if (globalUserlist != null) { User reqUser = null; for (User user : globalUserlist) { if (user.userID.equals(userID)) { reqUser = user; } } if (reqUser != null) { ArrayList<User> userList = new ArrayList<User>(Arrays.asList(personalFriendlist)); userList.add(reqUser); shared.setPersonalFriendlist(userList.toArray(new User[userList.size()])); } else { System.out.println("ERROR: Could not find requesting user. - " + userID); } } else { System.out.println("ERROR: Global userlist not retrieved yet."); } } public void setGlobalUserlist() { this.globalUserlist = shared.getGlobalUserlist(); if (addView != null) addView.setUserList(globalUserlist); } public User[] getFriendList() { return personalFriendlist; } public void refreshUserlist() { shared.getdHandler().requestGlobalUserlist(); } }