1 package net.suberic.pooka.gui.search; 2 import net.suberic.pooka.*; 3 import net.suberic.util.VariableBundle; 4 import javax.swing.*; 5 import java.util.*; 6 import java.awt.event.*; 7 import java.awt.FlowLayout ; 8 import javax.mail.search.SearchTerm ; 9 10 16 public class SearchEntryPanel extends JPanel { 17 18 public static int FIRST = -1; 19 public static int AND = 0; 20 public static int OR = 1; 21 22 public static String AND_LABEL = Pooka.getProperty("Search.button.and.label", "And"); 23 public static String OR_LABEL = Pooka.getProperty("Search.button.or.label", "Or"); 24 25 JPanel conditionPanel; 26 JPanel entryPanel; 27 JScrollPane entryScrollPane; 28 JButton buttonOne, buttonTwo; 29 Vector searchTerms = new Vector(); 30 SearchTermManager manager; 31 32 class SearchEntryPair { 33 SearchEntryForm form; 34 SearchConnector connector; 35 36 public SearchEntryPair(SearchEntryForm newForm, int newType) { 37 form = newForm; 38 connector = new SearchConnector(newType); 39 } 40 } 41 42 class SearchConnector { 43 44 JComboBox list; 45 SearchConnector(int newType) { 46 String [] choices = new String [2]; 47 choices[0] = AND_LABEL; 48 choices[1] = OR_LABEL; 49 list = new JComboBox(choices); 50 51 if (newType < 2) { 52 53 list.setSelectedIndex(newType); 54 } else 55 list.setSelectedIndex(0); 56 57 } 58 59 public int getType() { 60 return list.getSelectedIndex(); 61 } 62 63 public JComboBox getCombo() { 64 return list; 65 } 66 67 } 68 69 72 public SearchEntryPanel(SearchTermManager newManager) { 73 manager = newManager; 74 populatePanel(); 75 } 76 77 81 public SearchEntryPanel(SearchTermManager newManager, String property) { 82 this(newManager, property, Pooka.getResources()); 83 } 84 85 89 public SearchEntryPanel(SearchTermManager newManager, String property, VariableBundle bundle) { 90 this(newManager); 91 setSearchTerm(property, bundle); 92 } 93 94 95 99 public void populatePanel() { 100 this.setLayout(new java.awt.BorderLayout ()); 101 entryPanel = new JPanel(); 102 entryPanel.setLayout(new BoxLayout(entryPanel,BoxLayout.Y_AXIS)); 103 104 addSearchEntryForm(FIRST); 105 106 createConditionPanel(); 107 108 entryScrollPane = new JScrollPane(entryPanel); 109 int defaultHeight = entryPanel.getPreferredSize().height; 110 entryScrollPane.setPreferredSize(new java.awt.Dimension (entryPanel.getPreferredSize().width + 15, defaultHeight * 5)); 111 112 this.add(conditionPanel, java.awt.BorderLayout.SOUTH); 113 this.add(entryScrollPane, java.awt.BorderLayout.CENTER); 114 115 } 116 117 122 private void createConditionPanel() { 123 JPanel jp = new JPanel(); 124 jp.setLayout(new FlowLayout ()); 125 126 buttonOne = new JButton(Pooka.getProperty("Search.button.and.label", "And")); 127 buttonOne.addActionListener(new ActionListener() { 128 public void actionPerformed(ActionEvent e) { 129 addSearchEntryForm(AND); 130 } 131 }); 132 133 buttonTwo = new JButton(Pooka.getProperty("Search.button.or.label", "Or")); 134 buttonTwo.addActionListener(new ActionListener() { 135 public void actionPerformed(ActionEvent e) { 136 addSearchEntryForm(OR); 137 } 138 }); 139 140 jp.add(buttonOne); 141 jp.add(buttonTwo); 142 143 conditionPanel=jp; 144 } 145 146 public void addSearchEntryForm(int type) { 147 if (type == FIRST) { 148 SearchEntryForm sef = new SearchEntryForm(manager); 149 SearchConnector sc = new SearchConnector(AND); 150 Box fullPanel = new Box(BoxLayout.X_AXIS); 154 searchTerms.add(new SearchEntryPair(sef, AND)); 155 fullPanel.add(sef.getPanel()); 157 entryPanel.add(fullPanel); 158 } else { 159 SearchEntryForm sef = new SearchEntryForm(manager); 160 SearchEntryPair pair = new SearchEntryPair(sef, type); 161 searchTerms.add(pair); 162 163 JPanel newSearchPanel = new JPanel(); 164 newSearchPanel.add(pair.connector.getCombo()); 165 newSearchPanel.add(sef.getPanel()); 166 entryPanel.add(newSearchPanel); 167 170 entryPanel.revalidate(); 171 Runnable runMe = new Runnable () { 172 public void run() { 173 JScrollBar vsb = entryScrollPane.getVerticalScrollBar(); 174 vsb.setValue(vsb.getMaximum()); 175 } 177 }; 178 179 SwingUtilities.invokeLater(runMe); 180 } 181 182 } 183 184 187 public SearchTerm getSearchTerm() throws java.text.ParseException { 188 if (Pooka.isDebug()) 189 System.out.println("calling SearchEntryPanel.getSearchTerm()"); 190 if (searchTerms.size() > 0) { 191 if (Pooka.isDebug()) 192 System.out.println("SearchEntryPanel: searchTerms.size() > 0."); 193 SearchEntryPair pair = (SearchEntryPair) searchTerms.elementAt(0); 194 SearchTerm term = pair.form.generateSearchTerm(); 195 if (Pooka.isDebug()) 196 System.out.println("SearchEntryPanel: setting term to " + term); 197 for (int i = 1; i < searchTerms.size(); i++) { 198 SearchEntryPair newPair = (SearchEntryPair) searchTerms.elementAt(i); 199 SearchTerm newTerm = newPair.form.generateSearchTerm(); 200 if (newPair.connector.getType() == AND) { 201 term = new javax.mail.search.AndTerm (term, newTerm); 202 } else if (newPair.connector.getType() == OR) { 203 term = new javax.mail.search.OrTerm (term, newTerm); 204 } 205 } 206 207 return term; 208 } else 209 return null; 210 } 211 212 216 public void setSearchTerm(String rootProperty, VariableBundle bundle) { 217 searchTerms = new Vector(); 218 entryPanel = new JPanel(); 219 entryPanel.setLayout(new BoxLayout(entryPanel,BoxLayout.Y_AXIS)); 220 addSearchTermProperty(rootProperty, bundle, FIRST); 221 entryScrollPane.setViewportView(entryPanel); 222 entryPanel.revalidate(); 223 } 224 225 230 private void addSearchTermProperty(String rootProperty, VariableBundle bundle, int type) { 231 String termType = bundle.getProperty(rootProperty + ".type", "simple"); 232 if (termType.equalsIgnoreCase("compound")) { 233 Vector subProps = bundle.getPropertyAsVector(rootProperty + ".subTerms", ""); 234 int subType = AND; 235 String operation = bundle.getProperty(rootProperty + ".operation", "and"); 236 if (operation.equalsIgnoreCase("and")) 237 subType = AND; 238 else 239 subType = OR; 240 241 for (int i = 0; i < subProps.size(); i++) { 242 String nextTerm = (String ) subProps.elementAt(i); 243 if (i == 0) 244 addSearchTermProperty(nextTerm, bundle, type); 245 else 246 addSearchTermProperty(nextTerm, bundle, subType); 247 } 248 } else { 249 addSingleSearchTerm(rootProperty, bundle, type); 250 } 251 } 252 253 256 private void addSingleSearchTerm(String rootProperty, VariableBundle bundle, int type) { 257 if (type == FIRST) { 258 SearchEntryForm sef = new SearchEntryForm(manager, rootProperty, bundle); 259 SearchConnector sc = new SearchConnector(AND); 260 Box fullPanel = new Box(BoxLayout.X_AXIS); 261 searchTerms.add(new SearchEntryPair(sef, AND)); 262 fullPanel.add(sef.getPanel()); 263 entryPanel.add(fullPanel); 264 } else { 265 SearchEntryForm sef = new SearchEntryForm(manager, rootProperty, bundle); 266 SearchEntryPair pair = new SearchEntryPair(sef, type); 267 searchTerms.add(pair); 268 269 JPanel newSearchPanel = new JPanel(); 270 newSearchPanel.add(pair.connector.getCombo()); 271 newSearchPanel.add(sef.getPanel()); 272 entryPanel.add(newSearchPanel); 273 } 274 } 275 276 280 public java.util.Properties generateSearchTermProperties(String rootProperty) { 281 java.util.Properties returnValue = new java.util.Properties (); 282 283 Vector v = new Vector (searchTerms); 284 addToProperties(v, rootProperty, returnValue); 285 return returnValue; 286 } 287 288 291 private void addToProperties(SearchEntryPair pair, String rootProperty, java.util.Properties props) { 292 Properties tmpProperties = pair.form.generateSearchTermProperties(rootProperty); 293 Enumeration keys = tmpProperties.keys(); 294 while (keys.hasMoreElements()) { 295 String current = (String ) keys.nextElement(); 296 props.setProperty(current, tmpProperties.getProperty(current)); 297 } 298 } 299 300 304 private void addToProperties(Vector pairList, String rootProperty, Properties props) { 305 if (pairList.size() == 1) { 306 addToProperties((SearchEntryPair) pairList.remove(0), rootProperty, props); 307 } else { 308 addToProperties((SearchEntryPair) pairList.remove(0), rootProperty + ".term1", props); 309 int type = ((SearchEntryPair) pairList.elementAt(0)).connector.getType(); 310 addToProperties(pairList, rootProperty + ".term2", props); 311 props.setProperty(rootProperty + ".type", "compound"); 312 props.setProperty(rootProperty + ".subTerms", rootProperty + ".term1:" + rootProperty + ".term2"); 313 if (type == AND) { 314 props.setProperty(rootProperty + ".operation", "and"); 315 } else { 316 props.setProperty(rootProperty + ".operation", "or"); 317 } 318 } 319 } 320 321 324 public void setEnabled(boolean newValue) { 325 for (int i = 0; i < searchTerms.size(); i++) { 326 SearchEntryPair currentPair = (SearchEntryPair) searchTerms.elementAt(i); 327 currentPair.form.setEnabled(newValue); 328 currentPair.connector.getCombo().setEnabled(newValue); 329 } 330 buttonOne.setEnabled(newValue); 331 buttonTwo.setEnabled(newValue); 332 333 } 334 } 335 | Popular Tags |