/*
* Geotoolkit - An Open Source Java GIS Toolkit
* http://www.geotoolkit.org
*
* (C) 2012 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.style;
import java.awt.BorderLayout;
import java.awt.Component;
import java.util.List;
import javax.swing.DefaultListCellRenderer;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JList;
import org.geotoolkit.gui.swing.resource.MessageBundle;
import org.geotoolkit.map.MapLayer;
import org.jdesktop.swingx.combobox.ListComboBoxModel;
import org.opengis.style.Symbolizer;
/**
* BankManager of preview style
*
* @author Fabien Rétif (Geomatys)
*/
public class JBankPanel extends StyleElementEditor {
/**
* Bank model where style are stored
*/
private final StyleBank model = StyleBank.getInstance();
/**
* Part of the bank view
*/
private JBankView bankView = null;
private MapLayer layer = null;
private Object style = null;
/**
* Creates new form JBankPanel
*/
public JBankPanel() {
super(Object.class);
initComponents();
bankView = new JBankView(Object.class);
bankView.setCandidates(model.getCandidates(null));
add(BorderLayout.CENTER, bankView);
guiTypeSymbolizerList.setRenderer(new CategorieRenderer());
}
public Object getSelectedSymbol() {
return bankView.create();
}
public void setClazzList(List<Class> clazzList) {
guiTypeSymbolizerList.setModel(new ListComboBoxModel(clazzList));
//Update display
bankView.setCandidates(
model.getCandidates(new StyleBank.ByClassComparator(clazzList.toArray(new Class[0]))));
}
@Override
public void setLayer(final MapLayer layer) {
this.layer = layer;
}
@Override
public MapLayer getLayer() {
return layer;
}
@Override
public void parse(final Object style) {
if (style != null) {
this.style = style;
}
}
@Override
public Object create() {
return style;
}
@Override
protected Object[] getFirstColumnComponents() {
return new Object[]{};
}
/**
* 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() {
jPanel1 = new javax.swing.JPanel();
jLabel1 = new javax.swing.JLabel();
guiTypeSymbolizerList = new javax.swing.JComboBox();
guiImportButton = new javax.swing.JButton();
setLayout(new java.awt.BorderLayout());
jLabel1.setText("Catégories :");
guiTypeSymbolizerList.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
guiTypeSymbolizerList.addItemListener(new java.awt.event.ItemListener() {
public void itemStateChanged(java.awt.event.ItemEvent evt) {
guiTypeSymbolizerListItemStateChanged(evt);
}
});
guiImportButton.setText("Importer depuis un fichier");
guiImportButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
guiImportButtonActionPerformed(evt);
}
});
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addComponent(jLabel1)
.addGap(18, 18, 18)
.addComponent(guiTypeSymbolizerList, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 411, Short.MAX_VALUE)
.addComponent(guiImportButton)
.addContainerGap())
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel1)
.addComponent(guiTypeSymbolizerList, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(guiImportButton)))
);
add(jPanel1, java.awt.BorderLayout.PAGE_START);
}// </editor-fold>//GEN-END:initComponents
private void guiImportButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_guiImportButtonActionPerformed
// TODO add your handling code here:
final JFileChooser chooser = new JFileChooser();
final int result = chooser.showOpenDialog(this);
if (result == JFileChooser.APPROVE_OPTION) {
model.addFromFile(chooser.getSelectedFile());
}
}//GEN-LAST:event_guiImportButtonActionPerformed
private void guiTypeSymbolizerListItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_guiTypeSymbolizerListItemStateChanged
//Update display
bankView.setCandidates(model.getCandidates(new StyleBank.ByClassComparator(new Class[]{(Class) evt.getItem()})));
}//GEN-LAST:event_guiTypeSymbolizerListItemStateChanged
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JButton guiImportButton;
private javax.swing.JComboBox guiTypeSymbolizerList;
private javax.swing.JLabel jLabel1;
private javax.swing.JPanel jPanel1;
// End of variables declaration//GEN-END:variables
/**
* Redefines ListRenderer in order to display preview of stroke
*/
private static class CategorieRenderer extends DefaultListCellRenderer {
@Override
public Component getListCellRendererComponent(JList jlist, Object o, int i, boolean bln, boolean bln1) {
JLabel label = (JLabel) super.getListCellRendererComponent(jlist, o, i, bln, bln1);
if (o.equals(Symbolizer.class)) {
label.setText(MessageBundle.format("all"));
} else {
label.setText(((Class) o).getSimpleName());
}
return label;
}
}
}