/*
* Geotoolkit - An Open Source Java GIS Toolkit
* http://www.geotoolkit.org
*
* (C) 2011, Geomatys
*
* 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.geotoolkit.gui.swing.render2d.control.edition;
import com.vividsolutions.jts.geom.Geometry;
import com.vividsolutions.jts.io.WKTReader;
import com.vividsolutions.jts.io.WKTWriter;
import java.awt.BorderLayout;
import org.geotoolkit.cql.JCQLTextPane;
import org.geotoolkit.gui.swing.resource.MessageBundle;
/**
*
* @author Johann Sorel (Geomatys)
* @module
*/
public class JWKTPanel extends javax.swing.JPanel {
public static final String GEOMETRY_PROPERTY = "geometry";
private final WKTReader reader = new WKTReader();
private final WKTWriter writer = new WKTWriter();
private final JCQLTextPane guiText = new JCQLTextPane();
private Geometry original = null;
private Geometry current = null;
public JWKTPanel() {
initComponents();
guiPane.add(BorderLayout.CENTER,guiText);
}
public void setGeometry(Geometry geom){
this.original = geom;
if(original != null){
String str = writer.write(original);
str = str.replaceAll("\\(", "\\(\n");
str = str.replaceAll(",", ",\n");
str = str.replaceAll("\\)", "\n\\)");
guiText.setText(str);
}else{
guiText.setText("");
}
guiError.setText("");
this.current = null;
}
public Geometry getGeometry(){
if(current == null){
return original;
}else{
return current;
}
}
private boolean checkWKT(){
String txt = guiText.getText().trim();
try {
current = reader.read(txt);
guiError.setText(null);
return true;
} catch (Exception ex) {
current = null;
guiError.setText(ex.getLocalizedMessage());
return false;
}
}
/** 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() {
guiRollback = new javax.swing.JButton();
guiApply = new javax.swing.JButton();
guiError = new javax.swing.JLabel();
guiPane = new javax.swing.JPanel();
guiRollback.setText(MessageBundle.format("cancel")); // NOI18N
guiRollback.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
guiRollbackActionPerformed(evt);
}
});
guiApply.setText(MessageBundle.format("apply")); // NOI18N
guiApply.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
guiApplyActionPerformed(evt);
}
});
guiError.setForeground(new java.awt.Color(255, 0, 0));
guiError.setText(" ");
guiPane.setLayout(new java.awt.BorderLayout());
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
this.setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addComponent(guiPane, javax.swing.GroupLayout.DEFAULT_SIZE, 305, Short.MAX_VALUE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(guiApply, javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(guiRollback, javax.swing.GroupLayout.Alignment.TRAILING)))
.addComponent(guiError, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
);
layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {guiApply, guiRollback});
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(guiApply)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(guiRollback, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(0, 19, Short.MAX_VALUE))
.addComponent(guiPane, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(guiError))
);
}// </editor-fold>//GEN-END:initComponents
private void guiApplyActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_guiApplyActionPerformed
final Geometry old = getGeometry();
if(checkWKT()){
firePropertyChange(GEOMETRY_PROPERTY, old, getGeometry());
}
}//GEN-LAST:event_guiApplyActionPerformed
private void guiRollbackActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_guiRollbackActionPerformed
if(original != null){
String str = writer.write(original);
str = str.replaceAll("\\(", "\\(\n");
str = str.replaceAll(",", ",\n");
str = str.replaceAll("\\)", "\n\\)");
guiText.setText(str);
guiError.setText("");
}
final Geometry old = getGeometry();
current = null;
firePropertyChange(GEOMETRY_PROPERTY, old, getGeometry());
}//GEN-LAST:event_guiRollbackActionPerformed
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JButton guiApply;
private javax.swing.JLabel guiError;
private javax.swing.JPanel guiPane;
private javax.swing.JButton guiRollback;
// End of variables declaration//GEN-END:variables
}