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.datasetplugin.properties;
015:
016: import com.jgoodies.forms.layout.CellConstraints;
017: import com.jgoodies.forms.layout.FormLayout;
018: import org.gjt.xpp.XmlPullNode;
019: import org.jetbrains.annotations.NonNls;
020: import org.jetbrains.annotations.NotNull;
021: import org.jetbrains.annotations.Nullable;
022: import org.pentaho.reportdesigner.crm.report.PropertyKeys;
023: import org.pentaho.reportdesigner.crm.report.ReportDialog;
024: import org.pentaho.reportdesigner.crm.report.model.dataset.DataSetReportElement;
025: import org.pentaho.reportdesigner.crm.report.model.functions.ExpressionRegistry;
026: import org.pentaho.reportdesigner.crm.report.reportexporter.ReportCreationException;
027: import org.pentaho.reportdesigner.crm.report.reportexporter.ReportVisitor;
028: import org.pentaho.reportdesigner.lib.client.components.CenterPanelDialog;
029: import org.pentaho.reportdesigner.lib.client.i18n.TranslationManager;
030: import org.pentaho.reportdesigner.lib.client.undo.Undo;
031: import org.pentaho.reportdesigner.lib.client.undo.UndoEntry;
032: import org.pentaho.reportdesigner.lib.client.util.GUIUtils;
033: import org.pentaho.reportdesigner.lib.client.util.WindowUtils;
034: import org.pentaho.reportdesigner.lib.common.xml.ObjectConverter;
035: import org.pentaho.reportdesigner.lib.common.xml.ObjectConverterFactory;
036: import org.pentaho.reportdesigner.lib.common.xml.XMLConstants;
037: import org.pentaho.reportdesigner.lib.common.xml.XMLContext;
038: import org.pentaho.reportdesigner.lib.common.xml.XMLWriter;
039:
040: import javax.swing.*;
041: import javax.swing.event.ChangeEvent;
042: import javax.swing.event.ListSelectionEvent;
043: import javax.swing.event.ListSelectionListener;
044: import java.awt.event.ActionEvent;
045: import java.awt.event.ActionListener;
046: import java.io.IOException;
047: import java.util.ArrayList;
048: import java.util.Date;
049: import java.util.HashSet;
050: import java.util.TreeSet;
051: import java.util.logging.Level;
052: import java.util.logging.Logger;
053:
054: /**
055: * User: Martin
056: * Date: 31.01.2006
057: * Time: 10:31:01
058: */
059: public class PropertiesDataSetReportElement extends
060: DataSetReportElement {
061: @NonNls
062: @NotNull
063: private static final Logger LOG = Logger
064: .getLogger(PropertiesDataSetReportElement.class.getName());
065:
066: @NotNull
067: private TreeSet<PropertyInfo> properties;
068: @Nullable
069: private PropertiesTable infoTable;
070:
071: public PropertiesDataSetReportElement() {
072: properties = new TreeSet<PropertyInfo>();
073: }
074:
075: @NotNull
076: public String getQueryName() {
077: return "";//not used only one properties datase is possible and is handled different to the other DataSetReportElements
078: }
079:
080: public boolean canPreview() {
081: return true;
082: }
083:
084: public boolean canConfigure() {
085: return true;
086: }
087:
088: @NotNull
089: public TreeSet<PropertyInfo> getProperties() {
090: return properties;
091: }
092:
093: public void setProperties(@NotNull
094: final TreeSet<PropertyInfo> properties) {
095: final TreeSet<PropertyInfo> oldProperties = this .properties;
096: this .properties = properties;
097:
098: JTable infoTable = this .infoTable;
099: if (infoTable != null) {
100: infoTable.setModel(new PropertiesTableModel(
101: new ArrayList<PropertyInfo>(properties)));
102: }
103:
104: Undo undo = getUndo();
105: if (undo != null && !undo.isInProgress()) {
106: undo.startTransaction(PropertyKeys.PROPERTIES);
107: undo.add(new UndoEntry() {
108: public void undo() {
109: setProperties(oldProperties);
110: }
111:
112: public void redo() {
113: setProperties(properties);
114: }
115: });
116: undo.endTransaction();
117: }
118:
119: firePropertyChange(PropertyKeys.PROPERTIES, oldProperties,
120: properties);
121: }
122:
123: public boolean showConfigurationComponent(@NotNull
124: final ReportDialog parent, boolean firsttime) {
125: final CenterPanelDialog centerPanelDialog = CenterPanelDialog
126: .createDialog(parent, TranslationManager.getInstance()
127: .getTranslation("R",
128: "PropertiesDataSetConfigurator.Title"),
129: CenterPanelDialog.ModalityType.DOCUMENT_MODAL);
130:
131: @NonNls
132: FormLayout formLayout = new FormLayout(
133: "0dlu, fill:10dlu:grow, 4dlu, pref, 0dlu",
134: "0dlu, pref, 4dlu, pref, 4dlu, pref:grow, 0dlu");
135: @NonNls
136: CellConstraints cc = new CellConstraints();
137: JPanel configPanel = new JPanel(formLayout);
138:
139: ArrayList<PropertyInfo> propertyInfos = new ArrayList<PropertyInfo>();
140: propertyInfos.addAll(properties);
141: final PropertiesTableModel tableModel = new PropertiesTableModel(
142: propertyInfos);
143: final PropertiesTable table = new PropertiesTable(tableModel,
144: true);
145:
146: configPanel.add(new JScrollPane(table), cc.xywh(2, 2, 1, 5,
147: "fill, fill"));
148: JButton addButton = new JButton(TranslationManager
149: .getInstance().getTranslation("R",
150: "PropertiesDataSetConfigurator.ButtonAdd"));
151: configPanel.add(addButton, cc.xy(4, 2));
152: final JButton removeButton = new JButton(TranslationManager
153: .getInstance().getTranslation("R",
154: "PropertiesDataSetConfigurator.ButtonRemove"));
155: configPanel.add(removeButton, cc.xy(4, 4));
156:
157: removeButton.setEnabled(false);
158: table.getSelectionModel().addListSelectionListener(
159: new ListSelectionListener() {
160: public void valueChanged(@NotNull
161: ListSelectionEvent e) {
162: if (!e.getValueIsAdjusting()) {
163: removeButton.setEnabled(table
164: .getSelectedRows().length > 0);
165: }
166: }
167: });
168: removeButton.addActionListener(new ActionListener() {
169: public void actionPerformed(@NotNull
170: ActionEvent e) {
171: table.editingStopped(new ChangeEvent(this ));
172: tableModel.removeProperties(table.getSelectedRows());
173: }
174: });
175:
176: addButton.addActionListener(new ActionListener() {
177: public void actionPerformed(@NotNull
178: ActionEvent e) {
179: table.editingStopped(new ChangeEvent(this ));
180: tableModel.addProperty(new PropertyInfo("",
181: String.class, null));
182: }
183: });
184:
185: centerPanelDialog.setCenterPanel(configPanel);
186:
187: JButton okButton = new JButton(TranslationManager.getInstance()
188: .getTranslation("R", "Button.ok"));
189: JButton cancelButton = new JButton(TranslationManager
190: .getInstance().getTranslation("R", "Button.cancel"));
191:
192: okButton.addActionListener(new ActionListener() {
193: public void actionPerformed(@NotNull
194: ActionEvent e) {
195: table.editingStopped(new ChangeEvent(this ));
196: setProperties(new TreeSet<PropertyInfo>(tableModel
197: .getProperties()));
198: parent.getWorkspaceSettings().storeDialogBounds(
199: centerPanelDialog,
200: "PropertiesDataSetConfigurator");
201: centerPanelDialog.dispose();
202: }
203: });
204:
205: cancelButton.addActionListener(new ActionListener() {
206: public void actionPerformed(@NotNull
207: ActionEvent e) {
208: parent.getWorkspaceSettings().storeDialogBounds(
209: centerPanelDialog,
210: "PropertiesDataSetConfigurator");
211: centerPanelDialog.dispose();
212: }
213: });
214:
215: centerPanelDialog.setButtons(okButton, cancelButton, okButton,
216: cancelButton);
217:
218: if (!parent.getWorkspaceSettings().restoreDialogBounds(
219: centerPanelDialog, "PropertiesDataSetConfigurator")) {
220: centerPanelDialog.pack();
221: GUIUtils.ensureMinimumDialogSize(centerPanelDialog, 400,
222: 400);
223: }
224: WindowUtils.setLocationRelativeTo(centerPanelDialog, parent);
225: centerPanelDialog.setVisible(true);
226:
227: return true;
228: }
229:
230: @NotNull
231: public HashSet<String> getDefinedFields() {
232: HashSet<String> definedFields = new HashSet<String>();
233: for (PropertyInfo propertyInfo : properties) {
234: definedFields.add(propertyInfo.getKey());
235: }
236: return definedFields;
237: }
238:
239: @NotNull
240: public JComponent getInfoComponent() {
241: ArrayList<PropertyInfo> propertyInfos = new ArrayList<PropertyInfo>();
242: propertyInfos.addAll(properties);
243: infoTable = new PropertiesTable(new PropertiesTableModel(
244: propertyInfos), false);
245: return new JScrollPane(infoTable);
246: }
247:
248: @NotNull
249: public String getShortSummary() {
250: return "";
251: }
252:
253: public void accept(@Nullable
254: Object parent, @NotNull
255: ReportVisitor reportVisitor) throws ReportCreationException {
256: /*Object newParent = */
257: reportVisitor.visit(parent, this );
258: }
259:
260: protected void externalizeElements(@NotNull
261: XMLWriter xmlWriter, @NotNull
262: XMLContext xmlContext) throws IOException {
263: super .externalizeElements(xmlWriter, xmlContext);
264:
265: xmlWriter.startElement(PropertyKeys.PROPERTIES);
266: for (PropertyInfo propertyInfo : properties) {
267: Object value = propertyInfo.getValue();
268: if (value != null) {
269: ObjectConverter converter = ObjectConverterFactory
270: .getInstance().getConverter(value.getClass(),
271: null, xmlContext);
272: if (converter != null) {
273: xmlWriter.writeProperty(propertyInfo.getKey(),
274: propertyInfo.getClazz().getName(),
275: converter.getString(value));
276: }
277: } else {
278: xmlWriter.writeProperty(propertyInfo.getKey(),
279: propertyInfo.getClazz().getName(), "");
280: }
281: }
282: xmlWriter.closeElement(PropertyKeys.PROPERTIES);
283: }
284:
285: protected void readElement(@NotNull
286: ExpressionRegistry expressions, @NotNull
287: XmlPullNode node, @NotNull
288: XMLContext xmlContext) throws Exception {
289: super .readElement(expressions, node, xmlContext);
290:
291: if (PropertyKeys.PROPERTIES.equals(node.getRawName())) {
292: while (!node.isFinished()) {
293: Object child = node.readNextChild();
294: if (child instanceof XmlPullNode) {
295: XmlPullNode xmlPullNode = (XmlPullNode) child;
296: if (XMLConstants.PROPERTY.equals(xmlPullNode
297: .getRawName())) {
298: try {
299: String name = xmlPullNode
300: .getAttributeValueFromRawName(XMLConstants.NAME);
301: String type = xmlPullNode
302: .getAttributeValueFromRawName(XMLConstants.TYPE);
303: while (!xmlPullNode.isFinished()) {
304: Object value = xmlPullNode
305: .readNextChild();
306: if (value == null) {
307: value = "";
308: }
309: if (value instanceof String) {
310: String s = (String) value;
311: Class<?> clazz = Class
312: .forName(type);
313: if ("".equals(s)
314: && Boolean.class
315: .equals(clazz)) {
316: PropertyInfo propertyInfo = new PropertyInfo(
317: name, clazz,
318: Boolean.FALSE);
319: properties.add(propertyInfo);
320: } else if ("".equals(s)
321: && Integer.class
322: .equals(clazz)) {
323: PropertyInfo propertyInfo = new PropertyInfo(
324: name, clazz, null);
325: properties.add(propertyInfo);
326: } else if ("".equals(s)
327: && Date.class.equals(clazz)) {
328: PropertyInfo propertyInfo = new PropertyInfo(
329: name, clazz, null);
330: properties.add(propertyInfo);
331: } else if ("".equals(s)
332: && Double.class
333: .equals(clazz)) {
334: PropertyInfo propertyInfo = new PropertyInfo(
335: name, clazz, null);
336: properties.add(propertyInfo);
337: } else {
338: ObjectConverter converter = ObjectConverterFactory
339: .getInstance()
340: .getConverter(clazz,
341: null,
342: xmlContext);
343: if (converter != null) {
344: Object object = converter
345: .getObject(s);
346: PropertyInfo propertyInfo = new PropertyInfo(
347: name, clazz, object);
348: properties
349: .add(propertyInfo);
350: }
351: }
352: }
353: }
354: } catch (ClassNotFoundException e) {
355: if (LOG.isLoggable(Level.FINE))
356: LOG
357: .log(
358: Level.FINE,
359: "PropertiesDataSetReportElement.readElement ",
360: e);
361: }
362: }
363: }
364: }
365: }
366: }
367:
368: }
|