KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > form > layoutsupport > delegates > GridBagCustomizer


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.form.layoutsupport.delegates;
21
22 import java.beans.*;
23 import java.awt.*;
24 import java.awt.event.*;
25 import javax.swing.*;
26 import java.util.*;
27
28 import org.openide.nodes.*;
29 import org.openide.explorer.propertysheet.PropertySheet;
30 import org.openide.util.*;
31 import org.netbeans.api.javahelp.Help;
32
33 import org.netbeans.modules.form.*;
34 import org.netbeans.modules.form.layoutsupport.LayoutSupportManager;
35
36 /** A customizer providing better editing facility for GridBagLayout
37  *
38  * @author Petr Hrebejk
39  */

40 final public class GridBagCustomizer extends JPanel implements Customizer
41 {
42     /** bundle to obtain text information from */
43 // private static java.util.ResourceBundle bundle = org.openide.util.NbBundle.getBundle(GridBagCustomizer.class);
44

45     // -----------------------------------------------------------------------------
46
// private area
47

48     PropertyChangeSupport propertySupport = new PropertyChangeSupport(this);
49
50     static final int TOP = 1;
51     static final int BOTTOM = 2;
52     static final int LEFT = 4;
53     static final int RIGHT = 8;
54
55     static final int HORIZONTAL = LEFT + RIGHT;
56     static final int VERTICAL = TOP + BOTTOM;
57
58     static final int PLUS = 1;
59     static final int MINUS = -1;
60
61     private static final ImageIcon REMAINDER_ICON = new ImageIcon(
62         GridBagCustomizer.class.getResource("/org/netbeans/modules/form/layoutsupport/resources/remainder.gif")); // NOI18N
63

64 // private DesignGridBagLayout designLayout;
65
private GridBagLayoutSupport layoutSupport;
66
67     private FormModel formModel;
68     private FormModelListener formListener;
69     private RADVisualContainer radContainer;
70     private RADVisualComponent[] radComponents;
71     private GBComponentProxy[] gbcProxies;
72
73     // Customizer components
74
private JSplitPane splitPane;
75     private JPanel designPanel;
76
77     private GridBagControlCenter controlCenter;
78     private GBContainerProxy containerProxy;
79     private PropertySheet propertySheet;
80     private javax.swing.JScrollPane JavaDoc designScrollPane;
81     private JLayeredPane designLayeredPane;
82
83     private GBComponentProxy.DragLabel dragLabel = null;
84
85     /** This is a hack. We need to now whether the GLC is painted first time
86      * in order to paint empty cols and rows correctlly. Field is setted in
87      * setObject() and Ppaint() methods.
88      */

89     private boolean firstPaint = false;
90
91     static final long serialVersionUID =-632768048562391785L;
92
93     public GridBagCustomizer() {
94         initComponents();
95     }
96
97     private void initialize() {
98
99 // initComponents();
100

101         radContainer = ((LayoutSupportManager)
102                             layoutSupport.getLayoutSupportHack())
103                         .getMetaContainer(); // ugly hack
104
formModel = radContainer.getFormModel();
105         radComponents = radContainer.getSubComponents();
106
107         gbcProxies = new GBComponentProxy[radComponents.length];
108         for (int i = 0; i < radComponents.length; i++) {
109             gbcProxies[i] = new GBComponentProxy(radComponents[i], containerProxy);
110         }
111
112         containerProxy.removeAll();
113         
114         FormDesigner designer = FormEditor.getFormDesigner(formModel);
115         if (!designer.isInDesignedTree(radContainer)) {
116             designer.setTopDesignComponent(radContainer, true);
117             // terrible hack - wait for designer update
118
invokeLater(2, new Runnable JavaDoc() {
119                 public void run() {
120                     containerProxy.addAllProxies();
121                 }
122             });
123         }
124         else containerProxy.addAllProxies();
125
126         formListener = new FormListener();
127         formModel.addFormModelListener(formListener);
128     }
129
130     void customizerClosed() {
131         formModel.removeFormModelListener(formListener);
132     }
133
134     /** inits the components of the customizer */
135
136     private void initComponents() {
137
138         setBorder(new javax.swing.border.EmptyBorder JavaDoc(4, 0, 8, 0));
139         setLayout(new BorderLayout()); // [PENDING]
140

141         propertySheet = new PropertySheet();
142         try {
143             propertySheet.setSortingMode(PropertySheet.UNSORTED);
144         }
145         catch (java.beans.PropertyVetoException JavaDoc e) {
146 // ErrorManager.getDefault().notifyException(e);
147
}
148         propertySheet.setPreferredSize(new Dimension(300, 380));
149
150         JPanel panel = new JPanel();
151         panel.setLayout(new BorderLayout());
152         panel.add(propertySheet, BorderLayout.CENTER);
153         controlCenter = new GridBagControlCenter(this);
154         panel.add(controlCenter, BorderLayout.SOUTH);
155
156         designScrollPane = new javax.swing.JScrollPane JavaDoc();
157         designPanel = new JPanel() {
158             public Dimension getPreferredSize() {
159                 //System.out.println(" THE pref size of DesignPanel " + containerProxy.getPreferredSize()); // NOI18N
160
return containerProxy.getPreferredSize();
161             }
162         };
163
164         designPanel.setLayout(new GridBagLayout());
165         designPanel.setBackground((java.awt.Color JavaDoc) javax.swing.UIManager.getDefaults().get("desktop")); // NOI18N
166
GridBagConstraints con = new GridBagConstraints();
167         con.anchor = GridBagConstraints.CENTER;
168         con.fill = GridBagConstraints.NONE;
169         containerProxy = new GBContainerProxy();
170
171         designPanel.add(containerProxy, con);
172
173         designLayeredPane = new JLayeredPane() {
174             public Dimension getPreferredSize() {
175
176                 Dimension dpd = designPanel.getPreferredSize();
177                 Dimension spd = designScrollPane.getViewport().getExtentSize();
178
179                 int width = Math.max(dpd.width + 40, spd.width);
180                 int height = Math.max(dpd.height + 40 , spd.height);
181
182                 /*
183                   Dimension dpd = designPanel.getPreferredSize();
184                   Dimension spd = designScrollPane.getViewport().getExtentSize();
185
186                   return new Dimension(Math.max(dpd.width, spd.width), Math.max(dpd.height, spd.height));
187                 */

188                 //System.out.println(" THE GET " + new Dimension(Math.max(400, width), Math.max(300, height))); // NOI18N
189

190                 return new Dimension(Math.max(500, width), Math.max(300, height));
191             }
192         };
193
194         //designLayeredPane.setLayout(new BorderLayout());
195

196         //designLayeredPane.setLayer(designPanel, JLayeredPane.DEFAULT_LAYER.intValue());
197
designLayeredPane.add(designPanel, JLayeredPane.DEFAULT_LAYER);
198         designPanel.setBounds(20, 20, designPanel.getPreferredSize().width, designPanel.getPreferredSize().height);
199         designLayeredPane.setOpaque(true);
200         designLayeredPane.setBackground((java.awt.Color JavaDoc) javax.swing.UIManager.getDefaults().get("desktop")); // NOI18N
201

202         designScrollPane.setViewportView(designLayeredPane);
203         
204         splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
205         splitPane.setLeftComponent(panel);
206         splitPane.setRightComponent(designScrollPane);
207         splitPane.setUI(new javax.swing.plaf.basic.BasicSplitPaneUI JavaDoc());
208         splitPane.setBorder(BorderFactory.createEmptyBorder());
209         splitPane.setContinuousLayout(true);
210
211         add(splitPane, BorderLayout.CENTER);
212
213         getAccessibleContext().setAccessibleDescription(GridBagLayoutSupport.getBundleHack().getString("ACSD_GridBagCustomizer"));
214         HelpCtx.setHelpIDString(this, "gui.layouts.gbcustomizer"); // NOI18N
215
}
216
217     void setAnchor(int anchor) {
218         java.util.List JavaDoc selected = containerProxy.getSelectedProxies();
219         Iterator it = selected.iterator();
220         while (it.hasNext())
221 // setProperty((GBComponentProxy)it.next(), DesignGridBagLayout.PROP_ANCHOR, new Integer(anchor));
222
setProperty((GBComponentProxy)it.next(), "anchor", new Integer JavaDoc(anchor)); // NOI18N
223
}
224
225     void setFill(int fill) {
226         java.util.List JavaDoc selected = containerProxy.getSelectedProxies();
227         Iterator it = selected.iterator();
228         while (it.hasNext())
229 // setProperty((GBComponentProxy)it.next(), DesignGridBagLayout.PROP_FILL, new Integer(fill));
230
setProperty((GBComponentProxy)it.next(), "fill", new Integer JavaDoc(fill)); // NOI18N
231
};
232
233
234
235     void modifyIPad(int action, int what) {
236         java.util.List JavaDoc selected = containerProxy.getSelectedProxies();
237         Iterator it = selected.iterator();
238         while (it.hasNext()) {
239             GBComponentProxy p =(GBComponentProxy)it.next();
240             int value = what == HORIZONTAL ? p.getRealConstraints().ipadx : p.getRealConstraints().ipady;
241             value += action;
242             if (value < 0)
243                 continue;
244             setProperty(p,
245 // what == HORIZONTAL ? DesignGridBagLayout.PROP_IPADX : DesignGridBagLayout.PROP_IPADY ,
246
what == HORIZONTAL ? "ipadx" : "ipady" , // NOI18N
247
new Integer JavaDoc(value));
248         }
249     }
250
251     void modifyInsets(int action, int what) {
252         java.util.List JavaDoc selected = containerProxy.getSelectedProxies();
253         Iterator it = selected.iterator();
254         while (it.hasNext()) {
255             GBComponentProxy p =(GBComponentProxy)it.next();
256             Insets old_insets = p.getRealConstraints().insets;
257             Insets new_insets =(Insets)old_insets.clone();
258
259             if ((what & TOP) != 0) {
260                 new_insets.top += action;
261                 if (new_insets.top < 0)
262                     new_insets.top = 0;
263             }
264             if ((what & BOTTOM) != 0) {
265                 new_insets.bottom += action;
266                 if (new_insets.bottom < 0)
267                     new_insets.bottom = 0;
268             }
269             if ((what & LEFT) != 0) {
270                 new_insets.left += action;
271                 if (new_insets.left < 0)
272                     new_insets.left = 0;
273             }
274             if ((what & RIGHT) != 0) {
275                 new_insets.right += action;
276                 if (new_insets.right < 0)
277                     new_insets.right = 0;
278             }
279
280 // setProperty(p, DesignGridBagLayout.PROP_INSETS, new_insets);
281
setProperty(p, "insets", new_insets); // NOI18N
282
}
283     }
284
285
286     void modifyGridSize(int action, int what) {
287         java.util.List JavaDoc selected = containerProxy.getSelectedProxies();
288         Iterator it = selected.iterator();
289
290         while (it.hasNext()) {
291             GBComponentProxy p =(GBComponentProxy)it.next();
292             int value = what == HORIZONTAL ? p.getRealConstraints().gridwidth : p.getRealConstraints().gridheight;
293
294             if (action == 0)
295                 value = value == 0 ? 1 : 0;
296             else {
297                 value += action;
298                 if (value < 1) {
299                     value = 1;
300                 }
301             }
302
303             setProperty(p,
304 // what == HORIZONTAL ? DesignGridBagLayout.PROP_GRIDWIDTH : DesignGridBagLayout.PROP_GRIDHEIGHT ,
305
what == HORIZONTAL ? "gridwidth" : "gridheight" , // NOI18N
306
new Integer JavaDoc(value));
307         }
308     }
309
310     private void setProperty(GBComponentProxy p, String JavaDoc name, Object JavaDoc value) {
311         Node.Property prop = p.getComponent().getPropertyByName(
312                                         "GridBagLayoutConstraints "+name); // NOI18N
313
if (prop != null) {
314             try {
315                 prop.setValue(value);
316             }
317             catch (Exception JavaDoc ex) { // ignore
318
ex.printStackTrace();
319             }
320         }
321     }
322
323     // -----------------------------------------------------------------------------
324
// Customizer implementation
325

326     /**
327      * Set the object to be customized. This method should be called only
328      * once, before the Customizer has been added to any parent AWT container.
329      * @param bean The object to be customized.
330      */

331     public void setObject(Object JavaDoc bean) {
332         layoutSupport = (GridBagLayoutSupport) bean;
333 // ((LayoutSupportManager)bean).getLayoutDelegate();
334
initialize();
335         firstPaint = true;
336     }
337
338     /**
339      * Register a listener for the PropertyChange event. The customizer
340      * should fire a PropertyChange event whenever it changes the target
341      * bean in a way that might require the displayed properties to be
342      * refreshed.
343      *
344      * @param listener An object to be invoked when a PropertyChange
345      * event is fired.
346      */

347 // public void addPropertyChangeListener(PropertyChangeListener listener) {
348
// propertySupport.addPropertyChangeListener(listener);
349
// }
350

351     /**
352      * Remove a listener for the PropertyChange event.
353      *
354      * @param listener The PropertyChange listener to be removed.
355      */

356 // public void removePropertyChangeListener(PropertyChangeListener listener) {
357
// propertySupport.removePropertyChangeListener(listener);
358
// }
359
/*
360       public void propertyChange(final java.beans.PropertyChangeEvent p0) {
361       System.out.println("PCH :" + p0);
362       }
363     */

364
365     // -----------------------------------------------------------------------------
366
// Form listener implementation
367

368     class FormListener implements FormModelListener {
369         public void formChanged(FormModelEvent[] events) {
370             if (events != null && GridBagCustomizer.this.isShowing()) {
371                 boolean modifying = false;
372                 for (int i=0; i < events.length; i++)
373                     if (events[i].isModifying()) {
374                         modifying = true;
375                         break;
376                     }
377                 if (!modifying)
378                     return;
379
380                 // we perform update after designer is updated which takes
381
// three dispatch events - this is only temporary patch
382
// (very very very ugly :-)
383
// (it would be much nicer to have some listener on
384
// FormDesigner directly...)
385
invokeLater(3, new Runnable JavaDoc() {
386                     public void run() {
387                         containerProxy.updateAllProxies();
388                     }
389                 });
390             }
391 // if (propertyName == DesignGridBagLayout.PROP_ANCHOR || propertyName == DesignGridBagLayout.PROP_FILL ||
392
// propertyName == DesignGridBagLayout.PROP_GRIDWIDTH || propertyName == DesignGridBagLayout.PROP_GRIDHEIGHT)
393
// controlCenter.newSelection(containerProxy.getSelectedProxies());
394
}
395     }
396
397     void waitForDesignerUpdate(int count) {
398         try {
399             while (count > 0) {
400                 SwingUtilities.invokeAndWait(new Runnable JavaDoc() {
401                     public void run() {
402                     }
403                 });
404                 count--;
405             }
406         }
407         catch (Exception JavaDoc e) {
408             e.printStackTrace();
409         }
410     }
411
412     void invokeLater(final int count, final Runnable JavaDoc task) {
413         if (count == 0)
414             task.run();
415         else
416             SwingUtilities.invokeLater(new Runnable JavaDoc() {
417                 public void run() {
418                     invokeLater(count-1, task);
419                 }
420             });
421     }
422
423 // public void layoutChanged(RADVisualContainer container,RADVisualComponent component,
424
// String propertyName,Object oldValue,Object newValue) {
425
// //System.out.println("Layout changed" + propertyName + newValue); // NOI18N
426
// containerProxy.updateAllProxies();
427
// if (propertyName == DesignGridBagLayout.PROP_ANCHOR || propertyName == DesignGridBagLayout.PROP_FILL ||
428
// propertyName == DesignGridBagLayout.PROP_GRIDWIDTH || propertyName == DesignGridBagLayout.PROP_GRIDHEIGHT)
429
// controlCenter.newSelection(containerProxy.getSelectedProxies());
430
// }
431

432 // public void eventAdded(FormEventEvent evt) {}
433
// public void eventRemoved(FormEventEvent evt) {}
434
// public void eventRenamed(FormEventEvent evt) {}
435

436
437     void innerLayoutChanged() {
438
439         /*
440           System.out.println("1 CP Size : " + containerProxy.getSize());
441           System.out.println("1 CP Preff Size : " + containerProxy.getSize());
442
443           containerProxy.invalidate();
444           containerProxy.revalidate();
445           containerProxy.widenEmpty();
446           containerProxy.revalidate();
447
448           designPanel.invalidate();
449           designPanel.validate();
450           designPanel.revalidate();
451         */

452         //designLayeredPane.invalidate();
453

454
455         containerProxy.widenEmpty();
456         designPanel.setBounds(20, 20, designPanel.getPreferredSize().width, designPanel.getPreferredSize().height);
457
458         //containerProxy.widenEmpty();
459
//containerProxy.invalidate();
460
containerProxy.revalidate();
461
462         //designPanel.invalidate();
463
//designPanel.validate();
464
//designPanel.revalidate();
465

466         designLayeredPane.revalidate();
467         //designLayeredPane.repaint();
468

469         /*
470           System.out.println("2 CP Size : " + containerProxy.getSize());
471           System.out.println("2 CP Preff Size : " + containerProxy.getPreferredSize());
472           System.out.println("2 DP Size : " + designPanel.getSize());
473           System.out.println("2 DP Preff Size : " + designPanel.getPreferredSize());
474           System.out.println("2 DLP Size : " + designLayeredPane.getSize());
475           System.out.println("2 DLP Preff Size : " + designLayeredPane.getPreferredSize());
476         */

477
478     }
479
480     // -----------------------------------------------------------------------------
481
// Innerclasses
482

483
484     /** Proxy component for one component in the container */
485     class GBComponentProxy extends JPanel {
486         /*
487           private String name;
488
489           GBComponent(String name) {
490           this.name = name;
491           }
492         */

493
494         private GBContainerProxy parentProxy;
495         private RADVisualComponent component;
496         private ComponentProxyNode node;
497         private javax.swing.border.CompoundBorder JavaDoc compoundBorder;
498         private javax.swing.border.MatteBorder JavaDoc insetsBorder;
499         private javax.swing.border.MatteBorder JavaDoc remainderBorder;
500         // private JPanel innerPanel;
501
private JLabel componentLabel;
502         private GridBagLayout layout;
503 // private GridBagConstraints componentConstraints;
504
private Color INSETS_COLOR = new Color(255, 255, 204);
505         private Color CELL_COLOR = new Color(153, 153, 255);
506
507         private boolean isSelected = false;
508
509         static final long serialVersionUID =-6552012922564179923L;
510
511         GBComponentProxy(final RADVisualComponent component, GBContainerProxy parentProxy) {
512
513             this.component = component;
514             this.parentProxy = parentProxy;
515
516             //setLayout(new BorderLayout());
517

518             componentLabel = new javax.swing.JLabel JavaDoc()
519             {
520                 public void paint(Graphics g) {
521                     int borderSize = 5;
522                     Color borderColor = Color.blue;
523
524                     super.paint(g);
525
526                     if (GBComponentProxy.this.isSelected()) {
527
528                         Dimension size = getSize();
529                         g.setColor(borderColor);
530                         g.fillRect(0, 0, borderSize, borderSize); // UpLeft
531
g.fillRect(size.width-borderSize, 0, borderSize, borderSize); // UpRight
532
g.fillRect(0, size.height-borderSize, borderSize, borderSize); // LoLeft
533
g.fillRect(size.width-borderSize, size.height-borderSize, borderSize, borderSize); // LoRight
534

535
536                         /*
537                         //if (resizable) {
538                         //g.fillRect(midHor, 0, borderSize, borderSize); // UpMid
539                         //g.fillRect(0, midVer, borderSize, borderSize); // LeftMid
540                         g.fillRect(size.width-borderSize, midVer, borderSize, borderSize); // RightMid
541                         g.fillRect(midHor, size.height-borderSize, borderSize, borderSize); // LoMid
542                         //}
543                         */

544                     }
545
546                 }
547
548                 public Dimension getPreferredSize() {
549                     FormDesigner designer = FormEditor.getFormDesigner(formModel);
550                     Component comp = (Component)designer.getComponent(component);
551                     Dimension size;
552                     if (comp == null) {
553                         comp = (Component) component.getBeanInstance();
554                         size = comp.getPreferredSize();
555                     }
556                     else {
557                         if (designer.isOpened()) {
558                             size = comp.getSize();
559                             if (size.width > 4096) // [hack for issue 32311]
560
size.width = comp.getPreferredSize().width;
561                         }
562                         else size = comp.getPreferredSize();
563                     }
564
565                     // Use a new instance - avoid modification of
566
// the preferred size of the component (issue 48033)
567
size = new Dimension(size);
568                     
569                     if (comp instanceof JComponent && !(comp instanceof JPanel)) {
570                         Insets thisIns = getInsets();
571                         if (comp instanceof JComponent) {
572                             javax.swing.border.Border JavaDoc b = ((JComponent)comp).getBorder();
573                             if (b != null) {
574                                 Insets ins = b.getBorderInsets(comp);
575                                 thisIns.top -= ins.top;
576                                 thisIns.left -= ins.left;
577                                 thisIns.bottom -= ins.bottom;
578                                 thisIns.right -= ins.right;
579                             }
580                         }
581                         if (thisIns.top > 0) size.height += thisIns.top;
582                         if (thisIns.bottom > 0) size.height += thisIns.bottom;
583                         if (thisIns.left > 0) size.width += thisIns.left;
584                         if (thisIns.right > 0) size.width += thisIns.right;
585                     }
586
587                     if (size.width < 6) size.width = 6;
588                     if (size.height < 6) size.height = 6;
589
590                     return size;
591                 }
592
593                 public Dimension getMinimumSize() {
594                     Component comp = (Component)
595                         FormEditor.getFormDesigner(formModel).getComponent(component);
596                     if (comp == null)
597                         comp = (Component) component.getBeanInstance();
598                     return comp.getMinimumSize();
599 // return component.getComponent().getMinimumSize();
600
}
601
602                 public Dimension getMaximumSize() {
603                     Component comp = (Component)
604                         FormEditor.getFormDesigner(formModel).getComponent(component);
605                     if (comp == null)
606                         comp = (Component) component.getBeanInstance();
607                     return comp.getMaximumSize();
608 // return component.getComponent().getMaximumSize();
609
}
610             };
611             componentLabel.setOpaque(true);
612             componentLabel.setBorder(new javax.swing.border.EtchedBorder JavaDoc());
613             componentLabel.setText(component.getName());
614             componentLabel.setHorizontalAlignment(0);
615             componentLabel.addMouseListener(new java.awt.event.MouseAdapter JavaDoc() {
616                 public void mouseClicked(java.awt.event.MouseEvent JavaDoc evt) {
617                     GBComponentProxy.this.mouseClicked(evt);
618                 }
619
620                 public void mousePressed(java.awt.event.MouseEvent JavaDoc evt) {
621                     GBComponentProxy.this.mousePressed(evt);
622                 }
623
624                 public void mouseReleased(java.awt.event.MouseEvent JavaDoc evt) {
625                     GBComponentProxy.this.mouseReleased(evt);
626                 }
627
628             });
629
630             componentLabel.addMouseMotionListener(new java.awt.event.MouseMotionAdapter JavaDoc() {
631                 public void mouseDragged(java.awt.event.MouseEvent JavaDoc evt) {
632                     GBComponentProxy.this.mouseDragged(evt);
633                 }
634             }
635                                                   );
636
637
638             /*
639               innerPanel = new JPanel();
640               innerPanel.setLayout(layout = new GridBagLayout());
641               innerPanel.setBackground(CELL_COLOR);
642               innerPanel.setOpaque(true);
643                     
644               innerPanel.add(componentLabel, getInnerComponentConstraints());
645               add(innerPanel);
646             */

647
648
649             setLayout(layout = new GridBagLayout());
650             setBackground(CELL_COLOR);
651             setOpaque(true);
652             updateByComponent();
653             add(componentLabel, getInnerComponentConstraints());
654             node = new ComponentProxyNode(component.getNodeReference());
655         }
656
657         RADVisualComponent getComponent() {
658             return component;
659         }
660
661 // DesignGridBagLayout.GridBagConstraintsDescription getConstraints() {
662
// return(DesignGridBagLayout.GridBagConstraintsDescription) component.getConstraints(DesignGridBagLayout.class);
663
// }
664

665         GridBagConstraints getRealConstraints() {
666             return (GridBagConstraints)
667                    component.getParentContainer().getLayoutSupport()
668                        .getConstraints(component).getConstraintsObject();
669 // return getConstraints().getGridBagConstraints();
670
}
671
672         ComponentProxyNode getNode() {
673             return node;
674         }
675
676         void updateByComponent() {
677             layout.setConstraints(componentLabel, getInnerComponentConstraints());
678         }
679
680
681         /*
682           public void paint(Graphics g) {
683           int borderSize = 5;
684           Color borderColor = Color.blue;
685          
686           super.paint(g);
687           
688           if (isSelected()) {
689           Dimension size = getSize();
690           int midHor =(size.width - borderSize) / 2;
691           int midVer =(size.height - borderSize) / 2;
692           g.setColor(borderColor);
693           g.fillRect(0, 0, borderSize, borderSize); // UpLeft
694           g.fillRect(size.width-borderSize, 0, borderSize, borderSize); // UpRight
695           g.fillRect(0, size.height-borderSize, borderSize, borderSize); // LoLeft
696           g.fillRect(size.width-borderSize, size.height-borderSize, borderSize, borderSize); // LoRight
697
698             
699           //if (resizable) {
700           g.fillRect(midHor, 0, borderSize, borderSize); // UpMid
701           g.fillRect(0, midVer, borderSize, borderSize); // LeftMid
702           g.fillRect(size.width-borderSize, midVer, borderSize, borderSize); // RightMid
703           g.fillRect(midHor, size.height-borderSize, borderSize, borderSize); // LoMid
704           //}
705           }
706           }
707         */

708
709         GridBagConstraints getInnerComponentConstraints() {
710
711             GridBagConstraints con = new GridBagConstraints();
712             con.anchor = getRealConstraints().anchor;
713             con.fill = getRealConstraints().fill;
714             con.gridheight = 1;
715             con.gridwidth = 1;
716             con.gridx = 0;
717             con.gridy = 0;
718             // con.insets
719
con.ipadx = getRealConstraints().ipadx;
720             con.ipady = getRealConstraints().ipady;
721             con.weightx = 1.0;
722             con.weighty = 1.0;
723
724             return con;
725         }
726
727
728         GridBagConstraints getProxyConstraints() {
729             GridBagConstraints con = new GridBagConstraints();
730
731             con.anchor = GridBagConstraints.CENTER;
732             con.fill = GridBagConstraints.BOTH;
733             con.gridheight = getRealConstraints().gridheight;
734             con.gridwidth = getRealConstraints().gridwidth;
735             con.gridx = getRealConstraints().gridx;
736             con.gridy = getRealConstraints().gridy;
737             con.insets = new Insets(3, 3, 3, 3);
738             con.ipadx = 0;
739             con.ipady = 0;
740             //con.weightx = getRealConstraints().weightx;
741
//con.weighty = getRealConstraints().weighty;
742
con.weightx = 1.0;
743             con.weighty = 1.0;
744             Insets in = getRealConstraints().insets;
745
746             insetsBorder = new javax.swing.border.MatteBorder JavaDoc(in.top, in.left, in.bottom, in.right, INSETS_COLOR);
747             remainderBorder = new javax.swing.border.MatteBorder JavaDoc(0, 0, con.gridheight == 0 ? 4 : 0, con.gridwidth == 0 ? 4 : 0, REMAINDER_ICON);
748             compoundBorder = new javax.swing.border.CompoundBorder JavaDoc(remainderBorder, insetsBorder);
749             setBorder(compoundBorder);
750
751             return con;
752         }
753
754         boolean isSelected() {
755             return isSelected;
756         }
757
758         void setSelected(boolean isSelected) {
759             if (this.isSelected == isSelected)
760                 return;
761
762             this.isSelected = isSelected;
763             //innerPanel.invalidate();
764
componentLabel.repaint();
765             /*
766               componentLabel.setBackground(isSelected ?
767               (java.awt.Color) javax.swing.UIManager.getDefaults().get("controlLtHighlight") :
768               (java.awt.Color) javax.swing.UIManager.getDefaults().get("Button.background"));
769               componentLabel.repaint();
770             */

771         }
772
773         void mouseClicked(java.awt.event.MouseEvent JavaDoc evt) {
774             /*
775               if (evt.isShiftDown())
776               parentProxy.shiftSelect(this);
777               else
778               parentProxy.select(this);
779             */

780         }
781
782         void mousePressed(java.awt.event.MouseEvent JavaDoc evt) {
783             if (evt.isControlDown())
784                 parentProxy.shiftSelect(this);
785             else
786                 parentProxy.select(this);
787         }
788
789         void mouseReleased(java.awt.event.MouseEvent JavaDoc evt) {
790             if (dragLabel != null) {
791
792                 if (!dragLabel.getLastIndex().equals(dragLabel.getOriginalIndex())) {
793 // setProperty(this, DesignGridBagLayout.PROP_GRIDX, new Integer(dragLabel.getLastIndex().x));
794
// setProperty(this, DesignGridBagLayout.PROP_GRIDY, new Integer(dragLabel.getLastIndex().y));
795
setProperty(this, "gridx", new Integer JavaDoc(dragLabel.getLastIndex().x)); // NOI18N
796
setProperty(this, "gridy", new Integer JavaDoc(dragLabel.getLastIndex().y)); // NOI18N
797
}
798
799                 designLayeredPane.remove(dragLabel);
800                 dragLabel = null;
801                 componentLabel.setCursor(Cursor.getDefaultCursor());
802                 designLayeredPane.repaint();
803             }
804         }
805
806         void mouseDragged(java.awt.event.MouseEvent JavaDoc evt) {
807
808             //System.out.print("Dragged "); // NOI18N
809

810
811             if (dragLabel == null) {
812                 if (isSelected()) {
813
814                     //System.out.println(" - first time"); // NOI18N
815

816
817                     dragLabel = new DragLabel();
818                     dragLabel.setHotSpot(evt.getPoint());
819                     dragLabel.resolveOrigin();
820                     dragLabel.setLastIndex(dragLabel.getIndex(evt.getPoint()));
821                     dragLabel.setOriginalIndex(dragLabel.getIndex(evt.getPoint()));
822
823                     designLayeredPane.setLayer(dragLabel, JLayeredPane.DRAG_LAYER.intValue());
824                     designLayeredPane.add(dragLabel, BorderLayout.CENTER);
825                     dragLabel.setBounds(evt.getPoint().x, evt.getPoint().y);
826                     componentLabel.setCursor(Cursor.getPredefinedCursor(Cursor.MOVE_CURSOR));
827                 }
828                 else {
829                     //System.out.println(" - Undefined"); // NOI18N
830

831                 }
832             }
833             else
834             {
835
836                 Point newLoc;
837                 if (!dragLabel.getLastIndex().equals(newLoc = dragLabel.getIndex(evt.getPoint()))) {
838
839                     Point converted = SwingUtilities.convertPoint(dragLabel, evt.getPoint(), designLayeredPane);
840
841                     //System.out.println(" - with layout change"); // NOI18N
842

843
844                     GridBagConstraints con = getProxyConstraints();
845
846                     con.gridx = newLoc.x;
847                     con.gridy = newLoc.y;
848
849                     parentProxy.layout.setConstraints(this, con);
850
851                     /*
852                       invalidate();
853                       doLayout();
854                     */

855                     //System.out.println("Constraints set"); // NOI18N
856

857
858                     // parentProxy.widenEmpty();
859

860                     //System.out.println(" Widden "); // NOI18N
861

862
863                     //invalidate();
864
//innerPanel.invalidate();
865
//parentProxy.invalidateAllProxies();
866

867                     //parentProxy.invalidate();
868
//parentProxy.revalidate();
869

870                     //parentProxy.widenEmpty();
871

872                     //innerLayoutChanged();
873

874
875
876                     dragLabel.resolveOrigin();
877
878                     SwingUtilities.convertPoint(designLayeredPane, converted, dragLabel);
879                     //dragLabel.setBounds(converted.x, converted.y);
880
Rectangle r = dragLabel.getBounds();
881                     Point loc = r.getLocation();
882                     loc.x += r.width + 20;
883                     loc.y += r.height + 20;
884                     //designScrollPane.getViewport().setViewPosition(r.getLocation());
885

886                     innerLayoutChanged();
887                     dragLabel.repaint();
888
889                     //System.out.println(" layout changed"); // NOI18N
890

891
892                     dragLabel.setLastIndex(newLoc);
893
894                 }
895                 else
896                     //System.out.println(" - simple"); // NOI18N
897

898                     dragLabel.setBounds(evt.getPoint().x, evt.getPoint().y);
899             }
900         }
901
902         /** Innerclass for the component which is dragged */
903         class DragLabel extends JLabel {
904
905
906             Point origin;
907             Point hotSpot;
908             Point lastIndex;
909             Point originalIndex;
910
911             private Dimension preferredSize;
912
913             static final long serialVersionUID =992490305277357953L;
914             DragLabel() {
915                 setOpaque(false);
916                 setEnabled(false);
917                 setBorder(new javax.swing.border.EtchedBorder JavaDoc());
918                 setText(component.getName());
919                 setHorizontalAlignment(0);
920
921                 preferredSize =(Dimension)componentLabel.getPreferredSize().clone();
922             }
923
924             public Dimension getPreferredSize() {
925                 preferredSize.height = componentLabel.getPreferredSize().height + getRealConstraints().ipady;
926                 preferredSize.width = componentLabel.getPreferredSize().width + getRealConstraints().ipadx;
927
928                 return preferredSize;
929             }
930
931             public Dimension getMinimumSize() {
932                 return componentLabel.getMinimumSize();
933             }
934
935             public Dimension getMaximumSize() {
936                 return componentLabel.getMaximumSize();
937             }
938
939             void setHotSpot(Point hotSpot) {
940                 this.hotSpot = hotSpot;
941             }
942
943             void resolveOrigin() {
944                 origin = SwingUtilities.convertPoint(componentLabel, 0, 0, designLayeredPane);
945             }
946
947             Point getLastIndex() {
948                 return lastIndex;
949             }
950
951             void setLastIndex(Point lastIndex) {
952                 this.lastIndex = lastIndex;
953             }
954
955             Point getOriginalIndex() {
956                 return originalIndex;
957             }
958
959             void setOriginalIndex(Point originalIndex) {
960                 this.originalIndex = originalIndex;
961             }
962
963             Point getIndex(Point p) {
964                 return parentProxy.getLayoutLocation(SwingUtilities.convertPoint(componentLabel, p, parentProxy));
965             }
966
967             public void setBounds(int x, int y) {
968                 resolveOrigin();
969                 super.setBounds(origin.x + x - hotSpot.x , origin.y + y - hotSpot.y, getPreferredSize().width, getPreferredSize().height);
970             }
971
972         }
973
974     }
975
976     /** Properties of this node are displayed in the layout property sheet */
977     static class ComponentProxyNode extends FilterNode {
978         
979         ComponentProxyNode(Node original) {
980             super(original);
981         }
982         
983         public Node.PropertySet[] getPropertySets() {
984             Node.PropertySet[] sets = super.getPropertySets();
985             for (int i=0; i < sets.length; i++)
986                 if ("layout".equals(sets[i].getName())) // NOI18N
987
return new Node.PropertySet[] { sets[i] };
988                     return new Node.PropertySet[0]; // cannot return null...
989
}
990     }
991     
992
993     /** Proxy for the container it's layout is edited */
994     class GBContainerProxy extends JPanel {
995
996         private GridBagLayout layout;
997         private boolean[][] empties;
998
999
1000        static final long serialVersionUID =5113122235848232590L;
1001
1002        GBContainerProxy() {
1003            setLayout(layout = new GridBagLayout());
1004            setBorder(new javax.swing.border.BevelBorder JavaDoc(javax.swing.border.BevelBorder.RAISED));
1005            setOpaque(true);
1006        }
1007
1008        void addAllProxies() {
1009            for (int i = 0; i < gbcProxies.length; i++) {
1010                add(gbcProxies[i], gbcProxies[i].getProxyConstraints());
1011            }
1012            invalidate();
1013            validate();
1014            innerLayoutChanged();
1015            widenEmpty();
1016
1017            if (gbcProxies.length > 0)
1018                select(gbcProxies[0]);
1019        }
1020
1021        void widenEmpty() {
1022
1023            layout.rowHeights = layout.columnWidths = null;
1024
1025            layout.layoutContainer(this);
1026            validate();
1027
1028            int[][] dims = layout.getLayoutDimensions();
1029
1030            empties = new boolean[2][];
1031
1032            int[] widths = new int[ dims[0].length ];
1033            empties[0] = new boolean[ dims[0].length ];
1034
1035            for (int i = 0; i < widths.length; i++) {
1036                // System.out.println("Col [" + i + "] = " + dims[0][i]); // NOI18N
1037
widths[i] = 25;
1038                empties[0][i] = dims[0][i] == 0 ? true : false;
1039            }
1040            layout.columnWidths = widths;
1041
1042
1043            int[] heights = new int[ dims[1].length ];
1044            empties[1] = new boolean[ dims[1].length ];
1045            for (int i = 0; i < heights.length; i++) {
1046                // System.out.println("Rpw [" + i + "] = " + dims[1][i]); // NOI18N
1047
heights[i] = 25;
1048                empties[1][i] = dims[1][i] == 0 ? true : false;
1049            }
1050            layout.rowHeights = heights;
1051
1052            //layout.layoutContainer(this);
1053
}
1054
1055        public void paint(Graphics g) {
1056
1057            if (firstPaint) {
1058                innerLayoutChanged();
1059                firstPaint = false;
1060            }
1061
1062            super.paint(g);
1063
1064            if (gbcProxies.length > 0) {
1065
1066                Color emptyColor = new Color(255, 173, 173);
1067
1068                Point origin = layout.getLayoutOrigin();
1069
1070                int[][] dims = layout.getLayoutDimensions();
1071
1072                int width = 0;
1073                for (int i = 0; i < dims[0].length; i ++) {
1074                    width += dims[0][i];
1075                    //System.out.println("W " + i + " : " + dims[0][i]);
1076
}
1077
1078                int height = 0;
1079                for (int i = 0; i < dims[1].length; i ++) {
1080                    height += dims[1][i];
1081                    // System.out.println("H " + i + " : " + dims[1][i]);
1082
}
1083
1084                //Paint empty rows
1085
int yCoord = origin.y;
1086                g.setColor(emptyColor);
1087                for (int i = 0; i < dims[1].length; i ++) {
1088                    if (empties[1][i]) {
1089                        g.setColor(emptyColor);
1090                        g.fillRect(origin.x, yCoord, width, dims[1][i]);
1091                    }
1092                    yCoord += dims[1][i];
1093                }
1094
1095                //Paint empty columns
1096
int xCoord = origin.x;
1097                for (int i = 0; i < dims[0].length; i ++) {
1098                    if (empties[0][i]) {
1099                        g.setColor(emptyColor);
1100                        g.fillRect(xCoord, origin.y, dims[0][i], height);
1101                    }
1102                    xCoord += dims[0][i];
1103                }
1104
1105
1106                // Paint horizontal lines
1107
g.setColor(Color.black);
1108                yCoord = dims[1][0] + origin.y - 1;
1109                for (int i = 1; i < dims[1].length; i ++) {
1110                    g.drawLine(origin.x, yCoord, origin.x + width - 1, yCoord);
1111                    g.drawLine(origin.x, yCoord + 1, origin.x + width - 1, yCoord + 1);
1112                    yCoord += dims[1][i];
1113                }
1114
1115                // Paint vertical lines
1116
xCoord = dims[0][0] + origin.x - 1;
1117                for (int i = 1; i < dims[0].length; i ++) {
1118                    g.drawLine(xCoord, origin.y, xCoord, origin.y + height - 1);
1119                    g.drawLine(xCoord + 1, origin.y, xCoord + 1, origin.y + height - 1);
1120                    xCoord += dims[0][i];
1121                }
1122            }
1123
1124            paintChildren(g);
1125        }
1126
1127        Point getLayoutLocation(Point p) {
1128            return layout.location(p.x, p.y);
1129        }
1130
1131        /* Updates all proxies */
1132        void updateAllProxies() {
1133
1134            for (int i = 0; i < gbcProxies.length; i++) {
1135                updateProxy(gbcProxies[i]);
1136            }
1137
1138            //invalidate();
1139

1140            innerLayoutChanged();
1141            controlCenter.newSelection(getSelectedProxies());
1142
1143            //designPanel.revalidate();
1144
//designPanel.repaint();
1145
//designLayeredPane.revalidate();
1146
//designPanel.repaint();
1147
}
1148
1149        /* Updates the selected proxy */
1150
1151        void updateProxy(GBComponentProxy p) {
1152            p.updateByComponent();
1153
1154            p.getProxyConstraints();
1155            layout.setConstraints(p, p.getProxyConstraints());
1156
1157            p.invalidate();
1158            //p.innerPanel.invalidate();
1159
p.validate();
1160
1161        }
1162
1163        void invalidateAllProxies() {
1164            for (int i = 0; i < gbcProxies.length; i++) {
1165                gbcProxies[i].invalidate();
1166            }
1167            doLayout();
1168        }
1169
1170
1171        java.util.List JavaDoc getSelectedProxies() {
1172            java.util.List JavaDoc selected = new ArrayList(gbcProxies.length);
1173
1174            for (int i = 0; i < gbcProxies.length; i++) {
1175                if (gbcProxies[i].isSelected)
1176                    selected.add(gbcProxies[i]);
1177            }
1178
1179            return selected;
1180        }
1181
1182        Node[] getSelectedNodes() {
1183            java.util.List JavaDoc selected = getSelectedProxies();
1184
1185            Node[] result = new Node[ selected.size() ];
1186
1187            Iterator it = selected.iterator();
1188
1189            for (int i = 0; it.hasNext(); i++) {
1190                result[i] =((GBComponentProxy)it.next()).getNode();
1191            }
1192
1193            return result;
1194        }
1195
1196
1197        void select(GBComponentProxy p) {
1198            select(p, false);
1199        }
1200
1201        void shiftSelect(GBComponentProxy p) {
1202            select(p, true);
1203        }
1204
1205        void select(GBComponentProxy p, boolean shift) {
1206
1207            java.util.List JavaDoc selected = getSelectedProxies();
1208
1209            if (p.isSelected()) {
1210                if (selected.size() == 1) {
1211                    return;
1212                }
1213                else if (shift) {
1214                    p.setSelected(false);
1215                }
1216                else {
1217                    Iterator it = selected.iterator();
1218                    while (it.hasNext()) {
1219                        ((GBComponentProxy)it.next()).setSelected(false);
1220                    }
1221                    p.setSelected(true);
1222                }
1223
1224            }
1225            else {
1226                if (!shift) {
1227                    Iterator it = selected.iterator();
1228                    while (it.hasNext())
1229                        ((GBComponentProxy)it.next()).setSelected(false);
1230                }
1231                p.setSelected(true);
1232            }
1233
1234            propertySheet.setNodes(getSelectedNodes());
1235            controlCenter.newSelection(getSelectedProxies());
1236        }
1237
1238    }
1239
1240
1241    public static class Window extends JDialog implements Customizer, ActionListener {
1242        final private GridBagCustomizer customizerPanel;
1243        private boolean packCalled;
1244        public Window() {
1245            super(org.openide.windows.WindowManager.getDefault().getMainWindow());
1246            java.util.ResourceBundle JavaDoc bundle = GridBagLayoutSupport.getBundleHack();
1247
1248            setTitle(bundle.getString("CTL_CustomizerTitle")); // NOI18N
1249

1250            getContentPane().setLayout(new BorderLayout(0, 0));
1251            customizerPanel = new GridBagCustomizer();
1252            getContentPane().add(customizerPanel, BorderLayout.CENTER);
1253
1254            JPanel buttonPanel = new JPanel();
1255            buttonPanel.setLayout(new FlowLayout(FlowLayout.RIGHT, 6, 6));
1256
1257            JButton closeButton = new JButton();
1258            closeButton.setText(bundle.getString("CTL_CloseButton")); // NOI18N
1259
closeButton.setActionCommand("close"); // NOI18N
1260
closeButton.addActionListener(this);
1261            buttonPanel.add(closeButton);
1262
1263            JButton helpButton = new JButton();
1264            helpButton.setText(bundle.getString("CTL_HelpButton")); // NOi18N
1265
helpButton.setActionCommand("help"); // NOI18N
1266
helpButton.addActionListener(this);
1267            buttonPanel.add(helpButton);
1268
1269            getContentPane().add(buttonPanel, BorderLayout.SOUTH);
1270
1271            addWindowListener(new WindowAdapter() {
1272                public void windowClosed(WindowEvent e) {
1273                    customizerPanel.customizerClosed();
1274                }
1275            });
1276
1277            setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
1278        }
1279
1280        public void pack() {
1281            if (packCalled)
1282                return;
1283            packCalled = true;
1284
1285            super.pack();
1286            Dimension size = getSize();
1287            Rectangle screenBounds = Utilities.getUsableScreenBounds();
1288            if (size.width > screenBounds.width - 80)
1289                size.width = screenBounds.width * 4 / 5;
1290            if (size.height > screenBounds.height - 80)
1291                size.height = screenBounds.height * 4 / 5;
1292            setBounds(Utilities.findCenterBounds(size));
1293        }
1294
1295        public void setObject(Object JavaDoc bean) {
1296            customizerPanel.setObject(bean);
1297        }
1298
1299        public void actionPerformed(ActionEvent ev) {
1300            if (ev.getActionCommand().equals("close")) // NOI18N
1301
dispose();
1302            else if (ev.getActionCommand().equals("help")) { // NOI18N
1303
Help help = (Help) Lookup.getDefault().lookup(Help.class);
1304                if (help != null)
1305                    help.showHelp(HelpCtx.findHelp(customizerPanel));
1306            }
1307        }
1308    }
1309    
1310
1311}
1312
Popular Tags