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.jfree.report.style.BandStyleKeys;
022: import org.jfree.report.style.ElementStyleKeys;
023: import org.jfree.report.style.StyleKey;
024: import org.jfree.report.style.TextStyleKeys;
025: import org.pentaho.reportdesigner.crm.report.model.BandReportElement;
026: import org.pentaho.reportdesigner.crm.report.model.BandToplevelReportElement;
027: import org.pentaho.reportdesigner.crm.report.model.ReportElement;
028: import org.pentaho.reportdesigner.crm.report.model.StyleExpression;
029: import org.pentaho.reportdesigner.crm.report.model.StyleExpressions;
030: import org.pentaho.reportdesigner.crm.report.model.TextReportElement;
031: import org.pentaho.reportdesigner.crm.report.properties.PropertyTable;
032: import org.pentaho.reportdesigner.lib.client.components.CenterPanelDialog;
033: import org.pentaho.reportdesigner.lib.client.components.TextComponentHelper;
034: import org.pentaho.reportdesigner.lib.client.i18n.TranslationManager;
035: import org.pentaho.reportdesigner.lib.client.util.GUIUtils;
036: import org.pentaho.reportdesigner.lib.client.util.UndoHelper;
037: import org.pentaho.reportdesigner.lib.client.util.WindowUtils;
038:
039: import javax.swing.*;
040: import javax.swing.event.DocumentEvent;
041: import javax.swing.event.DocumentListener;
042: import javax.swing.event.ListSelectionEvent;
043: import javax.swing.event.ListSelectionListener;
044: import java.awt.*;
045: import java.awt.event.ActionEvent;
046: import java.awt.event.ActionListener;
047: import java.lang.reflect.Field;
048: import java.lang.reflect.Modifier;
049: import java.text.Collator;
050: import java.util.ArrayList;
051: import java.util.Arrays;
052: import java.util.LinkedHashSet;
053: import java.util.logging.Level;
054: import java.util.logging.Logger;
055:
056: /**
057: * User: Martin
058: * Date: 01.11.2005
059: * Time: 10:23:10
060: */
061: public class StyleExpressionsChooser {
062: @NonNls
063: @NotNull
064: private static final Logger LOG = Logger
065: .getLogger(StyleExpressionsChooser.class.getName());
066:
067: private StyleExpressionsChooser() {
068: }
069:
070: @NotNull
071: public static StyleExpressions showStyleExpressionsChooser(@NotNull
072: PropertyTable parent, @Nullable
073: ReportElement reportElement, @NotNull
074: String title, @NotNull
075: StyleExpressions origStyleExpressions) {
076: final CenterPanelDialog centerPanelDialog = CenterPanelDialog
077: .createDialog(parent, title,
078: CenterPanelDialog.ModalityType.DOCUMENT_MODAL);
079:
080: final boolean[] action = new boolean[] { false };
081:
082: JButton okButton = new JButton(TranslationManager.getInstance()
083: .getTranslation("R", "Button.ok"));
084: okButton.addActionListener(new ActionListener() {
085: public void actionPerformed(@NotNull
086: ActionEvent e) {
087: action[0] = true;
088: centerPanelDialog.dispose();
089: }
090: });
091:
092: JButton cancelButton = new JButton(TranslationManager
093: .getInstance().getTranslation("R", "Button.cancel"));
094: cancelButton.addActionListener(new ActionListener() {
095: public void actionPerformed(@NotNull
096: ActionEvent e) {
097: action[0] = false;
098: centerPanelDialog.dispose();
099: }
100: });
101:
102: centerPanelDialog.setButtons(okButton, cancelButton, okButton,
103: cancelButton);
104:
105: StyleExpressionsPanel centerPanel = new StyleExpressionsPanel(
106: reportElement, origStyleExpressions);
107:
108: centerPanelDialog.setCenterPanel(centerPanel);
109: centerPanelDialog.pack();
110: GUIUtils.ensureMinimumDialogSize(centerPanelDialog, 500, 400);
111: WindowUtils.setLocationRelativeTo(centerPanelDialog, parent);
112: centerPanelDialog.setVisible(true);
113:
114: if (action[0]) {
115: return centerPanel.getStyleExpressions();
116: }
117: //return font;
118: return origStyleExpressions;
119: }
120:
121: private static class StyleExpressionsPanel extends JPanel {
122: @NotNull
123: private StyleExpressions styleExpressions;
124:
125: private void updateStyleExpressions(@NotNull
126: JList styleKeyList, @NotNull
127: DefaultListModel listModel, @NotNull
128: JTextArea expressionTextArea) {
129: int index = styleKeyList.getSelectedIndex();
130: if (index != -1) {
131: StyleExpression styleExpression = (StyleExpression) listModel
132: .get(index);
133: styleExpressions = styleExpressions
134: .removeStyleExpression(styleExpression);
135:
136: StyleExpression newStyleExpression = new StyleExpression(
137: styleExpression.getStyleKeyName(),
138: expressionTextArea.getText());
139: styleExpressions = styleExpressions
140: .addStyleExpression(newStyleExpression);
141: listModel.set(index, newStyleExpression);
142: }
143: }
144:
145: private StyleExpressionsPanel(@Nullable
146: final ReportElement reportElement, @NotNull
147: StyleExpressions origStyleExpressions) {
148: this .styleExpressions = new StyleExpressions(
149: origStyleExpressions);
150:
151: FormLayout formLayout = new FormLayout(
152: "4dlu, fill:default:grow, 4dlu",
153: "4dlu, pref, 4dlu, fill:default:grow, 4dlu");//NON-NLS
154: @NonNls
155: CellConstraints cc = new CellConstraints();
156:
157: setLayout(formLayout);
158:
159: JPanel buttonPanel = new JPanel(new GridLayout(1, 0));
160:
161: JButton addButton = new JButton(TranslationManager
162: .getInstance().getTranslation("R",
163: "StyleExpressionsChooser.AddButton"));
164: final JButton removeButton = new JButton(TranslationManager
165: .getInstance().getTranslation("R",
166: "StyleExpressionsChooser.RemoveButton"));
167: removeButton.setEnabled(false);
168:
169: buttonPanel.add(addButton);
170: buttonPanel.add(removeButton);
171:
172: add(buttonPanel, cc.xy(2, 2, "left, fill"));
173:
174: final DefaultListModel listModel = new DefaultListModel();
175: for (StyleExpression styleExpression : styleExpressions
176: .getStyleExpressions()) {
177: listModel.addElement(styleExpression);
178: }
179:
180: final JList styleKeyList = new JList(listModel);
181: styleKeyList.setPrototypeCellValue(new StyleExpression(
182: "prototype cell value", ""));//NON-NLS
183:
184: final JTextArea expressionTextArea = new JTextArea();
185: expressionTextArea.setEnabled(false);
186: JSplitPane splitPane = new JSplitPane(
187: JSplitPane.HORIZONTAL_SPLIT, new JScrollPane(
188: styleKeyList), new JScrollPane(
189: expressionTextArea));
190:
191: add(splitPane, cc.xy(2, 4));
192:
193: styleKeyList
194: .addListSelectionListener(new ListSelectionListener() {
195: public void valueChanged(@NotNull
196: ListSelectionEvent e) {
197: if (!e.getValueIsAdjusting()) {
198: int index = styleKeyList
199: .getSelectedIndex();
200: if (index != -1) {
201: expressionTextArea.setEnabled(true);
202: removeButton.setEnabled(true);
203:
204: StyleExpression styleExpression = (StyleExpression) listModel
205: .get(index);
206: expressionTextArea
207: .setText(styleExpression
208: .getExpression());
209: } else {
210: expressionTextArea
211: .setEnabled(false);
212: removeButton.setEnabled(false);
213:
214: expressionTextArea.setText("");
215: }
216: }
217: }
218: });
219:
220: TextComponentHelper
221: .installDefaultPopupMenu(expressionTextArea);
222: UndoHelper.installUndoSupport(expressionTextArea);
223:
224: expressionTextArea.getDocument().addDocumentListener(
225: new DocumentListener() {
226: public void insertUpdate(@NotNull
227: DocumentEvent e) {
228: updateStyleExpressions(styleKeyList,
229: listModel, expressionTextArea);
230: }
231:
232: public void removeUpdate(@NotNull
233: DocumentEvent e) {
234: updateStyleExpressions(styleKeyList,
235: listModel, expressionTextArea);
236: }
237:
238: public void changedUpdate(@NotNull
239: DocumentEvent e) {
240: updateStyleExpressions(styleKeyList,
241: listModel, expressionTextArea);
242: }
243: });
244:
245: addButton.addActionListener(new ActionListener() {
246: public void actionPerformed(@NotNull
247: ActionEvent e) {
248: LinkedHashSet<String> possibleKeys = new LinkedHashSet<String>();
249: StyleKey[] styleKeys = StyleKey
250: .getDefinedStyleKeys();
251:
252: ArrayList<StyleKey> elementStyleKeys = getStyleKeys(ElementStyleKeys.class);
253: for (StyleKey styleKey : elementStyleKeys) {
254: possibleKeys.add(styleKey.getName());
255: }
256:
257: if (reportElement instanceof TextReportElement) {
258: ArrayList<StyleKey> textStyleKeys = getStyleKeys(TextStyleKeys.class);
259: for (StyleKey textStyleKey : textStyleKeys) {
260: possibleKeys.add(textStyleKey.getName());
261: }
262: }
263:
264: if (reportElement instanceof BandReportElement
265: || reportElement instanceof BandToplevelReportElement) {
266: ArrayList<StyleKey> bandStyleKeys = getStyleKeys(BandStyleKeys.class);
267: for (StyleKey bandStyleKey : bandStyleKeys) {
268: possibleKeys.add(bandStyleKey.getName());
269: }
270:
271: for (StyleKey styleKey : styleKeys) {
272: if (styleKey.isInheritable()) {
273: possibleKeys.add(styleKey.getName());
274: }
275: }
276: }
277:
278: if (reportElement == null) {
279: for (StyleKey styleKey : styleKeys) {
280: possibleKeys.add(styleKey.getName());
281: }
282: }
283:
284: for (StyleExpression styleExpression : styleExpressions
285: .getStyleExpressions()) {
286: possibleKeys.remove(styleExpression
287: .getStyleKeyName());
288: }
289:
290: String[] strings = possibleKeys
291: .toArray(new String[possibleKeys.size()]);
292: Collator collator = Collator.getInstance();
293: collator.setStrength(Collator.PRIMARY);
294: Arrays.sort(strings, collator);
295:
296: String s = ArrayElementChooser
297: .showOneElementChooser(
298: StyleExpressionsPanel.this ,
299: strings,
300: TranslationManager
301: .getInstance()
302: .getTranslation("R",
303: "StyleExpressionChooser.StyleKeyChooser.Title"));
304: if (s != null) {
305: StyleExpression styleExpression = new StyleExpression(
306: s, "");
307: styleExpressions = styleExpressions
308: .addStyleExpression(styleExpression);
309:
310: int newIndex = listModel.getSize();
311: listModel.add(newIndex, styleExpression);
312: styleKeyList.setSelectedIndex(newIndex);
313: }
314: }
315: });
316:
317: removeButton.addActionListener(new ActionListener() {
318: public void actionPerformed(@NotNull
319: ActionEvent e) {
320: StyleExpression styleExpression = (StyleExpression) listModel
321: .get(styleKeyList.getSelectedIndex());
322: styleExpressions = styleExpressions
323: .removeStyleExpression(styleExpression);
324: listModel.removeElement(styleExpression);
325: }
326: });
327: }
328:
329: @NotNull
330: private ArrayList<StyleKey> getStyleKeys(@NotNull
331: Class<?> elementStyleKeysClass) {
332: ArrayList<StyleKey> styleKeys = new ArrayList<StyleKey>();
333:
334: Field[] fields = elementStyleKeysClass.getFields();
335: for (Field field : fields) {
336: if (Modifier.isStatic(field.getModifiers())
337: && Modifier.isPublic(field.getModifiers())
338: && StyleKey.class.isAssignableFrom(field
339: .getType())) {
340: try {
341: styleKeys.add((StyleKey) field.get(null));
342: } catch (IllegalAccessException e) {
343: if (LOG.isLoggable(Level.FINE))
344: LOG
345: .log(
346: Level.FINE,
347: "StyleExpressionsChooser$StyleExpressionsPanel.getStyleKeys ",
348: e);
349: }
350: }
351: }
352: return styleKeys;
353: }
354:
355: @NotNull
356: public StyleExpressions getStyleExpressions() {
357: return styleExpressions;
358: }
359: }
360:
361: }
|