/*
* Geotoolkit - An Open Source Java GIS Toolkit
* http://www.geotoolkit.org
*
* (C) 2007 - 2008, Open Source Geospatial Foundation (OSGeo)
* (C) 2008 - 2009, Johann Sorel
*
* 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.style;
import java.awt.Component;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JSpinner;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.SpinnerNumberModel;
import javax.swing.SwingConstants;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import org.geotoolkit.factory.FactoryFinder;
import org.opengis.filter.FilterFactory;
import org.opengis.filter.expression.Expression;
/**
*
* @author Johann Sorel
* @module
*/
public class JDashPane extends javax.swing.JPanel {
/**
* Dashes panel
*
* Creates new form JDashPanel
*/
public JDashPane() {
initComponents();
final ChangeListener chl = new ChangeListener() {
@Override
public void stateChanged(ChangeEvent e) {
propertyChange();
}
};
guiBetween.addChangeListener(chl);
guiLength.addChangeListener(chl);
guiOffset.addChangeListener(chl);
}
private void propertyChange(){
firePropertyChange(StyleElementEditor.PROPERTY_UPDATED, null, getDashes());
}
/**
*
* @return float[]
*/
public float[] getDashes() {
if ( ((Number)guiLength.getValue()).doubleValue() == 0 || ((Number)guiBetween.getValue()).doubleValue() == 0) {
return null;
} else {
return new float[]{
(Float)guiLength.getValue(),
(Float)guiBetween.getValue()
};
}
}
/**
*
* @param dashes , the default dashes array
*/
public void setDashes(final float[] dashes) {
if (dashes!= null && dashes.length != 0) {
guiLength.setValue(dashes[0]);
guiBetween.setValue(dashes[1]);
}else{
guiLength.setValue(0f);
guiBetween.setValue(0f);
}
}
/**
*
* @return Expression dashes offset
*/
public Expression getOffset(){
FilterFactory FF = FactoryFinder.getFilterFactory(null);
return FF.literal(guiOffset.getValue());
}
/**
*
* @param exp default dashes offset
*/
public void setOffset(final Expression exp){
if(exp != null){
guiOffset.setValue( Float.parseFloat(exp.toString()) );
}else{
guiOffset.setValue(0f);
}
}
/** 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() {
guiOffset = new JSpinner();
guiBetween = new JSpinner();
guiLength = new JSpinner();
guiOffset.setModel(new SpinnerNumberModel(Float.valueOf(0.0f), null, null, Float.valueOf(1.0f)));
guiBetween.setModel(new SpinnerNumberModel(Float.valueOf(0.0f), Float.valueOf(0.0f), null, Float.valueOf(1.0f)));
guiLength.setModel(new SpinnerNumberModel(Float.valueOf(0.0f), Float.valueOf(0.0f), null, Float.valueOf(1.0f)));
GroupLayout layout = new GroupLayout(this);
this.setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(guiLength, GroupLayout.PREFERRED_SIZE, 46, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(ComponentPlacement.RELATED)
.addComponent(guiBetween, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(ComponentPlacement.RELATED)
.addComponent(guiOffset, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
);
layout.linkSize(SwingConstants.HORIZONTAL, new Component[] {guiBetween, guiLength, guiOffset});
layout.setVerticalGroup(
layout.createParallelGroup(Alignment.LEADING)
.addGroup(layout.createParallelGroup(Alignment.BASELINE)
.addComponent(guiLength, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addComponent(guiBetween, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addComponent(guiOffset, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
);
layout.linkSize(SwingConstants.VERTICAL, new Component[] {guiBetween, guiLength, guiOffset});
}// </editor-fold>//GEN-END:initComponents
// Variables declaration - do not modify//GEN-BEGIN:variables
private JSpinner guiBetween;
private JSpinner guiLength;
private JSpinner guiOffset;
// End of variables declaration//GEN-END:variables
}