Hi All,
I have an requirement wherein i need to count the number of JCheckboxes selected in a JTable column. I am using a separate renderer and editor for doing this.
Following is the code that I am using now. But I am not able to accomplish this. Please help me out.
Thanks,
Kannan
package com.emirates.res.easymars.ui.tools;
import static com.emirates.res.easymars.util.CCGUIUtilities.MAIN_PANEL_BGCOLOR;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.ArrayList;
import java.util.Properties;
import javax.swing.DefaultCellEditor;
import javax.swing.AbstractCellEditor;
import javax.swing.DefaultCellEditor;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JEditorPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingConstants;
import javax.swing.UIManager;
import javax.swing.border.Border;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellEditor;
import javax.swing.table.TableCellRenderer;
import javax.swing.text.html.HTMLEditorKit;
import com.emirates.res.easymars.core.t2se.CCGUIMessageBuilder;
import com.emirates.res.easymars.core.ui.CCGUIComboBox;
import com.emirates.res.easymars.core.ui.CCGUIPanel;
import com.emirates.res.easymars.core.ui.CCGUITable;
import com.emirates.res.easymars.core.ui.CCGUITextField;
import com.emirates.res.easymars.core.ui.CallCentreDialog;
import com.emirates.res.easymars.core.ui.CommandTask;
import com.emirates.res.easymars.core.ui.RequestWorker;
import com.emirates.res.easymars.core.ui.TaskListener;
import com.emirates.res.easymars.core.ui.exception.CCGUIException;
import com.emirates.res.easymars.core.ui.exception.ReLoginException;
import com.emirates.res.easymars.ui.ticketing.CCGUITicketUtilities;
import com.emirates.res.easymars.util.CCGUIUtilities;
public class MultipleTicketsDisplayPanel extends CCGUIPanel implements
CommandTask, TaskListener {
private static GridBagLayout gbl1 = new GridBagLayout();
private static CCGUIComboBox subjectCombo;
private CCGUITextField toTxt;
private static Dimension TABLE_SIZE;
private static CallCentreDialog dialog = null;
private RequestWorker worker;
private CCGUITable multipleTicketsTable = null;
private DefaultTableModel multipleTicketsModel = null;
private CCGUIComboBox languageCombo;
public static
String[] tickets = null;
// Changes start
private static JEditorPane editorPane = new JEditorPane();
private ArrayList<String> eTktNumber = null;
static ArrayList<JEditorPane> arrHTMlEditorPane = new ArrayList<JEditorPane>();
private static JEditorPane htmlEditorPane = new JEditorPane();
// Changes end
private static String newEReceipt;
private String rLoc = null;
private static final String CONTENTTYPE_TEXT_PLAIN = "text/plain";
public static boolean IS_NEW_ERECEIPT_SELECTED = false;
public static boolean IS_MULTIPLE_ERECEIPT_SELECTED = false;
static {
TABLE_SIZE = new Dimension(625, 120);
}
public MultipleTicketsDisplayPanel() {
createComponentLayout();
}
public void createComponentLayout() {
setBackground(CCGUIUtilities.MAIN_PANEL_BGCOLOR);
GridBagLayout gbl = (GridBagLayout) super.getCCGUILayout();
GridBagConstraints gbc = super.getGridBagConstraints();
this.setLayout(gbl);
gbc.weightx = 1.0;
gbc.gridx = 0;
gbc.gridy = 0;
}
JPanel createMultipleTicketsPanel(Object[] multipleTickets) {
Object[][] dataValues = new Object[multipleTickets.length][3];
Object columnNames[] = { "Select", "Ticket Number" };
Properties prop = new Properties();
JPanel panel = new JPanel();
String ticket = null;
for (int i = 0; i < multipleTickets.length; i++) {
ticket = (String) multipleTickets[i];
dataValues[i][0] = Boolean.TRUE;
dataValues[i][1] = ticket.substring(23, ticket.length());
}
multipleTicketsModel = new DefaultTableModel(dataValues, columnNames);
multipleTicketsTable = new CCGUITable(multipleTicketsModel);
multipleTicketsTable.getColumnModel().getColumn(0).setCellRenderer(
new MyCellRenderer());
JCheckBox checkBox = new JCheckBox();
multipleTicketsTable.getColumnModel().getColumn(0).setCellEditor(new MyCellEditor());
//multipleTicketsTable.getColumn(0).setCellRenderer(
//new MyCheckRenderer());
//multipleTicketsTable.getColumn(0).setCellEditor(
//new MyCheckEditor());
JScrollPane scrollPane = new JScrollPane(multipleTicketsTable);
scrollPane.setPreferredSize(TABLE_SIZE);
scrollPane.setMinimumSize(TABLE_SIZE);
multipleTicketsTable.setSize(new Dimension(50, 100));
multipleTicketsTable.getColumn("Select").setMaxWidth(50);
GridBagLayout gbl = new GridBagLayout();
GridBagConstraints gbc = new GridBagConstraints();
panel.setLayout(gbl);
panel.setBackground(MAIN_PANEL_BGCOLOR);
gbc.anchor = GridBagConstraints.WEST;
gbc.insets = CCGUIUtilities.ZERO_INSETS;
gbc.gridx = 0;
gbc.gridy = 0;
gbl.setConstraints(scrollPane, gbc);
panel.add(scrollPane);
JPanel panel1 = new JPanel();
panel1.setLayout(gbl1);
panel1.setBackground(MAIN_PANEL_BGCOLOR);
gbc.anchor = GridBagConstraints.SOUTH;
gbc.gridx = 0;
gbc.gridy = 0;
JButton okButton = new JButton();
okButton.setBorderPainted(true);
okButton.setText("Ok");
gbl1.setConstraints(okButton, gbc);
EmailListener1 listener = new EmailListener1(multipleTicketsTable);
okButton.addActionListener(listener);
panel1.add(okButton);
gbc.gridx = 1;
gbc.gridy = 0;
JButton cancelButton = new JButton();
cancelButton.setBorderPainted(true);
cancelButton.setText("Cancel");
gbl1.setConstraints(cancelButton, gbc);
panel1.add(cancelButton);
gbc.gridx = 0;
gbc.gridy = 1;
gbl.setConstraints(panel1, gbc);
panel.add(panel1);
return panel;
}
public void executeCommandTask() {
}
public void onTaskFailure(Exception e) {
CCGUIUtilities.showErrorMessage(e.getMessage(), "Tools : Email");
}
/*
* Added by Panneer Selvam. @Date 19-06-2006
*/
public void onTaskCompleted(Object result) {
setCursor(CCGUIUtilities.NORMAL_CURSOR);
resetTaskDetail();
String actionType = worker.getActionType();
try {
if (!worker.isFailure()) {
if (actionType.equals("new_ereceipt")) {
// eTktNumber.get(0));
newEReceipt = BookingFormatUtil.textToNewTicketFormat(
result, eTktNumber.get(0));
eTktNumber.remove(0);
if (eTktNumber.size() > 0) {
startETicketWorker((String) eTktNumber.get(0));
}
if (eTktNumber.size() == 0) {
EMailPanel.loadTicketDetailsHtml(newEReceipt);
String s = new String();
if (multipleTicketsTable.getRowCount() == tickets.length) {
s = tickets[0].trim() + "-"
+ tickets[tickets.length - 1].trim();
} else {
for (int i = 0; i < tickets.length; i++) {
if (i == tickets.length) {
s = s + tickets[i].trim();
} else {
s = s + tickets[i].trim() + ", ";
}
}
}
EMailPanel.subjectCombo.addItem(s);
// EMailPanel.subjectCombo.setSelectedIndex(EMailPanel.subjectCombo.getItemCount()
// - 1);
// EMailPanel.subjectCombo.setItems(new Object[]{s});
}
EMailPanel.dialog.setVisible(false);
}
}
} catch (CCGUIException ex) {
CCGUIUtilities.showErrorMessage(ex.getMessage(), "Tools : Email");
} catch (ReLoginException e) {
CCGUIUtilities.showReLoginMessage();
}
}
public static void loadEmailHtmlContent(String filePath) {
try {
htmlEditorPane.setEditorKit(new HTMLEditorKit());
htmlEditorPane.setPage(filePath);
setCurorAtStart();
} catch (Exception ex) {
CCGUIUtilities.showErrorMessage(ex.getMessage(), "Tools - Email");
ex.printStackTrace();
}
}
public static String loadTicketDetailsHtml(String htmlContents) {
final int fontSize4Display = 11;
if (htmlContents != null) {
newEReceipt = htmlContents;
try {
clearHtmlEditorPane();
htmlEditorPane.setBackground(java.awt.Color.WHITE);
htmlEditorPane.setText(resizeHtml(htmlContents,
fontSize4Display));
setCurorAtStart();
} catch (Exception ex) {
CCGUIUtilities.showErrorMessage(ex.getMessage(),
"Tools - Email");
ex.printStackTrace();
}
}
return htmlContents;
}
private static void setCurorAtStart() {
htmlEditorPane.requestFocusInWindow();
htmlEditorPane.setCaretPosition(0);
}
private static void clearHtmlEditorPane() {
htmlEditorPane.setContentType("text");
htmlEditorPane.setContentType(CONTENTTYPE_TEXT_PLAIN);
htmlEditorPane.setText("");
htmlEditorPane.setBackground(CCGUIUtilities.TEXT_FIELD_BGCOLOR);
htmlEditorPane.setContentType("text/html");
}
private static String resizeHtml(String html, int fontSize) {
int tmp = (2 * fontSize) + 3;
html = html
.replaceAll("font-size:11pt", "font-size:" + fontSize + "pt");
html = html.replaceAll("font-size:25px", "font-size:" + tmp + "px");
html = html.replaceAll("font-size:8pt", "font-size:" + fontSize + "pt");
html = html.replaceAll("font-size:30px", "font-size:" + tmp + "px");
html = html
.replaceAll(
"<tr id='TEMPROW'><td colspan='6' height='1' bgcolor='black'></td></tr>",
"");
return html;
}
public class EmailListener1 implements ActionListener {
CCGUITable table = null;
public EmailListener1(CCGUITable multipleTicketsTable) {
this.table = multipleTicketsTable;
}
public void actionPerformed(ActionEvent arg0) {
performAction();
CallCentreDialog dialog = MultipleTicketsDisplayPanel.dialog;
if (dialog != null) {
dialog.dispose();
}
}
public void performAction() {
eTktNumber = new ArrayList<String>();
//Changes start
System.out.println("********" + table.getCellEditor(0, 0).getCellEditorValue());
System.out.println("********" + table.getCellEditor(1, 0).getCellEditorValue());
System.out.println("********" + table.getCellEditor(2, 0).getCellEditorValue());
System.out.println("********" + table.getCellEditor(3, 0).getCellEditorValue());
//Changes end
for (int i = 0; i < table.getRowCount(); i++) {
eTktNumber.add(i, (String) table.getValueAt(i, 1));
}
IS_MULTIPLE_ERECEIPT_SELECTED = true;
tickets = new String[eTktNumber.size()];
eTktNumber.toArray(tickets);
//changes start
File file = new File("D:/EASYMARS/");
File[] arrFile = file.listFiles();
int iLength = arrFile.length;
String name = null;
for(int i = 0 ; i < iLength ; i++)
{
name = arrFile[i].getName();
if(name.length() > 4 && name.substring(name.length() - 4, name.length()).equals(".pdf"))
{
arrFile[i].delete();
}
}
//changes end
startETicketWorker(eTktNumber.get(0));
}
}
private void startETicketWorker(String eTktNumber) {
initMultiEReceiptWorker(eTktNumber);
IS_MULTIPLE_ERECEIPT_SELECTED = true;
if (worker != null)
worker.start();
}
private void initMultiEReceiptWorker(String eTktNumber) {
setCursor(CCGUIUtilities.WAIT_CURSOR);
setTaskDetail("Send e-Receipt in progress", true);
String request = CCGUITicketUtilities.NEW_MULTIPLE_ERECEIPT_REQ_MSG
.format(new Object[] { initET(eTktNumber) });
Object message = CCGUIMessageBuilder.createEReceiptFormatMsg(request,
"EReceipt");
worker = new RequestWorker(this, 1, "new_ereceipt");
worker.setMessage(message);
}
private String initET(String eTktNumber) {
int cnt = 1;
StringBuffer buffer = new StringBuffer("");
buffer.append(CCGUITicketUtilities.ETKT_REQ_MSG
.format(new Object[] { eTktNumber.trim() }));
return buffer.toString();
}
private class MyCellRenderer implements TableCellRenderer
{
protected Border noFocusBorder = new EmptyBorder(1, 1, 1, 1);
protected Border focusBorder = UIManager.getBorder("Table.focusCellHighlightBorder");
private JCheckBox checkBox;
public MyCellRenderer()
{
checkBox = new JCheckBox();
checkBox.setHorizontalAlignment(SwingConstants.CENTER);
checkBox.setBackground( Color.white);
}
public Component getTableCellRendererComponent(JTable
table, Object value, boolean isSelected, boolean
hasFocus, int row, int column)
{
checkBox.setSelected( Boolean.valueOf( value.toString() ).booleanValue() );
return checkBox;
}
}
private class MyCellEditor extends AbstractCellEditor implements TableCellEditor
{
JCheckBox checkBox = new JCheckBox();
public MyCellEditor()
{
//checkBox = new JCheckBox();
//checkBox.setHorizontalAlignment(SwingConstants.CENTER);
//checkBox.setBackground( Color.white);
}
public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column)
{
checkBox = new JCheckBox((String) value);
System.out.println("value is getTableCellEditorComponent method is: " + value);
checkBox.setSelected( Boolean.valueOf( value.toString() ).booleanValue() );
return checkBox;
}
public Object getCellEditorValue()
{
return Boolean.valueOf(checkBox.isSelected());
}
}
}