/*
* 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.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import org.geoforge.awt.image.GfrFactoryIconAbs;
import org.geoforge.guillc.button.BtnTransientDelOptionIconPdf;
import org.geoforge.guillc.button.BtnTransientImpOptionIconPdf;
import org.geoforge.guillc.frame.GfrFrmAbs;
import org.geoforge.guillc.optionpane.GfrOptionPaneAbs;
import org.geoforge.guillc.panel.GfrPnlCmdCloseWindow;
import org.geoforge.guillc.panel.GfrPnlContentsOptionIconPdf;
import org.geoforge.guillc.panel.GfrPnlStatusBarMain;
import org.geoforge.guillc.toolbar.GfrTbrHlpOptionIconPdf;
import org.geoforge.java.io.file.GfrUtilFile;
import org.geoforge.java.util.logging.filehandler.FileHandlerLogger;
import org.geoforge.wrpbasusr.GfrWrpUsrSpcDspPrtAppRoot;
/**
*
* @author bill
*
*/
public class GfrDlgCmdCancelOptionIconPdf extends GfrDlgCmdCancelAbs
{
// ----
// begin: instantiate logger for this class
final private static Logger _LOGGER_ = Logger.getLogger(GfrDlgCmdCancelOptionIconPdf.class.getName());
static
{
GfrDlgCmdCancelOptionIconPdf._LOGGER_.addHandler(FileHandlerLogger.s_getInstance());
}
// end: instantiate logger for this class
// ----
private GfrTbrHlpOptionIconPdf _tbr_ = null;
public GfrDlgCmdCancelOptionIconPdf() throws Exception
{
super(GfrFrmAbs.s_getFrameOwner(GfrPnlStatusBarMain.s_getInstance()),
"Your organisation logo option" // strTitleSuffix
);
super._pnlCommand = new GfrPnlCmdCloseWindow(
(ActionListener) this,
GfrFactoryIconAbs.INT_SIZE_XXSMALL);
ImageIcon iin = null;
boolean blnHasIcon = false;
File fleImgLogoOrg = GfrWrpUsrSpcDspPrtAppRoot.s_getFileImageLogoOrganization();
if (fleImgLogoOrg.exists())
{
InputStream ism;
try
{
ism = new FileInputStream(fleImgLogoOrg.getAbsolutePath());
BufferedImage bie = ImageIO.read(ism);
iin = new ImageIcon(bie);
ism.close();
}
catch (Exception exc)
{
exc.printStackTrace();
GfrDlgCmdCancelOptionIconPdf._LOGGER_.severe(exc.getMessage());
throw exc;
}
blnHasIcon = true;
}
super._pnlContents = new GfrPnlContentsOptionIconPdf(iin);
this._tbr_ = new GfrTbrHlpOptionIconPdf((ActionListener) this, blnHasIcon);
}
@Override
public void actionPerformed(ActionEvent evtAction)
{
if (evtAction.getSource() instanceof BtnTransientImpOptionIconPdf)
{
if (_importIcon_())
{
JButton btn = (JButton) evtAction.getSource();
btn.setEnabled(false);
this._tbr_.setEnabledDelete();
}
return;
}
if (evtAction.getSource() instanceof BtnTransientDelOptionIconPdf)
{
if (_deleteIcon_())
{
JButton btn = (JButton) evtAction.getSource();
btn.setEnabled(false);
this._tbr_.setEnabledImport();
((GfrPnlContentsOptionIconPdf) super._pnlContents).set(null);
}
return;
}
// ---
super.actionPerformed(evtAction);
}
@Override
public void destroy()
{
super.destroy();
if (this._tbr_ != null)
{
this._tbr_.destroy();
this._tbr_ = null;
}
}
@Override
public boolean init()
{
if(!super.init())
return false;
if (! this._tbr_.init())
return false;
super.getContentPane().add(this._tbr_, BorderLayout.NORTH);
return true;
}
private boolean _importIcon_()
{
GfrDlgIoVarImpOptionPdfImage dlg = new GfrDlgIoVarImpOptionPdfImage();
if (!dlg.init())
{
String strError = "! dlg.init()";
GfrDlgCmdCancelOptionIconPdf._LOGGER_.severe(strError);
GfrOptionPaneAbs.s_showDialogError(null, strError);
System.exit(1);
}
dlg.setVisible(true);
boolean blnCancelled = dlg.isCancelled();
String strPathAbsIn = null;
if (!blnCancelled)
{
strPathAbsIn = dlg.getPath();
}
dlg.destroy();
dlg = null;
if (blnCancelled) // action cancelled by user
return false;
File flePathAbsIn = new File(strPathAbsIn);
File flePathAbsOut = GfrWrpUsrSpcDspPrtAppRoot.s_getFileImageLogoOrganization();
try
{
GfrUtilFile.s_copy(flePathAbsIn, flePathAbsOut);
// ATTN: if not using flePathAbsIn, then use ImageIO.read(ism) like in constructor
// coz ImageIcon(path) uses ToolKit tat works with a cache
ImageIcon iin = new ImageIcon(flePathAbsIn.getAbsolutePath());
((GfrPnlContentsOptionIconPdf) super._pnlContents).set(iin);
}
catch(Exception exc)
{
exc.printStackTrace();
GfrDlgCmdCancelOptionIconPdf._LOGGER_.severe(exc.getMessage());
GfrOptionPaneAbs.s_showDialogError(null, exc.getMessage());
return false;
}
// ---
return true;
}
private boolean _deleteIcon_()
{
File flePathAbsImg = GfrWrpUsrSpcDspPrtAppRoot.s_getFileImageLogoOrganization();
// !!!!! put path of file in log and dialog
if (! flePathAbsImg.delete())
{
GfrDlgCmdCancelOptionIconPdf._LOGGER_.severe("Failed to delete image");
GfrOptionPaneAbs.s_showDialogError(null, "Failed to delete image");
return false;
}
((GfrPnlContentsOptionIconPdf) super._pnlContents).set((ImageIcon) null);
return true;
}
}