/* ========================
* JSynoptic : a free Synoptic editor
* ========================
*
* Project Info: http://jsynoptic.sourceforge.net/index.html
*
* This program 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;
* either version 2.1 of the License, or (at your option) any later version.
*
* This program 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.
*
* You should have received a copy of the GNU Lesser General Public License along with this
* program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
* Boston, MA 02111-1307, USA.
*
* (C) Copyright 2001-2006, by :
* Corporate:
* EADS Astrium SAS
* Individual:
* Mathias Choquet
*
*
* $$Id: StrokeDisplay.java,v 1.2 2006/01/18 13:33:23 mathias_choquet Exp $$
*
*/
package jsynoptic.builtin.ui;
import java.awt.BasicStroke;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Insets;
import java.awt.Stroke;
import java.awt.geom.Line2D;
import java.awt.geom.Point2D;
import javax.swing.JComponent;
import javax.swing.JList;
import javax.swing.ListCellRenderer;
import simtools.shapes.StrokeParameters;
/**
* Class StrokeDisplay
* Summary:
* this class represents the display of stroke.
* It extends a graphic component to be displayed in a JComboBox
* (for example)
*/
public class StrokeDisplay extends JComponent implements ListCellRenderer {
/**
* This table represents the default types of
* the strokes in JSynoptic
*/
public static StrokeDisplay[] defaultstrokes={
new StrokeDisplay(), // no dash
new StrokeDisplay(2.f, 2.f,1.f), // strokeParams...
new StrokeDisplay(2.f, 4.f,1.f),
new StrokeDisplay(4.f, 4.f,1.f),
new StrokeDisplay(2.f, 8.f,1.f),
new StrokeDisplay(4.f, 8.f,1.f),
new StrokeDisplay(6.f, 8.f,1.f),
new StrokeDisplay(8.f, 8.f,1.f),
};
private Stroke stroke;
private StrokeParameters strokeParams;
/** The preferred size of the component. */
private Dimension preferredSize;
public StrokeDisplay(){
strokeParams=new StrokeParameters();
stroke=StrokeParameters.createStroke(strokeParams.param1,strokeParams.param2,strokeParams.thickness);
preferredSize = new Dimension(80, 18);
}
public StrokeParameters getStrokeParams(){
return strokeParams;
}
public void setStrokeParams(StrokeParameters dp){
strokeParams = dp;
stroke=StrokeParameters.createStroke(strokeParams.param1,strokeParams.param2,strokeParams.thickness);
}
/**
* Creates a StrokeDisplay for the specified basic stroke.
* @param float p1, float p2 : the necessary datas for
* the construction of a stroke
*/
public StrokeDisplay(float p1, float p2, float thickness) {
strokeParams=new StrokeParameters(p1,p2,thickness);
stroke=StrokeParameters.createStroke(strokeParams.param1,strokeParams.param2,strokeParams.thickness);
preferredSize = new Dimension(80, 18);
}
/**
* Creates a StrokeDisplay for the specified basic stroke.
* @param strokeParams : the data model to build and handle stroke.
*/
public StrokeDisplay(StrokeParameters strokeParams) {
this.strokeParams=new StrokeParameters(strokeParams);
stroke=StrokeParameters.createStroke(strokeParams.param1,strokeParams.param2,strokeParams.thickness);
preferredSize = new Dimension(80, 18);
}
/* (non-Javadoc)
* @see java.awt.Component#getPreferredSize()
*/
public Dimension getPreferredSize() {
return preferredSize;
}
/* (non-Javadoc)
* @see javax.swing.JComponent#paintComponent(java.awt.Graphics)
*/
public void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
Dimension size = getSize();
Insets insets = getInsets();
double xx = insets.left;
double yy = insets.top;
double ww = size.getWidth() - insets.left - insets.right;
double hh = size.getHeight() - insets.top - insets.bottom;
// calculate point one
Point2D one = new Point2D.Double(xx + 6, yy + hh / 2);
// calculate point two
Point2D two = new Point2D.Double(xx + ww - 6, yy + hh / 2);
// draw a line connecting the points
Line2D line = new Line2D.Double(one, two);
if (stroke != null) {
g2.setStroke(stroke);
}
else{
g2.setStroke(new BasicStroke());
}
g2.draw(line);
}
public Component getListCellRendererComponent(JList list, Object value, int index,
boolean isSelected, boolean cellHasFocus) {
stroke=StrokeParameters.createStroke(((StrokeDisplay)value).strokeParams.param1,
((StrokeDisplay)value).strokeParams.param2,
strokeParams.thickness);
repaint();
return this;
}
}