1 19 20 package org.netbeans.modules.form; 21 22 import java.awt.*; 23 import java.io.PrintWriter ; 24 import java.io.StringWriter ; 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 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 52 private Container componentContainer; 53 54 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 127 private static class DesignerPanel extends JPanel { 128 private static int BORDER_THICKNESS = 4; 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 ( 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 ex) { 163 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex); 164 String msg = NbBundle.getMessage(ComponentLayer.class, "MSG_Paiting_Exception"); msg = "<html><b>" + msg + "</b><br><br>"; StringWriter sw = new StringWriter (); 168 ex.printStackTrace(new PrintWriter (sw)); 169 msg += sw.toString().replaceAll("\n", "<br>"); 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 |