0001: package net.xoetrope.builder.editor;
0002:
0003: import java.io.Reader;
0004: import java.lang.reflect.Method;
0005: import java.util.Enumeration;
0006: import java.util.Hashtable;
0007: import java.util.Vector;
0008:
0009: import java.awt.AWTEvent;
0010: import java.awt.BorderLayout;
0011: import java.awt.Color;
0012: import java.awt.Component;
0013: import java.awt.Container;
0014: import java.awt.Dimension;
0015: import java.awt.Point;
0016: import java.awt.Rectangle;
0017: import java.awt.SystemColor;
0018: import java.awt.event.ActionEvent;
0019: import java.awt.event.ActionListener;
0020: import java.awt.event.ComponentEvent;
0021: import java.awt.event.ComponentListener;
0022: import java.awt.event.FocusEvent;
0023: import java.awt.event.ItemEvent;
0024: import java.awt.event.ItemListener;
0025: import java.awt.event.KeyEvent;
0026: import java.awt.event.KeyListener;
0027: import java.awt.event.MouseEvent;
0028: import java.awt.event.MouseListener;
0029: import java.awt.event.MouseMotionListener;
0030: import java.awt.event.TextEvent;
0031: import javax.swing.AbstractAction;
0032: import javax.swing.BorderFactory;
0033: import javax.swing.ImageIcon;
0034: import javax.swing.JButton;
0035: import javax.swing.JMenu;
0036: import javax.swing.JMenuItem;
0037: import javax.swing.JPanel;
0038: import javax.swing.JPopupMenu;
0039: import javax.swing.JScrollPane;
0040: import javax.swing.JViewport;
0041: import javax.swing.SwingUtilities;
0042: import javax.swing.UIManager;
0043: import javax.swing.border.Border;
0044: import javax.swing.border.EmptyBorder;
0045: import javax.swing.border.LineBorder;
0046: import javax.swing.event.MenuEvent;
0047: import javax.swing.event.MenuListener;
0048: import javax.swing.plaf.ButtonUI;
0049:
0050: import net.xoetrope.builder.editor.components.ComponentHelper;
0051: import net.xoetrope.builder.editor.components.PropertyHelper;
0052: import net.xoetrope.builder.editor.components.swing.XComponentProxy;
0053: import net.xoetrope.builder.editor.dialog.XPageSelectionDialog;
0054: import net.xoetrope.builder.editor.events.ComponentSelectListener;
0055: import net.xoetrope.builder.editor.events.EditorComponentListener;
0056: import net.xoetrope.builder.editor.events.EventHandlerEditor;
0057: import net.xoetrope.builder.editor.events.ProjectListener;
0058: import net.xoetrope.builder.editor.events.SizerListener;
0059: import net.xoetrope.xml.XmlElement;
0060: import net.xoetrope.xml.XmlSource;
0061: import net.xoetrope.xui.XEventHandler;
0062: import net.xoetrope.xui.XLayoutHelper;
0063: import net.xoetrope.xui.XPage;
0064: import net.xoetrope.xui.XPageDisplay;
0065: import net.xoetrope.xui.XPageManager;
0066: import net.xoetrope.xui.XProjectManager;
0067: import net.xoetrope.xui.XTarget;
0068: import net.xoetrope.xui.validation.XValidationHandler;
0069:
0070: /**
0071: * <p>Description: </p>
0072: * <p>Copyright: Copyright (c) Xoetrope Ltd., 1998-2003</p>
0073: * $Revision: 1.20 $
0074: */
0075: public class XPageHolder extends JPanel implements XPageDisplay,
0076: ActionListener, ItemListener, KeyListener, MouseListener,
0077: MouseMotionListener, SizerListener, ProjectListener,
0078: MenuListener, ComponentListener {
0079: protected JPanel pageContainer;
0080: protected XPageResource currentPage;
0081:
0082: private Vector selectedComponents;
0083: private Vector componentClipboard;
0084: private Vector componentSelectionListeners;
0085: private EditorComponentListener editorComponentListener;
0086: private String pendingComponent = null;
0087: private XEditorComponentFactory styleFactory;
0088: private boolean previewMode = false;
0089: private Color borderColor[];
0090: private XComponentTree componentTree;
0091:
0092: private XEditorProject currentProject;
0093:
0094: private XGuidePane guidePane;
0095:
0096: private static final String PAGE_ACTIVATED = "Page Activated";
0097: private static final String PAGE_CREATED = "Page Created";
0098: private static final String PAGE_DEACTIVATED = "Page Deactivated";
0099:
0100: private EventHandlerEditor eventHandlerEditor;
0101: private XEditorEventHandler editorEventHandler;
0102:
0103: public XPageHolder(XComponentTree tree) {
0104: componentTree = tree;
0105:
0106: setLayout(null);
0107: XProjectManager.getPageManager().setPageDisplay(this );
0108: setBackground(SystemColor.control);
0109:
0110: selectedComponents = new Vector();
0111: componentSelectionListeners = new Vector();
0112: pageContainer = new JPanel();
0113: pageContainer.setOpaque(true);
0114: pageContainer.setBorder(new LineBorder(Color.black));
0115: pageContainer.setLocation(10, 10);
0116: pageContainer.setLayout(null);
0117:
0118: guidePane = new XGuidePane();
0119: add(guidePane, 0);
0120: add(pageContainer, 1);
0121:
0122: setOpaque(true);
0123: setDoubleBuffered(true);
0124:
0125: showGuides(true);
0126: addComponentListener(this );
0127: }
0128:
0129: public void addComponentSelectionListener(
0130: ComponentSelectListener listener) {
0131: componentSelectionListeners.add(listener);
0132: }
0133:
0134: public void setEditorComponentListener(
0135: EditorComponentListener listener) {
0136: editorComponentListener = listener;
0137: }
0138:
0139: public void setPage(XPage page) {
0140: if (!(page.getEventHandler() instanceof XEditorEventHandler)
0141: && !previewMode) {
0142: XEditorEventHandler eventHandler = new XEditorEventHandler(
0143: this , page.getEventHandler(), page,
0144: new XValidationHandler(page));
0145: page.setEventHandler(eventHandler);
0146: addListeners(page, eventHandler);
0147: }
0148:
0149: styleFactory = new XEditorComponentFactory(
0150: XPage.XUI_SWING_PACKAGE);
0151: }
0152:
0153: public void addListeners(Container page,
0154: XEditorEventHandler eventHandler) {
0155: editorEventHandler = eventHandler;
0156: page.addMouseListener(eventHandler);
0157: Component comps[] = page.getComponents();
0158: int numChildren = comps.length;
0159: for (int i = 0; i < numChildren; i++) {
0160: comps[i].addKeyListener(eventHandler);
0161: comps[i].addMouseMotionListener(eventHandler);
0162: // The recursive call will add the mouse listener
0163: if ((comps[i] instanceof Container)
0164: && !(comps[i] instanceof XComponentProxy))
0165: addListeners((Container) comps[i], eventHandler);
0166: else
0167: comps[i].addMouseListener(eventHandler);
0168: }
0169: }
0170:
0171: public void removeListeners(Container page,
0172: XEventHandler eventHandler) {
0173: page.removeMouseListener(eventHandler);
0174: Component comps[] = page.getComponents();
0175: int numChildren = comps.length;
0176: for (int i = 0; i < numChildren; i++) {
0177: comps[i].removeKeyListener(eventHandler);
0178: comps[i].removeMouseMotionListener(eventHandler);
0179: // The recursive call will remove the mouse listener
0180: if (comps[i] instanceof Container)
0181: removeListeners((Container) comps[i], eventHandler);
0182: else
0183: comps[i].removeMouseListener(eventHandler);
0184: }
0185: }
0186:
0187: /**
0188: * Adds the event handlers. Events are specified in the XML as having 'type'
0189: * (e.g. MouseHandler, ActionHandler etc...), 'name' (the method name to be
0190: * invoked) and 'target' (the name of the component to which the handler is
0191: * attached) attributes
0192: * @param page The page that contains the response methods
0193: * @param pageResource the object holding the event names
0194: */
0195: protected void setEventListeners(XPage page,
0196: XPageResource pageResource, Container cont) {
0197: String typeStr, methodStr, targetStr;
0198: XEventHandler handler = page.getEventHandler();
0199:
0200: int numChildren = cont.getComponentCount();
0201: for (int i = 0; i < numChildren; i++) {
0202: typeStr = methodStr = targetStr = null;
0203:
0204: Component comp = cont.getComponent(i);
0205: // Couldn't find a way of removing listeners without casting to the specific component type so
0206: // took the route of not re-adding listeners if one of that type is already present.
0207: // The listeners will eventually build up to cover all components which may not
0208: // accurately reflect the runtime system, but it should have little effect if the
0209: // appropriate handlers are defined
0210: String methodName = checkMethod(pageResource, handler,
0211: comp, AWTEvent.MOUSE_EVENT_MASK);
0212: if (methodName != null)
0213: page.addMouseHandler(comp, methodName);
0214:
0215: methodName = checkMethod(pageResource, handler, comp,
0216: AWTEvent.MOUSE_MOTION_EVENT_MASK);
0217: if (methodName != null)
0218: handler.addMouseMotionHandler(comp, methodName);
0219:
0220: methodName = checkMethod(pageResource, handler, comp,
0221: AWTEvent.ACTION_EVENT_MASK);
0222: if (methodName != null)
0223: handler.addActionHandler(comp, methodName);
0224:
0225: methodName = checkMethod(pageResource, handler, comp,
0226: AWTEvent.FOCUS_EVENT_MASK);
0227: if (methodName != null)
0228: handler.addFocusHandler(comp, methodName);
0229:
0230: methodName = checkMethod(pageResource, handler, comp,
0231: AWTEvent.ITEM_EVENT_MASK);
0232: if (methodName != null)
0233: handler.addItemHandler(comp, methodName);
0234:
0235: methodName = checkMethod(pageResource, handler, comp,
0236: AWTEvent.KEY_EVENT_MASK);
0237: if (methodName != null)
0238: handler.addKeyHandler(comp, methodName);
0239:
0240: methodName = checkMethod(pageResource, handler, comp,
0241: AWTEvent.TEXT_EVENT_MASK);
0242: if (methodName != null)
0243: handler.addTextHandler(comp, methodName);
0244:
0245: if (comp instanceof Container)
0246: setEventListeners(page, pageResource, (Container) comp);
0247: }
0248: }
0249:
0250: /**
0251: * Adds the event handlers. Events are specified in the XML as having 'type'
0252: * (e.g. MouseHandler, ActionHandler etc...), 'name' (the method name to be
0253: * invoked) and 'target' (the name of the component to which the handler is
0254: * attached) attributes
0255: * @param page The page that contains the response methods
0256: * @param pageResource the object holding the event names
0257: */
0258: protected void removeEventListeners(XPageResource pageResource,
0259: XEditorEventHandler handler, Container cont) {
0260: String typeStr, methodStr, targetStr;
0261:
0262: int numChildren = cont.getComponentCount();
0263: for (int i = 0; i < numChildren; i++) {
0264: typeStr = methodStr = targetStr = null;
0265:
0266: Component comp = cont.getComponent(i);
0267: String methodName = checkMethod(pageResource, handler,
0268: comp, AWTEvent.MOUSE_EVENT_MASK);
0269: if (methodName != null)
0270: handler.removeMouseHandler(comp, methodName);
0271:
0272: methodName = checkMethod(pageResource, handler, comp,
0273: AWTEvent.MOUSE_MOTION_EVENT_MASK);
0274: if (methodName != null)
0275: handler.removeMouseMotionHandler(comp, methodName);
0276:
0277: methodName = checkMethod(pageResource, handler, comp,
0278: AWTEvent.ACTION_EVENT_MASK);
0279: if (methodName != null)
0280: handler.removeActionHandler(comp, methodName);
0281:
0282: methodName = checkMethod(pageResource, handler, comp,
0283: AWTEvent.FOCUS_EVENT_MASK);
0284: if (methodName != null)
0285: handler.removeFocusHandler(comp, methodName);
0286:
0287: methodName = checkMethod(pageResource, handler, comp,
0288: AWTEvent.ITEM_EVENT_MASK);
0289: if (methodName != null)
0290: handler.removeItemHandler(comp, methodName);
0291:
0292: methodName = checkMethod(pageResource, handler, comp,
0293: AWTEvent.KEY_EVENT_MASK);
0294: if (methodName != null)
0295: handler.removeKeyHandler(comp, methodName);
0296:
0297: methodName = checkMethod(pageResource, handler, comp,
0298: AWTEvent.TEXT_EVENT_MASK);
0299: if (methodName != null)
0300: handler.removeTextHandler(comp, methodName);
0301:
0302: if (comp instanceof Container)
0303: removeEventListeners(pageResource, handler,
0304: (Container) comp);
0305: }
0306: }
0307:
0308: /**
0309: * Check whther a method has already been added under this name
0310: * @param pageResource the page resource container
0311: * @param handler the event handler for this page
0312: * @param comp the component to test
0313: * @param mask the event mack
0314: * @return the method name if it isn't already in use for the component
0315: */
0316: private String checkMethod(XPageResource pageResource,
0317: XEventHandler handler, Component comp, long mask) {
0318: String methodName = pageResource.getCtlEvent(comp, mask);
0319: if (methodName != null) {
0320: Method method = handler.findEvent(comp, mask);
0321: if ((method == null)
0322: || !method.getName().equals(methodName))
0323: return methodName;
0324: }
0325: return null;
0326: }
0327:
0328: //----------------------------------------------------------------------------
0329:
0330: /**
0331: * Toggle the preview mode. In preview mode the application can be executed whereas
0332: * in Edit mode the application can be modified.
0333: */
0334: protected void togglePreview()
0335: {
0336: Hashtable pages = currentProject.getPageResources();
0337: Enumeration enum = pages.keys();
0338:
0339: while ( enum.hasMoreElements()) {
0340: XPageResource comp = (XPageResource)pages.get( enum.nextElement());
0341: if ( comp != null ) {
0342: XPageResource targetResource;
0343: if ( comp instanceof XPageResource )
0344: targetResource = (XPageResource)comp;
0345: else
0346: continue;
0347:
0348: XPage page = targetResource.getPage();
0349: if ( !( page.getEventHandler() instanceof XEditorEventHandler ) ) {
0350: XEditorEventHandler eventHandler = targetResource.getEditorEventHandler();
0351: if ( eventHandler == null ) {
0352: XValidationHandler validationHandler = new XValidationHandler( targetResource );
0353: XEventHandler xuiEventHandler = page.getEventHandler();
0354: eventHandler = new XEditorEventHandler( this , xuiEventHandler, targetResource, validationHandler );
0355: }
0356: page.setEventHandler( eventHandler );
0357: removeEventListeners( targetResource, eventHandler, page );
0358: addListeners( targetResource, eventHandler );
0359:
0360: previewMode = false;
0361: }
0362: else {
0363: XEditorEventHandler editorEventHandler = ( XEditorEventHandler )page.getEventHandler();
0364: targetResource.setEditorEventHandler( editorEventHandler );
0365: page.setEventHandler( editorEventHandler.getOriginalEventHandler() );
0366: removeListeners( targetResource, editorEventHandler );
0367:
0368: // Add the listeners.
0369: setEventListeners( page, targetResource, page );
0370: previewMode = true;
0371: }
0372: }
0373: }
0374:
0375: // Set the current page in the history
0376: if ( previewMode ) {
0377: Container contentPanel = findTarget( null );
0378: XProjectManager.getPageManager().addHistory( ((XPageResource)contentPanel.getComponent(0)).getPage().getName() );
0379: }
0380: }
0381:
0382: /**
0383: * Called when a page has been added with the XPageManager
0384: * @param page The XPage which has been loaded.
0385: */
0386: public XPage displayPage(XPage page) {
0387: return displayPage(page, null);
0388: }
0389:
0390: /**
0391: * Called when a page has been added with the XPageManager
0392: * @param page The XPage which has been loaded.
0393: */
0394: public XPage displayPage(XPage page, String target) {
0395: Container targetContainer = findTarget(pageContainer, target);
0396:
0397: if (!(page.getEventHandler() instanceof XEditorEventHandler)
0398: && !previewMode) {
0399: XEditorEventHandler eventHandler = new XEditorEventHandler(
0400: this , page.getEventHandler(), page,
0401: new XValidationHandler(page));
0402: page.setEventHandler(eventHandler);
0403: addListeners(page, eventHandler);
0404: }
0405:
0406: page.setSize(new Dimension(XEditorProjectManager
0407: .getCurrentProject()
0408: .getStartupParamAsInt("ClientWidth"),
0409: XEditorProjectManager.getCurrentProject()
0410: .getStartupParamAsInt("ClientHeight")));
0411: page.setLocation(10, 10);
0412:
0413: page.setVisible(true);
0414: XPageResource lastResource = ((XPageResource) targetContainer
0415: .getComponent(0));
0416: XPage lastPage = lastResource.getPage();
0417:
0418: if (page != lastPage) {
0419: XPageResource targetResource = currentProject
0420: .getPageResource(page.getName());
0421: targetResource.getPage();
0422: targetContainer.add(targetResource);
0423:
0424: if (lastPage != null) {
0425: lastPage.saveBoundComponentValues();
0426: targetContainer.remove(lastResource);
0427: page.setStatus(XPage.DEACTIVATED);
0428: page.pageDeactivated();
0429: }
0430:
0431: if (previewMode) {
0432: XEventHandler editorEventHandler = page
0433: .getEventHandler();
0434: if (editorEventHandler instanceof XEditorEventHandler) {
0435: page
0436: .setEventHandler(((XEditorEventHandler) editorEventHandler)
0437: .getOriginalEventHandler());
0438: removeListeners(page,
0439: (XEditorEventHandler) editorEventHandler);
0440:
0441: // Add the listeners.
0442: setEventListeners(page, targetResource, page);
0443: targetResource
0444: .setEditorEventHandler((XEditorEventHandler) editorEventHandler);
0445: }
0446: }
0447:
0448: page.updateBoundComponentValues();
0449:
0450: targetResource.doLayout();
0451: targetContainer.doLayout();
0452: targetResource.doLayout();
0453: repaint();
0454: repaintGuides();
0455: }
0456: page.updateBoundComponentValues();
0457:
0458: pageContainer.doLayout();
0459: page.doLayout();
0460: page.setStatus(XPage.ACTIVATED);
0461: page.pageActivated();
0462: System.gc();
0463: return page;
0464: }
0465:
0466: /**
0467: * Add a new frame to a frameset
0468: * @param name the frame name
0469: * @param constraint the BorderlayoutConstraint
0470: * @param preferredWidth the preferred width
0471: * @param preferredHeight the preferred height
0472: */
0473: public void addTarget(String name, Object constraint,
0474: int preferredWidth, int preferredHeight) {
0475: XTarget container = new XTarget(name, preferredWidth,
0476: preferredHeight);
0477: pageContainer.add(container, constraint);
0478: }
0479:
0480: /**
0481: * Adds a frame to the frameset
0482: * @param frame the new traget frame
0483: */
0484: public void addTarget(XTarget frame, Object constraint) {
0485: pageContainer.add(frame, constraint);
0486: }
0487:
0488: /**
0489: * Finds the target for a page display request
0490: * @param container the root container
0491: * @param target the target area
0492: * @return the container to be updated
0493: */
0494: protected Container findTarget(Container container, String target) {
0495: Container targetContainer = container;
0496: if ((target == null) || (target.compareTo("content") == 0))
0497: return container;
0498:
0499: Component[] comps = container.getComponents();
0500: int numChildren = comps.length;
0501: for (int i = 0; i < numChildren; i++) {
0502: if (comps[i].getName().compareTo(target) == 0)
0503: return (Container) comps[i];
0504: }
0505: return container;
0506: }
0507:
0508: public void menuCanceled(MenuEvent e) {
0509: }
0510:
0511: public void menuDeselected(MenuEvent e) {
0512: }
0513:
0514: public void menuSelected(MenuEvent e) {
0515: boolean hasPasteItems = (componentClipboard != null);
0516: boolean hasSelection = (selectedComponents.size() > 0);
0517: if (hasSelection) {
0518: if (((XComponentSizer) selectedComponents.elementAt(0))
0519: .getTarget() instanceof XPage)
0520: hasSelection = false;
0521: }
0522: ((XEditorMenu) ((JMenu) e.getSource()).getParent())
0523: .initEditMenu(hasSelection, hasSelection,
0524: hasPasteItems, hasSelection);
0525: }
0526:
0527: public boolean hasFrames() {
0528: boolean hasFrames = pageContainer.getComponentCount() > 1;
0529: return hasFrames;
0530: }
0531:
0532: public boolean hasPageBorders() {
0533: Container contentPanel = findTarget(null);
0534: if (contentPanel.getComponentCount() > 0) {
0535: XPageResource pageRes = (XPageResource) contentPanel
0536: .getComponent(0);
0537: Border border = pageRes.getBorder();
0538: boolean hasPageBorders = ((border != null) && !(border instanceof EmptyBorder));
0539: return hasPageBorders;
0540: }
0541: return false;
0542: }
0543:
0544: public boolean hasGuides() {
0545: return guidePane.getPaintGuides();
0546: }
0547:
0548: public boolean isAddingComponent() {
0549: return (pendingComponent != null);
0550: }
0551:
0552: /**
0553: * Call copy which adds all of the selected components to the componentClipboard
0554: * Vector. Then remove the selected components to the pageHolder.
0555: */
0556: public void cut() {
0557: copy();
0558:
0559: int numChildren = selectedComponents.size();
0560: for (int i = 0; i < numChildren; i++) {
0561: XComponentSizer comp = (XComponentSizer) selectedComponents
0562: .elementAt(i);
0563: comp.getParent().remove(comp);
0564: comp.getTarget().getParent().remove(comp.getTarget());
0565: }
0566: selectedComponents.clear();
0567:
0568: // Hide all properties in the properties dialog.
0569: componentSized();
0570: }
0571:
0572: /**
0573: * Add all of the selected components to the componentClipboard Vector.
0574: */
0575: public void copy() {
0576: componentClipboard = new Vector();
0577: int numChildren = selectedComponents.size();
0578: for (int i = 0; i < numChildren; i++)
0579: componentClipboard
0580: .addElement((XComponentSizer) selectedComponents
0581: .elementAt(i));
0582: }
0583:
0584: /**
0585: * first check that the componentClipboard is not null and then effectively clone
0586: * each component found in it. This is done by creating the basic component using
0587: * the styleFactory. This creates the new component with the basic properties
0588: * after which the component helper is called to set component specific properties.
0589: * Add the keyListener and mouseListener for selecting and moving the new component.
0590: */
0591: public void paste() {
0592: Container parent = null;
0593: if (selectedComponents.size() > 0)
0594: parent = (Container) ((XComponentSizer) selectedComponents
0595: .elementAt(0)).getTarget();
0596:
0597: if (parent != null && componentClipboard != null) {
0598: int offsetX, offsetY;
0599: offsetX = offsetY = 10000;
0600: int numChildren = componentClipboard.size();
0601: for (int i = 0; i < numChildren; i++) {
0602: Component comp = ((XComponentSizer) componentClipboard
0603: .elementAt(i)).getTarget();
0604: offsetX = Math.min(offsetX, comp.getX());
0605: offsetY = Math.min(offsetY, comp.getY());
0606: if (comp instanceof XComponentProxy) {
0607: comp = ((XComponentProxy) comp)
0608: .getProxiedComponent();
0609: }
0610: }
0611:
0612: // if the current selected component is a container add the pasted components to it
0613: Component currentSelected = ((XComponentSizer) this
0614: .getSelectedComponents().elementAt(0)).getTarget();
0615: for (int i = 0; i < numChildren; i++) {
0616: Component comp = ((XComponentSizer) componentClipboard
0617: .elementAt(i)).getTarget();
0618: if (currentSelected.getClass().getName().indexOf(
0619: "XPanelProxy") > 0)
0620: styleFactory.setParentComponent(currentSelected);
0621: else
0622: styleFactory.setParentComponent(currentPage
0623: .getPage());
0624:
0625: Component newCompType = comp;
0626: if (newCompType instanceof XComponentProxy) {
0627: newCompType = ((XComponentProxy) comp)
0628: .getProxiedComponent();
0629: }
0630: Component newComp = styleFactory.addComponent(
0631: newCompType.getClass().getName(), 0, 0,
0632: newCompType.getWidth(),
0633: newCompType.getHeight(), currentPage
0634: .getStyleName(newCompType));
0635: PropertyHelper helper = ComponentHelper
0636: .getPropertyHelper(comp);
0637: helper.setCopiedProperties(currentPage, comp, newComp);
0638: currentPage.setStyleName(newComp, currentPage
0639: .getStyleName(comp));
0640: newComp.addKeyListener(editorEventHandler);
0641: newComp.addMouseListener(editorEventHandler);
0642: newComp.setLocation(comp.getX() - offsetX, comp.getY()
0643: - offsetY);
0644: selectComponent(newComp, i == 0 ? true : false);
0645: }
0646:
0647: // Hide all properties in the properties dialog.
0648: componentSized();
0649: }
0650: }
0651:
0652: /**
0653: * Check that there are items to paste
0654: * @return true if there are items to paste
0655: */
0656: public boolean hasPasteContent() {
0657: if ((componentClipboard != null)
0658: && (componentClipboard.size() > 0))
0659: return true;
0660:
0661: return false;
0662: }
0663:
0664: /**
0665: * Delete the selected items
0666: */
0667: public void deleteSelection() {
0668: int numChildren = selectedComponents.size();
0669: for (int i = 0; i < numChildren; i++) {
0670: XComponentSizer comp = (XComponentSizer) selectedComponents
0671: .elementAt(i);
0672: comp.remove();
0673: comp.getTarget().getParent().remove(comp.getTarget());
0674: }
0675: selectedComponents.clear();
0676:
0677: // Hide all properties in the properties dialog.
0678: componentSized();
0679: }
0680:
0681: public void getContextMenu(JPopupMenu popupMenu, AWTEvent evt) {
0682: if (selectedComponents != null) {
0683: if (evt.getSource() instanceof XPage) {
0684: JMenu eventMenu = new JMenu("Edit page event");
0685: popupMenu.add(eventMenu);
0686:
0687: addSubMenu(eventMenu, PAGE_ACTIVATED);
0688: addSubMenu(eventMenu, PAGE_CREATED);
0689: addSubMenu(eventMenu, PAGE_DEACTIVATED);
0690: } else {
0691: JMenuItem mi = new JMenuItem("Copy", KeyEvent.VK_D);
0692: mi.addActionListener(this );
0693: popupMenu.add(mi);
0694: mi = new JMenuItem("Cut", KeyEvent.VK_T);
0695: mi.addActionListener(this );
0696: popupMenu.add(mi);
0697: mi = new JMenuItem("Delete", KeyEvent.VK_D);
0698: mi.addActionListener(this );
0699: popupMenu.add(mi);
0700: if (selectedComponents.size() == 1) {
0701: popupMenu.addSeparator();
0702: XComponentSizer sizer = ((XComponentSizer) selectedComponents
0703: .elementAt(0));
0704: Component target = sizer.getTarget();
0705: if (isClickable(target)) {
0706: JMenu contextMenu = new JMenu("Navigation");
0707: contextMenu.setFont(XuiDefaults.defaultFont);
0708:
0709: NavigateAction navigateAction = new NavigateAction(
0710: "Go to page...",
0711: null,
0712: "Pick the page to show when this button is clicked",
0713: new Integer(KeyEvent.VK_G));
0714: JMenuItem navigateItem = new JMenuItem(
0715: navigateAction);
0716: navigateItem.setFont(XuiDefaults.defaultFont);
0717: contextMenu.add(navigateItem);
0718:
0719: contextMenu.addSeparator();
0720:
0721: navigateAction = new NavigateAction(
0722: "Home",
0723: null,
0724: "Show the 'Home' page when this button is clicked",
0725: new Integer(KeyEvent.VK_H));
0726: navigateItem = new JMenuItem(navigateAction);
0727: navigateItem.setFont(XuiDefaults.defaultFont);
0728: contextMenu.add(navigateItem);
0729:
0730: navigateAction = new NavigateAction(
0731: "Previous",
0732: null,
0733: "Go to the previous page when this button is clicked",
0734: new Integer(KeyEvent.VK_P));
0735: navigateItem = new JMenuItem(navigateAction);
0736: navigateItem.setFont(XuiDefaults.defaultFont);
0737: contextMenu.add(navigateItem);
0738:
0739: popupMenu.add(contextMenu);
0740: }
0741: }
0742: }
0743: }
0744: }
0745:
0746: /**
0747: * Check to see if the target object can be used for page navigation. For this
0748: * it must have a mouse interface or an action interface
0749: * @param target the selected object
0750: * @return true if the object can be used
0751: */
0752: private boolean isClickable(Component target) {
0753: boolean clickable = false;
0754: Class params[] = new Class[1];
0755: try {
0756: params[0] = java.awt.event.ActionListener.class;
0757: Method m = target.getClass().getMethod("addActionListener",
0758: params);
0759: clickable = (m != null);
0760: } catch (Exception ex) {
0761: // We are not interested in the exception as the method is not available or useable
0762: // for out intended purpose
0763: }
0764:
0765: if (!clickable) {
0766: try {
0767: params[0] = java.awt.event.MouseListener.class;
0768: Method m = target.getClass().getMethod(
0769: "addMouseListener", params);
0770: clickable = (m != null);
0771: } catch (Exception ex) {
0772: // We are not interested in this exception either
0773: }
0774: }
0775:
0776: return clickable;
0777: }
0778:
0779: /**
0780: * Respond to events such as menu selections
0781: * @param e
0782: */
0783: public void actionPerformed(ActionEvent e) {
0784: if (e.getActionCommand().equals("Cut"))
0785: cut();
0786: else if (e.getActionCommand().equals("Copy"))
0787: copy();
0788: else if (e.getActionCommand().equals("Delete"))
0789: deleteSelection();
0790: else if (e.getActionCommand().equals(PAGE_ACTIVATED))
0791: eventHandlerEditor.showPageEventHandler(currentPage,
0792: "pageActivated");
0793: else if (e.getActionCommand().equals(PAGE_CREATED))
0794: eventHandlerEditor.showPageEventHandler(currentPage,
0795: "pageCreated");
0796: else if (e.getActionCommand().equals(PAGE_DEACTIVATED))
0797: eventHandlerEditor.showPageEventHandler(currentPage,
0798: "pageDeactivated");
0799: }
0800:
0801: public void itemStateChanged(ItemEvent e) {
0802: }
0803:
0804: public void focusGained(FocusEvent e) {
0805: }
0806:
0807: public void focusLost(FocusEvent e) {
0808: }
0809:
0810: public void textValueChanged(TextEvent e) {
0811: }
0812:
0813: public void keyPressed(KeyEvent e) {
0814: int numComponents = selectedComponents.size();
0815: for (int i = 0; i < numComponents; i++) {
0816: XComponentSizer currentSizer = (XComponentSizer) selectedComponents
0817: .elementAt(i);
0818: int keyCode = e.getKeyCode();
0819: Rectangle r = currentSizer.getBounds();
0820:
0821: if ((e.getModifiers() & KeyEvent.CTRL_MASK) != 0) {
0822: // Size
0823: if ((keyCode == e.VK_UP)
0824: || (keyCode == KeyEvent.VK_KP_UP))
0825: r.height--;
0826: else if ((keyCode == e.VK_DOWN)
0827: || (keyCode == KeyEvent.VK_KP_DOWN))
0828: r.height++;
0829: else if ((keyCode == e.VK_LEFT)
0830: || (keyCode == KeyEvent.VK_KP_LEFT))
0831: r.width--;
0832: else if ((keyCode == e.VK_RIGHT)
0833: || (keyCode == KeyEvent.VK_KP_RIGHT))
0834: r.width++;
0835: else
0836: return;
0837: } else {
0838: // Location
0839: if ((keyCode == e.VK_UP)
0840: || (keyCode == KeyEvent.VK_KP_UP))
0841: r.y--;
0842: else if ((keyCode == e.VK_DOWN)
0843: || (keyCode == KeyEvent.VK_KP_DOWN))
0844: r.y++;
0845: else if ((keyCode == e.VK_LEFT)
0846: || (keyCode == KeyEvent.VK_KP_LEFT))
0847: r.x--;
0848: else if ((keyCode == e.VK_RIGHT)
0849: || (keyCode == KeyEvent.VK_KP_RIGHT))
0850: r.x++;
0851: else
0852: return;
0853: }
0854:
0855: currentSizer.adjustBounds(r);
0856: }
0857: }
0858:
0859: public void keyReleased(KeyEvent e) {
0860: }
0861:
0862: public void keyTyped(KeyEvent e) {
0863: }
0864:
0865: public Component addNewComponent(Component comp, Point pt) {
0866: Container cont = null;
0867: if (comp instanceof Container)
0868: cont = (Container) comp;
0869: else
0870: cont = (Container) comp.getParent();
0871: styleFactory.setParentComponent(cont);
0872:
0873: Component c = null;
0874: if (pendingComponent != null)
0875: c = styleFactory.addComponent(pendingComponent, pt.x, pt.y,
0876: 100, 20, null);
0877:
0878: if (c != null) {
0879: c.addMouseListener(this );
0880: c.addKeyListener(this );
0881: c.addMouseMotionListener(this );
0882: editorComponentListener.componentAdded();
0883: editorComponentListener.setNewComponent(c);
0884: }
0885: pendingComponent = null;
0886:
0887: return c;
0888: }
0889:
0890: /**
0891: * Change the component alignment
0892: * @param command the menu id used to invoke the option
0893: */
0894: public void alignSelectedComponents(int command) {
0895: int numComponents = selectedComponents.size();
0896: if (numComponents < 2)
0897: return;
0898:
0899: // Set the initial value
0900: int newValue = 0;
0901: int newValue2 = 0;
0902: switch (command) {
0903: case XEditorMenu.ID_MENU_ALIGN_LEFT:
0904: case XEditorMenu.ID_MENU_ALIGN_TOP:
0905: newValue = Integer.MAX_VALUE;
0906: break;
0907: case XEditorMenu.ID_MENU_ALIGN_RIGHT:
0908: case XEditorMenu.ID_MENU_ALIGN_BOTTOM:
0909: newValue = Integer.MIN_VALUE;
0910: break;
0911: case XEditorMenu.ID_MENU_ALIGN_CENTER:
0912: case XEditorMenu.ID_MENU_JUSTIFY_HORZ:
0913: newValue = Integer.MAX_VALUE;
0914: newValue2 = -Integer.MIN_VALUE;
0915: break;
0916: case XEditorMenu.ID_MENU_JUSTIFY_VERT:
0917: newValue = Integer.MAX_VALUE;
0918: newValue2 = -Integer.MIN_VALUE;
0919: break;
0920: }
0921:
0922: // Now get the value to match
0923: for (int i = 0; i < numComponents; i++) {
0924: XComponentSizer currentSizer = (XComponentSizer) selectedComponents
0925: .elementAt(i);
0926: Point location = currentSizer.getLocation();
0927: // location = SwingUtilities.convertPoint( currentSizer.getParent(), location, currentSizer.getTarget().getParent());
0928: switch (command) {
0929: case XEditorMenu.ID_MENU_ALIGN_LEFT:
0930: newValue = Math.min(newValue, location.x);
0931: break;
0932: case XEditorMenu.ID_MENU_ALIGN_TOP:
0933: newValue = Math.min(newValue, location.y);
0934: break;
0935: case XEditorMenu.ID_MENU_ALIGN_RIGHT:
0936: newValue = Math.max(newValue, location.x
0937: + currentSizer.getSize().width);
0938: break;
0939: case XEditorMenu.ID_MENU_ALIGN_BOTTOM:
0940: newValue = Math.max(newValue, location.y
0941: + currentSizer.getSize().height);
0942: break;
0943: case XEditorMenu.ID_MENU_ALIGN_CENTER:
0944: case XEditorMenu.ID_MENU_JUSTIFY_HORZ:
0945: newValue = Math.min(newValue, location.x);
0946: newValue2 = Math.max(newValue, location.x
0947: + currentSizer.getSize().width);
0948: break;
0949: case XEditorMenu.ID_MENU_JUSTIFY_VERT:
0950: newValue = Math.min(newValue, location.y);
0951: newValue2 = Math.max(newValue, location.y
0952: + currentSizer.getSize().height);
0953: break;
0954: }
0955: }
0956:
0957: // Now apply the new value
0958: for (int i = 0; i < numComponents; i++) {
0959: XComponentSizer currentSizer = (XComponentSizer) selectedComponents
0960: .elementAt(i);
0961: Point location = currentSizer.getLocation();
0962: // location = SwingUtilities.convertPoint( currentSizer.getParent(), location, currentSizer.getTarget().getParent());
0963: switch (command) {
0964: case XEditorMenu.ID_MENU_ALIGN_LEFT:
0965: currentSizer.setLocation(newValue, location.y);
0966: break;
0967: case XEditorMenu.ID_MENU_ALIGN_TOP:
0968: currentSizer.setLocation(location.x, newValue);
0969: break;
0970: case XEditorMenu.ID_MENU_ALIGN_RIGHT:
0971: currentSizer.setLocation(newValue
0972: - currentSizer.getSize().width, location.y);
0973: break;
0974: case XEditorMenu.ID_MENU_ALIGN_BOTTOM:
0975: currentSizer.setLocation(location.x, newValue
0976: - currentSizer.getSize().height);
0977: break;
0978: case XEditorMenu.ID_MENU_ALIGN_CENTER:
0979: currentSizer
0980: .setLocation(
0981: (newValue + newValue2 - currentSizer
0982: .getSize().width) / 2,
0983: location.y);
0984: break;
0985: case XEditorMenu.ID_MENU_JUSTIFY_HORZ:
0986: currentSizer.setLocation(newValue, location.y);
0987: currentSizer.setSize(newValue2,
0988: currentSizer.getSize().height);
0989: break;
0990:
0991: case XEditorMenu.ID_MENU_JUSTIFY_VERT:
0992: currentSizer.setLocation(location.x, newValue);
0993: currentSizer.setSize(currentSizer.getSize().width,
0994: newValue2);
0995: break;
0996:
0997: default:
0998: continue;
0999: }
1000: currentSizer.repositionTarget();
1001: }
1002: }
1003:
1004: /**
1005: * Update the state to reflect changes to the components
1006: */
1007: public void componentSized() {
1008: guidePane.alignComponents(selectedComponents);
1009: int numListeners = componentSelectionListeners.size();
1010: for (int i = 0; i < numListeners; i++)
1011: ((ComponentSelectListener) componentSelectionListeners
1012: .elementAt(i))
1013: .setSelectedComponents(selectedComponents);
1014:
1015: repaintGuides();
1016: }
1017:
1018: private void repaintGuides() {
1019: SwingUtilities.invokeLater(new Runnable() {
1020: public void run() {
1021: guidePane.setVisible(false);
1022: guidePane.setVisible(true);
1023: }
1024: });
1025: }
1026:
1027: public void toolbarClicked(String comp) {
1028: pendingComponent = comp;
1029: }
1030:
1031: public void mouseExited(MouseEvent evt) {
1032: }
1033:
1034: public void mouseEntered(MouseEvent evt) {
1035: }
1036:
1037: public void mouseReleased(MouseEvent evt) {
1038: Object evtSrc = evt.getSource();
1039: if (evtSrc instanceof XPageHolder)
1040: return;
1041:
1042: XComponentSizer currentSizer = null;
1043: int numSizers = selectedComponents.size();
1044: for (int i = 0; i < numSizers; i++) {
1045: Component comp = ((XComponentSizer) selectedComponents
1046: .elementAt(i)).getTarget();
1047: if (comp != null)
1048: repaintGuides();
1049: }
1050:
1051: if ((evt.getModifiers() & evt.CTRL_MASK) == 0) {
1052: for (int i = 0; i < numSizers; i++)
1053: ((XComponentSizer) selectedComponents.elementAt(i))
1054: .remove();
1055: selectedComponents.clear();
1056: }
1057:
1058: Component selectedComponent = null;
1059: boolean newComponentAdded = false;
1060: Component sourceComponent = (Component) evtSrc;
1061: if (sourceComponent instanceof JViewport)
1062: sourceComponent = sourceComponent.getParent();
1063: PropertyHelper helper = ComponentHelper
1064: .getPropertyHelper(sourceComponent);
1065: if (pendingComponent != null) {
1066: if (helper.allowsChildren()) {
1067: if (sourceComponent instanceof JScrollPane)
1068: sourceComponent = ((JScrollPane) sourceComponent)
1069: .getViewport();
1070: selectedComponent = addNewComponent(sourceComponent,
1071: evt.getPoint());
1072: componentTree.refresh();
1073: newComponentAdded = true;
1074: }
1075: } else
1076: selectedComponent = sourceComponent;
1077:
1078: if (selectedComponent != null) {
1079: currentSizer = new XComponentSizer(currentProject);
1080: currentSizer.setSizerListener(this );
1081: currentSizer
1082: .setVisible(!(selectedComponent instanceof XPage));
1083: currentSizer.highlightComponent(guidePane,
1084: getPageResource((Component) evtSrc),
1085: selectedComponent);
1086: currentSizer.addKeyListener(this );
1087:
1088: selectedComponents.add(currentSizer);
1089: if (newComponentAdded)
1090: guidePane.alignComponents(selectedComponents);
1091: }
1092:
1093: componentSized();
1094:
1095: if (evt.isPopupTrigger()) {
1096: JPopupMenu popupMenu = new JPopupMenu();
1097: getContextMenu(popupMenu, evt);
1098: currentProject.getPluginManager().getPluginContextMenu(evt,
1099: popupMenu);
1100: }
1101: }
1102:
1103: private void addSubMenu(JMenu menu, String text) {
1104: JMenuItem submenu = new JMenuItem(text);
1105: menu.add(submenu);
1106: submenu.addActionListener(this );
1107: }
1108:
1109: public void mousePressed(MouseEvent evt) {
1110: }
1111:
1112: public void mouseMoved(MouseEvent evt) {
1113: }
1114:
1115: public void mouseDragged(MouseEvent evt) {
1116: }
1117:
1118: public void mouseClicked(MouseEvent evt) {
1119: }
1120:
1121: public void selectComponent(Component selection, boolean init) {
1122: XComponentSizer currentSizer = null;
1123:
1124: int numSizers = selectedComponents.size();
1125: if (init) {
1126: for (int i = 0; i < numSizers; i++)
1127: ((XComponentSizer) selectedComponents.elementAt(i))
1128: .remove();
1129: selectedComponents.clear();
1130: }
1131:
1132: currentSizer = new XComponentSizer(currentProject);
1133: currentSizer.setSizerListener(this );
1134: currentSizer.setVisible(!(selection instanceof XPage));
1135: currentSizer.highlightComponent(guidePane,
1136: getPageResource(selection), selection);
1137: currentSizer.addKeyListener(this );
1138:
1139: selectedComponents.add(currentSizer);
1140: componentSized();
1141: }
1142:
1143: /**
1144: * Load a frameset
1145: * @param showFrames true to show the frames
1146: */
1147: public void loadFrames(boolean showFrames) {
1148: String framesetName = currentProject.getStartupParam("Frames");
1149:
1150: if (showFrames) {
1151: try {
1152: String packageName = currentProject
1153: .getStartupParam("StartPackage");
1154: String homePage = currentProject
1155: .getStartupParam("StartClass");
1156: packageName += ".";
1157: XPageManager pageMgr = currentProject.getPageManager();
1158: pageMgr.setPackageName(packageName);
1159: } catch (Exception ex) {
1160: ex.printStackTrace();
1161: }
1162:
1163: Reader r = null;
1164: try {
1165: if (framesetName != null)
1166: r = currentProject.getResourceManager()
1167: .getBufferedReader(framesetName + ".xml",
1168: null);
1169: } catch (Exception ex) {
1170: }
1171: if (r == null) {
1172: pageContainer.setLayout(new BorderLayout());
1173: return;
1174: }
1175:
1176: Container contentPanel = findTarget(null);
1177: if ((contentPanel != null)
1178: && (contentPanel != pageContainer))
1179: pageContainer.remove(contentPanel);
1180:
1181: pageContainer.setLayout(new BorderLayout());
1182: XmlElement model = XmlSource.read(r);
1183: Vector componentNodes = model.getChildren();
1184: int numChildren = componentNodes.size();
1185: for (int i = 0; i < numChildren; i++) {
1186: XmlElement childNode = (XmlElement) componentNodes
1187: .elementAt(i);
1188: String typeStr = childNode.getName();
1189: if (typeStr.compareTo("Frame") == 0) {
1190: try {
1191: String targetName = childNode
1192: .getAttribute("name");
1193: int pageWidth = new Integer(childNode
1194: .getAttribute("width")).intValue();
1195: int pageHeight = new Integer(childNode
1196: .getAttribute("height")).intValue();
1197: XTarget target = new XTarget(targetName,
1198: pageWidth, pageHeight);
1199: Object constraint = styleFactory
1200: .getLayoutHelper()
1201: .getConstraint(
1202: childNode
1203: .getAttribute("constraint"));
1204: target.setName(targetName);
1205: addTarget(target, constraint);
1206: String pageName = childNode
1207: .getAttribute("content");
1208: loadPage(pageName, targetName, pageWidth,
1209: pageHeight);
1210: } catch (NumberFormatException ex1) {
1211: }
1212: }
1213: }
1214: } else {
1215: Container contentPanel = findTarget(null);
1216: pageContainer.removeAll();
1217: pageContainer.setLayout(new BorderLayout());
1218: if (pageContainer != contentPanel)
1219: pageContainer.add(contentPanel, BorderLayout.CENTER);
1220: }
1221: pageContainer.doLayout();
1222: }
1223:
1224: /**
1225: * Get the name of the page in the content panel
1226: * @return the content page name
1227: */
1228: public XPageResource getContentPage() {
1229: Container target = findTarget(null);
1230: if (target.getComponentCount() > 0)
1231: return ((XPageResource) target.getComponent(0));
1232: else
1233: return null;
1234: }
1235:
1236: /**
1237: * Load a page into the named area
1238: * @param pageName the page name
1239: * @param target the name of the target area
1240: * @param pageWidth the page width
1241: * @param pageHeight the page height
1242: */
1243: public XPageResource loadPage(String pageName, String target,
1244: int pageWidth, int pageHeight) {
1245: if (target == null) {
1246: // If the page is already on display do not add it
1247: XPageResource displayedResource = findPageResource(pageName);
1248: if (displayedResource != null) {
1249: currentPage = displayedResource;
1250: return currentPage;
1251: }
1252: }
1253: Container targetContainer = findTarget(target);
1254: XPageResource pageResource = currentProject
1255: .getPageResource(pageName);
1256: XPage page = pageResource.getPage();
1257:
1258: XPageResource oldPage = currentPage;
1259: if (targetContainer.getComponentCount() > 0) {
1260: oldPage = (XPageResource) targetContainer.getComponent(0);
1261: page.setLocation(oldPage.getLocation());
1262: } else {
1263: oldPage = null;
1264: page.setLocation(0, 0);
1265: }
1266:
1267: if ((pageWidth > 0) && (pageHeight > 0))
1268: page.setSize(pageWidth, pageHeight);
1269: else
1270: pageResource.setSize(targetContainer.getSize());
1271:
1272: int containerWidth = XEditorProjectManager.getCurrentProject()
1273: .getStartupParamAsInt("ClientWidth");
1274: int containerHeight = XEditorProjectManager.getCurrentProject()
1275: .getStartupParamAsInt("ClientHeight");
1276: if (containerWidth == 0)
1277: containerWidth = 640;
1278: if (containerHeight == 0)
1279: containerHeight = 480;
1280: containerWidth += 2;
1281: containerHeight += 2;
1282: pageContainer.setSize(containerWidth, containerHeight);
1283: sizeGuidePane();
1284:
1285: targetContainer.add(pageResource);
1286: if (oldPage != null)
1287: targetContainer.remove(oldPage);
1288:
1289: setPage(page);
1290: currentPage = pageResource;
1291:
1292: targetContainer.doLayout();
1293: targetContainer.setVisible(true);
1294: targetContainer.repaint();
1295:
1296: page.doLayout();
1297: page.repaint();
1298:
1299: pageContainer.doLayout();
1300: pageContainer.repaint();
1301:
1302: repaintGuides();
1303: return pageResource;
1304: }
1305:
1306: /**
1307: * Finds the target for a page
1308: * @param screenName the screen to locate
1309: * @return the container holding the named page
1310: */
1311: public XPageResource findPageResource(String screenName) {
1312: Component[] comps = pageContainer.getComponents();
1313: int numChildren = comps.length;
1314: for (int i = 0; i < numChildren; i++) {
1315: if (comps[i] instanceof XTarget) {
1316: if (((XTarget) comps[i]).getComponentCount() > 0) {
1317: Component comp = ((XTarget) comps[i])
1318: .getComponent(0);
1319: if (comp != null) {
1320: XPageResource pageResource = (XPageResource) comp;
1321: if (pageResource.getName()
1322: .compareTo(screenName) == 0)
1323: return pageResource;
1324: }
1325: }
1326: } else if (comps[i] instanceof XPageResource) {
1327: XPageResource pageResource = (XPageResource) comps[i];
1328: if (pageResource.getName().compareTo(screenName) == 0)
1329: return pageResource;
1330: }
1331: }
1332: return null;
1333: }
1334:
1335: /**
1336: * Finds the target for a page display request
1337: * @param target the target area
1338: * @return the container to be updated
1339: */
1340: public Container findTarget(String target) {
1341: if (target == null)
1342: target = "content";
1343:
1344: Component[] comps = pageContainer.getComponents();
1345: int numChildren = comps.length;
1346: for (int i = 0; i < numChildren; i++) {
1347: if (comps[i].getName().compareTo(target) == 0)
1348: return (XTarget) comps[i];
1349: }
1350: return pageContainer;
1351: }
1352:
1353: /**
1354: * Get the target for a page display request
1355: * @param idx the target area
1356: * @return the container to be updated
1357: */
1358: public Container getTarget(int idx) {
1359: Component[] comps = pageContainer.getComponents();
1360: int numChildren = comps.length;
1361: return (XTarget) comps[idx];
1362: }
1363:
1364: /**
1365: * Get the number of targets in the container
1366: */
1367: public int getNumTargets() {
1368: return pageContainer.getComponentCount();
1369: }
1370:
1371: public void showPages(boolean showPages) {
1372: Component[] comps = pageContainer.getComponents();
1373: int numChildren = comps.length;
1374: XPageResource pageRes;
1375: for (int i = 0; i < numChildren; i++) {
1376: pageRes = (XPageResource) ((XTarget) comps[i])
1377: .getComponent(0);
1378: if (showPages)
1379: pageRes.setBorder(BorderFactory
1380: .createLineBorder(SystemColor.controlDkShadow));
1381: else
1382: pageRes.setBorder(BorderFactory.createEmptyBorder());
1383: }
1384: repaint();
1385: }
1386:
1387: public void showGuides(boolean showGuides) {
1388: guidePane.setPaintGuides(showGuides);
1389: repaint();
1390: }
1391:
1392: /**
1393: * Use the system look and feel.
1394: * @param showSystemLaf true to use the system look and feel, otherwise use the Java look and feel
1395: */
1396: public void showLaf(boolean showSystemLaf) {
1397: JButton proxyBtn = new JButton();
1398: try {
1399: ButtonUI ui = null;
1400: if (showSystemLaf) {
1401: ui = (ButtonUI) UIManager.getUI(proxyBtn);
1402: } else {
1403: ui = new javax.swing.plaf.basic.BasicButtonUI();
1404: }
1405: setLaf(this , ui);
1406: } catch (Exception ex) {
1407: }
1408: repaint();
1409: }
1410:
1411: void setLaf(Container cont, ButtonUI ui) {
1412: if (cont instanceof JButton)
1413: ((JButton) cont).setUI(ui);
1414:
1415: Component[] comps = cont.getComponents();
1416: int numChildren = comps.length;
1417: XPageResource pageRes;
1418: for (int i = 0; i < numChildren; i++) {
1419: if (comps[i] instanceof Container)
1420: setLaf((Container) comps[i], ui);
1421: }
1422: }
1423:
1424: /**
1425: * Get the guide pane
1426: * @returns
1427: */
1428: public XGuidePane getGuidePane() {
1429: return guidePane;
1430: }
1431:
1432: /**
1433: * Get the current page
1434: * @return the page instance
1435: */
1436: public XPageResource getCurrentPage() {
1437: return currentPage;
1438: }
1439:
1440: public XPageResource getPageResource(Component comp) {
1441: if (comp instanceof XPageResource)
1442: return (XPageResource) comp;
1443: else
1444: return getPageResource(comp.getParent());
1445: }
1446:
1447: public void projectLoaded(XEditorProject project) {
1448: currentProject = project;
1449: }
1450:
1451: public void resetStyles() {
1452: Component[] comps = getComponents();
1453: int numChildren = comps.length;
1454: for (int i = 0; i < numChildren; i++) {
1455: XPageResource resource = null;
1456: if (comps[i] instanceof XPage)
1457: resource = currentProject.getPageResource(comps[i]
1458: .getName());
1459: else
1460: resource = currentProject
1461: .getPageResource(((Container) comps[i])
1462: .getComponent(0).getName());
1463: resource.resetStyles();
1464: }
1465: }
1466:
1467: /**
1468: * Reset the page listeners after a build
1469: */
1470: void resetListeners() {
1471: if (!previewMode) {
1472: Component[] comps = getComponents();
1473: int numChildren = comps.length;
1474: for (int i = 0; i < numChildren; i++) {
1475: XPageResource resource = null;
1476: if (comps[i] instanceof XPage)
1477: resource = currentProject.getPageResource(comps[i]
1478: .getName());
1479: else {
1480: String compName = ((Container) comps[i])
1481: .getComponent(0).getName();
1482: if (compName != null)
1483: resource = currentProject
1484: .getPageResource(compName);
1485: }
1486: XPage page = null;
1487: if (resource != null)
1488: page = resource.getPage();
1489:
1490: if ((page != null)
1491: && (!(page.getEventHandler() instanceof XEditorEventHandler))) {
1492: XEditorEventHandler eventHandler = new XEditorEventHandler(
1493: this , page.getEventHandler(), page,
1494: new XValidationHandler(page));
1495: page.setEventHandler(eventHandler);
1496:
1497: removeEventListeners(resource, eventHandler, page);
1498: addListeners(page, eventHandler);
1499: }
1500: }
1501: }
1502: }
1503:
1504: Vector getSelectedComponents() {
1505: return selectedComponents;
1506: }
1507:
1508: public void clearSelectedComponents() {
1509: XComponentSizer currentSizer = null;
1510: int numSizers = selectedComponents.size();
1511: for (int i = 0; i < numSizers; i++)
1512: ((XComponentSizer) selectedComponents.elementAt(i))
1513: .remove();
1514: selectedComponents.clear();
1515: componentSized();
1516: }
1517:
1518: /**
1519: * Change the size of the guide pane to match that of this window
1520: */
1521: void sizeGuidePane() {
1522: Dimension size = getSize();
1523: guidePane.setBounds(0, 0, size.width, size.height);
1524: Point p = pageContainer.getLocation();
1525: size = pageContainer.getSize();
1526: guidePane.setPageBounds(p.x, p.y, size.width, size.height);
1527: }
1528:
1529: public void componentHidden(ComponentEvent e) {
1530: }
1531:
1532: public void componentMoved(ComponentEvent e) {
1533: }
1534:
1535: public void componentShown(ComponentEvent e) {
1536: }
1537:
1538: public void componentResized(ComponentEvent e) {
1539: sizeGuidePane();
1540: }
1541:
1542: /**
1543: * The project has been updated
1544: */
1545: public void projectUpdated() {
1546: }
1547:
1548: class NavigateAction extends AbstractAction {
1549: public NavigateAction(String text, ImageIcon icon, String desc,
1550: Integer mnemonic) {
1551: super (text, icon);
1552: putValue(SHORT_DESCRIPTION, desc);
1553: putValue("MnemonicKey"/*Action.MNEMONIC_KEY*/, mnemonic);
1554: }
1555:
1556: public void actionPerformed(ActionEvent e) {
1557: if ((selectedComponents != null)
1558: && (selectedComponents.size() == 1)) {
1559: XComponentSizer sizer = ((XComponentSizer) selectedComponents
1560: .elementAt(0));
1561: Component comp = sizer.getTarget();
1562: XPage page = sizer.getPage();
1563:
1564: String command = e.getActionCommand();
1565: if (command.compareTo("Go to page...") == 0) {
1566: XPageSelectionDialog psDialog = new XPageSelectionDialog(
1567: currentProject);
1568: psDialog.show(sizer.getLocationOnScreen());
1569: currentProject.getPageResource(page.getName())
1570: .addCtlEvent(comp,
1571: AWTEvent.ACTION_EVENT_MASK,
1572: "nextPage");
1573: } else if (command.compareTo("Home") == 0)
1574: currentProject.getPageResource(page.getName())
1575: .addCtlEvent(comp,
1576: AWTEvent.ACTION_EVENT_MASK,
1577: "homePage");
1578: else if (command.compareTo("Previous") == 0)
1579: currentProject.getPageResource(page.getName())
1580: .addCtlEvent(comp,
1581: AWTEvent.ACTION_EVENT_MASK,
1582: "prevPage");
1583: }
1584: }
1585: }
1586:
1587: public void setEventHandlerEditor(EventHandlerEditor ehe) {
1588: eventHandlerEditor = ehe;
1589: }
1590: }
|