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.properties.editors;
015:
016: import com.jgoodies.forms.layout.CellConstraints;
017: import com.jgoodies.forms.layout.FormLayout;
018: import org.jetbrains.annotations.NonNls;
019: import org.jetbrains.annotations.NotNull;
020: import org.jetbrains.annotations.Nullable;
021: import org.pentaho.reportdesigner.crm.report.components.PaletteColorChooser;
022: import org.pentaho.reportdesigner.crm.report.model.BorderDefinition;
023: import org.pentaho.reportdesigner.crm.report.model.LineDefinition;
024: import org.pentaho.reportdesigner.crm.report.util.ColorIcon;
025: import org.pentaho.reportdesigner.lib.client.components.CenterPanelDialog;
026: import org.pentaho.reportdesigner.lib.client.i18n.TranslationManager;
027: import org.pentaho.reportdesigner.lib.client.util.GUIUtils;
028: import org.pentaho.reportdesigner.lib.client.util.WindowUtils;
029:
030: import javax.swing.*;
031: import javax.swing.border.TitledBorder;
032: import javax.swing.event.ChangeEvent;
033: import javax.swing.event.ChangeListener;
034: import java.awt.*;
035: import java.awt.event.ActionEvent;
036: import java.awt.event.ActionListener;
037: import java.awt.event.MouseAdapter;
038: import java.awt.event.MouseEvent;
039: import java.util.HashMap;
040:
041: /**
042: * User: Martin
043: * Date: 01.11.2005
044: * Time: 10:23:10
045: */
046: public class BorderDefinitionChooser {
047:
048: private BorderDefinitionChooser() {
049: }
050:
051: @NotNull
052: public static BorderDefinition showBorderDefinitionChooser(@NotNull
053: JComponent parent, @NotNull
054: String title, @NotNull
055: BorderDefinition origBorderDefinition, boolean asLineStyle) {
056: final CenterPanelDialog centerPanelDialog = CenterPanelDialog
057: .createDialog(parent, title,
058: CenterPanelDialog.ModalityType.DOCUMENT_MODAL);
059:
060: final boolean[] action = new boolean[] { false };
061:
062: JButton okButton = new JButton(TranslationManager.getInstance()
063: .getTranslation("R", "Button.ok"));
064: okButton.addActionListener(new ActionListener() {
065: public void actionPerformed(@NotNull
066: ActionEvent e) {
067: action[0] = true;
068: centerPanelDialog.dispose();
069: }
070: });
071:
072: JButton cancelButton = new JButton(TranslationManager
073: .getInstance().getTranslation("R", "Button.cancel"));
074: cancelButton.addActionListener(new ActionListener() {
075: public void actionPerformed(@NotNull
076: ActionEvent e) {
077: action[0] = false;
078: centerPanelDialog.dispose();
079: }
080: });
081:
082: centerPanelDialog.setButtons(okButton, cancelButton, okButton,
083: cancelButton);
084:
085: BorderDefinitionPanel centerPanel = new BorderDefinitionPanel(
086: origBorderDefinition);
087:
088: centerPanelDialog.setCenterPanel(centerPanel);
089: centerPanelDialog.pack();
090: GUIUtils.ensureMinimumDialogSize(centerPanelDialog, 250, 250);
091: WindowUtils.setLocationRelativeTo(centerPanelDialog, parent);
092: centerPanelDialog.setVisible(true);
093:
094: if (action[0]) {
095: if (asLineStyle) {
096: return new LineDefinition(centerPanel
097: .getCurrentBorderDefinition().getColor(),
098: centerPanel.getCurrentBorderDefinition()
099: .getWidth(),
100: centerPanel.getCurrentBorderDefinition()
101: .getJoin(), centerPanel
102: .getCurrentBorderDefinition().getCap(),
103: centerPanel.getCurrentBorderDefinition()
104: .getMiterlimit(),
105: centerPanel.getCurrentBorderDefinition()
106: .getDash(), centerPanel
107: .getCurrentBorderDefinition()
108: .getDashPhase());
109: }
110: return centerPanel.getCurrentBorderDefinition();
111: }
112: //return font;
113: return origBorderDefinition;
114: }
115:
116: private static class SamplePanel extends JPanel {
117: @NotNull
118: private BorderDefinition borderDefinition;
119:
120: private SamplePanel(@NotNull
121: BorderDefinition borderDefinition) {
122: this .borderDefinition = borderDefinition;
123: }
124:
125: @NotNull
126: public BorderDefinition getBorderDefinition() {
127: return borderDefinition;
128: }
129:
130: public void setBorderDefinition(@NotNull
131: BorderDefinition borderDefinition) {
132: this .borderDefinition = borderDefinition;
133:
134: repaint();
135: }
136:
137: protected void paintComponent(@NotNull
138: Graphics g) {
139: super .paintComponent(g);
140:
141: Graphics2D g2d = (Graphics2D) g;
142: Stroke origStroke = g2d.getStroke();
143: Color origColor = g2d.getColor();
144: Shape origClip = g2d.getClip();
145:
146: g2d.setColor(borderDefinition.getColor());
147: g2d.setStroke(borderDefinition.getBasicStroke());
148:
149: g2d
150: .clip(new Rectangle(getInsets().left + 1,
151: getInsets().top + 1, getWidth()
152: - (getInsets().left
153: + getInsets().right + 2),
154: getHeight()
155: - (getInsets().top
156: + getInsets().bottom + 2)));
157: g2d.drawLine(getInsets().left + 1, getHeight() / 2,
158: getWidth() - (getInsets().right + 1),
159: getHeight() / 2);
160:
161: g2d.setClip(origClip);
162: g2d.setStroke(origStroke);
163: g2d.setColor(origColor);
164: }
165: }
166:
167: private static class BorderDefinitionPanel extends JPanel {
168: @NotNull
169: private static final String SOLID = "SOLID";
170: @NotNull
171: private static final String DASHED = "DASHED";
172: @NotNull
173: private static final String DOTTED = "DOTTED";
174: @NotNull
175: private static final String DOT_DASH = "DOT_DASH";
176: @NotNull
177: private static final String DOT_DOT_DASH = "DOT_DOT_DASH";
178:
179: @NotNull
180: private BorderDefinition borderDefinition;
181: @NotNull
182: private Color color;
183: @NotNull
184: private JButton colorButton;
185: @NotNull
186: private JSpinner widthSpinner;
187: @NotNull
188: private JComboBox dashComboBox;
189:
190: private BorderDefinitionPanel(@NotNull
191: BorderDefinition origBorderDefinition) {
192: this .borderDefinition = origBorderDefinition;
193:
194: @NonNls
195: FormLayout formLayout = new FormLayout(
196: "0dlu, default:grow, 4dlu, fill:default, 4dlu, default, 0dlu",
197: "0dlu, " + "pref, " + //color
198: "4dlu, " + "pref, " + //lineWidth
199: //"4dlu, " +
200: //"pref, " +//cap
201: //"4dlu, " +
202: //"pref, " +//join
203: //"4dlu, " +
204: //"pref, " +//miterlimit
205: "4dlu, " + "pref, " + //dash
206: "4dlu, " + "fill:40dlu, " + //sample
207: "0dlu:grow");
208:
209: setLayout(formLayout);
210: @NonNls
211: CellConstraints cc = new CellConstraints();
212:
213: JLabel colorLabel = new JLabel(TranslationManager
214: .getInstance().getTranslation("R",
215: "BorderDefinitionChooser.ColorLabel"));
216: color = borderDefinition.getColor();
217: final JLabel colorIconLabel = new JLabel(new ColorIcon(
218: Color.BLACK, color, 120, 20, true));
219: colorButton = new JButton("...");
220: colorButton.setMargin(new Insets(1, 1, 1, 1));
221:
222: JLabel widthLabel = new JLabel(TranslationManager
223: .getInstance().getTranslation("R",
224: "BorderDefinitionChooser.WidthLabel"));
225: widthSpinner = new JSpinner(new SpinnerNumberModel(
226: borderDefinition.getWidth(), 0, 1000, 0.1));
227:
228: //JLabel capLabel = new JLabel(TranslationManager.getInstance().getTranslation("R", "BorderDefinitionChooser.CapLabel"));
229: //Box capBox = new Box(BoxLayout.X_AXIS);
230: //JToggleButton capSquareToggleButton = new JToggleButton("Square");
231: //JToggleButton capButtToggleButton = new JToggleButton("Butt");
232: //JToggleButton capRoundToggleButton = new JToggleButton("Round");
233: //capBox.add(capSquareToggleButton);
234: //capBox.add(capButtToggleButton);
235: //capBox.add(capRoundToggleButton);
236: //capBox.add(Box.createHorizontalGlue());
237: //
238: //JLabel joinLabel = new JLabel(TranslationManager.getInstance().getTranslation("R", "BorderDefinitionChooser.JoinLabel"));
239: //JToggleButton joinMiterToggleButton = new JToggleButton("Square");
240: //JToggleButton joinRoundToggleButton = new JToggleButton("Butt");
241: //JToggleButton joinBevelToggleButton = new JToggleButton("Round");
242: //Box joinBox = new Box(BoxLayout.X_AXIS);
243: //joinBox.add(joinMiterToggleButton);
244: //joinBox.add(joinRoundToggleButton);
245: //joinBox.add(joinBevelToggleButton);
246: //joinBox.add(Box.createHorizontalGlue());
247: //
248: //JLabel miterLabel = new JLabel(TranslationManager.getInstance().getTranslation("R", "BorderDefinitionChooser.MiterLabel"));
249: //JSpinner miterSpinner = new JSpinner(new SpinnerNumberModel(10, 0.01, 1000, 0.1));
250:
251: JLabel dashLabel = new JLabel(TranslationManager
252: .getInstance().getTranslation("R",
253: "BorderDefinitionChooser.DashLabel"));
254: dashComboBox = new JComboBox(new String[] { SOLID, DASHED,
255: DOTTED, DOT_DASH, DOT_DOT_DASH });
256:
257: //noinspection NonStaticInitializer
258: dashComboBox.setRenderer(new DefaultListCellRenderer() {
259: @NotNull
260: private HashMap<String, StrokeIcon> hashMap = new HashMap<String, StrokeIcon>();
261:
262: {
263: hashMap.put(SOLID, new StrokeIcon(new BasicStroke(
264: 2, BasicStroke.CAP_SQUARE,
265: BasicStroke.JOIN_MITER, 10, null, 0)));
266: hashMap.put(DASHED, new StrokeIcon(new BasicStroke(
267: 2, BasicStroke.CAP_SQUARE,
268: BasicStroke.JOIN_MITER, 10, new float[] {
269: 5, 6 }, 0)));
270: hashMap.put(DOTTED, new StrokeIcon(new BasicStroke(
271: 2, BasicStroke.CAP_SQUARE,
272: BasicStroke.JOIN_MITER, 5, new float[] { 0,
273: 4 }, 0)));
274: hashMap.put(DOT_DASH, new StrokeIcon(
275: new BasicStroke(2, BasicStroke.CAP_SQUARE,
276: BasicStroke.JOIN_MITER, 10,
277: new float[] { 0, 4, 7, 4 }, 0)));
278: hashMap.put(DOT_DOT_DASH,
279: new StrokeIcon(
280: new BasicStroke(2,
281: BasicStroke.CAP_SQUARE,
282: BasicStroke.JOIN_MITER, 10,
283: new float[] { 0, 4, 0, 4,
284: 7, 4 }, 0)));
285: }
286:
287: @NotNull
288: public Component getListCellRendererComponent(@NotNull
289: JList list, @Nullable
290: Object value, int index, boolean isSelected,
291: boolean cellHasFocus) {
292: JLabel label = (JLabel) super
293: .getListCellRendererComponent(list, value,
294: index, isSelected, cellHasFocus);
295: //noinspection SuspiciousMethodCalls
296: StrokeIcon strokeIcon = hashMap.get(value);
297: label.setIcon(strokeIcon);
298: label.setText("");
299: return label;
300: }
301: });
302:
303: if (borderDefinition.isDashed()) {
304: dashComboBox.setSelectedItem(DASHED);
305: } else if (borderDefinition.isDotted()) {
306: dashComboBox.setSelectedItem(DOTTED);
307: } else if (borderDefinition.isDotDash()) {
308: dashComboBox.setSelectedItem(DOT_DASH);
309: } else if (borderDefinition.isDotDotDash()) {
310: dashComboBox.setSelectedItem(DOT_DOT_DASH);
311: } else {
312: dashComboBox.setSelectedItem(SOLID);
313:
314: borderDefinition = new BorderDefinition(
315: borderDefinition.getColor(), borderDefinition
316: .getWidth(),
317: borderDefinition.getJoin(), borderDefinition
318: .getCap(), borderDefinition
319: .getMiterlimit(), null, 0);
320: }
321:
322: final SamplePanel samplePanel = new SamplePanel(
323: borderDefinition);
324: samplePanel.setBorder(new TitledBorder(TranslationManager
325: .getInstance().getTranslation("R",
326: "BorderDefinitionPanel.Sample.Title")));
327:
328: add(colorLabel, cc.xy(2, 2));
329: add(colorIconLabel, cc.xy(4, 2));
330: add(colorButton, cc.xy(6, 2));
331:
332: add(widthLabel, cc.xy(2, 4));
333: add(widthSpinner, cc.xy(4, 4));
334:
335: //add(capLabel, cc.xy(2, 6));
336: //add(capBox, cc.xy(4, 6));
337:
338: //add(joinLabel, cc.xy(2, 8));
339: //add(joinBox, cc.xy(4, 8));
340: //
341: //add(miterLabel, cc.xy(2, 10));
342: //add(miterSpinner, cc.xy(4, 10));
343:
344: add(dashLabel, cc.xy(2, 6));
345: add(dashComboBox, cc.xy(4, 6));
346:
347: add(samplePanel, cc.xyw(2, 8, 5));
348:
349: dashComboBox.addActionListener(new ActionListener() {
350: public void actionPerformed(@NotNull
351: ActionEvent e) {
352: borderDefinition = getCurrentBorderDefinition();
353: samplePanel.setBorderDefinition(borderDefinition);
354: }
355: });
356:
357: colorButton.addActionListener(new ActionListener() {
358: public void actionPerformed(@NotNull
359: ActionEvent e) {
360: Color newColor = PaletteColorChooser
361: .showDialog(
362: BorderDefinitionPanel.this ,
363: TranslationManager
364: .getInstance()
365: .getTranslation("R",
366: "PropertyEditor.Color.Title"),
367: color);
368: if (newColor != null) {
369: color = newColor;
370: colorIconLabel.setIcon(new ColorIcon(
371: Color.BLACK, color, 120, 20, true));
372: }
373:
374: borderDefinition = getCurrentBorderDefinition();
375: samplePanel.setBorderDefinition(borderDefinition);
376: }
377: });
378:
379: colorIconLabel.addMouseListener(new MouseAdapter() {
380: public void mouseClicked(@NotNull
381: MouseEvent e) {
382: Color newColor = PaletteColorChooser
383: .showDialog(
384: BorderDefinitionPanel.this ,
385: TranslationManager
386: .getInstance()
387: .getTranslation("R",
388: "PropertyEditor.Color.Title"),
389: color);
390: if (newColor != null) {
391: color = newColor;
392: colorIconLabel.setIcon(new ColorIcon(
393: Color.BLACK, color, 120, 20, true));
394: }
395:
396: borderDefinition = getCurrentBorderDefinition();
397: samplePanel.setBorderDefinition(borderDefinition);
398: }
399: });
400:
401: widthSpinner.addChangeListener(new ChangeListener() {
402: public void stateChanged(@NotNull
403: ChangeEvent e) {
404: borderDefinition = getCurrentBorderDefinition();
405: samplePanel.setBorderDefinition(borderDefinition);
406: }
407: });
408:
409: }
410:
411: @NotNull
412: private BorderDefinition getCurrentBorderDefinition() {
413: double miterlimit;
414: double[] dash;
415: double dashPhase = 0;
416:
417: double borderWidth = ((Number) widthSpinner.getValue())
418: .doubleValue();
419:
420: Object selectedItem = dashComboBox.getSelectedItem();
421: if (DASHED.equals(selectedItem)) {
422: miterlimit = 10;
423: dash = BorderDefinition.getDashed(borderWidth);
424: } else if (DOTTED.equals(selectedItem)) {
425: miterlimit = 5;
426: dash = BorderDefinition.getDotted(borderWidth);
427: } else if (DOT_DASH.equals(selectedItem)) {
428: miterlimit = 10;
429: dash = BorderDefinition.getDotDash(borderWidth);
430: } else if (DOT_DOT_DASH.equals(selectedItem)) {
431: miterlimit = 10;
432: dash = BorderDefinition.getDotDotDash(borderWidth);
433: } else {
434: miterlimit = 10;
435: dash = null;
436: }
437:
438: return new BorderDefinition(color, borderWidth,
439: BasicStroke.JOIN_MITER, BasicStroke.CAP_SQUARE,
440: miterlimit, dash, dashPhase);
441: }
442: }
443:
444: private static class StrokeIcon implements Icon {
445: @NotNull
446: private BasicStroke basicStroke;
447:
448: private StrokeIcon(@NotNull
449: BasicStroke basicStroke) {
450: this .basicStroke = basicStroke;
451: }
452:
453: public void paintIcon(@NotNull
454: Component c, @NotNull
455: Graphics g, int x, int y) {
456: Graphics2D g2d = (Graphics2D) g;
457: Color origColor = g2d.getColor();
458: //Object hint = g2d.getRenderingHint(RenderingHints.KEY_ANTIALIASING);
459: //g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
460: Stroke origStroke = g2d.getStroke();
461:
462: g2d.translate(x, y);
463:
464: g2d.setColor(Color.BLACK);
465: g2d.setStroke(basicStroke);
466:
467: g2d.drawLine(0, getIconHeight() / 2, getIconWidth(),
468: getIconHeight() / 2);
469: g2d.setColor(origColor);
470:
471: g2d.translate(-x, -y);
472: g2d.setStroke(origStroke);
473: //g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, hint);
474: }
475:
476: public int getIconWidth() {
477: return 100;
478: }
479:
480: public int getIconHeight() {
481: return 20;
482: }
483: }
484: }
|