/*
* GeoTools - The Open Source Java GIS Toolkit
* http://geotools.org
*
* (C) 2002-2008, Open Source Geospatial Foundation (OSGeo)
*
* This library 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;
* version 2.1 of the License.
*
* This library 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.
*/
package org.geotools.gui.swing.style.sld;
import java.util.Collection;
import java.util.Iterator;
import java.util.Vector;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import org.geotools.factory.CommonFactoryFinder;
import org.geotools.map.MapLayer;
import org.opengis.feature.type.PropertyDescriptor;
import org.opengis.filter.FilterFactory2;
import org.opengis.filter.expression.Expression;
/**
* Expression dialog
*
* @author Johann Sorel
*
* @source $URL$
*/
public class JExpressionDialog extends javax.swing.JDialog {
/**
* Creates new form JExpressionDialog
*/
public JExpressionDialog() {
this(null, null);
}
/**
*
* @param layer the layer to edit
*/
public JExpressionDialog(MapLayer layer) {
this(layer, null);
}
/**
*
* @param exp the default expression
*/
public JExpressionDialog(Expression exp) {
this(null, exp);
}
/**
*
* @param layer the layer to edit
* @param exp the default expression
*/
public JExpressionDialog(MapLayer layer, Expression exp) {
initComponents();
setLayer(layer);
setExpression(exp);
lst_field.addListSelectionListener(new ListSelectionListener() {
public void valueChanged(ListSelectionEvent e) {
if (lst_field.getSelectedValue() != null) {
append(lst_field.getSelectedValue().toString());
}
}
});
}
private void append(String val) {
if (!jta.getText().endsWith(val)) {
if (!jta.getText().endsWith(" ") && jta.getText().length() > 0) {
jta.append(" ");
}
jta.append(val);
}
}
/**
*
* @param layer the layer to edit
*/
public void setLayer(MapLayer layer) {
lst_field.removeAll();
if (layer != null) {
lst_field.removeAll();
Collection<PropertyDescriptor> col = layer.getFeatureSource().getSchema().getDescriptors();
Iterator<PropertyDescriptor> it = col.iterator();
PropertyDescriptor desc;
Vector<String> vec = new Vector<String>();
while (it.hasNext()) {
desc = it.next();
vec.add(desc.getName().toString());
}
lst_field.setListData(vec);
}
}
/**
*
* @param exp the default expression
*/
public void setExpression(Expression exp) {
if (exp != null) {
if (exp != Expression.NIL) {
jta.setText(exp.toString());
}
}
}
/**
*
* @return Expression : New Expression
*/
public Expression getExpression() {
FilterFactory2 ff = CommonFactoryFinder.getFilterFactory2(null);
Expression expr = ff.property(jta.getText());
return expr;
// try {
// Expression expr = CQL.toExpression(jta.getText());
// return expr;
// } catch (CQLException ex) {
// ex.printStackTrace();
// }
//
// StyleBuilder sb = new StyleBuilder();
// return sb.literalExpression(jta.getText());
}
/** 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.
*/
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {
jScrollPane2 = new javax.swing.JScrollPane();
lst_field = new javax.swing.JList();
jScrollPane1 = new javax.swing.JScrollPane();
jta = new javax.swing.JTextArea();
jButton2 = new javax.swing.JButton();
jButton3 = new javax.swing.JButton();
jButton4 = new javax.swing.JButton();
jButton5 = new javax.swing.JButton();
jButton6 = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
jScrollPane2.setViewportView(lst_field);
jta.setColumns(20);
jta.setRows(5);
jScrollPane1.setViewportView(jta);
jButton2.setText("+");
jButton2.setMargin(new java.awt.Insets(2, 4, 2, 4));
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2actionPlus(evt);
}
});
jButton3.setText("-");
jButton3.setMargin(new java.awt.Insets(2, 4, 2, 4));
jButton3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton3actionMinus(evt);
}
});
jButton4.setText("/");
jButton4.setMargin(new java.awt.Insets(2, 4, 2, 4));
jButton4.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton4actionDivide(evt);
}
});
jButton5.setText("*");
jButton5.setMargin(new java.awt.Insets(2, 4, 2, 4));
jButton5.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton5actionMultiply(evt);
}
});
java.util.ResourceBundle bundle = java.util.ResourceBundle.getBundle("org/geotools/gui/swing/style/sld/Bundle"); // NOI18N
jButton6.setText(bundle.getString("apply")); // NOI18N
jButton6.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
actionClose(evt);
}
});
org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(layout.createSequentialGroup()
.addContainerGap()
.add(jScrollPane2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 104, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
.add(org.jdesktop.layout.GroupLayout.LEADING, jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 193, Short.MAX_VALUE)
.add(org.jdesktop.layout.GroupLayout.LEADING, layout.createSequentialGroup()
.add(jButton2)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(jButton3)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(jButton4)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(jButton5))
.add(jButton6))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(layout.createSequentialGroup()
.addContainerGap()
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(jScrollPane2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 136, Short.MAX_VALUE)
.add(layout.createSequentialGroup()
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
.add(jButton2)
.add(jButton3)
.add(jButton4)
.add(jButton5))
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 73, Short.MAX_VALUE)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(jButton6)
.add(5, 5, 5)))
.addContainerGap())
);
pack();
}// </editor-fold>//GEN-END:initComponents
private void jButton2actionPlus(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2actionPlus
append("+");
}//GEN-LAST:event_jButton2actionPlus
private void jButton3actionMinus(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3actionMinus
append("-");
}//GEN-LAST:event_jButton3actionMinus
private void jButton4actionDivide(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4actionDivide
append("/");
}//GEN-LAST:event_jButton4actionDivide
private void jButton5actionMultiply(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton5actionMultiply
append("*");
}//GEN-LAST:event_jButton5actionMultiply
private void actionClose(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_actionClose
dispose();
}//GEN-LAST:event_actionClose
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JButton jButton2;
private javax.swing.JButton jButton3;
private javax.swing.JButton jButton4;
private javax.swing.JButton jButton5;
private javax.swing.JButton jButton6;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JScrollPane jScrollPane2;
private javax.swing.JTextArea jta;
private javax.swing.JList lst_field;
// End of variables declaration//GEN-END:variables
}