001: // AttributeHelper.java
002: // $Id: AttributesHelper.java,v 1.19 2000/08/16 21:37:26 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.jigadm.editors;
007:
008: import java.awt.BorderLayout;
009: import java.awt.Button;
010: import java.awt.Color;
011: import java.awt.Component;
012: import java.awt.Container;
013: import java.awt.GridBagConstraints;
014: import java.awt.GridBagLayout;
015: import java.awt.Label;
016: import java.awt.Panel;
017: import java.awt.ScrollPane;
018:
019: import java.awt.event.ActionEvent;
020: import java.awt.event.ActionListener;
021: import java.awt.event.ComponentEvent;
022: import java.awt.event.MouseAdapter;
023: import java.awt.event.MouseEvent;
024:
025: import java.util.Hashtable;
026: import java.util.Properties;
027:
028: import org.w3c.jigsaw.admin.RemoteAccessException;
029: import org.w3c.jigsaw.admin.RemoteResource;
030:
031: import org.w3c.tools.resources.Attribute;
032: import org.w3c.tools.resources.serialization.AttributeDescription;
033:
034: import org.w3c.tools.widgets.BorderPanel;
035:
036: import org.w3c.jigadm.PropertyManager;
037: import org.w3c.jigadm.RemoteResourceWrapper;
038:
039: import org.w3c.jigadm.gui.ServerBrowser;
040:
041: import org.w3c.jigadm.events.ResourceChangeEvent;
042:
043: public class AttributesHelper extends ResourceHelper {
044:
045: class ButtonBarListener implements ActionListener {
046:
047: class Commiter extends Thread {
048: public void run() {
049: try {
050: commitChanges();
051: } catch (RemoteAccessException ex) {
052: errorPopup("RemoteAccessException", ex);
053: }
054: }
055: }
056:
057: public void actionPerformed(ActionEvent ae) {
058: if (ae.getActionCommand().equals("Reset"))
059: resetChanges();
060: else if (ae.getActionCommand().equals(COMMIT_L)) {
061: setMessage("Committing...");
062: (new Commiter()).start();
063: setMessage("Commit done.");
064: }
065: }
066: }
067:
068: class MouseButtonListener extends MouseAdapter {
069:
070: public void mouseEntered(MouseEvent e) {
071: Component comp = e.getComponent();
072: if (comp instanceof Button) {
073: String action = ((Button) comp).getActionCommand();
074: if (action.equals(COMMIT_L)) {
075: setMessage("Commit the changes to the server.");
076: } else if (action.equals(RESET_L)) {
077: setMessage("Reset changes");
078: }
079: }
080: }
081:
082: public void mouseExited(MouseEvent e) {
083: setMessage("");
084: }
085: }
086:
087: private RemoteResourceWrapper rrw = null;
088: private AttributeDescription[] a = null;
089: private AttributeEditor[] ae = null;
090: private ScrollPane pwidget;
091: private boolean initialized = false;
092:
093: protected static final String COMMIT_L = "Commit";
094: protected static final String RESET_L = "Reset";
095:
096: Panel widget;
097: Label message;
098:
099: public void setMessage(String msg) {
100: message.setText(msg);
101: }
102:
103: /**
104: * Commit changes (if any)
105: * @exception RemoteAccessException if a remote access error occurs.
106: */
107: public void commitChanges() throws RemoteAccessException {
108: if (!initialized)
109: return;
110:
111: int num = 0;
112: for (int i = 0; i < ae.length; i++) {
113: if (ae[i].hasChanged())
114: num++;
115: }
116: boolean authorized;
117: String s[] = new String[num];
118: Object o[] = new Object[num];
119: num = 0;
120: for (int i = 0; i < ae.length; i++) {
121: if (ae[i].hasChanged()) {
122: s[num] = a[i].getName();
123: o[num] = ae[i].getValue();
124: if (s[num].equals("identifier")) {
125: // should send an event
126: if (rrw.getBrowser() != null)
127: rrw.getBrowser().renameNode(rrw,
128: (String) o[num]);
129: processEvent(new ResourceChangeEvent(rrw,
130: "identifier", null, o[num]));
131: }
132: num++;
133: }
134: }
135: authorized = false;
136: while (!authorized) {
137: try {
138: authorized = true;
139: rrw.getResource().setValues(s, o);
140: } catch (RemoteAccessException ex) {
141: if (ex.getMessage().equals("Unauthorized")) {
142: authorized = false;
143: } else {
144: throw ex;
145: }
146: } finally {
147: if (!authorized) {
148: rrw.getBrowser().popupDialog("admin");
149: }
150: }
151: }
152: clearChanged();
153: // FIXME propagate event
154: }
155:
156: public boolean hasChanged() {
157: if (ae == null)
158: return false;
159: boolean changed = false;
160: for (int i = 0; !changed && i < ae.length; i++) {
161: changed = ae[i].hasChanged();
162: }
163: return changed;
164: }
165:
166: public void resetChanges() {
167: if (ae == null)
168: return;
169:
170: for (int i = 0; i < ae.length; i++) {
171: if (ae[i].hasChanged())
172: ae[i].resetChanges();
173: }
174: }
175:
176: public void clearChanged() {
177: if (ae == null)
178: return;
179: for (int i = 0; i < ae.length; i++) {
180: if (ae[i].hasChanged())
181: ae[i].clearChanged();
182: }
183: }
184:
185: public Component getComponent() {
186: return widget;
187: }
188:
189: public final String getTitle() {
190: return "Attribute";
191: }
192:
193: public AttributesHelper() {
194: widget = new Panel(new BorderLayout());
195: }
196:
197: /**
198: * initialize.
199: * @exception RemoteAccessException if a remote access error occurs.
200: */
201: public void initialize(RemoteResourceWrapper rrw, Properties pr)
202: throws RemoteAccessException {
203: if (initialized)
204: return;
205:
206: RemoteResource rr;
207: AttributeDescription b[] = null;
208: String s[] = null;
209: int nbn = 0;
210: boolean authorized;
211:
212: this .rrw = rrw;
213: rr = rrw.getResource();
214: authorized = false;
215: while (!authorized) {
216: try {
217: authorized = true;
218: b = rr.getAttributes();
219: } catch (RemoteAccessException ex) {
220: if (ex.getMessage().equals("Unauthorized")) {
221: authorized = false;
222: } else {
223: throw ex;
224: }
225: } finally {
226: if (!authorized) {
227: rrw.getBrowser().popupDialog("admin");
228: }
229: }
230: }
231: // we select only the editable Attributes.
232: for (int i = 0; i < b.length; i++)
233: if (b[i] == null)
234: nbn++;
235: else if (!b[i].getAttribute().checkFlag(Attribute.EDITABLE))
236: nbn++;
237: a = new AttributeDescription[b.length - nbn];
238: ae = new AttributeEditor[a.length];
239: int j = 0;
240: for (int i = 0; i < b.length; i++) {
241: if (b[i] != null
242: && b[i].getAttribute()
243: .checkFlag(Attribute.EDITABLE)) {
244: a[j++] = b[i];
245: }
246: }
247:
248: // add all the attribute editors
249:
250: Label l;
251: GridBagLayout gbl = new GridBagLayout();
252: GridBagConstraints gbc = new GridBagConstraints();
253: Panel p = new Panel(gbl);
254: p.setForeground(new Color(0, 0, 128));
255: pwidget = new ScrollPane();
256: gbc.fill = GridBagConstraints.HORIZONTAL;
257: gbc.weightx = 0;
258: gbc.weighty = 0;
259: for (int i = 0; i < a.length; i++) {
260: if (a[i] != null) {
261: PropertyManager pm = PropertyManager
262: .getPropertyManager();
263: Properties attrProps = pm.getAttributeProperties(rrw,
264: a[i].getAttribute());
265: String labelText = (String) attrProps.get("label");
266: if (labelText == null)
267: labelText = a[i].getName();
268: l = new Label(labelText, Label.RIGHT);
269: ae[i] = AttributeEditorFactory.getEditor(rrw, a[i]
270: .getAttribute());
271: authorized = false;
272: while (!authorized) {
273: try {
274: authorized = true;
275: ae[i].initialize(rrw, a[i].getAttribute(), a[i]
276: .getValue(), attrProps);
277: } catch (RemoteAccessException ex) {
278: if (ex.getMessage().equals("Unauthorized")) {
279: authorized = false;
280: } else {
281: throw ex;
282: }
283: } finally {
284: if (!authorized) {
285: rrw.getBrowser().popupDialog("admin");
286: }
287: }
288: }
289: gbc.gridwidth = 1;
290: gbl.setConstraints(l, gbc);
291: p.add(l);
292: gbc.gridwidth = GridBagConstraints.REMAINDER;
293: gbl.setConstraints(ae[i].getComponent(), gbc);
294: p.add(ae[i].getComponent());
295: }
296: }
297: pwidget.add(p);
298: widget.add("Center", pwidget);
299: // Now add the reset/commit button bar
300:
301: Panel toolpane = new Panel(new BorderLayout());
302: Button commitb = new Button(COMMIT_L);
303: Button resetb = new Button(RESET_L);
304:
305: MouseButtonListener mbl = new MouseButtonListener();
306: commitb.addMouseListener(mbl);
307: resetb.addMouseListener(mbl);
308:
309: ButtonBarListener bbl = new ButtonBarListener();
310: commitb.addActionListener(bbl);
311: resetb.addActionListener(bbl);
312:
313: message = new Label("", Label.CENTER);
314: message.setForeground(Color.white);
315: message.setBackground(Color.gray);
316:
317: BorderPanel pmsg = new BorderPanel(BorderPanel.IN, 2);
318: pmsg.setLayout(new BorderLayout());
319: pmsg.add("Center", message);
320:
321: toolpane.add("West", commitb);
322: toolpane.add("Center", pmsg);
323: toolpane.add("East", resetb);
324:
325: widget.add("South", toolpane);
326: // add information about the class of the resource edited
327:
328: String classes[] = { "" };
329: try {
330: classes = rr.getClassHierarchy();
331: } catch (RemoteAccessException ex) {
332: // big trouble but it may be temporary and this information
333: // is not vital, so just warn
334: ex.printStackTrace();
335: }
336: l = new Label("Class: " + classes[0], Label.CENTER);
337: l.setForeground(new Color(0, 0, 128));
338: widget.add("North", l);
339: initialized = true;
340: }
341: }
|