Package jsynoptic.ui

Source Code of jsynoptic.ui.DiagramSelectionPropertiesPanel$LineColorEditorPanel

/* ========================
* 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-2005, by :
*     Corporate:
*         EADS Astrium SAS
*         EADS CRC
*     Individual:
*         Claude Cazenave
*
* $Id: DiagramSelectionPropertiesPanel.java,v 1.7 2009/01/08 15:56:59 ogor Exp $
*
* Changes
* -------
* 1 juil. 2005 : Initial public release (CC);
*
*/
package jsynoptic.ui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.util.Vector;

import javax.swing.AbstractAction;
import javax.swing.Icon;
import javax.swing.JButton;
import javax.swing.JColorChooser;
import javax.swing.JOptionPane;
import javax.swing.event.UndoableEditEvent;
import javax.swing.undo.CompoundEdit;

import simtools.diagram.DiagramSelection;
import simtools.shapes.AbstractShape;
import simtools.ui.JPropertiesPanel;
import simtools.ui.MenuResourceBundle;
import simtools.ui.ResourceFinder;
import simtools.util.NamedProperties;

/**
* A class to apply a property change to all the selected shapes
* The first selected shape which contains one of the selected properties
* is used as a template
* The propeties values are then displayed and edited.
* If the user confirms then new values are applied on all the selected shapes
* The action is launched by clicking on a button
* @see getButton()
* @author cazenave_c
*/
public abstract class DiagramSelectionPropertiesPanel extends JPropertiesPanel {

  protected final String name;
  protected final String title;
  protected final Icon firstIcon;
  protected JButton button;
  protected final SelectionAction action;
  protected final String toolTip;
  protected String propertyName;
 
  /**
   * @param icon the button name
   * @param icon the button icon
   * @param title the properties editor dialog title
   * @param tip the button tool tip
   * @param propertyName the property name, assuming there is only one
   */
  public DiagramSelectionPropertiesPanel(String name, Icon icon,
      String title, String tip, String propertyName) {
    super(null);
    this.name=name;
    this.title=title;
    this.firstIcon=icon;
    this.propertyName=propertyName;
    this.toolTip=tip;
   
    action=new SelectionAction(title,icon);
  }
 
  public JButton createButton(){
    button=new JButton(action);
    button.setText(name);
    button.setToolTipText(toolTip);
    return button;
  }

  public SelectionAction getAction(){
    return action;
  }

  public String getToolTip(){
    return toolTip;
  }

  protected void doAction(ActionEvent e) {
    final ShapesContainer.ShapesComponent sc=JSynoptic.gui.getActiveComponent();
    if(sc==null){
      return;
    }
    DiagramSelection sel=sc.getDiagramSelection();
    final Vector v=sel.getSelectedElements();
    // look for properties initial values
    final NamedProperties initalValues=containsValidProperties(v);
    if(initalValues==null){
      return;
    }
    new LongAction(LongAction.LONG_ACTION_SHAPE, sel, e.getSource()) {
      protected void doAction() {
        setProperties(initalValues);
        int result = JOptionPane.showConfirmDialog(JSynoptic.gui.getOwner(),
            DiagramSelectionPropertiesPanel.this,
            title, JOptionPane.OK_CANCEL_OPTION,
            JOptionPane.PLAIN_MESSAGE);
        if (result == JOptionPane.OK_OPTION) {
          // update the button
          update();
         
          // update the selection
          CompoundEdit ce=new CompoundEdit();
          boolean ok=false;
          for(int i=0;i<v.size();i++){
            Object o=v.get(i);
            if(o instanceof AbstractShape){
              AbstractShape as=(AbstractShape)o;
              if(as.setProperties(DiagramSelectionPropertiesPanel.this, ce)){
                ok=true;
              }
            }
          }
          if(ok){
            ce.end();
            sc.fireUndoableEditUpdate(new UndoableEditEvent(sc, ce));
            sc.repaint();
          }
        }
      }
    }.start();
  }

  /* (non-Javadoc)
   * @see simtools.util.NamedProperties#getPropertyNames()
   */
  public String[] getPropertyNames() {
    return new String[]{propertyName};
  }
 
  /**
   * Check if object contains one of the valid properties
   * This implementation deals with a single property and can
   * be overloaded for more complex cases
   * @param np the object with properties
   * @return true if ok
   */
  protected boolean containsValidProperties(NamedProperties np){
    String[] names=np.getPropertyNames();
    if(names==null){
      return false;
    }
    for(int i=0;i<names.length;i++){
      if(names[i].equalsIgnoreCase(propertyName)){
        return true;
      }
    }
    return false;
  }
 
