Package org.pentaho.reporting.engine.classic.core.metadata.propertyeditors

Source Code of org.pentaho.reporting.engine.classic.core.metadata.propertyeditors.BasicStrokeEditor$StrokeIcon

/*
* This program is free software; you can redistribute it and/or modify it under the
* terms of the GNU Lesser General Public License, version 2.1 as published by the Free Software
* Foundation.
*
* You should have received a copy of the GNU Lesser General Public License along with this
* program; if not, you can obtain a copy at http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html
* or from the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* 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.
*
* Copyright (c) 2007 - 2009 Pentaho Corporation.  ..  All rights reserved.
*/

package org.pentaho.reporting.engine.classic.core.metadata.propertyeditors;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Rectangle;
import java.awt.RenderingHints;
import java.awt.Shape;
import java.awt.Stroke;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.beans.PropertyEditor;
import javax.swing.BorderFactory;
import javax.swing.DefaultListCellRenderer;
import javax.swing.Icon;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.event.DocumentListener;
import javax.swing.event.DocumentEvent;

import org.pentaho.reporting.engine.classic.core.modules.gui.commonswing.NumericDocument;
import org.pentaho.reporting.engine.classic.core.style.BorderStyle;
import org.pentaho.reporting.engine.classic.core.util.StrokeUtility;
import org.pentaho.reporting.libraries.base.util.StringUtils;
import org.pentaho.reporting.libraries.xmlns.common.ParserUtil;

/**
* @author wseyler
*/
public class BasicStrokeEditor implements PropertyEditor
{

  private BasicStroke value;

  private PropertyChangeSupport propertyChangeSupport;

  public BasicStrokeEditor()
  {
    super();
    value = new BasicStroke();
    propertyChangeSupport = new PropertyChangeSupport(this);
  }

  /* (non-Javadoc)
   * @see java.beans.PropertyEditor#addPropertyChangeListener(java.beans.PropertyChangeListener)
   */
  public void addPropertyChangeListener(PropertyChangeListener listener)
  {
    propertyChangeSupport.addPropertyChangeListener(listener);
  }

  /* (non-Javadoc)
   * @see java.beans.PropertyEditor#removePropertyChangeListener(java.beans.PropertyChangeListener)
   */
  public void removePropertyChangeListener(PropertyChangeListener listener)
  {
    propertyChangeSupport.removePropertyChangeListener(listener);
  }

  /* (non-Javadoc)
   * @see java.beans.PropertyEditor#setValue(java.lang.Object)
   */
  public void setValue(Object value)
  {
    final Object oldValue = this.value;
    if (value instanceof BasicStroke)
    {
      this.value = (BasicStroke) value;
    }
    else
    {
      this.value = null;
    }
    propertyChangeSupport.firePropertyChange(null, oldValue, this.value);
  }

  /* (non-Javadoc)
   * @see java.beans.PropertyEditor#getValue()
   */
  public Object getValue()
  {
    return value;
  }

  /* (non-Javadoc)
   * @see java.beans.PropertyEditor#getAsText()
   */
  public String getAsText()
  {
    final BorderStyle borderStyle = StrokeUtility.translateStrokeStyle(value);
    final float width = value.getLineWidth();
    return borderStyle + ", " + width;
  }

  /* (non-Javadoc)
   * @see java.beans.PropertyEditor#setAsText(java.lang.String)
   */
  public void setAsText(String text) throws IllegalArgumentException
  {
    final String[] strings = StringUtils.split(text, ",");
    if (strings.length == 0)
    {
      setValue(null);
      return;
    }
    if (strings.length == 1)
    {
      final float v = ParserUtil.parseFloat(strings[0].trim(), -1);
      if (v < 0)
      {
        setValue(BorderStyle.getBorderStyle(strings[0].trim()));
        return;
      }
      setValue(new BasicStroke(v));
      return;
    }
    if (strings.length > 2)
    {
      return;
    }

    float width = ParserUtil.parseFloat(strings[0].trim(), -1);
    if (width < 0)
    {
      width = ParserUtil.parseFloat(strings[1].trim(), -1);
    }
    if (width < 0)
    {
      setValue(null);
      return;
    }
    BorderStyle style = BorderStyle.getBorderStyle(strings[0].trim());
    if (style == null)
    {
      style = BorderStyle.getBorderStyle(strings[1].trim());
    }
    if (style == null)
    {
      setValue(null);
    }
    else
    {
      setValue(style.createStroke(width));
    }
  }

  /* (non-Javadoc)
   * @see java.beans.PropertyEditor#supportsCustomEditor()
   */
  public boolean supportsCustomEditor()
  {
    return true;
  }

