📄 swingformbuilder.java
字号:
/* CRMS, customer relationship management system Copyright (C) 2003 Service To Youth Council This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA For further information contact the SYC ICT department on GPL@syc.net.au 98 Kermode Street North Adelaide South Australia SA 5006 +61 (0)8 8367 0755 *//* * FormBuilder.java * * Created on 11 April 2003, 07:02 */package crms.form;import javax.swing.*;import java.awt.*;import java.awt.event.*;import mseries.Calendar.*;import mseries.ui.*;/** * * @author dmurphy */public class SwingFormBuilder { private FormPanel panel = null; private static SwingFormBuilder _instance = null; /** Creates a new instance of FormBuilder */ private SwingFormBuilder() { } public static SwingFormBuilder getInstance() { if (_instance == null) { _instance = new SwingFormBuilder(); } return _instance; } public FormPanel buildForm ( Form form ) { panel = new FormPanel(); panel.setForm(form); panel.setLayout(new BorderLayout()); if (form.getTitle() != null) { JPanel titlePanel = new JPanel(new BorderLayout()); JLabel label = new JLabel(form.getTitle()); label.setBackground(Color.WHITE); label.setHorizontalAlignment(JLabel.CENTER); titlePanel.add(label, BorderLayout.CENTER); titlePanel.setBackground(Color.WHITE); panel.add( titlePanel, BorderLayout.NORTH); } JPanel bodyPanel = new JPanel(); bodyPanel.setLayout(new GridBagLayout()); bodyPanel.setBackground(Color.WHITE); for (int i=0; i < form.getFieldCount(); i++ ) { FormField field = form.getField(i); Component widget = getWidgetForField(field, panel); bodyPanel.add( widget, getGridBagConstraints(field)); } panel.add(bodyPanel, BorderLayout.CENTER); return panel; } public GridBagConstraints getGridBagConstraints(FormField field) { int ipadx = 0; int ipady = 0; if (field.getType() == FieldType.TEXTAREA) { ipadx = 45; ipady = 44; } GridBagConstraints cons = new GridBagConstraints( field.getLocation().getX(), field.getLocation().getY(), field.getLocation().getWidth(), field.getLocation().getHeight(), 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(5,10,0,0), ipadx, ipady); return cons; } public Component getWidgetForField( FormField field, FormPanel panel ) { FieldType type = field.getType(); if (type == FieldType.SUBMIT_BUTTON || type == FieldType.RESET_BUTTON || type == FieldType.CANCEL_BUTTON) { JButton button = new JButton(field.getText()); if (type == FieldType.SUBMIT_BUTTON) { button.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent ev) { submitClicked(ev); } }); } else if (type == FieldType.RESET_BUTTON) { button.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent ev) { resetClicked(ev); } }); } else if (type == FieldType.CANCEL_BUTTON) { button.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent ev) { cancelClicked(ev); } }); } panel.addWidget(field.getName(), button); return button; } else if (type == FieldType.CHECKBOX) { JCheckBox checkBox = new JCheckBox(); final String fieldName = field.getName(); checkBox.addItemListener( new ItemListener() { public void itemStateChanged(ItemEvent e) { if (e.getStateChange() == ItemEvent.SELECTED) { checkBoxChanged(fieldName, true); } else { checkBoxChanged(fieldName, false); } } }); panel.addWidget(field.getName(), checkBox); return checkBox; } else if (type == FieldType.DATE) { MDateEntryField dateField = new MDateEntryField(); MDefaultPullDownConstraints c = new MDefaultPullDownConstraints(); c.firstDay = java.util.Calendar.MONDAY; dateField.setConstraints(c); MSimpleDateFormat dateFormat = new MSimpleDateFormat("d MMMM, yyyy"); dateField.setDateFormatter(dateFormat); panel.addWidget(field.getName(), dateField); return dateField; } else if (type == FieldType.DROPDOWN_LIST) { DefaultComboBoxModel model = new DefaultComboBoxModel(); String defaultOption = field.getDefaultOption(); FieldOption def = null; for (int i=0; i < field.getOptionCount(); i++) { FieldOption option = field.getOption(i); model.addElement(option); if (option.getCode().equals(defaultOption)) { def = option; } } JComboBox combo = new JComboBox(model); model.setSelectedItem(def); panel.addWidget(field.getName(), combo); return combo; } else if (type == FieldType.LABEL) { JLabel label = new JLabel(field.getText()); panel.addWidget(field.getName(), label); return label; } else if (type == FieldType.TEXTAREA) { int width = field.getInternalSize().getWidth(); if (width < 1) { width = 20; } int height = field.getInternalSize().getHeight(); if (height < 1) { height = 4; } JTextArea textArea = new JTextArea(width, height); textArea.setText(field.getText()); JScrollPane scrollPane = new JScrollPane(textArea); panel.addWidget(field.getName(), textArea); return scrollPane; } else if (type == FieldType.TEXTFIELD) { JTextField textField = new JTextField(); int width = field.getInternalSize().getWidth(); if (width < 1) { width = 20; } textField.setColumns(width); textField.setText(field.getText()); panel.addWidget(field.getName(), textField); return textField; } else if (type == FieldType.TIME) { MDateSpinner timeField = new MDateSpinner(); timeField.setFormatter(new MSimpleDateFormat("h:mm a")); panel.addWidget(field.getName(), timeField); return timeField; } return null; } public void submitClicked(ActionEvent ev) { panel.submitButtonClicked(ev); } public void resetClicked(ActionEvent ev) { panel.resetButtonClicked(ev); } public void cancelClicked(ActionEvent ev) { panel.cancelButtonClicked(ev); } public void checkBoxChanged(String fieldName, boolean value) { panel.getResult().setFieldValue(fieldName, String.valueOf(value)); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -