001: /*
002: * Copyright 2006-2007 Pentaho Corporation. All rights reserved.
003: * This software was developed by Pentaho Corporation and is provided under the terms
004: * of the Mozilla Public License, Version 1.1, or any later version. You may not use
005: * this file except in compliance with the license. If you need a copy of the license,
006: * please go to http://www.mozilla.org/MPL/MPL-1.1.txt.
007: *
008: * Software distributed under the Mozilla Public License is distributed on an "AS IS"
009: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. Please refer to
010: * the license for the specific language governing your rights and limitations.
011: *
012: * Additional Contributor(s): Martin Schmid gridvision engineering GmbH
013: */
014: package org.pentaho.reportdesigner.crm.report.components;
015:
016: import org.jetbrains.annotations.NotNull;
017: import org.jetbrains.annotations.Nullable;
018: import org.pentaho.reportdesigner.lib.client.i18n.TranslationManager;
019: import org.pentaho.reportdesigner.lib.client.util.WindowUtils;
020: import org.pentaho.reportdesigner.lib.client.components.CenterPanelDialog;
021:
022: import javax.swing.*;
023: import javax.swing.colorchooser.AbstractColorChooserPanel;
024: import java.awt.*;
025: import java.awt.event.ActionEvent;
026: import java.awt.event.ActionListener;
027: import java.awt.event.MouseAdapter;
028: import java.awt.event.MouseEvent;
029: import java.awt.event.MouseMotionAdapter;
030: import java.awt.geom.Arc2D;
031:
032: /**
033: * User: Martin
034: * Date: 20.01.2007
035: * Time: 10:53:46
036: */
037: public class PaletteColorChooser {
038: private PaletteColorChooser() {
039: }
040:
041: public static void main(@NotNull
042: String[] args) {
043: final JFrame frame = new JFrame();
044:
045: JButton button = new JButton("...");
046: button.addActionListener(new ActionListener() {
047: public void actionPerformed(@NotNull
048: ActionEvent e) {
049: Color color = showDialog(frame, "Select a Color",//NON-NLS
050: Color.RED, true);
051: //noinspection UseOfSystemOutOrSystemErr
052: System.out.println("color = " + color);//NON-NLS
053: }
054: });
055:
056: frame.getContentPane().add(button);
057:
058: frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
059: frame.setBounds(100, 100, 300, 200);
060: frame.setVisible(true);
061: }
062:
063: private static final int OFFSET = 3;
064:
065: @NotNull
066: private static final Color[] PALETTE_COLORS = new Color[] {
067: new Color(0x772200), new Color(0xAA6611),
068: new Color(0x1E8AD3), new Color(0xFFAA00),
069: new Color(0x445500), new Color(0x4A0866),
070: new Color(0x123D82), new Color(0xDDCC88),
071: new Color(0x467AA9), new Color(0xFCCF12),
072: new Color(0xBF0000), new Color(0x9EAA36),
073: new Color(0xEE7733), };
074:
075: @Nullable
076: public static Color showDialog(@NotNull
077: Component parent, @NotNull
078: String title, @Nullable
079: Color initalColor) {
080: return showDialog(parent, title, initalColor, true);
081: }
082:
083: @Nullable
084: public static Color showDialog(@NotNull
085: Component parent, @NotNull
086: String title, @Nullable
087: Color initalColor, boolean showPalette) {
088: final JColorChooser chooser = new JColorChooser(
089: initalColor != null ? initalColor : Color.WHITE);
090: if (showPalette) {
091: AbstractColorChooserPanel chooserPanel = new AbstractColorChooserPanel() {
092:
093: protected void paintComponent(@NotNull
094: Graphics g) {
095: super .paintComponent(g);
096: Graphics2D g2d = (Graphics2D) g;
097:
098: g2d.setRenderingHint(
099: RenderingHints.KEY_ANTIALIASING,
100: RenderingHints.VALUE_ANTIALIAS_ON);
101:
102: double ang = 360. / PALETTE_COLORS.length;
103: for (int i = 0; i < PALETTE_COLORS.length; i++) {
104: double start = (i + OFFSET) * ang;
105: Arc2D.Double arc = new Arc2D.Double(5, 5, 190,
106: 190, start, ang + 0.2, Arc2D.PIE);
107: g2d.setColor(PALETTE_COLORS[i]);
108: g2d.fill(arc);
109: }
110:
111: for (int i = 0; i < PALETTE_COLORS.length; i++) {
112: double start = (i + OFFSET) * ang;
113: Arc2D.Double arc = new Arc2D.Double(5, 5, 190,
114: 190, start, ang + 0.2, Arc2D.PIE);
115: if (PALETTE_COLORS[i]
116: .equals(getColorSelectionModel()
117: .getSelectedColor())) {
118: g2d.setColor(Color.BLACK);
119: g2d.draw(arc);
120: }
121: }
122: }
123:
124: public void updateChooser() {
125: repaint();
126: }
127:
128: protected void buildChooser() {
129: addMouseMotionListener(new MouseMotionAdapter() {
130: public void mouseDragged(@NotNull
131: MouseEvent e) {
132: selectColor(e);
133: }
134: });
135:
136: addMouseListener(new MouseAdapter() {
137:
138: public void mouseClicked(@NotNull
139: MouseEvent e) {
140: selectColor(e);
141: }
142:
143: public void mousePressed(@NotNull
144: MouseEvent e) {
145: selectColor(e);
146: }
147:
148: public void mouseReleased(@NotNull
149: MouseEvent e) {
150: selectColor(e);
151: }
152:
153: public void mouseDragged(@NotNull
154: MouseEvent e) {
155: selectColor(e);
156: }
157:
158: });
159: }
160:
161: private void selectColor(@NotNull
162: MouseEvent e) {
163: double ang = 360. / PALETTE_COLORS.length;
164: for (int i = 0; i < PALETTE_COLORS.length; i++) {
165: double start = (i + OFFSET) * ang;
166: Arc2D.Double arc = new Arc2D.Double(5, 5, 190,
167: 190, start, ang + 0.2, Arc2D.PIE);
168: if (arc.contains(e.getPoint())) {
169: getColorSelectionModel().setSelectedColor(
170: PALETTE_COLORS[i]);
171: repaint();
172: }
173: }
174: }
175:
176: @NotNull
177: public String getDisplayName() {
178: return TranslationManager.getInstance()
179: .getTranslation("R",
180: "PentahoColorChooser.PaletteTitle");
181: }
182:
183: @Nullable
184: public Icon getSmallDisplayIcon() {
185: return null;
186: }
187:
188: @Nullable
189: public Icon getLargeDisplayIcon() {
190: return null;
191: }
192: };
193: chooserPanel.setPreferredSize(new Dimension(200, 200));
194: chooser.addChooserPanel(chooserPanel);
195: }
196: final Color[] result = new Color[1];
197: JDialog jDialog = JColorChooser.createDialog(parent, title,
198: true, chooser, new ActionListener() {
199: public void actionPerformed(@NotNull
200: ActionEvent e) {
201: result[0] = chooser.getColor();
202: }
203: }, null);
204:
205: CenterPanelDialog.setModality(jDialog,
206: CenterPanelDialog.ModalityType.DOCUMENT_MODAL);
207:
208: WindowUtils.setLocationRelativeTo(jDialog, parent);
209: jDialog.setVisible(true);
210: return result[0];
211: }
212: }
|