~ubuntu-branches/ubuntu/quantal/netbeans/quantal

« back to all changes in this revision

Viewing changes to form/src/org/netbeans/modules/form/menu/DragOperation.java

  • Committer: Bazaar Package Importer
  • Author(s): Marek Slama
  • Date: 2008-01-29 14:11:22 UTC
  • Revision ID: james.westby@ubuntu.com-20080129141122-fnzjbo11ntghxfu7
Tags: upstream-6.0.1
ImportĀ upstreamĀ versionĀ 6.0.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 
3
 *
 
4
 * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
 
5
 *
 
6
 * The contents of this file are subject to the terms of either the GNU
 
7
 * General Public License Version 2 only ("GPL") or the Common
 
8
 * Development and Distribution License("CDDL") (collectively, the
 
9
 * "License"). You may not use this file except in compliance with the
 
10
 * License. You can obtain a copy of the License at
 
11
 * http://www.netbeans.org/cddl-gplv2.html
 
12
 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
 
13
 * specific language governing permissions and limitations under the
 
14
 * License.  When distributing the software, include this License Header
 
15
 * Notice in each file and include the License file at
 
16
 * nbbuild/licenses/CDDL-GPL-2-CP.  Sun designates this
 
17
 * particular file as subject to the "Classpath" exception as provided
 
18
 * by Sun in the GPL Version 2 section of the License file that
 
19
 * accompanied this code. If applicable, add the following below the
 
20
 * License Header, with the fields enclosed by brackets [] replaced by
 
21
 * your own identifying information:
 
22
 * "Portions Copyrighted [year] [name of copyright owner]"
 
23
 *
 
24
 * Contributor(s):
 
25
 *
 
26
 * The Original Software is NetBeans. The Initial Developer of the Original
 
27
 * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
 
28
 * Microsystems, Inc. All Rights Reserved.
 
29
 *
 
30
 * If you wish your version of this file to be governed by only the CDDL
 
31
 * or only the GPL Version 2, indicate your decision by adding
 
32
 * "[Contributor] elects to include this software in this distribution
 
33
 * under the [CDDL or GPL Version 2] license." If you do not indicate a
 
34
 * single choice of license, a recipient has the option to distribute
 
35
 * your version of this file under either the CDDL, the GPL Version 2 or
 
36
 * to extend the choice of license to its licensees as provided above.
 
37
 * However, if you add GPL Version 2 code and therefore, elected the GPL
 
38
 * Version 2 license, then the option applies only if the new code is
 
39
 * made subject to such option by the copyright holder.
 
40
 */
 
41
package org.netbeans.modules.form.menu;
 
42
 
 
43
import java.awt.Color;
 
44
import java.awt.Component;
 
45
import java.awt.Cursor;
 
46
import java.awt.Dimension;
 
47
import java.awt.Insets;
 
48
import java.awt.Point;
 
49
import java.util.ArrayList;
 
50
import java.util.List;
 
51
import javax.swing.BorderFactory;
 
52
import javax.swing.JCheckBoxMenuItem;
 
53
import javax.swing.JComponent;
 
54
import javax.swing.JLayeredPane;
 
55
import javax.swing.JMenu;
 
56
import javax.swing.JMenuBar;
 
57
import javax.swing.JMenuItem;
 
58
import javax.swing.JRadioButtonMenuItem;
 
59
import javax.swing.JSeparator;
 
60
import javax.swing.SwingUtilities;
 
61
import org.netbeans.modules.form.FormEditor;
 
62
import org.netbeans.modules.form.FormModel;
 
63
import org.netbeans.modules.form.HandleLayer;
 
64
import org.netbeans.modules.form.MetaComponentCreator;
 
65
import org.netbeans.modules.form.RADComponent;
 
66
import org.netbeans.modules.form.RADVisualComponent;
 
67
import org.netbeans.modules.form.RADVisualContainer;
 
68
import org.netbeans.modules.form.layoutdesign.LayoutComponent;
 
69
import org.netbeans.modules.form.palette.PaletteItem;
 
70
import org.netbeans.modules.form.palette.PaletteUtils;
 
