/*
* 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.panel.PnlContentsOkTextSave;
import org.geoforge.guillc.panel.GfrPnlCmdCancelOk;
import java.awt.Dimension;
import java.awt.event.KeyEvent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;
import org.geoforge.java.enumeration.GfrEnuSystemPropertiesKeys;
/**
*
* @author bantchao
*
* email: bantchao_AT_gmail.com
* ... please remove "_AT_" from the above string to get the right email address
*
*/
abstract public class GfrDlgOkTextSaveAbs extends GfrDlgOkAbs implements
DocumentListener,
java.awt.event.KeyListener
{
final static private int _INT_DIM_W = 220;
final static private int _INT_DIM_H = 100;
protected String[] _strsExistingItems = null;
protected GfrDlgOkTextSaveAbs(
JFrame frmOwner,
String strSentence,
String[] strsExistingItems,
String strSource)
{
super(frmOwner,
System.getProperty(GfrEnuSystemPropertiesKeys.NAME_LONG_APPLI.getLabel()) + " - " + strSentence,
GfrDlgOkTextSaveAbs._INT_DIM_W,
GfrDlgOkTextSaveAbs._INT_DIM_H);
this._strsExistingItems = strsExistingItems;
super._pnlContents = new PnlContentsOkTextSave(
(DocumentListener) this,
(java.awt.event.KeyListener) this,
strsExistingItems,
strSource);
}
@Override
public void keyTyped(KeyEvent e) {}
@Override
public void keyReleased(KeyEvent e) {}
@Override
public void keyPressed(KeyEvent e)
{
int key = e.getKeyCode();
if (key != KeyEvent.VK_ENTER)
return;
GfrPnlCmdCancelOk pnl = (GfrPnlCmdCancelOk) super._pnlCommand;
if (! pnl.isEnabledOkCommandDialog())
return;
super.setCancelled(false);
super.setVisible(false);
}
@Override
public boolean init()
{
if (! super.init())
return false;
if (this._strsExistingItems!=null && this._strsExistingItems.length>0)
{
JPanel pnl = (JPanel) super.getContentPane();
Dimension dim = pnl.getPreferredSize();
dim = new Dimension(
dim.width + 100,
dim.height + 140 // PnSellListExistingItemsView.INT_H
);
pnl.setPreferredSize(dim);
pnl.setMinimumSize(dim);
}
return true;
}
@Override
public void insertUpdate(DocumentEvent e)
{
try {
_update(e);
} catch (BadLocationException ex) {
}
}
@Override
public void removeUpdate(DocumentEvent e)
{
try {
_update(e);
} catch (BadLocationException ex) {
}
}
@Override
public void changedUpdate(DocumentEvent e) {}
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)
{
boolean blnAlreadyExists = _alreadyExists(str);
if (! blnAlreadyExists)
{
blnOk = true;
// TEMPO:
if (super._pnlContents instanceof PnlContentsOkTextSave)
((PnlContentsOkTextSave)super._pnlContents).updateWarning(null,
true // about valueTarget
);
}
else
{
String strMessage = "Name ";
strMessage += str;
strMessage += " already exists";
if (super._pnlContents instanceof PnlContentsOkTextSave)
((PnlContentsOkTextSave)super._pnlContents).updateWarning(strMessage,
true // about valueTarget
);
}
}
else
{
if (super._pnlContents instanceof PnlContentsOkTextSave)
((PnlContentsOkTextSave) super._pnlContents).updateWarning(null,
true // about valueTarget
);
}
}
else
{
// TEMPO:
if (super._pnlContents instanceof PnlContentsOkTextSave)
((PnlContentsOkTextSave)super._pnlContents).updateWarning(null,
true // about valueTarget
);
}
if (! blnOk)
{
((GfrPnlCmdCancelOk) super._pnlCommand).setEnabledOkCommandDialog(false);
}
else
((GfrPnlCmdCancelOk) super._pnlCommand).setEnabledOkCommandDialog(true);
}
private boolean _alreadyExists(String str)
{
if (str == null)
return false;
if (this._strsExistingItems == null)
return false;
if (this._strsExistingItems.length < 1)
return false;
for (int i=0; i<this._strsExistingItems.length; i++)
{
if (str.compareTo(this._strsExistingItems[i]) == 0)
return true;
}
return false;
}
}