Commit df709e5a authored by Susanti Gojali's avatar Susanti Gojali
Browse files

button tradebox done

parent d38bec03
......@@ -3,15 +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>
<<<<<<< HEAD
<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>
<file>file:/C:/Users/susanti_2/Documents/ITB/IF/netbeans/IF3230-Tugas-Besar-Sister-2015/src/client/src/tcpclient/TCPClient.java</file>
=======
<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/Stephen/Documents/NetBeansProjects/TubesSister/src/client/src/tcpclient/LoginPage.java</file>
>>>>>>> 7d12ee92a34d06c0f223c1804ac375528b1b069d
</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);
......@@ -2279,8 +2288,6 @@ public class MainPage extends javax.swing.JFrame {
}
}
JButton[] buttons;
private void generateTradebox(){
System.out.println("masuk generate tradebox");
......@@ -2293,8 +2300,6 @@ 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);
......@@ -2309,33 +2314,39 @@ public class MainPage extends javax.swing.JFrame {
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);
}
});
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..");
......@@ -2376,14 +2387,40 @@ 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");
}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!");
}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);
}
}
/**
* @param args the command line arguments
*/
......
......@@ -522,7 +522,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");
......@@ -547,7 +547,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");
......
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*/
// case 4: System.out.println(data[rowIndex][columnIndex].getClass().getName());
// final JButton button = new JButton(data[rowIndex][columnIndex].toString());
// button.addActionListener(new ActionListener() {
// public void actionPerformed(ActionEvent arg0) {
// System.out.println("haha");
// JOptionPane.showMessageDialog(JOptionPane.getFrameForComponent(button),
// "Button clicked for row "+rowIndex);
// }
// });
// return button;
default: return data[rowIndex][columnIndex];
}
// switch(columnIndex)
// {
// case 4:
// return ;
// default:
// return data[rowIndex][columnIndex];
// }
}
public void setValueAt(Object value, int row, int col) {
......@@ -59,6 +85,7 @@ public class TradeboxTableModel extends AbstractTableModel {
@Override
public Class<?> getColumnClass(int column) {
switch (column) {
case 0:
return ImageIcon.class;
......
......@@ -33,7 +33,7 @@ user.properties.file=C:\\Users\\susanti_2\\AppData\\Roaming\\NetBeans\\8.0.2\\bu
user.properties.file=C:\\Users\\susanti_2\\AppData\\Roaming\\NetBeans\\8.0.2\\build.properties
>>>>>>> f5cc2615451f416ad94e5b85c35aa84513a123d3
=======
user.properties.file=C:\\Users\\Stephen\\AppData\\Roaming\\NetBeans\\8.0.2\\build.properties
user.properties.file=C:\\Users\\susanti_2\\AppData\\Roaming\\NetBeans\\8.0.2\\build.properties
>>>>>>> 7d12ee92a34d06c0f223c1804ac375528b1b069d
>>>>>>> 9b3bab9e9eefddca873933beff1cff411b869149
>>>>>>> 4425b3f14619792ceebdcdb264cb7c61d1d9d38c
......
......@@ -3,19 +3,7 @@
<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>
<<<<<<< HEAD
<file>file:/C:/Users/susanti_2/Documents/ITB/IF/netbeans/IF3230-Tugas-Besar-Sister-2015/src/server/src/tcpserver/TCPServer.java</file>
=======
<<<<<<< HEAD
<file>file:/C:/Users/Stephen/Documents/NetBeansProjects/TubesSister/src/server/src/tcpserver/TCPServer.java</file>
<file>file:/C:/Users/Stephen/Documents/NetBeansProjects/TubesSister/src/server/src/tcpserver/Host.java</file>
<file>file:/C:/Users/Stephen/Documents/NetBeansProjects/TubesSister/src/server/src/tcpserver/MySQLAccess.java</file>
=======
<file>file:/D:/IF3230-Tugas-Besar-Sister-2015/src/server/map.txt</file>
<file>file:/D:/IF3230-Tugas-Besar-Sister-2015/src/server/src/tcpserver/MySQLAccess.java</file>
<file>file:/D:/IF3230-Tugas-Besar-Sister-2015/src/server/src/tcpserver/TCPServer.java</file>
>>>>>>> f5cc2615451f416ad94e5b85c35aa84513a123d3
>>>>>>> 7d12ee92a34d06c0f223c1804ac375528b1b069d
</group>
</open-files>
</project-private>
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment