001: // ResourceEditor.java
002: // $Id: ResourceEditor.java,v 1.26 2000/08/16 21:37:27 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.Canvas;
011: import java.awt.Color;
012: import java.awt.Component;
013: import java.awt.Container;
014: import java.awt.Dimension;
015: import java.awt.Graphics;
016: import java.awt.GridLayout;
017: import java.awt.Image;
018: import java.awt.Label;
019: import java.awt.Panel;
020: import java.awt.Toolkit;
021:
022: import java.awt.event.ActionEvent;
023: import java.awt.event.ActionListener;
024: import java.awt.event.ComponentEvent;
025: import java.awt.event.MouseAdapter;
026: import java.awt.event.MouseEvent;
027:
028: import java.util.EventObject;
029: import java.util.Properties;
030: import java.util.Vector;
031:
032: import org.w3c.jigadm.PropertyManager;
033: import org.w3c.jigadm.RemoteResourceWrapper;
034:
035: import org.w3c.jigadm.events.ResourceChangeEvent;
036: import org.w3c.jigadm.events.ResourceListener;
037:
038: import org.w3c.jigsaw.admin.RemoteAccessException;
039: import org.w3c.jigsaw.admin.RemoteResource;
040:
041: import org.w3c.tools.widgets.BorderPanel;
042: import org.w3c.tools.widgets.MessagePopup;
043:
044: public class ResourceEditor implements ResourceEditorInterface {
045:
046: public static final String UNREMOVABLE_P = "unremovable";
047:
048: class Alert extends Canvas {
049:
050: Image alert = null;
051: int width = 0;
052: int height = 0;
053:
054: public Dimension getMinimumSize() {
055: return new Dimension(width + 8, height + 8);
056: }
057:
058: public Dimension getPreferredSize() {
059: return new Dimension(width + 8, height + 8);
060: }
061:
062: public boolean imageUpdate(Image img, int flaginfo, int x,
063: int y, int width, int height) {
064: initSize();
065: Container parent = getParent();
066: if (parent != null)
067: parent.doLayout();
068: return super
069: .imageUpdate(img, flaginfo, x, y, width, height);
070: }
071:
072: private void initSize() {
073: if (alert != null) {
074: width = alert.getWidth(this );
075: height = alert.getHeight(this );
076: }
077: }
078:
079: public void paint(Graphics g) {
080: if (alert != null)
081: g.drawImage(alert, 0, 0, this );
082: }
083:
084: Alert() {
085: PropertyManager pm = PropertyManager.getPropertyManager();
086: String alertPath = pm.getIconLocation("alert");
087: if (alertPath != null) {
088: alert = Toolkit.getDefaultToolkit().getImage(alertPath);
089: prepareImage(alert, this );
090: }
091: initSize();
092: }
093: }
094:
095: class ButtonBarListener implements ActionListener {
096:
097: class Switcher extends Thread {
098: Button b;
099:
100: public void run() {
101: try {
102: switchHelper(b);
103: } catch (RemoteAccessException ex) {
104: // do fancy things FIXME
105: }
106: }
107:
108: Switcher(Button b) {
109: this .b = b;
110: }
111: }
112:
113: class Deleter extends Thread {
114: public void run() {
115: delete();
116: }
117: }
118:
119: class Reindexer extends Thread {
120: public void run() {
121: reindex();
122: }
123: }
124:
125: public void actionPerformed(ActionEvent ae) {
126: if (ae.getActionCommand().equals(DELETE_L)) {
127: (new Deleter()).start();
128: } else if (ae.getActionCommand().equals(REINDEX_L)) {
129: setMessage("Reindexation started...");
130: (new Reindexer()).start();
131: setMessage("Reindexation done.");
132: } else {
133: (new Switcher((Button) ae.getSource())).start();
134: }
135: }
136: }
137:
138: class MouseButtonListener extends MouseAdapter {
139:
140: public void mouseEntered(MouseEvent e) {
141: Component comp = e.getComponent();
142: if (comp instanceof Button) {
143: String action = ((Button) comp).getActionCommand();
144: if (action.equals(DELETE_L)) {
145: setMessage("Delete this resource!");
146: } else if (action.equals(REINDEX_L)) {
147: setMessage("Reindex the children.");
148: }
149: }
150: }
151:
152: public void mouseExited(MouseEvent e) {
153: setMessage("");
154: }
155: }
156:
157: protected Vector rls = null;
158:
159: Panel target = null;
160: Panel buttonp = null;
161: Panel editorp = null;
162: Button commitb = null;
163: Button resetb;
164: RemoteResource rr = null;
165: RemoteResourceWrapper rrw = null;
166: ResourceHelper rh[] = null;
167: protected Button b[] = null;
168: protected Label message = null;
169: Properties p;
170:
171: protected static final String DELETE_L = "Delete";
172: protected static final String REINDEX_L = "Reindex";
173:
174: public void setMessage(String msg) {
175: message.setText(msg);
176: }
177:
178: public void commitChanges() throws RemoteAccessException {
179: boolean authorized;
180: for (int i = 0; i < rh.length; i++) {
181: if (rh[i].hasChanged()) {
182: authorized = false;
183: while (!authorized) {
184: try {
185: authorized = true;
186: rh[i].commitChanges();
187: } catch (RemoteAccessException ex) {
188: if (ex.getMessage().equals("Unauthorized")) {
189: authorized = false;
190: } else {
191: (new MessagePopup("Exception : ", ex
192: .getMessage())).show();
193: throw ex; // another reason, throw it
194: }
195: } finally {
196: if (!authorized) {
197: rrw.getBrowser().popupDialog("admin");
198: }
199: }
200: }
201: }
202: }
203: }
204:
205: public void resetChanges() throws RemoteAccessException {
206: for (int i = 0; i < rh.length; i++) {
207: if (rh[i].hasChanged()) {
208: rh[i].resetChanges();
209: }
210: }
211: }
212:
213: protected void switchHelper(Button toggled)
214: throws RemoteAccessException {
215: for (int i = 0; i < b.length; i++) {
216: boolean authorized;
217: if (toggled.equals(b[i])) {
218: PropertyManager pm = PropertyManager
219: .getPropertyManager();
220: Properties props = pm.getHelperProperties(rrw, rh[i]
221: .getClass().getName());
222: authorized = false;
223: while (!authorized) {
224: try {
225: authorized = true;
226: rh[i].initialize(rrw, props);
227: } catch (RemoteAccessException ex) {
228: if (ex.getMessage().equals("Unauthorized")) {
229: authorized = false;
230: } else {
231: (new MessagePopup("Exception : ", ex
232: .getMessage())).show();
233: throw ex; // another reason, throw it
234: }
235: } finally {
236: if (!authorized) {
237: rrw.getBrowser().popupDialog("admin");
238: }
239: }
240: }
241: editorp.setVisible(false);
242: editorp.removeAll();
243: editorp.add("Center", rh[i].getComponent());
244: editorp.validate();
245: editorp.setVisible(true);
246: }
247: }
248: }
249:
250: protected void reindex() {
251: boolean authorized = false;
252: while (!authorized) {
253: try {
254: authorized = true;
255: rrw.getResource().reindex(true);
256: } catch (RemoteAccessException ex) {
257: if (ex.getMessage().equals("Unauthorized")) {
258: authorized = false;
259: } else {
260: (new MessagePopup("Exception : ", ex.getMessage()))
261: .show();
262: // must throw a fancy error
263: }
264: } finally {
265: if (!authorized) {
266: rrw.getBrowser().popupDialog("admin");
267: }
268: }
269: }
270: }
271:
272: protected void delete() {
273: boolean authorized = false;
274: while (!authorized) {
275: try {
276: authorized = true;
277: rrw.getResource().delete();
278: } catch (RemoteAccessException ex) {
279: if (ex.getMessage().equals("Unauthorized")) {
280: authorized = false;
281: } else {
282: (new MessagePopup("Exception : ", ex.getMessage()))
283: .show();
284: // must throw a fancy error
285: }
286: } finally {
287: if (!authorized) {
288: rrw.getBrowser().popupDialog("admin");
289: }
290: }
291: }
292: processEvent(new ResourceChangeEvent(rrw.getFatherWrapper(),
293: "deleted", rrw, null));
294: }
295:
296: public boolean hasChanged() {
297: for (int i = 0; i < rh.length; i++) {
298: if (rh[i].hasChanged()) {
299: return true;
300: }
301: }
302: return false;
303: }
304:
305: public void clearChanged() {
306: for (int i = 0; i < rh.length; i++) {
307: if (rh[i].hasChanged()) {
308: rh[i].clearChanged();
309: }
310: }
311: }
312:
313: public RemoteResource getValue() {
314: return rr;
315: }
316:
317: public synchronized void addResourceListener(ResourceListener rl) {
318: if (rls == null)
319: rls = new Vector(2);
320: rls.addElement(rl);
321: }
322:
323: public synchronized void removeResourceListener(ResourceListener rl) {
324: if (rls != null)
325: rls.removeElement(rl);
326: }
327:
328: protected void processEvent(EventObject eo) {
329: Vector rls = null;
330: ResourceListener rl;
331: synchronized (this ) {
332: if ((this .rls != null)
333: && (eo instanceof ResourceChangeEvent)) {
334: rls = (Vector) this .rls.clone();
335: } else {
336: return;
337: }
338: }
339: for (int i = 0; i < rls.size(); i++) {
340: rl = (ResourceListener) rls.elementAt(i);
341: rl.resourceChanged((ResourceChangeEvent) eo);
342: }
343: }
344:
345: /**
346: * initialize this editor
347: * @param rrw The RemoteResourceWrapper
348: * @param pr The properties
349: * @exception RemoteAccessException if a remote access error occurs.
350: */
351: public void initialize(RemoteResourceWrapper rrw, Properties p)
352: throws RemoteAccessException {
353: int num = 0;
354: this .rrw = rrw;
355: rr = rrw.getResource();
356: ButtonBarListener bbl = new ButtonBarListener();
357: this .p = p;
358: target.removeAll();
359: BorderPanel ptarget = new BorderPanel(BorderPanel.OUT, 2);
360: ptarget.setLayout(new BorderLayout());
361: rh = ResourceHelperFactory.getHelpers(rrw);
362:
363: PropertyManager pm = PropertyManager.getPropertyManager();
364: Properties props = pm.getEditorProperties(rrw);
365: if ((props == null)
366: || (props.getProperty(UNREMOVABLE_P, "false")
367: .equalsIgnoreCase("false"))) {
368: MouseButtonListener mbl = new MouseButtonListener();
369: Panel pan = new Panel(new BorderLayout());
370: Button deleteb = new Button(DELETE_L);
371: deleteb.addActionListener(bbl);
372: deleteb.addMouseListener(mbl);
373: pan.add("West", deleteb);
374:
375: message = new Label("", Label.CENTER);
376: message.setBackground(Color.gray);
377: message.setForeground(Color.white);
378:
379: BorderPanel pmsg = new BorderPanel(BorderPanel.IN, 2);
380: pmsg.setLayout(new BorderLayout());
381: pmsg.add(message);
382: pan.add("Center", pmsg);
383:
384: if (rr.isDirectoryResource()) {
385: Button reindexb = new Button(REINDEX_L);
386: reindexb.addActionListener(bbl);
387: reindexb.addMouseListener(mbl);
388: pan.add("East", reindexb);
389: }
390: //target.setVisible(false);
391: ptarget.add("South", pan);
392: }
393: if (rh != null) {
394: //buttonp = new Panel(new GridLayout(1, rh.length+1));
395: buttonp = new BorderPanel(BorderPanel.OUT, 1);
396: buttonp.setLayout(new GridLayout(1, rh.length + 1));
397: b = new Button[rh.length];
398: for (int i = 0; i < rh.length; i++) {
399: b[i] = new Button(rh[i].getTitle());
400: buttonp.add(b[i]);
401: b[i].addActionListener(bbl);
402: if (rh[i].getTitle().equalsIgnoreCase("shortcut"))
403: num = i;
404: }
405: editorp = new Panel(new BorderLayout());
406: //target.setVisible(false);
407: ptarget.add("North", buttonp);
408: ptarget.add("Center", editorp);
409: target.add("Center", ptarget);
410: target.validate();
411: target.setVisible(true);
412: switchHelper(b[num]);
413: } else {
414: target.add("Center", ptarget);
415: target.validate();
416: target.setVisible(true);
417: }
418: }
419:
420: public ResourceEditor(Panel p) {
421: target = p;
422: }
423: }
|