Package DisplayProject.swingdisplayer

Source Code of DisplayProject.swingdisplayer.ColorDisplayer$EditorComponent

/*
Copyright (c) 2003-2009 ITerative Consulting Pty Ltd. All Rights Reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted
provided that the following conditions are met:

o Redistributions of source code must retain the above copyright notice, this list of conditions and
the following disclaimer.
 
o Redistributions in binary form must reproduce the above copyright notice, this list of conditions
and the following disclaimer in the documentation and/or other materials provided with the distribution.
   
o This jcTOOL Helper Class software, whether in binary or source form may not be used within,
or to derive, any other product without the specific prior written permission of the copyright holder

 
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.


*/
package DisplayProject.swingdisplayer;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;
import java.util.EventObject;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JColorChooser;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.ListCellRenderer;
import javax.swing.plaf.basic.BasicComboBoxEditor;

import DisplayProject.UIutils;

@SuppressWarnings("serial")
public class ColorDisplayer extends PropertyDisplayer {
  private static Color[] colors = {
    UIutils.White,
    UIutils.Gray1,
    UIutils.Gray2,
    UIutils.Gray3,
    UIutils.Gray4,
    UIutils.Gray5,
    UIutils.Gray6,
    UIutils.Gray7,
    UIutils.Black,
    UIutils.PaleYellow,
    UIutils.Yellow,
    UIutils.BrightYellow,
    UIutils.PaleGreen,
    UIutils.Green,
    UIutils.BrightGreen,
    UIutils.PaleCyan,
    UIutils.Cyan,
    UIutils.BrightCyan,
    UIutils.PaleBlue,
    UIutils.Blue,
    UIutils.BrightBlue,
    UIutils.PaleMagenta,
    UIutils.Magenta,
    UIutils.BrightMagenta,
    UIutils.PaleRed,
    UIutils.Red,
    UIutils.BrightRed,
    UIutils.PaleBrown,
    UIutils.Brown,
    UIutils.BrightBrown
  };

  private static String[] colorNames = {
    "C_WHITE",
    "C_GRAY1",
    "C_GRAY2",
    "C_GRAY3",
    "C_GRAY4",
    "C_GRAY5",
    "C_GRAY6",
    "C_GRAY7",
    "C_BLACK",
    "C_PALEYELLOW",
    "C_YELLOW",
    "C_BRIGHTYELLOW",
    "C_PALEGREEN",
    "C_GREEN",
    "C_BRIGHTGREEN",
    "C_PALECYAN",
    "C_CYAN",
    "C_BRIGHTCYAN",
    "C_PALEBLUE",
    "C_BLUE",
    "C_BRIGHTBLUE",
    "C_PALEMAGENTA",
    "C_MAGENTA",
    "C_BRIGHTMAGENTA",
    "C_PALERED",
    "C_RED",
    "C_BRIGHTRED",
    "C_PALEBROWN",
    "C_BROWN",
    "C_BRIGHTBROWN"
  };

  private String getColorName(Color c) {
    if (c == null) {
      return "C_INHERIT";
    }
    else {
      for (int i = 0; i < colors.length; i++) {
        if (colors[i].equals(c)) {
          return colorNames[i];
        }
      }
      return "[r=" + c.getRed() + ",g=" + c.getGreen() + ",b=" + c.getBlue() + "]";
    }
  }
 
  private class RendererComponent extends JPanel {
    private Color color = null;
    private JLabel label;
    private JPanel panel;
    public RendererComponent() {
      this.setLayout(new BorderLayout());
      this.setOpaque(true);
      this.panel = new JPanel() {
        @Override
        protected void paintComponent(Graphics g) {
          int border = 3;
          Color oldColor = g.getColor();
          if (color != null) {
            g.setColor(color);
            g.fillRect(border, border, this.getWidth() - 2*border, this.getHeight() - 2*border);
          }
          else {
            g.setColor(Color.black);
            g.drawLine(border, border, this.getWidth() - border, this.getHeight() - border);
            g.drawLine(this.getWidth() - border, border, border, this.getHeight() - border);
          }
          g.setColor(Color.black);
          g.drawRect(border, border, this.getWidth() - 2*border, this.getHeight() - 2*border);
          g.setColor(oldColor);
        }
       
        @Override
        public Dimension getPreferredSize() {
          int height = this.getHeight();
          return new Dimension(height, height);
        }
      };
      this.panel.setOpaque(true);
      this.label = new JLabel();
      this.label.setForeground(null);
      this.add(this.panel, BorderLayout.WEST);
      this.add(this.label, BorderLayout.CENTER);
    }
   
