Source code: org/mitre/cvw/AdmitUserDialog.java
1 /*
2 * Copyright (c) 1996-2000. The MITRE Corporation (http://www.mitre.org/).
3 * All rights reserved.
4 * CVW comes with ABSOLUTELY NO WARRANTY. See license for details.
5 */
6
7 package org.mitre.cvw;
8
9 import javax.swing.*;
10 import java.awt.*;
11 import java.awt.event.*;
12 import java.util.Date;
13 import java.util.Vector;
14 import java.util.StringTokenizer;
15
16 /**
17 * This is a generic move dialog that should be used for any error message that
18 * the user should be notified of. The dialog is equipped with an ok button.
19 *
20 * @version 1.0
21 * @author unascribed
22 */
23 public class AdmitUserDialog extends CVWDialog implements KeyListener {
24
25 private static AdmitUserDialog currentInstance = null;
26
27 /**
28 * Gets the current instance of the dialog
29 *
30 * @return The dialog instance.
31 */
32 public static AdmitUserDialog getCurrentInstance() {
33 return currentInstance;
34 }
35
36 /**
37 * Sets the current dialog instance
38 *
39 * @param dialog The dialog instance
40 */
41 private static void setCurrentInstance(AdmitUserDialog dialog) {
42 currentInstance = dialog;
43 }
44
45 protected JTextField users;
46 protected JTextField time;
47 protected Vector userObjs;
48 protected GridBagLayout gridbag = new GridBagLayout();
49 protected Font bold;
50 protected JButton okButton;
51 protected JButton cancelButton;
52
53 JButton usersButton;
54
55 CVWRoom room;
56 protected int TAB = 9;
57 protected int RET = 10;
58 protected int CR = 13;
59 protected int ESC = 27;
60
61 /**
62 * Constructor
63 *
64 * @param appletFrame The frame to display the dialog in.
65 * @param r The room to admit the user into.
66 * @param obj The users object
67 */
68 AdmitUserDialog(Frame appletFrame, CVWRoom r, CVWObject obj) {
69 super(appletFrame, "Admit User to " + r.name, false);
70 room = r;
71 Rectangle rect = getParent().getBounds();
72 setBounds(rect.x + (int)(rect.width/3), rect.y + (int)(rect.height/3),
73 300, 250);
74
75
76 setContentsLayout(gridbag);
77 bold = new Font("Helvetica", Font.BOLD, 12);
78 // make label
79 JLabel infoLabel = new JLabel("Enter or select one or more users and enter the duration.");
80 JLabel userLabel= new JLabel("Users:");
81 userLabel.setFont(bold);
82 JLabel timeLabel = new JLabel("Duration:");
83 timeLabel.setFont(bold);
84 JLabel hrLabel = new JLabel("hour(s)");
85 hrLabel.setFont(bold);
86
87 users = new JTextField(25);
88 userObjs = new Vector(2);
89 userObjs.addElement(obj);
90 time = new JTextField("1",10);
91 // create buttons
92 okButton = new JButton("OK");
93 okButton.setFont(bold);
94 //srj 3/2/99 1.1
95 okButton.addActionListener(new ActionListener() {
96 public void actionPerformed(ActionEvent e) {
97 ok();
98 }
99 });
100
101 cancelButton = new JButton("Cancel");
102 cancelButton.addActionListener(new ActionListener() {
103 public void actionPerformed(ActionEvent e) {
104 dispose();
105 }
106 });
107
108 Image image = CVWCoordinator.getInstance().getImageFromFile("images/OnlineUsers.gif");
109
110 usersButton = new JButton(new ImageIcon(image));
111 //usersButton.setMaximumSize(new Dimension(32,32));
112 usersButton.setMargin(new Insets(0,0,0,0));
113 usersButton.addActionListener(new ActionListener() {
114 public void actionPerformed(ActionEvent e) {
115 CVWCoordinator.getInstance().onlineUsersCommand();
116 }
117 });
118
119 JPanel btnPanel = new JPanel();
120 btnPanel.setLayout(new GridLayout(1,0,5,5));
121 btnPanel.add(okButton);
122 btnPanel.add(cancelButton);
123
124 if (obj != null)
125 users.setText(obj.name);
126
127 constrain(this, infoLabel, gridbag, 0,0,GridBagConstraints.REMAINDER,1, GridBagConstraints.HORIZONTAL,
128 GridBagConstraints.WEST, 1.0, 0.0, 10, 5, 0, 5, 0, 0);
129 constrain(this, userLabel, gridbag, 0,1,1,1, GridBagConstraints.NONE,
130 GridBagConstraints.WEST, 0.0, 0.0, 0, 5, 0, 5, 0, 0);
131 constrain(this, users, gridbag, 1,1,2,1, GridBagConstraints.HORIZONTAL,
132 GridBagConstraints.WEST, 1.0, 0.0, 5, 5, 5, 5, 0, 0);
133 constrain(this, usersButton, gridbag, 3,1,GridBagConstraints.REMAINDER,1, GridBagConstraints.NONE,
134 GridBagConstraints.WEST, 0.0, 0.0, 5, 5, 5, 5, 0, 0);
135 constrain(this, timeLabel, gridbag, 0,3,1,1, GridBagConstraints.NONE,
136 GridBagConstraints.WEST, 0.0, 0.0, 0, 5, 0, 5, 0, 0);
137 constrain(this, time, gridbag, 1,3,1,1, GridBagConstraints.NONE,
138 GridBagConstraints.WEST, 0.0, 0.0, 5, 5, 5, 5, 0, 0);
139 constrain(this, hrLabel, gridbag,GridBagConstraints.RELATIVE,3,GridBagConstraints.REMAINDER,1, GridBagConstraints.HORIZONTAL,
140 GridBagConstraints.WEST, 1.0, 0.0, 5, 5, 5, 5, 0, 0);
141 constrain(this, btnPanel, gridbag, 0, 5, GridBagConstraints.REMAINDER, 1,
142 GridBagConstraints.NONE,
143 GridBagConstraints.CENTER, 1.0, 0.0, 0, 0, 10, 10, 0, 0);
144
145 setCurrentInstance(this);
146
147 }
148
149 /**
150 * Add a user to the dialog
151 *
152 * @param name The user's name
153 * @param obj The object representing the user
154 */
155 public void addUser(CVWObject obj) {
156 String orig = users.getText().trim();
157 orig = orig + " " + obj.name + " ";
158 users.setText(orig);
159 users.select(orig.length() -1, orig.length()-1);
160 userObjs.addElement(obj);
161 }
162
163
164 /**
165 * Sets the visibility of the dialog
166 *
167 * @param v if <code>true</code> then the dialog will become visible
168 * <p>if <code>false</code> then the dialog will be hidden.
169 */
170 public void setVisible(boolean v) {
171 if (v) {
172 users.requestFocus();
173 users.selectAll();
174 }
175 super.setVisible(v);
176 }
177
178 /**
179 * Get the user's name from the entry dialog
180 *
181 * @return The user's object
182 */
183 public String getUsersNames() {
184 String uNames = users.getText().trim();
185 if (userObjs.size() == 0)
186 return uNames;
187 StringTokenizer st = new StringTokenizer(uNames);
188 String each;
189 String sendObjs = "";
190 while (st.hasMoreTokens()) {
191 each = st.nextToken();
192 for (int i = 0; i < userObjs.size(); i++) {
193 CVWObject obj = (CVWObject)userObjs.elementAt(i);
194 if (obj.name.equals(each)) {
195 sendObjs = " " + obj.objNum.strValue();
196 }
197 }
198 }
199 return sendObjs;
200 }
201
202 /**
203 * The ok call for the dialog
204 */
205 public void ok() {
206 String userNames = users.getText().trim();
207 userNames = userNames.replace(',', ' ');
208 userNames = "\"" + userNames + "\"";
209 //String userNames = getUsersNames();
210 String duration = time.getText().trim();
211 if(userNames.equals(""))
212 return;
213 int dur;
214 try {
215 dur = Integer.parseInt(duration);
216 } catch (Exception e) {
217 //System.err.println("you need to enter a integer representing number of hours.");
218 time.selectAll();
219 time.requestFocus();
220 return;
221 }
222 //System.err.println("admit " + userNames.replace(',', ' ') + " to " + room.name + " for " + dur);
223 long time = (new Date()).getTime();
224 long start = time / 1000;
225 //System.err.println(time + " " + start);
226 long stop = dur * 3600;
227 stop = start + stop;
228 CVWServerComm.sendMCPCmdToServer("#$#cvw-access-admit-request",
229 " room: " + room.objNum.strValue() +
230 " to-name: " + userNames +
231 " start: 0" + // need to send 0 for now not start
232 " expire: " + stop);
233 dispose();
234 }
235
236 /**
237 * Remove the dialog instance
238 */
239 public synchronized void dispose() {
240 setCurrentInstance(null);
241 super.dispose();
242 }
243
244 public void keyPressed(KeyEvent e) {}
245 public void keyTyped(KeyEvent e) {}
246 public void keyReleased(KeyEvent e) {
247 int code = e.getKeyCode();
248 Component target = e.getComponent();
249 if (code == KeyEvent.VK_ENTER) {
250 if (target == cancelButton) {
251 dispose();
252 } else {
253 ok();
254 }
255 } else if (code == KeyEvent.VK_ESCAPE) {
256 dispose();
257 }
258 }
259 }
260