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.jdbc;
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.ReportDialog;
021: import org.pentaho.reportdesigner.crm.report.UndoConstants;
022: import org.pentaho.reportdesigner.crm.report.connection.ColumnInfo;
023: import org.pentaho.reportdesigner.crm.report.connection.ConnectionFactory;
024: import org.pentaho.reportdesigner.crm.report.connection.MetaDataService;
025: import org.pentaho.reportdesigner.lib.client.components.CenterPanelDialog;
026: import org.pentaho.reportdesigner.lib.client.components.ComponentFactory;
027: import org.pentaho.reportdesigner.lib.client.components.ProgressDialog;
028: import org.pentaho.reportdesigner.lib.client.i18n.TranslationManager;
029: import org.pentaho.reportdesigner.lib.client.util.GUIUtils;
030: import org.pentaho.reportdesigner.lib.client.util.WindowUtils;
031:
032: import javax.swing.*;
033: import java.awt.*;
034: import java.awt.event.ActionEvent;
035: import java.awt.event.ActionListener;
036: import java.beans.PropertyChangeEvent;
037: import java.beans.PropertyChangeListener;
038: import java.util.ArrayList;
039: import java.util.logging.Level;
040: import java.util.logging.Logger;
041:
042: /**
043: * User: Martin
044: * Date: 01.02.2006
045: * Time: 11:48:14
046: */
047: public class JDBCDataSetReportElementConfigurator {
048: @NonNls
049: @NotNull
050: private static final Logger LOG = Logger
051: .getLogger(JDBCDataSetReportElementConfigurator.class
052: .getName());
053:
054: public static boolean showJDBCDataSetReportElementConfigurator(
055: @NotNull
056: ReportDialog parent, @NotNull
057: JDBCDataSetReportElement jdbcDataSetReportElement,
058: boolean firsttime) {
059: JDBCDataSetReportElementConfigurator configurator = new JDBCDataSetReportElementConfigurator(
060: parent, jdbcDataSetReportElement, firsttime);
061: return configurator.ok;
062: }
063:
064: private boolean ok;
065: @NotNull
066: private CenterPanelDialog centerPanelDialog;
067: @NotNull
068: private JDBCConnectionConfigurationPanel connectionConfigurationPanel;
069:
070: private JDBCDataSetReportElementConfigurator(@NotNull
071: final ReportDialog parent, @NotNull
072: final JDBCDataSetReportElement jdbcDataSetReportElement,
073: boolean firsttime) {
074: ok = false;
075:
076: centerPanelDialog = CenterPanelDialog.createDialog(parent,
077: TranslationManager.getInstance().getTranslation("R",
078: "JDBCDataSetReportElementConfigurator.Title"),
079: CenterPanelDialog.ModalityType.DOCUMENT_MODAL);
080:
081: @NonNls
082: FormLayout formLayout = new FormLayout(
083: "4dlu, fill:default, 4dlu, pref, 4dlu, fill:default:grow, 4dlu",
084: "4dlu, " + "pref, " + "4dlu, " + "fill:10dlu:grow, "
085: + "4dlu");
086: @NonNls
087: CellConstraints cc = new CellConstraints();
088:
089: JPanel centerPanel = new JPanel(formLayout);
090:
091: JLabel connectionSettingsLabel = new JLabel(
092: TranslationManager
093: .getInstance()
094: .getTranslation("R",
095: "JDBCDataSetReportElementConfigurator.ConnectionSettings.Label"));
096: centerPanel.add(connectionSettingsLabel, cc.xy(2, 2));
097: final JButton connectionSettingsButton = ComponentFactory
098: .createButton("R",
099: "JDBCDataSetReportElementConfigurator.ConnectionSettings.Button");
100: connectionSettingsButton.setMargin(new Insets(1, 1, 1, 1));
101: centerPanel.add(connectionSettingsButton, cc.xy(4, 2));
102:
103: final SQLQueryConfigurationPanel sqlQueryConfigurationPanel = new SQLQueryConfigurationPanel(
104: parent, parent.getWorkspaceSettings());
105: centerPanel.add(sqlQueryConfigurationPanel, cc.xyw(2, 4, 5));
106:
107: final JButton okButton = new JButton(TranslationManager
108: .getInstance().getTranslation("R", "Button.ok"));
109: JButton cancelButton = new JButton(TranslationManager
110: .getInstance().getTranslation("R", "Button.cancel"));
111:
112: centerPanelDialog.setCenterPanel(centerPanel);
113: centerPanelDialog.setButtons(okButton, cancelButton, okButton,
114: cancelButton);
115:
116: connectionConfigurationPanel = new JDBCConnectionConfigurationPanel(
117: parent, parent.getWorkspaceSettings());
118:
119: connectionConfigurationPanel.setJars(jdbcDataSetReportElement
120: .getJars());
121: connectionConfigurationPanel
122: .setDriverClass(jdbcDataSetReportElement
123: .getDriverClass());
124: connectionConfigurationPanel
125: .setConnectionString(jdbcDataSetReportElement
126: .getConnectionString());
127: connectionConfigurationPanel
128: .setUsername(jdbcDataSetReportElement.getUserName());
129: connectionConfigurationPanel
130: .setPassword(jdbcDataSetReportElement.getPassword());
131: sqlQueryConfigurationPanel.setSQLQuery(jdbcDataSetReportElement
132: .getSqlQuery());
133: sqlQueryConfigurationPanel
134: .setMaxPreviewRows(jdbcDataSetReportElement
135: .getMaxPreviewRows());
136:
137: connectionSettingsButton
138: .addActionListener(new ActionListener() {
139: public void actionPerformed(@NotNull
140: ActionEvent e) {
141: final CenterPanelDialog connectionDialog = CenterPanelDialog
142: .createDialog(
143: centerPanelDialog,
144: TranslationManager
145: .getInstance()
146: .getTranslation("R",
147: "JDBCDataSetReportElementConfigurator.ConnectionSettings.Title"),
148: CenterPanelDialog.ModalityType.DOCUMENT_MODAL);
149: connectionDialog
150: .setCenterPanel(connectionConfigurationPanel);
151: final JButton okButton = new JButton(
152: TranslationManager.getInstance()
153: .getTranslation("R",
154: "Button.ok"));
155: JButton cancelButton = new JButton(
156: TranslationManager.getInstance()
157: .getTranslation("R",
158: "Button.cancel"));
159:
160: okButton.setEnabled(false);
161:
162: final PropertyChangeListener propertyChangeListener = new PropertyChangeListener() {
163: public void propertyChange(@NotNull
164: PropertyChangeEvent evt) {
165: okButton.setEnabled(Boolean.TRUE
166: .equals(evt.getNewValue()));
167: }
168: };
169: connectionConfigurationPanel
170: .addPropertyChangeListener2(
171: JDBCConnectionConfigurationPanel.CONNECTION_TEST,
172: propertyChangeListener);
173:
174: okButton
175: .addActionListener(new ActionListener() {
176: public void actionPerformed(
177: @NotNull
178: ActionEvent e) {
179: final ProgressDialog progressDialog = ProgressDialog
180: .createProgressDialog(
181: parent,
182: TranslationManager
183: .getInstance()
184: .getTranslation(
185: "R",
186: "JDBCDataSetReportElementConfigurator.ProgressDialog.Title"),
187: "");
188:
189: Thread t = new Thread() {
190: public void run() {
191: try {
192: final MetaDataService metaDataService = ConnectionFactory
193: .getJDBCService(
194: connectionConfigurationPanel
195: .getJars(),
196: connectionConfigurationPanel
197: .getDriverClass(),
198: connectionConfigurationPanel
199: .getConnectionString(),
200: connectionConfigurationPanel
201: .getUsername(),
202: connectionConfigurationPanel
203: .getPassword());
204:
205: EventQueue
206: .invokeLater(new Runnable() {
207: public void run() {
208: sqlQueryConfigurationPanel
209: .setMetaDataService(metaDataService);
210: connectionConfigurationPanel
211: .removePropertyChangeListener2(
212: JDBCConnectionConfigurationPanel.CONNECTION_TEST,
213: propertyChangeListener);
214: parent
215: .getWorkspaceSettings()
216: .storeDialogBounds(
217: connectionDialog,
218: "JDBCDataSetReportElementConfigurator.ConnectionSettings");
219: connectionDialog
220: .dispose();
221: progressDialog
222: .dispose();
223: }
224: });
225: } catch (Throwable e1) {
226: if (LOG
227: .isLoggable(Level.FINE))
228: LOG
229: .log(
230: Level.FINE,
231: "JDBCDataSetReportElementConfigurator.run ",
232: e1);
233: EventQueue
234: .invokeLater(new Runnable() {
235: public void run() {
236: sqlQueryConfigurationPanel
237: .setMetaDataService(null);
238: connectionConfigurationPanel
239: .removePropertyChangeListener2(
240: JDBCConnectionConfigurationPanel.CONNECTION_TEST,
241: propertyChangeListener);
242: parent
243: .getWorkspaceSettings()
244: .storeDialogBounds(
245: connectionDialog,
246: "JDBCDataSetReportElementConfigurator.ConnectionSettings");
247: connectionDialog
248: .dispose();
249: progressDialog
250: .dispose();
251: }
252: });
253: }
254: }
255: };
256: t.setDaemon(true);
257: t.start();
258:
259: if (t.isAlive()) {
260: progressDialog
261: .setVisible(true);
262: } else {
263: progressDialog.dispose();
264: }
265: }
266: });
267:
268: cancelButton
269: .addActionListener(new ActionListener() {
270: public void actionPerformed(
271: @NotNull
272: ActionEvent e) {
273: connectionConfigurationPanel
274: .removePropertyChangeListener2(
275: JDBCConnectionConfigurationPanel.CONNECTION_TEST,
276: propertyChangeListener);
277: parent
278: .getWorkspaceSettings()
279: .storeDialogBounds(
280: connectionDialog,
281: "JDBCDataSetReportElementConfigurator.ConnectionSettings");
282: connectionDialog.dispose();
283: }
284: });
285:
286: connectionDialog.setButtons(okButton,
287: cancelButton, okButton, cancelButton);
288: if (!parent
289: .getWorkspaceSettings()
290: .restoreDialogBounds(connectionDialog,
291: "JDBCDataSetReportElementConfigurator.ConnectionSettings")) {
292: connectionDialog.pack();
293: GUIUtils.ensureMinimumDialogSize(
294: connectionDialog, 500, 250);
295: }
296:
297: WindowUtils.setLocationRelativeTo(
298: connectionDialog, centerPanelDialog);
299: connectionDialog.setVisible(true);
300: }
301: });
302:
303: sqlQueryConfigurationPanel.addPropertyChangeListener2(
304: SQLQueryConfigurationPanel.OK,
305: new PropertyChangeListener() {
306: public void propertyChange(@NotNull
307: PropertyChangeEvent evt) {
308: okButton.setEnabled(Boolean.TRUE.equals(evt
309: .getNewValue()));
310: }
311: });
312:
313: final ProgressDialog progressDialog = ProgressDialog
314: .createProgressDialog(
315: parent,
316: TranslationManager
317: .getInstance()
318: .getTranslation("R",
319: "JDBCDataSetReportElementConfigurator.ProgressDialog.Title"),
320: "");
321: Thread t = new Thread() {
322: public void run() {
323: try {
324: final MetaDataService jdbcService = ConnectionFactory
325: .getJDBCService(
326: connectionConfigurationPanel
327: .getJars(),
328: connectionConfigurationPanel
329: .getDriverClass(),
330: connectionConfigurationPanel
331: .getConnectionString(),
332: connectionConfigurationPanel
333: .getUsername(),
334: connectionConfigurationPanel
335: .getPassword());
336: EventQueue.invokeLater(new Runnable() {
337: public void run() {
338: sqlQueryConfigurationPanel
339: .setMetaDataService(jdbcService);
340: progressDialog.dispose();
341: }
342: });
343: } catch (Throwable e) {
344: if (LOG.isLoggable(Level.FINE))
345: LOG
346: .log(
347: Level.FINE,
348: "JDBCDataSetReportElementConfigurator.run ",
349: e);
350: EventQueue.invokeLater(new Runnable() {
351: public void run() {
352: sqlQueryConfigurationPanel
353: .setMetaDataService(null);
354: progressDialog.dispose();
355: }
356: });
357: }
358: }
359: };
360: if (!firsttime) {
361: t.setDaemon(true);
362: t.start();
363: if (t.isAlive()) {
364: progressDialog.setVisible(true);
365: } else {
366: progressDialog.dispose();
367: }
368: }
369:
370: cancelButton.addActionListener(new ActionListener() {
371: public void actionPerformed(@NotNull
372: ActionEvent e) {
373: sqlQueryConfigurationPanel.dispose();
374: ok = false;
375: centerPanelDialog.dispose();
376: }
377: });
378:
379: okButton.addActionListener(new ActionListener() {
380: public void actionPerformed(@NotNull
381: ActionEvent e) {
382: parent.getUndo().startTransaction(
383: UndoConstants.CONNECTION_SETTINGS);
384: jdbcDataSetReportElement
385: .setJars(connectionConfigurationPanel.getJars());
386: jdbcDataSetReportElement
387: .setDriverClass(connectionConfigurationPanel
388: .getDriverClass());
389: jdbcDataSetReportElement
390: .setConnectionString(connectionConfigurationPanel
391: .getConnectionString());
392: jdbcDataSetReportElement
393: .setUserName(connectionConfigurationPanel
394: .getUsername());
395: jdbcDataSetReportElement
396: .setPassword(connectionConfigurationPanel
397: .getPassword());
398: jdbcDataSetReportElement
399: .setSqlQuery(sqlQueryConfigurationPanel
400: .getSQLQuery());
401: jdbcDataSetReportElement
402: .setColumnInfos(new ArrayList<ColumnInfo>(
403: sqlQueryConfigurationPanel
404: .getAvailableColumnInfos()));
405: jdbcDataSetReportElement
406: .setMaxPreviewRows(sqlQueryConfigurationPanel
407: .getMaxPreviewRows());
408: parent.getUndo().endTransaction();
409:
410: parent.getWorkspaceSettings().storeDialogBounds(
411: centerPanelDialog,
412: "JDBCDataSetReportElementConfigurator");
413: sqlQueryConfigurationPanel.dispose();
414: ok = true;
415: centerPanelDialog.dispose();
416: }
417: });
418:
419: if (!parent.getWorkspaceSettings().restoreDialogBounds(
420: centerPanelDialog,
421: "JDBCDataSetReportElementConfigurator")) {
422: centerPanelDialog.pack();
423: GUIUtils.ensureMinimumDialogSize(centerPanelDialog, 500,
424: 400);
425: }
426: WindowUtils.setLocationRelativeTo(centerPanelDialog, parent);
427:
428: if (firsttime) {
429: EventQueue.invokeLater(new Runnable() {
430: public void run() {
431: connectionSettingsButton.doClick();
432: }
433: });
434: }
435:
436: centerPanelDialog.setVisible(true);
437: }
438: }
|