📄 companylocationcomponent.java
字号:
package crms.applet.company;import javax.swing.*;import javax.swing.border.*;import java.awt.*;import java.awt.event.*;import java.util.*;import crms.vo.*;import crms.ui.*;import crms.util.*;public class CompanyLocationComponent extends CRMSComponent { public static final int LOCATION_CHANGE = 0; JComboBox comboSites = new JComboBox(); JTextField textTitle = new JTextField(); JTextField textAddress = new JTextField(); JTextField textSuburb = new JTextField(); JComboBox comboState = new JComboBox(StateCode.STATE_LIST); JTextField textCountry = new JTextField(); JTextField textPostcode = new JTextField(); MultiValueTextField mtextPhone = new MultiValueTextField(); MultiValueTextField mtextFax = new MultiValueTextField(); MultiValueTextField mtextEmail = new MultiValueTextField(); MultiValueTextField mtextURL = new MultiValueTextField(); JButton saveButton = new JButton("Update"); boolean editing = false; boolean readonly = false; boolean changed = false; private ArrayList locations = null; private Location current_location = null; private CallbackDestination callback = null; public CompanyLocationComponent() { } public CompanyLocationComponent(boolean readonly) { this.readonly = readonly; } public void setSelectable(boolean selectable) { comboSites.setEnabled(selectable); } public void setCallback(CallbackDestination destination) { callback = destination; } public ArrayList getLocations() { return locations; } public void setLocations(ArrayList locations) { this.locations = locations; if (locations == null) return; comboSites.removeAllItems(); for (int i = 0; i < locations.size(); i++) { Location tmp_loc = (Location)locations.get(i); comboSites.addItem(tmp_loc); if (i == 0) { swapLocation(tmp_loc); } } } public void setBackgroundFields(Color color) { textTitle.setBackground(color); textAddress.setBackground(color); textSuburb.setBackground(color); textPostcode.setBackground(color); textCountry.setBackground(color); ((JTextField)comboState.getEditor().getEditorComponent()).setBackground(color); ((JTextField)comboSites.getEditor().getEditorComponent()).setBackground(color); comboState.setBackground(color); mtextPhone.setBackground(color); mtextFax.setBackground(color); mtextEmail.setBackground(color); mtextURL.setBackground(color); } public void init() { // create the initial selector above setLayout(new GridBagLayout()); setBackground(Color.WHITE); setBorder(new EmptyBorder(4,4,4,4)); comboSites.setEditable(true);/*(new javax.swing.plaf.basic.BasicComboBoxEditor() { public Component getEditorComponent() { return textTitle; } public Object getItem() { return current_location; } public void setItem() { } } ); */ comboSites.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { Object selectedItem = comboSites.getSelectedItem(); if (selectedItem instanceof Location) { Location tmp_loc = (Location)selectedItem; swapLocation(tmp_loc); } else if (selectedItem instanceof String) { System.out.println("update title to: " + selectedItem); if (current_location != null) { current_location.setTitle((String)selectedItem); } } } } ); setEditing(false); ////////////////// SETUP GRID LAYOUT ////////////////////////// // Width = 5 (label, data, label, data, buttons) // Col 0 = Labels Insets defaultInsets = new Insets(4,0,0,4); // X Y W H WX WY Handle Expand add(new JLabel("Location"), new GridBagConstraints(0, 0, 1, 1, 0.4, 0.0, GridBagConstraints.EAST, GridBagConstraints.NONE, defaultInsets, 0, 0)); add(comboSites, new GridBagConstraints(1, 0, 3, 1, 1.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, defaultInsets, 0, 0));/* add(new JLabel("Name"), new GridBagConstraints(0, 1, 1, 1, 0.4, 0.0, GridBagConstraints.EAST, GridBagConstraints.NONE, defaultInsets, 0, 0)); add(textTitle, new GridBagConstraints(1, 1, 3, 1, 1.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, defaultInsets, 0, 0));*/ add(new JLabel("Phone"), new GridBagConstraints(0, 2, 1, 1, 0.4, 0.0, GridBagConstraints.EAST, GridBagConstraints.NONE, defaultInsets, 0, 0)); add(mtextPhone, new GridBagConstraints(1, 2, 1, 1, 1.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, defaultInsets, 0, 0)); add(new JLabel("Fax"), new GridBagConstraints(2, 2, 1, 1, 0.4, 0.0, GridBagConstraints.EAST, GridBagConstraints.NONE, defaultInsets, 0, 0)); add(mtextFax, new GridBagConstraints(3, 2, 1, 1, 1.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, defaultInsets, 0, 0)); add(new JLabel("Address"), new GridBagConstraints(0, 3, 1, 1, 0.4, 0.0, GridBagConstraints.EAST, GridBagConstraints.NONE, defaultInsets, 0, 0)); add(textAddress, new GridBagConstraints(1, 3, 3, 1, 1.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, defaultInsets, 0, 0)); add(new JLabel("Suburb"), new GridBagConstraints(0, 4, 1, 1, 0.4, 0.0, GridBagConstraints.EAST, GridBagConstraints.NONE, defaultInsets, 0, 0)); add(textSuburb, new GridBagConstraints(1, 4, 1, 1, 1.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, defaultInsets, 0, 0)); add(new JLabel("State"), new GridBagConstraints(2, 4, 1, 1, 0.4, 0.0, GridBagConstraints.EAST, GridBagConstraints.NONE, defaultInsets, 0, 0)); add(comboState, new GridBagConstraints(3, 4, 1, 1, 1.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, defaultInsets, 0, 0)); add(new JLabel("Country"), new GridBagConstraints(0, 5, 1, 1, 0.4, 0.0, GridBagConstraints.EAST, GridBagConstraints.NONE, defaultInsets, 0, 0)); add(textCountry, new GridBagConstraints(1, 5, 1, 1, 1.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, defaultInsets, 0, 0)); add(new JLabel("Postcode"), new GridBagConstraints(2, 5, 1, 1, 0.4, 0.0, GridBagConstraints.EAST, GridBagConstraints.NONE, defaultInsets, 0, 0)); add(textPostcode, new GridBagConstraints(3, 5, 1, 1, 1.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, defaultInsets, 0, 0)); add(new JLabel("Email"), new GridBagConstraints(0, 6, 1, 1, 0.4, 0.0, GridBagConstraints.EAST, GridBagConstraints.NONE, defaultInsets, 0, 0)); add(mtextEmail, new GridBagConstraints(1, 6, 1, 1, 1.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, defaultInsets, 0, 0)); add(new JLabel("URL"), new GridBagConstraints(2, 6, 1, 1, 0.4, 0.0, GridBagConstraints.EAST, GridBagConstraints.NONE, defaultInsets, 0, 0)); add(mtextURL, new GridBagConstraints(3, 6, 1, 1, 1.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, defaultInsets, 0, 0)); // SPECIAL CASE: When the display is read-write if (readonly == false) { JButton newButton = new JButton("New"); JButton delButton = new JButton("Del"); newButton.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { // update Location data setEditing(true); swapLocation(new Location()); if (locations == null) locations = new ArrayList(); locations.add(current_location); comboSites.addItem(current_location); comboSites.setSelectedItem(current_location); } } ); delButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { if (locations == null || current_location == null) return; locations.remove(current_location); comboSites.removeItem(current_location); // swapLocation will update a location if it exists, which it won't now current_location = null; if (locations.size() > 0) { swapLocation((Location)locations.get(0)); comboSites.setSelectedItem(current_location); } else { comboSites.setSelectedItem(null); swapLocation(new Location()); current_location = null; setEditing(false); } } } ); saveButton.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { // update Location data updateLocation(); debug(); } } ); add(newButton, new GridBagConstraints(4, 0, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, defaultInsets, 0, 0)); add(delButton, new GridBagConstraints(4, 1, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, defaultInsets, 0, 0)); // update button must be on the last row of this grid //add(saveButton, // new GridBagConstraints(4, 6, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, defaultInsets, 0, 0)); // add handler to detect changed items ActionListener comboChange = new ActionListener() { public void actionPerformed(ActionEvent e) { if (current_location != null && (e.getSource() instanceof JComboBox || e.getActionCommand().compareTo("comboBoxEdited") == 0)) { changed = true; saveButton.setEnabled(true); } } }; KeyListener textKey = new KeyAdapter() { public void keyPressed(KeyEvent e) { changed = true; saveButton.setEnabled(true); } }; textTitle.addKeyListener(textKey); textAddress.addKeyListener(textKey); textSuburb.addKeyListener(textKey); textPostcode.addKeyListener(textKey); textCountry.addKeyListener(textKey); comboState.addActionListener(comboChange); mtextPhone.addActionListener(comboChange); mtextFax.addActionListener(comboChange); mtextEmail.addActionListener(comboChange); mtextURL.addActionListener(comboChange); } else { // make the fields less intimidating setBackgroundFields(new Color(0xF0, 0xFF, 0xF0)); } } public void setEditing(boolean editing) { this.editing = editing; textTitle.setEditable(editing); textAddress.setEditable(editing); textSuburb.setEditable(editing); textPostcode.setEditable(editing); textCountry.setEditable(editing); mtextPhone.setEditable(editing); mtextFax.setEditable(editing); mtextEmail.setEditable(editing); mtextURL.setEditable(editing); ((JTextField)comboSites.getEditor().getEditorComponent()).setEditable(editing); comboState.setEnabled(editing); comboState.setEditable(true); ((JTextField)comboState.getEditor().getEditorComponent()).setEditable(editing); ((JTextField)comboState.getEditor().getEditorComponent()).setEnabled(true); saveButton.setEnabled(editing); } public void setSelected(int location_id) { if (locations == null) return; System.out.println("Selection request sent: " + location_id); Iterator i = locations.iterator(); while (i.hasNext()) { Location location = (Location)i.next(); if (location.getID() == location_id) { System.out.println("Swapping location: " + location); comboSites.setSelectedItem(location); //swapLocation(location); break; } } } public void swapLocation(Location new_location) { // save the current location updateLocation(); current_location = new_location; setEditing(readonly == false && current_location != null); // handle the case where the list is empty if (current_location == null) return; textTitle.setText(current_location.getTitle()); textAddress.setText(current_location.getAddress()); textSuburb.setText(current_location.getSuburb()); textPostcode.setText(current_location.getPostCode()); textCountry.setText(current_location.getCountry()); comboState.setSelectedItem(current_location.getState()); mtextPhone.setValues(current_location.getPhone()); mtextFax.setValues(current_location.getFax()); mtextEmail.setValues(current_location.getEmail()); mtextURL.setValues(current_location.getURL()); comboSites.setSelectedItem(current_location); // notify callbackee if (callback != null) callback.callback(this, LOCATION_CHANGE, new_location); } public void updateLocation() { if (current_location == null) return; // standard text entries // Edited in the combo box: current_location.setTitle(textTitle.getText()); current_location.setAddress(textAddress.getText()); current_location.setSuburb(textSuburb.getText()); current_location.setPostCode(textPostcode.getText()); current_location.setCountry(textCountry.getText()); // combo box String state = (String)comboState.getSelectedItem(); current_location.setState(state != null ? state : ""); // multi value boxen current_location.setPhone(mtextPhone.getValues()); current_location.setFax(mtextFax.getValues()); current_location.setEmail(mtextEmail.getValues()); current_location.setURL(mtextURL.getValues()); comboSites.setSelectedItem(current_location); saveButton.setEnabled(false); changed = false; } public void debug() { System.out.println("====================== DEBUG DUMP FOR LOCATIONS ======================"); for (int i = 0; i < locations.size(); i++) { Location loc = (Location)locations.get(i); System.out.println("----- "+loc+" -----"); System.out.println(loc.getAddress()); System.out.println(loc.getSuburb()); System.out.println(loc.getPhone()); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -