001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.form;
043:
044: import java.awt.*;
045: import java.io.PrintWriter;
046: import java.io.StringWriter;
047: import javax.swing.*;
048:
049: import org.netbeans.modules.form.fakepeer.*;
050: import org.openide.ErrorManager;
051: import org.openide.util.NbBundle;
052:
053: /**
054: * A JPanel subclass holding components presented in FormDesigner (contains
055: * also the resizable border and the white area around). Technically, this is
056: * a layer in FormDesigner, placed under HandleLayer.
057: *
058: * ComponentLayer
059: * +- DesignerPanel
060: * +- FakePeerContainer
061: * +- top visual component of the designed form (by VisualReplicator)
062: * +- subcomponents of the designed form
063: * +- ...
064: *
065: * @author Tomas Pavek
066: */
067:
068: class ComponentLayer extends JPanel {
069: private static final int HORIZONTAL_MARGIN = 12;
070: private static final int VERTICAL_MARGIN = 12;
071:
072: /** The container holding the top visual component of the form. */
073: private Container componentContainer;
074:
075: /** A panel (with a resizable border) positioning the component container
076: * in the whole ComponentLayer area. */
077: private DesignerPanel designerPanel;
078:
079: ComponentLayer(FormModel formModel) {
080: componentContainer = new FakePeerContainer();
081: componentContainer.setLayout(new BorderLayout());
082: componentContainer.setBackground(Color.white);
083: componentContainer.setFont(FakePeerSupport.getDefaultAWTFont());
084:
085: designerPanel = new DesignerPanel(formModel);
086: designerPanel.setLayout(new BorderLayout());
087: designerPanel.add(componentContainer, BorderLayout.CENTER);
088:
089: setLayout(new FlowLayout(FlowLayout.LEFT, HORIZONTAL_MARGIN,
090: VERTICAL_MARGIN));
091: add(designerPanel);
092:
093: updateBackground();
094: }
095:
096: Container getComponentContainer() {
097: return componentContainer;
098: }
099:
100: Rectangle getDesignerInnerBounds() {
101: Rectangle r = new Rectangle(designerPanel.getDesignerSize());
102: Insets i = designerPanel.getInsets();
103: r.x = HORIZONTAL_MARGIN + i.left;
104: r.y = VERTICAL_MARGIN + i.top;
105: return r;
106: }
107:
108: Rectangle getDesignerOuterBounds() {
109: return designerPanel.getBounds();
110: }
111:
112: Insets getDesignerOutsets() {
113: return designerPanel.getInsets();
114: }
115:
116: Dimension getDesignerSize() {
117: return designerPanel.getDesignerSize();
118: }
119:
120: Dimension setDesignerSize(Dimension size) {
121: if (size == null) {
122: size = componentContainer.getComponent(0)
123: .getPreferredSize();
124: }
125: if (!size.equals(designerPanel.getDesignerSize())) {
126: designerPanel.setDesignerSize(size);
127: }
128: return size;
129: }
130:
131: void setTopDesignComponent(Component component) {
132: if (componentContainer.getComponentCount() > 0)
133: componentContainer.removeAll();
134: componentContainer.add(component, BorderLayout.CENTER);
135: }
136:
137: void updateVisualSettings() {
138: updateBackground();
139: designerPanel.updateBorder();
140: }
141:
142: private void updateBackground() {
143: setBackground(FormLoaderSettings.getInstance()
144: .getFormDesignerBackgroundColor());
145: }
146:
147: // ---------
148:
149: private static class DesignerPanel extends JPanel {
150: private static int BORDER_THICKNESS = 4; // [could be changeable]
151:
152: private Dimension designerSize = new Dimension(400, 300);
153: private FormModel formModel;
154:
155: DesignerPanel(FormModel formModel) {
156: this .formModel = formModel;
157: updateBorder();
158: }
159:
160: void updateBorder() {
161: setBorder(new javax.swing.border.LineBorder(
162: FormLoaderSettings.getInstance()
163: .getFormDesignerBorderColor(),
164: BORDER_THICKNESS));
165: }
166:
167: Dimension getDesignerSize() {
168: return designerSize;
169: }
170:
171: void setDesignerSize(Dimension size) {
172: designerSize = size;
173: }
174:
175: @Override
176: public Dimension getPreferredSize() {
177: Dimension size = new Dimension(designerSize);
178: Insets insets = getInsets();
179: size.width += insets.left + insets.right;
180: size.height += insets.top + insets.bottom;
181: return size;
182: }
183:
184: @Override
185: public void paint(Graphics g) {
186: try {
187: FormLAF.setUseDesignerDefaults(formModel);
188: super .paint(g);
189: } catch (Exception ex) {
190: ErrorManager.getDefault().notify(
191: ErrorManager.INFORMATIONAL, ex);
192: // Issue 68776
193: String msg = NbBundle.getMessage(ComponentLayer.class,
194: "MSG_Paiting_Exception"); // NOI18N
195: msg = "<html><b>" + msg + "</b><br><br>"; // NOI18N
196: StringWriter sw = new StringWriter();
197: ex.printStackTrace(new PrintWriter(sw));
198: msg += sw.toString().replaceAll("\n", "<br>"); // NOI18N
199: Insets insets = getInsets();
200: JLabel label = new JLabel(msg);
201: label.setBorder(BorderFactory.createEmptyBorder(
202: insets.top, insets.left, insets.bottom,
203: insets.right));
204: label.setOpaque(true);
205: label.setVerticalAlignment(SwingConstants.TOP);
206: label.setSize(getWidth() - (insets.left + insets.top),
207: getHeight() - (insets.top + insets.bottom));
208: Shape oldClip = g.getClip();
209: Rectangle newClip = new Rectangle(insets.left,
210: insets.top, label.getWidth(), label.getHeight());
211: Rectangle clipBounds = g.getClipBounds();
212: if (clipBounds != null)
213: newClip = newClip.intersection(clipBounds);
214: g.setClip(newClip);
215: g.translate(insets.left, insets.top);
216: label.paint(g);
217: g.translate(-insets.left, -insets.top);
218: g.setClip(oldClip);
219: } finally {
220: FormLAF.setUseDesignerDefaults(null);
221: }
222: }
223: }
224: }
|