/*
* Title: japlot.jaxodraw
* Description: Graphical user interface for drawing Feynman diagrams
* @author Daniele Binosi
* @author Lukas Theussl
Copyright (C) 2003-2006, Daniele Binosi and Lukas Theussl
See the file LICENSE in the source distribution home directory
for a full copy of the GPL (GNU General Public License).
This file is part of japlot.jaxodraw.
japlot.jaxodraw is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
japlot.jaxodraw 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 General Public License for more details.
You should have received a copy of the GNU 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
*/
package japlot.jaxodraw;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.image.MemoryImageSource;
import java.util.ResourceBundle;
import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JColorChooser;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JToggleButton;
import javax.swing.border.Border;
import javax.swing.colorchooser.AbstractColorChooserPanel;
import javax.swing.colorchooser.ColorSelectionModel;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import jhplot.jadraw.JaObject;
/** This class allows to choose a color from a menu. The colors presenteed are
* those available in the colrdvi LaTex package, plus a set of gray scales.
*/
public class JaxoColorChooser {
private ResourceBundle language =
ResourceBundle.getBundle(JaxoPrefs.getPref(JaxoPrefs.PREF_LANGUAGE));
private Color newcolor;
/**
* Brings up a color chooser menu to select the color
* of the specified JaObject.
* @return The chosen color if the OK option is selected,
* if the CANCEL option is selected, the color of the selectedObject.
* @param fCol True if the color to be chosen is a fill color,
* false otherwise.
* @param selectedObject The JaObject whose color is to be chosen.
*/
public final Color color(JaObject selectedObject, boolean fCol) {
final Color oldColor = selectedObject.getColor();
final JColorChooser tcc = new JColorChooser();
tcc.getSelectionModel().addChangeListener(new ChangeListener() {
public final void stateChanged(ChangeEvent e) {
// return;
}
});
tcc.setColor(selectedObject.getColor());
JaxoColorPreviewPanel previewPanel = new JaxoColorPreviewPanel(tcc);
previewPanel.setSize(previewPanel.getPreferredSize());
tcc.setPreviewPanel(previewPanel);
ActionListener okListener =
new ActionListener() {
public final void actionPerformed(ActionEvent evt) {
newcolor = tcc.getColor();
}
};
ActionListener cancelListener =
new ActionListener() {
public final void actionPerformed(ActionEvent evt) {
newcolor = oldColor;
}
};
AbstractColorChooserPanel[] panels = {new JaxoColorPanel(fCol)};
tcc.setChooserPanels(panels);
//tcc.setBorder(BorderFactory.createTitledBorder("Choose a Color"));
tcc.setBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4));
tcc.setPreferredSize(new Dimension(500, 300));
// Make the dialog appear to change the color
boolean bool = true;
JDialog dialog =
JColorChooser.createDialog(null,
language.getString("Choose_a_Color"), bool, tcc, okListener,
cancelListener);
//dialog. setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
dialog.addWindowListener(new WindowAdapter() {
public final void windowClosing(WindowEvent we) {
newcolor = oldColor;
}
});
dialog.setVisible(true);
dialog.setEnabled(false);
dialog.pack();
return newcolor;
}
/**
* Brings up a color chooser menu to select the color of the
* specified JaObject.
* @return The chosen color if the OK option is selected,
* the color color, if the CANCEL option is selected.
* @param fCol True if the color to be chosen is a fill color,
* false otherwise.
* @param color The color to be returned if the CANCEL option is
* chosen in the color chooser dialog.
*/
public final Color color(Color color, boolean fCol) {
final Color oldColor = color;
final JColorChooser tcc = new JColorChooser();
tcc.getSelectionModel().addChangeListener(new ChangeListener() {
public final void stateChanged(ChangeEvent e) {
// return;
}
});
tcc.setColor(color);
JaxoColorPreviewPanel previewPanel = new JaxoColorPreviewPanel(tcc);
previewPanel.setSize(previewPanel.getPreferredSize());
tcc.setPreviewPanel(previewPanel);
ActionListener okListener =
new ActionListener() {
public final void actionPerformed(ActionEvent evt) {
newcolor = tcc.getColor();
}
};
ActionListener cancelListener =
new ActionListener() {
public final void actionPerformed(ActionEvent evt) {
newcolor = oldColor;
}
};
AbstractColorChooserPanel[] panels = {new JaxoColorPanel(fCol)};
tcc.setChooserPanels(panels);
//tcc.setBorder(BorderFactory.createTitledBorder("Choose a Color"));
tcc.setBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4));
tcc.setPreferredSize(new Dimension(500, 300));
// Make the dialog appear to change the color
boolean bool = true;
JDialog dialog =
JColorChooser.createDialog(null,
language.getString("Choose_a_Color"), bool, tcc, okListener,
cancelListener);
dialog.setVisible(true);
dialog.setEnabled(false);
dialog.pack();
return newcolor;
}
class JaxoColorPanel extends AbstractColorChooserPanel {
private int noC;
private JToggleButton[] jaxButton;
public JaxoColorPanel(boolean fillColor) {
super();
if (fillColor) {
noC = JaxoColor.getNofColors();
jaxButton = new JToggleButton[noC];
} else { // line colors of fill objects cannot be grayscales
noC = JaxoColor.getIndex(JaxoColor.GRAYSCALE15);
jaxButton = new JToggleButton[noC + 1];
}
}
public final void updateChooser() {
Color color = getColorFromModel();
//int noC = JaxoColor.getNofColors();
for (int i = 0; i < noC; i++) {
if (color.equals(JaxoColor.getColor(i))) {
jaxButton[i].setSelected(true);
}
}
}
public final Image getChooserImage(Color iconColor) {
MemoryImageSource source;
int[] pixels;
int width = 25;
int height = 25;
int size = width * height;
pixels = new int[size];
for (int i = 0; i < size; i++) {
pixels[i] = iconColor.getRGB();
}
source = new MemoryImageSource(width, height, pixels, 0, width);
Image jaxColorBox = createImage(source);
return jaxColorBox;
}
protected void buildChooser() {
setLayout(new GridLayout(7, 12));
JaxoColorListener jcl = new JaxoColorListener();
ButtonGroup boxOfJaxoColors = new ButtonGroup();
Border border = BorderFactory.createEmptyBorder(4, 4, 4, 4);
//int noC = JaxoColor.getNofColors();
if (noC == JaxoColor.getIndex(JaxoColor.GRAYSCALE15)) {
for (int i = 0; i < noC; i++) {
Image jaxBox = getChooserImage(JaxoColor.getColor(i));
String colorName =
language.getString(JaxoColor.getColorName(
JaxoColor.getColor(i)));
jaxButton[i] = new JToggleButton(new ImageIcon(jaxBox));
jaxButton[i].setActionCommand(JaxoColor.getColorName(i));
jaxButton[i].addActionListener(jcl);
jaxButton[i].setBorder(border);
jaxButton[i].setToolTipText(colorName);
boxOfJaxoColors.add(jaxButton[i]);
add(jaxButton[i]);
}
Image jaxBox = getChooserImage(JaxoColor.WHITE);
String colorName =
language.getString(JaxoColor.getColorName(JaxoColor.WHITE));
jaxButton[noC] = new JToggleButton(new ImageIcon(jaxBox));
jaxButton[noC].setActionCommand(JaxoColor.getColorName(
JaxoColor.WHITE));
jaxButton[noC].addActionListener(jcl);
jaxButton[noC].setBorder(border);
jaxButton[noC].setToolTipText(colorName);
boxOfJaxoColors.add(jaxButton[noC]);
add(jaxButton[noC]);
} else {
for (int i = 0; i < noC; i++) {
Image jaxBox = getChooserImage(JaxoColor.getColor(i));
String colorName =
language.getString(JaxoColor.getColorName(
JaxoColor.getColor(i)));
jaxButton[i] = new JToggleButton(new ImageIcon(jaxBox));
jaxButton[i].setActionCommand(JaxoColor.getColorName(i));
jaxButton[i].addActionListener(jcl);
jaxButton[i].setBorder(border);
jaxButton[i].setToolTipText(colorName);
boxOfJaxoColors.add(jaxButton[i]);
add(jaxButton[i]);
}
}
}
public final String getDisplayName() {
return language.getString("JaxoColors");
}
public final Icon getSmallDisplayIcon() {
return null;
}
public final Icon getLargeDisplayIcon() {
return null;
}
class JaxoColorListener implements ActionListener {
public final void actionPerformed(ActionEvent e) {
Color newColor = null;
JToggleButton source = (JToggleButton) e.getSource();
//int newColorIndex = 0;
//int noC = JaxoColor.getNofColors();
if (noC == JaxoColor.getIndex(JaxoColor.GRAYSCALE15)) {
for (int i = 0; i < noC; i++) {
if (source.getActionCommand().equals(JaxoColor
.getColorName(i))) {
newColor = JaxoColor.getColor(i);
//newColorIndex = i;
}
}
if (source.getActionCommand().equals(JaxoColor
.getColorName(JaxoColor.WHITE))) {
newColor = JaxoColor.WHITE;
//newColorIndex = JaxoColor.getIndex(JaxoColor.WHITE);
}
} else {
for (int i = 0; i < noC; i++) {
if (source.getActionCommand().equals(JaxoColor
.getColorName(i))) {
newColor = JaxoColor.getColor(i);
//newColorIndex = i;
}
}
}
getColorSelectionModel().setSelectedColor(newColor);
}
}
}
class JaxoColorPreviewPanel extends JComponent {
private Color curColor;
public JaxoColorPreviewPanel(JColorChooser chooser) {
curColor = chooser.getColor();
ColorSelectionModel model = chooser.getSelectionModel();
model.addChangeListener(new ChangeListener() {
public final void stateChanged(ChangeEvent evt) {
ColorSelectionModel newModel =
(ColorSelectionModel) evt.getSource();
curColor = newModel.getSelectedColor();
}
});
setPreferredSize(new Dimension(500, 20));
//setBorder(BorderFactory.createEmptyBorder(4,4,4,4));
}
public final void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
g2.setColor(curColor);
g2.setFont(new Font("SansSerif", Font.BOLD, 24));
if (JaxoPrefs.getBooleanPref(JaxoPrefs.PREF_ANTIALIAS)) {
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
} else {
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_OFF);
}
String base = language.getString("_This_color_is:_");
String colorName =
language.getString(JaxoColor.getColorName(curColor));
g2.drawString(base + colorName, 20, 30);
}
}
}
Ads help maintain this website.