  /* (non-Javadoc)
   * @see java.beans.PropertyEditor#getCustomEditor()
   */
  public Component getCustomEditor()
  {
    return new StrokeEditorComponent(this);
  }

  /* (non-Javadoc)
   * @see java.beans.PropertyEditor#isPaintable()
   */
  public boolean isPaintable()
  {
    return true;
  }

  /* (non-Javadoc)
   * @see java.beans.PropertyEditor#paintValue(java.awt.Graphics, java.awt.Rectangle)
   */
  public void paintValue(Graphics gfx, Rectangle box)
  {
    if (gfx instanceof Graphics2D)
    {
      Graphics2D graphics2D = (Graphics2D) gfx;
      graphics2D.setStroke(value);
      graphics2D.drawLine(box.x, box.height / 2 + box.y, box.x + box.width, box.height / 2 + box.y);
    }
  }

  /* (non-Javadoc)
   * @see java.beans.PropertyEditor#getJavaInitializationString()
   */
  public String getJavaInitializationString()
  {
    return null;
  }

  /* (non-Javadoc)
   * @see java.beans.PropertyEditor#getTags()
   */
  public String[] getTags()
  {
    return null;
  }

  private static class BorderStyleRenderer extends DefaultListCellRenderer
  {
    public Component getListCellRendererComponent(JList list,
                                                  Object value,
                                                  int index,
                                                  boolean isSelected,
                                                  boolean cellHasFocus)
    {
      JLabel label = (JLabel) super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
      if (value instanceof BorderStyle)
      {
        final BorderStyle style = (BorderStyle) value;
        StrokeIcon strokeIcon = new StrokeIcon(style.createStroke(2));
        label.setIcon(strokeIcon);
      }
      else
      {
        label.setIcon(null);
      }
      label.setText("");
      return label;
    }
  }

  private static class StrokeEditorComponent extends JPanel
  {
    private class DashSelectionHandler implements ActionListener
    {
      private final JComboBox dashComboBox;

      public DashSelectionHandler(final JComboBox dashComboBox)
      {
        this.dashComboBox = dashComboBox;
      }

      public void actionPerformed(ActionEvent e)
      {
        setDashType((BorderStyle) dashComboBox.getSelectedItem());
      }
    }

    private class WidthUpdateHandler implements DocumentListener
    {
      private JTextField textField;

      public WidthUpdateHandler(final JTextField textField)
      {
        this.textField = textField;
      }

      /**
       * Gives notification that there was an insert into the document.  The
       * range given by the DocumentEvent bounds the freshly inserted region.
       *
       * @param e the document event
       */
      public void insertUpdate(final DocumentEvent e)
      {
        final String s = this.textField.getText();
        try
        {
          setStrokeWidth(Float.parseFloat(s.trim()));
        }
        catch (Exception ex)
        {
          // ignored
        }
      }

      /**
       * Gives notification that a portion of the document has been
       * removed.  The range is given in terms of what the view last
       * saw (that is, before updating sticky positions).
       *
       * @param e the document event
       */
      public void removeUpdate(final DocumentEvent e)
      {
        insertUpdate(e);
      }

      /**
       * Gives notification that an attribute or set of attributes changed.
       *
       * @param e the document event
       */
      public void changedUpdate(final DocumentEvent e)
      {
        insertUpdate(e);
      }
    }
    private float width;
    private BorderStyle borderStyle;
    private SamplePanel samplePanel;
    private BasicStrokeEditor editor;

    public StrokeEditorComponent(BasicStrokeEditor editor)
    {
      this.editor = editor;
      samplePanel = new SamplePanel();

      final BasicStroke vb = (BasicStroke) editor.getValue();
      if (vb != null)
      {
        width = vb.getLineWidth();
        borderStyle = StrokeUtility.translateStrokeStyle(vb);
      }
      else
      {
        width = 0;
        borderStyle = BorderStyle.NONE;
      }
      initGUI();
    }

    public void setStrokeWidth(float width)
    {
      if (this.width == width)
      {
        return;
      }
      this.width = width;
      if (borderStyle == null || borderStyle == BorderStyle.NONE)
      {
        this.editor.setValue(null);
        this.samplePanel.setSampleValue(null);
      }
      else
      {
        final Stroke stroke = borderStyle.createStroke(width);
        this.editor.setValue(stroke);
        this.samplePanel.setSampleValue(stroke);
      }
    }

    public void setDashType(BorderStyle type)
    {
      if (this.borderStyle == type)
      {
        return;
      }
      this.borderStyle = type;

      if (borderStyle == null || borderStyle == BorderStyle.NONE)
      {
        this.editor.setValue(null);
        this.samplePanel.setSampleValue(null);
      }
      else
      {
        final Stroke stroke = borderStyle.createStroke(width);
        this.editor.setValue(stroke);
        this.samplePanel.setSampleValue(stroke);
      }
    }