    public void setColor(Color c) {
      this.color = c;
      this.label.setText(getColorName(c));
    }
  }

  private RendererComponent colorRenderer = new RendererComponent();
 
  public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected,
          boolean isFocused, boolean isReadOnly, Component owningComp, int row, int column) {
    if (value instanceof Color) {
      colorRenderer.setColor((Color)value);
      return colorRenderer;
    }
    return new JLabel("not a color");
  }

  private class EditorComponent extends JComboBox {
    private Color color;
    private JTable table;
    private final String otherSelection = "Other...";
    private final String inheritSelection = "Inherit";
    private RendererComponent colorEditor = new RendererComponent();
   
    public EditorComponent(JTable t, Color c) {
      super();
      this.setBorder(null);
      color = c;
      this.table = t;
      this.setEditor(new BasicComboBoxEditor() {
        public Component getEditorComponent() {
          return colorEditor;
        }

        public Object getItem() {
          return color;
        }

        public void setItem(Object anObject) {
          if (anObject instanceof Color) {
            color = (Color)anObject;
          }
          else if (anObject instanceof String) {
            color = Color.CYAN;
          }
        }
      });
      this.setRenderer(new ListCellRenderer() {
        public Component getListCellRendererComponent(JList list,
            Object value, int index, boolean isSelected,
            boolean cellHasFocus) {
         
          Component result;
          if (value instanceof Color || value == null) {
            colorRenderer.setColor((Color)value);
            result = colorRenderer;
          }
          else if (value instanceof String) {
            String str = (String)value;
            if (inheritSelection.equals(str)) {
              colorRenderer.setColor(null);
              result = colorRenderer;
            }
            else {
              result = new JLabel((String)value);
              ((JLabel)result).setOpaque(true);
            }
          }
          else {
            return null;
          }
          if (isSelected) {
            result.setBackground(table.getSelectionBackground());
            result.setForeground(table.getSelectionForeground());
          }
          else {
            result.setBackground(table.getBackground());
            result.setForeground(table.getForeground());
          }
          return result;
        }
      });
     
      this.setModel(new DefaultComboBoxModel() {
        @Override
        public Object getElementAt(int index) {
          if (index >= 0 && index < colors.length) {
            return colors[index];
          }
          else if (index == colors.length) {
            return null// C_INHERIT
          }
          return otherSelection;
        }
        @Override
        public int getSize() {
          return colors.length + 2;
        }

        @Override
        public void setSelectedItem(Object anObject) {
          if (anObject instanceof String) {
            Color result = JColorChooser.showDialog(EditorComponent.this, "Choose a color", color);
            if (result != null) {
              color = result;
              super.setSelectedItem(color);
            }
          }
          else if (anObject == null) {
            color = null;
            super.setSelectedItem(inheritSelection);
          }
          else if (anObject instanceof Color) {
            color = (Color)anObject;
            super.setSelectedItem(color);
          }
          else {
            super.setSelectedItem(anObject);
          }
        }
        @Override
        public Object getSelectedItem() {
          Object result = super.getSelectedItem();
          if (result == null) {
            if (hasFocus() && !isPopupVisible()) {
              return color;
            }
            else {
              return otherSelection;
            }
          }
          else {
            return result;
          }
        }
      });
      this.getModel().setSelectedItem(c);
      this.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
          if (descriptor != null) {
            Method writer = descriptor.getWriteMethod();
            try {
              writer.invoke(owningComponent, editorComponent.color);
            } catch (Exception e1) {
              e1.printStackTrace();
            }
          }
        }
      });
    }
  }
 
  private PropertyDescriptor descriptor;
  private Component owningComponent;
 
  private EditorComponent editorComponent;
  public Component getTableCellEditorComponent(JTable table,
      Object value, boolean isSelected, int row, int column) {
   
    editorComponent = new EditorComponent(table, (Color)value);
    return editorComponent;
  }

  public void cancelCellEditing() {
    super.cancelCellEditing();
    editorComponent = null;
  }

  public Object getCellEditorValue() {
    if (editorComponent != null) {
      return editorComponent.getSelectedItem();
    }
    return null;
  }

  public boolean isCellEditable(EventObject anEvent) {
    return true;
  }

  public boolean stopCellEditing() {
    // Do nothing as this was already mapped during the change event
    return true;
  }
 
  public void setEditingInformation(Component c, PropertyDescriptor pd) {
    this.owningComponent = c;
    this.descriptor = pd;
  }
}
TOP

Related Classes of DisplayProject.swingdisplayer.ColorDisplayer$EditorComponent

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.