/*
* 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.GraphicsEnvironment;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.Arrays;
import java.util.List;
import javax.swing.GroupLayout;
import javax.swing.JLabel;
import javax.swing.LayoutStyle;
import javax.swing.SwingConstants;
import org.geotoolkit.filter.DefaultLiteral;
import org.geotoolkit.gui.swing.resource.MessageBundle;
import org.geotoolkit.map.MapLayer;
import org.geotoolkit.style.StyleConstants;
import org.opengis.filter.expression.Literal;
import org.opengis.style.Font;
/**
* Font panel
*
* @author Johann Sorel
* @module
*/
public class JFontPane extends StyleElementEditor<Font>{
private MapLayer layer = null;
private final Literal[] fontFamilies;
/** Creates new form JfontPanel */
public JFontPane() {
super(Font.class);
initComponents();
//Initialize family font list with available font family
final GraphicsEnvironment environment = GraphicsEnvironment.getLocalGraphicsEnvironment();
final String[] fontNames = environment.getAvailableFontFamilyNames();
final List<String> fontNamesList = Arrays.asList(fontNames);
final int nbFamilies = fontNamesList.size();
fontFamilies = new Literal[nbFamilies];
for (int i=0; i<nbFamilies; i++) {
fontFamilies[i] = new DefaultLiteral(fontNamesList.get(i));
}
init();
}
private void init(){
guiSize.setModel(12d, 0d, Double.POSITIVE_INFINITY, 1d);
guiStyle.setModel(
StyleConstants.FONT_STYLE_NORMAL,
StyleConstants.FONT_STYLE_ITALIC,
StyleConstants.FONT_STYLE_OBLIQUE);
guiWeight.setModel(
StyleConstants.FONT_WEIGHT_NORMAL,
StyleConstants.FONT_WEIGHT_BOLD);
guiFamily.setModel(fontFamilies);
guiFamily.setSize(200, guiFamily.getPreferredSize().height);
// guiFamily.setPrototypeDisplayValueToFirstItem();
}
@Override
public void parse(final Font font){
if(font != null){
if(!font.getFamily().isEmpty()){
guiFamily.parse(font.getFamily().get(0));
}else{
guiFamily.parse(getFilterFactory().literal("Dialog"));
}
guiSize.parse(font.getSize());
guiStyle.parse(font.getStyle());
guiWeight.parse(font.getWeight());
}
}
@Override
public Font create(){
return getStyleFactory().font(
guiFamily.create(),
guiStyle.create(),
guiWeight.create(),
guiSize.create());
}
@Override
public void setLayer(final MapLayer layer){
guiFamily.setLayer(layer);
guiSize.setLayer(layer);
guiStyle.setLayer(layer);
guiWeight.setLayer(layer);
this.layer = layer;
}
@Override
public MapLayer getLayer(){
return layer;
}
public void setExpressionVisible(final boolean visible) {
guiFamily.setExpressionVisible(visible);
guiSize.setExpressionVisible(visible);
guiStyle.setExpressionVisible(visible);
guiWeight.setExpressionVisible(visible);
}
@Override
protected Object[] getFirstColumnComponents() {
return new Object[]{guiLabelFamily,guiLabelSize,guiLabelStyle,guiLabelWeight};
}
/** 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() {
guiLabelFamily = new JLabel();
guiFamily = new JComboExpressionPane();
guiLabelStyle = new JLabel();
guiStyle = new JComboExpressionPane();
guiLabelSize = new JLabel();
guiSize = new JNumberExpressionPane();
guiLabelWeight = new JLabel();
guiWeight = new JComboExpressionPane();
setOpaque(false);
guiLabelFamily.setText(MessageBundle.format("family")); // NOI18N
guiFamily.addPropertyChangeListener(new PropertyChangeListener() {
public void propertyChange(PropertyChangeEvent evt) {
JFontPane.this.propertyChange(evt);
}
});
guiLabelStyle.setText(MessageBundle.format("style")); // NOI18N
guiStyle.addPropertyChangeListener(new PropertyChangeListener() {
public void propertyChange(PropertyChangeEvent evt) {
JFontPane.this.propertyChange(evt);
}
});
guiLabelSize.setText(MessageBundle.format("size")); // NOI18N
guiSize.addPropertyChangeListener(new PropertyChangeListener() {
public void propertyChange(PropertyChangeEvent evt) {
JFontPane.this.propertyChange(evt);
}
});
guiLabelWeight.setText(MessageBundle.format("weight")); // NOI18N
guiWeight.addPropertyChangeListener(new PropertyChangeListener() {
public void propertyChange(PropertyChangeEvent evt) {
JFontPane.this.propertyChange(evt);
}
});
GroupLayout layout = new GroupLayout(this);
this.setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addComponent(guiLabelSize)
.addComponent(guiLabelFamily)
.addComponent(guiLabelStyle, GroupLayout.Alignment.TRAILING))
.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addComponent(guiSize, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.TRAILING, false)
.addComponent(guiStyle, GroupLayout.Alignment.LEADING, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(guiFamily, GroupLayout.Alignment.LEADING, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
.addGroup(layout.createSequentialGroup()
.addComponent(guiLabelWeight)
.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
.addComponent(guiWeight, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)))
.addGap(6, 6, 6))
);
layout.linkSize(SwingConstants.HORIZONTAL, new Component[] {guiLabelFamily, guiLabelSize, guiLabelStyle, guiLabelWeight});
layout.setVerticalGroup(
layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addComponent(guiFamily, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addComponent(guiLabelFamily))
.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(guiStyle, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addGap(12, 12, 12)
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.TRAILING)
.addComponent(guiSize, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addComponent(guiLabelSize))
.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addComponent(guiLabelWeight)
.addComponent(guiWeight, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)))
.addComponent(guiLabelStyle)))
);
layout.linkSize(SwingConstants.VERTICAL, new Component[] {guiFamily, guiLabelFamily});
layout.linkSize(SwingConstants.VERTICAL, new Component[] {guiLabelStyle, guiStyle});
layout.linkSize(SwingConstants.VERTICAL, new Component[] {guiLabelSize, guiSize});
layout.linkSize(SwingConstants.VERTICAL, new Component[] {guiLabelWeight, guiWeight});
}// </editor-fold>//GEN-END:initComponents
private void propertyChange(PropertyChangeEvent evt) {//GEN-FIRST:event_propertyChange
// TODO add your handling code here:
if (PROPERTY_UPDATED.equalsIgnoreCase(evt.getPropertyName())) {
firePropertyChange(PROPERTY_UPDATED, null, create());
}
}//GEN-LAST:event_propertyChange
// Variables declaration - do not modify//GEN-BEGIN:variables
private JComboExpressionPane guiFamily;
private JLabel guiLabelFamily;
private JLabel guiLabelSize;
private JLabel guiLabelStyle;
private JLabel guiLabelWeight;
private JNumberExpressionPane guiSize;
private JComboExpressionPane guiStyle;
private JComboExpressionPane guiWeight;
// End of variables declaration//GEN-END:variables
}