1 package com.ca.commons.cbutil; 2 3 import javax.swing.*; 4 import java.awt.*; 5 import java.awt.event.*; 6 import java.util.ArrayList ; 7 import java.util.logging.Level ; 8 import java.util.logging.Logger ; 9 10 11 18 19 public class CBListSelector extends CBDialog 20 { 21 JList availableList, selectedList; 22 JScrollPane availableScrollPane, selectedScrollPane; 23 CBButton btnAdd, btnRemove; 24 JLabel availableLabel, selectedLabel; 25 CBPanel leftPanel, middlePanel, rightPanel; 26 ArrayList arrayList = new ArrayList (); 27 28 private static Logger log = Logger.getLogger(CBListSelector.class.getName()); 29 30 44 45 public static ArrayList getSelectedValues(Frame owner, String [] list, String title, String helpID) 46 { 47 CBListSelector listSelector = new CBListSelector(owner, list, title, helpID); 48 49 listSelector.setVisible(true); 50 51 return listSelector.getSelectedValues(); 52 } 53 54 55 67 68 public CBListSelector(Frame owner, String [] list, String title, String helpID) 69 { 70 super(owner, CBIntText.get(title), helpID); 71 72 leftPanel = new CBPanel(); 73 middlePanel = new CBPanel(); 74 rightPanel = new CBPanel(); 75 76 78 leftPanel.addln(availableLabel = new JLabel(CBIntText.get("Available Attributes:"))); 79 leftPanel.makeHeavy(); 80 81 availableList = new JList(list); 82 availableList.setSelectionMode(0); availableList.setSelectionModel(new CBSingleSelectionModel(availableList)); 85 leftPanel.addln(availableScrollPane = new JScrollPane(availableList)); 86 87 89 btnAdd = new CBButton(CBIntText.get(">>"), CBIntText.get("Add an attribute from the attribute list on the left to the selection list on the right.")); 90 btnAdd.addActionListener(new ActionListener() 91 { 92 public void actionPerformed(ActionEvent e) 93 { 94 add(); 95 } 96 }); 97 98 btnRemove = new CBButton(CBIntText.get("<<"), CBIntText.get("Remove an attribute from the selection list on the right.")); 99 btnRemove.addActionListener(new ActionListener() 100 { 101 public void actionPerformed(ActionEvent e) 102 { 103 remove(); 104 } 105 }); 106 107 middlePanel.makeHeavy(); 108 middlePanel.addln(new JLabel(" ")); middlePanel.makeLight(); 110 middlePanel.addln(btnAdd); 111 middlePanel.addln(btnRemove); 112 middlePanel.makeHeavy(); 113 middlePanel.addln(new JLabel(" ")); 115 117 rightPanel.addln(selectedLabel = new JLabel(CBIntText.get("Selected Attributes:"))); 118 rightPanel.makeHeavy(); 119 120 selectedList = new JList(); 121 selectedList.setSelectionMode(0); selectedList.setSelectionModel(new CBSingleSelectionModel(selectedList)); 124 rightPanel.addln(selectedScrollPane = new JScrollPane(selectedList)); 125 126 128 display.addln(new JLabel(" ")); display.makeHeavy(); 130 display.add(leftPanel); 131 display.makeLight(); 132 display.add(middlePanel); 133 display.makeHeavy(); 134 display.add(rightPanel); 135 136 setSize(400, 300); 137 CBUtility.center(this, owner); 138 139 registerMouseListeners(); 140 } 141 142 143 149 150 protected void registerMouseListeners() 151 { 152 availableList.addMouseListener(new MouseAdapter() 153 { 154 public void mouseClicked(MouseEvent e) 155 { 156 if (e.getClickCount() == 2) 157 add(); 158 } 159 }); 160 161 selectedList.addMouseListener(new MouseAdapter() 162 { 163 public void mouseClicked(MouseEvent e) 164 { 165 if (e.getClickCount() == 2) 166 remove(); 167 } 168 }); 169 } 170 171 172 177 178 public void add() 179 { 180 try 181 { 182 if (!arrayList.contains(availableList.getSelectedValue())) 183 arrayList.add(availableList.getSelectedValue()); 184 185 selectedList.setListData(arrayList.toArray()); 186 } 187 catch (Exception e) 188 { 189 log.log(Level.FINER, "No selection to add.", e); 190 } 191 } 192 193 194 198 199 public void remove() 200 { 201 try 202 { 203 arrayList.remove(selectedList.getSelectedIndex()); 204 selectedList.setListData(arrayList.toArray()); 205 } 206 catch (Exception e) 207 { 208 log.log(Level.FINER, "No selection to remove.", e); 209 } 210 } 211 212 213 218 219 public ArrayList getSelectedValues() 220 { 221 if (arrayList.isEmpty()) 222 return null; 223 else 224 return arrayList; 225 } 226 } | Popular Tags |