0001: /*
0002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
0003: *
0004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
0005: *
0006: * The contents of this file are subject to the terms of either the GNU
0007: * General Public License Version 2 only ("GPL") or the Common
0008: * Development and Distribution License("CDDL") (collectively, the
0009: * "License"). You may not use this file except in compliance with the
0010: * License. You can obtain a copy of the License at
0011: * http://www.netbeans.org/cddl-gplv2.html
0012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
0013: * specific language governing permissions and limitations under the
0014: * License. When distributing the software, include this License Header
0015: * Notice in each file and include the License file at
0016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
0017: * particular file as subject to the "Classpath" exception as provided
0018: * by Sun in the GPL Version 2 section of the License file that
0019: * accompanied this code. If applicable, add the following below the
0020: * License Header, with the fields enclosed by brackets [] replaced by
0021: * your own identifying information:
0022: * "Portions Copyrighted [year] [name of copyright owner]"
0023: *
0024: * Contributor(s):
0025: *
0026: * The Original Software is NetBeans. The Initial Developer of the Original
0027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
0028: * Microsystems, Inc. All Rights Reserved.
0029: *
0030: * If you wish your version of this file to be governed by only the CDDL
0031: * or only the GPL Version 2, indicate your decision by adding
0032: * "[Contributor] elects to include this software in this distribution
0033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
0034: * single choice of license, a recipient has the option to distribute
0035: * your version of this file under either the CDDL, the GPL Version 2 or
0036: * to extend the choice of license to its licensees as provided above.
0037: * However, if you add GPL Version 2 code and therefore, elected the GPL
0038: * Version 2 license, then the option applies only if the new code is
0039: * made subject to such option by the copyright holder.
0040: */
0041:
0042: package org.netbeans.modules.form;
0043:
0044: import java.awt.*;
0045: import java.awt.datatransfer.*;
0046: import java.awt.dnd.*;
0047: import java.awt.event.*;
0048: import java.awt.geom.Area;
0049: import java.io.IOException;
0050: import javax.swing.*;
0051: import java.util.*;
0052: import java.text.MessageFormat;
0053: import javax.swing.undo.UndoableEdit;
0054: import org.netbeans.modules.form.actions.DuplicateAction;
0055: import org.netbeans.modules.form.assistant.AssistantModel;
0056: import org.netbeans.spi.palette.PaletteController;
0057:
0058: import org.openide.DialogDisplayer;
0059: import org.openide.NotifyDescriptor;
0060: import org.openide.awt.StatusDisplayer;
0061: import org.openide.cookies.SaveCookie;
0062: import org.openide.filesystems.FileObject;
0063: import org.openide.windows.TopComponent;
0064: import org.openide.nodes.Node;
0065: import org.openide.nodes.NodeOp;
0066: import org.openide.util.Utilities;
0067:
0068: import org.netbeans.modules.form.palette.PaletteItem;
0069: import org.netbeans.modules.form.palette.PaletteUtils;
0070: import org.netbeans.modules.form.project.ClassSource;
0071: import org.netbeans.modules.form.fakepeer.FakePeerSupport;
0072: import org.netbeans.modules.form.layoutsupport.*;
0073: import org.netbeans.modules.form.layoutdesign.*;
0074: import org.netbeans.modules.form.menu.MenuEditLayer;
0075: import org.openide.filesystems.FileUtil;
0076: import org.openide.util.Lookup;
0077:
0078: /**
0079: * A transparent layer (glass pane) handling user operations in designer (mouse
0080: * and keyboard events) and painting selection and drag&drop feedback.
0081: * Technically, this is a layer in FormDesigner, placed over ComponentLayer.
0082: *
0083: * @author Tran Duc Trung, Tomas Pavek
0084: */
0085:
0086: public class HandleLayer extends JPanel implements MouseListener,
0087: MouseMotionListener {
0088: // constants for mode parameter of getMetaComponentAt(Point,int) method
0089: public static final int COMP_DEEPEST = 0; // get the deepest component (at given position)
0090: public static final int COMP_SELECTED = 1; // get the deepest selected component
0091: public static final int COMP_ABOVE_SELECTED = 2; // get the component above the deepest selected component
0092: public static final int COMP_UNDER_SELECTED = 3; // get the component under the deepest selected component
0093:
0094: private static final int DESIGNER_RESIZING = 256; // flag for resizeType
0095: private static MessageFormat resizingHintFormat;
0096: private static MessageFormat sizeHintFormat;
0097:
0098: private FormDesigner formDesigner;
0099: private boolean viewOnly;
0100:
0101: private ComponentDrag draggedComponent;
0102: private JPanel dragPanel;
0103:
0104: private Point lastMousePosition;
0105: private int lastXPosDiff;
0106: private int lastYPosDiff;
0107:
0108: private Point lastLeftMousePoint;
0109: private Point prevLeftMousePoint;
0110: private boolean draggingEnded; // prevents dragging from starting inconveniently
0111: private int resizeType;
0112:
0113: private SelectionDragger selectionDragger;
0114: private Image resizeHandle;
0115:
0116: private DropTarget dropTarget;
0117: private NewComponentDropListener dropListener;
0118:
0119: /** The FormLoaderSettings instance */
0120: private static FormLoaderSettings formSettings = FormLoaderSettings
0121: .getInstance();
0122:
0123: // -------
0124:
0125: HandleLayer(FormDesigner fd) {
0126: formDesigner = fd;
0127: addMouseListener(this );
0128: addMouseMotionListener(this );
0129: setLayout(null);
0130:
0131: // Hack - the panel is used to ensure correct painting of dragged components
0132: dragPanel = new JPanel();
0133: dragPanel.setLayout(null);
0134: dragPanel.setBounds(-1, -1, 0, 0);
0135: add(dragPanel);
0136:
0137: // set Ctrl+TAB and Ctrl+Shift+TAB as focus traversal keys - to have
0138: // TAB and Shift+TAB free for component selection
0139: Set<AWTKeyStroke> keys = new HashSet<AWTKeyStroke>();
0140: keys.add(AWTKeyStroke.getAWTKeyStroke(9,
0141: InputEvent.CTRL_DOWN_MASK, true));
0142: setFocusTraversalKeys(
0143: KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS, keys);
0144: keys.clear();
0145: keys.add(AWTKeyStroke.getAWTKeyStroke(9,
0146: InputEvent.CTRL_DOWN_MASK | InputEvent.SHIFT_DOWN_MASK,
0147: true));
0148: setFocusTraversalKeys(
0149: KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS, keys);
0150:
0151: getAccessibleContext().setAccessibleName(
0152: FormUtils.getBundleString("ACSN_HandleLayer")); // NOI18N
0153: getAccessibleContext().setAccessibleDescription(
0154: FormUtils.getBundleString("ACSD_HandleLayer")); // NOI18N
0155:
0156: dropListener = new NewComponentDropListener();
0157: dropTarget = new DropTarget(this , dropListener);
0158: }
0159:
0160: //expose the drop listener so the MenuEditLayer can access it
0161: public DropTargetListener getNewComponentDropListener() {
0162: return dropListener;
0163: }
0164:
0165: void setViewOnly(boolean viewOnly) {
0166: if (this .viewOnly == viewOnly) {
0167: return;
0168: }
0169: if (viewOnly) {
0170: dropTarget.removeDropTargetListener(dropListener);
0171: } else {
0172: try {
0173: dropTarget.addDropTargetListener(dropListener);
0174: } catch (TooManyListenersException ex) {
0175: ex.printStackTrace();
0176: }
0177: }
0178: this .viewOnly = viewOnly;
0179: }
0180:
0181: private FormModel getFormModel() {
0182: return formDesigner.getFormModel();
0183: }
0184:
0185: private MetaComponentCreator getComponentCreator() {
0186: return formDesigner.getFormModel().getComponentCreator();
0187: }
0188:
0189: private LayoutModel getLayoutModel() {
0190: return formDesigner.getFormModel().getLayoutModel();
0191: }
0192:
0193: // ---------
0194:
0195: @Override
0196: protected void paintComponent(Graphics g) {
0197: Graphics2D g2 = (Graphics2D) g;
0198:
0199: // paint component in the connection mode (if any)
0200: if (formDesigner.getDesignerMode() == FormDesigner.MODE_CONNECT) {
0201: RADComponent conSource = formDesigner.getConnectionSource();
0202: RADComponent conTarget = formDesigner.getConnectionTarget();
0203: if (conSource != null || conTarget != null) {
0204: g2.setColor(formSettings.getConnectionBorderColor());
0205: g2.setStroke(getPaintStroke());
0206: if (conSource != null)
0207: paintSelection(g2, conSource, false);
0208: if (conTarget != null)
0209: paintSelection(g2, conTarget, false);
0210: }
0211: return; // that's all in connection mode
0212: }
0213:
0214: if (draggedComponent != null) {
0215: try {
0216: FormLAF.setUseDesignerDefaults(getFormModel());
0217: draggedComponent.paintFeedback(g2);
0218: } finally {
0219: FormLAF.setUseDesignerDefaults(null);
0220: }
0221: } else { // just paint the selection of selected components
0222: g2.setColor(formSettings.getSelectionBorderColor());
0223: g2.setStroke(getPaintStroke());
0224: boolean painted = false;
0225: try {
0226: boolean inLayout = selectedComponentsInSameVisibleContainer();
0227: Iterator metacomps = formDesigner
0228: .getSelectedComponents().iterator();
0229: while (metacomps.hasNext()) {
0230: RADComponent metacomp = (RADComponent) metacomps
0231: .next();
0232: RADVisualComponent layoutMetacomp = formDesigner
0233: .componentToLayoutComponent(metacomp);
0234: if (layoutMetacomp != null)
0235: metacomp = layoutMetacomp;
0236: paintSelection(g2, metacomp, inLayout);
0237: }
0238: painted = true;
0239: } finally {
0240: // Make sure that problems in selection painting
0241: // doesn't cause endless stream of exceptions.
0242: if (!painted) {
0243: formDesigner.clearSelection();
0244: }
0245: }
0246:
0247: if (selectionDragger != null)
0248: selectionDragger.paintDragFeedback(g2);
0249: }
0250: }
0251:
0252: /**
0253: * @param inLayout indicates whether to paint layout related decorations
0254: * (layout relations in container and resize handles)
0255: */
0256: private void paintSelection(Graphics2D g, RADComponent metacomp,
0257: boolean inLayout) {
0258: if (!(metacomp instanceof RADVisualComponent)
0259: && !(metacomp instanceof RADMenuItemComponent))
0260: return;
0261: Object comp = formDesigner.getComponent(metacomp);
0262: if (!(comp instanceof Component))
0263: return;
0264:
0265: Component component = (Component) comp;
0266: Component parent = component.getParent();
0267:
0268: if (parent != null && component.isShowing()) {
0269: Rectangle selRect = component.getBounds();
0270: RADVisualContainer metacont = metacomp.getParentComponent() instanceof RADVisualContainer ? (RADVisualContainer) metacomp
0271: .getParentComponent()
0272: : null;
0273: convertRectangleFromComponent(selRect, parent);
0274: Rectangle visible = new Rectangle(0, 0, parent.getWidth(),
0275: parent.getHeight());
0276: visible = convertVisibleRectangleFromComponent(visible,
0277: parent);
0278:
0279: if (inLayout && metacont != null
0280: && metacont.getLayoutSupport() == null
0281: && formDesigner.isInDesigner(metacont)) { // component in free design container - layout designer may want to paint something
0282: Component topComp = formDesigner
0283: .getTopDesignComponentView();
0284: Point convertPoint = convertPointFromComponent(0, 0,
0285: topComp);
0286: g.translate(convertPoint.x, convertPoint.y);
0287: LayoutDesigner layoutDesigner = formDesigner
0288: .getLayoutDesigner();
0289: Color oldColor = g.getColor();
0290: g.setColor(formSettings.getGuidingLineColor());
0291: Shape clip = g.getClip();
0292: visible.translate(-convertPoint.x, -convertPoint.y);
0293: Area area = new Area(visible);
0294: if (clip != null) {
0295: area.intersect(new Area(clip));
0296: }
0297: g.setClip(area);
0298: layoutDesigner.paintSelection(g, metacomp.getId());
0299: g.setClip(clip);
0300: g.setColor(oldColor);
0301: visible.translate(convertPoint.x, convertPoint.y);
0302: g.translate(-convertPoint.x, -convertPoint.y);
0303: }
0304: int resizable = 0;
0305: if (inLayout) {
0306: resizable = getComponentResizable((RADVisualComponent) metacomp);
0307: }
0308: if (resizable == 0) {
0309: selRect = selRect.intersection(visible);
0310: }
0311: int correction = formSettings.getSelectionBorderSize() % 2;
0312: int x = selRect.x - correction;
0313: int y = selRect.y - correction;
0314: int width = selRect.width + correction;
0315: int height = selRect.height + correction;
0316: g.drawRect(x, y, width, height);
0317: if (inLayout) {
0318: Image resizeHandle = resizeHandle();
0319: int iconHeight = resizeHandle.getHeight(null);
0320: int iconWidth = resizeHandle.getWidth(null);
0321: if ((resizable & LayoutSupportManager.RESIZE_LEFT) != 0) {
0322: g.drawImage(resizeHandle, x - iconWidth + 1, y
0323: + (height - iconHeight) / 2, null);
0324: if ((resizable & LayoutSupportManager.RESIZE_UP) != 0) {
0325: g.drawImage(resizeHandle, x - iconWidth + 1, y
0326: - iconHeight + 1, null);
0327: }
0328: if ((resizable & LayoutSupportManager.RESIZE_DOWN) != 0) {
0329: g.drawImage(resizeHandle, x - iconWidth + 1, y
0330: + height, null);
0331: }
0332: }
0333: if ((resizable & LayoutSupportManager.RESIZE_RIGHT) != 0) {
0334: g.drawImage(resizeHandle, x + width, y
0335: + (height - iconHeight) / 2, null);
0336: if ((resizable & LayoutSupportManager.RESIZE_UP) != 0) {
0337: g.drawImage(resizeHandle, x + width, y
0338: - iconHeight + 1, null);
0339: }
0340: if ((resizable & LayoutSupportManager.RESIZE_DOWN) != 0) {
0341: g.drawImage(resizeHandle, x + width,
0342: y + height, null);
0343: }
0344: }
0345: if ((resizable & LayoutSupportManager.RESIZE_UP) != 0) {
0346: g.drawImage(resizeHandle, x + (width - iconWidth)
0347: / 2, y - iconHeight + 1, null);
0348: }
0349: if ((resizable & LayoutSupportManager.RESIZE_DOWN) != 0) {
0350: g.drawImage(resizeHandle, x + (width - iconWidth)
0351: / 2, y + height, null);
0352: }
0353: }
0354: }
0355: }
0356:
0357: private Image resizeHandle() {
0358: if (resizeHandle == null) {
0359: resizeHandle = new ImageIcon(
0360: Utilities
0361: .loadImage("org/netbeans/modules/form/resources/resize_handle.png"))
0362: .getImage(); // NOI18N
0363: }
0364: return resizeHandle;
0365: }
0366:
0367: // paint stroke cached
0368: private static int lastPaintWidth = -1;
0369: private Stroke paintStroke;
0370:
0371: private Stroke getPaintStroke() {
0372: int width = formSettings.getSelectionBorderSize();
0373: if (lastPaintWidth != width) {
0374: paintStroke = null;
0375: }
0376: if (paintStroke == null) {
0377: paintStroke = new BasicStroke(width);
0378: lastPaintWidth = width;
0379: }
0380: return paintStroke;
0381: }
0382:
0383: void maskDraggingComponents() {
0384: if (draggedComponent != null) {
0385: draggedComponent.maskDraggingComponents();
0386: }
0387: }
0388:
0389: @Override
0390: public boolean isOpaque() {
0391: return false;
0392: }
0393:
0394: @Override
0395: protected void processKeyEvent(KeyEvent e) {
0396: int keyCode = e.getKeyCode();
0397:
0398: if (keyCode == KeyEvent.VK_TAB || e.getKeyChar() == '\t') {
0399: if (!e.isControlDown()) {
0400: if (e.getID() == KeyEvent.KEY_PRESSED) {
0401: RADComponent nextComp = formDesigner
0402: .getNextVisualComponent(!e.isShiftDown());
0403: if (nextComp != null)
0404: formDesigner.setSelectedComponent(nextComp);
0405: }
0406: e.consume();
0407: return;
0408: }
0409: } else if (keyCode == KeyEvent.VK_SPACE
0410: || keyCode == KeyEvent.VK_F2) {
0411: if (!viewOnly && e.getID() == KeyEvent.KEY_RELEASED) {
0412: java.util.List selected = formDesigner
0413: .getSelectedComponents();
0414: if (selected.size() == 1) { // just one component is selected
0415: RADComponent comp = (RADComponent) selected.get(0);
0416: if (formDesigner.getDesignerMode() == FormDesigner.MODE_SELECT) {
0417: // in selection mode SPACE starts in-place editing
0418: formDesigner.startInPlaceEditing(comp);
0419: }
0420: // else if (formDesigner.getDesignerMode() == FormDesigner.MODE_ADD) {
0421: // // in add mode SPACE adds selected item as component
0422: // PaletteItem item = CPManager.getDefault().getSelectedItem();
0423: // if (item != null) {
0424: // formDesigner.getModel().getComponentCreator()
0425: // .createComponent(item.getComponentClassSource(),
0426: // comp,
0427: // null);
0428: // formDesigner.toggleSelectionMode();
0429: // }
0430: // }
0431: }
0432: }
0433: e.consume();
0434: return;
0435: } else if (keyCode == KeyEvent.VK_ESCAPE) {
0436: if (formDesigner.getDesignerMode() != FormDesigner.MODE_SELECT) {
0437: formDesigner.toggleSelectionMode(); // also calls endDragging(null)
0438: repaint();
0439: e.consume();
0440: return;
0441: }
0442: if (endDragging(null)) {
0443: repaint();
0444: e.consume();
0445: return;
0446: }
0447: } else if ((keyCode == KeyEvent.VK_CONTEXT_MENU)
0448: || ((keyCode == KeyEvent.VK_F10) && e.isShiftDown())) { // Shift F10 invokes context menu
0449: Point p = null;
0450: java.util.List selected = formDesigner
0451: .getSelectedComponents();
0452: if (selected.size() > 0) {
0453: RADComponent metacomp = (RADComponent) selected.get(0);
0454: Object sel = (Component) formDesigner
0455: .getComponent(metacomp);
0456: if (sel instanceof Component) {
0457: Component comp = (Component) sel;
0458: p = convertPointFromComponent(comp.getLocation(),
0459: comp.getParent());
0460: } else
0461: p = new Point(6, 6);
0462:
0463: showContextMenu(p);
0464: e.consume();
0465: return;
0466: }
0467: } else if (e.getID() == KeyEvent.KEY_PRESSED
0468: && (keyCode == KeyEvent.VK_DOWN
0469: || keyCode == KeyEvent.VK_UP
0470: || keyCode == KeyEvent.VK_LEFT || keyCode == KeyEvent.VK_RIGHT)) {
0471: // cursor keys
0472: if (e.isControlDown() && !e.isAltDown() && !e.isShiftDown()) {
0473: // duplicating
0474: DuplicateAction.performAction(formDesigner
0475: .getSelectedComponentNodes(), keyCode);
0476: e.consume();
0477: return;
0478: }
0479: } else if (((keyCode == KeyEvent.VK_D) || (keyCode == KeyEvent.VK_E))
0480: && e.isAltDown()
0481: && e.isControlDown()
0482: && (e.getID() == KeyEvent.KEY_PRESSED)) {
0483: FormModel formModel = formDesigner.getFormModel();
0484: LayoutModel layoutModel = formModel.getLayoutModel();
0485: if (layoutModel != null) {
0486: Map<String, String> idToNameMap = new HashMap<String, String>();
0487: for (RADComponent comp : formModel.getAllComponents()) {
0488: if (comp != formModel.getTopRADComponent())
0489: idToNameMap.put(comp.getId(), comp.getName());
0490: }
0491: System.out.println(layoutModel.dump(idToNameMap));
0492: }
0493: } else if (((keyCode == KeyEvent.VK_W)) && e.isAltDown()
0494: && e.isControlDown()
0495: && (e.getID() == KeyEvent.KEY_PRESSED)) {
0496: // generate layout test (one checkpoint)
0497: if (formDesigner.getLayoutDesigner().logTestCode()) {
0498: FormModel formModel = formDesigner.getFormModel();
0499: LayoutModel layoutModel = formModel.getLayoutModel();
0500: if (layoutModel != null) {
0501: Map<String, String> idToNameMap = new HashMap<String, String>();
0502: for (RADComponent comp : formModel
0503: .getAllComponents()) {
0504: idToNameMap.put(comp.getId(), comp.getName());
0505: }
0506: FormDataObject formDO = formDesigner
0507: .getFormEditor().getFormDataObject();
0508: LayoutTestUtils.writeTest(formDesigner, formDO,
0509: idToNameMap, layoutModel);
0510: LayoutDesigner ld = formDesigner
0511: .getLayoutDesigner();
0512: ld.setModelCounter(ld.getModelCounter() + 1);
0513: }
0514: }
0515: } else if (((keyCode == KeyEvent.VK_S)) && e.isAltDown()
0516: && e.isControlDown()
0517: && (e.getID() == KeyEvent.KEY_PRESSED)) {
0518: // start layout test recording
0519: if (LayoutDesigner.testMode()) {
0520: FormDataObject formDO = formDesigner.getFormEditor()
0521: .getFormDataObject();
0522: FileObject formFile = formDO.getFormFile();
0523: SaveCookie saveCookie = formDO
0524: .getCookie(SaveCookie.class);
0525: try {
0526: if (saveCookie != null)
0527: saveCookie.save();
0528: FileObject copied = formFile.copy(LayoutTestUtils
0529: .getTargetFolder(formFile), formFile
0530: .getName()
0531: + "Test-StartingForm", // NOI18N
0532: formFile.getExt());
0533: formDesigner.getLayoutDesigner().setModelCounter(0);
0534: formDesigner.resetTopDesignComponent(true);
0535: StatusDisplayer
0536: .getDefault()
0537: .setStatusText(
0538: "The form was successfully copied to: "
0539: + FileUtil
0540: .getFileDisplayName(copied)); // NOI18N
0541: } catch (IOException ioe) {
0542: //TODO
0543: }
0544: }
0545: }
0546:
0547: super .processKeyEvent(e);
0548: }
0549:
0550: @Override
0551: public boolean isFocusable() {
0552: return true;
0553: }
0554:
0555: // -------
0556:
0557: /**
0558: * Returns metacomponent for visual component at given location.
0559: * @param point - location in component layer's coordinates
0560: * @param mode - defines what level in the hierarchy to prefer (in order to
0561: * distinguish between the leaf components and their parents):
0562: * COMP_DEEPEST - get the component which is the deepest in the hierarchy (leaf component)
0563: * COMP_SELECTED - get the deepest selected component
0564: * COMP_ABOVE_SELECTED - get the component above the deepest selected component
0565: * COMP_UNDER_SELECTED - get the component under the deepest selected component
0566: * @returns the metacomponent at given point
0567: * If no component is currently selected then:
0568: * for COMP_SELECTED the deepest component is returned
0569: * for COMP_ABOVE_SELECTED the deepest component is returned
0570: * for COMP_UNDER_SELECTED the top component is returned
0571: */
0572: public RADComponent getMetaComponentAt(Point point, int mode) {
0573: Component[] deepComps = getDeepestComponentsAt(formDesigner
0574: .getComponentLayer(), point);
0575: if (deepComps == null) {
0576: return null;
0577: }
0578:
0579: int dIndex = 0;
0580: Component comp = deepComps[dIndex];
0581:
0582: // find the component satisfying point and mode
0583: RADComponent topMetaComp = formDesigner.getTopDesignComponent();
0584: RADComponent firstMetaComp = null;
0585: RADComponent currMetaComp;
0586: RADComponent prevMetaComp = null;
0587:
0588: do {
0589: currMetaComp = formDesigner.getMetaComponent(comp);
0590: if (currMetaComp != null
0591: && !isDraggedComponent(currMetaComp)) {
0592: if (firstMetaComp == null)
0593: firstMetaComp = currMetaComp;
0594:
0595: switch (mode) {
0596: case COMP_DEEPEST:
0597: return currMetaComp;
0598:
0599: case COMP_SELECTED:
0600: if (formDesigner.isComponentSelected(currMetaComp))
0601: return currMetaComp;
0602: if (currMetaComp == topMetaComp)
0603: return firstMetaComp; // nothing selected - return the deepest
0604: break;
0605:
0606: case COMP_ABOVE_SELECTED:
0607: if (prevMetaComp != null
0608: && formDesigner
0609: .isComponentSelected(prevMetaComp))
0610: return currMetaComp;
0611: if (currMetaComp == topMetaComp)
0612: return firstMetaComp; // nothing selected - return the deepest
0613: break;
0614:
0615: case COMP_UNDER_SELECTED:
0616: if (formDesigner.isComponentSelected(currMetaComp))
0617: return prevMetaComp != null ? prevMetaComp
0618: : topMetaComp;
0619: if (currMetaComp == topMetaComp)
0620: return topMetaComp; // nothing selected - return the top
0621: break;
0622: }
0623:
0624: prevMetaComp = currMetaComp;
0625: }
0626:
0627: comp = dIndex + 1 < deepComps.length ? deepComps[++dIndex]
0628: : comp.getParent();
0629: } while (comp != null);
0630:
0631: return firstMetaComp;
0632: }
0633:
0634: private static Component[] getDeepestComponentsAt(Container parent,
0635: Point point) {
0636: Component deepestComp = SwingUtilities.getDeepestComponentAt(
0637: parent, point.x, point.y);
0638: if (deepestComp == null) {
0639: return null;
0640: }
0641:
0642: Container deepestParent = deepestComp.getParent();
0643: Point deepestPosition = SwingUtilities.convertPoint(parent,
0644: point, deepestParent);
0645: java.util.List<Component> compList = null; // in most cases there will be just one component
0646: for (int i = 0, n = deepestParent.getComponentCount(); i < n; i++) {
0647: Component comp = deepestParent.getComponent(i);
0648: Point p = comp.getLocation();
0649: if (comp != deepestComp
0650: && comp.isVisible()
0651: && comp.contains(deepestPosition.x - p.x,
0652: deepestPosition.y - p.y)) {
0653: if (compList == null) {
0654: compList = new ArrayList<Component>(n - i + 1);
0655: compList.add(deepestComp);
0656: }
0657: compList.add(comp);
0658: }
0659: }
0660:
0661: if (compList == null) { // just one component
0662: return new Component[] { deepestComp };
0663: } else {
0664: return compList.toArray(new Component[compList.size()]);
0665: }
0666: }
0667:
0668: private RADVisualContainer getMetaContainerAt(Point point, int mode) {
0669: RADComponent metacomp = getMetaComponentAt(point, mode);
0670: if (metacomp == null)
0671: return null;
0672: if (metacomp instanceof RADVisualContainer)
0673: return (RADVisualContainer) metacomp;
0674: if (metacomp instanceof RADVisualComponent)
0675: return (RADVisualContainer) metacomp.getParentComponent();
0676: return null;
0677: }
0678:
0679: /** Selects component at the position e.getPoint() on component layer.
0680: * What component is selected further depends on whether CTRL or ALT
0681: * keys are hold. */
0682: private RADComponent selectComponent(MouseEvent e,
0683: boolean mousePressed) {
0684: RADComponent hitMetaComp;
0685: if (formDesigner.getSelectedComponents().size() > 1
0686: && mousePressed && !e.isShiftDown()
0687: && !e.isControlDown() && !e.isAltDown()) {
0688: // If multiple components already selected and some of them is on
0689: // current mouse position, keep this component selected on mouse
0690: // pressed (i.e. don't try to selected a possible subcomponent).
0691: // This is to ease dragging of multiple scrollpanes or containers
0692: // covered entirely by subcomponents.
0693: // OTOH mouse release should cancel the multiselection - if no
0694: // dragging happened.
0695: hitMetaComp = selectedComponentAt(e.getPoint(), 0, false);
0696: if (hitMetaComp != null) {
0697: return hitMetaComp;
0698: }
0699: }
0700:
0701: int selMode = !e.isAltDown() ? COMP_DEEPEST
0702: : (!e.isShiftDown() ? COMP_ABOVE_SELECTED
0703: : COMP_UNDER_SELECTED);
0704: hitMetaComp = getMetaComponentAt(e.getPoint(), selMode);
0705:
0706: // Help with selecting a component in scroll pane (e.g. JTable of zero size).
0707: // Prefer selcting the component rather than the scrollpane if the view port
0708: // or header is clicked.
0709: if (hitMetaComp != null && !e.isAltDown()
0710: && hitMetaComp.getAuxValue("autoScrollPane") != null // NOI18N
0711: && hitMetaComp instanceof RADVisualContainer) {
0712: RADVisualComponent[] sub = ((RADVisualContainer) hitMetaComp)
0713: .getSubComponents();
0714: Component scroll = (Component) formDesigner
0715: .getComponent(hitMetaComp);
0716: if (sub.length > 0 && scroll instanceof JScrollPane) {
0717: Point p = e.getPoint();
0718: convertPointToComponent(p, scroll);
0719: Component clicked = SwingUtilities
0720: .getDeepestComponentAt(scroll, p.x, p.y);
0721: while (clicked != null && clicked != scroll) {
0722: if (clicked instanceof JViewport) {
0723: hitMetaComp = sub[0];
0724: break;
0725: }
0726: clicked = clicked.getParent();
0727: }
0728: }
0729: }
0730:
0731: if ((e.isControlDown() || e.isShiftDown()) && !e.isAltDown()) {
0732: if (hitMetaComp != null) {
0733: // Shift adds to selection, Ctrl toggles selection,
0734: // other components selection is not affected
0735: if (!formDesigner.isComponentSelected(hitMetaComp)) {
0736: formDesigner.addComponentToSelection(hitMetaComp);
0737: } else if (!e.isShiftDown()) {
0738: formDesigner
0739: .removeComponentFromSelection(hitMetaComp);
0740: }
0741: }
0742: } else if (hitMetaComp != null) {
0743: formDesigner.setSelectedComponent(hitMetaComp);
0744: } else {
0745: formDesigner.clearSelection();
0746: }
0747:
0748: return hitMetaComp;
0749: }
0750:
0751: /* private RADComponent[] getComponentsIntervalToSelect(
0752: RADComponent clickedComp,
0753: boolean forward)
0754: {
0755: if (!(clickedComp instanceof RADVisualComponent))
0756: return null;
0757:
0758: java.util.List toSelect = new LinkedList();
0759: RADVisualComponent comp = (RADVisualComponent) clickedComp;
0760: boolean selected = false;
0761:
0762: do // starting with clickedComp,
0763: { // go forward/backward in components until a selected one is reached
0764: if (forward)
0765: toSelect.add(comp);
0766: else
0767: toSelect.add(0, comp);
0768:
0769: comp = formDesigner.getNextVisualComponent(comp, forward);
0770: }
0771: while (comp != null && comp != clickedComp
0772: && !(selected = formDesigner.isComponentSelected(comp))
0773: && comp != formDesigner.getTopDesignComponent());
0774:
0775: if (selected) { // selected component found - we can make the interval
0776: if (comp == formDesigner.getTopDesignComponent()) {
0777: if (!forward) // top component is fine when going backward
0778: toSelect.add(0, comp);
0779: }
0780: else { // add also already selected components in the direction
0781: selected = false;
0782: do {
0783: if (forward)
0784: toSelect.add(comp);
0785: else
0786: toSelect.add(0, comp);
0787:
0788: comp = formDesigner.getNextVisualComponent(comp, forward);
0789: }
0790: while (comp != null
0791: && (selected = formDesigner.isComponentSelected(comp))
0792: && comp != formDesigner.getTopDesignComponent());
0793:
0794: if (selected && !forward)
0795: toSelect.add(0, comp); // top comp is fine when going backward
0796: }
0797:
0798: RADComponent[] compArray = new RADComponent[toSelect.size()];
0799: toSelect.toArray(compArray);
0800: return compArray;
0801: }
0802:
0803: return null;
0804: } */
0805:
0806: private void selectOtherComponentsNode() {
0807: FormEditor formEditor = formDesigner.getFormEditor();
0808: ComponentInspector ci = ComponentInspector.getInstance();
0809: Node[] selectedNode = new Node[] { formEditor
0810: .getOthersContainerNode() };
0811:
0812: try {
0813: ci.setSelectedNodes(selectedNode, formEditor);
0814: formDesigner.clearSelectionImpl();
0815: formDesigner.repaintSelection();
0816: } catch (java.beans.PropertyVetoException ex) {
0817: org.openide.ErrorManager.getDefault().notify(
0818: org.openide.ErrorManager.INFORMATIONAL, ex);
0819: }
0820:
0821: formDesigner.setActivatedNodes(selectedNode);
0822: }
0823:
0824: private boolean processDoubleClick(MouseEvent e) {
0825: if (e.isShiftDown() || e.isControlDown())
0826: return false;
0827:
0828: RADComponent metacomp = getMetaComponentAt(e.getPoint(),
0829: COMP_SELECTED);
0830: if (metacomp == null)
0831: return true;
0832:
0833: if (e.isAltDown()) {
0834: if (metacomp == formDesigner.getTopDesignComponent()) {
0835: metacomp = metacomp.getParentComponent();
0836: if (metacomp == null)
0837: return true;
0838: } else
0839: return false;
0840: }
0841:
0842: Node node = metacomp.getNodeReference();
0843: if (node != null) {
0844: Action action = node.getPreferredAction();
0845: if (action != null) {// && action.isEnabled()) {
0846: action.actionPerformed(new ActionEvent(node,
0847: ActionEvent.ACTION_PERFORMED, "")); // NOI18N
0848: prevLeftMousePoint = null; // to prevent inplace editing on mouse release
0849: return true;
0850: }
0851: }
0852:
0853: return false;
0854: }
0855:
0856: private void processMouseClickInLayoutSupport(
0857: RADComponent metacomp, MouseEvent e) {
0858: if (formDesigner.getMenuEditLayer().isVisible()) {
0859: if (!formDesigner.getMenuEditLayer().isMenuLayerComponent(
0860: metacomp)) {
0861: formDesigner.getMenuEditLayer().hideMenuLayer();
0862: }
0863: }
0864: if (metacomp != null
0865: && metacomp.getBeanClass().getName().equals(
0866: javax.swing.JMenu.class.getName())) {
0867: formDesigner.openMenu(metacomp);
0868: }
0869: if (!(metacomp instanceof RADVisualComponent))
0870: return;
0871:
0872: RADVisualContainer metacont = metacomp instanceof RADVisualContainer ? (RADVisualContainer) metacomp
0873: : (RADVisualContainer) metacomp.getParentComponent();
0874: LayoutSupportManager laysup = metacont != null ? metacont
0875: .getLayoutSupport() : null;
0876: if (laysup == null)
0877: return;
0878:
0879: Container cont = (Container) formDesigner
0880: .getComponent(metacont);
0881: Container contDelegate = metacont.getContainerDelegate(cont);
0882: Point p = convertPointToComponent(e.getPoint(), contDelegate);
0883: laysup.processMouseClick(p, cont, contDelegate);
0884: }
0885:
0886: private void showContextMenu(Point popupPos) {
0887: ComponentInspector inspector = ComponentInspector.getInstance();
0888: TopComponent activated = TopComponent.getRegistry()
0889: .getActivated();
0890: if (activated != formDesigner.multiViewObserver
0891: .getTopComponent()
0892: && activated != inspector)
0893: return;
0894:
0895: formDesigner.componentActivated(); // just for sure...
0896:
0897: Node[] selectedNodes = inspector.getSelectedNodes();
0898: JPopupMenu popup = NodeOp.findContextMenu(selectedNodes);
0899: if (popup != null) {
0900: popup.show(HandleLayer.this , popupPos.x, popupPos.y);
0901: }
0902: }
0903:
0904: // --------
0905:
0906: private boolean anyDragger() {
0907: return draggedComponent != null || selectionDragger != null;
0908: }
0909:
0910: private RADVisualComponent[] getComponentsToDrag() {
0911: // all selected components must be visible in the designer and have the
0912: // same parent; redundant sub-contained components must be filtered out
0913: java.util.List<RADComponent> selectedComps = formDesigner
0914: .getSelectedComponents();
0915: java.util.List<RADComponent> workingComps = new ArrayList<RADComponent>(
0916: selectedComps.size());
0917: java.util.List<String> workingIds = null;
0918: RADVisualContainer parent = null;
0919:
0920: //outside of a frame, there are no selected components so just return null
0921: if (selectedComps.size() == 0)
0922: return null;
0923:
0924: for (Iterator it = selectedComps.iterator(); it.hasNext();) {
0925: RADComponent metacomp = (RADComponent) it.next();
0926: if (!(metacomp instanceof RADVisualComponent))
0927: continue;
0928: boolean subcontained = false;
0929: for (Iterator it2 = selectedComps.iterator(); it2.hasNext();) {
0930: RADComponent metacomp2 = (RADComponent) it2.next();
0931: if (metacomp2 != metacomp
0932: && metacomp2.isParentComponent(metacomp)) {
0933: subcontained = true;
0934: break;
0935: }
0936: }
0937: if (!subcontained) {
0938: RADVisualContainer metacont = (RADVisualContainer) metacomp
0939: .getParentComponent();
0940:
0941: if (substituteForContainer(metacont)) {
0942: // hack: if trying to drag something in scrollpane,
0943: // drag the whole scrollpane instead
0944: metacomp = metacont;
0945: metacont = (RADVisualContainer) metacomp
0946: .getParentComponent();
0947: }
0948:
0949: if (parent != null) {
0950: if (parent != metacont)
0951: return null; // components in different containers
0952: } else {
0953: if (metacont == null
0954: || !formDesigner
0955: .isInDesigner((RADVisualComponent) metacont)) {
0956: return null; // out of visible tree
0957: }
0958: parent = metacont;
0959: if (metacont.getLayoutSupport() == null) { // new layout
0960: workingIds = new ArrayList<String>(
0961: selectedComps.size());
0962: }
0963: }
0964: workingComps.add(metacomp);
0965: if (workingIds != null) {
0966: workingIds.add(metacomp.getId());
0967: }
0968: }
0969: }
0970:
0971: if (parent != null && parent.getLayoutSupport() == null) { // new layout may impose more limitation
0972: workingIds = formDesigner.getLayoutDesigner()
0973: .getDraggableComponents(workingIds);
0974: if (workingIds.size() != workingComps.size()) {
0975: workingComps.clear();
0976: FormModel formModel = getFormModel();
0977: for (String compId : workingIds) {
0978: workingComps
0979: .add(formModel.getMetaComponent(compId));
0980: }
0981: }
0982: }
0983:
0984: return workingComps.isEmpty() ? null : workingComps
0985: .toArray(new RADVisualComponent[workingComps.size()]);
0986: }
0987:
0988: boolean endDragging(MouseEvent e) {
0989: if (!anyDragger())
0990: return false;
0991:
0992: if (resizeType != 0) {
0993: resizeType = 0;
0994: Cursor cursor = getCursor();
0995: if (cursor != null
0996: && cursor.getType() != Cursor.DEFAULT_CURSOR)
0997: setCursor(Cursor.getDefaultCursor());
0998: if (getToolTipText() != null)
0999: setToolTipText(null);
1000: }
1001:
1002: boolean done = true;
1003:
1004: if (draggedComponent != null) {
1005: boolean retVal = true;
1006: try {
1007: retVal = draggedComponent.end(e);
1008: } finally {
1009: if (retVal) {
1010: draggedComponent = null;
1011: draggingEnded = true;
1012: repaint();
1013: } else {
1014: done = false;
1015: }
1016: }
1017: } else if (selectionDragger != null) {
1018: if (e != null)
1019: selectionDragger.drop(e.getPoint());
1020: selectionDragger = null;
1021: // repaint();
1022: }
1023:
1024: if (done) {
1025: draggingEnded = true;
1026: StatusDisplayer.getDefault().setStatusText(""); // NOI18N
1027: }
1028:
1029: FormEditor.getAssistantModel(getFormModel()).setContext(
1030: "select"); // NOI18N
1031: return done;
1032: }
1033:
1034: private boolean isDraggedComponent(RADComponent metacomp) {
1035: if (draggedComponent != null
1036: && draggedComponent.movingComponents != null) {
1037: for (RADComponent c : draggedComponent.movingComponents) {
1038: if (c == metacomp || c.isParentComponent(metacomp))
1039: return true;
1040: }
1041: }
1042: return false;
1043: }
1044:
1045: // Highlighted panel
1046: private JPanel darkerPanel = null;
1047:
1048: private static class HighlightBorder extends
1049: javax.swing.border.LineBorder {
1050: HighlightBorder(Color color, int thickness) {
1051: super (color, thickness);
1052: }
1053:
1054: @Override
1055: public Insets getBorderInsets(Component c) {
1056: // Hack - don't affect component's content
1057: return new Insets(0, 0, 0, 0);
1058: }
1059: }
1060:
1061: // Highlights panel below mouse cursor.
1062: private void highlightPanel(MouseEvent e, boolean recheck) {
1063: Component[] comps = getDeepestComponentsAt(formDesigner
1064: .getComponentLayer(), e.getPoint());
1065: if (comps == null) {
1066: return;
1067: }
1068: Component comp = comps[comps.length - 1];
1069: RADComponent radcomp = formDesigner.getMetaComponent(comp);
1070: if ((radcomp != null)
1071: && !(radcomp instanceof RADVisualContainer)) {
1072: radcomp = radcomp.getParentComponent();
1073: comp = radcomp != null ? (Component) formDesigner
1074: .getComponent(radcomp) : null;
1075: }
1076: if ((radcomp == null)
1077: || (radcomp == formDesigner.getTopDesignComponent())
1078: || (!(comp instanceof JPanel))) {
1079: comp = null;
1080: }
1081: JPanel panel = (JPanel) comp;
1082: if ((darkerPanel != panel)
1083: || (recheck && !shouldHighlightPanel(panel, radcomp))) {
1084: if (darkerPanel != null) {
1085: // Reset only HighlightBorder border
1086: if (darkerPanel.getBorder() instanceof HighlightBorder) {
1087: darkerPanel.setBorder(null);
1088: }
1089: darkerPanel = null;
1090: }
1091: if (shouldHighlightPanel(panel, radcomp)) {
1092: panel.setBorder(new HighlightBorder(
1093: darkerPanelColor(panel.getBackground()), 1));
1094: darkerPanel = panel;
1095: }
1096: }
1097: }
1098:
1099: private boolean shouldHighlightPanel(JPanel panel,
1100: RADComponent radPanel) {
1101: if (panel != null) {
1102: if (panel.getBorder() != null) { // Maybe we should highlight also panels with EmptyBorder
1103: return false;
1104: }
1105: if (!(panel.getBackground() instanceof javax.swing.plaf.UIResource)) {
1106: return false;
1107: }
1108: if (radPanel == formDesigner.getTopDesignComponent()) {
1109: return false;
1110: }
1111: if ((formDesigner.getDesignerMode() == FormDesigner.MODE_SELECT)
1112: && formDesigner.getSelectedLayoutComponents()
1113: .contains(radPanel)) {
1114: return false;
1115: }
1116: if (radPanel instanceof RADVisualContainer) {
1117: RADVisualContainer metacont = (RADVisualContainer) radPanel;
1118: RADVisualContainer parent = metacont
1119: .getParentContainer();
1120: if (parent != null) {
1121: LayoutSupportManager manager = parent
1122: .getLayoutSupport();
1123: if ((manager != null) && manager.isDedicated()) {
1124: return false;
1125: }
1126: JPanel realPanel = (JPanel) formDesigner
1127: .getComponent(radPanel);
1128: Component parentBean = (Component) parent
1129: .getBeanInstance();
1130: Component realParent = (Component) formDesigner
1131: .getComponent(parent);
1132: if (realParent.getSize()
1133: .equals(realPanel.getSize())
1134: && realPanel.getLocation().equals(
1135: new Point(0, 0))) {
1136: if (parentBean instanceof JPanel) {
1137: return shouldHighlightPanel(
1138: (JPanel) parentBean, parent);
1139: } else {
1140: return false;
1141: }
1142: }
1143: }
1144: }
1145: }
1146: return (panel != null);
1147: }
1148:
1149: private static Color darkerPanelColor(Color color) {
1150: double factor = 0.9;
1151: return new Color((int) (color.getRed() * factor), (int) (color
1152: .getGreen() * factor), (int) (color.getBlue() * factor));
1153: }
1154:
1155: // Check the mouse cursor if it is at position where a component or the
1156: // designer can be resized. Change mouse cursor accordingly.
1157: private void checkResizing(MouseEvent e) {
1158: if (formDesigner.getTopDesignComponent() == null)
1159: return; // bean forms
1160: int resizing = checkComponentsResizing(e);
1161: if (resizing == 0) {
1162: resizing = checkDesignerResizing(e);
1163: if (resizing == 0) {
1164: if (getToolTipText() != null)
1165: setToolTipText(null);
1166: } else if (getToolTipText() == null) {
1167: Dimension size = formDesigner.getComponentLayer()
1168: .getDesignerSize();
1169:
1170: MessageFormat mf;
1171: if (viewOnly) {
1172: if (sizeHintFormat == null) {
1173: sizeHintFormat = new MessageFormat(
1174: FormUtils
1175: .getBundleString("FMT_HINT_DesignerSize")); // NOI18N
1176: }
1177: mf = sizeHintFormat;
1178: } else {
1179: if (resizingHintFormat == null) {
1180: resizingHintFormat = new MessageFormat(
1181: FormUtils
1182: .getBundleString("FMT_HINT_DesignerResizing")); // NOI18N
1183: }
1184: mf = resizingHintFormat;
1185: }
1186:
1187: String hint = mf.format(new Object[] {
1188: new Integer(size.width),
1189: new Integer(size.height) });
1190: setToolTipText(hint);
1191: ToolTipManager.sharedInstance().mouseEntered(e);
1192: }
1193: } else if (getToolTipText() != null)
1194: setToolTipText(null);
1195:
1196: if (resizing != 0 && !viewOnly)
1197: setResizingCursor(resizing);
1198: else {
1199: Cursor cursor = getCursor();
1200: if (cursor != null
1201: && cursor.getType() != Cursor.DEFAULT_CURSOR)
1202: setCursor(Cursor.getDefaultCursor());
1203: }
1204: }
1205:
1206: // Check the mouse cursor if it is at position where designer can be
1207: // resized.
1208: private int checkDesignerResizing(MouseEvent e) {
1209: if (!e.isAltDown() && !e.isControlDown() && !e.isShiftDown()) {
1210: ComponentLayer compLayer = formDesigner.getComponentLayer();
1211: int resizing = getSelectionResizable(e.getPoint(),
1212: compLayer.getComponentContainer(), compLayer
1213: .getDesignerOutsets().right + 2);
1214:
1215: resizeType = validDesignerResizing(resizing) ? resizing
1216: | DESIGNER_RESIZING : 0;
1217: } else
1218: resizeType = 0;
1219:
1220: return resizeType;
1221: }
1222:
1223: // Check whether given resize type is valid for designer.
1224: private boolean validDesignerResizing(int resizing) {
1225: return resizing == (LayoutSupportManager.RESIZE_DOWN | LayoutSupportManager.RESIZE_RIGHT)
1226: || resizing == LayoutSupportManager.RESIZE_DOWN
1227: || resizing == LayoutSupportManager.RESIZE_RIGHT;
1228: }
1229:
1230: // Check the mouse cursor if it is at position where a component (or more
1231: // components) can be resized.
1232: private int checkComponentsResizing(MouseEvent e) {
1233: resizeType = 0;
1234: if (e.isAltDown() || e.isControlDown() || e.isShiftDown()) {
1235: return 0;
1236: }
1237:
1238: // check selected components whether they are in the same container
1239: if (!selectedComponentsInSameVisibleContainer())
1240: return 0;
1241:
1242: Point p = e.getPoint();
1243: RADComponent compAtPoint = selectedComponentAt(p, 6, true);
1244:
1245: if (!(compAtPoint instanceof RADVisualComponent))
1246: return 0;
1247:
1248: /* if (!formDesigner.isComponentSelected(compAtPoint)) {
1249: // not on a selected component, but some might be near
1250: RADVisualComponent[] otherComps;
1251: if (compAtPoint instanceof RADVisualContainer) {
1252: otherComps = ((RADVisualContainer)compAtPoint).getSubComponents();
1253: }
1254: else {
1255: RADVisualContainer metacont = (RADVisualContainer)
1256: compAtPoint.getParentComponent();
1257: if (metacont != null)
1258: otherComps = metacont.getSubComponents();
1259: else
1260: return 0; // component without a parent
1261: }
1262:
1263: for (int i=0; i < otherComps.length; i++) {
1264: if (formDesigner.isComponentSelected(otherComps[i])) {
1265: resizeType = getComponentResizable(p, otherComps[i]);
1266: if (resizeType != 0)
1267: break;
1268: }
1269: }
1270: }
1271: else { // mouse on selected component */
1272: resizeType = getComponentResizable(p,
1273: (RADVisualComponent) compAtPoint);
1274: // }
1275:
1276: return resizeType;
1277: }
1278:
1279: private boolean selectedComponentsInSameVisibleContainer() {
1280: RADVisualContainer parent = null;
1281: Iterator selected = formDesigner.getSelectedComponents()
1282: .iterator();
1283: while (selected.hasNext()) {
1284: RADVisualComponent comp = formDesigner
1285: .componentToLayoutComponent((RADComponent) selected
1286: .next());
1287: if (comp == null)
1288: return false; // not visible in designer
1289: if (parent == null) {
1290: parent = comp.getParentContainer();
1291: if (!formDesigner.isInDesigner(parent)) {
1292: return false; // not visible in designer
1293: }
1294: } else if (comp.getParentContainer() != parent) {
1295: return false; // different parent
1296: }
1297: }
1298: return true;
1299: }
1300:
1301: // Returns selected component at the given point (even outside the designer area).
1302: private RADComponent selectedComponentAt(Point p, int borderSize,
1303: boolean inLayout) {
1304: RADComponent compAtPoint = null;
1305: Iterator selected = (inLayout ? formDesigner
1306: .getSelectedLayoutComponents() : formDesigner
1307: .getSelectedComponents()).iterator();
1308: while (selected.hasNext()) {
1309: RADComponent metacomp = (RADComponent) selected.next();
1310: if (metacomp instanceof RADVisualComponent
1311: && formDesigner
1312: .isInDesigner((RADVisualComponent) metacomp)) {
1313: Component comp = (Component) formDesigner
1314: .getComponent(metacomp);
1315: Rectangle rect = new Rectangle(-borderSize,
1316: -borderSize, comp.getWidth() + 2 * borderSize,
1317: comp.getHeight() + 2 * borderSize);
1318: convertRectangleFromComponent(rect, comp);
1319: if (rect.contains(p)) {
1320: compAtPoint = metacomp;
1321: }
1322: }
1323: }
1324: return compAtPoint;
1325: }
1326:
1327: // Check how possible component resizing (obtained from layout support)
1328: // matches with mouse position on component selection border.
1329: private int getComponentResizable(Point p,
1330: RADVisualComponent metacomp) {
1331: // RADVisualContainer metacont = metacomp.getParentContainer();
1332: // if (substituteForContainer(metacont)) {
1333: // metacomp = metacont;
1334: // metacont = metacomp.getParentContainer();
1335: // }
1336:
1337: int resizable = getComponentResizable(metacomp);
1338: if (resizable != 0) {
1339: Component comp = (Component) formDesigner
1340: .getComponent(metacomp);
1341: resizable &= getSelectionResizable(p, comp, 6);
1342: }
1343:
1344: return resizable;
1345: }
1346:
1347: private int getComponentResizable(RADVisualComponent metacomp) {
1348: RADVisualContainer metacont = metacomp.getParentContainer();
1349: if (metacont == null
1350: || metacomp == formDesigner.getTopDesignComponent()) {
1351: return 0;
1352: }
1353: Component comp = (Component) formDesigner
1354: .getComponent(metacomp);
1355: int resizable = 0;
1356: LayoutSupportManager laySup = metacont.getLayoutSupport();
1357: if (laySup == null) { // new layout support
1358: java.util.List selectedComps = formDesigner
1359: .getSelectedComponents();
1360: if (selectedComps.size() == 1) {
1361: // [real resizability spec TBD]
1362: resizable = LayoutSupportManager.RESIZE_LEFT
1363: | LayoutSupportManager.RESIZE_RIGHT
1364: | LayoutSupportManager.RESIZE_UP
1365: | LayoutSupportManager.RESIZE_DOWN;
1366: }
1367: } else { // old layout support
1368: Container cont = (Container) formDesigner
1369: .getComponent(metacont);
1370: if (cont != null) { // might be null if component just enclosed in container not yet cloned
1371: Container contDel = metacont.getContainerDelegate(cont);
1372:
1373: resizable = laySup.getResizableDirections(cont,
1374: contDel, comp, metacont.getIndexOf(metacomp));
1375: }
1376: }
1377: return resizable;
1378: }
1379:
1380: // Compute possible resizing directions according to mouse position on
1381: // component selection border.
1382: private int getSelectionResizable(Point p, Component comp,
1383: int borderWidth) {
1384: if (comp == null)
1385: return 0;
1386:
1387: int resizable = 0;
1388:
1389: Rectangle r = new Rectangle(0, 0, comp.getWidth(), comp
1390: .getHeight());
1391: convertRectangleFromComponent(r, comp);
1392: r.grow(borderWidth, borderWidth);
1393: if (r.contains(p)) {
1394: r.grow(-borderWidth, -borderWidth);
1395: r.grow(-3, -3);
1396: if (r.width < 0)
1397: r.width = 0;
1398: if (r.height < 0)
1399: r.height = 0;
1400:
1401: if (p.y >= r.y + r.height)
1402: resizable |= LayoutSupportManager.RESIZE_DOWN;
1403: else if (p.y < r.y)
1404: resizable |= LayoutSupportManager.RESIZE_UP;
1405: if (p.x >= r.x + r.width)
1406: resizable |= LayoutSupportManager.RESIZE_RIGHT;
1407: else if (p.x < r.x)
1408: resizable |= LayoutSupportManager.RESIZE_LEFT;
1409: }
1410:
1411: return resizable;
1412: }
1413:
1414: private void setResizingCursor(int resizeType) {
1415: Cursor cursor = null;
1416: if ((resizeType & LayoutSupportManager.RESIZE_UP) != 0) {
1417: if ((resizeType & LayoutSupportManager.RESIZE_LEFT) != 0)
1418: cursor = Cursor
1419: .getPredefinedCursor(Cursor.NW_RESIZE_CURSOR);
1420: else if ((resizeType & LayoutSupportManager.RESIZE_RIGHT) != 0)
1421: cursor = Cursor
1422: .getPredefinedCursor(Cursor.NE_RESIZE_CURSOR);
1423: else
1424: cursor = Cursor
1425: .getPredefinedCursor(Cursor.N_RESIZE_CURSOR);
1426: } else if ((resizeType & LayoutSupportManager.RESIZE_DOWN) != 0) {
1427: if ((resizeType & LayoutSupportManager.RESIZE_LEFT) != 0)
1428: cursor = Cursor
1429: .getPredefinedCursor(Cursor.SW_RESIZE_CURSOR);
1430: else if ((resizeType & LayoutSupportManager.RESIZE_RIGHT) != 0)
1431: cursor = Cursor
1432: .getPredefinedCursor(Cursor.SE_RESIZE_CURSOR);
1433: else
1434: cursor = Cursor
1435: .getPredefinedCursor(Cursor.S_RESIZE_CURSOR);
1436: } else if ((resizeType & LayoutSupportManager.RESIZE_LEFT) != 0)
1437: cursor = Cursor.getPredefinedCursor(Cursor.W_RESIZE_CURSOR);
1438: else if ((resizeType & LayoutSupportManager.RESIZE_RIGHT) != 0)
1439: cursor = Cursor.getPredefinedCursor(Cursor.E_RESIZE_CURSOR);
1440:
1441: if (cursor == null)
1442: cursor = Cursor.getDefaultCursor();
1443:
1444: setCursor(cursor);
1445: }
1446:
1447: private void setUserDesignerSize() {
1448: NotifyDescriptor.InputLine input = new NotifyDescriptor.InputLine(
1449: FormUtils.getBundleString("CTL_SetDesignerSize_Label"), // NOI18N
1450: FormUtils.getBundleString("CTL_SetDesignerSize_Title")); // NOI18N
1451: Dimension size = formDesigner.getComponentLayer()
1452: .getDesignerSize();
1453: input.setInputText(Integer.toString(size.width) + ", " // NOI18N
1454: + Integer.toString(size.height));
1455:
1456: if (DialogDisplayer.getDefault().notify(input) == NotifyDescriptor.OK_OPTION) {
1457: String txt = input.getInputText();
1458: int i = txt.indexOf(',');
1459: if (i > 0) {
1460: int n = txt.length();
1461: try {
1462: int w = Integer.parseInt(txt.substring(0, i));
1463: while (++i < n && txt.charAt(i) == ' ')
1464: ;
1465: int h = Integer.parseInt(txt.substring(i, n));
1466: if (w >= 0 && h >= 0) {
1467: size = new Dimension(w, h);
1468: formDesigner.setDesignerSize(size, null);
1469: setToolTipText(null);
1470: setCursor(Cursor.getDefaultCursor());
1471: }
1472: } catch (NumberFormatException ex) {
1473: } // silently ignore, do nothing
1474: }
1475: }
1476: }
1477:
1478: private Object getConstraintsAtPoint(RADComponent metacomp,
1479: Point point, Point hotSpot) {
1480: if (!(metacomp instanceof RADVisualComponent))
1481: return null;
1482:
1483: RADVisualContainer metacont = metacomp instanceof RADVisualContainer ? (RADVisualContainer) metacomp
1484: : (RADVisualContainer) metacomp.getParentComponent();
1485: LayoutSupportManager laysup = metacont != null ? metacont
1486: .getLayoutSupport() : null;
1487:
1488: Container cont = (Container) formDesigner
1489: .getComponent(metacont);
1490: Container contDel = metacont.getContainerDelegate(cont);
1491: Point p = convertPointToComponent(point.x, point.y, contDel);
1492: Object constraints = laysup.getNewConstraints(cont, contDel,
1493: null, -1, p, hotSpot);
1494: if ((constraints == null)
1495: && metacomp.getBeanInstance() instanceof Component) {
1496: int index = laysup.getNewIndex(cont, contDel,
1497: (Component) metacomp.getBeanInstance(), -1, p,
1498: hotSpot);
1499: if (index != -1) {
1500: constraints = new Integer(index);
1501: }
1502: }
1503: return constraints;
1504: }
1505:
1506: private static boolean substituteForContainer(
1507: RADVisualContainer metacont) {
1508: return metacont != null
1509: && metacont.getBeanClass().isAssignableFrom(
1510: JScrollPane.class)
1511: && metacont.getSubComponents().length > 0;
1512: }
1513:
1514: // ------
1515:
1516: boolean mouseOnVisual(Point p) {
1517: Rectangle r = formDesigner.getComponentLayer()
1518: .getDesignerOuterBounds();
1519: return r.contains(p);
1520: }
1521:
1522: /**
1523: * Determines whether the passed point is above the non-visual tray.
1524: *
1525: * @return <code>true</code> if the point is above the non-visual tray,
1526: * returns <code>false</code> otherwise.
1527: */
1528: boolean mouseOnNonVisualTray(Point p) {
1529: Component tray = formDesigner.getNonVisualTray();
1530: return tray != null ? tray.getBounds().contains(p) : false;
1531: }
1532:
1533: // NOTE: does not create a new Point instance
1534: private Point convertPointFromComponent(Point p,
1535: Component sourceComp) {
1536: return formDesigner.pointFromComponentToHandleLayer(p,
1537: sourceComp);
1538: }
1539:
1540: private Point convertPointFromComponent(int x, int y,
1541: Component sourceComp) {
1542: return formDesigner.pointFromComponentToHandleLayer(new Point(
1543: x, y), sourceComp);
1544: }
1545:
1546: // NOTE: does not create a new Point instance
1547: private Point convertPointToComponent(Point p, Component targetComp) {
1548: return formDesigner.pointFromHandleToComponentLayer(p,
1549: targetComp);
1550: }
1551:
1552: private Point convertPointToComponent(int x, int y,
1553: Component targetComp) {
1554: return formDesigner.pointFromHandleToComponentLayer(new Point(
1555: x, y), targetComp);
1556: }
1557:
1558: // NOTE: does not create a new Rectangle instance
1559: private Rectangle convertRectangleFromComponent(Rectangle rect,
1560: Component sourceComp) {
1561: Point p = convertPointFromComponent(rect.x, rect.y, sourceComp);
1562: rect.x = p.x;
1563: rect.y = p.y;
1564: return rect;
1565: }
1566:
1567: // NOTE: does not create a new Rectangle instance
1568: Rectangle convertRectangleToComponent(Rectangle rect,
1569: Component targetComp) {
1570: Point p = convertPointToComponent(rect.x, rect.y, targetComp);
1571: rect.x = p.x;
1572: rect.y = p.y;
1573: return rect;
1574: }
1575:
1576: Rectangle convertVisibleRectangleFromComponent(Rectangle rect,
1577: Component comp) {
1578: Component parent;
1579: while (!formDesigner.isCoordinatesRoot(comp)) {
1580: parent = comp.getParent();
1581: Rectangle size = new Rectangle(0, 0, parent.getWidth(),
1582: parent.getHeight());
1583: rect.translate(comp.getX(), comp.getY());
1584: rect = rect.intersection(size);
1585: comp = parent;
1586: }
1587: comp = this ;
1588: while (!formDesigner.isCoordinatesRoot(comp)) {
1589: rect.translate(-comp.getX(), -comp.getY());
1590: comp = comp.getParent();
1591: }
1592: return rect;
1593: }
1594:
1595: // ---------
1596: // MouseListener implementation
1597:
1598: public void mouseClicked(MouseEvent e) {
1599: if (SwingUtilities.isRightMouseButton(e) && !draggingEnded
1600: && !endDragging(null)) {
1601: if (mouseOnNonVisualTray(e.getPoint())) {
1602: dispatchToNonVisualTray(e);
1603: } else {
1604: showContextMenu(e.getPoint());
1605: }
1606: }
1607: highlightPanel(e, true);
1608: e.consume();
1609: }
1610:
1611: public void mouseReleased(MouseEvent e) {
1612: if (!HandleLayer.this .isVisible())
1613: return;
1614:
1615: if (SwingUtilities.isLeftMouseButton(e)) {
1616: if (formDesigner.getDesignerMode() == FormDesigner.MODE_SELECT
1617: && !draggingEnded && !endDragging(e)) { // there was no dragging, so mouse release may have other meaning
1618: boolean modifier = e.isControlDown() || e.isAltDown()
1619: || e.isShiftDown();
1620: if ((resizeType & DESIGNER_RESIZING) != 0
1621: && e.getClickCount() == 2 && !modifier
1622: && !viewOnly) { // doubleclick on designer's resizing border
1623: setUserDesignerSize();
1624: } else if (mouseOnNonVisualTray(e.getPoint())) {
1625: dispatchToNonVisualTray(e);
1626: } else if (prevLeftMousePoint != null
1627: && prevLeftMousePoint.distance(e.getPoint()) <= 3
1628: && !modifier) { // second click on the same place in a component
1629: RADComponent metacomp = getMetaComponentAt(e
1630: .getPoint(), COMP_SELECTED);
1631: if (metacomp != null) {
1632: formDesigner.startInPlaceEditing(metacomp);
1633: }
1634: } else if (e.getClickCount() == 1 && !e.isAltDown()
1635: && !e.isControlDown()) { // plain click or shift click
1636: selectComponent(e, false);
1637: }
1638: }
1639:
1640: prevLeftMousePoint = lastLeftMousePoint;
1641: lastLeftMousePoint = null;
1642: } else if (mouseOnNonVisualTray(e.getPoint())) {
1643: dispatchToNonVisualTray(e);
1644: }
1645:
1646: e.consume();
1647: }
1648:
1649: public void mouseEntered(MouseEvent e) {
1650: if (formDesigner.getDesignerMode() == FormDesigner.MODE_ADD) {
1651: formDesigner.requestActive();
1652: PaletteItem item = PaletteUtils.getSelectedItem();
1653: if (formDesigner.getMenuEditLayer()
1654: .isPossibleNewMenuComponent(item)) {
1655: formDesigner.getMenuEditLayer()
1656: .startNewMenuComponentPickAndPlop(item,
1657: e.getPoint());
1658: return;
1659: }
1660: if (null != item) {
1661: StatusDisplayer.getDefault().setStatusText(
1662: FormUtils.getFormattedBundleString(
1663: "FMT_MSG_AddingComponent", // NOI18N
1664: new String[] { item.getNode()
1665: .getDisplayName() }));
1666: }
1667: }
1668: }
1669:
1670: public void mouseExited(MouseEvent e) {
1671: if (draggedComponent != null
1672: && formDesigner.getDesignerMode() == FormDesigner.MODE_ADD) {
1673: draggedComponent.move(null);
1674: repaint();
1675: StatusDisplayer.getDefault().setStatusText(""); // NOI18N
1676: }
1677: }
1678:
1679: public void mousePressed(MouseEvent e) {
1680: formDesigner.componentActivated();
1681: if (!HandleLayer.this .isVisible())
1682: return;
1683:
1684: if (SwingUtilities.isRightMouseButton(e)) {
1685: if (formDesigner.getDesignerMode() != FormDesigner.MODE_SELECT) {
1686: formDesigner.toggleSelectionMode(); // calls endDragging(null)
1687: repaint();
1688: } else if (endDragging(null)) { // there was dragging, now canceled
1689: repaint();
1690: } else if (!SwingUtilities.isLeftMouseButton(e)) {
1691: // no dragging, ensure a component is selected for conext menu
1692: if (mouseOnNonVisualTray(e.getPoint())) {
1693: dispatchToNonVisualTray(e);
1694: } else if (!mouseOnVisual(e.getPoint())) {
1695: selectOtherComponentsNode();
1696: } else {
1697: // [we used to only select the component if there was nothing selected
1698: // on current position, but changed to always select - #94543]
1699: RADComponent hitMetaComp = selectComponent(e, true);
1700: processMouseClickInLayoutSupport(hitMetaComp, e);
1701: }
1702: draggingEnded = false; // reset flag preventing dragging from start
1703: }
1704: e.consume();
1705: } else if (SwingUtilities.isLeftMouseButton(e)) {
1706: lastLeftMousePoint = e.getPoint();
1707:
1708: boolean modifier = e.isControlDown() || e.isAltDown()
1709: || e.isShiftDown();
1710:
1711: if (formDesigner.getDesignerMode() == FormDesigner.MODE_SELECT) {
1712: if (mouseOnNonVisualTray(e.getPoint())) {
1713: dispatchToNonVisualTray(e);
1714: } else {
1715: checkResizing(e);
1716: if (!(e.isShiftDown() && e.isAltDown() && e
1717: .isControlDown())) {
1718: if (!mouseOnVisual(lastLeftMousePoint)) {
1719: if ((resizeType == 0)
1720: && (selectedComponentAt(
1721: lastLeftMousePoint, 0, true) == null))
1722: selectOtherComponentsNode();
1723: }
1724: // Shift+left is reserved for interval or area selection,
1725: // applied on mouse release or mouse dragged; ignore it here.
1726: else if (resizeType == 0 // no resizing
1727: && (e.getClickCount() != 2 || !processDoubleClick(e)) // no doubleclick
1728: && (!e.isShiftDown() || e.isAltDown())) {
1729: RADComponent hitMetaComp = selectComponent(
1730: e, true);
1731: if (!modifier) { // plain single click
1732: processMouseClickInLayoutSupport(
1733: hitMetaComp, e);
1734: }
1735: }
1736: }
1737: }
1738: // endDragging(null); // for sure
1739: draggingEnded = false; // reset flag preventing dragging from start
1740: } else if (!viewOnly) { // form can be modified
1741: if (formDesigner.getDesignerMode() == FormDesigner.MODE_CONNECT) {
1742: selectComponent(e, true);
1743: } else if (formDesigner.getDesignerMode() == FormDesigner.MODE_ADD) {
1744: endDragging(e);
1745: if (!e.isShiftDown()) {
1746: formDesigner.toggleSelectionMode();
1747: }
1748: // otherwise stay in adding mode
1749: }
1750: }
1751: e.consume();
1752: }
1753: }
1754:
1755: // ---------
1756: // MouseMotionListener implementation
1757:
1758: public void mouseDragged(MouseEvent e) {
1759: if (formDesigner.getDesignerMode() != FormDesigner.MODE_SELECT)
1760: return; // dragging makes sense only selection mode
1761: Point p = e.getPoint();
1762: if (lastMousePosition != null) {
1763: lastXPosDiff = p.x - lastMousePosition.x;
1764: lastYPosDiff = p.y - lastMousePosition.y;
1765: }
1766:
1767: if (!draggingEnded && !anyDragger()
1768: && lastLeftMousePoint != null) { // no dragging yet
1769: if (!viewOnly
1770: && !e.isControlDown()
1771: && (!e.isShiftDown() || e.isAltDown())
1772: && (resizeType != 0 || lastLeftMousePoint
1773: .distance(p) > 6)) { // start component dragging
1774: RADVisualComponent[] draggedComps = (resizeType & DESIGNER_RESIZING) == 0 ? getComponentsToDrag()
1775: : new RADVisualComponent[] { formDesigner
1776: .getTopDesignComponent() };
1777: if (draggedComps != null) {
1778: if (resizeType == 0) {
1779: draggedComponent = new ExistingComponentDrag(
1780: draggedComps, lastLeftMousePoint, e
1781: .getModifiers());
1782: } else {
1783: draggedComponent = new ResizeComponentDrag(
1784: draggedComps, lastLeftMousePoint,
1785: resizeType & ~DESIGNER_RESIZING);
1786: }
1787: }
1788: }
1789: if (draggedComponent == null // component dragging has not started
1790: && lastLeftMousePoint.distance(p) > 4
1791: && !e.isAltDown() && !e.isControlDown()) {
1792: // check for possible selection dragging
1793: RADComponent topComp = formDesigner
1794: .getTopDesignComponent();
1795: RADComponent comp = getMetaComponentAt(
1796: lastLeftMousePoint, COMP_DEEPEST);
1797: if (topComp != null
1798: && (e.isShiftDown() || comp == null
1799: || comp == topComp || comp
1800: .getParentComponent() == null)) {
1801: // start selection dragging
1802: selectionDragger = new SelectionDragger(
1803: lastLeftMousePoint);
1804: }
1805: }
1806: }
1807:
1808: if (draggedComponent != null) {
1809: draggedComponent.move(e);
1810: highlightPanel(e, false);
1811: repaint();
1812: } else if (selectionDragger != null) {
1813: selectionDragger.drag(p);
1814: repaint();
1815: }
1816:
1817: lastMousePosition = p;
1818: e.consume();
1819: }
1820:
1821: public void mouseMoved(MouseEvent e) {
1822: Point p = e.getPoint();
1823: if (lastMousePosition != null) {
1824: lastXPosDiff = p.x - lastMousePosition.x;
1825: lastYPosDiff = p.y - lastMousePosition.y;
1826: }
1827: if (formDesigner.getDesignerMode() == FormDesigner.MODE_ADD) {
1828: PaletteItem item = PaletteUtils.getSelectedItem();
1829: if (null == item) {
1830: if (null != draggedComponent) {
1831: endDragging(e);
1832: }
1833: return;
1834: }
1835: if (draggedComponent == null) {
1836: // first move event, pre-create visual component to be added
1837: if ((item.getComponentClassName().indexOf('.') == -1) // Issue 79573
1838: && (!FormJavaSource
1839: .isInDefaultPackage(getFormModel()))) {
1840: String message = FormUtils
1841: .getBundleString("MSG_DefaultPackageBean"); // NOI18N
1842: NotifyDescriptor nd = new NotifyDescriptor.Message(
1843: message, NotifyDescriptor.WARNING_MESSAGE);
1844: DialogDisplayer.getDefault().notify(nd);
1845: formDesigner.toggleSelectionMode();
1846: return;
1847: }
1848: draggedComponent = new NewComponentDrag(item);
1849: }
1850: draggedComponent.move(e);
1851: repaint();
1852: } else if (formDesigner.getDesignerMode() == FormDesigner.MODE_SELECT
1853: && !anyDragger()) {
1854: checkResizing(e);
1855: }
1856: highlightPanel(e, false);
1857: lastMousePosition = p;
1858: }
1859:
1860: /**
1861: * Dispatches the mouse event to the non-visual tray.
1862: *
1863: * @param e the event to dispatch.
1864: */
1865: private void dispatchToNonVisualTray(final MouseEvent e) {
1866: NonVisualTray tray = formDesigner.getNonVisualTray();
1867: if (tray == null) {
1868: return;
1869: }
1870: Point point = SwingUtilities.convertPoint(this , e.getPoint(),
1871: tray);
1872: Component component = SwingUtilities.getDeepestComponentAt(
1873: tray, point.x, point.y);
1874: point = SwingUtilities.convertPoint(tray, point, component);
1875: component.dispatchEvent(new MouseEvent(component, e.getID(), e
1876: .getWhen(), e.getModifiers(), point.x, point.y, e
1877: .getClickCount(), e.isPopupTrigger()));
1878: }
1879:
1880: @Override
1881: public String getToolTipText(MouseEvent e) {
1882: if (mouseOnNonVisualTray(e.getPoint())) {
1883: NonVisualTray tray = formDesigner.getNonVisualTray();
1884: Point point = SwingUtilities.convertPoint(this , e
1885: .getPoint(), tray);
1886: JComponent component = (JComponent) SwingUtilities
1887: .getDeepestComponentAt(tray, point.x, point.y);
1888: point = SwingUtilities.convertPoint(tray, point, component);
1889: return component.getToolTipText(new MouseEvent(tray, e
1890: .getID(), e.getWhen(), e.getModifiers(), point.x,
1891: point.y, e.getClickCount(), e.isPopupTrigger()));
1892: } else {
1893: return super .getToolTipText(e);
1894: }
1895: }
1896:
1897: // ----------
1898:
1899: private class SelectionDragger {
1900: private Point startPoint;
1901: private Point lastPoint;
1902:
1903: public SelectionDragger(Point startPoint) {
1904: this .startPoint = startPoint;
1905: }
1906:
1907: public void paintDragFeedback(Graphics g) {
1908: if (startPoint != null && lastPoint != null) {
1909: Rectangle r = getRectangle();
1910: g.drawRect(r.x, r.y, r.width, r.height);
1911: }
1912: }
1913:
1914: public void drag(Point p) {
1915: lastPoint = p;
1916: }
1917:
1918: public void drop(Point endPoint) {
1919: if (startPoint != null && endPoint != null) {
1920: lastPoint = endPoint;
1921: java.util.List<RADComponent> toSelect = new ArrayList<RADComponent>();
1922: collectSelectedComponents(getRectangle(), formDesigner
1923: .getComponentLayer().getComponentContainer(),
1924: toSelect);
1925:
1926: RADComponent[] selected = new RADComponent[toSelect
1927: .size()];
1928: toSelect.toArray(selected);
1929: formDesigner.setSelectedComponents(selected);
1930: }
1931: }
1932:
1933: private Rectangle getRectangle() {
1934: int x = startPoint.x <= lastPoint.x ? startPoint.x
1935: : lastPoint.x;
1936: int y = startPoint.y <= lastPoint.y ? startPoint.y
1937: : lastPoint.y;
1938: int w = lastPoint.x - startPoint.x;
1939: if (w < 0)
1940: w = -w;
1941: int h = lastPoint.y - startPoint.y;
1942: if (h < 0)
1943: h = -h;
1944:
1945: return new Rectangle(x, y, w, h);
1946: }
1947:
1948: private boolean collectSelectedComponents(Rectangle selRect,
1949: Container cont, java.util.List<RADComponent> toSelect) {
1950: java.util.List<Component> subContainers = new ArrayList<Component>();
1951:
1952: Component[] comps;
1953: if (cont instanceof JTabbedPane) {
1954: Component selectedTab = ((JTabbedPane) cont)
1955: .getSelectedComponent();
1956: comps = (selectedTab == null) ? new Component[0]
1957: : new Component[] { selectedTab };
1958: } else {
1959: comps = cont.getComponents();
1960: }
1961: for (int i = 0; i < comps.length; i++) {
1962: Component comp = comps[i];
1963: Rectangle bounds = convertRectangleFromComponent(
1964: comps[i].getBounds(), cont);
1965: boolean intersects = selRect.intersects(bounds);
1966:
1967: RADComponent metacomp = formDesigner
1968: .getMetaComponent(comp);
1969: if (metacomp != null && intersects) {
1970: toSelect.add(metacomp);
1971: }
1972:
1973: if (intersects && comp instanceof Container)
1974: subContainers.add(comp);
1975: }
1976:
1977: if (toSelect.size() > 1
1978: || (toSelect.size() == 1 && subContainers.size() == 0))
1979: return true;
1980:
1981: RADComponent theOnlyOne = toSelect.size() == 1 ? toSelect
1982: .get(0) : null;
1983:
1984: for (int i = 0; i < subContainers.size(); i++) {
1985: toSelect.clear();
1986: if (collectSelectedComponents(selRect,
1987: (Container) subContainers.get(i), toSelect))
1988: return true;
1989: }
1990:
1991: if (theOnlyOne != null) {
1992: toSelect.add(theOnlyOne);
1993: return true;
1994: }
1995:
1996: return false;
1997: }
1998: }
1999:
2000: // -------
2001:
2002: private abstract class ComponentDrag {
2003: RADVisualComponent[] movingComponents;
2004: boolean draggableLayoutComponents;
2005: RADVisualContainer targetContainer;
2006: RADVisualContainer fixedTarget;
2007: Component[] showingComponents;
2008: Rectangle[] originalBounds; // in coordinates of HandleLayer
2009: Rectangle compoundBounds; // compound from original bounds
2010: Rectangle[] movingBounds; // in coordinates of ComponentLayer
2011: Point hotSpot; // in coordinates of ComponentLayer
2012: Point convertPoint; // from HandleLayer to ComponentLayer (top visual component)
2013: boolean newDrag;
2014: boolean oldDrag;
2015: Object layoutUndoMark;
2016: UndoableEdit layoutUndoEdit;
2017:
2018: // ctor for adding new
2019: ComponentDrag() {
2020: if (formDesigner.getTopDesignComponentView() == null) {
2021: convertPoint = new Point(0, 0);
2022: } else {
2023: convertPoint = convertPointFromComponent(0, 0,
2024: formDesigner.getTopDesignComponentView());
2025: }
2026: }
2027:
2028: // ctor for moving and resizing
2029: ComponentDrag(RADVisualComponent[] components, Point hotspot) {
2030: this ();
2031: setMovingComponents(components);
2032:
2033: int count = components.length;
2034: showingComponents = new Component[count]; // [provisional - just one component can be moved]
2035: originalBounds = new Rectangle[count];
2036: movingBounds = new Rectangle[count];
2037: for (int i = 0; i < count; i++) {
2038: showingComponents[i] = (Component) formDesigner
2039: .getComponent(movingComponents[i]);
2040: originalBounds[i] = showingComponents[i].getBounds();
2041: convertRectangleFromComponent(originalBounds[i],
2042: showingComponents[i].getParent());
2043: compoundBounds = compoundBounds != null ? compoundBounds
2044: .union(originalBounds[i])
2045: : originalBounds[i];
2046: movingBounds[i] = new Rectangle();
2047: movingBounds[i].width = originalBounds[i].width;
2048: movingBounds[i].height = originalBounds[i].height;
2049: }
2050:
2051: this .hotSpot = hotspot == null ? new Point(4, 4)
2052: : new Point(hotspot.x - convertPoint.x, hotspot.y
2053: - convertPoint.y);
2054: }
2055:
2056: final void setMovingComponents(RADVisualComponent[] components) {
2057: this .movingComponents = components;
2058: if (components != null && components.length > 0
2059: && components[0] != null) {
2060: draggableLayoutComponents = !components[0]
2061: .isMenuComponent();
2062: } else {
2063: draggableLayoutComponents = false;
2064: }
2065: }
2066:
2067: final RADVisualContainer getSourceContainer() {
2068: return movingComponents != null
2069: && movingComponents.length > 0
2070: && formDesigner.getTopDesignComponent() != movingComponents[0] ? movingComponents[0]
2071: .getParentContainer()
2072: : null;
2073: }
2074:
2075: final boolean isTopComponent() {
2076: return movingComponents != null
2077: && movingComponents.length > 0
2078: && formDesigner.getTopDesignComponent() == movingComponents[0];
2079: }
2080:
2081: final boolean isDraggableLayoutComponent() {
2082: return draggableLayoutComponents;
2083: }
2084:
2085: final RADVisualContainer getTargetContainer(Point p,
2086: int modifiers) {
2087: if (fixedTarget != null) {
2088: return fixedTarget;
2089: }
2090: int mode = ((modifiers & InputEvent.ALT_MASK) != 0) ? COMP_SELECTED
2091: : COMP_DEEPEST;
2092: RADVisualContainer metacont = HandleLayer.this
2093: .getMetaContainerAt(p, mode);
2094: if ((metacont != null)
2095: && (metacont.getLayoutSupport() == null)) {
2096: RADVisualContainer dirMetacont = HandleLayer.this
2097: .getMetaContainerAt(
2098: getMoveDirectionSensitivePoint(p,
2099: modifiers), mode);
2100: if ((dirMetacont != null)
2101: && (dirMetacont.getLayoutSupport() == null)) {
2102: metacont = dirMetacont;
2103: }
2104: }
2105: if (movingComponents != null) {
2106: java.util.List comps = Arrays.asList(movingComponents);
2107: while (comps.contains(metacont)) {
2108: metacont = metacont.getParentContainer();
2109: }
2110: }
2111: if (substituteForContainer(metacont)) {
2112: metacont = metacont.getParentContainer();
2113: }
2114: return metacont;
2115: }
2116:
2117: private Point getMoveDirectionSensitivePoint(Point p,
2118: int modifiers) {
2119: if (lastMousePosition != null
2120: && compoundBounds != null
2121: && (modifiers & (InputEvent.ALT_MASK
2122: | InputEvent.CTRL_MASK | InputEvent.SHIFT_MASK)) == 0) {
2123: if (compoundBounds.width <= 0
2124: || compoundBounds.height <= 0) {
2125: return p;
2126: }
2127: int x;
2128: int y;
2129: if (lastXPosDiff != 0 && lastYPosDiff != 0) {
2130: double dx = lastXPosDiff;
2131: double dy = lastYPosDiff;
2132: double d = Math.abs(dy / dx);
2133: double r = compoundBounds.getHeight()
2134: / compoundBounds.getWidth();
2135: if (d > r) {
2136: x = p.x
2137: + (int) Math.round(compoundBounds
2138: .getHeight()
2139: / d / 2.0)
2140: * (lastXPosDiff > 0 ? 1 : -1);
2141: y = p.y
2142: - convertPoint.y
2143: - hotSpot.y
2144: + compoundBounds.y
2145: + (lastYPosDiff > 0 ? compoundBounds.height
2146: : 0);
2147: } else {
2148: x = p.x
2149: - convertPoint.x
2150: - hotSpot.x
2151: + compoundBounds.x
2152: + (lastXPosDiff > 0 ? compoundBounds.width
2153: : 0);
2154: y = p.y
2155: + (int) Math.round(compoundBounds
2156: .getWidth()
2157: * d / 2.0)
2158: * (lastYPosDiff > 0 ? 1 : -1);
2159: }
2160: } else {
2161: x = lastXPosDiff == 0 ? p.x : p.x
2162: - convertPoint.x
2163: - hotSpot.x
2164: + compoundBounds.x
2165: + (lastXPosDiff > 0 ? compoundBounds.width
2166: : 0);
2167: y = lastYPosDiff == 0 ? p.y : p.y
2168: - convertPoint.y
2169: - hotSpot.y
2170: + compoundBounds.y
2171: + (lastYPosDiff > 0 ? compoundBounds.height
2172: : 0);
2173: }
2174: Rectangle boundaries = formDesigner.getComponentLayer()
2175: .getDesignerInnerBounds();
2176: // don't let the component component fall into non-visual area easily
2177: if (x < boundaries.x && x + 8 >= boundaries.x) {
2178: x = boundaries.x;
2179: } else if (x > boundaries.x + boundaries.width
2180: && x - 8 < boundaries.x + boundaries.width) {
2181: x = boundaries.x + boundaries.width - 1;
2182: }
2183: if (y < boundaries.y && y + 8 >= boundaries.y) {
2184: y = boundaries.y;
2185: } else if (y > boundaries.y + boundaries.height
2186: && y - 8 < boundaries.y + boundaries.height) {
2187: y = boundaries.y + boundaries.height - 1;
2188: }
2189: return new Point(x, y);
2190: } else
2191: return p;
2192: }
2193:
2194: final void move(MouseEvent e) {
2195: if (e == null) {
2196: move(null, 0);
2197: } else {
2198: move(e.getPoint(), e.getModifiers());
2199: }
2200: }
2201:
2202: void move(Point p, int modifiers) {
2203: if (p == null) {
2204: for (int i = 0; i < movingBounds.length; i++) {
2205: movingBounds[i].x = Integer.MIN_VALUE;
2206: }
2207: return;
2208: }
2209:
2210: targetContainer = getTargetContainer(p, modifiers);
2211:
2212: // support for highlights in menu containers
2213: // hack: this only checks the first component.
2214: if (this .movingComponents != null) {
2215: RADVisualComponent moveComp = this .movingComponents[0];
2216: // if have a menu component over a menu container then do a highlight
2217: // hack: this only works for new comps, not moving existing comps
2218: if (newDrag
2219: && formDesigner.getMenuEditLayer()
2220: .canHighlightContainer(targetContainer,
2221: moveComp)) {
2222: formDesigner.getMenuEditLayer().rolloverContainer(
2223: targetContainer);
2224: } else {
2225: formDesigner.getMenuEditLayer().rolloverContainer(
2226: null);
2227: }
2228: }
2229:
2230: if (newDrag && isDraggableLayoutComponent()
2231: && targetContainer != null
2232: && targetContainer.getLayoutSupport() == null) {
2233: p.x -= convertPoint.x;
2234: p.y -= convertPoint.y;
2235: formDesigner.getLayoutDesigner().move(p,
2236: targetContainer.getId(),
2237: ((modifiers & InputEvent.ALT_MASK) == 0),
2238: ((modifiers & InputEvent.CTRL_MASK) != 0),
2239: movingBounds);
2240: String[] position = formDesigner.getLayoutDesigner()
2241: .positionCode();
2242: FormEditor.getAssistantModel(getFormModel())
2243: .setContext(position[0], position[1]);
2244: } else {
2245: if (oldDrag && isDraggableLayoutComponent()
2246: && targetContainer != null
2247: && targetContainer.getLayoutSupport() != null) {
2248: oldMove(p);
2249: } else {
2250: FormEditor.getAssistantModel(getFormModel())
2251: .setContext("generalPosition"); // NOI18N
2252: }
2253: for (int i = 0; i < movingBounds.length; i++) {
2254: movingBounds[i].x = p.x - convertPoint.x
2255: - hotSpot.x + originalBounds[i].x
2256: - convertPoint.x;
2257: movingBounds[i].y = p.y - convertPoint.y
2258: - hotSpot.y + originalBounds[i].y
2259: - convertPoint.y;
2260: }
2261: }
2262: }
2263:
2264: final void maskDraggingComponents() {
2265: if (!isTopComponent() && showingComponents != null) {
2266: for (int i = 0; i < showingComponents.length; i++) {
2267: Rectangle r = movingBounds[i];
2268: showingComponents[i].setBounds(r.x
2269: + Short.MIN_VALUE, r.y + Short.MIN_VALUE,
2270: r.width, r.height);
2271: }
2272: }
2273: }
2274:
2275: final void paintFeedback(Graphics2D g) {
2276: if ((movingBounds.length < 1)
2277: || (movingBounds[0].x == Integer.MIN_VALUE))
2278: return;
2279:
2280: for (int i = 0; i < showingComponents.length; i++) {
2281: Graphics gg = g.create(movingBounds[i].x
2282: + convertPoint.x, movingBounds[i].y
2283: + convertPoint.y, movingBounds[i].width + 1,
2284: movingBounds[i].height + 1);
2285:
2286: if (newDrag
2287: && isDraggableLayoutComponent()
2288: && ((targetContainer != null && targetContainer
2289: .getLayoutSupport() == null) || (targetContainer == null && isTopComponent()))) { // new layout support
2290: // paint the component being moved
2291: if (!isTopComponent()) {
2292: doLayout(showingComponents[i]);
2293: paintDraggedComponent(showingComponents[i], gg);
2294: } // resized top design component is painted automatically
2295:
2296: // paint the selection rectangle
2297: gg.setColor(formSettings.getSelectionBorderColor());
2298: gg.drawRect(0, 0, movingBounds[i].width,
2299: movingBounds[i].height);
2300:
2301: // paint the layout designer feedback
2302: g.translate(convertPoint.x, convertPoint.y);
2303: g.setColor(formSettings.getGuidingLineColor());
2304: formDesigner.getLayoutDesigner().paintMoveFeedback(
2305: g);
2306: g.translate(-convertPoint.x, -convertPoint.y);
2307: } else if (oldDrag
2308: && isDraggableLayoutComponent()
2309: && ((targetContainer != null && targetContainer
2310: .getLayoutSupport() != null) || (targetContainer == null && isTopComponent()))) {
2311: if (!isTopComponent()) {
2312: doLayout(showingComponents[i]);
2313: oldPaintFeedback(g, gg, i);
2314: }
2315: } else { // non-visual area
2316: doLayout(showingComponents[i]);
2317: paintDraggedComponent(showingComponents[i], gg);
2318: }
2319: }
2320: }
2321:
2322: final boolean end(final MouseEvent e) {
2323: dragPanel.removeAll();
2324:
2325: boolean retVal;
2326: if (e == null) {
2327: retVal = end(null, 0);
2328: } else {
2329: retVal = end(e.getPoint(), e.getModifiers());
2330: }
2331: if (retVal) {
2332: movingComponents = null;
2333: targetContainer = null;
2334: fixedTarget = null;
2335: showingComponents = null;
2336: } else {
2337: // re-init in next AWT round - to have the designer updated
2338: EventQueue.invokeLater(new Runnable() {
2339: public void run() {
2340: init();
2341: move(e);
2342: }
2343: });
2344: }
2345: return retVal;
2346: }
2347:
2348: // methods to extend/override ---
2349:
2350: void init() {
2351: if (showingComponents != null) {
2352: // showing components need to be in a container to paint
2353: // correctly (relates to newly added components);
2354: // components in old layout need to be hidden
2355: RADVisualContainer sourceCont = getSourceContainer();
2356: boolean oldSource = sourceCont != null
2357: && sourceCont.getLayoutSupport() != null;
2358: dragPanel.removeAll();
2359: for (int i = 0; i < showingComponents.length; i++) {
2360: Component comp = showingComponents[i];
2361: if (comp.getParent() == null) {
2362: dragPanel.add(comp);
2363: } else if (oldSource) {
2364: comp.setVisible(false);
2365: // VisualReplicator makes it visible again...
2366: }
2367: avoidDoubleBuffering(comp);
2368: }
2369: }
2370: }
2371:
2372: private void avoidDoubleBuffering(Component comp) {
2373: if (comp instanceof JComponent) {
2374: ((JComponent) comp).setDoubleBuffered(false);
2375: }
2376: if (comp instanceof Container) {
2377: Container cont = (Container) comp;
2378: for (int i = 0; i < cont.getComponentCount(); i++) {
2379: avoidDoubleBuffering(cont.getComponent(i));
2380: }
2381: }
2382: }
2383:
2384: boolean end(Point p, int modifiers) {
2385: // clear the rollover just in case it was set
2386: formDesigner.getMenuEditLayer().clearRollover();
2387: return true;
2388: }
2389:
2390: void oldMove(Point p) {
2391: }
2392:
2393: void oldPaintFeedback(Graphics2D g, Graphics gg, int index) {
2394: }
2395:
2396: // layout model undo/redo ---
2397:
2398: final void createLayoutUndoableEdit() {
2399: layoutUndoMark = getLayoutModel().getChangeMark();
2400: layoutUndoEdit = getLayoutModel().getUndoableEdit();
2401: }
2402:
2403: final void placeLayoutUndoableEdit(boolean autoUndo) {
2404: if (!layoutUndoMark
2405: .equals(getLayoutModel().getChangeMark())) {
2406: getFormModel().addUndoableEdit(layoutUndoEdit);
2407: }
2408: if (autoUndo) {
2409: getFormModel().forceUndoOfCompoundEdit();
2410: }
2411: layoutUndoMark = null;
2412: layoutUndoEdit = null;
2413: }
2414: }
2415:
2416: private static void doLayout(Component component) {
2417: if (component instanceof Container) {
2418: Container cont = (Container) component;
2419: cont.doLayout();
2420: for (int i = 0, n = cont.getComponentCount(); i < n; i++) {
2421: doLayout(cont.getComponent(i));
2422: }
2423: }
2424: }
2425:
2426: private static void paintDraggedComponent(Component comp, Graphics g) {
2427: try {
2428: if (comp instanceof JComponent)
2429: comp.paint(g);
2430: else
2431: comp.getPeer().paint(g);
2432: } catch (RuntimeException ex) { // inspired by bug #62041 (JProgressBar bug #5035852)
2433: org.openide.ErrorManager.getDefault().notify(
2434: org.openide.ErrorManager.INFORMATIONAL, ex);
2435: }
2436: }
2437:
2438: // for moving existing components
2439: private class ExistingComponentDrag extends ComponentDrag {
2440: private int modifiers; // for the old layout support
2441: private ComponentDragger oldDragger; // drags components in the old layout support
2442:
2443: ExistingComponentDrag(RADVisualComponent[] comps,
2444: Point hotspot, // in HandleLayer coordinates
2445: int modifiers) {
2446: super (comps, hotspot);
2447: this .modifiers = modifiers;
2448: init();
2449: }
2450:
2451: @Override
2452: void init() {
2453: RADVisualContainer metacont = getSourceContainer();
2454: String[] compIds = new String[showingComponents.length];
2455: for (int i = 0; i < showingComponents.length; i++) {
2456: compIds[i] = movingComponents[i].getId();
2457: originalBounds[i].x -= convertPoint.x;
2458: originalBounds[i].y -= convertPoint.y;
2459: }
2460:
2461: if (metacont.getLayoutSupport() == null) { // new layout support
2462: formDesigner.getLayoutDesigner().startMoving(compIds,
2463: originalBounds, hotSpot);
2464: } else { // dragging started in the old layout support
2465: LayoutComponent[] layoutComps = new LayoutComponent[compIds.length];
2466: for (int i = 0; i < compIds.length; i++) {
2467: layoutComps[i] = getLayoutModel()
2468: .getLayoutComponent(compIds[i]);
2469: if (layoutComps[i] == null) {
2470: layoutComps[i] = new LayoutComponent(
2471: compIds[i], false);
2472: }
2473: }
2474: formDesigner.getLayoutDesigner().startAdding(
2475: layoutComps, originalBounds, hotSpot, null);
2476: }
2477:
2478: if ((modifiers & InputEvent.ALT_MASK) != 0) {
2479: // restricted dragging - within the same container, or one level up
2480: fixedTarget = (modifiers & InputEvent.SHIFT_MASK) != 0
2481: || formDesigner.getTopDesignComponent() == metacont ? metacont
2482: : metacont.getParentContainer();
2483: }
2484:
2485: // old layout component dragger requires coordinates related to HandleLayer
2486: for (int i = 0; i < originalBounds.length; i++) {
2487: originalBounds[i].x += convertPoint.x;
2488: originalBounds[i].y += convertPoint.y;
2489: }
2490: oldDragger = new ComponentDragger(formDesigner,
2491: HandleLayer.this , movingComponents, originalBounds,
2492: new Point(hotSpot.x + convertPoint.x, hotSpot.y
2493: + convertPoint.y), fixedTarget);
2494:
2495: newDrag = oldDrag = true;
2496:
2497: super .init();
2498: }
2499:
2500: @Override
2501: boolean end(Point p, int modifiers) {
2502: // clear the rollover just in case it was set
2503: formDesigner.getMenuEditLayer().clearRollover();
2504:
2505: RADVisualContainer originalCont = getSourceContainer();
2506: // fail if trying to move a menu component to a non-menu container
2507: if (MenuEditLayer
2508: .containsMenuTypeComponent(movingComponents)) {
2509: if (!MenuEditLayer
2510: .isValidMenuContainer(targetContainer)) {
2511: formDesigner.getLayoutDesigner().endMoving(false);
2512: formDesigner.updateContainerLayout(originalCont);
2513: return true;
2514: }
2515: }
2516: // fail if trying to move a non-menu component into a menu container
2517: if (!MenuEditLayer
2518: .containsMenuTypeComponent(movingComponents)) {
2519: if (MenuEditLayer.isValidMenuContainer(targetContainer)) {
2520: formDesigner.getLayoutDesigner().endMoving(false);
2521: formDesigner.updateContainerLayout(originalCont);
2522: return true;
2523: }
2524: }
2525: if (p != null) {
2526: if (targetContainer == null
2527: || targetContainer.getLayoutSupport() != null) {
2528: // dropped in old layout support, or on non-visual area
2529: createLayoutUndoableEdit();
2530: boolean autoUndo = true;
2531: try {
2532: formDesigner.getLayoutDesigner()
2533: .removeDraggedComponents();
2534: oldDragger.dropComponents(p, targetContainer);
2535: autoUndo = false;
2536: } finally {
2537: placeLayoutUndoableEdit(autoUndo);
2538: }
2539: } else { // dropped in new layout support
2540: if (targetContainer != originalCont) {
2541: for (int i = 0; i < movingComponents.length; i++) {
2542: getFormModel().removeComponent(
2543: movingComponents[i], false);
2544: }
2545: // Issue 69410 (don't mix remove/add chnages)
2546: for (int i = 0; i < movingComponents.length; i++) {
2547: getFormModel().addVisualComponent(
2548: movingComponents[i],
2549: targetContainer, null, false);
2550: }
2551: }
2552: createLayoutUndoableEdit();
2553: boolean autoUndo = true;
2554: try {
2555: formDesigner.getLayoutDesigner()
2556: .endMoving(true);
2557: autoUndo = false;
2558: } finally {
2559: getFormModel().fireContainerLayoutChanged(
2560: targetContainer, null, null, null);
2561: placeLayoutUndoableEdit(autoUndo);
2562: }
2563: }
2564: } else { // canceled
2565: formDesigner.getLayoutDesigner().endMoving(false);
2566: formDesigner.updateContainerLayout(originalCont); //, false);
2567: }
2568:
2569: return true;
2570: }
2571:
2572: @Override
2573: void oldMove(Point p) {
2574: oldDragger.drag(p, targetContainer);
2575: }
2576:
2577: @Override
2578: void oldPaintFeedback(Graphics2D g, Graphics gg, int index) {
2579: oldDragger.paintDragFeedback(g);
2580:
2581: // don't paint if component dragged from old layout (may have strange size)
2582: Component comp = showingComponents[index];
2583: paintDraggedComponent(comp, gg);
2584: }
2585: }
2586:
2587: // for resizing existing components
2588: private class ResizeComponentDrag extends ComponentDrag {
2589: private int resizeType;
2590: private Dimension originalSize;
2591:
2592: private ComponentDragger oldDragger; // drags components in the old layout support
2593:
2594: ResizeComponentDrag(RADVisualComponent[] comps, Point hotspot, // in HandleLayer coordinates
2595: int resizeType) {
2596: super (comps, hotspot);
2597: this .resizeType = resizeType;
2598: init();
2599: }
2600:
2601: @Override
2602: void init() {
2603: RADVisualContainer sourceCont = getSourceContainer();
2604: if (isTopComponent()) {
2605: LayoutModel layoutModel = getLayoutModel();
2606: newDrag = layoutModel != null
2607: && layoutModel
2608: .getLayoutComponent(movingComponents[0]
2609: .getId()) != null;
2610: oldDrag = !newDrag;
2611: fixedTarget = null;
2612: originalSize = formDesigner.getComponentLayer()
2613: .getDesignerSize();
2614: } else if (sourceCont != null) {
2615: if (sourceCont.getLayoutSupport() == null) {
2616: newDrag = true;
2617: } else {
2618: oldDrag = true;
2619: }
2620: fixedTarget = sourceCont;
2621: }
2622:
2623: if (newDrag) { // new layout support
2624: String[] compIds = new String[showingComponents.length];
2625: for (int i = 0; i < showingComponents.length; i++) {
2626: compIds[i] = movingComponents[i].getId();
2627: originalBounds[i].x -= convertPoint.x;
2628: originalBounds[i].y -= convertPoint.y;
2629: }
2630:
2631: int[] res = new int[2];
2632: int horiz = resizeType
2633: & (LayoutSupportManager.RESIZE_LEFT | LayoutSupportManager.RESIZE_RIGHT);
2634: if (horiz == LayoutSupportManager.RESIZE_LEFT) {
2635: res[LayoutConstants.HORIZONTAL] = LayoutConstants.LEADING;
2636: } else if (horiz == LayoutSupportManager.RESIZE_RIGHT) {
2637: res[LayoutConstants.HORIZONTAL] = LayoutConstants.TRAILING;
2638: } else {
2639: res[LayoutConstants.HORIZONTAL] = LayoutConstants.DEFAULT;
2640: }
2641: int vert = resizeType
2642: & (LayoutSupportManager.RESIZE_UP | LayoutSupportManager.RESIZE_DOWN);
2643: if (vert == LayoutSupportManager.RESIZE_UP) {
2644: res[LayoutConstants.VERTICAL] = LayoutConstants.LEADING;
2645: } else if (vert == LayoutSupportManager.RESIZE_DOWN) {
2646: res[LayoutConstants.VERTICAL] = LayoutConstants.TRAILING;
2647: } else {
2648: res[LayoutConstants.VERTICAL] = LayoutConstants.DEFAULT;
2649: }
2650:
2651: formDesigner.getLayoutDesigner().startResizing(compIds,
2652: originalBounds, hotSpot, res,
2653: sourceCont != null);
2654:
2655: // convert back to HandleLayer
2656: for (int i = 0; i < originalBounds.length; i++) {
2657: originalBounds[i].x += convertPoint.x;
2658: originalBounds[i].y += convertPoint.y;
2659: }
2660: } else if (oldDrag) { // old layout support
2661: oldDragger = new ComponentDragger(formDesigner,
2662: HandleLayer.this , movingComponents,
2663: originalBounds, new Point(hotSpot.x
2664: + convertPoint.x, hotSpot.y
2665: + convertPoint.y), resizeType);
2666: }
2667:
2668: super .init();
2669: }
2670:
2671: @Override
2672: boolean end(Point p, int modifiers) {
2673: if (p != null) {
2674: if (newDrag) { // new layout support
2675: // make sure the visual component has the current size set
2676: // (as still being in its container the layout manager tries to
2677: // restore the original size)
2678: showingComponents[0].setSize(movingBounds[0].width,
2679: movingBounds[0].height);
2680: doLayout(showingComponents[0]);
2681:
2682: createLayoutUndoableEdit();
2683: boolean autoUndo = true;
2684: try {
2685: formDesigner.getLayoutDesigner()
2686: .endMoving(true);
2687: for (int i = 0; i < movingComponents.length; i++) {
2688: RADVisualComponent metacomp = movingComponents[i];
2689: if (metacomp instanceof RADVisualContainer) {
2690: RADVisualContainer visCont = (RADVisualContainer) metacomp;
2691: if (visCont.getLayoutSupport() == null) {
2692: getFormModel()
2693: .fireContainerLayoutChanged(
2694: visCont, null,
2695: null, null);
2696: }
2697: }
2698: }
2699: autoUndo = false;
2700: } finally {
2701: if (targetContainer != null) {
2702: getFormModel().fireContainerLayoutChanged(
2703: targetContainer, null, null, null);
2704: }
2705: placeLayoutUndoableEdit(autoUndo);
2706: }
2707: } else { // old layout support
2708: if (targetContainer != null) {
2709: oldDragger.dropComponents(p, targetContainer);
2710: }
2711: }
2712: if (isTopComponent()) {
2713: formDesigner.setDesignerSize(new Dimension(
2714: movingBounds[0].width,
2715: movingBounds[0].height), originalSize);
2716: }
2717: } else { // resizing canceled
2718: formDesigner.getLayoutDesigner().endMoving(false);
2719:
2720: if (isTopComponent()) {
2721: // just revert ComponentLayer's designer size (don't need to go through FormDesigner)
2722: ComponentLayer compLayer = formDesigner
2723: .getComponentLayer();
2724: if (!compLayer.getDesignerSize().equals(
2725: originalSize)) {
2726: compLayer.setDesignerSize(originalSize);
2727: compLayer.revalidate();
2728: }
2729: compLayer.repaint();
2730: } else { // add resized component back
2731: formDesigner
2732: .updateContainerLayout(getSourceContainer()); //, false);
2733: }
2734: }
2735: return true;
2736: }
2737:
2738: @Override
2739: void move(Point p, int modifiers) {
2740: if (isTopComponent()) {
2741: if (newDrag) {
2742: p.x -= convertPoint.x;
2743: p.y -= convertPoint.y;
2744: formDesigner.getLayoutDesigner().move(p, null,
2745: ((modifiers & InputEvent.ALT_MASK) == 0),
2746: ((modifiers & InputEvent.CTRL_MASK) != 0),
2747: movingBounds);
2748: showingComponents[0].setSize(movingBounds[0].width,
2749: movingBounds[0].height);
2750: } else {
2751: Rectangle r = formDesigner.getComponentLayer()
2752: .getDesignerInnerBounds();
2753: int w = r.width;
2754: int h = r.height;
2755: if ((resizeType & LayoutSupportManager.RESIZE_DOWN) != 0) {
2756: h = p.y - r.y;
2757: if (h < 0)
2758: h = 0;
2759: }
2760: if ((resizeType & LayoutSupportManager.RESIZE_RIGHT) != 0) {
2761: w = p.x - r.x;
2762: if (w < 0)
2763: w = 0;
2764: }
2765: movingBounds[0].width = w;
2766: movingBounds[0].height = h;
2767: }
2768: Dimension size = new Dimension(movingBounds[0].width,
2769: movingBounds[0].height);
2770: formDesigner.getComponentLayer().setDesignerSize(size);
2771: doLayout(formDesigner.getComponentLayer());
2772: } else if (oldDrag
2773: && (targetContainer = getTargetContainer(p,
2774: modifiers)) != null
2775: && targetContainer.getLayoutSupport() != null) {
2776: oldMove(p);
2777: for (int i = 0; i < movingBounds.length; i++) {
2778: int xchange = p.x - convertPoint.x - hotSpot.x;
2779: if ((resizeType & LayoutSupportManager.RESIZE_LEFT) != 0) {
2780: movingBounds[i].x = originalBounds[i].x
2781: - convertPoint.x + xchange;
2782: xchange = -xchange;
2783: } else {
2784: movingBounds[i].x = originalBounds[i].x
2785: - convertPoint.x;
2786: }
2787: if ((resizeType & (LayoutSupportManager.RESIZE_RIGHT | LayoutSupportManager.RESIZE_LEFT)) != 0) {
2788: movingBounds[i].width = originalBounds[i].width
2789: + xchange;
2790: }
2791: int ychange = p.y - convertPoint.y - hotSpot.y;
2792: if ((resizeType & LayoutSupportManager.RESIZE_UP) != 0) {
2793: movingBounds[i].y = originalBounds[i].y
2794: - convertPoint.y + ychange;
2795: ychange = -ychange;
2796: } else {
2797: movingBounds[i].y = originalBounds[i].y
2798: - convertPoint.y;
2799: }
2800: if ((resizeType & (LayoutSupportManager.RESIZE_DOWN | LayoutSupportManager.RESIZE_UP)) != 0) {
2801: movingBounds[i].height = originalBounds[i].height
2802: + ychange;
2803: }
2804: }
2805: } else {
2806: super .move(p, modifiers);
2807: }
2808: }
2809:
2810: @Override
2811: void oldMove(Point p) {
2812: oldDragger.drag(p, targetContainer);
2813: FormEditor.getAssistantModel(getFormModel()).setContext(
2814: "generalResizing"); // NOI18N
2815: }
2816:
2817: @Override
2818: void oldPaintFeedback(Graphics2D g, Graphics gg, int index) {
2819: paintDraggedComponent(showingComponents[index], gg);
2820: oldDragger.paintDragFeedback(g);
2821: }
2822: }
2823:
2824: // for moving a component being newly added
2825: private class NewComponentDrag extends ComponentDrag {
2826: private PaletteItem paletteItem;
2827: RADComponent addedComponent;
2828:
2829: private int index = -1; // for the old layout support
2830: private LayoutConstraints constraints; // for the old layout support
2831:
2832: NewComponentDrag(PaletteItem paletteItem) {
2833: super ();
2834: this .paletteItem = paletteItem;
2835: showingComponents = new Component[1];
2836: init();
2837: }
2838:
2839: @Override
2840: void init() { // can be re-inited
2841: RADVisualComponent precreated = getComponentCreator()
2842: .precreateVisualComponent(
2843: paletteItem.getComponentClassSource());
2844:
2845: if (precreated != null) {
2846: if (movingComponents == null) {
2847: setMovingComponents(new RADVisualComponent[] { precreated });
2848: } else { // continuing adding - new instance of the same component
2849: movingComponents[0] = precreated;
2850: }
2851:
2852: LayoutComponent precreatedLC = isDraggableLayoutComponent() ? getComponentCreator()
2853: .getPrecreatedLayoutComponent()
2854: : null;
2855: // (precreating LayoutComponent also adjusts the initial size of
2856: // the visual component which is used below)
2857:
2858: showingComponents[0] = (Component) precreated
2859: .getBeanInstance();
2860: // Force creation of peer - AWT components don't have preferred size otherwise
2861: if (!(showingComponents[0] instanceof JComponent)) {
2862: FakePeerSupport
2863: .attachFakePeer(showingComponents[0]);
2864: if (showingComponents[0] instanceof Container) {
2865: FakePeerSupport
2866: .attachFakePeerRecursively((Container) showingComponents[0]);
2867: }
2868: }
2869:
2870: Dimension size = showingComponents[0]
2871: .getPreferredSize();
2872: if (originalBounds == null) { // new adding
2873: hotSpot = new Point();
2874: originalBounds = new Rectangle[] { new Rectangle(
2875: convertPoint.x, convertPoint.y, size.width,
2876: size.height) };
2877: movingBounds = new Rectangle[] { new Rectangle(0,
2878: 0, size.width, size.height) };
2879: } else { // repeated adding of the same component type, reuse last bounds
2880: movingBounds[0].width = size.width;
2881: movingBounds[0].height = size.height;
2882: originalBounds[0] = movingBounds[0];
2883: movingBounds[0] = new Rectangle(movingBounds[0]);
2884: originalBounds[0].x += convertPoint.x;
2885: originalBounds[0].y += convertPoint.y;
2886: }
2887: compoundBounds = originalBounds[0];
2888: hotSpot.x = movingBounds[0].x + size.width / 2 - 4;
2889: hotSpot.y = movingBounds[0].y + size.height / 2;
2890: if (hotSpot.x < movingBounds[0].x)
2891: hotSpot.x = movingBounds[0].x;
2892:
2893: if (precreatedLC != null) {
2894: LayoutComponent[] layoutComponents = new LayoutComponent[] { precreatedLC };
2895: if (formDesigner.getLayoutDesigner() != null) {
2896: formDesigner
2897: .getLayoutDesigner()
2898: .startAdding(
2899: layoutComponents,
2900: movingBounds,
2901: hotSpot,
2902: targetContainer != null
2903: && targetContainer
2904: .getLayoutSupport() == null ? targetContainer
2905: .getId()
2906: : null);
2907: }
2908: }
2909:
2910: newDrag = oldDrag = true;
2911: } else {
2912: if (paletteItem.getComponentClass() != null) {
2913: // non-visual component - present it as icon
2914: Node node = paletteItem.getNode();
2915: Image icon;
2916: if (node == null) {
2917: icon = paletteItem
2918: .getIcon(java.beans.BeanInfo.ICON_COLOR_16x16);
2919: if (icon == null) {
2920: icon = Utilities
2921: .loadImage("org/netbeans/modules/form/resources/form.gif"); // NOI18N
2922: }
2923: } else {
2924: icon = node
2925: .getIcon(java.beans.BeanInfo.ICON_COLOR_16x16);
2926: }
2927: showingComponents[0] = new JLabel(new ImageIcon(
2928: icon));
2929: Dimension dim = showingComponents[0]
2930: .getPreferredSize();
2931: hotSpot = new Point(dim.width / 2, dim.height / 2);
2932: if (hotSpot.x < 0) {
2933: hotSpot.x = 0;
2934: }
2935: originalBounds = new Rectangle[] { new Rectangle(
2936: convertPoint.x, convertPoint.y, dim.width,
2937: dim.height) };
2938: showingComponents[0].setBounds(originalBounds[0]);
2939: movingBounds = new Rectangle[] { showingComponents[0]
2940: .getBounds() };
2941:
2942: newDrag = oldDrag = false;
2943: } else {
2944: // The corresponding class cannot be loaded - cancel the drag.
2945: showingComponents = null;
2946: movingBounds = new Rectangle[0];
2947: EventQueue.invokeLater(new Runnable() {
2948: public void run() {
2949: formDesigner.toggleSelectionMode(); // calls endDragging(null)
2950: }
2951: });
2952: }
2953: }
2954:
2955: super .init();
2956: }
2957:
2958: /** Overrides end(Point,int) in ComponentDrag to support adding new components
2959: */
2960: @Override
2961: boolean end(Point p, int modifiers) {
2962: // clear the rollover just in case it was set
2963: formDesigner.getMenuEditLayer().clearRollover();
2964:
2965: if (p != null) {
2966: targetContainer = getTargetContainer(p, modifiers);
2967:
2968: if (movingComponents != null) { // there is a precreated visual component
2969: boolean newLayout;
2970: boolean oldLayout;
2971: Object constraints; // for old layout
2972: if (targetContainer != null) {
2973: newLayout = targetContainer.getLayoutSupport() == null;
2974: oldLayout = !newLayout;
2975: Point posInComp = new Point(hotSpot.x
2976: - originalBounds[0].x + convertPoint.x,
2977: hotSpot.y - originalBounds[0].y
2978: + convertPoint.y);
2979: constraints = oldLayout
2980: && isDraggableLayoutComponent() ? getConstraintsAtPoint(
2981: targetContainer, p, posInComp)
2982: : null;
2983: } else {
2984: newLayout = oldLayout = false;
2985: constraints = null;
2986: }
2987: addedComponent = movingComponents[0];
2988: LayoutComponent layoutComponent = isDraggableLayoutComponent() ? getComponentCreator()
2989: .getPrecreatedLayoutComponent()
2990: : null;
2991: // add the component to FormModel
2992: boolean added = getComponentCreator()
2993: .addPrecreatedComponent(targetContainer,
2994: constraints);
2995: // add the cmponent to LayoutModel
2996: if (layoutComponent != null
2997: && getLayoutModel() != null) { // Some beans don't have layout
2998: createLayoutUndoableEdit();
2999: boolean autoUndo = true;
3000: try {
3001: formDesigner.getLayoutDesigner().endMoving(
3002: added && newLayout);
3003: if (added) {
3004: if (layoutComponent.isLayoutContainer()) {
3005: if (!newLayout) { // always add layout container to the model
3006: getLayoutModel()
3007: .addRootComponent(
3008: layoutComponent);
3009: }
3010: }
3011: } else {
3012: repaint();
3013: }
3014: autoUndo = false;
3015: } finally {
3016: placeLayoutUndoableEdit(autoUndo);
3017: }
3018: }
3019: } else { // component not precreated ...
3020: RADComponent targetComponent = targetContainer;
3021: if (javax.swing.border.Border.class
3022: .isAssignableFrom(paletteItem
3023: .getComponentClass())) {
3024: int mode = ((modifiers & InputEvent.ALT_MASK) != 0) ? COMP_SELECTED
3025: : COMP_DEEPEST;
3026: targetComponent = HandleLayer.this
3027: .getMetaComponentAt(p, mode);
3028: }
3029: addedComponent = getComponentCreator()
3030: .createComponent(
3031: paletteItem
3032: .getComponentClassSource(),
3033: targetComponent, null, false);
3034: if (addedComponent == null) {
3035: repaint();
3036: }
3037: }
3038:
3039: if (addedComponent != null) {
3040: java.beans.BeanDescriptor bDesc = addedComponent
3041: .getBeanInfo().getBeanDescriptor();
3042: if ((bDesc != null)
3043: && (bDesc.getValue("customizeOnCreation") != null)) { // NOI18N
3044: modifiers &= ~InputEvent.SHIFT_MASK;
3045: EventQueue.invokeLater(new Runnable() {
3046: public void run() {
3047: RADComponentNode node = addedComponent
3048: .getNodeReference();
3049: if (node.hasCustomizer()) {
3050: org.openide.nodes.NodeOperation
3051: .getDefault().customize(
3052: node);
3053: }
3054: }
3055: });
3056: }
3057: }
3058: if ((modifiers & InputEvent.SHIFT_MASK) != 0) {
3059: // init();
3060: return false;
3061: }
3062: } else {
3063: if (formDesigner.getLayoutDesigner() != null) {
3064: formDesigner.getLayoutDesigner().endMoving(false);
3065: }
3066: getComponentCreator().releasePrecreatedComponent();
3067: }
3068: formDesigner.toggleSelectionMode();
3069: return true;
3070: }
3071:
3072: @Override
3073: void oldMove(Point p) {
3074: LayoutSupportManager laysup = targetContainer
3075: .getLayoutSupport();
3076: Container cont = (Container) formDesigner
3077: .getComponent(targetContainer);
3078: Container contDel = targetContainer
3079: .getContainerDelegate(cont);
3080: Point posInCont = convertPointToComponent(p.x, p.y, contDel);
3081: Point posInComp = new Point(hotSpot.x - originalBounds[0].x
3082: + convertPoint.x, hotSpot.y - originalBounds[0].y
3083: + convertPoint.y);
3084: index = laysup.getNewIndex(cont, contDel,
3085: showingComponents[0], -1, posInCont, posInComp);
3086: constraints = laysup.getNewConstraints(cont, contDel,
3087: showingComponents[0], -1, posInCont, posInComp);
3088: }
3089:
3090: @Override
3091: void oldPaintFeedback(Graphics2D g, Graphics gg, int index) {
3092: LayoutSupportManager laysup = targetContainer
3093: .getLayoutSupport();
3094: Container cont = (Container) formDesigner
3095: .getComponent(targetContainer);
3096: Container contDel = targetContainer
3097: .getContainerDelegate(cont);
3098: Point contPos = convertPointFromComponent(0, 0, contDel);
3099: g.setColor(formSettings.getSelectionBorderColor());
3100: g.setStroke(ComponentDragger.dashedStroke1);
3101: g.translate(contPos.x, contPos.y);
3102: laysup.paintDragFeedback(cont, contDel,
3103: showingComponents[0], constraints, this .index, g);
3104: g.translate(-contPos.x, -contPos.y);
3105: // g.setStroke(stroke);
3106: paintDraggedComponent(showingComponents[0], gg);
3107: }
3108: }
3109:
3110: private class NewComponentDropListener implements
3111: DropTargetListener {
3112: private NewComponentDrop newComponentDrop;
3113: private int dropAction;
3114: /** Assistant context requested by newComponentDrop. */
3115: private String dropContext;
3116: /** Additional assistant context requested by newComponentDrop. */
3117: private String additionalDropContext;
3118:
3119: public void dragEnter(DropTargetDragEvent dtde) {
3120: try {
3121: dropAction = dtde.getDropAction();
3122: newComponentDrop = null;
3123: Transferable transferable = dtde.getTransferable();
3124: PaletteItem item = null;
3125: if (dtde
3126: .isDataFlavorSupported(PaletteController.ITEM_DATA_FLAVOR)) {
3127: Lookup itemLookup = (Lookup) transferable
3128: .getTransferData(PaletteController.ITEM_DATA_FLAVOR);
3129: item = itemLookup.lookup(PaletteItem.class);
3130: } else {
3131: ClassSource classSource = CopySupport
3132: .getCopiedBeanClassSource(transferable);
3133: if (classSource != null) {
3134: Class componentClass = getComponentCreator()
3135: .prepareClass(classSource);
3136: if (componentClass != null) {
3137: item = new PaletteItem(classSource,
3138: componentClass);
3139: }
3140: } else {
3141: Lookup.Template<NewComponentDropProvider> template = new Lookup.Template<NewComponentDropProvider>(
3142: NewComponentDropProvider.class);
3143: Collection<? extends NewComponentDropProvider> providers = Lookup
3144: .getDefault().lookup(template)
3145: .allInstances();
3146: for (NewComponentDropProvider provider : providers) {
3147: newComponentDrop = provider
3148: .processTransferable(
3149: getFormModel(),
3150: transferable);
3151: if (newComponentDrop != null) {
3152: dropContext = null;
3153: AssistantModel aModel = FormEditor
3154: .getAssistantModel(getFormModel());
3155: String preContext = aModel.getContext();
3156: item = newComponentDrop
3157: .getPaletteItem(dtde);
3158: String postContext = aModel
3159: .getContext();
3160: if (!preContext.equals(postContext)) {
3161: dropContext = postContext;
3162: additionalDropContext = aModel
3163: .getAdditionalContext();
3164: }
3165: break;
3166: }
3167: }
3168: }
3169: }
3170: //switch to the menu layer if this is a menu component other than JMenuBar
3171: if (item != null
3172: && MenuEditLayer
3173: .isMenuRelatedComponentClass(item
3174: .getComponentClass())
3175: && !JMenuBar.class.isAssignableFrom(item
3176: .getComponentClass())) {
3177: if (!formDesigner.getMenuEditLayer()
3178: .isDragProxying()) {
3179: formDesigner.getMenuEditLayer()
3180: .startNewMenuComponentDragAndDrop(item);
3181: return;
3182: }
3183: }
3184: if (item != null) {
3185: if ((item.getComponentClassName().indexOf('.') != -1) // Issue 79573
3186: || FormJavaSource
3187: .isInDefaultPackage(getFormModel())) {
3188: draggedComponent = new NewComponentDrag(item);
3189: draggedComponent.move(dtde.getLocation(), 0);
3190: repaint();
3191: } else {
3192: dtde.rejectDrag();
3193: }
3194: } else {
3195: dtde.rejectDrag();
3196: }
3197: } catch (Exception ex) {
3198: ex.printStackTrace();
3199: }
3200: }
3201:
3202: public void dragOver(java.awt.dnd.DropTargetDragEvent dtde) {
3203: if (draggedComponent != null) {
3204: if ((newComponentDrop != null)
3205: && (dropAction != dtde.getDropAction())) {
3206: dragExit(dtde);
3207: dragEnter(dtde);
3208: return;
3209: }
3210: draggedComponent.move(dtde.getLocation(), 0);
3211: if (dropContext != null) {
3212: FormEditor.getAssistantModel(getFormModel())
3213: .setContext(dropContext,
3214: additionalDropContext);
3215: }
3216: repaint();
3217: }
3218: }
3219:
3220: public void dropActionChanged(
3221: java.awt.dnd.DropTargetDragEvent dtde) {
3222: }
3223:
3224: public void dragExit(java.awt.dnd.DropTargetEvent dte) {
3225: if (draggedComponent != null) {
3226: endDragging(null);
3227: repaint();
3228: }
3229: }
3230:
3231: public void drop(java.awt.dnd.DropTargetDropEvent dtde) {
3232: if (draggedComponent != null) {
3233: NewComponentDrag newComponentDrag = ((NewComponentDrag) draggedComponent);
3234: try {
3235: newComponentDrag.end(dtde.getLocation(), 0);
3236: } finally {
3237: draggedComponent = null;
3238: draggingEnded = true;
3239: }
3240: if (newComponentDrag.addedComponent != null) {
3241: String id = newComponentDrag.addedComponent.getId();
3242: if (newComponentDrop != null) {
3243: String droppedOverId = null;
3244: if (!(newComponentDrag.addedComponent instanceof RADVisualComponent)) {
3245: RADComponent comp = getMetaComponentAt(dtde
3246: .getLocation(), COMP_DEEPEST);
3247: if (comp != null)
3248: droppedOverId = comp.getId();
3249: }
3250: newComponentDrop.componentAdded(id,
3251: droppedOverId);
3252: }
3253: }
3254: formDesigner.toggleSelectionMode();
3255: formDesigner.requestActive();
3256: }
3257: }
3258:
3259: }
3260:
3261: }
|