001: package com.xoetrope.swing;
002:
003: import com.xoetrope.carousel.build.BuildProperties;
004: import java.awt.event.ActionEvent;
005: import java.awt.event.ActionListener;
006:
007: import net.xoetrope.swing.XImage;
008: import com.xoetrope.event.XClickListener;
009: import java.awt.Component;
010: import java.lang.reflect.Method;
011: import net.xoetrope.xui.XAttributedComponent;
012: import net.xoetrope.xui.XProjectManager;
013:
014: /**
015: * An easter eggs is a special control used to obscure access to certain
016: * functionality. Frequently it is used to control access to superuser mode
017: * menus and actions. In a modal/fullscreen application it may obscure access
018: * to the exit/shutdown functionality. A password can also be added to further
019: * protect access.
020: *
021: * <p> Copyright (c) Xoetrope Ltd., 2001-2006, This software is licensed under
022: * the GNU Public License (GPL), please see license.txt for more details. If
023: * you make commercial use of this software you must purchase a commercial
024: * license from Xoetrope.</p>
025: * <p> $Revision: 1.10 $</p>
026: */
027: public class XEasterEgg extends XImage implements XAttributedComponent,
028: ActionListener {
029: private String password = "";
030: protected XClickListener clickListener;
031:
032: /** The name of the callback method */
033: private String callback;
034:
035: /** The owner of the callback method */
036: private Component callbackParent;
037:
038: /**
039: * Create a new easter egg
040: */
041: public XEasterEgg() {
042: clickListener = new XClickListener(this );
043: if (XProjectManager.getCurrentProject().getAppFrame() != null)
044: addMouseListener(clickListener);
045: clickListener.addActionListener(this );
046: }
047:
048: /**
049: * Set the method to be called back when the password is entered and OK clicked
050: * @param cbParent The parent/owner for purposes of a callback.
051: * @param cb The name of a callback method in the parent (or null) to be invoked when the dialog is dismissed.
052: */
053: public void setCallback(Component cbParent, String cb) {
054: callbackParent = cbParent;
055: callback = cb;
056: }
057:
058: /**
059: * Set one or more attributes of the component.
060: * @param attribName the name of the attribute
061: * @param attribValue the value of the attribute
062: * @return 0 for success, non zero otherwise
063: */
064: public int setAttribute(String attribName, Object attribValue) {
065: if (attribName.compareToIgnoreCase("password") == 0)
066: password = (String) attribValue;
067: else
068: super .setAttribute(attribName, attribValue);
069:
070: return 0;
071: }
072:
073: /**
074: * Pops up a password dialog and exits the survey if the password matches
075: * @param e the action event
076: */
077: public void actionPerformed(ActionEvent e) {
078: XPasswordDlg passwordDlg = new XPasswordDlg();
079: passwordDlg.showDialog(getParent(), null);
080: if (passwordDlg.getPassword().compareTo(password) == 0)
081: doExit();
082: passwordDlg = null;
083: }
084:
085: /**
086: * Set the password
087: * @param pswd the password
088: */
089: public void setPassword(String pswd) {
090: password = pswd;
091: }
092:
093: /**
094: * Get the password
095: * @return the password
096: */
097: public String getPassword() {
098: return password;
099: }
100:
101: /**
102: * Exist the survey and shuts down the application
103: */
104: public void doExit() {
105: if (callback != null) {
106: try {
107: Class params[] = new Class[0];
108: Method m = callbackParent.getClass().getMethod(
109: callback, params);
110: Object args[] = new Object[0];
111: m.invoke(callbackParent, args);
112: } catch (Exception ex) {
113: if (BuildProperties.DEBUG)
114: ex.getCause().printStackTrace();
115: }
116: }
117: }
118: }
|