Commit ce5a2b11 authored by Stephen's avatar Stephen
Browse files
parents 02c85311 ac1a8519
......@@ -3,8 +3,12 @@
<editor-bookmarks xmlns="http://www.netbeans.org/ns/editor-bookmarks/2" lastBookmarkId="0"/>
<open-files xmlns="http://www.netbeans.org/ns/projectui-open-files/2">
<group>
<file>file:/C:/Users/Stephen/Documents/NetBeansProjects/TubesSister/src/client/src/tcpclient/MainPage.java</file>
<file>file:/C:/Users/Stephen/Documents/NetBeansProjects/TubesSister/src/client/src/tcpclient/TCPClient.java</file>
<file>file:/C:/Users/susanti_2/Documents/ITB/IF/netbeans/IF3230-Tugas-Besar-Sister-2015/src/client/src/tcpclient/ButtonRenderer.java</file>
<file>file:/C:/Users/susanti_2/Documents/ITB/IF/netbeans/IF3230-Tugas-Besar-Sister-2015/src/client/src/tcpclient/ButtonEditor.java</file>
<file>file:/C:/Users/susanti_2/Documents/ITB/IF/netbeans/IF3230-Tugas-Besar-Sister-2015/src/client/src/tcpclient/MapTableModel.java</file>
<file>file:/C:/Users/susanti_2/Documents/ITB/IF/netbeans/IF3230-Tugas-Besar-Sister-2015/src/client/src/tcpclient/TradeboxTableModel.java</file>
<file>file:/C:/Users/susanti_2/Documents/ITB/IF/netbeans/IF3230-Tugas-Besar-Sister-2015/src/client/src/tcpclient/JTableButtonMouseListener.java</file>
<file>file:/C:/Users/susanti_2/Documents/ITB/IF/netbeans/IF3230-Tugas-Besar-Sister-2015/src/client/src/tcpclient/MainPage.java</file>
</group>
</open-files>
</project-private>
package tcpclient;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.table.*;
/**
* The ButtonColumn class provides a renderer and an editor that looks like a
* JButton. The renderer and editor will then be used for a specified column
* in the table. The TableModel will contain the String to be displayed on
* the button.
*
* The button can be invoked by a mouse click or by pressing the space bar
* when the cell has focus. Optionally a mnemonic can be set to invoke the
* button. When the button is invoked the provided Action is invoked. The
* source of the Action will be the table. The action command will contain
* the model row number of the button that was clicked.
*
*/
public class ButtonColumn extends AbstractCellEditor
implements TableCellRenderer, TableCellEditor, ActionListener, MouseListener
{
private JTable table;
private Action action;
private int mnemonic;
private Border originalBorder;
private Border focusBorder;
private JButton renderButton;
private JButton editButton;
private Object editorValue;
private boolean isButtonColumnEditor;
/**
* Create the ButtonColumn to be used as a renderer and editor. The
* renderer and editor will automatically be installed on the TableColumn
* of the specified column.
*
* @param table the table containing the button renderer/editor
* @param action the Action to be invoked when the button is invoked
* @param column the column to which the button renderer/editor is added
*/
public ButtonColumn(JTable table, Action action, int column)
{
this.table = table;
this.action = action;
renderButton = new JButton();
editButton = new JButton();
editButton.setFocusPainted( false );
editButton.addActionListener( this );
originalBorder = editButton.getBorder();
setFocusBorder( new LineBorder(Color.BLUE) );
TableColumnModel columnModel = table.getColumnModel();
columnModel.getColumn(column).setCellRenderer( this );
columnModel.getColumn(column).setCellEditor( this );
table.addMouseListener( this );
}
/**
* Get foreground color of the button when the cell has focus
*
* @return the foreground color
*/
public Border getFocusBorder()
{
return focusBorder;
}
/**
* The foreground color of the button when the cell has focus
*
* @param focusBorder the foreground color
*/
public void setFocusBorder(Border focusBorder)
{
this.focusBorder = focusBorder;
editButton.setBorder( focusBorder );
}
public int getMnemonic()
{
return mnemonic;
}
/**
* The mnemonic to activate the button when the cell has focus
*
* @param mnemonic the mnemonic
*/
public void setMnemonic(int mnemonic)
{
this.mnemonic = mnemonic;
renderButton.setMnemonic(mnemonic);
editButton.setMnemonic(mnemonic);
}
@Override
public Component getTableCellEditorComponent(
JTable table, Object value, boolean isSelected, int row, int column)
{
if (value == null)
{
editButton.setText( "" );
editButton.setIcon( null );
}
else if (value instanceof Icon)
{
editButton.setText( "" );
editButton.setIcon( (Icon)value );
}
else
{
editButton.setText( value.toString() );
editButton.setIcon( null );
}
this.editorValue = value;
return editButton;
}
@Override
public Object getCellEditorValue()
{
return editorValue;
}
//
// Implement TableCellRenderer interface
//
public Component getTableCellRendererComponent(
JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column)
{
if (isSelected)
{
renderButton.setForeground(table.getSelectionForeground());
renderButton.setBackground(table.getSelectionBackground());
}
else
{
renderButton.setForeground(table.getForeground());
renderButton.setBackground(UIManager.getColor("Button.background"));
}
if (hasFocus)
{
renderButton.setBorder( focusBorder );
}
else
{
renderButton.setBorder( originalBorder );
}
// renderButton.setText( (value == null) ? "" : value.toString() );
if (value == null)
{
renderButton.setText( "" );
renderButton.setIcon( null );
}
else if (value instanceof Icon)
{
renderButton.setText( "" );
renderButton.setIcon( (Icon)value );
}
else
{
renderButton.setText( value.toString() );
renderButton.setIcon( null );
}
return renderButton;
}
//
// Implement ActionListener interface
//
/*
* The button has been pressed. Stop editing and invoke the custom Action
*/
public void actionPerformed(ActionEvent e)
{
int row = table.convertRowIndexToModel( table.getEditingRow() );
fireEditingStopped();
// Invoke the Action
ActionEvent event = new ActionEvent(
table,
ActionEvent.ACTION_PERFORMED,
"" + row);
action.actionPerformed(event);
}
//
// Implement MouseListener interface
//
/*
* When the mouse is pressed the editor is invoked. If you then then drag
* the mouse to another cell before releasing it, the editor is still
* active. Make sure editing is stopped when the mouse is released.
*/
public void mousePressed(MouseEvent e)
{
if (table.isEditing()
&& table.getCellEditor() == this)
isButtonColumnEditor = true;
}
public void mouseReleased(MouseEvent e)
{
if (isButtonColumnEditor
&& table.isEditing())
table.getCellEditor().stopCellEditing();
isButtonColumnEditor = false;
}
public void mouseClicked(MouseEvent e) {}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
}
......@@ -1323,6 +1323,7 @@
<Property name="columnModel" type="javax.swing.table.TableColumnModel" editor="org.netbeans.modules.form.editors2.TableColumnModelEditor">
<TableColumnModel selectionModel="0"/>
</Property>
<Property name="rowSelectionAllowed" type="boolean" value="false"/>
<Property name="tableHeader" type="javax.swing.table.JTableHeader" editor="org.netbeans.modules.form.editors2.JTableHeaderEditor">
<TableHeader reorderingAllowed="true" resizingAllowed="true"/>
</Property>
......
......@@ -7,6 +7,9 @@ import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.text.DateFormat;
......@@ -19,9 +22,12 @@ import java.text.DateFormat;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
......@@ -29,11 +35,13 @@ import javax.swing.Timer;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.JTableHeader;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;
import javax.swing.table.TableColumnModel;
import javax.swing.table.TableModel;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import sun.swing.ImageIconUIResource;
......@@ -1042,6 +1050,7 @@ public class MainPage extends javax.swing.JFrame {
MainPanel.add(mixitemPanel, "MixItemCard");
tradeboxTable.setModel(tradeboxModel);
tradeboxTable.setRowSelectionAllowed(false);
tradeboxScrollPane.setViewportView(tradeboxTable);
javax.swing.GroupLayout tradeboxPanelLayout = new javax.swing.GroupLayout(tradeboxPanel);
......@@ -2273,17 +2282,13 @@ public class MainPage extends javax.swing.JFrame {
showMessage("Map Error. Try again later..");
}
} catch (Exception ex) {
showMessage("Server map does not respond. Please try again later..");
showMessage("Server does not respond. Please try again later..");
System.out.println("Map Exception");
Logger.getLogger(MainPage.class.getName()).log(Level.SEVERE, null, ex);
}
}
JButton[] buttons;
private void generateTradebox(){
System.out.println("masuk generate tradebox");
try {
JSONObject response = client.tradebox();
if(response.getString("status").equals("ok")) {
......@@ -2293,55 +2298,52 @@ public class MainPage extends javax.swing.JFrame {
int dimY=5;
tradeboxModel=new TradeboxTableModel(dimX, dimY);
buttons= new JButton[dimX];
for(int i=0;i<offer.length();i++)
{
ImageIcon newPlayerIcon = getImage(offer.getJSONArray(i).getInt(0), 100, 100);
tradeboxModel.setValueAt(newPlayerIcon, i, 0);
tradeboxModel.setValueAt(offer.getJSONArray(i).get(1), i, 1);
newPlayerIcon = getImage(offer.getJSONArray(i).getInt(2), 100, 100);
tradeboxModel.setValueAt(newPlayerIcon, i, 2);
tradeboxModel.setValueAt(offer.getJSONArray(i).get(3), i, 3);
if(offer.getJSONArray(i).get(4).toString().equals("true")) //avalaible
{
buttons[i] = new JButton("Cancel");
String offerToken=offer.getJSONArray(i).get(4).toString();
buttons[i].setActionCommand(offerToken);
buttons[i].addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String offerToken=e.getActionCommand();
cancelOffer(offerToken);
}
});
if(offer.getJSONArray(i).get(4).toString().equals("true")){ //avalaible
tradeboxModel.setValueAt("Cancel", i, 4);
}else{
buttons[i] = new JButton("Fetch");
String offerToken=offer.getJSONArray(i).get(4).toString();
buttons[i].setActionCommand(offerToken);
buttons[i].addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String offerToken=e.getActionCommand();
fetchItem(offerToken);
}
});
tradeboxModel.setValueAt("Fetch", i, 4);
}
tradeboxModel.setValueAt(buttons[i], i, 4);
}
tradeboxTable.setModel(tradeboxModel);
tradeboxTable.setRowHeight(100);
Action delete = new AbstractAction()
{
public void actionPerformed(ActionEvent e)
{
JTable table = (JTable)e.getSource();
int modelRow = Integer.valueOf( e.getActionCommand() );
try {
if(offer.getJSONArray(modelRow).get(4).toString().equals("true"))
{
String offerToken=offer.getJSONArray(modelRow).get(5).toString();
cancelOffer(offerToken);
}else{
String offerToken=offer.getJSONArray(modelRow).get(5).toString();
fetchItem(offerToken);
}
} catch (JSONException ex) {
System.out.println("mouse click json Exception");
Logger.getLogger(MainPage.class.getName()).log(Level.SEVERE, null, ex);
}
}
};
ButtonColumn buttonColumn = new ButtonColumn(tradeboxTable, delete, 4);
buttonColumn.setMnemonic(KeyEvent.VK_D);
} else {
showMessage("Tradebox Error. Try again later..");
}
} catch (Exception ex) {
showMessage("Server tradebox does not respond. Please try again later..");
showMessage("Server does not respond. Please try again later..");
System.out.println("Tradebox Exception");
Logger.getLogger(MainPage.class.getName()).log(Level.SEVERE, null, ex);
}
......@@ -2372,17 +2374,47 @@ public class MainPage extends javax.swing.JFrame {
}
private void fetchItem(String offerToken)
{
try {
JSONObject response = client.fetchItem(offerToken);
if(response.getString("status").equals("ok")){
showMessage("Success!");
generateTradebox();
}else if(response.getString("status").equals("fail")){
showMessage(response.getString("description"));
}else{
showMessage(response.getString("status"));
}
} catch (Exception ex) {
showMessage("Server does not respond. Please try again later..");
System.out.println("Fetch Item Exception");
Logger.getLogger(MainPage.class.getName()).log(Level.SEVERE, null, ex);
}
}
private void cancelOffer(String offerToken)
{
try {
JSONObject response = client.cancelOffer(offerToken);
if(response.getString("status").equals("ok")){
showMessage("Cancel offer success!");
generateTradebox();
}else if(response.getString("status").equals("fail")){
showMessage(response.getString("description"));
}else{
showMessage(response.getString("status"));
}
} catch (Exception ex) {
showMessage("Server does not respond. Please try again later..");
System.out.println("Cancel Offer Exception");
Logger.getLogger(MainPage.class.getName()).log(Level.SEVERE, null, ex);
}
}
/**
* @param args the command line arguments
*/
......
......@@ -572,7 +572,7 @@ public class TCPClient {
closeConnection();
return responseJSON;
}
public JSONObject accept(String token,String offer_token) throws Exception
{
openConnection();
......@@ -626,7 +626,7 @@ public class TCPClient {
return responseJSON;
}
public JSONObject fetchitem(String token, String offer_token) throws Exception
public JSONObject fetchItem(String offer_token) throws Exception
{
openConnection();
System.out.println("Fetch item");
......@@ -653,7 +653,7 @@ public class TCPClient {
return responseJSON;
}
public JSONObject canceloffer(String token, String offer_token) throws Exception
public JSONObject cancelOffer(String offer_token) throws Exception
{
openConnection();
System.out.println("Cancel Offer");
......@@ -684,107 +684,6 @@ public class TCPClient {
{
TCPClient client = new TCPClient("localhost", 6789);
// do {
// BufferedReader inFromUser = new BufferedReader( new InputStreamReader(System.in));
// DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());
// BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
//
// // Test Register
// do {
// System.out.println("Register");
// System.out.print("Username : "); username = inFromUser.readLine();
// System.out.print("Password : "); password = inFromUser.readLine();
// String toSend = registerJSON(username, password).toString();
//
// outToServer.write(toSend.getBytes("UTF-8"));
//
// String response = inFromServer.readLine();
// JSONObject responseJSON = new JSONObject(response);
// responseStatus = responseJSON.getString("status");
// if(responseStatus.equalsIgnoreCase("ok")) {
// System.out.println("Registration Success!");
// } else if(responseStatus.equalsIgnoreCase("fail")) {
// String reason = responseJSON.getString("description");
// System.out.println("Registration fail, " + reason);
// }
// System.out.println();
// } while(!responseStatus.equalsIgnoreCase("ok"));
//
// // Test Login
// do {
// System.out.println("Login");
// System.out.print("Username : "); username = inFromUser.readLine();
// System.out.print("Password : "); password = inFromUser.readLine();
// String toSend = loginJSON(username, password).toString();
//
// outToServer.write(toSend.getBytes("UTF-8"));
//
// String response = inFromServer.readLine();
// JSONObject responseJSON = new JSONObject(response);
// responseStatus = responseJSON.getString("status");
// if(responseStatus.equalsIgnoreCase("ok")) {
// System.out.println("Login Success!");
// System.out.println("Token : " + responseJSON.getString("token"));
// token = responseJSON.getString("token");
// } else if(responseStatus.equalsIgnoreCase("fail")) {
// String reason = responseJSON.getString("description");
// System.out.println("Login fail, " + reason);
// }
// System.out.println();
// } while(!responseStatus.equalsIgnoreCase("ok"));
//
// // Test Inventory
// do {
// do {
// System.out.print("Command : "); command = inFromUser.readLine();
// } while(!command.equalsIgnoreCase("inventory") && !command.equalsIgnoreCase("exit"));
//
// if(command.equalsIgnoreCase("inventory")) {
// String toSend = inventoryJSON(token).toString();
//
// outToServer.write(toSend.getBytes("UTF-8"));
//
// String response = inFromServer.readLine();
// JSONObject responseJSON = new JSONObject(response);
// responseStatus = responseJSON.getString("status");
// if(responseStatus.equalsIgnoreCase("ok")) {
// System.out.println("Your Inventory : ");
// List inventory = new ArrayList<Integer>();
// JSONArray jsonarray = responseJSON.getJSONArray("inventory");
// for(int i=0; i<jsonarray.length(); i++) {
// inventory.add(jsonarray.getInt(i));
// }
// printItemList(inventory);
// } else if(responseStatus.equalsIgnoreCase("error")) {
// System.out.println("Error opening inventory");
// }
// System.out.println();
// }
// } while(!command.equalsIgnoreCase("exit"));
//
// // Test Mix Item
// do {
// System.out.println("Mix Item");
// System.out.print("Item 1 ID : "); int id1 = Integer.parseInt(inFromUser.readLine());
// System.out.print("Item 2 ID : "); int id2 = Integer.parseInt(inFromUser.readLine());
// String toSend = mixitemJSON(token, id1, id2).toString();
//
// outToServer.write(toSend.getBytes("UTF-8"));
//
// String response = inFromServer.readLine();
// JSONObject responseJSON = new JSONObject(response);
// responseStatus = responseJSON.getString("status");
// if(responseStatus.equalsIgnoreCase("ok")) {
// System.out.println("Mix item succeded, your new item id : " + responseJSON.getInt("item"));
// } else if(responseStatus.equalsIgnoreCase("fail")) {
// System.out.println("Failed to mix item, " + responseJSON.getString("description"));
// } else if(responseStatus.equalsIgnoreCase("error")) {
// System.out.println("Error mixing item");
// }
// System.out.println();
// } while(true);
// } while(!command.equalsIgnoreCase("exit"));
//
JSONObject response = client.login("ayam", "ayam");
if(response.getString("status").equals("ok")) {
client.token = response.getString("token");
......
package tcpclient;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableModel;
public class TradeboxTableModel extends AbstractTableModel {
public class TradeboxTableModel extends DefaultTableModel {
private final String[] header={"Offered Item", "Number Offered", "Demanded Item", "Number Demanded", "Act"};
private final Class[] m_colTypes = { ImageIcon.class, Integer.class, ImageIcon.class, Integer.class, JButton.class };
......@@ -49,7 +52,30 @@ public class TradeboxTableModel extends AbstractTableModel {
@Override
public Object getValueAt(int rowIndex, int columnIndex) {
return data[rowIndex][columnIndex];
switch (columnIndex) {
/*Adding button and creating click listener*/