/*
* 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 2
* 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, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
package org.geoforge.guillc.dialog;
import org.geoforge.guillc.handler.IGfrHandlerSelectComponentDialog;
import java.awt.Dimension;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.logging.Logger;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;
import org.geoforge.guillc.panel.GfrPnlCmdCancelOk;
import org.geoforge.guillc.panel.PnlContentsOkAbs;
import org.geoforge.guillc.optionpane.GfrOptionPaneAbs;
import org.geoforge.lang.util.GfrResBundleLang;
import org.geoforge.java.util.logging.filehandler.FileHandlerLogger;
/**
*
* @author bantchao
*
* email: bantchao_AT_gmail.com
* ... please remove "_AT_" from the above string to get the right email address
*
*/
abstract public class GfrDlgOkSelectAbs extends GfrDlgOkAbs implements
ListSelectionListener,
MouseListener,
DocumentListener
{
// ----
// begin: instantiate logger for this class
final private static Logger _LOGGER_ = Logger.getLogger(GfrDlgOkSelectAbs.class.getName());
static
{
GfrDlgOkSelectAbs._LOGGER_.addHandler(FileHandlerLogger.s_getInstance());
}
// end: instantiate logger for this class
// ----
final static private int _INT_DIM_H = 140;
private String[] _strsItemCurrent_ = null;
protected GfrDlgOkSelectAbs(JFrame frmOwner, String strPrefix, String strTitleSuffix,
int intDimW, int intDimH,
String[] strsExistingItems)
{
super(frmOwner,
strPrefix + " " + strTitleSuffix,
intDimW, intDimH);
this._strsItemCurrent_ = strsExistingItems;
}
protected GfrDlgOkSelectAbs(JFrame frmOwner, String strPrefix, String strTitleSuffix, int intDimW,
String[] strsExistingItems)
{
this(frmOwner, strPrefix, strTitleSuffix,
intDimW, GfrDlgOkSelectAbs._INT_DIM_H, strsExistingItems);
}
protected GfrDlgOkSelectAbs(JFrame frmOwner, String strTitleSuffix, int intDimW,
String[] strsExistingItems)
{
this(frmOwner,
GfrResBundleLang.s_getInstance().getValue("verb.select"),
strTitleSuffix, intDimW,
strsExistingItems);
}
@Override
public void changedUpdate(DocumentEvent e) {}
@Override
public boolean init()
{
if (! super.init())
return false;
if (this._strsItemCurrent_!=null && this._strsItemCurrent_.length>0)
{
JPanel pnl = (JPanel) super.getContentPane();
Dimension dim = pnl.getPreferredSize();
dim = new Dimension(
dim.width, // + 100,
dim.height + 140
);
pnl.setPreferredSize(dim);
pnl.setMinimumSize(dim);
}
return true;
}
@Override
public void valueChanged(ListSelectionEvent evt)
{
if (evt.getValueIsAdjusting())
return;
if (! (evt.getSource() instanceof JList))
{
String str = "! (evt.getSource() instanceof JList)";
GfrDlgOkSelectAbs._LOGGER_.severe(str);
GfrOptionPaneAbs.s_showDialogError(null, str);
System.exit(1);
}
JList lst = (JList)evt.getSource();
int intRow = lst.getMinSelectionIndex();
if (intRow == -1)
{
//Don't care user as deselected
IGfrHandlerSelectComponentDialog obj = (IGfrHandlerSelectComponentDialog) this._pnlContents;
obj.setValueSelectComponentDialog("");
super.setEnabledOkCommandDialog(false);
return;
}
String str = (String) lst.getSelectedValue();
if (this._pnlContents instanceof IGfrHandlerSelectComponentDialog)
{
IGfrHandlerSelectComponentDialog obj = (IGfrHandlerSelectComponentDialog) this._pnlContents;
obj.setValueSelectComponentDialog(str);
}
}
@Override
public void mouseClicked(MouseEvent e)
{
if (e.getClickCount() != 2)
return;
String strValue = ((PnlContentsOkAbs)super._pnlContents).getValue();
if (strValue == null)
return;
if (! ((GfrPnlCmdCancelOk) super._pnlCommand).isEnabledOkCommandDialog())
return;
if (! (e.getSource() instanceof JList)) // should never happen
return;
//JList lst = (JList) e.getSource();
//int cellIndex = lst.locationToIndex(e.getPoint());
//GfrDlgOkOpenAbs._LOGGER_.info("cellIndex=" + cellIndex);
super.setCancelled(false);
super.setVisible(false);
}
@Override
public void mousePressed(MouseEvent e) {
}
@Override
public void mouseReleased(MouseEvent e) {
}
@Override
public void mouseEntered(MouseEvent e) {
}
@Override
public void mouseExited(MouseEvent e) {
}
@Override
public void insertUpdate(DocumentEvent e)
{
try {
_update(e);
} catch (BadLocationException ex) {
}
}
@Override
public void removeUpdate(DocumentEvent e)
{
try {
_update(e);
} catch (BadLocationException ex) {
}
}
private void _update(DocumentEvent e) throws BadLocationException
{
Document doc = (Document)e.getDocument();
String str = doc.getText(0, doc.getLength());
boolean blnOk = false;
if (str != null)
{
str = str.trim();
if (str.length() > 0)
blnOk = true;
}
((GfrPnlCmdCancelOk) super._pnlCommand).setEnabledOkCommandDialog(blnOk);
}
}