/*
* 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 java.awt.Dimension;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JComboBox;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.SwingConstants;
import org.geotoolkit.map.MapLayer;
import org.jdesktop.swingx.combobox.ListComboBoxModel;
import org.opengis.filter.expression.Expression;
import org.opengis.filter.expression.Literal;
/**
*
* @author Johann Sorel (Puzzle-GIS)
* @module
*/
public class JComboExpressionPane extends StyleElementEditor<Expression>{
private final List<Literal> values = new ArrayList<Literal>();
private volatile boolean updatingModel = false;
private final Dimension specialSize;
/** Creates new form JColorExpressionPane */
public JComboExpressionPane() {
super(Expression.class);
initComponents();
guiCombo.setEditable(false);
specialSize = guiSpecial.getPreferredSize();
}
public void setExpressionVisible(boolean visible){
guiSpecial.setPreferredSize( visible ? new Dimension(specialSize) : new Dimension(1, 1));
guiSpecial.setVisible(visible);
}
@Override
public void setLayer(final MapLayer layer) {
super.setLayer(layer);
guiSpecial.setLayer(layer);
}
public void setModel(final List<Literal> values){
updatingModel = true;
this.values.clear();
this.values.add(getFilterFactory().literal("-"));
this.values.addAll(values);
guiCombo.setModel(new ListComboBoxModel(this.values));
updatingModel = false;
}
public void setModel(final Literal ... values){
updatingModel = true;
this.values.clear();
this.values.add(getFilterFactory().literal("-"));
for(final Literal lit : values){
this.values.add(lit);
}
guiCombo.setModel(new ListComboBoxModel(this.values));
updatingModel = false;
}
/**
* Allow to modify the prototype element used for combo size calculation.
*
* @param prototypeDisplayValue an element on which combo box will be based to calculate
* its size
* @see JComboBox#setPrototypeDisplayValue(java.lang.Object)
*/
public void setPrototypeDisplayValue(final Object prototypeDisplayValue) {
guiCombo.setPrototypeDisplayValue(specialSize);
}
/**
* Set the prototype display value to the first item in the list.
* Should be called after adding items to do something.
*
* @see #setPrototypeDisplayValue(java.lang.Object)
*/
public void setPrototypeDisplayValueToFirstItem() {
if (guiCombo.getItemCount() > 0) {
setPrototypeDisplayValue(guiCombo.getItemAt(0));
}
}
/** 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() {
guiSpecial = new JSpecialExpressionButton();
guiCombo = new JComboBox();
setOpaque(false);
guiSpecial.addPropertyChangeListener(new PropertyChangeListener() {
public void propertyChange(PropertyChangeEvent evt) {
guiSpecialPropertyChange(evt);
}
});
guiCombo.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent evt) {
guiComboItemStateChanged(evt);
}
});
GroupLayout layout = new GroupLayout(this);
this.setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(guiCombo, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(ComponentPlacement.RELATED)
.addComponent(guiSpecial, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
);
layout.setVerticalGroup(
layout.createParallelGroup(Alignment.LEADING)
.addComponent(guiSpecial, Alignment.TRAILING, GroupLayout.PREFERRED_SIZE, 24, GroupLayout.PREFERRED_SIZE)
.addComponent(guiCombo, Alignment.TRAILING, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
);
layout.linkSize(SwingConstants.VERTICAL, new Component[] {guiCombo, guiSpecial});
}// </editor-fold>//GEN-END:initComponents
private void guiSpecialPropertyChange(final PropertyChangeEvent evt) {//GEN-FIRST:event_guiSpecialPropertyChange
if(evt.getPropertyName().equals(JSpecialExpressionButton.EXPRESSION_PROPERTY)){
parse(guiSpecial.get());
}
}//GEN-LAST:event_guiSpecialPropertyChange
private void guiComboItemStateChanged(ItemEvent evt) {//GEN-FIRST:event_guiComboItemStateChanged
if(!updatingModel && evt.getStateChange() == ItemEvent.SELECTED){
final Expression exp = (Expression) evt.getItem();
if(exp instanceof Literal && "-".equals(((Literal)exp).getValue())){
return;
}
Expression old = null;
parse((Literal)guiCombo.getSelectedItem());
firePropertyChange(PROPERTY_UPDATED, old, create());
}
}//GEN-LAST:event_guiComboItemStateChanged
// Variables declaration - do not modify//GEN-BEGIN:variables
private JComboBox guiCombo;
private JSpecialExpressionButton guiSpecial;
// End of variables declaration//GEN-END:variables
@Override
public void parse(final Expression target) {
if(target != null){
if(values != null && values.contains(target)){
guiSpecial.parse(null);
guiCombo.setSelectedItem(target);
}else{
guiCombo.setSelectedItem(getFilterFactory().literal("-"));
guiSpecial.parse(target);
}
}else{
guiSpecial.parse(null);
guiCombo.setSelectedItem(values.get(1));
}
guiCombo.setEnabled(guiSpecial.get()==null);
guiCombo.setToolTipText(guiSpecial.getToolTipText());
}
@Override
public Expression create() {
final Expression special = guiSpecial.get();
if(special != null){
return special;
}else{
return (Literal)guiCombo.getSelectedItem();
}
}
@Override
protected Object[] getFirstColumnComponents() {
return new Object[]{};
}
}