001: // AttributeHelper.java
002: // $Id: AttributesHelper.java,v 1.8 2000/08/16 21:37:30 ylafon Exp $
003: // (c) COPYRIGHT MIT and INRIA, 1997.
004: // Please first read the full copyright statement in file COPYRIGHT.html
005:
006: package org.w3c.jigadmin.editors;
007:
008: import java.awt.Component;
009: import java.awt.Color;
010: import java.awt.ScrollPane;
011: import java.awt.BorderLayout;
012: import java.awt.GridBagLayout;
013: import java.awt.GridBagConstraints;
014: import java.awt.event.ActionListener;
015: import java.awt.event.ActionEvent;
016: import java.awt.event.MouseAdapter;
017: import java.awt.event.MouseEvent;
018:
019: import javax.swing.JPanel;
020: import javax.swing.JLabel;
021: import javax.swing.JButton;
022: import javax.swing.BorderFactory;
023: import javax.swing.border.BevelBorder;
024: import javax.swing.JScrollPane;
025:
026: import java.util.Properties;
027:
028: import org.w3c.jigadmin.RemoteResourceWrapper;
029: import org.w3c.jigadmin.PropertyManager;
030: import org.w3c.jigadm.editors.AttributeEditor;
031: import org.w3c.jigadm.events.ResourceChangeEvent;
032:
033: import org.w3c.jigsaw.admin.RemoteResource;
034: import org.w3c.jigsaw.admin.RemoteAccessException;
035:
036: import org.w3c.tools.resources.Attribute;
037: import org.w3c.tools.resources.serialization.AttributeDescription;
038: import org.w3c.tools.widgets.Utilities;
039:
040: /**
041: * The Attributes helper
042: * @version $Revision: 1.8 $
043: * @author Benoît Mahé (bmahe@w3.org)
044: */
045: public class AttributesHelper extends ResourceHelper {
046:
047: /**
048: * Our internal ActionListener
049: */
050: ActionListener al = new ActionListener() {
051: public void actionPerformed(ActionEvent ae) {
052: if (ae.getActionCommand().equals("Reset"))
053: resetChanges();
054: else if (ae.getActionCommand().equals(COMMIT_L)) {
055: Thread commiter = new Thread() {
056: public void run() {
057: try {
058: commitChanges();
059: } catch (RemoteAccessException ex) {
060: errorPopup("RemoteAccessException", ex);
061: }
062: }
063: };
064: setMessage("Committing...");
065: commiter.start();
066: setMessage("Commit done.");
067: }
068: }
069: };
070:
071: /**
072: * Our internal MouseListener
073: */
074: MouseAdapter ma = new MouseAdapter() {
075: public void mouseEntered(MouseEvent e) {
076: Component comp = e.getComponent();
077: if (comp instanceof JButton) {
078: String action = ((JButton) comp).getActionCommand();
079: if (action.equals(COMMIT_L)) {
080: setMessage("Commit the changes to the server.");
081: } else if (action.equals(RESET_L)) {
082: setMessage("Reset changes");
083: }
084: }
085: }
086:
087: public void mouseExited(MouseEvent e) {
088: setMessage("");
089: }
090: };
091:
092: private RemoteResourceWrapper rrw = null;
093: private AttributeDescription[] a = null;
094: private AttributeEditor[] ae = null;
095: private boolean initialized = false;
096:
097: protected static final String COMMIT_L = "Commit";
098: protected static final String RESET_L = "Reset";
099:
100: JPanel widget;
101: JLabel message;
102:
103: /**
104: * Set the message of the information Label.
105: * @param msg the message to display
106: */
107: public void setMessage(String msg) {
108: message.setText(msg);
109: }
110:
111: /**
112: * Commit changes (if any)
113: * @exception RemoteAccessException if a remote access error occurs.
114: */
115: public void commitChanges() throws RemoteAccessException {
116: if (!initialized)
117: return;
118:
119: int num = 0;
120: for (int i = 0; i < ae.length; i++) {
121: if (ae[i].hasChanged())
122: num++;
123: }
124: boolean authorized;
125: String s[] = new String[num];
126: Object o[] = new Object[num];
127: num = 0;
128: for (int i = 0; i < ae.length; i++) {
129: if (ae[i].hasChanged()) {
130: s[num] = a[i].getName();
131: o[num] = ae[i].getValue();
132: if (s[num].equals("identifier")) {
133: // should send an event FIXME!!!!
134: if (rrw.getBrowser() != null)
135: rrw.getBrowser().renameNode(rrw,
136: (String) o[num]);
137: processEvent(new ResourceChangeEvent(rrw,
138: "identifier", null, o[num]));
139: }
140: num++;
141: }
142: }
143: authorized = false;
144: while (!authorized) {
145: try {
146: authorized = true;
147: rrw.getResource().setValues(s, o);
148: } catch (RemoteAccessException ex) {
149: if (ex.getMessage().equals("Unauthorized")) {
150: authorized = false;
151: } else {
152: throw ex;
153: }
154: } finally {
155: if (!authorized) {
156: rrw.getServerBrowser().popupPasswdDialog("admin");
157: }
158: }
159: }
160: clearChanged();
161: // FIXME propagate event
162: }
163:
164: /**
165: * tells if the edited resource in the helper has changed
166: * @return <strong>true</strong> if the values changed.
167: * to get more informations about what has changed, you can use the
168: * three methods below.
169: */
170: public boolean hasChanged() {
171: if (ae == null)
172: return false;
173: boolean changed = false;
174: for (int i = 0; !changed && i < ae.length; i++) {
175: changed = ae[i].hasChanged();
176: }
177: return changed;
178: }
179:
180: /**
181: * undo the not-yet-commited changes
182: */
183: public void resetChanges() {
184: if (ae == null)
185: return;
186:
187: for (int i = 0; i < ae.length; i++) {
188: if (ae[i].hasChanged())
189: ae[i].resetChanges();
190: }
191: }
192:
193: /**
194: * set the current resource to be the original resource (ie: the
195: * hasChanged() method must return <strong>false</false> now.
196: * to do a "fine tuned" reset, use one of the three following method.
197: */
198: public void clearChanged() {
199: if (ae == null)
200: return;
201: for (int i = 0; i < ae.length; i++) {
202: if (ae[i].hasChanged())
203: ae[i].clearChanged();
204: }
205: }
206:
207: /**
208: * Get the AttributeHelper component
209: */
210: public Component getComponent() {
211: return widget;
212: }
213:
214: /**
215: * Get the AttributeHelper title
216: */
217: public final String getTitle() {
218: return "Attribute";
219: }
220:
221: /**
222: * Constructor.
223: */
224: public AttributesHelper() {
225: widget = new JPanel(new BorderLayout());
226: }
227:
228: /**
229: * initialize the helper
230: * @param r the ResourceWrapper containing the Resource edited with
231: * this helper
232: * @param p some Properties, used to fine-tune the helper
233: * @exception RemoteAccessException if a remote access error occurs.
234: */
235: public void initialize(org.w3c.jigadm.RemoteResourceWrapper r,
236: Properties pr) throws RemoteAccessException {
237: if (initialized)
238: return;
239:
240: RemoteResource rr;
241: AttributeDescription b[] = null;
242: String s[] = null;
243: int nbn = 0;
244: boolean authorized;
245:
246: this .rrw = (RemoteResourceWrapper) r;
247: rr = rrw.getResource();
248: authorized = false;
249: while (!authorized) {
250: try {
251: authorized = true;
252: b = rr.getAttributes();
253: } catch (RemoteAccessException ex) {
254: if (ex.getMessage().equals("Unauthorized")) {
255: authorized = false;
256: } else {
257: throw ex;
258: }
259: } finally {
260: if (!authorized) {
261: rrw.getServerBrowser().popupPasswdDialog("admin");
262: }
263: }
264: }
265: // we select only the editable Attributes.
266: for (int i = 0; i < b.length; i++)
267: if (b[i] == null)
268: nbn++;
269: else if (!b[i].getAttribute().checkFlag(Attribute.EDITABLE))
270: nbn++;
271: a = new AttributeDescription[b.length - nbn];
272: ae = new AttributeEditor[a.length];
273: int j = 0;
274: for (int i = 0; i < b.length; i++) {
275: if (b[i] != null
276: && b[i].getAttribute()
277: .checkFlag(Attribute.EDITABLE)) {
278: a[j++] = b[i];
279: }
280: }
281:
282: // add all the attribute editors
283:
284: JLabel l;
285: GridBagLayout gbl = new GridBagLayout();
286: GridBagConstraints gbc = new GridBagConstraints();
287: JPanel p = new JPanel(gbl);
288: gbc.fill = GridBagConstraints.HORIZONTAL;
289: gbc.weightx = 0;
290: gbc.weighty = 0;
291: gbc.insets = Utilities.insets4;
292: for (int i = 0; i < a.length; i++) {
293: if (a[i] != null) {
294: PropertyManager pm = PropertyManager
295: .getPropertyManager();
296: Properties attrProps = pm.getAttributeProperties(rrw,
297: a[i].getAttribute());
298: String labelText = (String) attrProps.get("label");
299: if (labelText == null)
300: labelText = a[i].getName();
301: l = new JLabel(labelText, JLabel.RIGHT);
302: ae[i] = AttributeEditorFactory.getEditor(rrw, a[i]
303: .getAttribute());
304: authorized = false;
305: while (!authorized) {
306: try {
307: authorized = true;
308: ae[i].initialize(rrw, a[i].getAttribute(), a[i]
309: .getValue(), attrProps);
310: } catch (RemoteAccessException ex) {
311: if (ex.getMessage().equals("Unauthorized")) {
312: authorized = false;
313: } else {
314: throw ex;
315: }
316: } finally {
317: if (!authorized) {
318: rrw.getServerBrowser().popupPasswdDialog(
319: "admin");
320: }
321: }
322: }
323: gbc.gridwidth = 1;
324: gbl.setConstraints(l, gbc);
325: p.add(l);
326: gbc.gridwidth = GridBagConstraints.REMAINDER;
327: gbl.setConstraints(ae[i].getComponent(), gbc);
328: p.add(ae[i].getComponent());
329: }
330: }
331:
332: JScrollPane pwidget = new JScrollPane(p);
333: //pwidget.add(p);
334: widget.add("Center", pwidget);
335: // Now add the reset/commit button bar
336:
337: JPanel toolpane = new JPanel(new BorderLayout());
338: JButton commitb = new JButton(COMMIT_L);
339: JButton resetb = new JButton(RESET_L);
340:
341: commitb.addMouseListener(ma);
342: resetb.addMouseListener(ma);
343:
344: commitb.addActionListener(al);
345: resetb.addActionListener(al);
346:
347: message = new JLabel("", JLabel.CENTER);
348: message.setForeground(Color.white);
349: message.setBackground(Color.gray);
350:
351: JPanel pmsg = new JPanel(new BorderLayout());
352: pmsg.setBorder(BorderFactory
353: .createBevelBorder(BevelBorder.LOWERED));
354: pmsg.add("Center", message);
355:
356: toolpane.add("West", commitb);
357: toolpane.add("Center", pmsg);
358: toolpane.add("East", resetb);
359:
360: widget.add("South", toolpane);
361: // add information about the class of the resource edited
362:
363: String classes[] = { "" };
364: try {
365: classes = rr.getClassHierarchy();
366: } catch (RemoteAccessException ex) {
367: // big trouble but it may be temporary and this information
368: // is not vital, so just warn
369: ex.printStackTrace();
370: }
371: l = new JLabel("Class: " + classes[0], JLabel.CENTER);
372: l.setForeground(new Color(0, 0, 128));
373: widget.add("North", l);
374: initialized = true;
375: }
376: }
|