71
 
 
72
/**
 
73
 * DragOperation handles all drag operations whether they are drag and drop or pick and plop. It
 
74
 * also deals with new components from the palette and rearranging existing menu items within the menu.
 
75
 * It does *not* handle the actual adding and removing of components. Instead that is delegated back
 
76
 * to the MenuEditLayer.
 
77
 * @author joshua.marinacci@sun.com
 
78
 */
 
79
class DragOperation {
 
80
    private static final boolean DEBUG = false;
 
81
    private MenuEditLayer menuEditLayer;
 
82
    private JComponent dragComponent;
 
83
    private boolean started = false;
 
84
    private JComponent targetComponent;
 
85
    private enum Op { PICK_AND_PLOP_FROM_PALETTE, INTER_MENU_DRAG, NO_MENUBAR };
 
86
    private Op op = Op.PICK_AND_PLOP_FROM_PALETTE;
 
87
    private JMenuItem payloadComponent;
 
88
    private List<JMenuItem> payloadComponents;
 
89
    private PaletteItem currentItem;
 
90
    
 
91
    public boolean isPickAndPlop() {
 
92
        return op == Op.PICK_AND_PLOP_FROM_PALETTE;
 
93
    }
 
94
    
 
95
    public DragOperation(MenuEditLayer menuEditLayer) {
 
96
        this.menuEditLayer = menuEditLayer;
 
97
        this.started = false;
 
98
    }
 
99
    
 
100
    public JComponent getDragComponent() {
 
101
        return dragComponent;
 
102
    }
 
103
    
 
104
    // start a drag from one menu item to another
 
105
    void start(JMenuItem item, Point pt) {
 
106
        op = Op.INTER_MENU_DRAG;
 
107
        p("starting an inner menu drag for: " + item + " at " + pt);
 
108
        started = true;
 
109
        
 
110
        
 
111
        //josh: intial support for dragging multiple items
 
112
        //in the future we should use the payloadComponents variable
 
113
        //for dragging components instead of the payloadComponent variable.
 
114
        List<RADComponent> rads = menuEditLayer.getSelectedRADComponents();
 
115
        
 
116
        payloadComponents = new ArrayList<JMenuItem>();
 
117
        if(rads.size() > 1) {
 
118
            for(RADComponent rad : rads) {
 
119
                Object comp = menuEditLayer.formDesigner.getComponent(rad);
 
120
                if(comp instanceof JMenuItem) {
 
121
                    payloadComponents.add((JMenuItem)comp);
 
122
                } else {
 
123
                    fastEnd();
 
124
                    return;
 
125
                }
 
126
            }
 
127
        } else {
 
128
            payloadComponents.add(item);
 
129
        }
 
130
        
 
131
        dragComponent = (JMenuItem) createDragFeedbackComponent(item, null);
 
132
        dragComponent.setSize(dragComponent.getPreferredSize());
 
133
        dragComponent.setLocation(pt);
 
134
        menuEditLayer.layers.add(dragComponent, JLayeredPane.DRAG_LAYER);
 
135
        menuEditLayer.repaint();
 
136
        payloadComponent = item;
 
137
    }
 
138
 
 
139
    void setTargetVisible(boolean vis) {
 
140
        if(!vis) {
 
141
            menuEditLayer.layers.remove(dragComponent);
 
142
        } else {
 
143
            menuEditLayer.layers.add(dragComponent, JLayeredPane.DRAG_LAYER);
 
144
        }
 
145
        menuEditLayer.repaint();
 
146
    }
 
147
    
 
148
    private JComponent createDragFeedbackComponent(JMenuItem item, Class type) {
 
149
        // get the pre-created component for use as drag feedback
 
150
        PaletteItem paletteItem = PaletteUtils.getSelectedItem();
 
151
        if(paletteItem != null) {
 
152
            MetaComponentCreator creator = menuEditLayer.formDesigner.getFormModel().getComponentCreator();
 
153
            RADVisualComponent precreated = creator.precreateVisualComponent(
 
154
                    paletteItem.getComponentClassSource());
 
155
            if(precreated != null) {
 
156
                p("precreated: " + precreated.getBeanClass());
 
157
                Object comp = precreated.getBeanInstance();
 
158
                if(comp instanceof JComponent) {
 
159
                    JComponent jcomp = (JComponent) comp;
 
160
                    p("it's a jcomponent");
 
161
                    if(comp instanceof JMenuItem) {
 
162
                        JMenuItem mcomp = (JMenuItem) comp;
 
163
                        mcomp.setBorder(MenuEditLayer.DRAG_MENU_BORDER);
 
164
                        mcomp.setIcon(new MenuEditLayer.WrapperIcon());
 
165
                        mcomp.setMargin(new Insets(1,1,1,1));
 
166
                        mcomp.setBorderPainted(true);
 
167
                    }
 
168
                    if(comp instanceof JSeparator) {
 
169
                        p("it's a separator");
 
170
                        //jcomp.setBorder(BorderFactory.createLineBorder(new Color(0xFFA400), 1));//, thickness)MenuEditLayer.DRAG_SEPARATOR_BORDER);
 
171
                        //jcomp.setPreferredSize(new Dimension(80,5));
 
172
                        p("border = " + jcomp.getBorder());
 
173
                    }
 
174
                    return jcomp;
 
175
                }
 
176
            }
 
177
        }
 
178
        
 
179
        JComponent dragComponent = null;
 
180
        dragComponent = new JMenuItem();
 
181
        
 
182
        if(item == null && type != null && JComponent.class.isAssignableFrom(type)) {
 
183
            try {
 
184
                dragComponent = (JComponent)type.newInstance();
 
185
                p("created a drag component here: " + dragComponent);
 
186
            } catch (Exception ex) {
 
187
                System.out.println("exception: " + ex.getMessage());
 
188
                ex.printStackTrace();
 
189
            }
 
190
        }
 
191
        if(item instanceof JMenu) { 
 
192
            dragComponent = new JMenu(); 
 
193
        }
 
194
        if(item instanceof JCheckBoxMenuItem) { 
 
195
            dragComponent = new JCheckBoxMenuItem(); 
 
196
            ((JCheckBoxMenuItem)dragComponent).setSelected(true);
 
197
        }
 
198
        if(item instanceof JRadioButtonMenuItem) { 
 
199
            dragComponent = new JRadioButtonMenuItem(); 
 
200
            ((JRadioButtonMenuItem)dragComponent).setSelected(true);
 
201
        }
 
202
        if(dragComponent instanceof JMenuItem) {
 
203
            JMenuItem dragItem = (JMenuItem) dragComponent;
 
204
            if(item != null) {
 
205
                dragItem.setText(item.getText());
 
206
                dragItem.setIcon(item.getIcon());
 
207
                if(! (item instanceof JMenu)) {
 
208
                    if(!DropTargetLayer.isMetal()) {
 
209
                        dragItem.setAccelerator(item.getAccelerator());
 
210
                    }
 
211
                }
 
212
            } else {
 
213
                dragItem.setText("a new menu item");
 
214
            }
 
215
            dragItem.setMargin(new Insets(1,1,1,1));
 
216
            dragItem.setBorderPainted(true);
 
217
        }
 
218
        dragComponent.setBorder(MenuEditLayer.DRAG_MENU_BORDER);
 
219
        return dragComponent;
 
220
 
 
221
    }
 
222
    
 
223
    // start a pick and plop from the palette operation
 
224
    void start(PaletteItem item, Point pt) {
 
225
        // clean up prev is necessary
 
226
        if(dragComponent != null) {
 
227
            menuEditLayer.layers.remove(dragComponent);
 
228
            dragComponent = null;
 
229
        }
 
230
        
 
231
        if(!menuEditLayer.doesFormContainMenuBar()) {
 
232
            //op = Op.NO_MENUBAR;
 
233
            menuEditLayer.showMenubarWarning = true;
 
234
            FormEditor.getAssistantModel(menuEditLayer.formDesigner.getFormModel()).setContext("missingMenubar"); // NOI18N
 
235
            menuEditLayer.repaint();
 
236
        }
 
237
        
 
238
        op = Op.PICK_AND_PLOP_FROM_PALETTE;
 
239
        p("starting drag op for : " + item.getComponentClassName() + " at " + pt);
 
240
        started = true;
 
241
        dragComponent = createDragFeedbackComponent(null, item.getComponentClass());
 
242
        dragComponent.setSize(dragComponent.getPreferredSize());
 
243
        p("created drag component = " + dragComponent);
 
244
        dragComponent.setLocation(pt);
 
245
        menuEditLayer.layers.add(dragComponent, JLayeredPane.DRAG_LAYER);
 
246
        menuEditLayer.repaint();
 
247
        currentItem = item;
 
248
        menuEditLayer.glassLayer.requestFocusInWindow();
 
249
    }
 
250
    
 
251
    void move(Point pt) {
 
252
        if(dragComponent != null) {
 
253
            // move the drag component
 
254
            dragComponent.setLocation(pt);
 
255
            
 
256
            
 
257
            // look at the rad component under the cursor before checking the popups
 
258
            RADComponent rad = menuEditLayer.formDesigner.getHandleLayer().getMetaComponentAt(pt, HandleLayer.COMP_DEEPEST);
 
259
            
 
260
            // if dragging a JMenu over an open spot in the menu bar
 
261
            if(rad != null && JMenuBar.class.isAssignableFrom(rad.getBeanClass()) && JMenu.class.isAssignableFrom(dragComponent.getClass())) {
 
262
                //p("over the menu bar");
 
263
                menuEditLayer.dropTargetLayer.setDropTarget(rad, pt);
 
264
                targetComponent = (JComponent) menuEditLayer.formDesigner.getComponent(rad);
 
265
            }
 
266
            
 
267
            // open any relevant top-level menus
 
268
            if(rad != null && JMenu.class.isAssignableFrom(rad.getBeanClass())) {
 
269
                //p("over a menu: " + rad);
 
270
                targetComponent = (JComponent) menuEditLayer.formDesigner.getComponent(rad);
 
271
                menuEditLayer.openMenu(rad, targetComponent);
 
272
                if(JMenu.class.isAssignableFrom(dragComponent.getClass())) {
 
273
                    menuEditLayer.dropTargetLayer.setDropTarget(rad, pt);
 
274
                } else {
 
275
                    menuEditLayer.dropTargetLayer.setDropTarget(rad, pt, DropTargetLayer.DropTargetType.INTO_SUBMENU);
 
276
                }
 
277
                return;
 
278
            }
 
279
            
 
280
            //show any drop target markers
 
281
            Component child = getDeepestComponentInPopups(pt);
 
282
            
 
283
            
 
284
            if(child instanceof JMenuItem && child != dragComponent) {
 
285
                targetComponent = (JComponent)child;
 
286
                if(targetComponent != null) {
 
287
                    menuEditLayer.dropTargetLayer.setDropTarget(targetComponent, pt, DropTargetLayer.DropTargetType.INTER_MENU);
 
288
                }
 
289
                menuEditLayer.repaint();
 
290
            }
 
291
            
 
292
            if(child instanceof JMenu) {
 
293
                Point pt2 = SwingUtilities.convertPoint(menuEditLayer.glassLayer, pt, child);
 
294
                JMenu menu = (JMenu) child;
 
295
                Point point = pt2;
 
296
                menu.setBorderPainted(true);
 
297
                if(DropTargetLayer.isSubMenuRightEdge(point, menu)) {
 
298
                    menuEditLayer.dropTargetLayer.setDropTarget(menu, point, DropTargetLayer.DropTargetType.INTO_SUBMENU);
 
299
                    menu.repaint();
 
300
                } else {
 
301
                    menuEditLayer.dropTargetLayer.setDropTarget(menu, pt, DropTargetLayer.DropTargetType.INTER_MENU);
 
302
                }
 
303
                menuEditLayer.showMenuPopup(menu);
 
304
            }
 
305
            
 
306
            if(child == null) {
 
307
                menuEditLayer.dropTargetLayer.clearDropTarget();
 
308
            }
 
309
            
 
310
        } else {
 
311
            p("DragOperation: dragComponent shouldn't be null when moving");
 
312
        }
 
313
    }
 
314
    
 
315
    
 
316
    
 
317
    void end(Point pt) {
 
318
        end(pt, true);
 
319
    }
 
320
    void end(Point pt, boolean clear) {
 
321
        started = false;
 
322
        currentItem = null;
 
323
        if(dragComponent == null) return;
 
324
        p("ending an operation at: " + pt);
 
325
        menuEditLayer.layers.remove(dragComponent);
 
326
        menuEditLayer.dropTargetLayer.clearDropTarget();
 
327
        
 
328
        p("op = " + op);
 
329
        switch (op) {
 
330
        case PICK_AND_PLOP_FROM_PALETTE: completePickAndPlopFromPalette(pt, clear); break;
 
331
        case INTER_MENU_DRAG: completeInterMenuDrag(pt); break ;
 
332
        case NO_MENUBAR: /* do nothing */ break;
 
333
        }
 
334
        
 
335
        menuEditLayer.glassLayer.requestFocusInWindow();
 
336
        payloadComponent = null;
 
337
        targetComponent = null;
 
338
        menuEditLayer.repaint();
 
339
        
 
340
    }
 
341
    
 
342
    void fastEnd() {
 
343
        started = false;
 
344
        if(dragComponent != null) {
 
345
            menuEditLayer.layers.remove(dragComponent);
 
346
            menuEditLayer.repaint();
 
347
        }
 
348
        menuEditLayer.dropTargetLayer.clearDropTarget();
 
349
    }
 
350
    
 
351
    // only looks at JMenu and JMenubar RADComponents as well as anything in the popups
 
352
    JComponent getDeepestComponent(Point pt) {
 
353
        if(pt == null) return null;
 
354
        RADComponent rad = menuEditLayer.formDesigner.getHandleLayer().getMetaComponentAt(pt, HandleLayer.COMP_DEEPEST);
 
355
        if(rad != null && (JMenu.class.isAssignableFrom(rad.getBeanClass()) ||
 
356
                JMenuBar.class.isAssignableFrom(rad.getBeanClass()))) {
 
357
           return (JComponent) menuEditLayer.formDesigner.getComponent(rad);
 
358
        } else {
 
359
            return (JComponent) getDeepestComponentInPopups(pt);
 
360
        }
 
361
    }
 
362
    
 
363
    public JComponent getTargetComponent() {
 
364
        return targetComponent;
 
365
    }
 
366
    
 
367
    private void completeInterMenuDrag(Point pt) {
 
368
        p("================\n\n\n\n==========\n\n========");
 
369
        p("complete inter menu drag: target comp = " + targetComponent);
 
370
        if(targetComponent == null) return;
 
371
        
 
372
        //check if it's still a valid target
 
373
        JComponent tcomp = (JComponent) getDeepestComponent(pt);
 
374
        p("target = " + targetComponent);
 
375
        p("tcomp = " + tcomp);
 
376
        if(targetComponent != tcomp) {
 
377
            p("no longer over a valid target. bailing");
 
378
            menuEditLayer.formDesigner.toggleSelectionMode();
 
379
            return;
 
380
        }
 
381
        
 
382
        // conver to target component's coords.
 
383
        Point pt2 = SwingUtilities.convertPoint(menuEditLayer.glassLayer, pt, tcomp);
 
384
        if(tcomp instanceof JMenu) {
 
385
            JMenu menu = (JMenu) tcomp;
 
386
            
 
387
            
 
388
            // if dragging a jmenu onto a toplevel jmenu
 
389
            if(menu.getParent() instanceof JMenuBar && isOnlyJMenus(payloadComponents)) { //payloadComponent instanceof JMenu) {
 
390
                p("dropping into a toplevel menu");
 
391
                if(DropTargetLayer.isMenuLeftEdge(pt2, menu)) {
 
392
                    p("doing a left drop");
 
393
                    menuEditLayer.moveRadComponentToBefore(payloadComponent, menu);
 
394
                    return;
 
395
                } else if (DropTargetLayer.isMenuRightEdge(pt2, menu)) {
 
396
                    p("doing a right drop");
 
397
                    menuEditLayer.moveRadComponentToAfter(payloadComponent, menu);
 
398
                    return;
 
399
                } else {  // else must be in the center so just add to the menu instead of next to
 
400
                    p("doing a center drop");
 
401
                    menuEditLayer.moveRadComponentInto(payloadComponent, menu);
 
402
                    return;
 
403
                }
 
404
            }
 
405
            p("on a jmenu. could be in or above");
 
406
            p("converted point = " + pt2);
 
407
            if(DropTargetLayer.isSubMenuRightEdge(pt2, menu)) {
 
408
                p("doing 'in' menu drop");
 
409
                menuEditLayer.moveRadComponentInto(payloadComponent, menu);
 
410
            } else {
 
411
                p("doing above/below menu drop");
 
412
                if(DropTargetLayer.isBelowItem(pt2,menu)) {
 
413
                    menuEditLayer.moveRadComponentToAfter(payloadComponent, targetComponent);
 
414
                } else {
 
415
                    menuEditLayer.moveRadComponentToBefore(payloadComponent, targetComponent);
 
416
                }
 
417
            }
 
418
            return;
 
419
        }
 
420
        
 
421
        if(tcomp instanceof JMenuBar) {
 
422
            p("dragging to the menubar itself");
 
423
            if(payloadComponent instanceof JMenu) {
 
424
                p("adding menu to the end of the menubar");
 
425
                menuEditLayer.moveRadComponentInto(payloadComponent, targetComponent);
 
426
                return;
 
427
            } else {
 
428
                p("can't drag a non-JMenu to the menubar");
 
429
                return;
 
430
            }
 
431
        }
 
432
        
 
433
        //if after or before the current item
 
434
        if(DropTargetLayer.isBelowItem(pt2,tcomp)) {
 
435
            menuEditLayer.moveRadComponentToAfter(payloadComponent, targetComponent);
 
436
        } else {
 
437
            menuEditLayer.moveRadComponentToBefore(payloadComponent, targetComponent);
 
438
        }
 
439
    }
 
440
    
 
441
    private void completePickAndPlopFromPalette(Point pt, boolean clear) {
 
442
        p("complete pick and plop from palette: target comp = " + targetComponent);
 
443
        PaletteItem paletteItem = PaletteUtils.getSelectedItem();
 
444
        if(paletteItem == null) return;
 
445
        
 
446
        if(targetComponent == null) return;
 
447
        
 
448
        
 
449
        //check if it's still a valid target
 
450
        JComponent tcomp = (JComponent) getDeepestComponent(pt);
 
451
        p("target = " + targetComponent);
 
452
        p("tcomp = " + tcomp);
 
453
        if(targetComponent != tcomp) {
 
454
            p("no longer over a valid target. bailing");
 
455
            if(clear) {
 
456
                menuEditLayer.formDesigner.toggleSelectionMode();
 
457
            }
 
458
            return;
 
459
        }
 
460
        
 
461
        JComponent newComponent = null;
 
462
        // get the pre-created component
 
463
        MetaComponentCreator creator = menuEditLayer.formDesigner.getFormModel().getComponentCreator();
 
464
        if(creator != null) {
 
465
            RADVisualComponent precreated = creator.precreateVisualComponent(
 
466
                    paletteItem.getComponentClassSource());
 
467
            if(precreated != null) {
 
468
                newComponent = (JComponent) precreated.getBeanInstance();
 
469
            }
 
470
        }
 
471
        // if pre-creation failed then make new component manually
 
472
        if(newComponent == null) {
 
473
            try {
 
474
                newComponent = (JComponent)paletteItem.getComponentClass().newInstance();
 
475
            } catch (Exception ex) {
 
476
                p("couldn't create new component!");
 
477
                ex.printStackTrace();
 
478
                return;
 
479
            }
 
480
        }
 
481
        
 
482
        // add new component reference to the form
 
483
        // i can probably remove both of these variables
 
484
        LayoutComponent layoutComponent = creator.getPrecreatedLayoutComponent();
 
485
        Object constraints = null;
 
486
        
 
487
        
 
488
        Point pt2 = SwingUtilities.convertPoint(menuEditLayer.glassLayer, pt, targetComponent);
 
489
        // dragged to a menu, add inside the menu instead of next to it
 
490
        if(targetComponent instanceof JMenu) {
 
491
            p("============== doing a new comp to a jmenu");
 
492
            
 
493
            //toplevel menus
 
494
            if(targetComponent.getParent() instanceof JMenuBar) {
 
495
                p("on top of a toplevel menu");
 
496
                
 
497
                if(DropTargetLayer.isMenuLeftEdge(pt2, targetComponent) && isMenuPayload(creator)) {
 
498
                    p("on the left edge");
 
499
                    RADVisualComponent newRad = creator.getPrecreatedMetaComponent();
 
500
                    menuEditLayer.addRadComponentToBefore(newRad, targetComponent);
 
501
                } else if(DropTargetLayer.isMenuRightEdge(pt2, targetComponent) && isMenuPayload(creator)) {
 
502
                    p("on the right edge");
 
503
                    RADVisualComponent newRad = creator.getPrecreatedMetaComponent();
 
504
                    menuEditLayer.addRadComponentToAfter(newRad, targetComponent);
 
505
                } else {
 
506
                    p("else in the center");
 
507
                    menuEditLayer.addRadComponentToEnd(targetComponent, creator);
 
508
                }
 
509
            } else {
 
510
                if(DropTargetLayer.isSubMenuRightEdge(pt2, targetComponent)) {
 
511
                    p("doing in menu drop");
 
512
                    menuEditLayer.addRadComponentToEnd(targetComponent, creator);
 
513
                } else {
 
514
                    p("doing above/below menu drop");
 
515
                    RADVisualComponent newRad = creator.getPrecreatedMetaComponent();
 
516
                    p("new rad = " + newRad);
 
517
                    if(DropTargetLayer.isBelowItem(pt2, targetComponent)) {
 
518
                        menuEditLayer.addRadComponentToAfter(newRad, targetComponent);
 
519
                    } else {
 
520
                        menuEditLayer.addRadComponentToBefore(newRad, targetComponent);
 
521
                    }
 
522
                }
 
523
            }
 
524
        } else {
 
525
            if(targetComponent instanceof JMenuBar) {
 
526
                p("======= doing a new comp directly to the jmenubar");
 
527
                menuEditLayer.addRadComponentToEnd(targetComponent, creator);
 
528
            } else {
 
529
                // add the new component to the target's containing menu
 
530
                p("doing the new kind of add");
 
531
                RADVisualComponent newRad = creator.getPrecreatedMetaComponent();
 
532
                p("new rad = " + newRad);
 
533
                //menuEditLayer.addRadComponentToBefore(newRad, targetComponent);
 
534
                if(DropTargetLayer.isBelowItem(pt2, targetComponent)) {
 
535
                    menuEditLayer.addRadComponentToAfter(newRad, targetComponent);
 
536
                } else {
 
537
                    menuEditLayer.addRadComponentToBefore(newRad, targetComponent);
 
538
                }
 
539
            }
 
540
        }
 
541
        
 
542
        if(clear) {
 
543
            menuEditLayer.formDesigner.toggleSelectionMode();
 
544
        }
 
545
        
 
546
    }
 
547
    
 
548
    private boolean isOnlyJMenus(List<JMenuItem> items) {
 
549
        for(JMenuItem item : items) {
 
550
            if(item instanceof JMenu) continue;
 
551
            return false;
 
552
        }
 
553
        return true;
 
554
    }
 
555
    
 
556
    private boolean isMenuPayload(MetaComponentCreator creator) {
 
557
        if(JMenu.class.isAssignableFrom(creator.getPrecreatedMetaComponent().getBeanClass())) {
 
558
            return true;
 
559
        }
 
560
        return false;
 
561
    }
 
562
    
 
563
    //josh: this is a very slow way to find the component under the mouse cursor.
 
564
    //there must be a faster way to do it
 
565
    public JComponent getDeepestComponentInPopups(Point pt) {
 
566
        Component[] popups = menuEditLayer.layers.getComponentsInLayer(JLayeredPane.DEFAULT_LAYER);
 
567
        for(Component popup : popups) {
 
568
            //p("looking at popup: " + popup);
 
569
            if(popup.isVisible()) {
 
570
                Point pt2 = SwingUtilities.convertPoint(menuEditLayer, pt, popup);
 
571
                JComponent child = (JComponent) javax.swing.SwingUtilities.getDeepestComponentAt(popup, pt2.x, pt2.y);
 
572
                if(child != null) return child;
 
573
            }
 
574
        }
 
575
        return null;
 
576
    }
 
577
    
 
578
    
 
579
    public boolean isStarted() {
 
580
        return started;
 
581
    }
 
582
    
 
583
    public PaletteItem getCurrentItem() {
 
584
        return currentItem;
 
585
    }
 
586
    
 
587
    private static void p(String s) {
 
588
        if(DEBUG) {
 
589
            System.out.println(s);
 
590
        }
 
591
    }
 
592
}