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

Quick Search    Search Deep

Source code: it/rabellino/toska/gui/NewUserPanel.java


1   package it.rabellino.toska.gui;
2   
3   import it.rabellino.toska.DuplicateUserException;
4   import it.rabellino.toska.Host;
5   import it.rabellino.toska.User;
6   import java.awt.BorderLayout;
7   import java.awt.event.ActionEvent;
8   
9   import javax.swing.JButton;
10  import javax.swing.JLabel;
11  import javax.swing.JOptionPane;
12  import javax.swing.JPanel;
13  import javax.swing.JTextField;
14  import javax.swing.SwingConstants;
15  import javax.swing.tree.DefaultMutableTreeNode;
16  import javax.swing.tree.DefaultTreeModel;
17  
18  /**
19   * @author <a href="gianugo@apache.org">Gianugo Rabellino</a>
20   *
21   */
22  public class NewUserPanel extends AbstractPanel  {
23  
24    private JTextField userField;
25  
26    private Host host;
27    private Constants context;
28      
29    /**
30     * Constructor for NewUserPanel.
31     */
32    public NewUserPanel() {
33      super();
34  
35      this.setLayout(new BorderLayout());
36      JLabel title = new JLabel("Add a username");
37      title.setHorizontalAlignment(SwingConstants.CENTER);
38      this.add(title, BorderLayout.NORTH);
39      
40      JPanel contentPanel = new JPanel();
41      JLabel userLabel = new JLabel("Username: ");
42      
43      contentPanel.setAlignmentY(SwingConstants.CENTER);
44      userField = new JTextField(16);
45      contentPanel.add(userLabel);
46      contentPanel.add(userField);
47      
48      JButton okButton =  new JButton("Add this user");
49      okButton.addActionListener(this);
50      this.add(contentPanel, BorderLayout.CENTER);
51      this.add(okButton, BorderLayout.SOUTH);
52      
53          
54    }
55    
56    /**
57     * @see ActionListener#actionPerformed(ActionEvent)
58     */
59    public void actionPerformed(ActionEvent arg0) {
60       
61       if (userField.getText().length() == 0) {
62         JOptionPane.showMessageDialog(
63          this, "Please insert a valid Unix username", "Error", 
64          JOptionPane.ERROR_MESSAGE);
65          return;     
66       }
67       
68       host = (Host)currentNode.getUserObject();
69       
70       // Update model
71       User newUser = new User(userField.getText());
72       try {
73         this.host.addUser(newUser);
74       } catch (DuplicateUserException e) {
75         JOptionPane.showMessageDialog(
76          this, "The username already exists, delete it first", 
77            "Error", JOptionPane.ERROR_MESSAGE);
78         return;          
79       }
80       
81       // Update view
82          
83       DefaultMutableTreeNode newNode = 
84         new DefaultMutableTreeNode(newUser);
85       newNode.setUserObject(newUser);
86       currentNode.add(newNode);
87      
88      
89       ((DefaultTreeModel)tree.getModel()).nodeStructureChanged(currentNode);
90       
91       logger.info("Added user: " + newUser.getName());
92       handler.setChanged(true);    
93       menuBar.modelChanged();
94  
95    }   
96  
97  }