1 19 20 package org.netbeans.modules.xml.multiview; 21 22 import javax.swing.*; 23 import java.awt.event.ActionEvent ; 24 import java.awt.event.ActionListener ; 25 import java.util.Collections ; 26 27 32 public abstract class ItemOptionHelper implements ActionListener , Refreshable { 33 34 private final AbstractButton[] buttons; 35 private final AbstractButton unmatchedOption; 36 private XmlMultiViewDataSynchronizer synchronizer; 37 38 47 public ItemOptionHelper(XmlMultiViewDataSynchronizer synchronizer, ButtonGroup group) { 48 49 this.synchronizer = synchronizer; 50 buttons = (AbstractButton[]) Collections.list(group.getElements()).toArray(new AbstractButton[0]); 51 AbstractButton unmatchedOption = null; 52 for (int i = 0; i < buttons.length; i++) { 53 final AbstractButton button = buttons[i]; 54 button.addActionListener(this); 55 if (getOptionText(button) == null) { 56 unmatchedOption = button; 57 } 58 } 59 this.unmatchedOption = unmatchedOption; 60 setOption(getItemValue()); 61 } 62 63 66 public final void actionPerformed(ActionEvent e) { 67 final String option = getOption(); 68 if (!option.equals(getItemValue())) { 69 setItemValue(getOption()); 70 synchronizer.requestUpdateData(); 71 } 72 } 73 74 82 public void setOption(String itemValue) { 83 AbstractButton matchingButton = getMatchingButton(itemValue); 84 if (matchingButton != null && !matchingButton.isSelected()) { 85 matchingButton.setSelected(true); 86 } 87 return; 88 } 89 90 private AbstractButton getMatchingButton(String itemValue) { 91 AbstractButton matchingButton = null; 92 for (int i = 0; i < buttons.length; i++) { 93 final AbstractButton button = buttons[i]; 94 if (getOptionText(button).equals(itemValue)) { 95 matchingButton = button; 96 break; 97 } 98 } 99 if (matchingButton == null && unmatchedOption != null) { 100 matchingButton = unmatchedOption; 101 } 102 return matchingButton; 103 } 104 105 private String getOptionText(AbstractButton button) { 106 String fixedValue = (String )button.getClientProperty(PROPERTY_FIXED_VALUE); 107 if (fixedValue!=null) return fixedValue; 108 else return button.getText(); 109 } 110 111 117 public String getOption() { 118 for (int i = 0; i < buttons.length; i++) { 119 AbstractButton button = buttons[i]; 120 if (button.isSelected()) { 121 return getOptionText(button); 122 } 123 } 124 return null; 125 } 126 127 132 public abstract String getItemValue(); 133 134 139 public abstract void setItemValue(String value); 140 141 public void refresh() { 142 setOption(getItemValue()); 143 } 144 } 145 | Popular Tags |