/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/*
* jifFavoritos.java
*
* Created on 29-jun-2010, 17:46:43
*/
package com.GestDB.swing;
import com.GestDB.general.Trackbug;
import com.GestDB.*;
import com.GestDB.sql.ConexionIFrame;
import com.GestDB.sql.SQLPannel;
import com.utils.utilidades;
import java.util.Date;
import java.util.Map.Entry;
import java.util.TreeMap;
import java.util.Vector;
import javax.swing.JInternalFrame;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import org.w3c.dom.Comment;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
/**
*
* @author seni
*/
public class jifFavoritos extends javax.swing.JInternalFrame {
/** Creates new form jifFavoritos */
public jifFavoritos(Gestor padre, JInternalFrame jifPadre) {
this.padre = padre;
this.jifPadre = jifPadre;
initComponents();
PonLiterales();
tm = new tmodelTabla();
jtableContenidos.setModel(tm);
loadData();
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {
jScrollPane2 = new javax.swing.JScrollPane();
jtableContenidos = new javax.swing.JTable();
jPanel2 = new javax.swing.JPanel();
jlNombre = new javax.swing.JLabel();
jtNombre = new javax.swing.JTextField();
jlQuery = new javax.swing.JLabel();
jScrollPane3 = new javax.swing.JScrollPane();
jtaQuery = new javax.swing.JTextArea();
jlComentario = new javax.swing.JLabel();
jScrollPane4 = new javax.swing.JScrollPane();
jtaComentario = new javax.swing.JTextArea();
jbUp = new javax.swing.JButton();
jbDown = new javax.swing.JButton();
jbSeleccionar = new javax.swing.JButton();
jbBorrar = new javax.swing.JButton();
setClosable(true);
setIconifiable(true);
setMaximizable(true);
setResizable(true);
setTitle("Favoritos");
setFrameIcon(new javax.swing.ImageIcon(getClass().getResource("/com/GestDB/images/fav.png"))); // NOI18N
addInternalFrameListener(new javax.swing.event.InternalFrameListener() {
public void internalFrameOpened(javax.swing.event.InternalFrameEvent evt) {
}
public void internalFrameClosing(javax.swing.event.InternalFrameEvent evt) {
}
public void internalFrameClosed(javax.swing.event.InternalFrameEvent evt) {
formInternalFrameClosed(evt);
}
public void internalFrameIconified(javax.swing.event.InternalFrameEvent evt) {
}
public void internalFrameDeiconified(javax.swing.event.InternalFrameEvent evt) {
}
public void internalFrameActivated(javax.swing.event.InternalFrameEvent evt) {
}
public void internalFrameDeactivated(javax.swing.event.InternalFrameEvent evt) {
}
});
jtableContenidos.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
{null, null, null, null},
{null, null, null, null},
{null, null, null, null},
{null, null, null, null}
},
new String [] {
"Title 1", "Title 2", "Title 3", "Title 4"
}
));
jtableContenidos.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
jtableContenidosMouseClicked(evt);
}
});
jtableContenidos.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyReleased(java.awt.event.KeyEvent evt) {
jtableContenidosKeyReleased(evt);
}
});
jScrollPane2.setViewportView(jtableContenidos);
getContentPane().add(jScrollPane2, java.awt.BorderLayout.CENTER);
jlNombre.setText("Nombre:");
jtNombre.addFocusListener(new java.awt.event.FocusAdapter() {
public void focusGained(java.awt.event.FocusEvent evt) {
jtNombreFocusGained(evt);
}
public void focusLost(java.awt.event.FocusEvent evt) {
jtNombreFocusLost(evt);
}
});
jlQuery.setText("Query:");
jtaQuery.setColumns(20);
jtaQuery.setRows(5);
jtaQuery.addFocusListener(new java.awt.event.FocusAdapter() {
public void focusGained(java.awt.event.FocusEvent evt) {
jtaQueryFocusGained(evt);
}
public void focusLost(java.awt.event.FocusEvent evt) {
jtaQueryFocusLost(evt);
}
});
jScrollPane3.setViewportView(jtaQuery);
jlComentario.setText("Comentario:");
jtaComentario.setColumns(20);
jtaComentario.setRows(5);
jtaComentario.addFocusListener(new java.awt.event.FocusAdapter() {
public void focusGained(java.awt.event.FocusEvent evt) {
jtaComentarioFocusGained(evt);
}
public void focusLost(java.awt.event.FocusEvent evt) {
jtaComentarioFocusLost(evt);
}
});
jScrollPane4.setViewportView(jtaComentario);
jbUp.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/GestDB/images/arrow_up.png"))); // NOI18N
jbUp.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jbUpActionPerformed(evt);
}
});
jbDown.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/GestDB/images/arrow_down.png"))); // NOI18N
jbDown.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jbDownActionPerformed(evt);
}
});
jbSeleccionar.setText("Seleccionar");
jbSeleccionar.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jbSeleccionarActionPerformed(evt);
}
});
jbBorrar.setText("Borrar");
jbBorrar.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jbBorrarActionPerformed(evt);
}
});
javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
jPanel2.setLayout(jPanel2Layout);
jPanel2Layout.setHorizontalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel2Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
.addComponent(jbBorrar)
.addGap(18, 18, 18)
.addComponent(jbSeleccionar)
.addGap(18, 18, 18)
.addComponent(jbUp)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jbDown))
.addGroup(jPanel2Layout.createSequentialGroup()
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jlComentario)
.addComponent(jlNombre)
.addComponent(jlQuery))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jtNombre, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 495, Short.MAX_VALUE)
.addComponent(jScrollPane3, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 495, Short.MAX_VALUE)
.addComponent(jScrollPane4, javax.swing.GroupLayout.DEFAULT_SIZE, 495, Short.MAX_VALUE))))
.addContainerGap())
);
jPanel2Layout.setVerticalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel2Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jlNombre)
.addComponent(jtNombre, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jlQuery)
.addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 66, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(7, 7, 7)
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jlComentario)
.addComponent(jScrollPane4, javax.swing.GroupLayout.PREFERRED_SIZE, 66, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jbDown)
.addComponent(jbUp))
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jbSeleccionar)
.addComponent(jbBorrar)))
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
getContentPane().add(jPanel2, java.awt.BorderLayout.SOUTH);
pack();
}// </editor-fold>//GEN-END:initComponents
private long lLastClick = 1000;
private int irowLastClick = -1;
private void jtableContenidosMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jtableContenidosMouseClicked
if((new Date()).getTime() - lLastClick < 1000 && irowLastClick == jtableContenidos.getSelectedRow())
{
// hay un doble click, tenemos que coger la query pulsada y ponerla en el editor activo
String sQuery = (String)tm.getValueAt(jtableContenidos.getSelectedRow(), 1);
if(((ConexionIFrame)jifPadre).jTabbedPane1.getComponentAt(((ConexionIFrame)jifPadre).jTabbedPane1.getSelectedIndex()) instanceof SQLPannel)
{
((SQLPannel)((ConexionIFrame)jifPadre).jTabbedPane1.getComponentAt(((ConexionIFrame)jifPadre).jTabbedPane1.getSelectedIndex())).gEditor.setQuery(sQuery);
}
}
else
{
jtableContenidosKeyReleased(null);
}
lLastClick = (new Date()).getTime();
irowLastClick = jtableContenidos.getSelectedRow();
}//GEN-LAST:event_jtableContenidosMouseClicked
private void jtableContenidosKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jtableContenidosKeyReleased
if(jtableContenidos.getSelectedRow() != -1)
{
jtNombre.setText(String.valueOf(tm.getValueAt(jtableContenidos.getSelectedRow(), 0)));
jtaQuery.setText(String.valueOf(tm.getValueAt(jtableContenidos.getSelectedRow(), 1)));
jtaComentario.setText(String.valueOf(tm.getValueAt(jtableContenidos.getSelectedRow(), 2)));
}
else
{
jtNombre.setText("");
jtaQuery.setText("");
jtaComentario.setText("");
}
}//GEN-LAST:event_jtableContenidosKeyReleased
private void jtNombreFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_jtNombreFocusGained
sNombreAnterior = jtNombre.getText();
}//GEN-LAST:event_jtNombreFocusGained
private void jtNombreFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_jtNombreFocusLost
if(sNombreAnterior.equals(jtNombre.getText()) == false)
{
// ha cambiado el texto, tenemos que actualizarlo
if(jtableContenidos.getSelectedRow() != -1)
{
int nRow = jtableContenidos.getSelectedRow();
tm.setValueAt(jtNombre.getText(), jtableContenidos.getSelectedRow(), 0);
tm.fireTableChanged();
jtableContenidos.setRowSelectionInterval(nRow, nRow);
save();
}
}
}//GEN-LAST:event_jtNombreFocusLost
private void jtaQueryFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_jtaQueryFocusGained
sQueryAnterior = jtaQuery.getText();
}//GEN-LAST:event_jtaQueryFocusGained
private void jtaQueryFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_jtaQueryFocusLost
if(sQueryAnterior.equals(jtaQuery.getText()) == false)
{
// ha cambiado el texto, tenemos que actualizarlo
if(jtableContenidos.getSelectedRow() != -1)
{
int nRow = jtableContenidos.getSelectedRow();
tm.setValueAt(jtaQuery.getText(), jtableContenidos.getSelectedRow(), 1);
tm.fireTableChanged();
jtableContenidos.setRowSelectionInterval(nRow, nRow);
save();
}
}
}//GEN-LAST:event_jtaQueryFocusLost
private void jtaComentarioFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_jtaComentarioFocusGained
sComentarioAnterior = jtaComentario.getText();
}//GEN-LAST:event_jtaComentarioFocusGained
private void jtaComentarioFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_jtaComentarioFocusLost
if(sComentarioAnterior.equals(jtaComentario.getText()) == false)
{
// ha cambiado el texto, tenemos que actualizarlo
if(jtableContenidos.getSelectedRow() != -1)
{
int nRow = jtableContenidos.getSelectedRow();
tm.setValueAt(jtaComentario.getText(), jtableContenidos.getSelectedRow(), 2);
tm.fireTableChanged();
jtableContenidos.setRowSelectionInterval(nRow, nRow);
save();
}
}
}//GEN-LAST:event_jtaComentarioFocusLost
private void formInternalFrameClosed(javax.swing.event.InternalFrameEvent evt) {//GEN-FIRST:event_formInternalFrameClosed
save();
}//GEN-LAST:event_formInternalFrameClosed
private void jbUpActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbUpActionPerformed
// Movemos el elemento seleccionado hacia arriba siempre que no sea el primer elemento
if(jtableContenidos.getSelectedRow() > 0)
{
int posicionNueva = tm.changeRowPosition(-1, jtableContenidos.getSelectedRow());
jtableContenidos.setRowSelectionInterval(posicionNueva, posicionNueva);
save();
}
}//GEN-LAST:event_jbUpActionPerformed
private void jbDownActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbDownActionPerformed
// Movemos el elemento seleccionado hacia abajo siempre que no sea el ultimo elemento
if(jtableContenidos.getSelectedRow() != -1 && jtableContenidos.getSelectedRow() < (tm.getRowCount() - 1))
{
int posicionNueva = tm.changeRowPosition(1, jtableContenidos.getSelectedRow());
jtableContenidos.setRowSelectionInterval(posicionNueva, posicionNueva);
save();
}
}//GEN-LAST:event_jbDownActionPerformed
private void jbSeleccionarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbSeleccionarActionPerformed
// Tenemos que coger la query pulsada y ponerla en el editor activo
if(jtableContenidos.getSelectedRow() != -1)
{
String sQuery = (String)tm.getValueAt(jtableContenidos.getSelectedRow(), 1);
if(((ConexionIFrame)jifPadre).jTabbedPane1.getComponentAt(((ConexionIFrame)jifPadre).jTabbedPane1.getSelectedIndex()) instanceof SQLPannel)
{
((SQLPannel)((ConexionIFrame)jifPadre).jTabbedPane1.getComponentAt(((ConexionIFrame)jifPadre).jTabbedPane1.getSelectedIndex())).gEditor.setQuery(sQuery);
}
}
}//GEN-LAST:event_jbSeleccionarActionPerformed
private void jbBorrarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbBorrarActionPerformed
// Tenemos que coger la query pulsada y borrarla
if(jtableContenidos.getSelectedRow() != -1)
{
tm.removeRow(jtableContenidos.getSelectedRows());
save();
}
}//GEN-LAST:event_jbBorrarActionPerformed
/**
* Cambia los textos de los controles
*/
public void PonLiterales()
{
this.jlComentario.setText(Gestor.GetLiteral("favorites.comment","Comentario") + ":");
this.jlQuery.setText(Gestor.GetLiteral("favorites.query","Query") + ":");
this.jlNombre.setText(Gestor.GetLiteral("estructurabd.name","Nombre") + ":");
this.jbDown.setToolTipText(Gestor.GetLiteral("favorites.button_down_tooltips","Mueve el favorito hacia abajo en la lista"));
this.jbUp.setToolTipText(Gestor.GetLiteral("favorites.button_up_tooltips","Mueve el favorito hacia arriba en la lista"));
this.jbSeleccionar.setText(Gestor.GetLiteral("importardlg.select","Seleccionar"));
this.jbBorrar.setText(Gestor.GetLiteral("selectconexion.delete","Borrar"));
}
public void reloadData()
{
loadData();
}
private void loadData()
{
// limpiamos los datos de todos los campos
jtNombre.setText("");
jtaComentario.setText("");
jtaQuery.setText("");
String sTitulos[] = { Gestor.GetLiteral("estructurabd.name","Nombre"),
Gestor.GetLiteral("favorites.query","Query"),
Gestor.GetLiteral("favorites.comment","Comentario") };
Class cClases[] = { String.class, String.class, String.class };
Vector vRows = new Vector();
Vector vCol = null;
// cargamos la lista de favoritos
try{
// Manejar XML
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder parser = factory.newDocumentBuilder();
TransformerFactory tranFactory = TransformerFactory.newInstance();
Transformer aTransformer = tranFactory.newTransformer();
// aTransformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, "systmId");
// Cargar el xml de favoritos
Document doc = Gestor.CargaFavoritos(this.getClass());
if(doc != null && doc.hasChildNodes())
{
NodeList nodes = doc.getElementsByTagName("favorito"); // coger todos los elementos de un tipo
Element element = null;
TreeMap tmap = new TreeMap();
// Nos los recorremos
for (int i=0; i<nodes.getLength(); i++)
{
element = (Element)nodes.item(i);
vCol = new Vector(3);
vCol.addElement(element.getElementsByTagName("nombre").item(0).getTextContent().trim());
vCol.addElement(element.getElementsByTagName("query").item(0).getTextContent().trim());
vCol.addElement(element.getElementsByTagName("comentario").item(0).getTextContent().trim());
tmap.put(element.getAttribute("posicion"), vCol);
}
// los metemos en el vector vRows
Object oValues[] = tmap.entrySet().toArray();
for(int i=0; i < oValues.length; i++)
{
vRows.addElement((Vector)((Entry)oValues[i]).getValue());
}
}
}catch(Exception e){
Trackbug.error(e, e.getMessage());
}
tm.CargaDatos(sTitulos, cClases, true, vRows);
}
/**
* Guarda los datos de la tabla en un xml
*/
private void save()
{
// Lo guardamos en un archivo xml
// aTransformer.setOutputProperty(OutputKeys.METHOD, "text");
try {
// Manejar XML
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder parser = factory.newDocumentBuilder();
TransformerFactory tranFactory = TransformerFactory.newInstance();
Transformer aTransformer = tranFactory.newTransformer();
// lo creamos de cero por que no existe
Document doc = parser.newDocument();
Comment comment = doc.createComment("GestDB. File containing the user's favorite querys");
doc.appendChild(comment);
Element eFavoritos = doc.createElement("favoritos"); // creamos el elemento root
doc.appendChild(eFavoritos);
// le a�adimos los elementos "favorito"
Element eFavorito = null;
Element eNombre = null;
Element eQuery = null;
Element eComentario = null;
for(int i=0, limite = tm.getRowCount(); i < limite; i++)
{
eFavorito = doc.createElement("favorito");
eFavorito.setAttribute("posicion",utilidades.AddLeftCadena(String.valueOf(Integer.toHexString(i)), 5, "0"));
eNombre = doc.createElement("nombre");
eNombre.setTextContent((String)tm.getValueAt(i, 0));
eFavorito.appendChild(eNombre);
eQuery = doc.createElement("query");
eQuery.setTextContent((String)tm.getValueAt(i, 1));
eFavorito.appendChild(eQuery);
eComentario = doc.createElement("comentario");
eComentario.setTextContent((String)tm.getValueAt(i, 2));
eFavorito.appendChild(eComentario);
// lo a�adimos
eFavoritos.appendChild(eFavorito);
}
// Lo guardamos
Gestor.GuardaFavoritos(this.getClass(), doc, aTransformer);
} catch(Exception e)
{
}
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JPanel jPanel2;
private javax.swing.JScrollPane jScrollPane2;
private javax.swing.JScrollPane jScrollPane3;
private javax.swing.JScrollPane jScrollPane4;
private javax.swing.JButton jbBorrar;
private javax.swing.JButton jbDown;
private javax.swing.JButton jbSeleccionar;
private javax.swing.JButton jbUp;
private javax.swing.JLabel jlComentario;
private javax.swing.JLabel jlNombre;
private javax.swing.JLabel jlQuery;
private javax.swing.JTextField jtNombre;
private javax.swing.JTextArea jtaComentario;
private javax.swing.JTextArea jtaQuery;
private javax.swing.JTable jtableContenidos;
// End of variables declaration//GEN-END:variables
private Gestor padre = null;
private tmodelTabla tm = null;
private String sNombreAnterior = "";
private String sQueryAnterior = "";
private String sComentarioAnterior = "";
private JInternalFrame jifPadre = null;
}