001: /*
002: * Copyright (C) 2005 - 2008 JasperSoft Corporation. All rights reserved.
003: * http://www.jaspersoft.com.
004: *
005: * Unless you have purchased a commercial license agreement from JasperSoft,
006: * the following license terms apply:
007: *
008: * This program is free software; you can redistribute it and/or modify
009: * it under the terms of the GNU General Public License version 2 as published by
010: * the Free Software Foundation.
011: *
012: * This program is distributed WITHOUT ANY WARRANTY; and without the
013: * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
014: * See the GNU General Public License for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * along with this program; if not, see http://www.gnu.org/licenses/gpl.txt
018: * or write to:
019: *
020: * Free Software Foundation, Inc.,
021: * 59 Temple Place - Suite 330,
022: * Boston, MA USA 02111-1307
023: *
024: *
025: *
026: *
027: * ConnectionsDialog.java
028: *
029: * Created on 7 maggio 2003, 23.43
030: *
031: */
032:
033: package it.businesslogic.ireport.gui;
034:
035: import it.businesslogic.ireport.IReportConnection;
036: import javax.swing.table.*;
037: import javax.swing.*;
038: import javax.swing.event.*;
039: import it.businesslogic.ireport.*;
040: import it.businesslogic.ireport.util.Misc;
041:
042: import java.util.*;
043:
044: import org.apache.xerces.parsers.DOMParser;
045: import org.w3c.dom.*;
046: import org.xml.sax.SAXException;
047: import it.businesslogic.ireport.util.I18n;
048:
049: /**
050: *
051: * @author Administrator
052: */
053: public class ConnectionsDialog extends javax.swing.JDialog {
054:
055: /** Creates new form ValuesDialog */
056: public ConnectionsDialog(java.awt.Frame parent, boolean modal) {
057: super (parent, modal);
058: initComponents();
059: applyI18n();
060: this .setSize(490, 500);
061: //DefaultTableColumnModel dtcm = (DefaultTableColumnModel)jTableParameters.getColumnModel();
062: //DefaultTableCellRenderer tcr = (DefaultTableCellRenderer)dtcm.getColumn(0).getHeaderRenderer();
063:
064: //new DefaultTableCellRenderer();
065: //tcr.setFont(jTableParameters.getFont());
066: //tcr.setBackground(this.getBackground());
067: //tcr.setBorder( new javax.swing.border.BevelBorder( javax.swing.border.BevelBorder.RAISED));
068: //dtcm.getColumn(0).setHeaderRenderer(tcr);
069:
070: DefaultListSelectionModel dlsm = (DefaultListSelectionModel) this .jTableParameters
071: .getSelectionModel();
072: dlsm
073: .addListSelectionListener(new javax.swing.event.ListSelectionListener() {
074: public void valueChanged(ListSelectionEvent e) {
075: jTableParametersListSelectionValueChanged(e);
076: }
077: });
078:
079: // Open in center...
080: it.businesslogic.ireport.util.Misc.centerFrame(this );
081:
082: DefaultTableModel dtm = (DefaultTableModel) jTableParameters
083: .getModel();
084:
085: Enumeration e = MainFrame.getMainInstance().getConnections()
086: .elements();
087: Object defaultConnection = MainFrame.getMainInstance()
088: .getProperties().get("DefaultConnection");
089:
090: IReportConnection default_irc = null;
091: if (defaultConnection != null
092: && defaultConnection instanceof IReportConnection) {
093: default_irc = (IReportConnection) defaultConnection;
094: } else {
095: // Default connection not set!!!
096: if (((MainFrame) parent).getConnections().size() > 0) {
097: default_irc = (IReportConnection) MainFrame
098: .getMainInstance().getConnections()
099: .elementAt(0);
100: MainFrame.getMainInstance().setActiveConnection(
101: default_irc);
102: }
103: }
104:
105: while (e.hasMoreElements()) {
106: IReportConnection con = (IReportConnection) e.nextElement();
107: dtm.addRow(new Object[] { con, con.getDescription(),
108: new Boolean(default_irc == con) });
109: }
110:
111: javax.swing.KeyStroke escape = javax.swing.KeyStroke
112: .getKeyStroke(java.awt.event.KeyEvent.VK_ESCAPE, 0,
113: false);
114: javax.swing.Action escapeAction = new javax.swing.AbstractAction() {
115: public void actionPerformed(java.awt.event.ActionEvent e) {
116: setVisible(false);
117: }
118: };
119:
120: getRootPane().getInputMap(
121: javax.swing.JComponent.WHEN_IN_FOCUSED_WINDOW).put(
122: escape, "ESCAPE");
123: getRootPane().getActionMap().put("ESCAPE", escapeAction);
124:
125: //to make the default button ...
126: //this.getRootPane().setDefaultButton(this.jButtonOK);
127: }
128:
129: public void jTableParametersListSelectionValueChanged(
130: javax.swing.event.ListSelectionEvent e) {
131: if (this .jTableParameters.getSelectedRowCount() > 0) {
132: this .jButtonModifyParameter.setEnabled(true);
133: this .jButtonDeleteParameter.setEnabled(true);
134: this .jButtonDefault.setEnabled(true);
135: } else {
136: this .jButtonModifyParameter.setEnabled(false);
137: this .jButtonDeleteParameter.setEnabled(false);
138: this .jButtonDefault.setEnabled(false);
139: }
140: }
141:
142: /** This method is called from within the constructor to
143: * initialize the form.
144: * WARNING: Do NOT modify this code. The content of this method is
145: * always regenerated by the Form Editor.
146: */
147: // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
148: private void initComponents() {
149: java.awt.GridBagConstraints gridBagConstraints;
150:
151: jPanelParameters = new javax.swing.JPanel();
152: jScrollPane1 = new javax.swing.JScrollPane();
153: jTableParameters = new javax.swing.JTable();
154: jPanelButtons = new javax.swing.JPanel();
155: jButtonNewParameter = new javax.swing.JButton();
156: jButtonModifyParameter = new javax.swing.JButton();
157: jButtonDeleteParameter = new javax.swing.JButton();
158: jButtonDefault = new javax.swing.JButton();
159: jButtonImport = new javax.swing.JButton();
160: jButtonExport = new javax.swing.JButton();
161:
162: setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
163: setTitle("Connections / Datasources");
164: addWindowListener(new java.awt.event.WindowAdapter() {
165: public void windowClosing(java.awt.event.WindowEvent evt) {
166: closeDialog(evt);
167: }
168: });
169:
170: jPanelParameters.setLayout(new java.awt.GridBagLayout());
171:
172: jScrollPane1.setBackground(new java.awt.Color(255, 255, 255));
173: jTableParameters
174: .setModel(new javax.swing.table.DefaultTableModel(
175: new Object[][] {
176:
177: }, new String[] { "Name", "Datasource type",
178: "Default" }) {
179: Class[] types = new Class[] {
180: java.lang.Object.class,
181: java.lang.Object.class,
182: java.lang.Boolean.class };
183: boolean[] canEdit = new boolean[] { false, false,
184: false };
185:
186: public Class getColumnClass(int columnIndex) {
187: return types[columnIndex];
188: }
189:
190: public boolean isCellEditable(int rowIndex,
191: int columnIndex) {
192: return canEdit[columnIndex];
193: }
194: });
195: jTableParameters
196: .addMouseListener(new java.awt.event.MouseAdapter() {
197: public void mouseClicked(
198: java.awt.event.MouseEvent evt) {
199: jTableParametersMouseClicked(evt);
200: }
201: });
202:
203: jScrollPane1.setViewportView(jTableParameters);
204:
205: gridBagConstraints = new java.awt.GridBagConstraints();
206: gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
207: gridBagConstraints.weightx = 1.0;
208: gridBagConstraints.weighty = 1.0;
209: gridBagConstraints.insets = new java.awt.Insets(4, 4, 4, 4);
210: jPanelParameters.add(jScrollPane1, gridBagConstraints);
211:
212: jPanelButtons.setLayout(new java.awt.GridBagLayout());
213:
214: jPanelButtons.setMinimumSize(new java.awt.Dimension(100, 10));
215: jPanelButtons.setPreferredSize(new java.awt.Dimension(100, 10));
216: jButtonNewParameter.setText("New");
217: jButtonNewParameter
218: .addActionListener(new java.awt.event.ActionListener() {
219: public void actionPerformed(
220: java.awt.event.ActionEvent evt) {
221: jButtonNewParameterActionPerformed(evt);
222: }
223: });
224:
225: gridBagConstraints = new java.awt.GridBagConstraints();
226: gridBagConstraints.gridx = 0;
227: gridBagConstraints.gridy = 0;
228: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
229: gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
230: gridBagConstraints.insets = new java.awt.Insets(3, 3, 3, 3);
231: jPanelButtons.add(jButtonNewParameter, gridBagConstraints);
232:
233: jButtonModifyParameter.setText("Modify");
234: jButtonModifyParameter.setEnabled(false);
235: jButtonModifyParameter
236: .addActionListener(new java.awt.event.ActionListener() {
237: public void actionPerformed(
238: java.awt.event.ActionEvent evt) {
239: jButtonModifyParameterActionPerformed(evt);
240: }
241: });
242:
243: gridBagConstraints = new java.awt.GridBagConstraints();
244: gridBagConstraints.gridx = 0;
245: gridBagConstraints.gridy = 1;
246: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
247: gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
248: gridBagConstraints.insets = new java.awt.Insets(0, 3, 5, 3);
249: jPanelButtons.add(jButtonModifyParameter, gridBagConstraints);
250:
251: jButtonDeleteParameter.setText("Delete");
252: jButtonDeleteParameter.setEnabled(false);
253: jButtonDeleteParameter
254: .addActionListener(new java.awt.event.ActionListener() {
255: public void actionPerformed(
256: java.awt.event.ActionEvent evt) {
257: jButtonDeleteParameterActionPerformed(evt);
258: }
259: });
260:
261: gridBagConstraints = new java.awt.GridBagConstraints();
262: gridBagConstraints.gridx = 0;
263: gridBagConstraints.gridy = 2;
264: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
265: gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
266: gridBagConstraints.weightx = 1.0;
267: gridBagConstraints.insets = new java.awt.Insets(0, 3, 5, 3);
268: jPanelButtons.add(jButtonDeleteParameter, gridBagConstraints);
269:
270: jButtonDefault.setText("Set as default");
271: jButtonDefault.setActionCommand("Set as active");
272: jButtonDefault.setEnabled(false);
273: jButtonDefault.setMargin(new java.awt.Insets(2, 4, 2, 4));
274: jButtonDefault
275: .addActionListener(new java.awt.event.ActionListener() {
276: public void actionPerformed(
277: java.awt.event.ActionEvent evt) {
278: jButtonDeleteParameterActionPerformed1(evt);
279: }
280: });
281:
282: gridBagConstraints = new java.awt.GridBagConstraints();
283: gridBagConstraints.gridx = 0;
284: gridBagConstraints.gridy = 3;
285: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
286: gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
287: gridBagConstraints.weightx = 1.0;
288: gridBagConstraints.insets = new java.awt.Insets(8, 3, 5, 3);
289: jPanelButtons.add(jButtonDefault, gridBagConstraints);
290:
291: jButtonImport.setText("Import...");
292: jButtonImport.setActionCommand("Set as active");
293: jButtonImport.setMargin(new java.awt.Insets(2, 4, 2, 4));
294: jButtonImport
295: .addActionListener(new java.awt.event.ActionListener() {
296: public void actionPerformed(
297: java.awt.event.ActionEvent evt) {
298: jButtonImportActionPerformed(evt);
299: }
300: });
301:
302: gridBagConstraints = new java.awt.GridBagConstraints();
303: gridBagConstraints.gridx = 0;
304: gridBagConstraints.gridy = 4;
305: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
306: gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
307: gridBagConstraints.weightx = 1.0;
308: gridBagConstraints.insets = new java.awt.Insets(8, 3, 5, 3);
309: jPanelButtons.add(jButtonImport, gridBagConstraints);
310:
311: jButtonExport.setText("Export...");
312: jButtonExport.setActionCommand("Set as active");
313: jButtonExport.setMargin(new java.awt.Insets(2, 4, 2, 4));
314: jButtonExport
315: .addActionListener(new java.awt.event.ActionListener() {
316: public void actionPerformed(
317: java.awt.event.ActionEvent evt) {
318: jButtonExportActionPerformed(evt);
319: }
320: });
321:
322: gridBagConstraints = new java.awt.GridBagConstraints();
323: gridBagConstraints.gridx = 0;
324: gridBagConstraints.gridy = 5;
325: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
326: gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
327: gridBagConstraints.weightx = 1.0;
328: gridBagConstraints.weighty = 1.0;
329: gridBagConstraints.insets = new java.awt.Insets(0, 3, 5, 3);
330: jPanelButtons.add(jButtonExport, gridBagConstraints);
331:
332: gridBagConstraints = new java.awt.GridBagConstraints();
333: gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
334: gridBagConstraints.weighty = 1.0;
335: gridBagConstraints.insets = new java.awt.Insets(4, 4, 4, 4);
336: jPanelParameters.add(jPanelButtons, gridBagConstraints);
337:
338: getContentPane().add(jPanelParameters,
339: java.awt.BorderLayout.CENTER);
340:
341: pack();
342: }// </editor-fold>//GEN-END:initComponents
343:
344: private void jButtonExportActionPerformed(
345: java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonExportActionPerformed
346:
347: if (jTableParameters.getRowCount() == 0) {
348: JOptionPane
349: .showMessageDialog(
350: MainFrame.getMainInstance(),
351: I18n
352: .getString(
353: "messages.connectionsDialog.noConnectionToExport",
354: "No connections to export"),
355: "", JOptionPane.INFORMATION_MESSAGE);
356: return;
357: }
358:
359: JFileChooser jfc = new JFileChooser();
360: jfc.setFileFilter(new javax.swing.filechooser.FileFilter() {
361: public boolean accept(java.io.File file) {
362: String filename = file.getName().toLowerCase();
363: return (filename.endsWith(".xml") || file.isDirectory());
364: }
365:
366: public String getDescription() {
367: return "iReport connection/datasource definition (*.xml)";
368: }
369: });
370:
371: if (jfc.showSaveDialog(this ) == JFileChooser.APPROVE_OPTION) {
372:
373: try {
374: java.io.File f = jfc.getSelectedFile();
375: String fileName = f.getName();
376: if (fileName.indexOf(".") < 0) {
377: fileName += ".xml";
378: }
379: f = new java.io.File(f.getParent(), fileName);
380:
381: if (f.exists()) {
382: if (JOptionPane
383: .showConfirmDialog(
384: this ,
385: I18n
386: .getFormattedString(
387: "messages.connectionsDialog.overwrite",
388: "{0} already exists.\nDo you want overwrite it?",
389: new Object[] { ""
390: + f }), "",
391: JOptionPane.YES_NO_CANCEL_OPTION,
392: JOptionPane.WARNING_MESSAGE) != JOptionPane.OK_OPTION) {
393: return;
394: }
395: }
396:
397: java.io.PrintWriter pw = new java.io.PrintWriter(
398: new java.io.OutputStreamWriter(
399: new java.io.FileOutputStream(f), "UTF8")); //UTF8
400: pw.print("<?xml version=\"1.0\"?>");
401: pw.println("<!-- iReport connections -->");
402: pw.println("<iReportConnectionSet>");
403:
404: Enumeration con_enum = MainFrame.getMainInstance()
405: .getConnections().elements();
406: int i = 0;
407:
408: while (con_enum.hasMoreElements()) {
409: i++;
410: IReportConnection con = (IReportConnection) con_enum
411: .nextElement();
412: con.save(pw);
413: }
414:
415: pw.println("</iReportConnectionSet>");
416:
417: pw.close();
418:
419: JOptionPane
420: .showMessageDialog(
421: MainFrame.getMainInstance(),
422: I18n
423: .getFormattedString(
424: "messages.connectionsDialog.connectionsExported",
425: "{0,number,integer} connection(s) succesfully exported.",
426: new Object[] { new Integer(
427: i) }), "",
428: JOptionPane.INFORMATION_MESSAGE);
429:
430: } catch (Exception ex) {
431: JOptionPane
432: .showMessageDialog(
433: MainFrame.getMainInstance(),
434: I18n
435: .getFormattedString(
436: "messages.connectionsDialog.errorSavingConnections",
437: "Error saving connections:\n{0}",
438: new Object[] { ex
439: .getMessage() }));
440: ex.printStackTrace();
441: }
442:
443: }
444:
445: }//GEN-LAST:event_jButtonExportActionPerformed
446:
447: private void jButtonImportActionPerformed(
448: java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonImportActionPerformed
449:
450: JFileChooser jfc = new JFileChooser();
451: jfc.setMultiSelectionEnabled(false);
452: jfc.setFileFilter(new javax.swing.filechooser.FileFilter() {
453: public boolean accept(java.io.File file) {
454: String filename = file.getName().toLowerCase();
455: return (filename.endsWith(".xml") || file.isDirectory());
456: }
457:
458: public String getDescription() {
459: return "iReport connection/datasource definition (*.xml)";
460: }
461: });
462:
463: if (jfc.showOpenDialog(this ) == JFileChooser.APPROVE_OPTION) {
464:
465: Vector new_cons = loadConnections(jfc.getSelectedFile());
466: if (new_cons != null) {
467: DefaultTableModel dtm = (DefaultTableModel) jTableParameters
468: .getModel();
469: int i = 0;
470: for (i = 0; i < new_cons.size(); ++i) {
471: IReportConnection con = (IReportConnection) new_cons
472: .elementAt(i);
473: dtm
474: .addRow(new Object[] { con,
475: con.getDescription() });
476: MainFrame.getMainInstance().getConnections()
477: .addElement(con);
478: }
479: MainFrame.getMainInstance().saveiReportConfiguration();
480:
481: JOptionPane
482: .showMessageDialog(
483: MainFrame.getMainInstance(),
484: I18n
485: .getFormattedString(
486: "messages.connectionsDialog.connectionsImported",
487: "{0,number,integer} connection(s) succesfully imported.",
488: new Object[] { new Integer(
489: i) }), "",
490: JOptionPane.INFORMATION_MESSAGE);
491: }
492: }
493: }//GEN-LAST:event_jButtonImportActionPerformed
494:
495: private void jTableParametersMouseClicked(
496: java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTableParametersMouseClicked
497:
498: if (evt.getClickCount() == 2 && evt.getButton() == evt.BUTTON1) {
499: jButtonModifyParameterActionPerformed(null);
500: }
501:
502: }//GEN-LAST:event_jTableParametersMouseClicked
503:
504: private void jButtonDeleteParameterActionPerformed1(
505: java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonDeleteParameterActionPerformed1
506:
507: if (jTableParameters.getSelectedRowCount() > 0) {
508: IReportConnection irc = null;
509: try {
510: irc = (IReportConnection) jTableParameters.getModel()
511: .getValueAt(jTableParameters.getSelectedRow(),
512: 0);
513: MainFrame.getMainInstance().setActiveConnection(irc);
514: jTableParameters.getModel().setValueAt(
515: new Boolean(true),
516: jTableParameters.getSelectedRow(), 2);
517: for (int i = 0; i < jTableParameters.getRowCount(); ++i) {
518: if (i != jTableParameters.getSelectedRow()) {
519: jTableParameters.getModel().setValueAt(
520: new Boolean(false), i, 2);
521: }
522: }
523: } catch (Exception ex) {
524: return;
525: }
526: }
527: }//GEN-LAST:event_jButtonDeleteParameterActionPerformed1
528:
529: private void jButtonDeleteParameterActionPerformed(
530: java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonDeleteParameterActionPerformed
531: // Get the selected connection...
532: if (jTableParameters.getSelectedRowCount() > 0) {
533: while (jTableParameters.getSelectedRowCount() > 0) {
534: IReportConnection irc = null;
535: try {
536: irc = (IReportConnection) jTableParameters
537: .getModel().getValueAt(
538: jTableParameters.getSelectedRow(),
539: 0);
540: } catch (Exception ex) {
541: return;
542: }
543:
544: if (MainFrame.getMainInstance().getProperties().get(
545: "DefaultConnection") == irc) {
546: MainFrame.getMainInstance().getProperties().remove(
547: "DefaultConnection");
548: }
549:
550: MainFrame.getMainInstance().getConnections()
551: .removeElement(irc);
552: ((DefaultTableModel) jTableParameters.getModel())
553: .removeRow(jTableParameters.getSelectedRow());
554: jTableParameters.updateUI();
555: }
556:
557: MainFrame.getMainInstance().saveiReportConfiguration();
558: }
559:
560: }//GEN-LAST:event_jButtonDeleteParameterActionPerformed
561:
562: private void jButtonModifyParameterActionPerformed(
563: java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonModifyParameterActionPerformed
564: java.awt.Frame parent = Misc.frameFromComponent(this );
565: it.businesslogic.ireport.connection.gui.ConnectionDialog cd = new it.businesslogic.ireport.connection.gui.ConnectionDialog(
566: parent, false);
567:
568: // Take the selected connection from the table...
569: if (jTableParameters.getSelectedRow() < 0)
570: return;
571: IReportConnection irc = null;
572: try {
573: irc = (IReportConnection) jTableParameters.getModel()
574: .getValueAt(jTableParameters.getSelectedRow(), 0);
575: } catch (Exception ex) {
576: return;
577: }
578:
579: if (irc == null)
580: return;
581:
582: cd.setIReportConnection(irc);
583:
584: cd.setVisible(true);
585:
586: if (cd.getDialogResult() == JOptionPane.OK_OPTION) {
587: IReportConnection con = cd.getIReportConnection();
588: // Now we have an old and a new object.
589: // 1. Adjust table...
590: try {
591: jTableParameters.getModel().setValueAt(con,
592: jTableParameters.getSelectedRow(), 0);
593: jTableParameters.getModel().setValueAt(
594: con.getDescription(),
595: jTableParameters.getSelectedRow(), 1);
596: jTableParameters.getModel().setValueAt(
597: new Boolean(true),
598: jTableParameters.getSelectedRow(), 2);
599: } catch (Exception ex) {
600: return;
601: }
602:
603: for (int i = 0; i < jTableParameters.getRowCount(); ++i) {
604: if (i != jTableParameters.getSelectedRow()) {
605: jTableParameters.getModel().setValueAt(
606: new Boolean(false), i, 2);
607: }
608: }
609:
610: MainFrame.getMainInstance().getConnections().setElementAt(
611: con,
612: MainFrame.getMainInstance().getConnections()
613: .indexOf(irc));
614: MainFrame.getMainInstance().setActiveConnection(con);
615: MainFrame.getMainInstance().saveiReportConfiguration();
616: }
617: }//GEN-LAST:event_jButtonModifyParameterActionPerformed
618:
619: private void jButtonNewParameterActionPerformed(
620: java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonNewParameterActionPerformed
621: java.awt.Frame parent = Misc.frameFromComponent(this );
622:
623: //ConnectionDialog cd = new ConnectionDialog(parent,false);
624: it.businesslogic.ireport.connection.gui.ConnectionDialog cd = new it.businesslogic.ireport.connection.gui.ConnectionDialog(
625: this , true);
626:
627: cd.setVisible(true);
628:
629: if (cd.getDialogResult() == JOptionPane.OK_OPTION) {
630:
631: IReportConnection con = cd.getIReportConnection();
632: DefaultTableModel dtm = (DefaultTableModel) jTableParameters
633: .getModel();
634: dtm.addRow(new Object[] { con, con.getDescription(),
635: new Boolean(true) });
636:
637: for (int i = 0; i < jTableParameters.getRowCount(); ++i) {
638: if (i != dtm.getRowCount() - 1) {
639: jTableParameters.getModel().setValueAt(
640: new Boolean(false), i, 2);
641: }
642: }
643:
644: MainFrame.getMainInstance().getConnections()
645: .addElement(con);
646: MainFrame.getMainInstance().setActiveConnection(con);
647: MainFrame.getMainInstance().saveiReportConfiguration();
648: }
649:
650: }//GEN-LAST:event_jButtonNewParameterActionPerformed
651:
652: /** Closes the dialog */
653: private void closeDialog(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_closeDialog
654: setVisible(false);
655: }//GEN-LAST:event_closeDialog
656:
657: /**
658: * @param args the command line arguments
659: */
660: public static void main(String args[]) {
661: new ValuesDialog(new javax.swing.JFrame(), true)
662: .setVisible(true);
663: }
664:
665: public void updateConnections() {
666: /*
667: DefaultTableModel dtm = (DefaultTableModel)jTableParameters.getModel();
668: dtm.setRowCount(0);
669:
670: Enumeration enum = jReportFrame.getReport().getParameters().elements();
671: while (enum.hasMoreElements())
672: {
673: it.businesslogic.ireport.JRParameter parameter = (it.businesslogic.ireport.JRParameter)enum.nextElement();
674: Vector row = new Vector();
675: row.addElement( parameter);
676: row.addElement( parameter.getClassType());
677: row.addElement( parameter.isIsForPrompting()+"");
678:
679: dtm.addRow(row);
680: }
681: */
682: }
683:
684: // Variables declaration - do not modify//GEN-BEGIN:variables
685: private javax.swing.JButton jButtonDefault;
686: private javax.swing.JButton jButtonDeleteParameter;
687: private javax.swing.JButton jButtonExport;
688: private javax.swing.JButton jButtonImport;
689: private javax.swing.JButton jButtonModifyParameter;
690: private javax.swing.JButton jButtonNewParameter;
691: private javax.swing.JPanel jPanelButtons;
692: private javax.swing.JPanel jPanelParameters;
693: private javax.swing.JScrollPane jScrollPane1;
694: private javax.swing.JTable jTableParameters;
695:
696: // End of variables declaration//GEN-END:variables
697:
698: public void setVisible(boolean visible) {
699:
700: updateConnections();
701: super .setVisible(visible);
702: }
703:
704: /**
705: * Load a set of connections from a file. The connection are renamed if already present
706: * in the connection list. See getAvailableConnectionName() for details about the new name.
707: */
708: public Vector loadConnections(java.io.File xmlfile) {
709: Vector v = new Vector();
710: try {
711: DOMParser parser = new DOMParser();
712: java.io.FileInputStream fis = new java.io.FileInputStream(
713: xmlfile);
714: org.xml.sax.InputSource input_sss = new org.xml.sax.InputSource(
715: fis);
716: //input_sss.setSystemId(filename);
717: parser.parse(input_sss);
718:
719: Document document = parser.getDocument();
720: Node node = document.getDocumentElement();
721:
722: NodeList list_child = node.getChildNodes(); // The root is iReportConnections
723: for (int ck = 0; ck < list_child.getLength(); ck++) {
724: Node connectionNode = (Node) list_child.item(ck);
725: if (connectionNode.getNodeName() != null
726: && connectionNode.getNodeName().equals(
727: "iReportConnection")) {
728: // Take the CDATA...
729: String connectionName = "";
730: String connectionClass = "";
731: HashMap hm = new HashMap();
732: NamedNodeMap nnm = connectionNode.getAttributes();
733: if (nnm.getNamedItem("name") != null)
734: connectionName = nnm.getNamedItem("name")
735: .getNodeValue();
736: if (nnm.getNamedItem("connectionClass") != null)
737: connectionClass = nnm.getNamedItem(
738: "connectionClass").getNodeValue();
739:
740: // Get all connections parameters...
741: NodeList list_child2 = connectionNode
742: .getChildNodes();
743: for (int ck2 = 0; ck2 < list_child2.getLength(); ck2++) {
744: String parameterName = "";
745: Node child_child = (Node) list_child2.item(ck2);
746: if (child_child.getNodeType() == Node.ELEMENT_NODE
747: && child_child.getNodeName().equals(
748: "connectionParameter")) {
749:
750: NamedNodeMap nnm2 = child_child
751: .getAttributes();
752: if (nnm2.getNamedItem("name") != null)
753: parameterName = nnm2.getNamedItem(
754: "name").getNodeValue();
755: hm.put(parameterName, Report
756: .readPCDATA(child_child));
757: }
758: }
759:
760: // If the name exists, rename it as "name (2)"
761: connectionName = getAvailableConnectionName(connectionName);
762:
763: try {
764: IReportConnection con = (IReportConnection) Class
765: .forName(connectionClass).newInstance();
766: con.loadProperties(hm);
767: con.setName(connectionName);
768: v.addElement(con);
769: } catch (Exception ex) {
770:
771: JOptionPane
772: .showMessageDialog(
773: MainFrame.getMainInstance(),
774: I18n
775: .getFormattedString(
776: "messages.connectionsDialog.errorLoadingConnection",
777: "Error loading {0}",
778: new Object[] { connectionName }),
779: I18n.getString(
780: "message.title.error",
781: "Error"),
782: JOptionPane.ERROR_MESSAGE);
783: }
784: }
785: }
786: } catch (Exception ex) {
787: JOptionPane
788: .showMessageDialog(
789: MainFrame.getMainInstance(),
790: I18n
791: .getFormattedString(
792: "messages.connectionsDialog.errorLoadingConnections",
793: "Error loading connections.\n{0}",
794: new Object[] { ex
795: .getMessage() }),
796: I18n.getString("message.title.error",
797: "Error"), JOptionPane.ERROR_MESSAGE);
798: ex.printStackTrace();
799: }
800:
801: return v;
802: }
803:
804: // If the name exists, rename it as "name (2)"
805: /**
806: * This method take a proposed connection name. Check for duplicates names. If the
807: * proposed name is already present, the name "proposed (2)" is checked and so
808: * on up to when a valid name is found....
809: */
810: public static String getAvailableConnectionName(
811: String proposedConnectionName) {
812: return getAvailableConnectionName(proposedConnectionName, 0);
813: }
814:
815: private static String getAvailableConnectionName(
816: String proposedConnectionName, int testNumber) {
817: String name = proposedConnectionName;
818: if (testNumber != 0)
819: name += " (" + testNumber + ")";
820:
821: Enumeration con_enum = MainFrame.getMainInstance()
822: .getConnections().elements();
823: while (con_enum.hasMoreElements()) {
824: // toString for an iReportConnection is the getName method...
825: String conName = "" + con_enum.nextElement();
826: if (name.equals(conName)) {
827: return getAvailableConnectionName(
828: proposedConnectionName, testNumber + 1);
829: }
830: }
831: return name;
832: }
833:
834: public void applyI18n() {
835: // Start autogenerated code ----------------------
836: jButtonDefault.setText(I18n.getString(
837: "connectionsDialog.buttonDefault", "Set as default"));
838: jButtonDeleteParameter.setText(I18n.getString(
839: "connectionsDialog.buttonDeleteParameter", "Delete"));
840: jButtonExport.setText(I18n.getString(
841: "connectionsDialog.buttonExport", "Export..."));
842: jButtonImport.setText(I18n.getString(
843: "connectionsDialog.buttonImport", "Import..."));
844: jButtonModifyParameter.setText(I18n.getString(
845: "connectionsDialog.buttonModifyParameter", "Modify"));
846: jButtonNewParameter.setText(I18n.getString(
847: "connectionsDialog.buttonNewParameter", "New"));
848: // End autogenerated code ----------------------
849: setTitle(I18n.getString("connectionsDialog.title",
850: "Connections / Datasources"));
851: jTableParameters.getColumnModel().getColumn(0).setHeaderValue(
852: I18n.getString("connectionsDialog.tablecolumn.name",
853: "Name"));
854: jTableParameters.getColumnModel().getColumn(1).setHeaderValue(
855: I18n.getString(
856: "connectionsDialog.tablecolumn.datasourceType",
857: "Datasource type"));
858: jTableParameters.getColumnModel().getColumn(2).setHeaderValue(
859: I18n.getString("connectionsDialog.tablecolumn.default",
860: "Default"));
861: }
862: }
|