  /**
   * Check if one object contains one of the valid properties
   * @param v the list of objects
   * @return the first matching object or null if none
   */
  protected NamedProperties containsValidProperties(Vector v) {
    NamedProperties initalValues = null;
    for (int i = 0; i < v.size(); i++) {
      Object o = v.get(i);
      if (o instanceof NamedProperties) {
        NamedProperties np = (NamedProperties) o;
        if (containsValidProperties(np)) {
          setProperties(np);
          initalValues = np;
          break;
        }
      }
    }
    return initalValues;
  }

  /**
   * default behavior
   */
  protected void update(){
    if(propertyName.indexOf("COLOR")>=0){
      action.firePropertyChange("background", null, getPropertyValue(propertyName));
      if(button!=null){
        button.setBackground((Color)getPropertyValue(propertyName));
      }
    }
  }
 
  public class SelectionAction extends AbstractAction {

    /**
     * @param name
     * @param icon
     */
    public SelectionAction(String name, Icon icon) {
      super(name,icon);
    }

    /* (non-Javadoc)
     * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
     */
    public void actionPerformed(ActionEvent e) {
      doAction(e);
    }
   
   
    /* (non-Javadoc)
     * @see javax.swing.AbstractAction#firePropertyChange(java.lang.String, java.lang.Object, java.lang.Object)
     */
    protected void firePropertyChange(String propertyName, Object oldValue,
        Object newValue) {
      // to get access from outer class
      super.firePropertyChange(propertyName, oldValue, newValue);
    }

    /**
     * @param v
     */
    public boolean update(Vector v) {
      NamedProperties np=containsValidProperties(v);
      if(np!=null){
        setProperties(np);
        DiagramSelectionPropertiesPanel.this.update();
        return true;
      }
      return false;
    }
  }
 
  /** Resources */
  public static MenuResourceBundle resources = ResourceFinder
      .getMenu(DiagramSelectionPropertiesPanel.class);

 
  public static class ColorEditorPanel extends DiagramSelectionPropertiesPanel{

    JColorChooser colorChooser;
   
    /**
     */
    public ColorEditorPanel(Icon icon, String title, String tipText,
        String colorPropertyName) {
      super("", icon,
          title,
          tipText,
          colorPropertyName);
      setLayout(new BorderLayout());
      add(colorChooser=new JColorChooser(),BorderLayout.CENTER);
    }

    /* (non-Javadoc)
     * @see simtools.ui.JPropertiesPanel#getPropertyValue(java.lang.String)
     */
    public Object getPropertyValue(String name) {
      Object res = null;
      if (name.equalsIgnoreCase(propertyName)) {
        res = colorChooser.getColor();
      }
      return res;
    }

    /* (non-Javadoc)
     * @see simtools.util.NamedProperties#setPropertyValue(java.lang.String, java.lang.Object)
     */
    public void setPropertyValue(String name, Object value) {
      if (name.equalsIgnoreCase(propertyName) && (value instanceof Color)) {
        colorChooser.setColor((Color)value);
      }
    }
  }
 
  public static class LineColorEditorPanel extends ColorEditorPanel {
   
    /**
     */
    public LineColorEditorPanel() {
      super(resources.getIcon("lineColorIcon"),
          resources.getStringValue("lineColorTitle"),
          resources.getStringValue("lineColorTip"),
          "STROKE_COLOR");
    }
  }
 
  public static class BackgroundColorEditorPanel extends ColorEditorPanel {
   
    /**
     */
    public BackgroundColorEditorPanel() {
      super(resources.getIcon("backgroundColorIcon"),
          resources.getStringValue("backgroundColorTitle"),
          resources.getStringValue("backgroundColorTip"),
          "FILL_COLOR");
    }
  }

  public static class FontColorEditorPanel extends ColorEditorPanel {
   
    /**
     */
    public FontColorEditorPanel() {
      super(resources.getIcon("fontColorIcon"),
          resources.getStringValue("fontColorTitle"),
          resources.getStringValue("fontColorTip"),
          "TEXT_COLOR");
    }
  }

}
TOP

Related Classes of jsynoptic.ui.DiagramSelectionPropertiesPanel$LineColorEditorPanel

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.