Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: org/bdgp/apps/dagedit/dataadapter/PostgresAdapterGUI.java


1   package org.bdgp.apps.dagedit.dataadapter;
2   
3   import javax.swing.*;
4   import java.awt.*;
5   import java.awt.event.*;
6   import java.util.Properties;
7   import java.sql.*;
8   import java.text.SimpleDateFormat;
9   import org.bdgp.io.*;
10  import org.bdgp.util.*;
11  import org.bdgp.swing.*;
12  import org.bdgp.swing.widget.DataAdapterChooser;
13  import org.bdgp.apps.dagedit.datamodel.*;
14  import org.bdgp.apps.dagedit.gui.Controller;
15  
16  public class PostgresAdapterGUI extends AbstractIntDataAdapUI {
17  
18      DataAdapter driver;
19      JLabel urlLabel;
20      JLabel driverLabel;
21      JLabel usernameLabel;
22      JLabel passwordLabel;
23      JLabel commentLabel;
24  
25      JTextField urlField;
26      JTextField driverField;
27      JTextField usernameField;
28      JCheckBox repopulateCheckbox;
29      JCheckBox forceHistoryCheckbox;
30      JPasswordField passwordField;
31   
32      JTextArea commentField;
33  
34      JButton advancedButton;
35  
36      JButton okButton;
37      JButton cancelButton;
38  
39      IOOperation op;
40  
41      String url;
42      String jdbcDriver;
43      String username;
44      String password;
45      String comment;
46      boolean repopulate;
47      boolean forceHistory;
48  
49      Controller controller;
50  
51      JDialog advancedDialog;
52  
53      protected JTextField pwUrlField = new JTextField();
54      protected JTextField pwDriverField = new JTextField();
55      protected JTextField pwUsernameField = new JTextField();
56      protected JPasswordField pwPasswordField = new JPasswordField();
57      protected JPasswordField pwNewPasswordField = new JPasswordField();
58      protected JPasswordField pwNewPasswordVerifyField = new JPasswordField();
59      protected JButton pwChangeButton = new JButton("Change password");
60      protected Statement stmt;
61  
62      protected JLabel pwUrlLabel = new JLabel("Database url");
63      protected JLabel pwDriverLabel = new JLabel("Database driver");
64      protected JLabel pwUsernameLabel = new JLabel("User name");
65      protected JLabel pwPasswordLabel = new JLabel("Password");
66      protected JLabel pwNewPasswordLabel = new JLabel("New Password");
67      protected JLabel pwNewPasswordVerifyLabel = new JLabel("Verify New Password");
68  
69  
70      protected JPanel getAdvancedPanel(JPanel panel) {
71    ActionListener listener = new ActionListener() {
72      public void actionPerformed(ActionEvent event) {
73          changePassword();
74      }
75        };
76    pwChangeButton.addActionListener(listener);
77    panel.removeAll();
78    panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
79  
80    JPanel buttonBox = new JPanel();
81    buttonBox.setLayout(new BoxLayout(buttonBox, BoxLayout.X_AXIS));
82    buttonBox.setAlignmentX(LEFT_ALIGNMENT);
83  
84    pwChangeButton.setAlignmentX(LEFT_ALIGNMENT);
85  
86    pwUrlField.setFont(controller.getDefaultFont());
87    pwDriverField.setFont(controller.getDefaultFont());
88    pwUsernameField.setFont(controller.getDefaultFont());
89    pwPasswordField.setFont(controller.getDefaultFont());
90    pwNewPasswordField.setFont(controller.getDefaultFont());
91    pwNewPasswordVerifyField.setFont(controller.getDefaultFont());
92    pwChangeButton.setFont(controller.getDefaultFont());
93    pwUrlLabel.setFont(controller.getDefaultFont());
94    pwDriverLabel.setFont(controller.getDefaultFont());
95    pwUsernameLabel.setFont(controller.getDefaultFont());
96    pwPasswordLabel.setFont(controller.getDefaultFont());
97    pwNewPasswordLabel.setFont(controller.getDefaultFont());
98    pwNewPasswordVerifyLabel.setFont(controller.getDefaultFont());
99  
100   pwUrlField.setText(urlField.getText());
101   pwDriverField.setText(driverField.getText());
102   pwUsernameField.setText(usernameField.getText());
103   pwPasswordField.setText("");
104   pwNewPasswordField.setText("");
105   pwNewPasswordVerifyField.setText("");
106 
107   buttonBox.add(Box.createHorizontalGlue());
108   buttonBox.add(pwChangeButton);
109   buttonBox.add(Box.createHorizontalGlue());
110 
111   int dist = 10;
112   int width = 300;
113   int height = controller.getDefaultFont().getSize()+6;
114   panel.add(Box.createVerticalStrut(10));
115   panel.add(getLabeledBox(pwUrlLabel, pwUrlField, dist, width, height));
116   panel.add(getLabeledBox(pwDriverLabel, pwDriverField, dist, width, height));
117   panel.add(getLabeledBox(pwUsernameLabel, pwUsernameField, dist, width, height));
118   panel.add(getLabeledBox(pwPasswordLabel, pwPasswordField, dist, width, height));
119   panel.add(Box.createVerticalStrut(10));
120   panel.add(getLabeledBox(pwNewPasswordLabel, pwNewPasswordField, dist, width, height));
121   panel.add(getLabeledBox(pwNewPasswordVerifyLabel, pwNewPasswordVerifyField, dist, width, height));
122   panel.add(Box.createVerticalStrut(10));
123   panel.add(buttonBox);
124   panel.add(Box.createVerticalStrut(10));
125 
126   return panel;
127     }
128 
129     private JPanel getLabeledBox(JLabel label, JComponent component, int minDistance, int width, int height) {
130   label.setAlignmentX(LEFT_ALIGNMENT);
131   component.setAlignmentX(LEFT_ALIGNMENT);
132 
133   component.setPreferredSize(new Dimension(width, height));
134   component.setMaximumSize(new Dimension(width, 10000000));
135   JPanel out = new JPanel();
136   out.setLayout(new BoxLayout(out, BoxLayout.X_AXIS));
137   out.setAlignmentX(LEFT_ALIGNMENT);
138   out.add(Box.createHorizontalStrut(10));
139   out.add(label);
140   out.add(Box.createHorizontalStrut(minDistance));
141   out.add(Box.createHorizontalGlue());
142   out.add(component);
143   out.add(Box.createHorizontalStrut(10));
144   return out;
145     }
146 
147     protected void changePassword() {
148   String password = new String(pwPasswordField.getPassword());
149   String newPassword = new String(pwNewPasswordField.getPassword());
150   String newPasswordVerify = new String(pwNewPasswordVerifyField.
151                 getPassword());
152   if (!newPassword.equals(newPasswordVerify)) {
153       JOptionPane.showMessageDialog(null, "New password and verify password do not match.");
154       return;
155   }
156   try {
157       ((PostgresDataAdapter) driver).
158     init(pwDriverField.getText(),
159          pwUrlField.getText(),
160          pwUsernameField.getText(),
161          password);
162       ((PostgresDataAdapter) driver).
163     changePassword(newPassword);
164       JOptionPane.showMessageDialog(null, "Password successfully changed");
165       if (advancedDialog != null)
166     advancedDialog.dispose();
167       else
168     controllingObject.cancel();
169   } catch (Exception e) {
170       e.printStackTrace();
171       JOptionPane.showMessageDialog(null, "Could not change password because of server error: "+e.getMessage());
172       return;
173   }
174     }
175 
176 
177     public PostgresAdapterGUI(IOOperation op) {
178   this.op = op;
179   setPreferredSize(new Dimension(450,300));
180   //setMinimumSize(new Dimension(400,300));
181   urlField = new JTextField(20);
182   driverField = new JTextField(20);
183   usernameField = new JTextField(20);
184   passwordField = new JPasswordField(20);
185   commentField = new JTextArea(4, 35);
186   okButton = new JButton("Ok");
187   cancelButton = new JButton("Cancel");
188   advancedButton = new JButton("Change Password");
189 
190         this.controller = Controller.getController();
191 
192 
193   repopulateCheckbox = new JCheckBox("Repopulate database");
194   forceHistoryCheckbox = new JCheckBox("Force history");
195 
196   commentField.setLineWrap(true);
197   commentField.setWrapStyleWord(true);
198   
199   urlLabel = new JLabel("Database URL");
200   driverLabel = new JLabel("Database Driver");
201 
202   usernameLabel = new JLabel("Username");
203   passwordLabel = new JLabel("Password");
204   commentLabel = new JLabel("Comment");
205 
206   urlField.setAlignmentX(LEFT_ALIGNMENT);
207   driverField.setAlignmentX(LEFT_ALIGNMENT);
208   usernameField.setAlignmentX(LEFT_ALIGNMENT);
209   passwordField.setAlignmentX(LEFT_ALIGNMENT);
210   commentField.setAlignmentX(LEFT_ALIGNMENT);
211   okButton.setAlignmentX(LEFT_ALIGNMENT);
212   cancelButton.setAlignmentX(LEFT_ALIGNMENT);
213   advancedButton.setAlignmentX(LEFT_ALIGNMENT);
214   repopulateCheckbox.setAlignmentX(LEFT_ALIGNMENT);
215   forceHistoryCheckbox.setAlignmentX(LEFT_ALIGNMENT);
216   commentField.setAlignmentX(LEFT_ALIGNMENT);
217   urlLabel.setAlignmentX(LEFT_ALIGNMENT);
218   driverLabel.setAlignmentX(LEFT_ALIGNMENT);
219   usernameLabel.setAlignmentX(LEFT_ALIGNMENT);
220   passwordLabel.setAlignmentX(LEFT_ALIGNMENT);
221   commentLabel.setAlignmentX(LEFT_ALIGNMENT);
222 
223   JPanel driverLabelBox = new JPanel();
224   driverLabelBox.setLayout(new BoxLayout(driverLabelBox,
225                  BoxLayout.X_AXIS));
226   driverLabelBox.setAlignmentX(LEFT_ALIGNMENT);
227   driverLabelBox.add(driverLabel);
228   driverLabelBox.add(Box.createHorizontalGlue());
229 
230   JPanel urlLabelBox = new JPanel();
231   urlLabelBox.setLayout(new BoxLayout(urlLabelBox,
232               BoxLayout.X_AXIS));
233   urlLabelBox.setAlignmentX(LEFT_ALIGNMENT);
234   urlLabelBox.add(urlLabel);
235   urlLabelBox.add(Box.createHorizontalGlue());
236 
237   JPanel usernameLabelBox = new JPanel();
238   usernameLabelBox.setLayout(new BoxLayout(usernameLabelBox,
239              BoxLayout.X_AXIS));
240   usernameLabelBox.setAlignmentX(LEFT_ALIGNMENT);
241   usernameLabelBox.add(usernameLabel);
242   usernameLabelBox.add(Box.createHorizontalGlue());
243 
244   JPanel passwordLabelBox = new JPanel();
245   passwordLabelBox.setLayout(new BoxLayout(passwordLabelBox,
246              BoxLayout.X_AXIS));
247   passwordLabelBox.setAlignmentX(LEFT_ALIGNMENT);
248   passwordLabelBox.add(passwordLabel);
249   passwordLabelBox.add(Box.createHorizontalGlue());
250 
251   JPanel commentLabelBox = new JPanel();
252   commentLabelBox.setLayout(new BoxLayout(commentLabelBox,
253             BoxLayout.X_AXIS));
254   commentLabelBox.setAlignmentX(LEFT_ALIGNMENT);
255   commentLabelBox.add(commentLabel);
256   commentLabelBox.add(Box.createHorizontalGlue());
257 
258   JPanel advancedBox = new JPanel();
259   advancedBox.setLayout(new BoxLayout(advancedBox,
260               BoxLayout.X_AXIS));
261   advancedBox.setAlignmentX(LEFT_ALIGNMENT);
262 
263   if (op.equals(DEDataAdapterI.CONFIGURE)) {
264       JPanel panel = getAdvancedPanel(this);
265       validate();
266       return;
267   }
268 
269   advancedBox.add(Box.createHorizontalGlue());
270   advancedBox.add(advancedButton);
271   advancedBox.add(Box.createHorizontalGlue());
272 
273         setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
274   add(driverLabelBox);
275   add(driverField);
276   add(Box.createVerticalStrut(10));
277   add(urlLabelBox);
278   add(urlField);
279   add(Box.createVerticalStrut(10));
280   add(usernameLabelBox);
281   add(usernameField);
282   add(Box.createVerticalStrut(10));
283   add(passwordLabelBox);
284   add(passwordField);
285   if (op == DEDataAdapterI.WRITE_TERMS) {
286       JScrollPane pane = new
287     JScrollPane(commentField,
288           JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
289           JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
290       pane.setAlignmentX(LEFT_ALIGNMENT);
291       add(Box.createVerticalStrut(20));
292       add(commentLabelBox);
293       add(pane);
294       
295       JPanel repopulatePanel = new JPanel();
296       repopulatePanel.setAlignmentX(LEFT_ALIGNMENT);
297       repopulatePanel.setLayout(new BoxLayout(repopulatePanel,
298                 BoxLayout.X_AXIS));
299       
300       repopulatePanel.add(Box.createHorizontalGlue());
301       repopulatePanel.add(repopulateCheckbox);
302       repopulatePanel.add(Box.createHorizontalGlue());
303       repopulatePanel.add(Box.createHorizontalStrut(10));
304       repopulatePanel.add(Box.createHorizontalGlue());
305       repopulatePanel.add(forceHistoryCheckbox);
306       repopulatePanel.add(Box.createHorizontalGlue());
307 
308       add(repopulatePanel);
309   }
310   add(Box.createVerticalGlue());
311   add(Box.createVerticalStrut(10));
312   JPanel buttonBox = new JPanel();
313   buttonBox.setLayout(new BoxLayout(buttonBox, BoxLayout.X_AXIS));
314   buttonBox.setAlignmentX(LEFT_ALIGNMENT);
315   buttonBox.add(Box.createHorizontalGlue());
316   buttonBox.add(okButton);
317   buttonBox.add(Box.createHorizontalStrut(10));
318   buttonBox.add(cancelButton);
319   buttonBox.add(Box.createHorizontalGlue());
320   add(buttonBox);
321   add(Box.createVerticalStrut(10));
322   add(advancedBox);
323   add(Box.createVerticalGlue());
324   attachListeners();
325   loadDefaults();
326     }
327 
328     private class ErrorUI extends AbstractIntDataAdapUI {
329   JButton retryButton;
330   JLabel headerLabel;
331   JTextArea messageField;
332   JLabel extraLabel;
333   JScrollPane scrollPane;
334 
335   public ErrorUI(String errorType, String message, String extras) {
336       retryButton = new JButton("Start over");
337 
338       retryButton.addActionListener(new ActionListener() {
339         public void actionPerformed(ActionEvent e) {
340       retry();
341         }
342     });
343       setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
344       headerLabel = new JLabel(errorType);
345       messageField = new JTextArea(5, 30);
346       messageField.setWrapStyleWord(true);
347       messageField.setLineWrap(true);
348       Box extraBox = null;
349       if (extras != null) {
350     extraLabel = new JLabel(extras);
351     extraLabel.setFont(new Font("Arial", 0, 6));
352     extraBox = new Box(BoxLayout.X_AXIS);
353     extraBox.add(extraLabel);
354     extraBox.add(Box.createHorizontalGlue());
355       }
356 
357       scrollPane = new
358     JScrollPane(messageField,
359           JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
360           JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
361 
362       messageField.setText(message);
363 
364       Font font = PostgresAdapterGUI.this.getFont();
365 
366       messageField.setFont(font);
367       headerLabel.setFont(font);
368       retryButton.setFont(font);
369 
370       Box headerBox = new Box(BoxLayout.X_AXIS);
371       headerBox.add(headerLabel);
372       headerBox.add(Box.createHorizontalGlue());
373 
374       Box buttonBox = new Box(BoxLayout.X_AXIS);
375       buttonBox.add(Box.createHorizontalGlue());
376       buttonBox.add(retryButton);
377       buttonBox.add(Box.createHorizontalGlue());
378 
379       add(headerBox);
380       add(Box.createVerticalStrut(10));
381       add(scrollPane);
382       add(Box.createVerticalStrut(5));
383       add(buttonBox);
384       if (extras != null) {
385     add(Box.createVerticalStrut(5));
386     add(extraBox);
387       }
388   }
389 
390   public void retry() {
391       fireDataAdapterUIEvent(new
392     DataAdapterUIEvent(this,
393            PostgresAdapterGUI.this));
394   }
395 
396   public Object doOperation(Object obj) throws DataAdapterException {
397       return PostgresAdapterGUI.this.doOperation(obj);
398   }
399     }
400 
401     private class Committer extends Thread {
402   Commitable controllingObject;
403 
404   public Committer(Commitable controllingObject) {
405       this.controllingObject = controllingObject;
406   }
407 
408   public void run() {
409       try {
410     ((DataAdapterChooser) controllingObject).
411         doCommitWithExceptions();
412       } catch (PostgresDataAdapter.ConflictException e) {
413     fireDataAdapterUIEvent(new
414         DataAdapterUIEvent(this,
415                new ErrorUI("Database conflict "+
416                "detected",
417                e.formatConflictMessage(),
418                null)));
419       } catch (DataAdapterException e) {
420     if (e.getSubThrowable() instanceof SQLException) {
421         SQLException sqlEx = (SQLException) e.getSubThrowable();
422         fireDataAdapterUIEvent(new
423       DataAdapterUIEvent(this,
424              new ErrorUI("Database Error",
425                    sqlEx.getMessage(),
426                    "code:"+
427                    sqlEx.getErrorCode()+
428                    ",state:"+
429                    sqlEx.getSQLState())
430                  ));
431     } else if (e.getSubThrowable() != null &&
432          e.getSubThrowable() instanceof ClassNotFoundException) {
433         fireDataAdapterUIEvent(new
434       DataAdapterUIEvent(this,
435              new ErrorUI("Could not load database driver.",
436                    e.getMessage(),
437                    "Could not load database driver "+
438                    "\""+jdbcDriver+"\". Is the "+
439                    "driver class mentioned in the "+
440                    "class path (or the jar manifest "+
441                    "file, if you are using Windows)?"
442                  )));
443     } else {
444         String detail = "";
445         if (e.getSubThrowable() != null)
446       detail = e.getSubThrowable().toString();
447         e.printStackTrace();
448         fireDataAdapterUIEvent(new
449       DataAdapterUIEvent(this,
450              new ErrorUI("Adapter Error",
451                    e.getMessage(),
452                    detail
453                  )));
454     }
455       }
456   }
457     }
458 
459     public void runCommitter(Commitable controllingObject) {
460   try {
461       Committer committer = new Committer(controllingObject);
462       committer.start();
463   } catch (Exception ex) {
464       ex.printStackTrace();
465   }
466     }
467 
468     protected void attachListeners() {
469   okButton.addActionListener(new ActionListener() {
470     public void actionPerformed(ActionEvent e) {
471         if (controllingObject instanceof DataAdapterChooser) {
472       runCommitter(controllingObject);
473         }
474     }
475       });
476   cancelButton.addActionListener(new ActionListener() {
477     public void actionPerformed(ActionEvent e) {
478         if (controllingObject instanceof DataAdapterChooser) {
479       ((DataAdapterChooser) controllingObject).
480           cancel();
481         }
482     }
483       });
484   advancedButton.addActionListener(new ActionListener() {
485     public void actionPerformed(ActionEvent e) {
486         showAdvancedOptions();
487     }
488       });
489     }
490 
491     protected void showAdvancedOptions() {
492   JPanel advancedPanel = getAdvancedPanel(new JPanel());
493   advancedDialog = new JDialog();
494   advancedDialog.setModal(true);
495   advancedDialog.setContentPane(advancedPanel);
496   advancedDialog.setTitle("Change password");
497   advancedDialog.pack();
498   advancedDialog.show();
499   SwingUtil.center(advancedDialog);
500     }
501 
502     public void setFont(Font font) {
503   super.setFont(font);
504   if (urlField != null) {
505       okButton.setFont(font);
506       cancelButton.setFont(font);
507       advancedButton.setFont(font);
508       urlLabel.setFont(font);
509       driverLabel.setFont(font);
510       passwordLabel.setFont(font);
511       usernameLabel.setFont(font);
512       urlField.setFont(font);
513       driverField.setFont(font);
514       passwordField.setFont(font);
515       usernameField.setFont(font);
516       commentLabel.setFont(font);
517       commentField.setFont(font);
518       repopulateCheckbox.setFont(font);
519       forceHistoryCheckbox.setFont(font);
520       validate();
521   }
522     }
523 
524     public void loadDefaults() {
525   driverField.setText("");
526   usernameField.setText("");
527   passwordField.setText("");
528   commentField.setText("");
529     }
530 
531     public void setDataAdapter(DataAdapter in) {
532   driver = in;
533     }
534 
535     protected void collectParams() {
536   url = urlField.getText();
537   jdbcDriver = driverField.getText();
538   password = new String(passwordField.getPassword());
539   username = usernameField.getText();
540   comment = commentField.getText();
541   repopulate = repopulateCheckbox.isSelected();
542   forceHistory = forceHistoryCheckbox.isSelected();
543     }
544 
545     public Object doOperation(Object values)
546   throws DataAdapterException {
547   collectParams();
548   PostgresDataAdapter driver = (PostgresDataAdapter) this.driver;
549   try {
550       driver.init(jdbcDriver, url, username, password);
551       driver.setDefaultType(controller.getDefaultRelationshipType());
552   } catch (Exception e) {
553       throw new DataAdapterException(e, "Could not initialize adapter");
554   }
555   if (op == DEDataAdapterI.READ_TERMS) {
556       return driver.getRoot();
557   } else if (op == DEDataAdapterI.WRITE_TERMS){
558       driver.setSaveComment(comment);
559       driver.setRepopulate(repopulate);
560       driver.setForceHistory(forceHistory);
561       return driver.write((DEEditHistory) values);
562   } else
563       return null;
564     }
565 
566     public Properties getProperties() {
567   Properties out = new Properties();
568   String driver = driverField.getText();
569   String url = urlField.getText();
570   String user = usernameField.getText();
571 
572   if (driver == null || driver.length() == 0) {
573       driver = pwDriverField.getText();
574   }
575   if (url == null || url.length() == 0) {
576       url = pwUrlField.getText();
577   }
578   if (user == null || user.length() == 0) {
579       user = pwUsernameField.getText();
580   }
581 
582   out.setProperty("driver", driverField.getText());
583   out.setProperty("path", urlField.getText());
584   out.setProperty("user", usernameField.getText());
585   return out;
586     }
587 
588     public void setProperties(Properties in) {
589   String url = in.getProperty("path");
590   if (url == null)
591       url = "jdbc:postgresql://yossarian.colorado.edu/go";
592   String driver = in.getProperty("driver");
593   if (driver == null)
594       driver = "org.postgresql.Driver";
595   String user = in.getProperty("user");
596   urlField.setText(url);
597   urlField.setCaretPosition(0);
598   driverField.setText(driver);
599   driverField.setCaretPosition(0);
600   usernameField.setText(user);
601   usernameField.setCaretPosition(0);
602 
603   pwUrlField.setText(url);
604   pwUrlField.setCaretPosition(0);
605   pwDriverField.setText(driver);
606   pwDriverField.setCaretPosition(0);
607   pwUsernameField.setText(user);
608   pwUsernameField.setCaretPosition(0);
609   // do nothing
610     }
611 }