/*
* Copyright (C) 2011-2014 GeoForge Project
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.geoforge.guillc.panel;
import java.util.ArrayList;
import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import org.geoforge.java.awt.color.GfrColor;
import org.geoforge.guillc.table.tablecelleditor.GfrImplTableCellEditorAceRbn;
import org.geoforge.guillc.table.tablecellrenderer.GfrImplTableCellRendererRbn;
import org.geoforge.guillc.radiobutton.GfrRbn;
import org.geoforge.guillc.tablemodel.GfrDtmSelectObject;
/**
*
* @author Amadeus.Sowerby
*
* email: Amadeus.Sowerby_AT_gmail.com
* ... please remove "_AT_" from the above string to get the right email address
*/
public class GfrPnlGrpSngTblSelectObject extends GfrPnlGrpSngTbl implements
ListSelectionListener,
ChangeListener
{
private GfrRbn _rbn_ = null;
private ChangeListener _csr_ = null;
private ListSelectionListener _lsr_ = null;
public String getResult()
{
JTable tbl = super.getTable();
ArrayList<String> alt = new ArrayList<String>();
for (int i = 0; i < tbl.getRowCount(); i++)
{
Boolean bln = (Boolean) tbl.getValueAt(i, 0);
String str = (String) tbl.getValueAt(i, 1);
if (bln)
alt.add(str);
}
if(alt.size() != 1)
throw new UnsupportedOperationException("Only one object should be selected.");
return alt.get(0);
}
public GfrPnlGrpSngTblSelectObject(
String strWhat,
Object[][] objs)
{
super(
strWhat,
new GfrDtmSelectObject(objs));
this._lsr_ = (ListSelectionListener) this;
this._csr_ = (ChangeListener) this;
this._rbn_ = new GfrRbn();
}
@Override
public boolean init()
{
if (!super.init())
return false;
if (!this._rbn_.init())
return false;
super.getSelectionModel().setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
this._rbn_.addChangeListener(this._csr_);
this._rbn_.setHorizontalAlignment(JLabel.CENTER);
super.getColumnModel().getColumn(0).setCellEditor(new GfrImplTableCellEditorAceRbn(this._rbn_));
GfrImplTableCellRendererRbn tcrRbn = new GfrImplTableCellRendererRbn();
if (! tcrRbn.init())
{
System.err.print(GfrPnlGrpSngTblSelectObject.class.getName() + ": ! tcrRbn.init()");
return false;
}
super.getColumnModel().getColumn(0).setCellRenderer(tcrRbn);
super.getSelectionModel().addListSelectionListener(this._lsr_);
super.getColumnModel().getColumn(1).setPreferredWidth(220);
super.getColumnModel().getColumn(0).setPreferredWidth(45);
super.getColumnModel().getColumn(0).setMinWidth(45);
super.getColumnModel().getColumn(0).setResizable(false);
super.setMandatoryIcon(false);
super.setBorder(BorderFactory.createMatteBorder(
10, 10, 10, 10,
GfrColor.GRAY_1));
return true;
}
public boolean isAllUnchecked()
{
JTable tbl = super.getTable();
for (int i = 0; i < tbl.getRowCount(); i++)
{
if (((Boolean) tbl.getValueAt(i, 0)).booleanValue())
return false;
}
return true;
}
public boolean isAllChecked()
{
JTable tbl = super.getTable();
for (int i = 0; i < tbl.getRowCount(); i++)
{
if (!((Boolean) tbl.getValueAt(i, 0)).booleanValue())
return false;
}
return true;
}
public void check(int intIndex)
{
JTable tbl = super.getTable();
tbl.setValueAt(true, intIndex, 0);
for (int i = 0; i < tbl.getRowCount(); i++)
{
tbl.setValueAt(false, intIndex, 0);
}
}
public int getSelectedIndex()
{
JTable tbl = super.getTable();
for (int i = 0; i < tbl.getRowCount(); i++)
{
Boolean bln = (Boolean) tbl.getValueAt(i, 0);
if (bln)
return i;
}
return -1;
}
@Override
public void stateChanged(ChangeEvent e)
{
if (e.getSource() instanceof GfrRbn)
{
boolean bln = ((GfrRbn) e.getSource()).isSelected();
System.out.println("stateChanged : " + bln);
}
else
{
System.out.println("uncaught : " + e.getSource().toString());
}
super.firePanelUpdate();
}
@Override
public void valueChanged(ListSelectionEvent e)
{
if (e.getSource() instanceof GfrRbn)
{
boolean bln = ((GfrRbn) e.getSource()).isSelected();
System.out.println("valueChanged : " + bln);
}
else
{
System.out.println("uncaught : " + e.getSource().toString());
}
super.firePanelUpdate();
}
}