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.pentaho.reportdesigner.crm.report.components.PaletteColorChooser;
021: import org.pentaho.reportdesigner.crm.report.model.ElementBorderDefinition;
022: import org.pentaho.reportdesigner.crm.report.util.ColorIcon;
023: import org.pentaho.reportdesigner.lib.client.components.CenterPanelDialog;
024: import org.pentaho.reportdesigner.lib.client.i18n.TranslationManager;
025: import org.pentaho.reportdesigner.lib.client.util.GUIUtils;
026: import org.pentaho.reportdesigner.lib.client.util.WindowUtils;
027:
028: import javax.swing.*;
029: import javax.swing.border.TitledBorder;
030: import javax.swing.event.ChangeEvent;
031: import javax.swing.event.ChangeListener;
032: import java.awt.*;
033: import java.awt.event.ActionEvent;
034: import java.awt.event.ActionListener;
035: import java.awt.event.MouseAdapter;
036: import java.awt.event.MouseEvent;
037:
038: /**
039: * User: Martin
040: * Date: 01.11.2005
041: * Time: 10:23:10
042: */
043: public class ElementBorderDefinitionSideChooser {
044:
045: private ElementBorderDefinitionSideChooser() {
046: }
047:
048: @NotNull
049: public static ElementBorderDefinition.Side showBorderDefinitionChooser(
050: @NotNull
051: JComponent parent, @NotNull
052: String title, @NotNull
053: ElementBorderDefinition.Side origBorderDefinition) {
054: final CenterPanelDialog centerPanelDialog = CenterPanelDialog
055: .createDialog(parent, title,
056: CenterPanelDialog.ModalityType.DOCUMENT_MODAL);
057:
058: final boolean[] action = new boolean[] { false };
059:
060: JButton okButton = new JButton(TranslationManager.getInstance()
061: .getTranslation("R", "Button.ok"));
062: okButton.addActionListener(new ActionListener() {
063: public void actionPerformed(@NotNull
064: ActionEvent e) {
065: action[0] = true;
066: centerPanelDialog.dispose();
067: }
068: });
069:
070: JButton cancelButton = new JButton(TranslationManager
071: .getInstance().getTranslation("R", "Button.cancel"));
072: cancelButton.addActionListener(new ActionListener() {
073: public void actionPerformed(@NotNull
074: ActionEvent e) {
075: action[0] = false;
076: centerPanelDialog.dispose();
077: }
078: });
079:
080: centerPanelDialog.setButtons(okButton, cancelButton, okButton,
081: cancelButton);
082:
083: BorderDefinitionPanel centerPanel = new BorderDefinitionPanel(
084: origBorderDefinition);
085:
086: centerPanelDialog.setCenterPanel(centerPanel);
087: centerPanelDialog.pack();
088: GUIUtils.ensureMinimumDialogSize(centerPanelDialog, 250, 250);
089: WindowUtils.setLocationRelativeTo(centerPanelDialog, parent);
090: centerPanelDialog.setVisible(true);
091:
092: if (action[0]) {
093: return centerPanel.getCurrentBorderDefinition();
094: }
095: //return font;
096: return origBorderDefinition;
097: }
098:
099: private static class SamplePanel extends JPanel {
100: @NotNull
101: private ElementBorderDefinition.Side borderDefinition;
102:
103: private SamplePanel(@NotNull
104: ElementBorderDefinition.Side borderDefinition) {
105: this .borderDefinition = borderDefinition;
106: }
107:
108: @NotNull
109: public ElementBorderDefinition.Side getBorderDefinition() {
110: return borderDefinition;
111: }
112:
113: public void setBorderDefinition(@NotNull
114: ElementBorderDefinition.Side borderDefinition) {
115: this .borderDefinition = borderDefinition;
116:
117: repaint();
118: }
119:
120: protected void paintComponent(@NotNull
121: Graphics g) {
122: super .paintComponent(g);
123:
124: Graphics2D g2d = (Graphics2D) g;
125: Stroke origStroke = g2d.getStroke();
126: Color origColor = g2d.getColor();
127: Shape origClip = g2d.getClip();
128:
129: if (borderDefinition.getType() != ElementBorderDefinition.BorderType.NONE) {
130: g2d.setColor(borderDefinition.getColor());
131: g2d.setStroke(borderDefinition.getStroke());
132:
133: g2d
134: .clip(new Rectangle(
135: getInsets().left + 1,
136: getInsets().top + 1,
137: getWidth()
138: - (getInsets().left
139: + getInsets().right + 2),
140: getHeight()
141: - (getInsets().top
142: + getInsets().bottom + 2)));
143: g2d.drawLine(getInsets().left + 1, getHeight() / 2,
144: getWidth() - (getInsets().right + 1),
145: getHeight() / 2);
146: }
147:
148: g2d.setClip(origClip);
149: g2d.setStroke(origStroke);
150: g2d.setColor(origColor);
151: }
152: }
153:
154: private static class BorderDefinitionPanel extends JPanel {
155: @NotNull
156: private ElementBorderDefinition.Side borderDefinition;
157: @NotNull
158: private Color color;
159: @NotNull
160: private JButton colorButton;
161: @NotNull
162: private JSpinner widthSpinner;
163: @NotNull
164: private JComboBox dashComboBox;
165:
166: private BorderDefinitionPanel(@NotNull
167: ElementBorderDefinition.Side origBorderDefinition) {
168: this .borderDefinition = origBorderDefinition;
169:
170: @NonNls
171: FormLayout formLayout = new FormLayout(
172: "0dlu, default:grow, 4dlu, fill:default, 4dlu, default, 0dlu",
173: "0dlu, " + "pref, " + //color
174: "4dlu, " + "pref, " + //lineWidth
175: "4dlu, " + "pref, " + //dash
176: "4dlu, " + "fill:40dlu, " + //sample
177: "0dlu:grow");
178:
179: setLayout(formLayout);
180: @NonNls
181: CellConstraints cc = new CellConstraints();
182:
183: JLabel colorLabel = new JLabel(TranslationManager
184: .getInstance().getTranslation("R",
185: "BorderDefinitionChooser.ColorLabel"));
186: color = borderDefinition.getColor();
187: final JLabel colorIconLabel = new JLabel(new ColorIcon(
188: Color.BLACK, color, 120, 20, true));
189: colorButton = new JButton("...");
190: colorButton.setMargin(new Insets(1, 1, 1, 1));
191:
192: JLabel widthLabel = new JLabel(TranslationManager
193: .getInstance().getTranslation("R",
194: "BorderDefinitionChooser.WidthLabel"));
195: widthSpinner = new JSpinner(new SpinnerNumberModel(
196: borderDefinition.getWidth(), 0, 1000, 0.1));
197:
198: JLabel dashLabel = new JLabel(TranslationManager
199: .getInstance().getTranslation("R",
200: "BorderDefinitionChooser.DashLabel"));
201: dashComboBox = new JComboBox(
202: ElementBorderDefinition.BorderType.values());
203: dashComboBox
204: .setSelectedItem(origBorderDefinition.getType());
205:
206: final SamplePanel samplePanel = new SamplePanel(
207: borderDefinition);
208: samplePanel.setBorder(new TitledBorder(TranslationManager
209: .getInstance().getTranslation("R",
210: "BorderDefinitionPanel.Sample.Title")));
211:
212: add(colorLabel, cc.xy(2, 2));
213: add(colorIconLabel, cc.xy(4, 2));
214: add(colorButton, cc.xy(6, 2));
215:
216: add(widthLabel, cc.xy(2, 4));
217: add(widthSpinner, cc.xy(4, 4));
218:
219: //add(capLabel, cc.xy(2, 6));
220: //add(capBox, cc.xy(4, 6));
221:
222: //add(joinLabel, cc.xy(2, 8));
223: //add(joinBox, cc.xy(4, 8));
224: //
225: //add(miterLabel, cc.xy(2, 10));
226: //add(miterSpinner, cc.xy(4, 10));
227:
228: add(dashLabel, cc.xy(2, 6));
229: add(dashComboBox, cc.xy(4, 6));
230:
231: add(samplePanel, cc.xyw(2, 8, 5));
232:
233: dashComboBox.addActionListener(new ActionListener() {
234: public void actionPerformed(@NotNull
235: ActionEvent e) {
236: borderDefinition = getCurrentBorderDefinition();
237: samplePanel.setBorderDefinition(borderDefinition);
238: }
239: });
240:
241: colorButton.addActionListener(new ActionListener() {
242: public void actionPerformed(@NotNull
243: ActionEvent e) {
244: Color newColor = PaletteColorChooser
245: .showDialog(
246: BorderDefinitionPanel.this ,
247: TranslationManager
248: .getInstance()
249: .getTranslation("R",
250: "PropertyEditor.Color.Title"),
251: color);
252: if (newColor != null) {
253: color = newColor;
254: colorIconLabel.setIcon(new ColorIcon(
255: Color.BLACK, color, 120, 20, true));
256: }
257:
258: borderDefinition = getCurrentBorderDefinition();
259: samplePanel.setBorderDefinition(borderDefinition);
260: }
261: });
262:
263: colorIconLabel.addMouseListener(new MouseAdapter() {
264: public void mouseClicked(@NotNull
265: MouseEvent e) {
266: Color newColor = PaletteColorChooser
267: .showDialog(
268: BorderDefinitionPanel.this ,
269: TranslationManager
270: .getInstance()
271: .getTranslation("R",
272: "PropertyEditor.Color.Title"),
273: color);
274: if (newColor != null) {
275: color = newColor;
276: colorIconLabel.setIcon(new ColorIcon(
277: Color.BLACK, color, 120, 20, true));
278: }
279:
280: borderDefinition = getCurrentBorderDefinition();
281: samplePanel.setBorderDefinition(borderDefinition);
282: }
283: });
284:
285: widthSpinner.addChangeListener(new ChangeListener() {
286: public void stateChanged(@NotNull
287: ChangeEvent e) {
288: borderDefinition = getCurrentBorderDefinition();
289: samplePanel.setBorderDefinition(borderDefinition);
290: }
291: });
292:
293: }
294:
295: @NotNull
296: private ElementBorderDefinition.Side getCurrentBorderDefinition() {
297: return new ElementBorderDefinition.Side(
298: ((Number) widthSpinner.getValue()).doubleValue(),
299: (ElementBorderDefinition.BorderType) dashComboBox
300: .getSelectedItem(), color);
301: }
302: }
303:
304: }
|