/*
* 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.dialog;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.logging.Logger;
import javax.swing.*;
import org.geoforge.java.awt.font.GfrUtilFont;
import org.geoforge.guillc.border.GfrUtilBorder;
import org.geoforge.guillc.component.GfrComponent;
import org.geoforge.guillc.panel.GfrPnlCmdCancel;
import org.geoforge.guillc.progressbar.GfrProgressBar;
import org.geoforge.guillc.textfield.GfrTfd;
import org.geoforge.lang.handler.IGfrHandlerLifeCycleObject;
import org.geoforge.java.enumeration.GfrEnuSystemPropertiesKeys;
import org.geoforge.lang.thread.GfrPgsThrAbs;
import org.geoforge.java.util.logging.filehandler.FileHandlerLogger;
/**
*
* @author bantchao
*/
/*
* "Pgs" means "Progress"
*/
abstract public class GfrDlgPgsAbs extends JDialog implements
IGfrHandlerLifeCycleObject,
ActionListener
{
// ----
// begin: instantiate logger for this class
final private static Logger _LOGGER_ = Logger.getLogger(GfrDlgPgsAbs.class.getName());
static
{
GfrDlgPgsAbs._LOGGER_.addHandler(FileHandlerLogger.s_getInstance());
}
// end: instantiate logger for this class
// ----
protected GfrPgsThrAbs _thr = null;
protected JFrame _frmOwner = null;
protected GfrProgressBar _pbr = null;
protected JLabel _lbl = null;
private GfrTfd _tfdMessage_ = null;
protected JPanel _pnlAll = null;
protected boolean _blnCancelled = false;
protected GfrPnlCmdCancel _pnlCancel = null;
abstract protected void _invokeLaterCleanUp();
protected GfrDlgPgsAbs(
JFrame frmOwner,
String strMessageTextfield, // nil value allowed
String strTitleWhat,
String strMessageLabel,
String strLocationLabel
)
{
super();
// test
GfrComponent.s_removeCloseWindowButton(this);
// BEG IMPORTANT NOTICE: don't uncomment the line below coz thread hanging
//super.setModalityType(ModalityType.APPLICATION_MODAL);
// END IMPORTANT NOTICE
this._frmOwner = frmOwner;
// tempo in comments
if (this._frmOwner != null)
this._frmOwner.setEnabled(false);
String strTitle = System.getProperty(GfrEnuSystemPropertiesKeys.NAME_LONG_APPLI.getLabel());
strTitle += " - " + strTitleWhat;
super.setTitle(strTitle);
if (this._frmOwner != null) // !!!!
{
Image img = this._frmOwner.getIconImage();
if (img != null)
super.setIconImage(img);
this._frmOwner.addWindowListener(new WindowAdapter()
{
@Override
public void windowActivated(WindowEvent windowEvent)
{
if (GfrDlgPgsAbs.this.isVisible())
GfrDlgPgsAbs.this.toFront();
}
});
}
this._lbl = new JLabel(strMessageLabel);
this._lbl.setFont(GfrUtilFont.s_get(Font.ITALIC, 14));
if (strMessageTextfield != null)
this._tfdMessage_ = new GfrTfd(strMessageTextfield);
this._pbr = new GfrProgressBar();
this._pbr.setIndeterminate(true);
Container cntContentPane = getContentPane();
cntContentPane.setLayout(new BorderLayout());
_pnlAll = new JPanel();
// --- BEGIN border
GfrUtilBorder.s_setEtched(_pnlAll);
// --- END border
cntContentPane.add(_pnlAll, BorderLayout.CENTER);
_pnlAll.setLayout(new BoxLayout(_pnlAll, BoxLayout.Y_AXIS));
if (this._tfdMessage_ != null)
{
_pnlAll.add(Box.createVerticalStrut(10));
_pnlAll.add(this._tfdMessage_);
_pnlAll.add(Box.createVerticalStrut(20));
}
JPanel pnlLabel = new JPanel();
pnlLabel.setLayout(new BorderLayout());
pnlLabel.add(this._lbl, strLocationLabel);
_pnlAll.add(pnlLabel);
_pnlAll.add(Box.createVerticalStrut(10));
_pnlAll.add(this._pbr);
_pnlCancel = new GfrPnlCmdCancel((ActionListener) this);
_pnlCancel.init();
_pnlCancel.setEnabledCancelCommandDialog(true);
_pnlAll.add(Box.createVerticalStrut(10));
_pnlAll.add(_pnlCancel);
}
@Override
public void actionPerformed(ActionEvent e)
{
_cancel_();
}
@Override
public boolean init()
{
if (this._tfdMessage_!=null && !this._tfdMessage_.init())
{
if (this._frmOwner != null)
this._frmOwner.setEnabled(true);
return false;
}
if (this._tfdMessage_ != null)
this._tfdMessage_.setEditable(false);
if (this._frmOwner != null) // !!!
super.setLocationRelativeTo(this._frmOwner);
else
super.setLocationRelativeTo(null);
return true;
}
@Override
public void destroy()
{
if (this._tfdMessage_ != null)
{
this._tfdMessage_.destroy();
this._tfdMessage_ = null;
}
super.setVisible(false);
if (this._frmOwner != null)
{
this._frmOwner.setEnabled(true);
this._frmOwner.toFront();
}
super.dispose();
}
private void _cancel_()
{
GfrDlgPgsAbs._LOGGER_.info("Action cancelled by user");
if (super.isVisible())
super.setVisible(false);
if (this._frmOwner != null)
{
this._frmOwner.setEnabled(true);
this._frmOwner.toFront();
}
if (this._thr!=null && this._thr.isAlive())
this._thr.interrupt();
this._blnCancelled = true;
javax.swing.SwingUtilities.invokeLater(new Runnable()
{
@Override
public void run()
{
_invokeLaterCleanUp();
}
});
}
}