/*
* 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.guillcogc.dialog;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.logging.Logger;
import javax.swing.JFrame;
import org.geoforge.guillc.border.GfrFactoryBorder;
import org.geoforge.guillc.optionpane.GfrOptionPaneAbs;
import org.geoforge.guillc.panel.GfrPnl;
import org.geoforge.guillcogc.util.GfrResBundleGuiLlcOgc;
import org.geoforge.guillc.dialog.GfrDlgCmdCancelOkExtItmLstUnselectableAbs;
import org.geoforge.guillc.event.GfrEvtValidityChanged;
import org.geoforge.guillc.handler.IGfrHandlerListenerPanelDialog;
import org.geoforge.guillc.event.GfrEvtPanelChange;
import org.geoforge.guillcogc.panel.GfrPnlChooseEditSelectWmsServer;
import org.geoforge.guillcogc.panel.GfrPnlSelEdtServerEdit;
import org.geoforge.guillcogc.panel.GfrPnlSelEdtServerSelect;
import org.geoforge.lang.util.GfrResBundleLang;
import org.geoforge.java.util.logging.filehandler.FileHandlerLogger;
import org.geoforge.wrpbasusr.GfrWrpUsrSpcDspPrtAppRoot;
/**
*
* @author Amadeus.Sowerby
*
* email: Amadeus.Sowerby_AT_gmail.com ... please remove "_AT_" from the above
* string to get the right email address
*/
public class GfrDlgNewTloOgcWms extends GfrDlgCmdCancelOkExtItmLstUnselectableAbs
{
// ----
// begin: instantiate logger for this class
final private static Logger _LOGGER_ = Logger.getLogger(GfrDlgNewTloOgcWms.class.getName());
static
{
GfrDlgNewTloOgcWms._LOGGER_.addHandler(FileHandlerLogger.s_getInstance());
}
// end: instantiate logger for this class
// ----
//--
//--beg static fields
final private static int INT_SIZE_LABEL = 160;
final private static int INT_SIZE_CONTENT = 370;
final private static int INT_SIZE_BUTTON = 30;
//--end static fields
//--
//--
//--beg private fields
private GfrPnlChooseEditSelectWmsServer _pnlLabelChoice_;
private GfrPnlSelEdtServerEdit _pnlServerEdit_;
private GfrPnlSelEdtServerSelect _pnlServerSelect_;
private GfrPnl _pnlPersp_;
private boolean _isSelectListAvailable_;
//--end private fields
//--
//--
//--beg private methods
private boolean _pnlServerEditHasNoError_()
{
String strUrl = this._pnlServerEdit_.getValue();
if (strUrl == null || strUrl.length() < 1)
return true;
return this._pnlServerEditEnablesOk_();
}
private boolean _pnlServerEditEnablesOk_()
{
String strUrl = this._pnlServerEdit_.getValue();
if (strUrl == null || strUrl.length() < 1)
return false;
if (super._alreadyExists(strUrl))
{
validityChanged(new GfrEvtValidityChanged(false, "This url is already imported."));
return false;
}
strUrl = strUrl.trim();
try
{
new URI(strUrl);
new URL(strUrl);
}
catch (URISyntaxException excURISyntax)
{
validityChanged(new GfrEvtValidityChanged(false, "Wrong URL."));
return false;
}
catch (MalformedURLException excMalformedURL)
{
validityChanged(new GfrEvtValidityChanged(false, "Wrong URL."));
return false;
}
catch (Exception exc)
{
validityChanged(new GfrEvtValidityChanged(false, "Please enter a valid URL."));
return false;
}
return true;
}
private boolean _pnlServerSelectChecked_()
{
String strUrl = this._pnlServerSelect_.getValue();
if (strUrl == null || strUrl.length() < 1)
{
//validityChanged(new GfrEvtValidityChanged(false, "Please select a server."));
return false;
}
//validityChanged(new GfrEvtValidityChanged(false, strUrl));
return true;
}
private void _updateEditOrSelectPanel_()
{
if (_pnlLabelChoice_.isEditSelected())
{
this._pnlPersp_.setBorder(GfrFactoryBorder.createTitledBorder(
GfrResBundleGuiLlcOgc.s_getInstance().getValue("sentence.enterWmsUrl")));
//_pnlLabelChoice_.setWhat("Select a server from the default list.");
this._pnlPersp_.remove(this._pnlServerSelect_);
_pnlServerSelect_.setVisible(false);
this._pnlPersp_.add(this._pnlServerEdit_, BorderLayout.CENTER);
_pnlServerEdit_.setVisible(true);
this.pack();
return;
}
if (!_pnlLabelChoice_.isEditSelected()) // ???????????????? always true !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
{
this._pnlPersp_.setBorder(GfrFactoryBorder.createTitledBorder(
GfrResBundleGuiLlcOgc.s_getInstance().getValue("sentence.selectWmsFromList")));
//_pnlLabelChoice_.setWhat("Enter manually a new server.");
this._pnlPersp_.remove(this._pnlServerEdit_);
_pnlServerEdit_.setVisible(false);
this._pnlPersp_.add(this._pnlServerSelect_, BorderLayout.CENTER);
_pnlServerSelect_.setVisible(true);
this.pack();
return;
}
// ???????????????? statement never reached !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
this.pack();
}
//--end private methods
//--
//--
//--beg public methods
public GfrDlgNewTloOgcWms(
JFrame frmOwner,
String[] strsExistingItems,
String[] strsDefaultServerTitle,
String[] strsDefaultUrlServer,
String[] strsDefaultWho,
String[] strsDefaultWhat,
String[] strsDefaultUrlHomePage)
{
super(
frmOwner,
strsExistingItems,
GfrResBundleGuiLlcOgc.s_getInstance().getValue("sentence.newWmsServiceByEditing"));
int defaultListLength = strsDefaultUrlServer.length;
int existing = 0;
for (int i = 0; i < strsExistingItems.length; i++)
{
for (int j = 0; j < defaultListLength; j++)
{
if (strsDefaultUrlServer[j] != null && strsDefaultUrlServer[j].compareTo(strsExistingItems[i]) == 0)
{
strsDefaultUrlServer[j] = null;
existing++;
}
}
}
int newSize = defaultListLength - existing;
String[] strsCleanUrlServer = new String[newSize];
String[] strsCleanWho = new String[newSize];
String[] strsCleanWhat = new String[newSize];
String[] strsCleanUrlHomePage = new String[newSize];
String[] strsCleanServerTitle = new String[newSize];
int j = 0;
for (int i = 0; i < newSize; i++)
{
while (strsDefaultUrlServer[j] == null)
{
j++;
}
strsCleanUrlServer[i] = strsDefaultUrlServer[j];
strsCleanWho[i] = strsDefaultWho[j];
strsCleanWhat[i] = strsDefaultWhat[j];
strsCleanUrlHomePage[i] = strsDefaultUrlHomePage[j];
strsCleanServerTitle[i] = strsDefaultServerTitle[j];
j++;
}
this._isSelectListAvailable_ = true;
if (strsCleanUrlServer.length == 0)
this._isSelectListAvailable_ = false;
this._pnlPersp_ = new GfrPnl();
this._pnlServerEdit_ = new GfrPnlSelEdtServerEdit((IGfrHandlerListenerPanelDialog) this);
this._pnlServerSelect_ = new GfrPnlSelEdtServerSelect(
strsCleanServerTitle,
GfrResBundleLang.s_getInstance().getValue("sentence.yourChoice"),
strsCleanUrlServer,
strsCleanWho,
strsCleanWhat,
strsCleanUrlHomePage,
(IGfrHandlerListenerPanelDialog) this);
this._pnlLabelChoice_ = new GfrPnlChooseEditSelectWmsServer(this._isSelectListAvailable_);
this._pnlLabelChoice_.addPanelListener((IGfrHandlerListenerPanelDialog) this);
this._pnlServerSelect_.addPanelListener(((IGfrHandlerListenerPanelDialog) (this)));
this._pnlServerEdit_.addPanelListener(((IGfrHandlerListenerPanelDialog) (this)));
super.addToListResizable(this._pnlLabelChoice_);
this._pnlServerEdit_.setLengths(
INT_SIZE_LABEL, INT_SIZE_CONTENT, INT_SIZE_BUTTON);
this._pnlServerSelect_.setLengths(
INT_SIZE_LABEL, INT_SIZE_CONTENT, INT_SIZE_BUTTON);
super.setLengths(
INT_SIZE_LABEL, INT_SIZE_CONTENT, INT_SIZE_BUTTON);
}
@Override
public boolean init()
{
try
{
if (!super.init())
return false;
if (!this._pnlPersp_.init())
return false;
if (!this._pnlServerEdit_.init())
return false;
if (!this._pnlServerSelect_.init())
return false;
if (!this._pnlLabelChoice_.init())
return false;
if (_isSelectListAvailable_)
this._pnlLabelChoice_.setPreferredSize(
new Dimension(this._pnlLabelChoice_.getWidth(), 50));
this._pnlPersp_.setLayout(new BorderLayout());
this.setResizable(false);
super._pnlContent.add(this._pnlLabelChoice_);
super._pnlContent.add(this._pnlPersp_);
_pnlLabelChoice_.setEditSelected(!GfrWrpUsrSpcDspPrtAppRoot.getInstance().isChoiceSelectNewServerWms());
this._updateEditOrSelectPanel_();
this._pnlProblem.hideMessage();
this.pack();
return true;
}
catch (Exception exc)
{
exc.printStackTrace();
String str = exc.getMessage();
GfrDlgNewTloOgcWms._LOGGER_.severe(str);
return false;
}
}
@Override
public void destroy()
{
super.destroy();
if (this._pnlPersp_ != null)
{
this._pnlPersp_.destroy();
this._pnlPersp_ = null;
}
if (this._pnlServerEdit_ != null)
{
this._pnlServerEdit_.destroy();
this._pnlServerEdit_ = null;
}
if (this._pnlServerSelect_ != null)
{
this._pnlServerSelect_.destroy();
this._pnlServerSelect_ = null;
}
if (this._pnlLabelChoice_ != null)
{
this._pnlLabelChoice_.destroy();
this._pnlLabelChoice_ = null;
}
}
@Override
protected void _performAction()
{
try
{
GfrWrpUsrSpcDspPrtAppRoot.getInstance().setChoiceSelectNewServerWms(
!_pnlLabelChoice_.isEditSelected());
super._performAction();
}
catch (Exception exc)
{
exc.printStackTrace();
String str = exc.getMessage();
GfrDlgNewTloOgcWms._LOGGER_.severe(str);
GfrOptionPaneAbs.s_showDialogError(null, str);
System.exit(1);
}
}
public String getValue()
{
if (_pnlLabelChoice_.isEditSelected())
return this._pnlServerEdit_.getValue();
return this._pnlServerSelect_.getValue();
}
public String getValueDescShort()
{
if (_pnlLabelChoice_.isEditSelected())
return this._pnlServerEdit_.getValueDescShort();
return this._pnlServerSelect_.getValueDescShort();
}
public String getWmsProviderWebSite()
{
if (_pnlLabelChoice_.isEditSelected())
return this._pnlServerEdit_.getValueUrl();
return this._pnlServerSelect_.getValueUrl();
}
public String getWho()
{
if (_pnlLabelChoice_.isEditSelected())
return this._pnlServerEdit_.getWho();
return this._pnlServerSelect_.getWho();
}
@Override
protected boolean _okEnableable()
{
if (_pnlLabelChoice_.isEditSelected())
return this._pnlServerEditEnablesOk_();
return this._pnlServerSelectChecked_();
}
@Override
protected boolean _noErrorToDisplay()
{
if (_pnlLabelChoice_.isEditSelected())
return this._pnlServerEditHasNoError_();
return this._pnlServerSelectChecked_();
}
@Override
protected void _updateModifiedFieldStatus(GfrEvtPanelChange e)
{
Object obj = e.getSource();
if (obj.equals(this._pnlLabelChoice_))
{
//nothing to update in this case
return;
}
if (obj.equals(this._pnlServerEdit_.getUrlObject()))
{
boolean bln = _pnlServerEditEnablesOk_();
this._pnlServerEdit_.setCorrectInput(bln);
return;
}
if (obj.equals(this._pnlServerSelect_.getUrlObject()))
{
boolean bln = _pnlServerSelectChecked_();
this._pnlServerSelect_.setCorrectInput(bln);
return;
}
}
@Override
public void somethingHasHappened(GfrEvtPanelChange e)
{
Object obj = e.getSource();
if (obj.equals(this._pnlLabelChoice_))
{
_updateEditOrSelectPanel_();
super._pnlProblem.hideMessage();
}
super.somethingHasHappened(e);
}
}