001: /*
002: * ConnectionDialog.java
003: *
004: * Created on March 26, 2007, 3:52 PM
005: */
006:
007: package it.businesslogic.ireport.connection.gui;
008:
009: import it.businesslogic.ireport.IReportConnection;
010: import it.businesslogic.ireport.IReportConnectionEditor;
011: import it.businesslogic.ireport.gui.MainFrame;
012: import it.businesslogic.ireport.gui.sheet.Tag;
013: import it.businesslogic.ireport.util.I18n;
014: import it.businesslogic.ireport.util.Misc;
015: import java.util.List;
016: import javax.swing.DefaultListModel;
017: import javax.swing.JOptionPane;
018: import javax.swing.SwingUtilities;
019:
020: /**
021: *
022: * @author gtoffoli
023: */
024: public class ConnectionDialog extends javax.swing.JDialog {
025:
026: protected IReportConnection iReportConnection = null;
027: protected IReportConnectionEditor iReportConnectionEditor = null;
028:
029: private int dialogResult = JOptionPane.CANCEL_OPTION;
030: private int step = 0;
031:
032: /**
033: * Creates new form ConnectionDialog
034: */
035: public ConnectionDialog(java.awt.Frame parent,
036: boolean isNewDatasource) {
037: super (parent, true);
038: initAll(isNewDatasource);
039: }
040:
041: /**
042: * Creates new form ConnectionDialog
043: */
044: public ConnectionDialog(java.awt.Dialog parent,
045: boolean isNewDatasource) {
046: super (parent, true);
047: initAll(isNewDatasource);
048: }
049:
050: public void initAll(boolean isNewDatasource) {
051: initComponents();
052: if (isNewDatasource) {
053: this .setStep(0);
054: DefaultListModel dlm = new DefaultListModel();
055: List types = MainFrame.getMainInstance()
056: .getConnectionImplementations();
057: for (int i = 0; i < types.size(); ++i) {
058: String cName = "" + types.get(i);
059:
060: try {
061: IReportConnection c = (IReportConnection) Class
062: .forName(
063: cName,
064: true,
065: MainFrame.getMainInstance()
066: .getReportClassLoader())
067: .newInstance();
068: String sName = c.getDescription();
069: //(String)c.getMethod("getConnectionTypeName",new Class[]{}).invoke(null, new Object[]{});
070: dlm.addElement(new Tag(c, sName));
071:
072: } catch (Throwable tw) {
073: tw.printStackTrace();
074: }
075: }
076: jList1.setModel(dlm);
077: this .jButtonSave.setEnabled(false);
078: if (jList1.getModel().getSize() > 0) {
079: jList1.setSelectedIndex(0);
080: }
081: }
082:
083: javax.swing.KeyStroke escape = javax.swing.KeyStroke
084: .getKeyStroke(java.awt.event.KeyEvent.VK_ESCAPE, 0,
085: false);
086: javax.swing.Action escapeAction = new javax.swing.AbstractAction() {
087: public void actionPerformed(java.awt.event.ActionEvent e) {
088: jButtonCancelActionPerformed(e);
089: }
090: };
091:
092: applyI18n();
093:
094: getRootPane().getInputMap(
095: javax.swing.JComponent.WHEN_IN_FOCUSED_WINDOW).put(
096: escape, "ESCAPE");
097: getRootPane().getActionMap().put("ESCAPE", escapeAction);
098:
099: //to make the default button ...
100: this .getRootPane().setDefaultButton(this .jButtonSave);
101:
102: this .pack();
103: Misc.centerFrame(this );
104: }
105:
106: public void setIReportConnection(IReportConnection conn) {
107: setIReportConnection(conn, false);
108: }
109:
110: private void setIReportConnection(IReportConnection conn,
111: boolean isNew) {
112: if (conn == null)
113: return;
114: this .iReportConnection = cloneConnection(conn);
115: this .jTextFieldName.setText(iReportConnection.getName());
116: this .jLabelTitle.setText(conn.getDescription());
117:
118: jButtonSave.setText(I18n.getString("connectionDialog.buttonOK",
119: "Save"));
120: jButtonTest.setEnabled(true);
121: this .setStep(1);
122: jPanelRoot.removeAll();
123:
124: iReportConnectionEditor = iReportConnection
125: .getIReportConnectionEditor();
126: if (!isNew) {
127: iReportConnectionEditor
128: .setIReportConnection(iReportConnection);
129: }
130:
131: jPanelCustomComponent
132: .add((java.awt.Component) iReportConnectionEditor);
133: jPanelRoot.add(jPanelModifyDatasource);
134: jPanelRoot.updateUI();
135: jTextFieldName.requestFocusInWindow();
136: }
137:
138: public IReportConnection getIReportConnection() {
139: return this .iReportConnection;
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: jPanelModifyDatasource = new javax.swing.JPanel();
152: jLabelName = new javax.swing.JLabel();
153: jTextFieldName = new javax.swing.JTextField();
154: jSeparator3 = new javax.swing.JSeparator();
155: jPanelCustomComponent = new javax.swing.JPanel();
156: jLabelTitle = new javax.swing.JLabel();
157: jSeparator1 = new javax.swing.JSeparator();
158: jPanelRoot = new javax.swing.JPanel();
159: jPanelNewDatasource = new javax.swing.JPanel();
160: jLabel2 = new javax.swing.JLabel();
161: jScrollPane1 = new javax.swing.JScrollPane();
162: jList1 = new javax.swing.JList();
163: jSeparator2 = new javax.swing.JSeparator();
164: jPanelButtons = new javax.swing.JPanel();
165: jButtonTest = new javax.swing.JButton();
166: jButtonSave = new javax.swing.JButton();
167: jButtonCancel = new javax.swing.JButton();
168:
169: jPanelModifyDatasource.setLayout(new java.awt.GridBagLayout());
170:
171: jLabelName.setText("Name");
172: gridBagConstraints = new java.awt.GridBagConstraints();
173: gridBagConstraints.gridx = 0;
174: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
175: gridBagConstraints.insets = new java.awt.Insets(4, 4, 4, 4);
176: jPanelModifyDatasource.add(jLabelName, gridBagConstraints);
177:
178: gridBagConstraints = new java.awt.GridBagConstraints();
179: gridBagConstraints.gridx = 1;
180: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
181: gridBagConstraints.weightx = 1.0;
182: gridBagConstraints.insets = new java.awt.Insets(4, 4, 4, 4);
183: jPanelModifyDatasource.add(jTextFieldName, gridBagConstraints);
184:
185: jSeparator3.setMinimumSize(new java.awt.Dimension(0, 2));
186: gridBagConstraints = new java.awt.GridBagConstraints();
187: gridBagConstraints.gridx = 0;
188: gridBagConstraints.gridwidth = 2;
189: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
190: jPanelModifyDatasource.add(jSeparator3, gridBagConstraints);
191:
192: jPanelCustomComponent.setLayout(new java.awt.BorderLayout());
193:
194: gridBagConstraints = new java.awt.GridBagConstraints();
195: gridBagConstraints.gridx = 0;
196: gridBagConstraints.gridwidth = 2;
197: gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
198: gridBagConstraints.weightx = 1.0;
199: gridBagConstraints.weighty = 1.0;
200: gridBagConstraints.insets = new java.awt.Insets(4, 4, 4, 4);
201: jPanelModifyDatasource.add(jPanelCustomComponent,
202: gridBagConstraints);
203:
204: getContentPane().setLayout(new java.awt.GridBagLayout());
205:
206: setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
207: jLabelTitle.setBackground(new java.awt.Color(255, 255, 255));
208: jLabelTitle.setFont(new java.awt.Font("Dialog", 1, 12));
209: jLabelTitle
210: .setIcon(new javax.swing.ImageIcon(
211: getClass()
212: .getResource(
213: "/it/businesslogic/ireport/icons/datasource/datasource.png")));
214: jLabelTitle.setText("<html>Datasource");
215: jLabelTitle
216: .setVerticalAlignment(javax.swing.SwingConstants.BOTTOM);
217: jLabelTitle.setOpaque(true);
218: jLabelTitle
219: .setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
220: gridBagConstraints = new java.awt.GridBagConstraints();
221: gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
222: getContentPane().add(jLabelTitle, gridBagConstraints);
223:
224: jSeparator1.setMinimumSize(new java.awt.Dimension(0, 2));
225: gridBagConstraints = new java.awt.GridBagConstraints();
226: gridBagConstraints.gridx = 0;
227: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
228: getContentPane().add(jSeparator1, gridBagConstraints);
229:
230: jPanelRoot.setLayout(new java.awt.BorderLayout());
231:
232: jPanelRoot.setPreferredSize(new java.awt.Dimension(440, 400));
233: jPanelNewDatasource.setLayout(new java.awt.GridBagLayout());
234:
235: jLabel2.setText("Select the datasource type");
236: gridBagConstraints = new java.awt.GridBagConstraints();
237: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
238: gridBagConstraints.weightx = 1.0;
239: gridBagConstraints.insets = new java.awt.Insets(4, 4, 4, 4);
240: jPanelNewDatasource.add(jLabel2, gridBagConstraints);
241:
242: jList1
243: .setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
244: jList1
245: .addListSelectionListener(new javax.swing.event.ListSelectionListener() {
246: public void valueChanged(
247: javax.swing.event.ListSelectionEvent evt) {
248: jList1ValueChanged(evt);
249: }
250: });
251: jList1.addMouseListener(new java.awt.event.MouseAdapter() {
252: public void mouseClicked(java.awt.event.MouseEvent evt) {
253: jList1MouseClicked(evt);
254: }
255: });
256:
257: jScrollPane1.setViewportView(jList1);
258:
259: gridBagConstraints = new java.awt.GridBagConstraints();
260: gridBagConstraints.gridx = 0;
261: gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
262: gridBagConstraints.weightx = 1.0;
263: gridBagConstraints.weighty = 1.0;
264: gridBagConstraints.insets = new java.awt.Insets(0, 4, 4, 4);
265: jPanelNewDatasource.add(jScrollPane1, gridBagConstraints);
266:
267: jPanelRoot.add(jPanelNewDatasource,
268: java.awt.BorderLayout.CENTER);
269:
270: gridBagConstraints = new java.awt.GridBagConstraints();
271: gridBagConstraints.gridx = 0;
272: gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
273: gridBagConstraints.weightx = 1.0;
274: gridBagConstraints.weighty = 1.0;
275: getContentPane().add(jPanelRoot, gridBagConstraints);
276:
277: jSeparator2.setMinimumSize(new java.awt.Dimension(0, 2));
278: gridBagConstraints = new java.awt.GridBagConstraints();
279: gridBagConstraints.gridx = 0;
280: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
281: getContentPane().add(jSeparator2, gridBagConstraints);
282:
283: jPanelButtons.setLayout(new java.awt.GridBagLayout());
284:
285: jPanelButtons.setMinimumSize(new java.awt.Dimension(50, 30));
286: jPanelButtons.setPreferredSize(new java.awt.Dimension(50, 30));
287: jButtonTest.setText("Test");
288: jButtonTest.setEnabled(false);
289: jButtonTest
290: .addActionListener(new java.awt.event.ActionListener() {
291: public void actionPerformed(
292: java.awt.event.ActionEvent evt) {
293: jButtonTestActionPerformed(evt);
294: }
295: });
296:
297: gridBagConstraints = new java.awt.GridBagConstraints();
298: gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
299: gridBagConstraints.weightx = 1.0;
300: gridBagConstraints.insets = new java.awt.Insets(0, 4, 0, 4);
301: jPanelButtons.add(jButtonTest, gridBagConstraints);
302:
303: jButtonSave.setText("Next >");
304: jButtonSave
305: .addActionListener(new java.awt.event.ActionListener() {
306: public void actionPerformed(
307: java.awt.event.ActionEvent evt) {
308: jButtonSaveActionPerformed(evt);
309: }
310: });
311:
312: gridBagConstraints = new java.awt.GridBagConstraints();
313: gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
314: gridBagConstraints.insets = new java.awt.Insets(0, 0, 0, 4);
315: jPanelButtons.add(jButtonSave, gridBagConstraints);
316:
317: jButtonCancel.setText("Cancel");
318: jButtonCancel
319: .addActionListener(new java.awt.event.ActionListener() {
320: public void actionPerformed(
321: java.awt.event.ActionEvent evt) {
322: jButtonCancelActionPerformed(evt);
323: }
324: });
325:
326: gridBagConstraints = new java.awt.GridBagConstraints();
327: gridBagConstraints.insets = new java.awt.Insets(0, 0, 0, 4);
328: jPanelButtons.add(jButtonCancel, gridBagConstraints);
329:
330: gridBagConstraints = new java.awt.GridBagConstraints();
331: gridBagConstraints.gridx = 0;
332: gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
333: gridBagConstraints.weightx = 1.0;
334: getContentPane().add(jPanelButtons, gridBagConstraints);
335:
336: pack();
337: }// </editor-fold>//GEN-END:initComponents
338:
339: private void jButtonTestActionPerformed(
340: java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonTestActionPerformed
341:
342: IReportConnection tmpIReportConnection = this .iReportConnectionEditor
343: .getIReportConnection();
344: if (tmpIReportConnection != null) {
345: try {
346: tmpIReportConnection.test();
347: } catch (Exception ex) {
348: ex.printStackTrace();
349: }
350: }
351: }//GEN-LAST:event_jButtonTestActionPerformed
352:
353: private void jList1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jList1MouseClicked
354:
355: if (SwingUtilities.isLeftMouseButton(evt)
356: && evt.getClickCount() == 2) {
357: jButtonSaveActionPerformed(null);
358: }
359:
360: }//GEN-LAST:event_jList1MouseClicked
361:
362: private void jButtonSaveActionPerformed(
363: java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonSaveActionPerformed
364:
365: if (getStep() == 0) {
366: if (jList1.getSelectedValue() == null)
367: return;
368: IReportConnection ic = (IReportConnection) (((Tag) jList1
369: .getSelectedValue()).getValue());
370: this .setIReportConnection(ic, true);
371: return;
372: } else {
373: if (this .jTextFieldName.getText().trim().length() == 0) {
374: javax.swing.JOptionPane
375: .showMessageDialog(
376: this ,
377: I18n
378: .getString(
379: "messages.connectionDialog.invalidName",
380: "Please insert a valid connection name!"),
381: I18n
382: .getString(
383: "messages.connectionDialog.invalidNameCaption",
384: "Invalid connection name!"),
385: javax.swing.JOptionPane.WARNING_MESSAGE);
386: return;
387: }
388: IReportConnection tmpIReportConnection = this .iReportConnectionEditor
389: .getIReportConnection();
390: if (tmpIReportConnection == null)
391: return;
392: tmpIReportConnection.setName(jTextFieldName.getText());
393: this .iReportConnection = tmpIReportConnection;
394: this .setDialogResult(JOptionPane.OK_OPTION);
395: this .setVisible(false);
396: this .dispose();
397: }
398:
399: }//GEN-LAST:event_jButtonSaveActionPerformed
400:
401: private void jList1ValueChanged(
402: javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_jList1ValueChanged
403:
404: jButtonSave.setEnabled(jList1.getSelectedIndex() >= 0);
405: }//GEN-LAST:event_jList1ValueChanged
406:
407: private void jButtonCancelActionPerformed(
408: java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonCancelActionPerformed
409:
410: this .setVisible(false);
411: this .dispose();
412:
413: }//GEN-LAST:event_jButtonCancelActionPerformed
414:
415: /**
416: * @param args the command line arguments
417: */
418: public static void main(String args[]) {
419: java.awt.EventQueue.invokeLater(new Runnable() {
420: public void run() {
421: new ConnectionDialog(new javax.swing.JFrame(), true)
422: .setVisible(true);
423: }
424: });
425: }
426:
427: public void applyI18n() {
428: jButtonSave.setText(I18n.getString("next", "Next >"));
429: jButtonCancel.setText(I18n.getString(
430: "connectionDialog.buttonCancel", "Cancel"));
431: jButtonTest.setText(I18n.getString(
432: "connectionDialog.buttonOK1", "Test"));
433: jLabelTitle.setText(I18n
434: .getString("connectionDialog.newDatasourceTitle",
435: "New datasource"));
436:
437: jLabelName.setText(I18n.getString("connectionDialog.label1",
438: "Name"));
439: setTitle(I18n.getString("connectionDialog.title",
440: "Connections properties"));
441:
442: jButtonCancel
443: .setMnemonic(I18n.getString(
444: "connectionDialog.buttonCancelMnemonic", "c")
445: .charAt(0));
446: jButtonSave.setMnemonic(I18n.getString(
447: "connectionDialog.buttonOKMnemonic", "o").charAt(0));
448: jButtonTest.setMnemonic(I18n.getString(
449: "connectionDialog.buttonOK1Mnemonic", "t").charAt(0));
450:
451: }
452:
453: public int getStep() {
454: return step;
455: }
456:
457: public void setStep(int step) {
458: this .step = step;
459: }
460:
461: public int getDialogResult() {
462: return dialogResult;
463: }
464:
465: public void setDialogResult(int dialogResult) {
466: this .dialogResult = dialogResult;
467: }
468:
469: // Variables declaration - do not modify//GEN-BEGIN:variables
470: private javax.swing.JButton jButtonCancel;
471: private javax.swing.JButton jButtonSave;
472: private javax.swing.JButton jButtonTest;
473: private javax.swing.JLabel jLabel2;
474: private javax.swing.JLabel jLabelName;
475: private javax.swing.JLabel jLabelTitle;
476: private javax.swing.JList jList1;
477: private javax.swing.JPanel jPanelButtons;
478: private javax.swing.JPanel jPanelCustomComponent;
479: private javax.swing.JPanel jPanelModifyDatasource;
480: private javax.swing.JPanel jPanelNewDatasource;
481: private javax.swing.JPanel jPanelRoot;
482: private javax.swing.JScrollPane jScrollPane1;
483: private javax.swing.JSeparator jSeparator1;
484: private javax.swing.JSeparator jSeparator2;
485: private javax.swing.JSeparator jSeparator3;
486: private javax.swing.JTextField jTextFieldName;
487:
488: // End of variables declaration//GEN-END:variables
489:
490: /**
491: * This method close the connection instancing a new connection of the same tipe and loading/storing the
492: * properties... newConn.loadProperties( conn.getProperties() );
493: *
494: * We assume that the specific connection has a null constructor.
495: *
496: */
497: public IReportConnection cloneConnection(IReportConnection conn) {
498: if (conn == null)
499: return null;
500: Class clazz = conn.getClass();
501:
502: try {
503: IReportConnection newConn = (IReportConnection) clazz
504: .newInstance();
505: newConn.loadProperties(conn.getProperties());
506: newConn.setName(conn.getName());
507: return newConn;
508: } catch (Throwable t) {
509: }
510:
511: return null;
512: }
513: }
|