Source Code Cross Referenced for FormComponentBox.java in  » IDE-Netbeans » visualweb.api.designer » org » netbeans » modules » visualweb » css2 » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » IDE Netbeans » visualweb.api.designer » org.netbeans.modules.visualweb.css2 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


0001:        /*
0002:         * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
0003:         *
0004:         * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
0005:         *
0006:         * The contents of this file are subject to the terms of either the GNU
0007:         * General Public License Version 2 only ("GPL") or the Common
0008:         * Development and Distribution License("CDDL") (collectively, the
0009:         * "License"). You may not use this file except in compliance with the
0010:         * License. You can obtain a copy of the License at
0011:         * http://www.netbeans.org/cddl-gplv2.html
0012:         * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
0013:         * specific language governing permissions and limitations under the
0014:         * License.  When distributing the software, include this License Header
0015:         * Notice in each file and include the License file at
0016:         * nbbuild/licenses/CDDL-GPL-2-CP.  Sun designates this
0017:         * particular file as subject to the "Classpath" exception as provided
0018:         * by Sun in the GPL Version 2 section of the License file that
0019:         * accompanied this code. If applicable, add the following below the
0020:         * License Header, with the fields enclosed by brackets [] replaced by
0021:         * your own identifying information:
0022:         * "Portions Copyrighted [year] [name of copyright owner]"
0023:         *
0024:         * Contributor(s):
0025:         *
0026:         * The Original Software is NetBeans. The Initial Developer of the Original
0027:         * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
0028:         * Microsystems, Inc. All Rights Reserved.
0029:         *
0030:         * If you wish your version of this file to be governed by only the CDDL
0031:         * or only the GPL Version 2, indicate your decision by adding
0032:         * "[Contributor] elects to include this software in this distribution
0033:         * under the [CDDL or GPL Version 2] license." If you do not indicate a
0034:         * single choice of license, a recipient has the option to distribute
0035:         * your version of this file under either the CDDL, the GPL Version 2 or
0036:         * to extend the choice of license to its licensees as provided above.
0037:         * However, if you add GPL Version 2 code and therefore, elected the GPL
0038:         * Version 2 license, then the option applies only if the new code is
0039:         * made subject to such option by the copyright holder.
0040:         */
0041:        package org.netbeans.modules.visualweb.css2;
0042:
0043:        import org.netbeans.modules.visualweb.api.designer.cssengine.CssProvider;
0044:        import org.netbeans.modules.visualweb.api.designer.markup.MarkupService;
0045:        import org.netbeans.modules.visualweb.designer.CssUtilities;
0046:        import org.netbeans.modules.visualweb.designer.ImageCache;
0047:        import java.awt.Color;
0048:        import java.awt.Component;
0049:        import java.awt.Container;
0050:        import java.awt.Dimension;
0051:        import java.awt.Font;
0052:        import java.awt.FontMetrics;
0053:        import java.awt.Graphics;
0054:        import java.awt.Graphics2D;
0055:        import java.awt.Image;
0056:        import java.awt.Insets;
0057:        import java.awt.geom.AffineTransform;
0058:        import java.awt.image.BufferedImage;
0059:        import java.net.URL;
0060:        import java.util.ArrayList;
0061:        import java.util.List;
0062:        import java.util.Vector;
0063:        import javax.swing.AbstractButton;
0064:
0065:        import javax.swing.CellRendererPane;
0066:        import javax.swing.ComboBoxModel;
0067:        import javax.swing.DefaultComboBoxModel;
0068:        import javax.swing.ImageIcon;
0069:        import javax.swing.JButton;
0070:        import javax.swing.JCheckBox;
0071:        import javax.swing.JComboBox;
0072:        import javax.swing.JComponent;
0073:        import javax.swing.JList;
0074:        import javax.swing.JPasswordField;
0075:        import javax.swing.JRadioButton;
0076:        import javax.swing.JScrollPane;
0077:        import javax.swing.JTextArea;
0078:        import javax.swing.JTextField;
0079:        import javax.swing.JViewport;
0080:        import javax.swing.ListModel;
0081:        import javax.swing.LookAndFeel;
0082:        import javax.swing.SwingConstants;
0083:        import javax.swing.UIDefaults;
0084:        import javax.swing.UIManager;
0085:        import javax.swing.plaf.ButtonUI;
0086:        import javax.swing.plaf.ComponentUI;
0087:        import javax.swing.plaf.ScrollPaneUI;
0088:        import javax.swing.plaf.TextUI;
0089:        import javax.swing.text.Document;
0090:        import javax.swing.text.JTextComponent;
0091:        import javax.swing.text.PlainDocument;
0092:
0093:        import org.openide.ErrorManager;
0094:        import org.openide.util.NbBundle;
0095:        import org.openide.util.Utilities;
0096:        import org.w3c.dom.Attr;
0097:        import org.w3c.dom.Element;
0098:        import org.w3c.dom.Node;
0099:        import org.w3c.dom.NodeList;
0100:
0101:        import org.netbeans.modules.visualweb.designer.DesignerUtils;
0102:        import org.netbeans.modules.visualweb.designer.WebForm;
0103:        import org.netbeans.modules.visualweb.api.designer.cssengine.XhtmlCss;
0104:        import org.netbeans.modules.visualweb.designer.html.HtmlAttribute;
0105:        import org.netbeans.modules.visualweb.designer.html.HtmlTag;
0106:        import org.w3c.dom.Text;
0107:
0108:        /**
0109:         * Box that visually represents a form component - such as a button,
0110:         * dropdown, checkbox, text area, or listbox.
0111:         *
0112:         * <p>
0113:         * Portions of this code taken from javax.swing.text.html.FormView
0114:         * <p>
0115:         * @todo "checked" is not supported: 'the value "radio" or "checkbox",
0116:         * this boolean attribute specifies that the button is on.'
0117:         * @todo the "size" attribute should be supported: it sets the pixel
0118:         * width - unless it's a password field and text field in which case
0119:         * it refers to text field.
0120:         * @todo Support the "button" element; it's similar to input type="submit",
0121:         * except the button label comes from the text node children of the
0122:         * button instead of from an attribute.
0123:         * @todo Fonts
0124:         * @todo Background painting for for example radios/checkboxes:
0125:         *
0126:         * @author Timothy Prinzing
0127:         * @author Sunita Mani
0128:         * @author Tor Norbye
0129:         */
0130:        public class FormComponentBox extends ContainerBox {
0131:            /** Show Mac-style buttons (on the mac) instead of firefox/cross platform style buttons? */
0132:            private static final boolean MAC_STYLE_BUTTONS = System
0133:                    .getProperty("rave.macButtons") != null; // NOI18N
0134:            private static boolean sCreateChildren; // static used before object create
0135:
0136:            //    /** UIDefaults to use when creating native components */
0137:            //    private static UIDefaults uidef = null;
0138:            private JComponent createdC;
0139:            private boolean nonrectangular;
0140:            private boolean createChildren;
0141:
0142:            //private static int id = 0;
0143:            private Image image = null;
0144:            private AffineTransform transform = new AffineTransform();
0145:            private boolean attempted = false;
0146:            private int imageWidth = -1;
0147:            private int imageHeight = -1;
0148:            //    private boolean focus = false;
0149:            private int baseline;
0150:
0151:            private FormComponentBox(WebForm webform, Element element,
0152:                    JComponent createdC, BoxType boxType, boolean inline,
0153:                    boolean replaced, boolean createChildren) {
0154:                super (webform, element, boxType, inline, replaced);
0155:                this .createdC = createdC;
0156:                this .nonrectangular = !createdC.isOpaque();
0157:                this .createChildren = createChildren;
0158:                //        focus = DesignerActions.isFocus(getDesignBean());
0159:            }
0160:
0161:            /**
0162:             *  Create a FormComponentBox representing the given element's
0163:             *  component. May return null if it's an invisible component
0164:             *  (e.g. input hidden).
0165:             *
0166:             * @param element The element this inline box is associated with
0167:             * @param x The x position where the inline box should be rendered
0168:             *        relative to the containing block.
0169:             * @param y The y position where the inline box should be rendered
0170:             *        relative to the containing block.
0171:             * @param width The width of this inline box.
0172:             * @param height The height of this inline box.
0173:             */
0174:            public static CssBox getBox(WebForm webform, Element element,
0175:                    HtmlTag tag, BoxType boxType, boolean inline,
0176:                    boolean replaced) {
0177:                // <input type="image"/> special case: don't use a FormComponentBox,
0178:                // use an image box instead
0179:                if ((tag == HtmlTag.INPUT) || (tag == HtmlTag.BUTTON)) {
0180:                    String type = element.getAttribute(HtmlAttribute.TYPE);
0181:
0182:                    if ("image".equals(type)) { // NOI18N
0183:
0184:                        Container container = webform.getPane();
0185:
0186:                        // This works because the image button is also supposed to
0187:                        // use the "src" and "alt" attributes to locate image and
0188:                        // alternate text for the button
0189:                        return ImageBox.getImageBox(webform, element,
0190:                                container, boxType, inline);
0191:                    } else if (("submit".equals(type) || "button".equals(type) || "reset"
0192:                            .equals(type))
0193:                            &&
0194:                            // No gradient buttons on the mac - Safari doesn't do it, probably
0195:                            // intentionally
0196:                            (!MAC_STYLE_BUTTONS || (Utilities
0197:                                    .getOperatingSystem() != Utilities.OS_MAC))) {
0198:                        // See if we need to create a custom-painted button. This is
0199:                        // the case if the user specifies a background-image (normal
0200:                        // background-color we can handle in the normal button code.)
0201:                        // TODO - decide if we need to use custom button painting code
0202:                        // for unusual borders too.
0203:                        //                ImageIcon bgImage = BackgroundImagePainter.getBackgroundImage(webform, element);
0204:                        //                URL imageUrl = CssBoxUtilities.getBackgroundImageUrl(element, webform.getMarkup().getBase());
0205:                        //                URL imageUrl = CssProvider.getEngineService().getBackgroundImageUrlForElement(element, webform.getMarkup().getBase());
0206:                        URL imageUrl = CssProvider.getEngineService()
0207:                                .getBackgroundImageUrlForElement(element,
0208:                                        webform.getBaseUrl());
0209:                        ImageIcon bgImage;
0210:                        if (imageUrl != null) {
0211:                            // XXX Revise this caching impl.
0212:                            //                    ImageCache imageCache = webform.getDocument().getImageCache();
0213:                            ImageCache imageCache = webform.getImageCache();
0214:                            bgImage = imageCache.get(imageUrl);
0215:                            if (bgImage == null) {
0216:                                bgImage = new ImageIcon(imageUrl);
0217:                                imageCache.put(imageUrl, bgImage);
0218:                            }
0219:                        } else {
0220:                            bgImage = null;
0221:                        }
0222:
0223:                        if (bgImage != null) {
0224:                            // XXX TODO - provide a facility to pass in the background image?
0225:                            CssBorder border = CssBorder.getBorder(element, 2,
0226:                                    CssBorder.STYLE_OUTSET,
0227:                                    CssBorder.FRAME_UNSET);
0228:                            String label = getButtonLabel(element, type);
0229:
0230:                            return new CustomButtonBox(webform, element,
0231:                                    boxType, label, border, inline);
0232:                        }
0233:                    }
0234:                }
0235:
0236:                // The following method will also as a side effect set the
0237:                // sCreateChildren flag which we'll use to seed the
0238:                // "createChildren" flag for the box we create
0239:                JComponent proxyComp = createComponent(tag, webform, element);
0240:
0241:                if (proxyComp == null) { // invisible component
0242:                    return null;
0243:                }
0244:
0245:                return new FormComponentBox(webform, element, proxyComp,
0246:                        boxType, inline, replaced, sCreateChildren);
0247:            }
0248:
0249:            protected void initialize() {
0250:                super .initialize(); // hmmm this gets done repeatedly... during layout (see LineBoxGroup's relayout super call)
0251:
0252:                if (createdC != null) { // might be null for inputHidden for example
0253:
0254:                    JComponent c = createdC;
0255:
0256:                    // Make background color visible. 
0257:                    // This may not have the desired effect on for example textfields. Check!
0258:                    //if (bg != null) {
0259:                    //    c.setOpaque(false);
0260:                    //}
0261:                    // Set dimensions and colors according to CSS
0262:                    // XXX This doesn't -quite- work; somehow the component may end
0263:                    // up a bit smaller than I ask it to be - I think the borders
0264:                    // are getting subtracted or something like that.
0265:                    Element fel = getElement();
0266:
0267:                    //            int width = CssLookup.getLength(fel, XhtmlCss.WIDTH_INDEX);
0268:                    int width = CssUtilities.getCssLength(fel,
0269:                            XhtmlCss.WIDTH_INDEX);
0270:
0271:                    if ((width != AUTO) && (c instanceof  JTextField)) {
0272:                        // If a width is defined for the text field,
0273:                        // wed need to reset the columns property since
0274:                        // JTextField.getPreferredSize overrides the
0275:                        // preferred setting if columns != 0
0276:                        ((JTextField) c).setColumns(0);
0277:                    }
0278:
0279:                    //            int height = CssLookup.getLength(fel, XhtmlCss.HEIGHT_INDEX);
0280:                    int height = CssUtilities.getCssLength(fel,
0281:                            XhtmlCss.HEIGHT_INDEX);
0282:
0283:                    if ((width != AUTO) || (height != AUTO)) {
0284:                        if (width == AUTO) {
0285:                            width = (int) c.getPreferredSize().getWidth();
0286:                        } else if (height == AUTO) {
0287:                            height = (int) c.getPreferredSize().getHeight();
0288:                        }
0289:
0290:                        c.setPreferredSize(new Dimension(width, height));
0291:                    }
0292:
0293:                    // For some components we wrap the real component inside a JScrollPane;
0294:                    // JTextArea is one example. However, fonts and colors should be set on 
0295:                    // the JTextArea itself, not the JScrollPane
0296:                    if (c instanceof  JScrollPane) {
0297:                        c = (JComponent) ((JScrollPane) c).getViewport()
0298:                                .getView();
0299:                    }
0300:
0301:                    // Colors and fonts should only be set if they have been set
0302:                    // in CSS -on- this element, NOT inherited (even though color
0303:                    // is an inherited property).  This appears to be against
0304:                    // what the CSS spec says, but browsers do quite a few weird
0305:                    // things with the form input components
0306:                    Element element = getElement();
0307:                    //            XhtmlCssEngine engine = CssLookup.getCssEngine(element);
0308:
0309:                    //            if (!engine.isInheritedValue((RaveElement)element, XhtmlCss.COLOR_INDEX)) {
0310:                    if (!CssProvider.getEngineService()
0311:                            .isInheritedStyleValueForElement(element,
0312:                                    XhtmlCss.COLOR_INDEX)) {
0313:                        //                Color color = CssLookup.getColor(element, XhtmlCss.COLOR_INDEX);
0314:                        Color color = CssProvider.getValueService()
0315:                                .getColorForElement(element,
0316:                                        XhtmlCss.COLOR_INDEX);
0317:
0318:                        if (color != null) {
0319:                            c.setForeground(color);
0320:                        }
0321:                    }
0322:
0323:                    //bg = Css.getColor(element, XhtmlCssEngine.BACKGROUND_COLOR_INDEX); 
0324:                    // We've already looked up the background in the box constructor
0325:                    if (bg != null) {
0326:                        c.setBackground(bg);
0327:                    }
0328:
0329:                    // XXX should I check to see if a particular font is defined first?
0330:                    // Or I guess one always would be through style cascade/inheritance?
0331:                    //            if (!engine.isInheritedValue((RaveElement)element, XhtmlCss.FONT_SIZE_INDEX)) {
0332:                    if (!CssProvider.getEngineService()
0333:                            .isInheritedStyleValueForElement(element,
0334:                                    XhtmlCss.FONT_SIZE_INDEX)) {
0335:                        // check font-family, font-variant etc. too
0336:                        //                Font font = CssLookup.getFont(fel, DesignerSettings.getInstance().getDefaultFontSize());
0337:                        //                Font font = CssProvider.getValueService().getFontForElement(fel, DesignerSettings.getInstance().getDefaultFontSize(), Font.PLAIN);
0338:                        Font font = CssUtilities.getDesignerFontForElement(fel,
0339:                                getTextFromComponent(c), webform
0340:                                        .getDefaultFontSize());
0341:                        setFontForComponent(c, font);
0342:                    }
0343:                }
0344:
0345:                if (createdC != null) {
0346:                    getImage(); // initialize sizes
0347:                    contentWidth = width = getWidth();
0348:                    contentHeight = height = getHeight();
0349:                    baseline = height;
0350:
0351:                    Font font = createdC.getFont();
0352:
0353:                    // XXX #109310 The baseline is at the bottom.
0354:                    //            // Set the baseline alignment for all form components except Text Areas
0355:                    //            // (these are not baseline aligned)
0356:                    //            if ((tag != HtmlTag.TEXTAREA) && (font != null)) {
0357:                    //                // This returns 0 for some reason:
0358:                    //                //baseline = font.getBaselineFor('j');
0359:                    ////                FontMetrics metrics = Toolkit.getDefaultToolkit().getFontMetrics(font);
0360:                    //                FontMetrics metrics = DesignerUtils.getFontMetrics(font);
0361:                    //                baseline = metrics.getHeight() - metrics.getDescent();
0362:                    //
0363:                    //                // Here I wanted to look up the component's insets, and border insets
0364:                    //                // and add in the top offsets - but that doesn't work right. Various
0365:                    //                // look and feels are returning strange values here, so components end
0366:                    //                // up getting aligned in a weird way. Therefore, instead I just use 
0367:                    //                // half leading:
0368:                    //                baseline += ((height - baseline) / 2);
0369:                    //            }
0370:                }
0371:            }
0372:
0373:            private static void setFontForComponent(JComponent component,
0374:                    Font font) {
0375:                if (component instanceof  JScrollPane) {
0376:                    JScrollPane scrollPane = (JScrollPane) component;
0377:                    JViewport viewport = scrollPane.getViewport();
0378:                    if (viewport != null) {
0379:                        Component comp = viewport.getView();
0380:                        if (comp != null) {
0381:                            comp.setFont(font);
0382:                        }
0383:                    }
0384:                } else {
0385:                    component.setFont(font);
0386:                }
0387:            }
0388:
0389:            /** Tries to retrieve the text to be shown from the component.
0390:             * It checks AbstractButton, JTextComponent, JList and content of JScrollPane. */
0391:            private static String getTextFromComponent(Component component) {
0392:                if (component instanceof  AbstractButton) {
0393:                    return ((AbstractButton) component).getText();
0394:                } else if (component instanceof  JTextComponent) {
0395:                    return ((JTextComponent) component).getText();
0396:                } else if (component instanceof  JComboBox) {
0397:                    JComboBox comboBox = (JComboBox) component;
0398:                    ComboBoxModel comboBoxModel = comboBox.getModel();
0399:                    List<String> strings = new ArrayList<String>();
0400:                    for (int i = 0; i < comboBoxModel.getSize(); i++) {
0401:                        Object item = comboBoxModel.getElementAt(i);
0402:                        if (item != null) {
0403:                            // XXX Relying upon the items are strings.
0404:                            strings.add(item.toString());
0405:                        }
0406:                    }
0407:                    StringBuilder sb = new StringBuilder();
0408:                    for (String string : strings) {
0409:                        sb.append(string);
0410:                        sb.append('\n'); // NOI18N
0411:                    }
0412:                    return sb.toString();
0413:                } else if (component instanceof  JList) {
0414:                    JList list = (JList) component;
0415:                    ListModel listModel = list.getModel();
0416:                    List<String> strings = new ArrayList<String>();
0417:                    for (int i = 0; i < listModel.getSize(); i++) {
0418:                        Object item = listModel.getElementAt(i);
0419:                        if (item != null) {
0420:                            // XXX Relying upon the items are strings.
0421:                            strings.add(item.toString());
0422:                        }
0423:                    }
0424:                    StringBuilder sb = new StringBuilder();
0425:                    for (String string : strings) {
0426:                        sb.append(string);
0427:                        sb.append('\n'); // NOI18N
0428:                    }
0429:                    return sb.toString();
0430:                } else if (component instanceof  JScrollPane) {
0431:                    JScrollPane scrollPane = (JScrollPane) component;
0432:                    JViewport viewport = scrollPane.getViewport();
0433:                    if (viewport == null) {
0434:                        return null;
0435:                    }
0436:                    return getTextFromComponent(viewport.getView());
0437:                }
0438:                return null;
0439:            }
0440:
0441:            public boolean isBorderSizeIncluded() {
0442:                return true;
0443:            }
0444:
0445:            public Insets getCssSizeInsets() {
0446:                return new Insets(0, 0, 0, 0);
0447:            }
0448:
0449:            /**
0450:             * Used to indicate if the maximum span should be the same as the
0451:             * preferred span. This is used so that the Component's size doesn't
0452:             * change if there is extra room on a line. The first bit is used for
0453:             * the X direction, and the second for the y direction.
0454:             */
0455:
0456:            //private short maxIsPreferred;
0457:            /**
0458:             * COPIED FROM javax.swing.text.html.FormView: then modified for
0459:             * Rave purposes.... (for example, no live models)
0460:             * <p>
0461:             * Create the component.  This is basically a
0462:             * big switch statement based upon the tag type
0463:             * and html attributes of the associated element.
0464:             */
0465:            private static JComponent createComponent(HtmlTag tag,
0466:                    WebForm webform, Element element) {
0467:                /*
0468:                type = text|password|checkbox|radio|submit|reset|file|hidden|image|button [CI]
0469:                type = text|password|checkbox|radio|submit|reset|file|hidden|image|button [CI]
0470:                This attribute specifies the type of control to create. The default value for this attribute is "text".
0471:                name = cdata [CI]
0472:                This attribute assigns the control name.
0473:                value = cdata [CA]
0474:                This attribute specifies the initial value of the control. It is optional except when the type attribute has the value "radio" or "checkbox".
0475:                size = cdata [CN]
0476:                This attribute tells the user agent the initial width of the control. The width is given in pixels except when type attribute has the value "text" or "password". In that case, its value refers to the (integer) number of characters.
0477:                maxlength = number [CN]
0478:                When the type attribute has the value "text" or "password", this attribute specifies the maximum number of characters the user may enter. This number may exceed the specified size, in which case the user agent should offer a scrolling mechanism. The default value for this attribute is an unlimited number.
0479:                checked [CI]
0480:                When the type attribute has the value "radio" or "checkbox", this boolean attribute specifies that the button is on. User agents must ignore this attribute for other control types.
0481:                src = uri [CT]
0482:                When the type attribute has the value "image", this attribute specifies the location of the image to be used to decorate the graphical submit button.
0483:
0484:                text
0485:                Creates a single-line text input control.
0486:                password
0487:                Like "text", but the input text is rendered in such a way as to hide the characters (e.g., a series of asterisks). This control type is often used for sensitive input such as passwords. Note that the current value is the text entered by the user, not the text rendered by the user agent.
0488:
0489:                Note. Application designers should note that this mechanism affords only light security protection. Although the password is masked by user agents from casual observers, it is transmitted to the server in clear text, and may be read by anyone with low-level access to the network.
0490:                checkbox
0491:                Creates a checkbox.
0492:                radio
0493:                Creates a radio button.
0494:                submit
0495:                Creates a submit button.
0496:                image
0497:                Creates a graphical submit button. The value of the src attribute specifies the URI of the image that will decorate the button. For accessibility reasons, authors should provide alternate text for the image via the alt attribute.
0498:
0499:                When a pointing device is used to click on the image, the form is submitted and the click coordinates passed to the server. The x value is measured in pixels from the left of the image, and the y value in pixels from the top of the image. The submitted data includes name.x=x-value and name.y=y-value where "name" is the value of the name attribute, and x-value and y-value are the x and y coordinate values, respectively.
0500:
0501:                If the server takes different actions depending on the location clicked, users of non-graphical browsers will be disadvantaged. For this reason, authors should consider alternate approaches:
0502:
0503:                 * Use multiple submit buttons (each with its own image) in place of a single graphical submit button. Authors may use style sheets to control the positioning of these buttons.
0504:                 * Use a client-side image map together with scripting.
0505:
0506:                reset
0507:                Creates a reset button.
0508:                button
0509:                Creates a push button. User agents should use the value of the value attribute as the button's label.
0510:                hidden
0511:                Creates a hidden control.
0512:                file
0513:                Creates a file select control. User agents may use the value of the value attribute as the initial file name.
0514:
0515:
0516:
0517:                 */
0518:                Element el = element;
0519:                JComponent c = null;
0520:
0521:                if ((tag == HtmlTag.INPUT) || (tag == HtmlTag.BUTTON)) {
0522:                    c = createInputComponent(tag, el);
0523:                } else if (tag == HtmlTag.SELECT) {
0524:                    // <select> cannot have children. It has <option>
0525:                    // children that should go into the list, but we process
0526:                    // those separately.
0527:                    sCreateChildren = false;
0528:
0529:                    int size = HtmlAttribute.getIntegerAttributeValue(el,
0530:                            HtmlAttribute.SIZE, 1);
0531:                    boolean multiple = el.hasAttribute(HtmlAttribute.MULTIPLE);
0532:
0533:                    if ((size > 1) || multiple) {
0534:                        Vector<String> v = new Vector<String>();
0535:                        int[] selected = populateOptions(webform, element, v);
0536:
0537:                        JList list;
0538:                        if (v.size() == 0) {
0539:                            // XXX #6195204 Faking default component.
0540:                            if (size == 1) {
0541:                                // XXX No scrollbar will be shown, so make the comp wider.
0542:                                v.addElement("    " + "    "); // NOI18N // XXX Fake width.
0543:                            } else {
0544:                                v.addElement("    "); // NOI18N // XXX Fake width.
0545:                            }
0546:                            // XXX Why DefaultComboBoxModel?
0547:                            list = new JList(new DefaultComboBoxModel(v));
0548:                            list.setVisibleRowCount(size > 0 ? size : 2);
0549:                        } else {
0550:                            // XXX Why DefaultComboBoxModel?
0551:                            list = new JList(new DefaultComboBoxModel(v));
0552:                            list.setVisibleRowCount(size);
0553:                        }
0554:
0555:                        if ((selected != null) && (selected.length > 0)) {
0556:                            list.setSelectedIndices(selected);
0557:                        }
0558:
0559:                        // List should not respond to selection!
0560:                        //list.setSelectionModel((ListSelectionModel)model);
0561:                        JScrollPane jp = new JScrollPane(list);
0562:                        if (list.getVisibleRowCount() > 1) {
0563:                            // XXX Show scrollbars when more than one row available.
0564:                            jp
0565:                                    .setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
0566:                        }
0567:
0568:                        c = jp;
0569:
0570:                        // XXX #110555 Java app can't mix two or more L&F's at the same time.
0571:                        //                ScrollPaneUI ui = (ScrollPaneUI)createNativeUI(c);
0572:                        //
0573:                        //                if (ui != null) {
0574:                        //                    jp.setUI(ui);
0575:                        //                }
0576:                    } else {
0577:                        Vector<String> v = new Vector<String>();
0578:                        int[] selected = populateOptions(webform, element, v);
0579:
0580:                        JComboBox jc;
0581:                        if (v.size() == 0) {
0582:                            // XXX #6195204 Faking default component.
0583:                            v.addElement("    "); // NOI18N // XXX Fake width.
0584:                            // XXX Why DefaultComboBoxModel?
0585:                            jc = new JComboBox(new DefaultComboBoxModel(v));
0586:                            jc.setEditable(false);
0587:                        } else {
0588:                            // XXX Why DefaultComboBoxModel?
0589:                            jc = new JComboBox(new DefaultComboBoxModel(v));
0590:                            jc.setEditable(false);
0591:                        }
0592:
0593:                        c = jc;
0594:
0595:                        if ((selected != null) && (selected.length > 0)) {
0596:                            jc.setSelectedIndex(selected[0]);
0597:                        }
0598:
0599:                        //maxIsPreferred = 3;
0600:
0601:                        /*
0602:                        // J1 HACK: Make sure the combo box is roughly the same
0603:                        // size as in the browser. Screenshots from 1280x1024 and
0604:                        // 1400x1050 resolutions showed the width of the combo
0605:                        // to be roughly 140-145 pixels.
0606:                        Dimension pfz = c.getPreferredSize();
0607:                        if (c.getWidth() < 142) {
0608:                            pfz.setSize(142, pfz.getHeight());
0609:                            c.setPreferredSize(pfz);
0610:                        }
0611:                         */
0612:                    }
0613:                } else if (tag == HtmlTag.TEXTAREA) {
0614:                    // <textarea> cannot have children. It has content
0615:                    // text that should go into the text area, but we that
0616:                    // separately.
0617:                    sCreateChildren = false;
0618:
0619:                    PlainDocument sdoc = new PlainDocument();
0620:                    StringBuffer sb = new StringBuffer();
0621:                    populateText(element, sb);
0622:
0623:                    if (sb.length() == 0) {
0624:                        sb.append("          "); // ensure some width
0625:                    }
0626:
0627:                    try {
0628:                        sdoc.insertString(0, sb.toString(), null);
0629:                    } catch (javax.swing.text.BadLocationException e) {
0630:                        ErrorManager.getDefault().notify(
0631:                                ErrorManager.INFORMATIONAL, e);
0632:                    }
0633:
0634:                    JTextArea area = new JTextAreaColumnWidth(sdoc);
0635:
0636:                    int rows = HtmlAttribute.getIntegerAttributeValue(el,
0637:                            HtmlAttribute.ROWS, 3);
0638:                    area.setRows(rows);
0639:
0640:                    int cols = HtmlAttribute.getIntegerAttributeValue(el,
0641:                            HtmlAttribute.COLS, 22);
0642:
0643:                    //maxIsPreferred = 3;
0644:                    // or check CSS attribute first!
0645:                    area.setColumns(cols);
0646:
0647:                    // XXX #110555 Java app can't mix two or more L&F's at the same time.
0648:                    //            TextUI tui = (TextUI)createNativeUI(area);
0649:                    //
0650:                    //            if (tui != null) {
0651:                    //                area.setUI(tui);
0652:                    //            }
0653:
0654:                    // On Windows, show a vertical scrollbar by default, since
0655:                    // that's what IE does. Mozilla does not so on other platforms
0656:                    // we'll leave out the vertical scrollbar.
0657:                    int vertical;
0658:
0659:                    if (org.openide.util.Utilities.isWindows()) {
0660:                        vertical = JScrollPane.VERTICAL_SCROLLBAR_ALWAYS;
0661:                    } else {
0662:                        vertical = JScrollPane.VERTICAL_SCROLLBAR_NEVER;
0663:                    }
0664:
0665:                    JScrollPane jc = new JScrollPane(area,
0666:                    // Even if vertical == VERTICAL_SCROLLBAR_NEVER,
0667:                            // we use a scrollpane because we want the 
0668:                            // inset border it provides
0669:                            vertical, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
0670:                    c = jc;
0671:
0672:                    // XXX #110555 Java app can't mix two or more L&F's at the same time.
0673:                    //            ScrollPaneUI ui = (ScrollPaneUI)createNativeUI(c);
0674:                    //
0675:                    //            if (ui != null) {
0676:                    //                jc.setUI(ui);
0677:                    //            }
0678:                }
0679:
0680:                if (c != null) {
0681:                    c.setAlignmentY(1.0f);
0682:                }
0683:
0684:                // c might be null for a <input type="hidden"/> for example...
0685:                return c;
0686:            }
0687:
0688:            /** Find all the <option> children of this tag, and add the
0689:             * text content below each <option> as a separate item
0690:             * of the given Vector. (After whitespace is stripped, of course.)
0691:             * Return the set of selected indices.
0692:             */
0693:            public static int[] populateOptions(WebForm webForm,
0694:                    Element element, Vector<String> v) {
0695:                // <markup_separation>
0696:                //        MarkupService markupService = MarkupServiceProvider.getDefault();
0697:                // </markup_separation>
0698:                List<Integer> selected = new ArrayList<Integer>();
0699:                NodeList list = element.getChildNodes();
0700:                int len = list.getLength();
0701:
0702:                for (int i = 0; i < len; i++) {
0703:                    Node child = list.item(i);
0704:
0705:                    if (child.getNodeType() != Node.ELEMENT_NODE) {
0706:                        continue;
0707:                    }
0708:
0709:                    Element option = (Element) child;
0710:
0711:                    if (!option.getTagName()
0712:                            .equals(HtmlTag.OPTION.getTagName())) {
0713:                        continue;
0714:                    }
0715:
0716:                    // Found an option
0717:                    NodeList list2 = option.getChildNodes();
0718:                    int len2 = list2.getLength();
0719:                    StringBuffer sb = new StringBuffer();
0720:
0721:                    for (int j = 0; j < len2; j++) {
0722:                        Node child2 = list2.item(j);
0723:
0724:                        if ((child2.getNodeType() != Node.TEXT_NODE)
0725:                                && (child2.getNodeType() != Node.CDATA_SECTION_NODE)) {
0726:                            continue;
0727:                        }
0728:
0729:                        String nodeVal = child2.getNodeValue();
0730:
0731:                        if (nodeVal != null) {
0732:                            nodeVal = nodeVal.trim();
0733:
0734:                            //                    RaveText textNode = (child2 instanceof RaveText) ? (RaveText)child2 : null;
0735:                            //                    if ((textNode != null) && textNode.isJspx()) {
0736:                            Text textNode = (child2 instanceof  Text) ? (Text) child2
0737:                                    : null;
0738:                            if (textNode != null
0739:                                    && MarkupService.isJspxNode(textNode)) {
0740:                                // <markup_separation>
0741:                                //                        nodeVal = markupService.expandHtmlEntities(nodeVal, true, element);
0742:                                // ====
0743:                                //                        nodeVal = InSyncService.getProvider().expandHtmlEntities(nodeVal, true, element);
0744:                                nodeVal = webForm.getDomProviderService()
0745:                                        .expandHtmlEntities(nodeVal, true,
0746:                                                element);
0747:                                // </markup_separation>
0748:                            } // ELSE: regular entity fixing?
0749:
0750:                            sb.append(nodeVal);
0751:
0752:                            // XXX I should be able to bail here - for combo
0753:                            // boxes I only show the first item! (There's no
0754:                            // way for the user to open the menu). However,
0755:                            // for things like a multi select, you need to
0756:                            // show possibly multiple choices, so perhaps pass
0757:                            // in a max count?
0758:                        }
0759:                    }
0760:
0761:                    if (sb.length() > 0) {
0762:                        // Is this item selected too?
0763:                        Attr attr = option
0764:                                .getAttributeNode(HtmlAttribute.SELECTED);
0765:
0766:                        if (attr != null) {
0767:                            selected.add(new Integer(v.size()));
0768:                        }
0769:
0770:                        v.addElement(sb.toString());
0771:                    }
0772:                }
0773:
0774:                if (selected != null) {
0775:                    int[] result = new int[selected.size()];
0776:
0777:                    for (int i = 0, n = selected.size(); i < n; i++) {
0778:                        result[i] = (selected.get(i)).intValue();
0779:                    }
0780:
0781:                    return result;
0782:                }
0783:
0784:                return null;
0785:            }
0786:
0787:            /** Find all the text children of this tag, and add the
0788:             * text into the string buffer; whitespace is trimmed.
0789:             */
0790:            private static void populateText(Element element, StringBuffer sb) {
0791:                NodeList list = element.getChildNodes();
0792:                int len = list.getLength();
0793:
0794:                for (int i = 0; i < len; i++) {
0795:                    Node child = list.item(i);
0796:
0797:                    if ((child.getNodeType() != Node.TEXT_NODE)
0798:                            && (child.getNodeType() != Node.CDATA_SECTION_NODE)) {
0799:                        continue;
0800:                    }
0801:
0802:                    String nodeVal = child.getNodeValue();
0803:
0804:                    if (nodeVal != null) {
0805:                        nodeVal = nodeVal.trim();
0806:                        sb.append(nodeVal);
0807:                    }
0808:                }
0809:            }
0810:
0811:            protected void createChildren(CreateContext context) {
0812:                if (createChildren) {
0813:                    super .createChildren(context);
0814:                }
0815:            }
0816:
0817:            protected boolean isOpaqueBox() {
0818:                return true;
0819:            }
0820:
0821:            // XXX #6473220 The default size of the input components seems to be 20.
0822:            private static final int DEFAULT_INPUT_COMPONENT_SIZE = 20;
0823:
0824:            /**
0825:             * COPIED FROM javax.swing.text.html.FormView:
0826:             * <p>
0827:             * Creates a component for an &lt;INPUT&gt; element based on the
0828:             * value of the "type" attribute.
0829:             *
0830:             * @param set of attributes associated with the &lt;INPUT&gt; element.
0831:             * @param model the value of the StyleConstants.ModelAttribute
0832:             * @return the component.
0833:             */
0834:            private static JComponent createInputComponent(HtmlTag tag,
0835:                    Element el) {
0836:                JComponent c = null;
0837:                String type = el.getAttribute(HtmlAttribute.TYPE);
0838:
0839:                // <input> content should be added in
0840:                sCreateChildren = true;
0841:
0842:                // Default is text field
0843:                if ((tag == HtmlTag.INPUT)
0844:                        && ((type == null) || (type.length() == 0) || type
0845:                                .equals("text"))) { // NOI18N
0846:
0847:                    JTextField field = new JTextFieldColumnWidth();
0848:                    c = field;
0849:
0850:                    int size = HtmlAttribute.getIntegerAttributeValue(el,
0851:                            HtmlAttribute.SIZE, -1);
0852:                    field.setColumns((size > 0) ? size
0853:                            : DEFAULT_INPUT_COMPONENT_SIZE);
0854:
0855:                    // XXX #110555 Java app can't mix two or more L&F's at the same time.
0856:                    //            // Don't use IDE look and feel; use the platform's native l&f
0857:                    //            TextUI ui = (TextUI)createNativeUI(c);
0858:                    //
0859:                    //            if (ui != null) {
0860:                    //                field.setUI(ui);
0861:                    //            }
0862:
0863:                    String value = el.getAttribute(HtmlAttribute.VALUE);
0864:
0865:                    if ((value != null) && (value.length() > 0)) {
0866:                        field.setText(value);
0867:                    }
0868:
0869:                    if (CssProvider.getValueService()
0870:                            .isColorTransparentForElement(el,
0871:                                    XhtmlCss.BACKGROUND_COLOR_INDEX)) {
0872:                        field.setOpaque(false);
0873:                    }
0874:                    //field.addActionListener(this);
0875:                    //maxIsPreferred = 3;
0876:                } else if ((tag == HtmlTag.BUTTON) || type.equals("submit") // NOI18N
0877:                        || type.equals("reset") // NOI18N
0878:                        || type.equals("button")) { // NOI18N
0879:
0880:                    String value = getButtonLabel(el, type);
0881:
0882:                    JButton button = new JButton(value);
0883:
0884:                    // XXX change insets here??? Try to make buttons narrower
0885:                    // the below is not enough
0886:                    java.awt.Insets insets = button.getMargin();
0887:
0888:                    if (insets != null) {
0889:                        button.setMargin(new java.awt.Insets(insets.top / 2,
0890:                                insets.left / 2, insets.bottom / 2,
0891:                                insets.right / 2));
0892:                    }
0893:
0894:                    // XXX #110555 Java app can't mix two or more L&F's at the same time.
0895:                    //            // Grrrr.. unlike JComboBox, setUI is not public in JButton
0896:                    //            button.setUI((ButtonUI)createNativeUI(button));
0897:
0898:                    if (org.openide.util.Utilities.getOperatingSystem() == org.openide.util.Utilities.OS_MAC) {
0899:                        // Buttons are rounded to treat as nonrectangular
0900:                        // even though the aqua buttons will paint                    
0901:                        // a rectangular area - but it respects the background
0902:                        // color in this area (it does NOT paint the background
0903:                        // on the button itself, which is a bit weird...)
0904:                        // See Apple Technical Q&A QA1272
0905:                        // http://developer.apple.com/qa/qa2001/qa1272.html
0906:                        button.setOpaque(false); // will indicate nonrectangular
0907:                    }
0908:
0909:                    c = button;
0910:
0911:                    //maxIsPreferred = 3;
0912:                    //} else if (type.equals("image")) { // NOI18N
0913:                    // handled in factory instead - delegates to ImageBox rather
0914:                    // than using a JButton component for this as before
0915:                } else if (type.equals("checkbox")) {
0916:                    JCheckBox jc = new JCheckBox();
0917:                    c = jc;
0918:
0919:                    jc.setVerticalAlignment(SwingConstants.BOTTOM);
0920:
0921:                    java.awt.Insets insets = jc.getMargin();
0922:
0923:                    if (insets != null) {
0924:                        jc.setMargin(new java.awt.Insets(0, 0, 0, 0));
0925:                    }
0926:
0927:                    String checked = el.getAttribute(HtmlAttribute.CHECKED);
0928:
0929:                    if ((checked != null) && (checked.length() > 0)) {
0930:                        jc.setSelected(true);
0931:                    }
0932:
0933:                    // XXX #110555 Java app can't mix two or more L&F's at the same time.
0934:                    //            // Don't use IDE look and feel; use the platform's native l&f
0935:                    //            ButtonUI ui = (ButtonUI)createNativeUI(c);
0936:                    //
0937:                    //            if (ui != null) {
0938:                    //                jc.setUI(ui);
0939:                    //            }
0940:
0941:                    //maxIsPreferred = 3;
0942:                    jc.setOpaque(false); // will indicate nonrectangular
0943:                } else if (type.equals("radio")) {
0944:                    JRadioButton jc = new JRadioButton();
0945:                    c = jc;
0946:
0947:                    // Try to isolate only the radiobutton graphic itself
0948:                    jc.setVerticalAlignment(SwingConstants.BOTTOM);
0949:
0950:                    java.awt.Insets insets = jc.getMargin();
0951:
0952:                    if (insets != null) {
0953:                        jc.setMargin(new java.awt.Insets(0, 0, 0, 0));
0954:                    }
0955:
0956:                    String checked = el.getAttribute(HtmlAttribute.CHECKED);
0957:
0958:                    if ((checked != null) && (checked.length() > 0)) {
0959:                        jc.setSelected(true);
0960:                    }
0961:
0962:                    //            // Don't use IDE look and feel; use the platform's native l&f
0963:                    //            ButtonUI ui = (ButtonUI)createNativeUI(c);
0964:                    //
0965:                    //            if (ui != null) {
0966:                    //                jc.setUI(ui);
0967:                    //            }
0968:
0969:                    //maxIsPreferred = 3;
0970:                    jc.setOpaque(false); // will indicate nonrectangular
0971:                } else if (type.equals("password")) {
0972:                    JPasswordField field = new JPasswordFieldColumnWidth();
0973:                    c = field;
0974:
0975:                    // XXX #110555 Java app can't mix two or more L&F's at the same time.
0976:                    //            TextUI ui = (TextUI)createNativeUI(c);
0977:                    //
0978:                    //            if (ui != null) {
0979:                    //                field.setUI(ui);
0980:                    //            }
0981:
0982:                    int size = HtmlAttribute.getIntegerAttributeValue(el,
0983:                            HtmlAttribute.SIZE, -1);
0984:                    field.setColumns((size > 0) ? size
0985:                            : DEFAULT_INPUT_COMPONENT_SIZE);
0986:
0987:                    String value = el.getAttribute(HtmlAttribute.VALUE);
0988:
0989:                    if ((value != null) && (value.length() > 0)) {
0990:                        field.setText(value);
0991:                    } else {
0992:                        // No point looking up bean name - will become "*****"s anyway
0993:                        field.setText("abcdefghijk");
0994:                    }
0995:
0996:                    //field.addActionListener(this);
0997:                    //maxIsPreferred = 3;
0998:                } else if (type.equals("file")) {
0999:                    JTextField field = new JTextFieldColumnWidth();
1000:                    int size = HtmlAttribute.getIntegerAttributeValue(el,
1001:                            HtmlAttribute.SIZE, -1);
1002:                    field.setColumns((size > 0) ? size
1003:                            : DEFAULT_INPUT_COMPONENT_SIZE);
1004:
1005:                    // XXX #110555 Java app can't mix two or more L&F's at the same time.
1006:                    //            TextUI ui = (TextUI)createNativeUI(field);
1007:                    //
1008:                    //            if (ui != null) {
1009:                    //                field.setUI(ui);
1010:                    //            }
1011:
1012:                    // XXX Why am I using the Swing label here?
1013:                    JButton browseButton = new JButton(UIManager
1014:                            .getString("FormView.browseFileButtonText"));
1015:                    // XXX #110555 Java app can't mix two or more L&F's at the same time.
1016:                    //            browseButton.setUI((ButtonUI)createNativeUI(browseButton));
1017:
1018:                    javax.swing.Box box = javax.swing.Box.createHorizontalBox();
1019:                    box.add(field);
1020:                    box.add(javax.swing.Box.createHorizontalStrut(5));
1021:                    box.add(browseButton);
1022:                    c = box;
1023:
1024:                    //maxIsPreferred = 3;
1025:                    c.setOpaque(false); // will indicate nonrectangular
1026:                }
1027:
1028:                return c;
1029:            }
1030:
1031:            /** Return the label to be shown for a given button element */
1032:            private static String getButtonLabel(Element element, String type) {
1033:                if (element.getTagName().equals(HtmlTag.BUTTON.name)) {
1034:                    StringBuffer sb = new StringBuffer();
1035:                    //            DesignerUtils.addNodeText(sb, element, true);
1036:                    addNodeText(sb, element, true);
1037:
1038:                    return sb.toString().trim();
1039:                }
1040:
1041:                // Can't do this:
1042:                //String value = el.getAttribute(HtmlAttribute.VALUE);
1043:                // because I need to distinguish between value not set
1044:                // (in which case I show "Submit" instead) or value set to ""
1045:                // in which case I should leave the button blank
1046:                Attr attr = element.getAttributeNode(HtmlAttribute.VALUE);
1047:                String value = null;
1048:
1049:                if (attr != null) {
1050:                    value = attr.getValue();
1051:
1052:                    if (value.length() == 0) {
1053:                        // "" isn't good enough because we want the
1054:                        // button to be as tall as text
1055:                        value = " "; // NOI18N
1056:                    }
1057:                } else {
1058:                    if (type.equals("reset")) {
1059:                        value = NbBundle.getMessage(FormComponentBox.class,
1060:                                "Reset"); // NOI18N
1061:                    } else {
1062:                        // submit or button
1063:                        value = NbBundle.getMessage(FormComponentBox.class,
1064:                                "Submit"); // NOI18N
1065:                    }
1066:                }
1067:
1068:                return value;
1069:            }
1070:
1071:            // XXX Moved from DesignerUtils.
1072:            /**
1073:             * Add all the text content you can find under the given node into
1074:             * the given StringBuffer.
1075:             */
1076:            private static void addNodeText(StringBuffer sb, Node n,
1077:                    boolean skipSpace) {
1078:                int type = n.getNodeType();
1079:
1080:                if (type == Node.TEXT_NODE) {
1081:                    if (skipSpace
1082:                            && DesignerUtils.onlyWhitespace(n.getNodeValue())) {
1083:                        return;
1084:                    }
1085:
1086:                    sb.append(n.getNodeValue());
1087:                } else if (type == Node.COMMENT_NODE) {
1088:                    String comment = n.getNodeValue();
1089:                    int newline = comment.indexOf('\n');
1090:
1091:                    if (newline != -1) {
1092:                        sb.append(comment.substring(newline + 1));
1093:                    }
1094:                } else if (type == Node.CDATA_SECTION_NODE) {
1095:                    if (skipSpace
1096:                            && DesignerUtils.onlyWhitespace(n.getNodeValue())) {
1097:                        return;
1098:                    }
1099:
1100:                    sb.append(n.getNodeValue());
1101:                } else {
1102:                    NodeList children = n.getChildNodes();
1103:
1104:                    for (int i = 0; i < children.getLength(); i++) {
1105:                        addNodeText(sb, children.item(i), skipSpace);
1106:                    }
1107:                }
1108:            }
1109:
1110:            // XXX #110555 Java app can't mix two or more L&F's at the same time.
1111:            //    /**
1112:            //     * Look up the native look and feel to use on this platform.
1113:            //     * This will return the current look and feel (if it calls itself
1114:            //     * native), otherwise it will pick an arbitrary native (& supported)
1115:            //     * look and feel. If none can be found, it just returns the current
1116:            //     * look and feel.
1117:            //     */
1118:            //    private static LookAndFeel findNativeLnF() {
1119:            //        // Check the current look and feel first, and give that one
1120:            //        // preference if it fits the bill
1121:            //        LookAndFeel feel = UIManager.getLookAndFeel();
1122:            //
1123:            //        if (feel.isNativeLookAndFeel()) {
1124:            //            return feel;
1125:            //        }
1126:            //
1127:            //        UIManager.LookAndFeelInfo[] info = UIManager.getInstalledLookAndFeels();
1128:            //
1129:            //        if (info == null) {
1130:            //            return feel;
1131:            //        }
1132:            //
1133:            //        for (int i = 0; i < info.length; i++) {
1134:            //            String clsname = info[i].getClassName();
1135:            //            Class clz;
1136:            //
1137:            //            try {
1138:            //                clz = Class.forName(clsname);
1139:            //            } catch (ClassNotFoundException clf) {
1140:            //                clf.printStackTrace();
1141:            //
1142:            //                return feel;
1143:            //            }
1144:            //
1145:            //            if (clz != null) {
1146:            //                try {
1147:            //                    LookAndFeel f = (LookAndFeel)clz.newInstance();
1148:            //
1149:            //                    if (f.isSupportedLookAndFeel() && f.isNativeLookAndFeel()) {
1150:            //                        // Found one...
1151:            //                        return f;
1152:            //                    }
1153:            //                } catch (InstantiationException ie) {
1154:            //                    ie.printStackTrace();
1155:            //
1156:            //                    // continue
1157:            //                } catch (IllegalAccessException iae) {
1158:            //                    iae.printStackTrace();
1159:            //
1160:            //                    // continue
1161:            //                }
1162:            //            }
1163:            //        }
1164:            //
1165:            //        // Give up - no native look and feels found.
1166:            //        // Just use the current look and feel.
1167:            //        return feel;
1168:            //    }
1169:            //
1170:            //    /** Create a ComponentUI for the given JComponent. It will attempt
1171:            //     * to find a native look and feel for the component.
1172:            //     */
1173:            //    private static ComponentUI createNativeUI(JComponent c) {
1174:            //        if (uidef == null) {
1175:            //            LookAndFeel feel = findNativeLnF();
1176:            //            uidef = feel.getDefaults();
1177:            //
1178:            //            if (uidef == null) {
1179:            //                return null;
1180:            //            }
1181:            //        }
1182:            //
1183:            //        javax.swing.plaf.ComponentUI cu = uidef.getUI(c);
1184:            //
1185:            //        return cu;
1186:            //    }
1187:
1188:            protected void paintBackground(Graphics g, int x, int y) {
1189:                // We don't need any background painted - fully covered
1190:                // by component bitmap, unless we have children
1191:                if (createChildren && (getBoxCount() > 0)) {
1192:                    super .paintBackground(g, x, y);
1193:                }
1194:            }
1195:
1196:            public void paint(Graphics g, int px, int py) {
1197:                super .paint(g, px, py);
1198:
1199:                if (hidden) {
1200:                    return;
1201:                }
1202:
1203:                px += leftMargin;
1204:                py += effectiveTopMargin;
1205:
1206:                // XXX Don't I need to add in margins?
1207:                if (getComponent() != null) { // not the case for e.g. input hidden
1208:
1209:                    Image image = getImage();
1210:
1211:                    if (image != null) {
1212:                        // XXX #117371.
1213:                        if (hasInitialFocus()
1214:                                && webform.getInitialFocusMarkCssBox() == null) {
1215:                            webform.setInitialFocusMarkCssBox(this );
1216:                            Graphics og = image.getGraphics();
1217:                            paintFocusWaterMark(og, 0, 0);
1218:                        }
1219:
1220:                        transform.setToTranslation((float) (px + getX()),
1221:                                (float) (py + getY()));
1222:                        ((Graphics2D) g).drawImage(image, transform, null);
1223:                    }
1224:                }
1225:            }
1226:
1227:            private Image getImage() {
1228:                if ((image != null) || attempted) { // only try once
1229:
1230:                    return image;
1231:                }
1232:
1233:                if (getComponent() == null) {
1234:                    return null;
1235:                }
1236:
1237:                attempted = true;
1238:
1239:                JComponent comp = getComponent();
1240:                Dimension size = comp.getPreferredSize();
1241:                imageWidth = size.width;
1242:                imageHeight = size.height;
1243:
1244:                //System.out.println("size=" + size);
1245:                if ((imageWidth == 0) || (imageHeight == 0)) {
1246:                    // XXX #6504407 If not relevant case (component should have actual 0 size) -> bad architecture.
1247:                    ErrorManager.getDefault().log(
1248:                            "Component has 0 size - " + this  + " w="
1249:                                    + imageWidth + " h=" + imageHeight);
1250:
1251:                    // XXX #6504407 Don't count this attempt, 
1252:                    // there might be still uninitialized lengths (bad architecture).
1253:                    attempted = false;
1254:
1255:                    return null;
1256:                }
1257:
1258:                comp.setSize(size);
1259:
1260:                image = comp.createImage(imageWidth, imageHeight);
1261:
1262:                if (image == null) {
1263:                    image = new BufferedImage(imageWidth, imageHeight,
1264:                            BufferedImage.TYPE_INT_RGB);
1265:
1266:                    if (image == null) {
1267:                        return null;
1268:                    }
1269:                }
1270:
1271:                Graphics og = image.getGraphics(); // offscreen buffer
1272:
1273:                // XXX This 'nonrectangular' (should be transparent) stuff is very suspicious,
1274:                // why to repaint the parent's color again? Is it because of the grid dots only?
1275:                if (nonrectangular) {
1276:                    //og.setColor(bg);
1277:                    // Hmmm, what about borders? Just call super-duper instead?
1278:                    //og.fillRect(0, 0, imageWidth, imageHeight);
1279:                    //super.paintBackground(og, 0, 0, imageWidth, imageHeight);
1280:                    Color oldBg = bg;
1281:                    CssBox p = this ;
1282:
1283:                    while ((p != null) && (p.bg == null)) {
1284:                        p = p.getParent();
1285:                    }
1286:
1287:                    if (p != null) {
1288:                        bg = p.bg;
1289:                    } else {
1290:                        // XXX Ugh! How did this happen?
1291:                        if (DEBUGFORMAT) {
1292:                            ErrorManager.getDefault().log(
1293:                                    "Surprising - no parent with bg! this="
1294:                                            + this  + ", element="
1295:                                            + getElement() + ", parent="
1296:                                            + getParent() + ", p=" + p);
1297:                        }
1298:
1299:                        bg = Color.white;
1300:                    }
1301:
1302:                    super .paintBackground(og, 0, 0);
1303:                    bg = oldBg;
1304:                }
1305:
1306:                CellRendererPane rendererPane = webform.getRenderPane();
1307:
1308:                if (rendererPane == null) { // testsuite
1309:                    og.drawString("Error", 0, imageHeight / 2);
1310:
1311:                    return image;
1312:                }
1313:
1314:                rendererPane.paintComponent(og, comp, null, 0, 0, imageWidth,
1315:                        imageHeight, true);
1316:
1317:                // Empty out the renderer pane, allowing renderers to be gc'ed.
1318:                // (The paintComponent call above implicitly adds the component
1319:                // to its child list and does not remove it!)
1320:                rendererPane.removeAll();
1321:
1322:                // XXX #6277297. This is needed, otherwise it gets overlapped by the image.
1323:                //        // FIXME Improve the painting machinery.
1324:                //        if (hasInitialFocus()) {
1325:                //            paintFocusWaterMark(og, 0, 0);
1326:                //        }
1327:                //paintFacesWatermark(og, 0, 0);
1328:                //        String name ="foo";
1329:                //        if (bean != null) {
1330:                //            name = bean.getInstanceName();
1331:                //        }
1332:                //        String filename = "/tmp/comp" + name + ".png";
1333:                //        try {
1334:                //            if (image instanceof BufferedImage) {
1335:                //                javax.imageio.ImageIO.write((BufferedImage)image, "png", new java.io.File(filename));
1336:                //            } else {
1337:                //                System.out.println("Image was not a BufferedImage");
1338:                //            }
1339:                //        } catch (java.io.IOException e) {
1340:                //            e.printStackTrace();
1341:                //        }
1342:                return image;
1343:            }
1344:
1345:            // XXX #112462 Width of text fields (and other textuals) is computed
1346:            // differently in browser and differently in Swing, trying to adjust.
1347:            private static Dimension computeSizeForTextComponent(
1348:                    JTextComponent textComponent) {
1349:                if (textComponent instanceof  JTextField) {
1350:                    JTextField textField = (JTextField) textComponent;
1351:                    int columns = textField.getColumns();
1352:                    if (columns > 0) {
1353:                        return computeSizeForTextComponentColumns(
1354:                                textComponent, columns);
1355:                    }
1356:                } else if (textComponent instanceof  JTextArea) {
1357:                    JTextArea textArea = (JTextArea) textComponent;
1358:                    int columns = textArea.getColumns();
1359:                    if (columns > 0) {
1360:                        return computeSizeForTextComponentColumns(
1361:                                textComponent, columns);
1362:                    }
1363:                }
1364:                return textComponent.getPreferredSize();
1365:            }
1366:
1367:            // XXX #112462 Width of text fields (and other textuals) is computed
1368:            // differently in browser and differently in Swing, trying to adjust.
1369:            private static Dimension computeSizeForTextComponentColumns(
1370:                    JTextComponent textComponent, int columns) {
1371:                Dimension size = textComponent.getPreferredSize();
1372:                Font font = textComponent.getFont();
1373:                if (font == null) {
1374:                    return size;
1375:                }
1376:                FontMetrics fontMetrics = textComponent.getFontMetrics(font);
1377:                if (fontMetrics == null) {
1378:                    return size;
1379:                }
1380:                //        Insets insets = textComponent.getInsets();
1381:                //        int insetsWidth = insets == null ? 0 : insets.left + insets.right;
1382:                size.width = columns * (fontMetrics.charWidth('a'))/* + insetsWidth*/; // NOI18N
1383:                return size;
1384:            }
1385:
1386:            //    /**
1387:            //     * Paints a watermark on the component, indicating that it has initial focus.
1388:            //     * @param g The graphics context
1389:            //     */
1390:            //    private static void paintFocusWatermark(Graphics g) {
1391:            //        Image watermark =
1392:            //            org.openide.util.Utilities.loadImage("org/netbeans/modules/visualweb/css2/focus-watermark.gif"); // NOI18N
1393:            //
1394:            //        if ((watermark != null) && (g instanceof Graphics2D)) {
1395:            //            Graphics2D g2d = (Graphics2D)g;
1396:            //            AffineTransform t = new AffineTransform(); // XXX keep transform object around?
1397:            //            t.translate(0.0D, 0.0D);
1398:            //
1399:            //            Composite oldAlpha = g2d.getComposite();
1400:            //            g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.5f));
1401:            //            g2d.drawImage(watermark, t, null);
1402:            //            g2d.setComposite(oldAlpha);
1403:            //        }
1404:            //    }
1405:
1406:            /**
1407:             * Determines the preferred span for this view along an
1408:             * axis.  This is implemented to return the value
1409:             * returned by Component.getPreferredSize along the
1410:             * axis of interest.
1411:             *
1412:             * @param axis may be either CssBox.X_AXIS or CssBox.Y_AXIS
1413:             * @return   the span the view would like to be rendered into >= 0.
1414:             *           Typically the view is told to render into the span
1415:             *           that is returned, although there is no guarantee.
1416:             *           The parent may choose to resize or break the view.
1417:             * @exception IllegalArgumentException for an invalid axis
1418:             */
1419:            public float getPreferredSpan(int axis) {
1420:                if ((axis != X_AXIS) && (axis != Y_AXIS)) {
1421:                    throw new IllegalArgumentException("Invalid axis: " + axis);
1422:                }
1423:
1424:                if (imageWidth != -1) {
1425:                    return (axis == CssBox.X_AXIS) ? imageWidth : imageHeight;
1426:                }
1427:
1428:                if (createdC != null) {
1429:                    Dimension size = createdC.getPreferredSize();
1430:
1431:                    if (axis == CssBox.X_AXIS) {
1432:                        return size.width;
1433:                    } else {
1434:                        return size.height;
1435:                    }
1436:                }
1437:
1438:                return 0;
1439:            }
1440:
1441:            /**
1442:             * Determines the minimum span for this view along an
1443:             * axis.  This is implemented to return the value
1444:             * returned by Component.getMinimumSize along the
1445:             * axis of interest.
1446:             *
1447:             * @param axis may be either CssBox.X_AXIS or CssBox.Y_AXIS
1448:             * @return   the span the view would like to be rendered into >= 0.
1449:             *           Typically the view is told to render into the span
1450:             *           that is returned, although there is no guarantee.
1451:             *           The parent may choose to resize or break the view.
1452:             * @exception IllegalArgumentException for an invalid axis
1453:             */
1454:            public float getMinimumSpan(int axis) {
1455:                if ((axis != X_AXIS) && (axis != Y_AXIS)) {
1456:                    throw new IllegalArgumentException("Invalid axis: " + axis);
1457:                }
1458:
1459:                if (imageWidth != -1) {
1460:                    // XXX TODO: return createdC.getMinimumSize() ? Must cache!
1461:                    return (axis == CssBox.X_AXIS) ? imageWidth : imageHeight;
1462:                }
1463:
1464:                if (createdC != null) {
1465:                    Dimension size = createdC.getMinimumSize();
1466:
1467:                    if (axis == CssBox.X_AXIS) {
1468:                        return size.width;
1469:                    } else {
1470:                        return size.height;
1471:                    }
1472:                }
1473:
1474:                return 0;
1475:            }
1476:
1477:            /**
1478:             * Determines the maximum span for this view along an
1479:             * axis.  This is implemented to return the value
1480:             * returned by Component.getMaximumSize along the
1481:             * axis of interest.
1482:             *
1483:             * @param axis may be either CssBox.X_AXIS or CssBox.Y_AXIS
1484:             * @return   the span the view would like to be rendered into >= 0.
1485:             *           Typically the view is told to render into the span
1486:             *           that is returned, although there is no guarantee.
1487:             *           The parent may choose to resize or break the view.
1488:             * @exception IllegalArgumentException for an invalid axis
1489:             */
1490:            public float getMaximumSpan(int axis) {
1491:                // XXX TODO Roll in the FormView subclass of this
1492:                if ((axis != X_AXIS) && (axis != Y_AXIS)) {
1493:                    throw new IllegalArgumentException("Invalid axis: " + axis);
1494:                }
1495:
1496:                if (imageWidth != -1) {
1497:                    // XXX TODO: return createdC.getMaximumSize() ? Must cache!
1498:                    return (axis == CssBox.X_AXIS) ? imageWidth : imageHeight;
1499:                }
1500:
1501:                if (createdC != null) {
1502:                    Dimension size = createdC.getMaximumSize();
1503:
1504:                    if (axis == CssBox.X_AXIS) {
1505:                        return size.width;
1506:                    } else {
1507:                        return size.height;
1508:                    }
1509:                }
1510:
1511:                return 0;
1512:            }
1513:
1514:            /**
1515:             * FOR TESTSUITE ONLY!
1516:             * Fetch the component associated with the view.
1517:             */
1518:            public final JComponent getComponent() {
1519:                return createdC;
1520:            }
1521:
1522:            public int getWidth() {
1523:                // XXX This doesn't account for children - how do we do that?
1524:                return (int) getPreferredSpan(X_AXIS);
1525:            }
1526:
1527:            public int getHeight() {
1528:                // XXX This doesn't account for children - how do we do that?
1529:                return (int) getPreferredSpan(Y_AXIS);
1530:            }
1531:
1532:            //    public String toString() {
1533:            //        Element element = getElement();
1534:            //        return "FormComponentBox[value=" + (element == null ? null : element.getAttribute("value")) + ", " +
1535:            //        //"component=" + createdC + ", " +
1536:            //        paramString() + "]";
1537:            //    }
1538:
1539:            protected String paramString() {
1540:                Element element = getElement();
1541:                return "value="
1542:                        + (element == null ? null : element
1543:                                .getAttribute("value")) + ", "
1544:                        + super .paramString() + ", component=" + createdC; // NOI18N
1545:            }
1546:
1547:            /*
1548:            protected String paramString() {
1549:                return super.paramString() + ", " +
1550:                    "component=" + createdC;
1551:            }
1552:             */
1553:            public int getIntrinsicWidth() {
1554:                return (int) getPreferredSpan(X_AXIS);
1555:            }
1556:
1557:            public int getIntrinsicHeight() {
1558:                return (int) getPreferredSpan(Y_AXIS);
1559:            }
1560:
1561:            public int getBaseline() {
1562:                return baseline;
1563:            }
1564:
1565:            /** XXX #112462 Text field with adjusted (more 'browser-like') column width. */
1566:            private static class JTextFieldColumnWidth extends JTextField {
1567:                private int columnWidth;
1568:
1569:                public JTextFieldColumnWidth() {
1570:                    super ();
1571:                }
1572:
1573:                public JTextFieldColumnWidth(String text, int columns) {
1574:                    super (text, columns);
1575:                }
1576:
1577:                @Override
1578:                public void setFont(Font font) {
1579:                    super .setFont(font);
1580:                    columnWidth = 0;
1581:                }
1582:
1583:                @Override
1584:                protected int getColumnWidth() {
1585:                    if (columnWidth == 0) {
1586:                        FontMetrics metrics = getFontMetrics(getFont());
1587:                        //                columnWidth = metrics.charWidth('m');
1588:                        columnWidth = metrics.charWidth('a'); // NOI18N
1589:                    }
1590:                    return columnWidth;
1591:                }
1592:            } // End of JTextFieldColumnWidth.
1593:
1594:            /** XXX #112462 Text field with adjusted (more 'browser-like') column width. */
1595:            private static class JPasswordFieldColumnWidth extends
1596:                    JPasswordField {
1597:                private int columnWidth;
1598:
1599:                public JPasswordFieldColumnWidth() {
1600:                    super ();
1601:                }
1602:
1603:                public JPasswordFieldColumnWidth(String text, int columns) {
1604:                    super (text, columns);
1605:                }
1606:
1607:                @Override
1608:                public void setFont(Font font) {
1609:                    super .setFont(font);
1610:                    columnWidth = 0;
1611:                }
1612:
1613:                @Override
1614:                protected int getColumnWidth() {
1615:                    if (columnWidth == 0) {
1616:                        FontMetrics metrics = getFontMetrics(getFont());
1617:                        //                columnWidth = metrics.charWidth('m');
1618:                        columnWidth = metrics.charWidth('a'); // NOI18N
1619:                    }
1620:                    return columnWidth;
1621:                }
1622:            } // End of JTextFieldColumnWidth.
1623:
1624:            /** XXX #112462 Text area with adjusted (more 'browser-like') column width. */
1625:            private static class JTextAreaColumnWidth extends JTextArea {
1626:                private int columnWidth;
1627:
1628:                public JTextAreaColumnWidth(Document doc) {
1629:                    super (doc);
1630:                }
1631:
1632:                public JTextAreaColumnWidth(String text, int rows, int columns) {
1633:                    super (text, rows, columns);
1634:                }
1635:
1636:                @Override
1637:                public void setFont(Font font) {
1638:                    super .setFont(font);
1639:                    columnWidth = 0;
1640:                }
1641:
1642:                @Override
1643:                protected int getColumnWidth() {
1644:                    if (columnWidth == 0) {
1645:                        FontMetrics metrics = getFontMetrics(getFont());
1646:                        //                columnWidth = metrics.charWidth('m');
1647:                        columnWidth = metrics.charWidth('a'); // NOI18N
1648:                    }
1649:                    return columnWidth;
1650:                }
1651:            } // End of JTextAreaColumnWidth.
1652:
1653:            public static JTextField createTextField() {
1654:                return new JTextFieldColumnWidth();
1655:            }
1656:
1657:            public static JTextField createTextField(String text, int columns) {
1658:                return new JTextFieldColumnWidth(text, columns);
1659:            }
1660:
1661:            public static JTextArea createTextArea(String text, int rows,
1662:                    int columns) {
1663:                return new JTextAreaColumnWidth(text, rows, columns);
1664:            }
1665:
1666:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.