1 package net.suberic.pooka.gui.search; 2 import net.suberic.pooka.*; 3 import javax.swing.*; 4 import java.awt.event.*; 5 import java.awt.Dimension ; 6 import java.util.Vector ; 7 import java.util.HashMap ; 8 9 13 public class SearchFolderPanel extends JPanel { 14 15 Vector selected = new Vector (); 16 boolean isEditable = true; 17 JList folderListSelector; 18 java.util.HashMap labelToFolderMap; 19 20 25 public SearchFolderPanel() { 26 this(new FolderInfo[0], Pooka.getStoreManager().getAllOpenFolders()); 27 } 28 29 30 35 public SearchFolderPanel(StoreInfo[] storeList) { 36 this(storeList, Pooka.getStoreManager().getAllOpenFolders()); 37 } 38 39 43 public SearchFolderPanel(StoreInfo[] storeList, Vector allowedValues) { 44 selected = new Vector (); 45 for (int i = 0; i < storeList.length; i++) 46 selected.addAll(storeList[i].getAllFolders()); 47 48 if (!isEditable) { 49 createStaticPanel(selected); 50 } else { 51 createDynamicPanel(selected, allowedValues); 52 } 53 } 54 55 60 public SearchFolderPanel(FolderInfo[] folderList) { 61 this(folderList, Pooka.getStoreManager().getAllOpenFolders()); 62 } 63 64 68 public SearchFolderPanel(FolderInfo[] folderList, Vector allowedValues) { 69 for (int i = 0; i < folderList.length; i++) { 70 selected.add(folderList[i]); 71 } 72 if (!isEditable) { 73 createStaticPanel(selected); 74 } else { 75 createDynamicPanel(selected, allowedValues); 76 } 77 } 78 79 82 public void createStaticPanel(Vector folderList) { 83 StringBuffer msg = new StringBuffer (); 84 msg.append("Searching folders:\n"); 85 for (int i = 0; i < folderList.size(); i++) 86 msg.append(((FolderInfo)folderList.elementAt(i)).getFolderID() + "\n"); 87 JTextArea label = new JTextArea(msg.toString()); 88 this.add(label); 89 } 90 91 94 public void createDynamicPanel(Vector folderList, Vector allowedValues) { 95 Vector folderNameList = new Vector (); 96 labelToFolderMap = new java.util.HashMap (); 97 for (int i = 0; i < allowedValues.size(); i++) { 98 FolderInfo folder = (FolderInfo) allowedValues.elementAt(i); 99 String name = folder.getFolderID(); 100 folderNameList.add(name); 101 labelToFolderMap.put(name, folder); 102 } 103 104 folderListSelector = new JList(folderNameList); 105 JScrollPane scroller = new JScrollPane(folderListSelector, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS); 106 111 Dimension scrollerPrefSize = new Dimension (500, 100); 112 scroller.setPreferredSize(scrollerPrefSize); 113 114 this.add(scroller); 115 116 folderListSelector.setValueIsAdjusting(true); 117 for (int i = 0 ; i < folderList.size(); i++) { 119 String folderId = ((FolderInfo)folderList.elementAt(i)).getFolderID(); 120 if (folderListSelector.getSelectedIndex() == -1) { 121 folderListSelector.setSelectedValue(folderId, true); 122 } else { 123 int index = folderNameList.indexOf(folderId); 124 if (index != -1) 125 folderListSelector.addSelectionInterval(index, index); 126 } 127 } 128 folderListSelector.setValueIsAdjusting(false); 129 } 130 131 134 public Vector getSelectedFolders() { 135 if (! isEditable) { 136 return selected; 137 } else { 138 Vector returnValue = new Vector (); 140 Object [] selectedValues = folderListSelector.getSelectedValues(); 141 for (int i = 0; i < selectedValues.length; i++) { 142 String currentSelection = (String ) selectedValues[i]; 143 FolderInfo currentFolder = (FolderInfo) labelToFolderMap.get(currentSelection); 144 returnValue.add(currentFolder); 145 } 146 147 return returnValue; 148 } 149 } 150 151 } 152 | Popular Tags |