0001: /*
0002: * Licensed to the Apache Software Foundation (ASF) under one or more
0003: * contributor license agreements. See the NOTICE file distributed with
0004: * this work for additional information regarding copyright ownership.
0005: * The ASF licenses this file to You under the Apache License, Version 2.0
0006: * (the "License"); you may not use this file except in compliance with
0007: * the License. You may obtain a copy of the License at
0008: *
0009: * http://www.apache.org/licenses/LICENSE-2.0
0010: *
0011: * Unless required by applicable law or agreed to in writing, software
0012: * distributed under the License is distributed on an "AS IS" BASIS,
0013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0014: * See the License for the specific language governing permissions and
0015: * limitations under the License.
0016: */
0017: /**
0018: * @author Roman I. Chernyatchik
0019: * @version $Revision$
0020: */package javax.swing.text.html;
0021:
0022: import java.awt.Color;
0023: import java.awt.Component;
0024: import java.awt.ComponentOrientation;
0025: import java.awt.Cursor;
0026: import java.awt.Dimension;
0027: import java.awt.Font;
0028: import java.awt.FontMetrics;
0029: import java.awt.Graphics;
0030: import java.awt.Insets;
0031: import java.awt.event.ActionEvent;
0032: import java.awt.event.ActionListener;
0033: import java.awt.event.InputEvent;
0034: import java.net.URL;
0035:
0036: import javax.swing.AbstractButton;
0037: import javax.swing.Box;
0038: import javax.swing.ButtonGroup;
0039: import javax.swing.ButtonModel;
0040: import javax.swing.Icon;
0041: import javax.swing.JButton;
0042: import javax.swing.JCheckBox;
0043: import javax.swing.JComboBox;
0044: import javax.swing.JComponent;
0045: import javax.swing.JFileChooser;
0046: import javax.swing.JList;
0047: import javax.swing.JPasswordField;
0048: import javax.swing.JRadioButton;
0049: import javax.swing.JScrollPane;
0050: import javax.swing.JTextArea;
0051: import javax.swing.JTextField;
0052: import javax.swing.JToggleButton;
0053: import javax.swing.KeyStroke;
0054: import javax.swing.ListSelectionModel;
0055: import javax.swing.JToggleButton.ToggleButtonModel;
0056: import javax.swing.border.BevelBorder;
0057: import javax.swing.border.Border;
0058: import javax.swing.border.CompoundBorder;
0059: import javax.swing.border.EmptyBorder;
0060: import javax.swing.text.AttributeSet;
0061: import javax.swing.text.JTextComponent;
0062: import javax.swing.text.PlainDocument;
0063: import javax.swing.text.SimpleAttributeSet;
0064:
0065: import org.apache.harmony.x.swing.Utilities;
0066: import org.apache.harmony.x.swing.text.html.HTMLIconFactory;
0067: import org.apache.harmony.x.swing.text.html.form.Form;
0068: import org.apache.harmony.x.swing.text.html.form.FormButtonModel;
0069: import org.apache.harmony.x.swing.text.html.form.FormSelectComboBoxModel;
0070: import org.apache.harmony.x.swing.text.html.form.FormSelectListModel;
0071: import org.apache.harmony.x.swing.text.html.form.FormTextModel;
0072: import org.apache.harmony.x.swing.text.html.form.FormToggleButtonModel;
0073:
0074: final class FormViewComponentFactory {
0075: static class InputImageIcon implements Icon {
0076: private BackgroundImageLoader loader;
0077: private Icon icon;
0078:
0079: public InputImageIcon(final String src, final URL baseURL,
0080: final FormView view) {
0081: if (src == null) {
0082: icon = HTMLIconFactory.getNoImageIcon();
0083: } else {
0084: URL url = HTML.resolveURL(src, baseURL);
0085: if (url == null) {
0086: icon = HTMLIconFactory.getLoadingFailedIcon();
0087: } else {
0088: loader = new BackgroundImageLoader(url, true, -1,
0089: -1) {
0090: protected void onReady() {
0091: super .onReady();
0092: view.preferenceChanged(view, true, true);
0093: }
0094:
0095: protected void onError() {
0096: super .onError();
0097: icon = HTMLIconFactory.getNoImageIcon();
0098: view.preferenceChanged(view, true, true);
0099: }
0100: };
0101: }
0102: }
0103: }
0104:
0105: public boolean imageWasLoaded() {
0106: return loader != null && loader.isReady();
0107: }
0108:
0109: public void paintIcon(final Component c, final Graphics g,
0110: final int x, final int y) {
0111: if (icon != null) {
0112: icon.paintIcon(c, g, x, y);
0113: return;
0114: }
0115:
0116: if (!loader.isReady()) {
0117: HTMLIconFactory.getLoadingImageIcon().paintIcon(c, g,
0118: x, y);
0119: return;
0120: }
0121:
0122: g.drawImage(loader.image, x, y, getIconWidth(),
0123: getIconHeight(), loader);
0124: }
0125:
0126: public int getIconWidth() {
0127: if (icon != null) {
0128: return icon.getIconWidth();
0129: }
0130:
0131: if (!loader.isReady()) {
0132: return HTMLIconFactory.getLoadingImageIcon()
0133: .getIconWidth();
0134: }
0135:
0136: return loader.getWidth();
0137: }
0138:
0139: public int getIconHeight() {
0140: if (icon != null) {
0141: return icon.getIconHeight();
0142: }
0143:
0144: if (!loader.isReady()) {
0145: return HTMLIconFactory.getLoadingImageIcon()
0146: .getIconHeight();
0147: }
0148:
0149: return loader.getHeight();
0150: }
0151: };
0152:
0153: private static final int DEFAULT_TEXTFIELD_SIZE = 20;
0154: private static final int DEFAULT_STRUT = 5;
0155: private static final int DEFAULT_COLS_COUNT = 20;
0156: private static final int DEFAULT_ROWS_COUNT = 3;
0157:
0158: private static final char MEAN_CHAR = 'z';
0159:
0160: private static final Color IMG_BORDER_HIGHLIGHT = new Color(136,
0161: 136, 136);
0162: private static final Color IMG_BORDER_SHADOW = new Color(204, 204,
0163: 204);
0164:
0165: private static final String DIR_RTL = "rtl";
0166: private static final String BROWSE_BUTTON_DEFAULT_TEXT = "Browse...";
0167: private static final String SUBMIT_DEFAULT_TEXT = "Submit Query";
0168: private static final String RESET_DEFAULT_TEXT = "Reset";
0169:
0170: private FormViewComponentFactory() {
0171: }
0172:
0173: public static Component createButtonComponent(final Object model,
0174: final AttributeSet attrs, final FormView view) {
0175:
0176: // TODO Implement support of BUTTON content
0177: return createImageComponent(model, attrs, view);
0178: }
0179:
0180: public static Component createInputButtonComponent(
0181: final Object model, final AttributeSet attrs) {
0182:
0183: ButtonModel buttonModel = (ButtonModel) model;
0184: final JButton button = new JButton("");
0185:
0186: // Model
0187: if (buttonModel == null) {
0188: buttonModel = new FormButtonModel(new Form(
0189: SimpleAttributeSet.EMPTY), SimpleAttributeSet.EMPTY);
0190: }
0191: button.setModel(buttonModel);
0192:
0193: // VALUE
0194: String attribute = (String) attrs
0195: .getAttribute(HTML.Attribute.VALUE);
0196: if (!Utilities.isEmptyString(attribute)) {
0197: button.setText(attribute);
0198: } else {
0199: final int width, height;
0200: final FontMetrics fontMetrics = button
0201: .getFontMetrics(button.getFont());
0202: final Insets insets = button.getInsets();
0203: width = DEFAULT_STRUT + insets.top + insets.bottom;
0204: height = fontMetrics.getHeight() + insets.top
0205: + insets.bottom;
0206:
0207: Dimension size = button.getPreferredSize();
0208: size.width = width;
0209: size.height = height;
0210: button.setPreferredSize(size);
0211: button.setMaximumSize(size);
0212: button.setMinimumSize(size);
0213: }
0214:
0215: // SIZE
0216: setButtonSize(button, attrs);
0217:
0218: // TITLE
0219: setTitle(button, attrs);
0220:
0221: // ACCESSKEY
0222: setButtonAccessKey(button, attrs);
0223:
0224: // ALIGN
0225: setButtonAlign(button);
0226:
0227: // DISABLED
0228: setDisabled(button, attrs);
0229:
0230: return button;
0231: }
0232:
0233: public static Component createInputCheckBoxComponent(
0234: final Object model, final AttributeSet attrs) {
0235: ToggleButtonModel checkBoxModel = (ToggleButtonModel) model;
0236: final JCheckBox checkBox = new JCheckBox();
0237:
0238: // Model
0239: if (checkBoxModel == null) {
0240: checkBoxModel = new FormToggleButtonModel(new Form(
0241: SimpleAttributeSet.EMPTY), SimpleAttributeSet.EMPTY);
0242: }
0243: checkBox.setModel(checkBoxModel);
0244:
0245: // SIZE
0246: setButtonSize(checkBox, attrs);
0247:
0248: // TITLE
0249: setTitle(checkBox, attrs);
0250:
0251: // CHECKED
0252: setChecked(checkBox, attrs);
0253:
0254: // ACCESSKEY
0255: setButtonAccessKey(checkBox, attrs);
0256:
0257: // ALIGN
0258: setButtonAlign(checkBox);
0259:
0260: // DISABLED
0261: setDisabled(checkBox, attrs);
0262:
0263: return checkBox;
0264: }
0265:
0266: public static Component createInputImageComponent(
0267: final Object model, final AttributeSet attrs,
0268: final FormView view) {
0269: final Component image = createImageComponent(model, attrs, view);
0270:
0271: // ActionPerformed
0272: image.addMouseListener(view.new MouseEventListener());
0273:
0274: return image;
0275: }
0276:
0277: public static Component createInputPasswordComponent(
0278: final Object model, final AttributeSet attrs,
0279: final FormView view) {
0280: PlainDocument document = (PlainDocument) model;
0281: final JPasswordField passwordField = new JPasswordField();
0282:
0283: // Model
0284: if (document == null) {
0285: document = new FormTextModel(new Form(
0286: SimpleAttributeSet.EMPTY), SimpleAttributeSet.EMPTY);
0287: }
0288: passwordField.setDocument(document);
0289:
0290: // ActionPerformed
0291: passwordField.addActionListener(new ActionListener() {
0292:
0293: public void actionPerformed(final ActionEvent event) {
0294: view.actionPerformed(event);
0295: }
0296:
0297: });
0298:
0299: // VALUE
0300: String attribute = (String) attrs
0301: .getAttribute(HTML.Attribute.VALUE);
0302:
0303: if (!Utilities.isEmptyString(attribute)) {
0304: passwordField.setText(attribute);
0305: }
0306:
0307: // SIZE
0308: setTextSize(passwordField, attrs, passwordField.getEchoChar());
0309:
0310: // TITLE
0311: setTitle(passwordField, attrs);
0312:
0313: // ACCESSKEY
0314: setTextAccessKey(passwordField, attrs);
0315:
0316: // DIR
0317: setTextDir(passwordField, attrs);
0318:
0319: // READONLY
0320: setTextReadonly(passwordField, attrs);
0321:
0322: // ALIGN
0323: setTextAlign(passwordField);
0324:
0325: // DISABLED
0326: setDisabled(passwordField, attrs);
0327:
0328: return passwordField;
0329: }
0330:
0331: public static Component createInputRadioComponent(
0332: final Object model, final AttributeSet attrs) {
0333: ToggleButtonModel radioButtonModel;
0334: final JRadioButton radioButton = new JRadioButton();
0335:
0336: // NAME
0337: String attribute = (String) attrs
0338: .getAttribute(HTML.Attribute.NAME);
0339: if (!Utilities.isEmptyString(attribute)) {
0340: radioButtonModel = (ToggleButtonModel) model;
0341: } else {
0342: radioButtonModel = new ToggleButtonModel() {
0343: public void setGroup(final ButtonGroup group) {
0344: //Do nothing
0345: };
0346: };
0347: }
0348:
0349: // Model
0350: if (radioButtonModel == null) {
0351: radioButtonModel = new FormToggleButtonModel(new Form(
0352: SimpleAttributeSet.EMPTY), SimpleAttributeSet.EMPTY);
0353: }
0354: radioButton.setModel(radioButtonModel);
0355:
0356: // SIZE
0357: setButtonSize(radioButton, attrs);
0358:
0359: // TITLE
0360: setTitle(radioButton, attrs);
0361:
0362: // CHECKED
0363: setChecked(radioButton, attrs);
0364:
0365: // ACCESSKEY
0366: setButtonAccessKey(radioButton, attrs);
0367:
0368: // ALIGN
0369: setButtonAlign(radioButton);
0370:
0371: // DISABLED
0372: setDisabled(radioButton, attrs);
0373:
0374: return radioButton;
0375: }
0376:
0377: public static Component createInputResetComponent(
0378: final Object model, final AttributeSet attrs,
0379: final FormView view) {
0380: ButtonModel resetButtonModel = (ButtonModel) model;
0381: final JButton resetButton = new JButton();
0382:
0383: // Model
0384: if (resetButtonModel == null) {
0385: resetButtonModel = new FormButtonModel(new Form(
0386: SimpleAttributeSet.EMPTY), SimpleAttributeSet.EMPTY);
0387: }
0388: resetButton.setModel(resetButtonModel);
0389:
0390: // ActionPerformed
0391: resetButton.addActionListener(new ActionListener() {
0392:
0393: public void actionPerformed(final ActionEvent event) {
0394: view.actionPerformed(event);
0395: }
0396:
0397: });
0398:
0399: // VALUE
0400: String attribute = (String) attrs
0401: .getAttribute(HTML.Attribute.VALUE);
0402:
0403: if (!Utilities.isEmptyString(attribute)) {
0404: resetButton.setText(attribute);
0405: } else {
0406: resetButton.setText(RESET_DEFAULT_TEXT);
0407: }
0408:
0409: // SIZE
0410: setButtonSize(resetButton, attrs);
0411:
0412: // TITLE
0413: setTitle(resetButton, attrs);
0414:
0415: // ACCESSKEY
0416: setButtonAccessKey(resetButton, attrs);
0417:
0418: // ALIGN
0419: setButtonAlign(resetButton);
0420:
0421: // DISABLED
0422: setDisabled(resetButton, attrs);
0423:
0424: return resetButton;
0425: }
0426:
0427: public static Component createInputSubmitComponent(
0428: final Object model, final AttributeSet attrs,
0429: final FormView view) {
0430: ButtonModel submitButtonModel = (ButtonModel) model;
0431: final JButton submitButton = new JButton();
0432:
0433: // Model
0434: if (submitButtonModel == null) {
0435: submitButtonModel = new FormButtonModel(new Form(
0436: SimpleAttributeSet.EMPTY), SimpleAttributeSet.EMPTY);
0437: }
0438: submitButton.setModel(submitButtonModel);
0439:
0440: // ActionPerformed
0441: submitButton.addActionListener(new ActionListener() {
0442:
0443: public void actionPerformed(final ActionEvent event) {
0444: view.actionPerformed(event);
0445: }
0446:
0447: });
0448:
0449: // VALUE
0450: String attribute = (String) attrs
0451: .getAttribute(HTML.Attribute.VALUE);
0452:
0453: if (!Utilities.isEmptyString(attribute)) {
0454: submitButton.setText(attribute);
0455: } else {
0456: submitButton.setText(SUBMIT_DEFAULT_TEXT);
0457: }
0458:
0459: // SIZE
0460: setButtonSize(submitButton, attrs);
0461:
0462: // TITLE
0463: setTitle(submitButton, attrs);
0464:
0465: // ACCESSKEY
0466: setButtonAccessKey(submitButton, attrs);
0467:
0468: // ALIGN
0469: setButtonAlign(submitButton);
0470:
0471: // DISABLED
0472: setDisabled(submitButton, attrs);
0473:
0474: return submitButton;
0475: }
0476:
0477: public static Component createInputTextComponent(
0478: final Object model, final AttributeSet attrs,
0479: final FormView view) {
0480: PlainDocument document = (PlainDocument) model;
0481: final JTextField textField = new JTextField();
0482:
0483: // Model
0484: if (document == null) {
0485: document = new FormTextModel(new Form(
0486: SimpleAttributeSet.EMPTY), SimpleAttributeSet.EMPTY);
0487: }
0488: textField.setDocument(document);
0489:
0490: // ActionPerformed
0491: textField.addActionListener(new ActionListener() {
0492:
0493: public void actionPerformed(final ActionEvent event) {
0494: view.actionPerformed(event);
0495: }
0496:
0497: });
0498:
0499: // VALUE
0500: final String attribute = (String) attrs
0501: .getAttribute(HTML.Attribute.VALUE);
0502: if (!Utilities.isEmptyString(attribute)) {
0503: textField.setText(attribute);
0504: }
0505:
0506: // SIZE
0507: setTextSize(textField, attrs, MEAN_CHAR);
0508:
0509: // TITLE
0510: setTitle(textField, attrs);
0511:
0512: // ACCESSKEY
0513: setTextAccessKey(textField, attrs);
0514:
0515: // DIR
0516: setTextDir(textField, attrs);
0517:
0518: // READONLY
0519: setTextReadonly(textField, attrs);
0520:
0521: // ALIGN
0522: setTextAlign(textField);
0523:
0524: // DISABLED
0525: setDisabled(textField, attrs);
0526:
0527: return textField;
0528: }
0529:
0530: public static Component createInputFileComponent(
0531: final Object model, final AttributeSet attrs) {
0532: /*
0533: * FilePath attributes
0534: */
0535: PlainDocument document = (PlainDocument) model;
0536: final JTextField filePath = new JTextField();
0537:
0538: // Model
0539: if (document == null) {
0540: document = new FormTextModel(new Form(
0541: SimpleAttributeSet.EMPTY), SimpleAttributeSet.EMPTY);
0542: }
0543: filePath.setDocument(document);
0544:
0545: // SIZE
0546: setTextSize(filePath, attrs, MEAN_CHAR);
0547:
0548: // ACCESSKEY
0549: setTextAccessKey(filePath, attrs);
0550:
0551: // DIR
0552: boolean isRTL = setTextDir(filePath, attrs);
0553:
0554: /*
0555: * Browse button attributes
0556: */
0557: final JButton browseButton = new JButton(
0558: BROWSE_BUTTON_DEFAULT_TEXT);
0559:
0560: // READONLY
0561: String attribute = (String) attrs
0562: .getAttribute(HTML.Attribute.READONLY);
0563: if (attribute != null) {
0564: filePath.setEditable(false);
0565: } else {
0566: browseButton.addActionListener(new ActionListener() {
0567: private JFileChooser chooser;
0568:
0569: public void actionPerformed(final ActionEvent e) {
0570: if (chooser == null) {
0571: chooser = new JFileChooser();
0572: }
0573: if (chooser.showOpenDialog(browseButton) == JFileChooser.APPROVE_OPTION) {
0574:
0575: filePath.setText(chooser.getSelectedFile()
0576: .getPath());
0577: }
0578: }
0579: });
0580: }
0581:
0582: /*
0583: * Box attributes
0584: */
0585: final Box box = Box.createHorizontalBox();
0586:
0587: // TITLE
0588: attribute = (String) attrs.getAttribute(HTML.Attribute.TITLE);
0589: if (!Utilities.isEmptyString(attribute)) {
0590: filePath.setToolTipText(attribute);
0591: browseButton.setToolTipText(attribute);
0592: }
0593:
0594: // ALIGN
0595: box.setAlignmentX(JComponent.CENTER_ALIGNMENT);
0596: box.setAlignmentY(JComponent.BOTTOM_ALIGNMENT);
0597: browseButton.setAlignmentX(JComponent.LEFT_ALIGNMENT);
0598: browseButton.setAlignmentY(JComponent.CENTER_ALIGNMENT);
0599:
0600: // DISABLED
0601: if (attrs.getAttribute(HTML.Attribute.DISABLED) != null) {
0602: filePath.setEnabled(false);
0603: browseButton.setEnabled(false);
0604: }
0605:
0606: box.add(filePath);
0607: box.add(Box.createHorizontalStrut(5));
0608: box.add(browseButton);
0609:
0610: if (isRTL) {
0611: box
0612: .setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
0613: }
0614:
0615: return box;
0616: }
0617:
0618: public static JComponent createSelectMultipleComponent(
0619: final Object model, final AttributeSet attrs) {
0620: // MULTIPLE
0621: final boolean isMultiple = (attrs
0622: .getAttribute(HTML.Attribute.MULTIPLE) != null);
0623:
0624: // SIZE
0625: int linesCount = 0;
0626: String attribute = (String) attrs
0627: .getAttribute(HTML.Attribute.SIZE);
0628: if (!Utilities.isEmptyString(attribute)) {
0629: try {
0630: linesCount = Integer.parseInt(attribute);
0631: } catch (NumberFormatException e) {
0632: //DO nothing
0633: }
0634: }
0635:
0636: /*
0637: * JList attributes
0638: */
0639: JList selectionList = new JList();
0640: FormSelectListModel optionModel;
0641:
0642: // Model
0643: if (model != null) {
0644: optionModel = (FormSelectListModel) model;
0645: } else {
0646: optionModel = new FormSelectListModel(new Form(
0647: SimpleAttributeSet.EMPTY),
0648: SimpleAttributeSet.EMPTY, selectionList
0649: .getSelectionModel());
0650: }
0651:
0652: selectionList.setModel(optionModel);
0653: selectionList
0654: .setSelectionModel(optionModel.getSelectionModel());
0655:
0656: if (isMultiple) {
0657: selectionList
0658: .setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
0659:
0660: }
0661:
0662: // TITLE
0663: if (!Utilities.isEmptyString(optionModel.getTitle())) {
0664: selectionList.setToolTipText(optionModel.getTitle());
0665: }
0666:
0667: // DIR
0668: setTextDir(selectionList, attrs);
0669:
0670: // OPTION attributes
0671: if (linesCount <= 1) {
0672: linesCount = Math
0673: .max(1, selectionList.getModel().getSize());
0674: }
0675:
0676: // Selection
0677: FormViewUtils.resetMultipleSelection(optionModel);
0678:
0679: /*
0680: * JScrollPane attributes
0681: */
0682: final FontMetrics fontMetrics = selectionList
0683: .getFontMetrics(selectionList.getFont());
0684: Dimension size;
0685: if (optionModel.getSize() == 0) {
0686: size = selectionList.getPreferredSize();
0687: Insets insets = selectionList.getInsets();
0688: size.width = fontMetrics.charWidth(MEAN_CHAR) + insets.left
0689: + insets.right;
0690: selectionList.setPreferredSize(size);
0691: }
0692:
0693: JScrollPane pane = new JScrollPane(selectionList,
0694: JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
0695: JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
0696:
0697: size = pane.getPreferredSize();
0698: size.height = linesCount * fontMetrics.getHeight();
0699: pane.setMinimumSize(size);
0700: pane.setMaximumSize(size);
0701: pane.setPreferredSize(size);
0702: pane.setAlignmentY(JComponent.BOTTOM_ALIGNMENT);
0703:
0704: // DISABLED
0705: if (optionModel.isEnabled()) {
0706: pane.setEnabled(false);
0707: }
0708:
0709: return pane;
0710: }
0711:
0712: public static JComponent createSelectSimpleComponent(
0713: final Object model, final AttributeSet attrs) {
0714: JComboBox selectElement = new JComboBox();
0715: FormSelectComboBoxModel comboBoxModel = (FormSelectComboBoxModel) model;
0716: // Model
0717: if (comboBoxModel == null) {
0718: comboBoxModel = new FormSelectComboBoxModel(new Form(
0719: SimpleAttributeSet.EMPTY), SimpleAttributeSet.EMPTY);
0720:
0721: }
0722: selectElement.setModel(comboBoxModel);
0723:
0724: selectElement.setAlignmentY(JComponent.BOTTOM_ALIGNMENT);
0725:
0726: // TITLE
0727: if (!Utilities.isEmptyString(comboBoxModel.getTitle())) {
0728: selectElement.setToolTipText(comboBoxModel.getTitle());
0729: }
0730:
0731: // DIR
0732: setTextDir(selectElement, attrs);
0733:
0734: // Selection
0735: FormViewUtils.resetSimpleSelection(selectElement.getModel());
0736:
0737: // Size
0738: final Dimension size = selectElement.getPreferredSize();
0739: selectElement.setMinimumSize(size);
0740: selectElement.setMaximumSize(size);
0741:
0742: // DISABLED
0743: if (!comboBoxModel.isEnabled()) {
0744: selectElement.setEnabled(false);
0745: }
0746:
0747: return selectElement;
0748: }
0749:
0750: public static Component createTextAreaComponent(final Object model,
0751: final AttributeSet attrs, final FormView view) {
0752: /*
0753: * JTextArea attributes
0754: */
0755: Dimension size;
0756: PlainDocument document = (PlainDocument) model;
0757:
0758: //ROWS
0759: int rowsCount = DEFAULT_ROWS_COUNT;
0760: String attribute = (String) attrs
0761: .getAttribute(HTML.Attribute.ROWS);
0762: if (!Utilities.isEmptyString(attribute)) {
0763: try {
0764: rowsCount = Integer.parseInt(attribute);
0765: } catch (NumberFormatException e) {
0766: //Do nothing
0767: }
0768: }
0769:
0770: //COLS
0771: int columnsCount = DEFAULT_COLS_COUNT;
0772: attribute = (String) attrs.getAttribute(HTML.Attribute.COLS);
0773: if (!Utilities.isEmptyString(attribute)) {
0774: try {
0775: columnsCount = Integer.parseInt(attribute);
0776: } catch (NumberFormatException e) {
0777: //Do nothing
0778: }
0779: }
0780:
0781: //Model
0782: if (document == null) {
0783: document = new FormTextModel(new Form(
0784: SimpleAttributeSet.EMPTY), SimpleAttributeSet.EMPTY);
0785: }
0786:
0787: final JTextArea textArea = new JTextArea(document, null,
0788: rowsCount, columnsCount);
0789: //DIR
0790: setTextDir(textArea, attrs);
0791:
0792: //ACCESSKEY
0793: setTextAccessKey(textArea, attrs);
0794:
0795: //READONLY
0796: setTextReadonly(textArea, attrs);
0797:
0798: /*
0799: * JScrollPane attributes
0800: */
0801: final JScrollPane pane = new JScrollPane(textArea);
0802: size = pane.getPreferredSize();
0803: pane.setMinimumSize(size);
0804: pane.setPreferredSize(size);
0805: pane.setMaximumSize(size);
0806:
0807: pane.setAlignmentY(JComponent.BOTTOM_ALIGNMENT);
0808:
0809: //TITLE
0810: attribute = (String) attrs.getAttribute(HTML.Attribute.TITLE);
0811: if (!Utilities.isEmptyString(attribute)) {
0812: textArea.setToolTipText(attribute);
0813: pane.setToolTipText(attribute);
0814: pane.getVerticalScrollBar().setToolTipText(attribute);
0815: pane.getHorizontalScrollBar().setToolTipText(attribute);
0816: }
0817:
0818: //DISABLED
0819: if (attrs.getAttribute(HTML.Attribute.DISABLED) != null) {
0820: textArea.setEnabled(false);
0821: pane.setEnabled(false);
0822: }
0823:
0824: return pane;
0825: }
0826:
0827: private static Component createImageComponent(final Object model,
0828: final AttributeSet attrs, final FormView view) {
0829:
0830: ButtonModel imageModel = (ButtonModel) model;
0831: final JButton image = new JButton("");
0832:
0833: // Model
0834: if (imageModel == null) {
0835: imageModel = new FormButtonModel(new Form(
0836: SimpleAttributeSet.EMPTY), SimpleAttributeSet.EMPTY);
0837: }
0838: image.setModel(imageModel);
0839:
0840: image.setCursor(new Cursor(Cursor.HAND_CURSOR));
0841:
0842: // SRC, ALT
0843: String attribute = (String) attrs
0844: .getAttribute(HTML.Attribute.SRC);
0845:
0846: InputImageIcon icon = new InputImageIcon(attribute,
0847: ((HTMLDocument) view.getDocument()).getBase(), view);
0848: image.setIcon(icon);
0849: image.setBackground(Color.WHITE);
0850: Dimension size;
0851: if (icon.imageWasLoaded()) {
0852: image.setBorderPainted(false);
0853: size = new Dimension(icon.getIconWidth(), icon
0854: .getIconHeight());
0855: } else {
0856: Border outside = new BevelBorder(BevelBorder.LOWERED,
0857: IMG_BORDER_SHADOW, IMG_BORDER_HIGHLIGHT);
0858: image.setBorder(new CompoundBorder(outside,
0859: new EmptyBorder(5, 5, 5, 5)));
0860: image.setContentAreaFilled(false);
0861: image.setFocusPainted(false);
0862: attribute = (String) attrs.getAttribute(HTML.Attribute.ALT);
0863: if (!Utilities.isEmptyString(attribute)) {
0864: image.setFont(new Font("Button.font", 0, 12));
0865: image.setText(attribute);
0866: image.setToolTipText(attribute);
0867: }
0868: size = image.getPreferredSize();
0869: }
0870: image.setMinimumSize(size);
0871: image.setPreferredSize(size);
0872: image.setMaximumSize(size);
0873:
0874: //SIZE
0875: setButtonSize(image, attrs);
0876:
0877: //TITLE
0878: setTitle(image, attrs);
0879:
0880: //ACCESSKEY
0881: setButtonAccessKey(image, attrs);
0882:
0883: //ALIGN
0884: setButtonAlign(image);
0885:
0886: //DISABLED
0887: setDisabled(image, attrs);
0888:
0889: return image;
0890: }
0891:
0892: private static void setTextSize(final JTextComponent textComponent,
0893: final AttributeSet attrs, final char widestChar) {
0894: final String attribute = (String) attrs
0895: .getAttribute(HTML.Attribute.SIZE);
0896: int width = DEFAULT_TEXTFIELD_SIZE;
0897: if (attribute != null) {
0898: try {
0899: final int newWidth = Integer.parseInt(attribute);
0900: if (newWidth > width) {
0901: width = newWidth;
0902: }
0903: } catch (NumberFormatException e) {
0904: // do nothing
0905: }
0906: }
0907: final FontMetrics fontMetrics = textComponent
0908: .getFontMetrics(textComponent.getFont());
0909: final int charWidth = fontMetrics.charWidth(widestChar);
0910: Dimension size = textComponent.getPreferredSize();
0911:
0912: size.width = width * charWidth;
0913: textComponent.setPreferredSize(size);
0914: textComponent.setMaximumSize(size);
0915:
0916: size = new Dimension(DEFAULT_TEXTFIELD_SIZE * charWidth,
0917: size.height);
0918: textComponent.setMinimumSize(size);
0919: }
0920:
0921: private static String setTitle(final JComponent component,
0922: final AttributeSet attrs) {
0923: final String attribute = (String) attrs
0924: .getAttribute(HTML.Attribute.TITLE);
0925: if (!Utilities.isEmptyString(attribute)) {
0926: component.setToolTipText(attribute);
0927: }
0928: return attribute;
0929: }
0930:
0931: private static void setTextReadonly(
0932: final JTextComponent textComponent, final AttributeSet attrs) {
0933: if (attrs.getAttribute(HTML.Attribute.READONLY) != null) {
0934: textComponent.setEditable(false);
0935: }
0936: }
0937:
0938: private static void setButtonAccessKey(final AbstractButton button,
0939: final AttributeSet attrs) {
0940: final String attribute = (String) attrs
0941: .getAttribute(HTML.Attribute.ACCESSKEY);
0942: if (!Utilities.isEmptyString(attribute)) {
0943: button.setMnemonic(attribute.charAt(0));
0944: }
0945: }
0946:
0947: private static void setButtonAlign(final AbstractButton button) {
0948: button.setAlignmentX(JComponent.LEFT_ALIGNMENT);
0949: button.setAlignmentY(JComponent.BOTTOM_ALIGNMENT);
0950: }
0951:
0952: private static void setButtonSize(final AbstractButton button,
0953: final AttributeSet attrs) {
0954: final String attribute;
0955: attribute = (String) attrs.getAttribute(HTML.Attribute.SIZE);
0956: if (attribute != null) {
0957: Dimension size = button.getPreferredSize();
0958: try {
0959: size.width = Integer.parseInt(attribute);
0960: } catch (NumberFormatException e) {
0961: //Do nothing
0962: }
0963: button.setPreferredSize(size);
0964: button.setMaximumSize(size);
0965: button.setMinimumSize(size);
0966: }
0967: }
0968:
0969: private static void setChecked(final JToggleButton button,
0970: final AttributeSet attrs) {
0971: if (attrs.getAttribute(HTML.Attribute.CHECKED) != null) {
0972: button.setSelected(true);
0973: }
0974: }
0975:
0976: private static void setDisabled(final Component component,
0977: final AttributeSet attrs) {
0978: if (attrs.getAttribute(HTML.Attribute.DISABLED) != null) {
0979: component.setEnabled(false);
0980: }
0981: }
0982:
0983: private static void setTextAccessKey(
0984: final JTextComponent textComponent, final AttributeSet attrs) {
0985: final String attribute = (String) attrs
0986: .getAttribute(HTML.Attribute.ACCESSKEY);
0987: if (!Utilities.isEmptyString(attribute)) {
0988: ActionListener listener = new ActionListener() {
0989:
0990: public void actionPerformed(final ActionEvent e) {
0991: textComponent.requestFocusInWindow();
0992: }
0993:
0994: };
0995: final char key = attribute.charAt(0);
0996: final KeyStroke keystroke1 = KeyStroke.getKeyStroke(
0997: Character.toLowerCase(key), InputEvent.ALT_MASK);
0998: final KeyStroke keystroke2 = KeyStroke.getKeyStroke(
0999: Character.toUpperCase(key), InputEvent.ALT_MASK);
1000: textComponent.registerKeyboardAction(listener, keystroke1,
1001: JComponent.WHEN_IN_FOCUSED_WINDOW);
1002: textComponent.registerKeyboardAction(listener, keystroke2,
1003: JComponent.WHEN_IN_FOCUSED_WINDOW);
1004: }
1005: }
1006:
1007: private static void setTextAlign(final JTextComponent component) {
1008: component.setAlignmentX(JComponent.CENTER_ALIGNMENT);
1009: component.setAlignmentY(JComponent.BOTTOM_ALIGNMENT);
1010: }
1011:
1012: private static boolean setTextDir(final Component component,
1013: final AttributeSet attrs) {
1014: final String attribute = (String) attrs
1015: .getAttribute(HTML.Attribute.DIR);
1016: if (!Utilities.isEmptyString(attribute)) {
1017: if (DIR_RTL.equals(attribute.toLowerCase())) {
1018: component
1019: .setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
1020: return true;
1021: }
1022: }
1023: return false;
1024: }
1025:
1026: }
|