001package csheets.ext.share.ui; 002 003import java.awt.GridLayout; 004import java.awt.event.*; 005import java.util.*; 006 007import javax.swing.*; 008import javax.swing.border.TitledBorder; 009 010import csheets.ext.Extension; 011import csheets.ext.share.SharingExtension; 012import csheets.ext.share.controller.ReceiveController; 013import csheets.ext.share.core.*; 014import csheets.ui.ctrl.UIController; 015import csheets.ui.ext.UIExtension; 016 017/** 018 * This class implements the UI interface extension for the sharing extension. 019 * 020 * @see UIExtension 021 * @author Andre 022 * 023 */ 024public class UISharingExtension extends UIExtension implements Observer { 025 026 /** The sidebar of the extension */ 027 private JComponent sidebar; 028 029 /** The menu of the extension */ 030 private SharingMenu menu; 031 032 /** The sharing extension */ 033 private final UISharingExtension uiShare = this; 034 035 /** 036 * Creates a new UI for sharing extension 037 * 038 * @param extension 039 * the sharing extension 040 * @param uiController 041 * the user interface controller 042 */ 043 public UISharingExtension(Extension extension, UIController uiController) { 044 super(extension, uiController); 045 } 046 047 @Override 048 public JMenu getMenu() { 049 if (menu == null) 050 menu = new SharingMenu(uiController); 051 052 return menu; 053 } 054 055 @SuppressWarnings({ "rawtypes", "unchecked" }) 056 @Override 057 public JComponent getSideBar() { 058 if (sidebar == null) { 059 sidebar = new JPanel(new GridLayout(2, 1)); 060 sidebar.setName(SharingExtension.NAME); 061 062 // Creates components 063 064 JPanel sendPanel = new JPanel(); 065 JLabel sendStaticPort = new JLabel("Port"); 066 final JTextField sendPort = new JTextField(10); 067 JLabel sendPass = new JLabel("Pass"); 068 final JPasswordField pass = new JPasswordField(10); 069 String[] propsChoose = new String[] { "Writable", "Read-Only" }; 070 final JComboBox comboProps = new JComboBox(propsChoose); 071 JLabel labelProp = new JLabel("Writable/Read-Only"); 072 JButton sendAction = new JButton("Send"); 073 sendAction.addActionListener(new ActionListener() { 074 075 @Override 076 public void actionPerformed(ActionEvent e) { 077 String propsTemp = (String) comboProps.getSelectedItem(); 078 String props = "r"; 079 if (propsTemp.equals("Writable")) { 080 props = "wr"; 081 082 } 083 String portTemp = sendPort.getText(); 084 if (Validate.checkIfANumber(portTemp)) { 085 int port = Integer.parseInt(portTemp); 086 if (Validate.checkPort(port)) { 087 SendAction.getInstance().clickOnSidebar( 088 port, 089 Validate.encrypt(String.copyValueOf( 090 pass.getPassword()).getBytes()), 091 props, uiShare); 092 } else { 093 JOptionPane.showMessageDialog(null, 094 "Check if port is between 49152 and 65535", 095 "Port Error", JOptionPane.ERROR_MESSAGE); 096 } 097 } else { 098 JOptionPane.showMessageDialog(null, 099 "Inserted port is not a number", "Port Error", 100 JOptionPane.ERROR_MESSAGE); 101 } 102 } 103 }); 104 sendPanel.add(sendStaticPort); 105 sendPanel.add(sendPort); 106 sendPanel.add(sendPass); 107 sendPanel.add(pass); 108 sendPanel.add(labelProp); 109 sendPanel.add(comboProps); 110 sendPanel.add(sendAction); 111 112 JPanel receivePanel = new JPanel(); 113 JLabel recStaticIP = new JLabel("IP"); 114 final JTextField recIP = new JTextField(10); 115 JLabel recStaticPort = new JLabel("Port"); 116 final JTextField recPort = new JTextField(10); 117 final JPasswordField passReceive = new JPasswordField(10); 118 JLabel passLabel = new JLabel("Password"); 119 JButton recAction = new JButton("Receive"); 120 recAction.addActionListener(new ActionListener() { 121 122 @Override 123 public void actionPerformed(ActionEvent e) { 124 String portTemp = recPort.getText(); 125 String passwordReceive; 126 if (Validate.checkIfANumber(portTemp)) { 127 int port = Integer.parseInt(portTemp); 128 if (Validate.checkPort(port)) { 129 String IP = recIP.getText(); 130 if (Validate.checkIFIPIsCorrect(IP)) { 131 passwordReceive = new String(passReceive 132 .getPassword()); 133 ReceiveAction.getInstance().clickOnSidebar( 134 IP, 135 port, 136 Validate.encrypt(passwordReceive 137 .getBytes()), uiShare); 138 } else { 139 JOptionPane 140 .showMessageDialog( 141 null, 142 "Insert in IPv4 format, or use localhost", 143 "IP Error", 144 JOptionPane.ERROR_MESSAGE); 145 } 146 147 } else { 148 JOptionPane.showMessageDialog(null, 149 "Check if port is between 49152 and 65535", 150 "Port Error", JOptionPane.ERROR_MESSAGE); 151 } 152 153 } else { 154 JOptionPane.showMessageDialog(null, 155 "Inserted port is not a number", "Port Error", 156 JOptionPane.ERROR_MESSAGE); 157 } 158 } 159 }); 160 161 JButton recDiscoverServer = new JButton("Discover Servers"); 162 recDiscoverServer.addActionListener(new ActionListener() { 163 164 @Override 165 public void actionPerformed(ActionEvent e) { 166 ReceiveController rec = new ReceiveController(); 167 List<Connections> connections = rec.findServers(uiShare); 168 String passwordDiscover = null; 169 170 if (connections.size() > 0) { 171 172 Object[] objs = new Object[connections.size()]; 173 for (int i = 0; (i < connections.size()); i++) { 174 objs[i] = connections.get(i); 175 } 176 Connections con = (Connections) JOptionPane 177 .showInputDialog(sidebar, "Select a server", 178 "Server Discover", 179 JOptionPane.PLAIN_MESSAGE, null, objs, 180 ""); 181 182 if (con != null) { 183 passwordDiscover = passwordField(); 184 if (passwordDiscover != null) { 185 ReceiveAction.getInstance().clickOnSidebar( 186 con, 187 uiShare, 188 Validate.encrypt(passwordDiscover 189 .getBytes())); 190 } 191 } 192 } else { 193 JOptionPane.showMessageDialog(sidebar, 194 "Servers not found"); 195 } 196 197 } 198 }); 199 200 receivePanel.add(recStaticIP); 201 receivePanel.add(recIP); 202 receivePanel.add(recStaticPort); 203 receivePanel.add(recPort); 204 receivePanel.add(passLabel); 205 receivePanel.add(passReceive); 206 receivePanel.add(recAction); 207 receivePanel.add(recDiscoverServer); 208 209 // Adds borders 210 TitledBorder border = BorderFactory.createTitledBorder("Receiving"); 211 border.setTitleJustification(TitledBorder.CENTER); 212 receivePanel.setBorder(border); 213 border = BorderFactory.createTitledBorder("Sending"); 214 border.setTitleJustification(TitledBorder.CENTER); 215 sendPanel.setBorder(border); 216 217 // Creates side bar 218 sidebar.add(sendPanel); 219 sidebar.add(receivePanel); 220 } 221 return sidebar; 222 } 223 224 @Override 225 public void update(Observable o, Object arg) { 226 JOptionPane.showMessageDialog(null, "Connection Error"); 227 } 228 229 /** 230 * Creates a new dialog with password 231 * 232 * @return the password 233 */ 234 public String passwordField() { 235 char[] password = null; 236 JPanel panel = new JPanel(); 237 JLabel label = new JLabel("Enter a password:"); 238 JPasswordField pass = new JPasswordField(10); 239 panel.add(label); 240 panel.add(pass); 241 String[] options = new String[] { "OK", "Cancel" }; 242 int option = JOptionPane.showOptionDialog(null, panel, "The title", 243 JOptionPane.NO_OPTION, JOptionPane.PLAIN_MESSAGE, null, 244 options, options[1]); 245 if (option == 0) { 246 password = pass.getPassword(); 247 } 248 249 return new String(password); 250 } 251}