KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > form > ComponentLayer


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;
21
22 import java.awt.*;
23 import java.io.PrintWriter JavaDoc;
24 import java.io.StringWriter JavaDoc;
25 import javax.swing.*;
26
27 import org.netbeans.modules.form.fakepeer.*;
28 import org.openide.ErrorManager;
29 import org.openide.util.NbBundle;
30
31 /**
32  * A JPanel subclass holding components presented in FormDesigner (contains
33  * also the resizable border and the white area around). Technically, this is
34  * a layer in FormDesigner, placed under HandleLayer.
35  *
36  * ComponentLayer
37  * +- DesignerPanel
38  * +- FakePeerContainer
39  * +- top visual component of the designed form (by VisualReplicator)
40  * +- subcomponents of the designed form
41  * +- ...
42  *
43  * @author Tomas Pavek
44  */

45
46 class ComponentLayer extends JPanel
47 {
48     private static final int HORIZONTAL_MARGIN = 12;
49     private static final int VERTICAL_MARGIN = 12;
50
51     /** The container holding the top visual component of the form. */
52     private Container componentContainer;
53
54     /** A panel (with a resizable border) positioning the component container
55      * in the whole ComponentLayer area. */

56     private DesignerPanel designerPanel;
57
58     ComponentLayer() {
59         componentContainer = new FakePeerContainer();
60         componentContainer.setLayout(new BorderLayout());
61         componentContainer.setBackground(Color.white);
62         componentContainer.setFont(FakePeerSupport.getDefaultAWTFont());
63
64         designerPanel = new DesignerPanel();
65         designerPanel.setLayout(new BorderLayout());
66         designerPanel.add(componentContainer, BorderLayout.CENTER);
67
68         setLayout(new FlowLayout(FlowLayout.LEFT,
69                                  HORIZONTAL_MARGIN,
70                                  VERTICAL_MARGIN));
71         add(designerPanel);
72
73         updateBackground();
74     }
75
76     Container getComponentContainer() {
77         return componentContainer;
78     }
79
80     Rectangle getDesignerInnerBounds() {
81         Rectangle r = new Rectangle(designerPanel.getDesignerSize());
82         Insets i = designerPanel.getInsets();
83         r.x = HORIZONTAL_MARGIN + i.left;
84         r.y = VERTICAL_MARGIN + i.top;
85         return r;
86     }
87
88     Rectangle getDesignerOuterBounds() {
89         return designerPanel.getBounds();
90     }
91
92     Insets getDesignerOutsets() {
93         return designerPanel.getInsets();
94     }
95
96     Dimension getDesignerSize() {
97         return designerPanel.getDesignerSize();
98     }
99
100     Dimension setDesignerSize(Dimension size) {
101         if (size == null) {
102             size = componentContainer.getComponent(0).getPreferredSize();
103         }
104         if (!size.equals(designerPanel.getDesignerSize())) {
105             designerPanel.setDesignerSize(size);
106         }
107         return size;
108     }
109
110     void setTopDesignComponent(Component component) {
111         if (componentContainer.getComponentCount() > 0)
112             componentContainer.removeAll();
113         componentContainer.add(component, BorderLayout.CENTER);
114     }
115
116     void updateVisualSettings() {
117         updateBackground();
118         designerPanel.updateBorder();
119     }
120
121     private void updateBackground() {
122         setBackground(FormLoaderSettings.getInstance().getFormDesignerBackgroundColor());
123     }
124
125     // ---------
126

127     private static class DesignerPanel extends JPanel {
128         private static int BORDER_THICKNESS = 4; // [could be changeable]
129

130         private Dimension designerSize = new Dimension(400, 300);
131
132         DesignerPanel() {
133             updateBorder();
134         }
135
136         void updateBorder() {
137             setBorder(new javax.swing.border.LineBorder JavaDoc(
138                 FormLoaderSettings.getInstance().getFormDesignerBorderColor(),
139                 BORDER_THICKNESS));
140         }
141
142         Dimension getDesignerSize() {
143             return designerSize;
144         }
145
146         void setDesignerSize(Dimension size) {
147             designerSize = size;
148         }
149
150         public Dimension getPreferredSize() {
151             Dimension size = new Dimension(designerSize);
152             Insets insets = getInsets();
153             size.width += insets.left + insets.right;
154             size.height += insets.top + insets.bottom;
155             return size;
156         }
157
158         public void paint(Graphics g) {
159             try {
160                 FormLAF.setUseDesignerDefaults(true);
161                 super.paint(g);
162             } catch (Exception JavaDoc ex) {
163                 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex);
164                 // Issue 68776
165
String JavaDoc msg = NbBundle.getMessage(ComponentLayer.class, "MSG_Paiting_Exception"); // NOI18N
166
msg = "<html><b>" + msg + "</b><br><br>"; // NOI18N
167
StringWriter JavaDoc sw = new StringWriter JavaDoc();
168                 ex.printStackTrace(new PrintWriter JavaDoc(sw));
169                 msg += sw.toString().replaceAll("\n", "<br>"); // NOI18N
170
Insets insets = getInsets();
171                 JLabel label = new JLabel(msg);
172                 label.setBorder(BorderFactory.createEmptyBorder(insets.top, insets.left, insets.bottom, insets.right));
173                 label.setOpaque(true);
174                 label.setVerticalAlignment(SwingConstants.TOP);
175                 label.setSize(getWidth() - (insets.left + insets.top),
176                     getHeight() - (insets.top + insets.bottom));
177                 Shape oldClip = g.getClip();
178                 Rectangle newClip = new Rectangle(insets.left, insets.top, label.getWidth(), label.getHeight());
179                 Rectangle clipBounds = g.getClipBounds();
180                 if (clipBounds != null) newClip = newClip.intersection(clipBounds);
181                 g.setClip(newClip);
182                 g.translate(insets.left, insets.top);
183                 label.paint(g);
184                 g.translate(-insets.left, -insets.top);
185                 g.setClip(oldClip);
186             } finally {
187                 FormLAF.setUseDesignerDefaults(false);
188             }
189         }
190     }
191 }
192
Popular Tags