001: // RealmsServerHelper.java
002: // $Id: RealmsServerHelper.java,v 1.12 2000/08/16 21:37:30 ylafon Exp $
003: // (c) COPYRIGHT MIT and INRIA, 1998.
004: // Please first read the full copyright statement in file COPYRIGHT.html
005:
006: package org.w3c.jigadmin.editors;
007:
008: import javax.swing.JPanel;
009: import javax.swing.JLabel;
010: import javax.swing.JComboBox;
011: import javax.swing.JScrollPane;
012: import javax.swing.JButton;
013: import javax.swing.JTextField;
014: import javax.swing.JList;
015: import javax.swing.ListSelectionModel;
016: import javax.swing.BoxLayout;
017: import javax.swing.BorderFactory;
018: import javax.swing.border.TitledBorder;
019: import javax.swing.event.ListSelectionListener;
020: import javax.swing.event.ListSelectionEvent;
021:
022: import java.awt.Component;
023: import java.awt.BorderLayout;
024: import java.awt.Cursor;
025: import java.awt.GridLayout;
026: import java.awt.event.ActionListener;
027: import java.awt.event.ActionEvent;
028:
029: import java.util.Properties;
030: import java.util.Vector;
031:
032: import org.w3c.jigadmin.RemoteResourceWrapper;
033: import org.w3c.jigadmin.PropertyManager;
034: import org.w3c.jigadmin.gui.Message;
035:
036: import org.w3c.jigsaw.admin.RemoteAccessException;
037:
038: import org.w3c.tools.sorter.Sorter;
039: import org.w3c.tools.widgets.Utilities;
040:
041: /**
042: * The server helper dedicated to the Realms
043: * @version $Revision: 1.12 $
044: * @author Benoît Mahé (bmahe@w3.org)
045: */
046: public class RealmsServerHelper extends JPanel implements
047: ServerHelperInterface {
048: protected final static String ADD_USER_AC = "add_user";
049: protected final static String DEL_USER_AC = "del_user";
050: protected final static String DEL_REALM_AC = "del_realm";
051:
052: protected String name = null;
053: protected String tooltip = null;
054: protected RemoteResourceWrapper root = null;
055: protected RemoteResourceWrapper realmrrw = null;
056: protected RemoteResourceWrapper userrrw = null;
057: protected Vector realms = null;
058: protected Vector users = null;
059:
060: protected JComboBox combo = null;
061: protected JPanel usersPanel = null;
062: protected JPanel userPanel = null;
063: protected JList usersList = null;
064: protected JTextField userT = null;
065:
066: /**
067: * Our internal ActionListener
068: */
069: ActionListener al = new ActionListener() {
070: public void actionPerformed(ActionEvent e) {
071: Object obj = e.getSource();
072: if (obj == combo) {
073: String realm = (String) combo.getSelectedItem();
074: if (realms.contains(realm))
075: selectRealm(realm);
076: else if (realm.length() > 0)
077: addRealm(realm);
078: } else if (e.getActionCommand().equals(ADD_USER_AC)
079: || (obj == userT)) {
080: String user = userT.getText();
081: if ((user.length() > 0) && (!users.contains(user)))
082: addUser(user);
083: } else if (e.getActionCommand().equals(DEL_USER_AC)) {
084: Thread thread = new Thread() {
085: public void run() {
086: deleteCurrentUser();
087: }
088: };
089: thread.start();
090: } else if (e.getActionCommand().equals(DEL_REALM_AC)) {
091: Thread thread = new Thread() {
092: public void run() {
093: deleteCurrentRealm();
094: }
095: };
096: thread.start();
097: }
098: }
099: };
100:
101: /**
102: * Our internal ListSelectionListener
103: */
104: ListSelectionListener lsl = new ListSelectionListener() {
105: public void valueChanged(ListSelectionEvent e) {
106: if (!e.getValueIsAdjusting()) {
107: int idx = usersList.getSelectedIndex();
108: selectUser((String) users.elementAt(idx));
109: }
110: }
111: };
112:
113: /**
114: * Initialize this editor.
115: * @param name the editor name
116: * @param rrw the RemoteResourceWrapper wrapping the editor node.
117: * @param p the editor properties
118: */
119: public void initialize(String name, RemoteResourceWrapper rrw,
120: Properties p) {
121: this .root = rrw;
122: this .name = name;
123: this .tooltip = (String) p.get(TOOLTIP_P);
124: build();
125: }
126:
127: /**
128: * Build the interface
129: */
130: protected void build() {
131: removeAll();
132: String srealms[] = null;
133: try {
134: srealms = root.getResource().enumerateResourceIdentifiers();
135: } catch (RemoteAccessException ex) {
136: while (root.getServerBrowser().shouldRetry(ex)) {
137: try {
138: srealms = root.getResource()
139: .enumerateResourceIdentifiers();
140: break;
141: } catch (RemoteAccessException ex2) {
142: ex = ex2;
143: }
144: }
145: }
146: if (srealms == null)
147: return;
148:
149: Sorter.sortStringArray(srealms, true);
150: realms = new Vector(srealms.length);
151: for (int i = 0; i < srealms.length; i++)
152: realms.addElement(srealms[i]);
153:
154: setLayout(new BorderLayout());
155:
156: JPanel comboPanel = new JPanel();
157: combo = new JComboBox(realms);
158: combo.setEditable(true);
159: combo.addActionListener(al);
160:
161: JLabel realmLabel1 = new JLabel("Enter the realm name or");
162: JLabel realmLabel2 = new JLabel("select one from the list:");
163: realmLabel1.setAlignmentX(CENTER_ALIGNMENT);
164: realmLabel2.setAlignmentX(CENTER_ALIGNMENT);
165:
166: comboPanel
167: .setLayout(new BoxLayout(comboPanel, BoxLayout.Y_AXIS));
168: comboPanel.add(realmLabel1);
169: comboPanel.add(realmLabel2);
170: comboPanel.add(combo);
171: TitledBorder border = BorderFactory
172: .createTitledBorder("Realms");
173: border.setTitleFont(Utilities.mediumBoldFont);
174: comboPanel.setBorder(border);
175:
176: usersPanel = new JPanel(new BorderLayout(4, 4));
177: border = BorderFactory.createTitledBorder("Realm");
178: border.setTitleFont(Utilities.mediumBoldFont);
179: usersPanel.setBorder(border);
180:
181: JPanel realmsPanel = new JPanel(new BorderLayout());
182: realmsPanel.add(comboPanel, BorderLayout.NORTH);
183: realmsPanel.add(usersPanel, BorderLayout.CENTER);
184:
185: userPanel = new JPanel(new BorderLayout());
186: initUserPanel();
187:
188: add(realmsPanel, BorderLayout.WEST);
189: add(userPanel, BorderLayout.CENTER);
190: }
191:
192: /**
193: * Initialize the User Panel.
194: */
195: protected void initUserPanel() {
196: userPanel.removeAll();
197: userPanel.invalidate();
198: TitledBorder border = BorderFactory.createTitledBorder("User");
199: border.setTitleFont(Utilities.mediumBoldFont);
200: userPanel.setBorder(border);
201: userPanel.validate();
202: }
203:
204: /**
205: * Select (and display) the given realm.
206: * @param realm the Realm to select
207: */
208: protected void selectRealm(String realm) {
209: initUserPanel();
210: usersPanel.removeAll();
211: users = null;
212: realmrrw = null;
213:
214: try {
215: realmrrw = root.getChildResource(realm);
216: } catch (RemoteAccessException ex) {
217: while (root.getServerBrowser().shouldRetry(ex)) {
218: try {
219: realmrrw = root.getChildResource(realm);
220: break;
221: } catch (RemoteAccessException ex2) {
222: ex = ex2;
223: }
224: }
225: }
226:
227: if (realmrrw == null)
228: return;
229:
230: try {
231: String susers[] = realmrrw.getResource()
232: .enumerateResourceIdentifiers();
233: Sorter.sortStringArray(susers, true);
234: users = new Vector(susers.length);
235: for (int i = 0; i < susers.length; i++)
236: users.addElement(susers[i]);
237: } catch (RemoteAccessException ex) {
238: Message.showErrorMessage(this , ex);
239: }
240:
241: if (users == null)
242: return;
243:
244: //graphics...
245: usersPanel.invalidate();
246: if (usersList != null)
247: usersList.removeListSelectionListener(lsl);
248: usersList = new JList(users);
249: usersList.addListSelectionListener(lsl);
250: usersList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
251: usersList.setBorder(BorderFactory.createLoweredBevelBorder());
252:
253: JButton addUserB = new JButton("Add user");
254: addUserB.setMargin(Utilities.insets0);
255: addUserB.setActionCommand(ADD_USER_AC);
256: addUserB.addActionListener(al);
257:
258: JButton delRealmB = new JButton("Delete " + realm);
259: delRealmB.setMargin(Utilities.insets0);
260: delRealmB.setActionCommand(DEL_REALM_AC);
261: delRealmB.addActionListener(al);
262:
263: userT = new JTextField(8);
264: userT.addActionListener(al);
265:
266: JPanel p1 = new JPanel(new BorderLayout(1, 4));
267: p1.add(userT, BorderLayout.WEST);
268: p1.add(addUserB, BorderLayout.CENTER);
269: //p1.setBorder(BorderFactory.createEtchedBorder());
270:
271: JPanel p2 = new JPanel(new GridLayout(2, 1));
272: p2.add(p1);
273: p2.add(delRealmB);
274:
275: TitledBorder border = BorderFactory
276: .createTitledBorder("Realm: " + realm);
277: border.setTitleFont(Utilities.mediumBoldFont);
278: usersPanel.setBorder(border);
279: usersPanel.add(new JScrollPane(usersList), BorderLayout.CENTER);
280: usersPanel.add(p2, BorderLayout.SOUTH);
281: usersPanel.validate();
282: }
283:
284: /**
285: * Select (and display) the given user.
286: * @param realm the User to select
287: */
288: protected void selectUser(String user) {
289: try {
290: if (realmrrw == null) //no realm selected
291: return;
292: root.getServerBrowser().setCursor(Cursor.WAIT_CURSOR);
293: userrrw = realmrrw.getChildResource(user);
294: userPanel.removeAll();
295: userPanel.invalidate();
296: TitledBorder border = BorderFactory
297: .createTitledBorder("User: " + user);
298: border.setTitleFont(Utilities.mediumBoldFont);
299: userPanel.setBorder(border);
300: AttributesHelper helper = new AttributesHelper();
301: PropertyManager pm = PropertyManager.getPropertyManager();
302: Properties props = pm.getEditorProperties(userrrw);
303: helper.initialize(userrrw, props);
304:
305: JButton delUserB = new JButton("Delete user " + user);
306: delUserB.setMargin(Utilities.insets0);
307: delUserB.setActionCommand(DEL_USER_AC);
308: delUserB.addActionListener(al);
309:
310: userPanel.add(helper.getComponent(), BorderLayout.CENTER);
311: userPanel.add(delUserB, BorderLayout.SOUTH);
312: userPanel.validate();
313: root.getServerBrowser().setCursor(Cursor.DEFAULT_CURSOR);
314: } catch (RemoteAccessException ex) {
315: Message.showErrorMessage(this , ex);
316: }
317: }
318:
319: /**
320: * Add a user to the current selected realm.
321: * @param user the user name
322: */
323: protected void addUser(String user) {
324: try {
325: if (realmrrw == null)
326: return;
327: realmrrw.getResource().registerResource(user,
328: "org.w3c.jigsaw.auth.AuthUser");
329: selectRealm((String) combo.getSelectedItem());
330: } catch (RemoteAccessException ex) {
331: Message.showErrorMessage(this , ex);
332: }
333: }
334:
335: /**
336: * Delete the selected user.
337: */
338: protected void deleteCurrentUser() {
339: if (userrrw == null)
340: return;
341: try {
342: userrrw.getResource().delete();
343: selectRealm((String) combo.getSelectedItem());
344: } catch (RemoteAccessException ex) {
345: Message.showErrorMessage(this , ex);
346: }
347: }
348:
349: /**
350: * Create a new Realm.
351: * @param realm The name of the new realm.
352: */
353: protected void addRealm(String realm) {
354: try {
355: root.getResource().registerResource(realm,
356: "org.w3c.jigsaw.auth.AuthRealm");
357: combo.invalidate();
358: combo.addItem(realm);
359: combo.validate();
360: selectRealm(realm);
361: } catch (RemoteAccessException ex) {
362: Message.showErrorMessage(this , ex);
363: }
364: }
365:
366: /**
367: * Delete the current realm.
368: */
369: protected void deleteCurrentRealm() {
370: if (realmrrw == null) //no realm selected
371: return;
372: try {
373: realmrrw.getResource().delete();
374: invalidate();
375: build();
376: validate();
377: } catch (RemoteAccessException ex) {
378: Message.showErrorMessage(this , ex);
379: }
380: }
381:
382: /**
383: * Get the helper name.
384: * @return a String instance
385: */
386: public String getName() {
387: return name;
388: }
389:
390: /**
391: * Get the helper tooltip
392: * @return a String
393: */
394: public String getToolTip() {
395: return tooltip;
396: }
397:
398: /**
399: * Get the Component.
400: * @return a Component instance
401: */
402: public Component getComponent() {
403: return this ;
404: }
405:
406: /**
407: * Constructor.
408: */
409: public RealmsServerHelper() {
410: }
411:
412: }
|