|
| 1 | +package Client; |
| 2 | + |
| 3 | +import java.io.IOException; |
| 4 | +import java.text.SimpleDateFormat; |
| 5 | +import java.util.ArrayList; |
| 6 | +import java.util.Calendar; |
| 7 | + |
| 8 | +import org.eclipse.swt.widgets.Display; |
| 9 | +import org.eclipse.swt.widgets.Shell; |
| 10 | + |
| 11 | +import swing2swt.layout.BorderLayout; |
| 12 | + |
| 13 | +import org.eclipse.swt.widgets.Text; |
| 14 | +import org.eclipse.swt.SWT; |
| 15 | +import org.eclipse.swt.widgets.Event; |
| 16 | +import org.eclipse.swt.widgets.List; |
| 17 | +import org.eclipse.swt.widgets.Group; |
| 18 | +import org.eclipse.swt.widgets.Button; |
| 19 | +import org.eclipse.swt.widgets.Listener; |
| 20 | +import org.eclipse.swt.events.SelectionAdapter; |
| 21 | +import org.eclipse.swt.events.SelectionEvent; |
| 22 | + |
| 23 | +import swing2swt.layout.BorderLayout; |
| 24 | + |
| 25 | +import org.eclipse.swt.events.KeyAdapter; |
| 26 | +import org.eclipse.swt.events.KeyEvent; |
| 27 | +import org.eclipse.swt.events.ShellAdapter; |
| 28 | +import org.eclipse.swt.events.ShellEvent; |
| 29 | +import org.eclipse.swt.graphics.Rectangle; |
| 30 | + |
| 31 | +public class ChatRoomGUI { |
| 32 | + |
| 33 | + protected Shell shell; |
| 34 | + private Group group; |
| 35 | + |
| 36 | + private Button btnSend; |
| 37 | + private Text message; |
| 38 | + private Text txtChat; |
| 39 | + private String text = ""; |
| 40 | + private List userList; |
| 41 | + private String selectedRCVRString = ""; |
| 42 | + private static Client client; |
| 43 | + |
| 44 | + private Calendar cal; |
| 45 | + private SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss"); |
| 46 | + |
| 47 | + public ChatRoomGUI(Client c){ |
| 48 | + setClient(c); |
| 49 | + } |
| 50 | + /** |
| 51 | + * Launch the application. |
| 52 | + * @param args |
| 53 | + */ |
| 54 | + |
| 55 | + /** |
| 56 | + * Open the window. |
| 57 | + * @throws IOException |
| 58 | + */ |
| 59 | + /** |
| 60 | + * @wbp.parser.entryPoint |
| 61 | + */ |
| 62 | + public void open() throws IOException { |
| 63 | + Display display = Display.getDefault(); |
| 64 | + createContents(); |
| 65 | + shell.open(); |
| 66 | + shell.layout(); |
| 67 | + while (!shell.isDisposed()) { |
| 68 | + if (!display.readAndDispatch()) { |
| 69 | + display.sleep(); |
| 70 | + } |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * Create contents of the window. |
| 76 | + * @throws IOException |
| 77 | + */ |
| 78 | + protected void createContents() throws IOException { |
| 79 | + shell = new Shell(); |
| 80 | + shell.addShellListener(new ShellAdapter() { |
| 81 | + @Override |
| 82 | + public void shellClosed(ShellEvent e) { |
| 83 | + client.sendEXIT(); |
| 84 | + System.exit(0); |
| 85 | + } |
| 86 | + }); |
| 87 | + shell.setSize(450, 300); |
| 88 | + shell.setText("Username " + "[" + client.getUsername() + "]" + " | Host " + "[" + client.getHost() + "]" + " | Port " + "[" + client.getPortNumber() + "]"); |
| 89 | + shell.setLayout(new BorderLayout(0, 0)); |
| 90 | + |
| 91 | + userList = new List(shell, SWT.BORDER); |
| 92 | + userList.addSelectionListener(new SelectionAdapter() { |
| 93 | + @Override |
| 94 | + public void widgetSelected(SelectionEvent e) { |
| 95 | + int[] selectedItems = userList.getSelectionIndices(); |
| 96 | + for (int loopIndex = 0; loopIndex < selectedItems.length; loopIndex++){ |
| 97 | + selectedRCVRString = userList.getItem(selectedItems[loopIndex]); |
| 98 | + } |
| 99 | + System.out.println("Selected Items: " + selectedRCVRString); |
| 100 | + } |
| 101 | + }); |
| 102 | + userList.setItems(new String[] {}); |
| 103 | + userList.setLayoutData(BorderLayout.EAST); |
| 104 | + |
| 105 | + group = new Group(shell, SWT.NONE); |
| 106 | + group.setLayoutData(BorderLayout.SOUTH); |
| 107 | + group.setLayout(new BorderLayout(0, 0)); |
| 108 | + |
| 109 | + message = new Text(group, SWT.BORDER); |
| 110 | + message.addKeyListener(new KeyAdapter() { |
| 111 | + @Override |
| 112 | + public void keyPressed(KeyEvent e) { |
| 113 | + if(e.keyCode == SWT.CR || e.keyCode == SWT.KEYPAD_CR){ |
| 114 | + client.sendMessage(selectedRCVRString,message.getText()); |
| 115 | + Calendar cal = Calendar.getInstance(); |
| 116 | + if(selectedRCVRString.equals("") || selectedRCVRString.equals("GLOBAL")){ |
| 117 | + text += "(" + sdf.format(cal.getTime()) + ")" + "[GLOBAL] " + client.getUsername() + ": " + message.getText() + " \n"; |
| 118 | + }else{ |
| 119 | + text += "(" + sdf.format(cal.getTime()) + ")" + "[TO " + "[" + selectedRCVRString + "]" + "] " + client.getUsername() + ": " + message.getText() + " \n"; |
| 120 | + } |
| 121 | + txtChat.setText(text); |
| 122 | + message.setText(""); |
| 123 | + } |
| 124 | + } |
| 125 | + }); |
| 126 | + message.setLayoutData(BorderLayout.CENTER); |
| 127 | + |
| 128 | + btnSend = new Button(group, SWT.NONE); |
| 129 | + btnSend.addSelectionListener(new SelectionAdapter() { |
| 130 | + @Override |
| 131 | + public void widgetSelected(SelectionEvent e) { |
| 132 | + client.sendMessage(selectedRCVRString,message.getText()); |
| 133 | + Calendar cal = Calendar.getInstance(); |
| 134 | + if(selectedRCVRString.equals("") || selectedRCVRString.equals("GLOBAL")){ |
| 135 | + text += "(" + sdf.format(cal.getTime()) + ")" + "[GLOBAL] " + client.getUsername() + ": " + message.getText() + " \n"; |
| 136 | + }else{ |
| 137 | + text += "(" + sdf.format(cal.getTime()) + ")" + "[TO " + "[" + selectedRCVRString + "]" + "] " + client.getUsername() + ": " + message.getText() + " \n"; |
| 138 | + } |
| 139 | + txtChat.setText(text); |
| 140 | + message.setText(""); |
| 141 | + } |
| 142 | + }); |
| 143 | + btnSend.setLayoutData(BorderLayout.EAST); |
| 144 | + btnSend.setText("Send"); |
| 145 | + |
| 146 | + txtChat = new Text(shell, SWT.BORDER | SWT.WRAP | SWT.V_SCROLL | SWT.MULTI); |
| 147 | + txtChat.addListener(SWT.Modify, new Listener(){ |
| 148 | + public void handleEvent(Event e){ |
| 149 | + txtChat.setTopIndex(txtChat.getLineCount() - 1); |
| 150 | + } |
| 151 | + }); |
| 152 | + txtChat.setText(text); |
| 153 | + txtChat.setEditable(false); |
| 154 | + txtChat.setLayoutData(BorderLayout.CENTER); |
| 155 | + |
| 156 | + } |
| 157 | + |
| 158 | + public void addUserList(final ArrayList<String> users){ |
| 159 | + new Thread(new Runnable() { |
| 160 | + public void run() { |
| 161 | + Display.getDefault().asyncExec(new Runnable() { |
| 162 | + public void run() { |
| 163 | + try{ |
| 164 | + userList.add(""); |
| 165 | + removeAllUsernames(); |
| 166 | + for (int i = 0; i < users.size(); i++) { |
| 167 | + userList.add(users.get(i)); |
| 168 | + } |
| 169 | + shell.layout(); |
| 170 | + }catch(Exception e){ |
| 171 | + e.printStackTrace(); |
| 172 | + } |
| 173 | + } |
| 174 | + }); |
| 175 | + |
| 176 | + } |
| 177 | + }).start(); |
| 178 | + } |
| 179 | + |
| 180 | + public void updateChat(final String serverMSG){ |
| 181 | + new Thread(new Runnable() { |
| 182 | + public void run() { |
| 183 | + Display.getDefault().asyncExec(new Runnable() { |
| 184 | + public void run() { |
| 185 | + try{ |
| 186 | + text += serverMSG +" \n"; |
| 187 | + txtChat.setText(text); |
| 188 | + shell.layout(); |
| 189 | + }catch(Exception e){ |
| 190 | + e.printStackTrace(); |
| 191 | + } |
| 192 | + } |
| 193 | + }); |
| 194 | + } |
| 195 | + }).start(); |
| 196 | + } |
| 197 | + |
| 198 | + private void removeAllUsernames(){ |
| 199 | + userList.removeAll(); |
| 200 | + userList.add("GLOBAL"); |
| 201 | + } |
| 202 | + |
| 203 | + public static Client getClient() { |
| 204 | + return client; |
| 205 | + } |
| 206 | + |
| 207 | + public static void setClient(Client client) { |
| 208 | + ChatRoomGUI.client = client; |
| 209 | + } |
| 210 | + |
| 211 | + |
| 212 | +} |
0 commit comments