KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ozoneDB > adminGui > feature > account > users > UserPanel


1 //You can redistribute this software and/or modify it under the terms of
2
//the Ozone Library License version 1 published by ozone-db.org.
3
//
4
//The original code and portions created by SMB are
5
//Copyright (C) 1997-@year@ by SMB GmbH. All rights reserved.
6
//
7
package org.ozoneDB.adminGui.feature.account.users;
8
9 import java.awt.BorderLayout JavaDoc;
10 import java.awt.Point JavaDoc;
11 import java.awt.event.MouseEvent JavaDoc;
12 import javax.swing.*;
13
14 import org.ozoneDB.adminGui.feature.account.Account;
15 import org.ozoneDB.adminGui.widget.TableSorter;
16 import org.ozoneDB.adminGui.widget.TitledPanel;
17
18
19 //#############################################################################
20
/**
21  * This class is used to manage the account list panel.
22  *
23  * @author <p align=center>Ibsen Ramos-Bonilla
24  * <br>Copyright &copy 1997-@year@ by SMB GmbH. All Rights Reserved.</p>
25  *
26  * @version 1.0
27  */

28 //#############################################################################
29

30 public class UserPanel extends TitledPanel {
31
32     /** Handle to the parent panel. */
33     private JComponent parent;
34     /** Handle to the Users information instance. */
35     private UserAccount userAccount;
36
37     /** The account list table. */
38     private JTable userTable = new JTable();
39     /** Table model for the account list table. */
40     private UserTableModel userTableModel = new UserTableModel();
41     /** The object that provides column sorting. */
42     private TableSorter tableSorter = new TableSorter();
43     /** Indicates the last account selected. */
44     private String JavaDoc lastUser = "";
45
46
47     /**
48      * The overloaded constructor.
49      *
50      * @param parent - the account panel parent instance.
51      */

52     public UserPanel(JComponent parent) {
53         super("Users");
54         this.parent = parent;
55         init();
56
57         //load the table
58
this.userAccount.list();
59     }
60
61     /**
62      * This method initializes the account panel.
63      */

64     private void init() {
65         //set the account account
66
this.userAccount = new UserAccount(this);
67
68         //initialize the table components
69
//TODO:this.tableSorter.setModel(this.userTableModel);
70
//TODO:this.userTable.setModel(this.tableSorter);
71
this.userTable.setModel(this.userTableModel);
72
73         //set the account table and sorter routine
74
//TODO:this.tableSorter.addMouseListenerToHeaderInTable(this.userTable);
75
// TODO:userTable.getTableHeader().addMouseListener(this);
76

77         //add the controls to the panel
78
JScrollPane scrollPane = new JScrollPane(this.userTable);
79         this.add(scrollPane, BorderLayout.CENTER);
80     }
81
82     /**
83      * Sets the column sizes to appropriate widths and adds a mouse listener to
84      * the table so we know what row the account has selected.
85      */

86     /*private void initTable()
87     {
88         //add a listener for click events on the table.
89         userTable.addMouseListener(new MouseAdapter()
90         {
91             /*
92               detect mouse pressed instead of mouse clicked on this table
93               because if you use mouseClicked and the account moves the mouse even
94               a pixel between mouse pressed and mouse released, the mouse
95               click event never happens and the module buttons therefore don't
96               get updated properly.
97             */

98     /*public void mousePressed(MouseEvent e)
99     {
100         userTable_mousePressed(e);
101     }
102 });*/

103
104     //set up the columns
105
/*setUpColumns();
106     userTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
107
108     ETClientMouseEventAdapter etMouseAdapter =
109       new ETClientMouseEventAdapter (this.module.getFrame(),
110         this.userTable);
111     userTable.addMouseListener(etMouseAdapter);
112     userTable.addMouseMotionListener(etMouseAdapter);
113     resizeTable();
114 }*/

115
116     /**
117      * This method returns the id of the selected account.
118      *
119      * @return String -- account id.
120      */

121     public String JavaDoc getId() {
122         try {
123             return (String JavaDoc) (this.userTable.getModel().
124                     getValueAt(this.userTable.getSelectedRow(),
125                             Account.COLUMN_POS_USER_ID));
126         } catch (Exception JavaDoc e) {
127             return null;
128         }
129     }
130
131     /**
132      * This method returns the name of the selected account.
133      *
134      * @return String -- account name.
135      */

136     public String JavaDoc getName() {
137         try {
138             return (String JavaDoc) (this.userTable.getModel().
139                     getValueAt(this.userTable.getSelectedRow(),
140                             Account.COLUMN_POS_USER_NAME));
141         } catch (Exception JavaDoc e) {
142             return null;
143         }
144     }
145
146     /**
147      * This method returns the password of the selected account.
148      *
149      * @return String -- account password.
150      */

151     public String JavaDoc getPassword() {
152         try {
153             return (String JavaDoc) (this.userTable.getModel().
154                     getValueAt(this.userTable.getSelectedRow(),
155                             Account.COLUMN_POS_USER_PWD));
156         } catch (Exception JavaDoc e) {
157             return null;
158         }
159     }
160
161     /**
162      * This method returns the status of the selected account.
163      *
164      * @return String - account current status.
165      */

166     public String JavaDoc getStatus() {
167         try {
168             return (String JavaDoc) (this.userTable.getModel().
169                     getValueAt(this.userTable.getSelectedRow(),
170                             Account.COLUMN_POS_USER_STATUS));
171         } catch (Exception JavaDoc e) {
172             return null;
173         }
174     }
175
176     /**
177      * This method returns a handle to the account panel table.
178      *
179      * @return UserTable - handle to the account table.
180      */

181     public JTable getTable() {
182         return this.userTable;
183     }
184
185     /**
186      * This method returns a handle to the account panel table model.
187      *
188      * @return UserTableModel - handle to the account table model.
189      */

190     public UserTableModel getModel() {
191         return this.userTableModel;
192     }
193
194     /**
195      * This method returns a handle to the account panel table sorter.
196      *
197      * @return UserTableModel - handle to the account table sorter.
198      */

199     public TableSorter getSorter() {
200         return this.tableSorter;
201     }
202
203     /**
204      * This method returns a handle to the account panel group information.
205      *
206      * @return Groups - the groups information instance.
207      */

208     public UserAccount getUsers() {
209         return this.userAccount;
210     }
211
212     /**
213      * Returns the first name and last name of the account with the given id
214      * @param - String userId
215      * @return - an array of length two: first name and last name
216      */

217     /*public String[] getUserNames(String userId)
218     {
219         String[] names = {"", ""};
220
221         //get the number of rows in the table
222         int rowCount = userTable.getRowCount();
223         boolean found = false;
224         int index = 0;
225
226         //go through the rows until the correct userid is found. Add the first
227         //and last names to the array.
228         while(!found && index < rowCount)
229         {
230             String testId = (String)userTable.getValueAt(index, 0);
231             if(testId.equals(userId))
232             {
233                 found = true;
234                 names[0] = (String)userTable.getValueAt(index, 2);
235                 names[1] = (String)userTable.getValueAt(index, 1);
236             }
237             index++;
238         }
239         return (names);
240     }*/

241
242     /**
243      * Clears any selection made in the account list table.
244      */

245     /*public void clearSelection()
246     {
247         userTable.clearSelection();
248         module.setButtonVisible("changeUserStatusButton", false);
249         module.setButtonVisible("cloneUserButton", false);
250         module.setButtonVisible("modifyUserButton", false);
251         module.setButtonVisible("printUserProfileButton", false);
252         module.setButtonVisible("removeUserButton", false);
253         module.setButtonVisible("resetLoginButton", false);
254         module.setButtonVisible("resetPasswordButton", false);
255         module.setButtonVisible("saveUserProfileButton", false);
256
257     }*/

258
259     /**
260      * Sets up and formats the columns
261      */

262     /*public void setUpColumns()
263     {
264         for(int i = 0; i < 4; i++)
265         {
266             if(userTableModel.getColumnName(i).equals("User Id"))
267             {
268                 userTable.getColumnModel().getColumn(i)
269                                               .setPreferredWidth(100);
270                 userTable.getColumnModel().getColumn(i).setMinWidth(100);
271                 userTable.getColumnModel().getColumn(i)
272                                               .setIdentifier("User Id");
273                 userTable.getColumnModel().getColumn(i)
274                                               .setHeaderValue("User Id");
275             }
276             else if(userTableModel.getColumnName(i).equals("Last Name"))
277             {
278                 userTable.getColumnModel().getColumn(i)
279                                               .setPreferredWidth(200);
280                 userTable.getColumnModel().getColumn(i).setMinWidth(200);
281                 userTable.getColumnModel().getColumn(i)
282                                               .setIdentifier("Last Name");
283                 userTable.getColumnModel().getColumn(i)
284                                               .setHeaderValue("Last Name");
285             }
286             else if(userTableModel.getColumnName(i).equals("First Name"))
287             {
288                 userTable.getColumnModel().getColumn(i)
289                                               .setPreferredWidth(200);
290                 userTable.getColumnModel().getColumn(i).setMinWidth(200);
291                 userTable.getColumnModel().getColumn(i)
292                                               .setIdentifier("First Name");
293                 userTable.getColumnModel().getColumn(i)
294                                               .setHeaderValue("First Name");
295             }
296             else if(userTableModel.getColumnName(i).equals("Status"))
297             {
298                 userTable.getColumnModel().getColumn(i)
299                                               .setPreferredWidth(100);
300                 userTable.getColumnModel().getColumn(i).setMinWidth(100);
301                 userTable.getColumnModel().getColumn(i)
302                                               .setIdentifier("Status");
303                 userTable.getColumnModel().getColumn(i)
304                                               .setHeaderValue("Status");
305             }
306         }
307     }*/

308
309     /**
310      * This method is called when the account clicks on the users table. It
311      * determines what row was clicked on and sets the "lastSelectedUser"
312      * accordingly.
313      * @param MouseEvent -- the triggering event
314      */

315     /*public void userTable_mousePressed(MouseEvent e)
316     {
317         int selRow = userTable.getSelectedRow();
318         int userIdCol = userTable.getColumnModel().getColumnIndex("User Id");
319         if(selRow != -1)
320         {
321             lastSelectedUser = (String)userTable.getValueAt(selRow, userIdCol);
322             module.setButtonVisible("changeUserStatusButton", true);
323             module.setButtonVisible("cloneUserButton", true);
324             module.setButtonVisible("modifyUserButton", true);
325             module.setButtonVisible("printUserProfileButton", true);
326             module.setButtonVisible("removeUserButton", true);
327             module.setButtonVisible("resetLoginButton", true);
328             module.setButtonVisible("resetPasswordButton", true);
329             module.setButtonVisible("saveUserProfileButton", true);
330         }
331         else
332         {
333             lastSelectedUser = "";
334             module.setButtonVisible("changeUserStatusButton", false);
335             module.setButtonVisible("cloneUserButton", false);
336             module.setButtonVisible("modifyUserButton", false);
337             module.setButtonVisible("printUserProfileButton", false);
338             module.setButtonVisible("removeUserButton", false);
339             module.setButtonVisible("resetLoginButton", false);
340             module.setButtonVisible("resetPasswordButton", false);
341             module.setButtonVisible("saveUserProfileButton", false);
342         }
343     }*/

344
345     /**
346      * Populates the account list table.
347      * @param boolean -- if the table is just being refreshed, send true. If
348      * the account has clicked onto another module and come back,
349      * send false.
350      */

351     /*public void populateUserTable(boolean refresh)
352     {
353
354         //Get the account list from workflow
355         UserListRS users = module.getUserList();
356
357         //if there is a account list, use it to populate the table.
358         if(users != null)
359         {
360             userTableModel.setTableData(users);
361             if(!tableInit)
362             {
363                 initTable();
364                 module.setButtonVisible("printUserListButton", true);
365                 module.setButtonVisible("saveUserListButton", true);
366                 module.setButtonVisible("refreshUserListButton", true);
367                 module.setButtonVisible("addUserButton", true);
368                 tableInit = true;
369             }
370             else
371             {
372                 if(!refresh)
373                 {
374                     userTable.clearSelection();
375                     module.setButtonVisible("changeUserStatusButton",false);
376                     module.setButtonVisible("cloneUserButton", false);
377                     module.setButtonVisible("modifyUserButton", false);
378                     module.setButtonVisible("printUserProfileButton",false);
379                     module.setButtonVisible("removeUserButton", false);
380                     module.setButtonVisible("resetLoginButton", false);
381                     module.setButtonVisible("resetPasswordButton", false);
382                     module.setButtonVisible("saveUserProfileButton", false);
383                     lastSelectedUser = "";
384                 }
385             }
386             userTableModel.fireTableDataChanged();
387             tableSorter.setModel(userTableModel);
388         }
389         else
390         {
391             userTableModel.setTableData(null);
392             userTableModel.fireTableDataChanged();
393             tableSorter.setModel(userTableModel);
394         }
395     }*/

396
397     /**
398      * This method checks if the header is double-mouse clicked (the table is
399      * sorted) set the appropriate buttons and/or re-select the row that was
400      * selected before the sort.
401      *
402      * @param e - the triggering event.
403      */

404     public void mouseClicked(MouseEvent JavaDoc e) {
405         //if the source is the table header, then a sort may have been done
406
//and the table selection and buttons need to be properly updated.
407
if (e.getSource().equals(userTable.getTableHeader())) {
408             //double-clicking indicates a sort on the table. We don't care
409
//about other clicking.
410
if (e.getClickCount() == 2) {
411                 //find the column being clicked
412
Point JavaDoc p = e.getPoint();
413                 int x = (int) p.getX();
414                 int selCol = userTable.getColumnModel().getColumnIndexAtX(x);
415
416                 //if no account was selected at the time of the sort, then no
417
//new account should be selected now, so don't show any buttons.
418
if (lastUser.equals("")) {
419                     //todo do something here, disable buttons
420
//parent.getToolBar().setVisible("AccountToolBar", "removeUserButton", false);
421
}
422                 //otherwise, try to select the row that was selected at sort
423
//time.
424
else {
425                     int rowCount = userTable.getRowCount();
426                     int userIdCol = userTable.getColumnModel().getColumnIndex(
427                             Account.COLUMN_USER_ID);
428                     int selRow = -1;
429
430                     for (int i = 0; i < rowCount; i++) {
431                         String JavaDoc userid = (String JavaDoc) userTable.getValueAt(i,
432                                 userIdCol);
433                         if (lastUser.equals(userid)) {
434                             selRow = i;
435                             break;
436                         }
437                     }
438
439                     if (selRow != -1) {
440                         userTable.setRowSelectionInterval(selRow, selRow);
441                         userTable.setColumnSelectionInterval(selCol, selCol);
442                         userTable.scrollRectToVisible(
443                                 userTable.getCellRect(selRow, selCol, true));
444                         userTable.repaint();
445                     } else {
446                         //todo do something here, show some buttons
447
//parent.getToolBar().setVisible("AccountToolBar", "removeUserButton", true);
448
}
449                 }
450             }
451         }
452     }
453
454 } //--------------------------------- E O F -----------------------------------
455

456
457
Popular Tags