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.model;
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.ReportDesignerUtils;
024: import org.pentaho.reportdesigner.crm.report.ReportDesignerWindows;
025: import org.pentaho.reportdesigner.crm.report.ReportDialog;
026: import org.pentaho.reportdesigner.crm.report.ReportDialogConstants;
027: import org.pentaho.reportdesigner.crm.report.commands.ReportFileView;
028: import org.pentaho.reportdesigner.crm.report.connection.ColumnInfo;
029: import org.pentaho.reportdesigner.crm.report.datasetplugin.ColumnInfoTableModel;
030: import org.pentaho.reportdesigner.crm.report.datasetplugin.multidataset.MultiDataSetReportElement;
031: import org.pentaho.reportdesigner.crm.report.datasetplugin.multidataset.Query;
032: import org.pentaho.reportdesigner.crm.report.model.dataset.DataSetsReportElement;
033: import org.pentaho.reportdesigner.crm.report.model.functions.ExpressionRegistry;
034: import org.pentaho.reportdesigner.crm.report.reportelementinfo.ReportElementInfoFactory;
035: import org.pentaho.reportdesigner.crm.report.reportexporter.ReportCreationException;
036: import org.pentaho.reportdesigner.crm.report.reportexporter.ReportVisitor;
037: import org.pentaho.reportdesigner.crm.report.util.JFileChooserHelper;
038: import org.pentaho.reportdesigner.crm.report.util.ReportElementUtilities;
039: import org.pentaho.reportdesigner.lib.client.i18n.TranslationManager;
040: import org.pentaho.reportdesigner.lib.client.components.CenterPanelDialog;
041: import org.pentaho.reportdesigner.lib.common.xml.XMLConstants;
042: import org.pentaho.reportdesigner.lib.common.xml.XMLContext;
043: import org.pentaho.reportdesigner.lib.common.xml.XMLUtils;
044: import org.pentaho.reportdesigner.lib.common.xml.XMLWriter;
045:
046: import javax.swing.*;
047: import javax.swing.filechooser.FileFilter;
048: import java.awt.datatransfer.DataFlavor;
049: import java.awt.datatransfer.Transferable;
050: import java.awt.datatransfer.UnsupportedFlavorException;
051: import java.awt.dnd.DnDConstants;
052: import java.io.File;
053: import java.io.IOException;
054: import java.util.ArrayList;
055: import java.util.Collection;
056: import java.util.Collections;
057: import java.util.HashSet;
058: import java.util.logging.Level;
059: import java.util.logging.Logger;
060:
061: /**
062: * User: Martin
063: * Date: 20.10.2005
064: * Time: 15:40:13
065: */
066: public class SubReportDataElement extends ReportElement {
067: @NonNls
068: @NotNull
069: private static final Logger LOG = Logger
070: .getLogger(SubReportDataElement.class.getName());
071:
072: @Nullable
073: private ArrayList<ColumnInfo> columnInfos;
074:
075: @Nullable
076: private File currentMainReport;
077: @NotNull
078: private JTable infoTable;
079:
080: private boolean columnInfosLoaded;
081:
082: public SubReportDataElement() {
083: infoTable = new JTable();
084: }
085:
086: public void accept(@Nullable
087: Object parent, @NotNull
088: ReportVisitor reportVisitor) throws ReportCreationException {
089: /*Object newParent = */
090: reportVisitor.visit(parent, this );
091: }
092:
093: @Nullable
094: public File getCurrentMainReport() {
095: return currentMainReport;
096: }
097:
098: public void setCurrentMainReport(@Nullable
099: File currentMainReport) {
100: this .currentMainReport = currentMainReport;
101:
102: loadColumnInfos();
103: }
104:
105: @NotNull
106: public JComponent getInfoComponent() {
107: loadColumnInfos();
108:
109: ArrayList<ColumnInfo> columnInfos = this .columnInfos;
110:
111: @NonNls
112: FormLayout formLayout = new FormLayout(
113: "2dlu, pref, 4dlu, 0dlu:grow, 2dlu",
114: "4dlu, pref, 4dlu, pref, 4dlu, fill:default:grow, 2dlu");
115: JPanel infoPanel = new JPanel(formLayout);
116: @NonNls
117: CellConstraints cc = new CellConstraints();
118:
119: JLabel mainReportLabel = new JLabel(TranslationManager
120: .getInstance().getTranslation("R",
121: "SubReportDataElement.MainReportLabel"));
122:
123: File report = getCurrentMainReport();
124: JLabel mainReportPathLabel = new JLabel(TranslationManager
125: .getInstance().getTranslation("R",
126: "SubReportDataElement.NotSelected"));
127: if (report != null) {
128: mainReportPathLabel.setText(report.getName());
129: }
130:
131: infoPanel.add(mainReportLabel, cc.xy(2, 2));
132: infoPanel.add(mainReportPathLabel, cc.xy(4, 2));
133:
134: if (columnInfos != null) {
135: infoTable = new JTable(
136: new ColumnInfoTableModel(columnInfos));
137: infoTable
138: .setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
139: initDragAndDrop(infoTable, columnInfos);
140:
141: infoPanel.add(new JScrollPane(infoTable), cc.xyw(2, 6, 3,
142: "fill, fill"));
143: } else {
144: infoPanel
145: .add(
146: new JLabel(
147: TranslationManager
148: .getInstance()
149: .getTranslation("R",
150: "SubReportDataElement.CouldNotLoadColumns")),
151: cc.xyw(2, 6, 3, "fill, fill"));
152: }
153:
154: return infoPanel;
155: }
156:
157: public void loadColumnInfosCached() {
158: if (!columnInfosLoaded) {
159: columnInfosLoaded = true;
160: loadColumnInfos();
161: }
162: }
163:
164: private void loadColumnInfos() {
165: Report mainReport = null;
166:
167: File reportFile = getCurrentMainReport();
168:
169: ReportDialog dialog = null;
170: if (reportFile != null) {
171: dialog = ReportDesignerUtils
172: .getOpenReportDialog(reportFile);
173:
174: try {
175: mainReport = ReportDesignerUtils.openReport(reportFile);
176: } catch (Exception e) {
177: if (LOG.isLoggable(Level.FINE))
178: LOG
179: .log(
180: Level.FINE,
181: "SubReportDataElement.getInfoComponent ",
182: e);
183: }
184: }
185: if (dialog != null) {
186: mainReport = dialog.getReport();
187: }
188:
189: Report sr = getReport();
190: if (sr instanceof SubReport) {
191: ReportDialog subReportDialog = ReportDesignerWindows
192: .getInstance().getReportDialog(sr);
193: if (mainReport != null && subReportDialog != null) {
194: SubReport subReport = (SubReport) sr;
195: String query = getCurrentQuery(mainReport, subReport,
196: subReportDialog);
197:
198: if (query != null) {
199: DataSetsReportElement reportElement = mainReport
200: .getDataSetsReportElement();
201: ArrayList<ReportElement> elementArrayList = reportElement
202: .getChildren();
203: for (ReportElement element : elementArrayList) {
204: if (element instanceof MultiDataSetReportElement) {
205: MultiDataSetReportElement dataSetReportElement = (MultiDataSetReportElement) element;
206: ArrayList<Query> queries = dataSetReportElement
207: .getQueries();
208: if (!queries.isEmpty()
209: && query.equals(queries.get(0)
210: .getQueryName())) {
211: ArrayList<ColumnInfo> columnInfos = dataSetReportElement
212: .getColumnInfos();
213: this .columnInfos = columnInfos;
214: infoTable
215: .setModel(new ColumnInfoTableModel(
216: columnInfos));
217: return;
218: }
219: }
220: }
221: }
222: }
223: }
224: this .columnInfos = new ArrayList<ColumnInfo>();
225: infoTable.setModel(new ColumnInfoTableModel(columnInfos));
226: }
227:
228: @Nullable
229: private String getCurrentQuery(@NotNull
230: Report mainReport, @NotNull
231: SubReport subReport, @NotNull
232: ReportDialog subReportDialog) {
233: File currentReportFile = subReportDialog.getCurrentReportFile();
234: if (currentReportFile != null) {
235: FilePath filePath = new FilePath(currentReportFile
236: .getAbsolutePath());
237: SubReportElement subReportElement = ReportElementUtilities
238: .findSubReportElement(mainReport, filePath);
239: if (subReportElement != null) {
240: String query = subReportElement.getQuery();
241: if (query != null && !"".equals(query.trim())) {
242: return query;
243: }
244: }
245: }
246: return subReport.getQuery();
247: }
248:
249: private void initDragAndDrop(@NotNull
250: final JTable table, @NotNull
251: final ArrayList<ColumnInfo> columnInfos) {
252: @NonNls
253: final DataFlavor dataFlavorLibraryItems = new DataFlavor(
254: "application/x-icore-reportelement;class="
255: + ReportElement.class.getName(),
256: "ReportElement " + ReportDialogConstants.UNSELECTED);
257:
258: table.setTransferHandler(new TransferHandler() {
259: @NotNull
260: protected Transferable createTransferable(@NotNull
261: JComponent c) {
262: TextFieldReportElement textFieldReportElement = ReportElementInfoFactory
263: .getInstance().getTextFieldReportElementInfo()
264: .createReportElement();
265: int selectedRow = table.getSelectedRow();
266: ColumnInfo columnInfo = columnInfos.get(selectedRow);
267: textFieldReportElement.setFieldName(columnInfo
268: .getColumnName());
269: final ReportElement reportElement = textFieldReportElement;
270:
271: return new Transferable() {
272: @NotNull
273: public DataFlavor[] getTransferDataFlavors() {
274: return new DataFlavor[] { dataFlavorLibraryItems };
275: }
276:
277: public boolean isDataFlavorSupported(@NotNull
278: DataFlavor flavor) {
279: return dataFlavorLibraryItems.equals(flavor);
280: }
281:
282: @NotNull
283: public Object getTransferData(@NotNull
284: DataFlavor flavor)
285: throws UnsupportedFlavorException {
286: if (dataFlavorLibraryItems.equals(flavor)) {
287: return reportElement;
288: } else {
289: throw new UnsupportedFlavorException(flavor);
290: }
291: }
292: };
293: }
294:
295: public int getSourceActions(@NotNull
296: JComponent c) {
297: return DnDConstants.ACTION_COPY;
298: }
299: });
300:
301: table.setDragEnabled(true);
302:
303: }
304:
305: public boolean canConfigure() {
306: return true;
307: }
308:
309: public void showConfigurationComponent(@NotNull
310: ReportDialog parent) {
311: File f = getCurrentMainReport();
312:
313: JFileChooser fileChooser;
314: if (f != null && f.canRead()) {
315: fileChooser = new JFileChooserHelper(f,
316: CenterPanelDialog.ModalityType.DOCUMENT_MODAL);
317: fileChooser.setSelectedFile(f);
318: } else {
319: fileChooser = new JFileChooserHelper(
320: CenterPanelDialog.ModalityType.DOCUMENT_MODAL);
321: File currentReportFile = parent.getCurrentReportFile();
322: if (currentReportFile != null) {
323: fileChooser.setCurrentDirectory(currentReportFile);
324: }
325: }
326:
327: fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
328: fileChooser.setFileFilter(new FileFilter() {
329: public boolean accept(@NotNull
330: File f) {
331: return f.isDirectory()
332: || f
333: .getName()
334: .toLowerCase()
335: .endsWith(
336: ReportDialogConstants.REPORT_FILE_ENDING);
337: }
338:
339: @NotNull
340: public String getDescription() {
341: return TranslationManager.getInstance().getTranslation(
342: "R", "FileChooser.ReportFiles.Description");
343: }
344: });
345:
346: fileChooser.setFileView(new ReportFileView());
347:
348: if (fileChooser.showOpenDialog(parent) == JFileChooser.APPROVE_OPTION) {
349: setCurrentMainReport(fileChooser.getSelectedFile());
350:
351: loadColumnInfos();
352: }
353: }
354:
355: @NotNull
356: public Collection<String> getDefinedFields() {
357: ArrayList<ColumnInfo> columnInfos = this .columnInfos;
358: if (columnInfos != null) {
359: HashSet<String> hashSet = new HashSet<String>();
360: for (ColumnInfo ci : columnInfos) {
361: hashSet.add(ci.getColumnName());
362: }
363: return hashSet;
364: }
365: return Collections.emptySet();
366: }
367:
368: protected void externalizeElements(@NotNull
369: XMLWriter xmlWriter, @NotNull
370: XMLContext xmlContext) throws IOException {
371: super .externalizeElements(xmlWriter, xmlContext);
372:
373: if (currentMainReport != null) {
374: xmlWriter.writeProperty(PropertyKeys.CURRENT_MAINREPORT,
375: currentMainReport.getAbsolutePath());
376: }
377: }
378:
379: protected void readElement(@NotNull
380: ExpressionRegistry expressions, @NotNull
381: XmlPullNode node, @NotNull
382: XMLContext xmlContext) throws Exception {
383: super .readElement(expressions, node, xmlContext);
384:
385: if (XMLConstants.PROPERTY.equals(node.getRawName())
386: && PropertyKeys.CURRENT_MAINREPORT
387: .equals(node
388: .getAttributeValueFromRawName(XMLConstants.NAME))) {
389: currentMainReport = new File(XMLUtils.readProperty(
390: PropertyKeys.CURRENT_MAINREPORT, node));
391: }
392: }
393: }
|