    /**
     *
     */
    private void initGUI()
    {
      this.setLayout(new GridBagLayout());
      GridBagConstraints constraints = new GridBagConstraints();

      // Add the width label
      constraints.gridx = 0;
      constraints.gridy = 0;
      constraints.anchor = GridBagConstraints.EAST;
      this.add(new JLabel("Width:"), constraints);

      // Add the spinner with its model
      constraints.gridx = 1;
      constraints.gridy = 0;
      constraints.anchor = GridBagConstraints.WEST;
      JTextField strokeWidthField = new JTextField();
      strokeWidthField.getDocument().addDocumentListener(new WidthUpdateHandler(strokeWidthField));
      strokeWidthField.setText(String.valueOf(width));
      strokeWidthField.setColumns(6);
      this.add(strokeWidthField, constraints);

      // Add the dash Label
      constraints.gridx = 0;
      constraints.gridy = 1;
      constraints.anchor = GridBagConstraints.CENTER;
      this.add(new JLabel("Dashes:"), constraints);

      // Add the dash comboBox
      constraints.gridx = 1;
      constraints.gridy = 1;

      final JComboBox dashComboBox = new JComboBox(new Object[]{BorderStyle.SOLID, BorderStyle.DASHED,
          BorderStyle.DOTTED, BorderStyle.DOT_DASH, BorderStyle.DOT_DOT_DASH});
      dashComboBox.setRenderer(new BorderStyleRenderer());
      dashComboBox.setSelectedItem(borderStyle);
      dashComboBox.addActionListener(new DashSelectionHandler(dashComboBox));
      this.add(dashComboBox, constraints);

      // Add the sample box
      constraints.gridx = 0;
      constraints.gridy = 2;
      constraints.gridwidth = GridBagConstraints.REMAINDER;
      constraints.fill = GridBagConstraints.HORIZONTAL;

      this.add(samplePanel, constraints);
    }
  }

  private static class SamplePanel extends JPanel
  {
    private Stroke sampleValue;

    public SamplePanel()
    {
      setBorder(BorderFactory.createTitledBorder("Sample"));
      this.setMinimumSize(new Dimension(40, 40));
    }

    public void paintComponent(Graphics g)
    {
      super.paintComponent(g);
      if (sampleValue != null)
      {
        Graphics2D g2d = (Graphics2D) g;
        Stroke origStroke = g2d.getStroke();
        Color origColor = g2d.getColor();
        Shape origClip = g2d.getClip();

        g2d.setStroke(sampleValue);

        g2d.drawLine(getInsets().left + 1, (getHeight() / 2) + 1, getWidth() - (getInsets().right + 1), (getHeight() / 2) + 1);

        g2d.setClip(origClip);
        g2d.setStroke(origStroke);
        g2d.setColor(origColor);
      }

    }

    public Stroke getSampleValue()
    {
      return sampleValue;
    }

    public void setSampleValue(final Stroke sampleValue)
    {
      this.sampleValue = sampleValue;
      repaint();
    }
  }

  private static class StrokeIcon implements Icon
  {
    private Stroke basicStroke;

    private StrokeIcon(Stroke basicStroke)
    {
      this.basicStroke = basicStroke;
    }

    public void paintIcon(Component c, Graphics g, int x, int y)
    {
      Graphics2D g2d = (Graphics2D) g;
      Color origColor = g2d.getColor();
      Object hint = g2d.getRenderingHint(RenderingHints.KEY_ANTIALIASING);
      g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
      Stroke origStroke = g2d.getStroke();

      g2d.translate(x, y);

      g2d.setColor(Color.BLACK);
      g2d.setStroke(basicStroke);

      g2d.drawLine(0, getIconHeight() / 2, getIconWidth(), getIconHeight() / 2);
      g2d.setColor(origColor);

      g2d.translate(-x, -y);
      g2d.setStroke(origStroke);
      g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, hint);
    }

    public int getIconWidth()
    {
      return 100;
    }

    public int getIconHeight()
    {
      return 20;
    }
  }
}
TOP

Related Classes of org.pentaho.reporting.engine.classic.core.metadata.propertyeditors.BasicStrokeEditor$StrokeIcon

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.
ush(arguments)},i[r].l=1*new Date();a=s.createElement(o), m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m) })(window,document,'script','//www.google-analytics.com/analytics.js','ga'); ga('create', 'UA-20639858-1', 'auto'); ga('send', 'pageview');