KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > shark > swingclient > Connection


1 package org.enhydra.shark.swingclient;
2
3 import java.awt.*;
4 import java.awt.event.*;
5
6 import javax.swing.*;
7
8
9 import org.enhydra.shark.api.client.wfservice.*;
10
11 /**
12  * Implements dialog for connection to the workflow engine. User
13  * enters it's username, password, nameserver host name and
14  * nameserver port. If data are correct, it retrieves the
15  * WorkflowServer object that is registred at the nameserver, and
16  * gets the WorkflowClientService instance which is used to
17  * retreive data from engine.
18  *
19  * @author Sasa Bojanic
20  * @version 1.0
21  */

22 public class Connection extends ActionPanel {
23
24    private static Dimension textFieldDimension=new Dimension(200,20);
25
26
27    private String JavaDoc username=null;
28    private String JavaDoc pwd=null;
29
30    private JTextField usernameField;
31    private JPasswordField passwordField;
32
33    private SharkConnection workflowService;
34
35    private SharkClient workflowClient;
36
37    public Connection(JFrame parent,SharkClient workflowClient){
38       super();
39       this.workflowClient=workflowClient;
40       super.init();
41       super.initDialog(parent,ResourceManager.getLanguageDependentString("DialogConnectToWorkflowServer"),true,true);
42    }
43
44    protected void createActions () {}
45
46    protected Component createCenterComponent (){
47       JPanel panel = new JPanel();
48
49       panel.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
50       panel.setLayout(new BoxLayout(panel,BoxLayout.Y_AXIS));
51
52       // username
53
JPanel unPanel=new JPanel();
54       unPanel.setLayout(new BoxLayout(unPanel,BoxLayout.X_AXIS));
55       JLabel unl=new JLabel(ResourceManager.getLanguageDependentString("UsernameKey")+":");
56       unPanel.add(Box.createHorizontalGlue());
57       unPanel.add(unl);
58       usernameField=new JTextField();
59       usernameField.setMinimumSize(new Dimension(textFieldDimension));
60       usernameField.setMaximumSize(new Dimension(textFieldDimension));
61       usernameField.setPreferredSize(new Dimension(textFieldDimension));
62       unPanel.add(usernameField);
63
64       // password
65
JPanel pwdPanel=new JPanel();
66       pwdPanel.setLayout(new BoxLayout(pwdPanel,BoxLayout.X_AXIS));
67       JLabel pwdl = new JLabel(ResourceManager.getLanguageDependentString("PasswordKey")+":");
68       pwdPanel.add(Box.createHorizontalGlue());
69       pwdPanel.add(pwdl);
70       passwordField = new JPasswordField();
71       passwordField.setMinimumSize(new Dimension(textFieldDimension));
72       passwordField.setMaximumSize(new Dimension(textFieldDimension));
73       passwordField.setPreferredSize(new Dimension(textFieldDimension));
74       pwdPanel.add(passwordField);
75
76       panel.add(unPanel);
77       panel.add(pwdPanel);
78       return panel;
79    }
80
81    protected void applyChanges () {
82       try {
83          connectToServer();
84          UserGroupAdministration uga=SharkClient.getUserGroupAmin();
85          try {
86             if (!uga.doesUserExist(usernameField.getText())) {
87                String JavaDoc defGName="SharkGroup";
88                try {
89                   if (!uga.doesGroupExist(defGName)) {
90                      uga.createGroup(defGName,"");
91                   }
92                } catch (Exception JavaDoc ex) {
93                }
94                uga.createUser(defGName,
95                               usernameField.getText(),
96                               new String JavaDoc(passwordField.getPassword()),
97                               "Default User", "", "");
98             }
99          } catch (Exception JavaDoc ex) {}
100          workflowService.connect(usernameField.getText(),
101                                  new String JavaDoc(passwordField.getPassword()),"","");
102          username=usernameField.getText();
103          pwd=new String JavaDoc(passwordField.getPassword());
104       } catch (ConnectFailed cf) {
105          workflowService=null;
106          username=null;
107          pwd=null;
108          JOptionPane.showMessageDialog(this,
109                                        ResourceManager.getLanguageDependentString("ErrorUncorrectLogin"),
110                                        ResourceManager.getLanguageDependentString("DialogConnectToWorkflowServer"),
111                                        JOptionPane.ERROR_MESSAGE);
112       } catch (Exception JavaDoc ex) {
113          throw new Error JavaDoc("Something went wrong with Shark initialization");
114       }
115       updateClient();
116       if (username!=null) {
117          myDialog.hide();
118       }
119    }
120
121    private void connectToServer () {
122       try{
123          workflowService=SharkClient.findWorkflowServer().getSharkConnection();
124       } catch(Exception JavaDoc e) {
125          //e.printStackTrace();
126
}
127    }
128
129    protected void cancelChanges () {
130       workflowService=null;
131       username=null;
132       pwd=null;
133       myDialog.hide();
134       updateClient();
135    }
136
137    private void updateClient () {
138       workflowClient.setUsername(username);
139       workflowClient.setPassword(pwd);
140       workflowClient.setService(workflowService);
141       workflowClient.setTitle();
142    }
143
144    public void showDialog() {
145       passwordField.setText("");
146       super.showDialog();
147    }
148
149 }
150
151
Popular Tags