1 package com.ca.commons.cbutil; 2 3 import javax.swing.*; 4 import javax.swing.border.TitledBorder ; 5 import java.awt.*; 6 import java.awt.event.ActionEvent ; 7 import java.awt.event.ActionListener ; 8 import java.util.Properties ; 9 import java.util.Vector ; 10 import java.util.logging.Level ; 11 import java.util.logging.Logger ; 12 13 14 37 38 public class CBSaveLoadTemplate extends JPanel 39 { 40 protected CBButton save, delete, makeDefault; 41 protected CBJComboBox loadops; 42 43 public Properties templates; String configFile; 48 int numTemplates; 50 private boolean saveFlag = false; 52 Vector illegalComponents = new Vector (); 54 static final String NUMTEMPLATES = "number_of_templates"; 55 static final String TEMPLATENAME = "template_name"; 56 static final String DEFAULT = "default"; 57 58 private static Logger log = Logger.getLogger(CBSaveLoadTemplate.class.getName()); 59 60 64 65 public CBSaveLoadTemplate(String fileName) 66 { 67 save = new CBButton(CBIntText.get("Save"), CBIntText.get("Click here to save current settings.")); 68 makeDefault = new CBButton(CBIntText.get("Default"), CBIntText.get("Click here to make the current setting the default.")); 69 delete = new CBButton(CBIntText.get("Delete"), CBIntText.get("Click here to delete a previously saved setting.")); 70 loadops = new CBJComboBox(); 71 templates = new Properties (); 72 73 configFile = parseFile(fileName); 74 75 CBPanel main = new CBPanel(); 76 77 main.add(save); 78 main.makeHeavy(); 79 main.add(loadops); 80 main.makeLight(); 81 main.add(delete); 82 main.add(makeDefault); 83 84 setBorder(new TitledBorder (CBIntText.get(" Use a Template "))); 85 setLayout(new BorderLayout()); 86 add(main); 87 88 save.addActionListener(new ActionListener () 89 { 90 public void actionPerformed(ActionEvent e) 91 { 92 saveFlag = true; 93 save(); 94 } 95 }); 96 97 loadops.addActionListener(new ActionListener () 98 { 99 public void actionPerformed(ActionEvent e) 100 { 101 if (!saveFlag) load(); 103 saveFlag = false; 104 } 105 }); 106 loadops.setToolTipText(CBIntText.get("Click here to load a previously saved setting.")); 107 108 delete.addActionListener(new ActionListener () 109 { 110 public void actionPerformed(ActionEvent e) 111 { 112 int response = showMessage(); 113 114 if (response != JOptionPane.YES_OPTION) return; 116 117 delete(); 118 } 119 }); 120 121 makeDefault.addActionListener(new ActionListener () 122 { 123 public void actionPerformed(ActionEvent e) 124 { 125 makeDefault(); 126 } 127 }); 128 } 129 130 131 134 135 public CBJComboBox getLoadComboBox() 136 { 137 return loadops; 138 } 139 140 public CBButton getSaveButton() 141 { 142 return save; 143 } 144 145 150 151 public int showMessage() 152 { 153 return JOptionPane.showConfirmDialog(this, CBIntText.get("Are you sure you want to delete the template?"), 154 CBIntText.get("Delete Confirmation"), JOptionPane.YES_NO_OPTION); 155 } 156 157 158 163 public void loadDefault() 164 { 165 String defaultName = templates.getProperty(DEFAULT); 166 if ((defaultName != null) && (defaultName.length() != 0)) 167 loadTemplateName(defaultName); 168 } 169 170 171 176 177 protected String parseFile(String fileName) 178 { 179 String configFile = CBUtility.getPropertyConfigPath(fileName); 180 if (configFile == null) 181 { 182 CBUtility.error(this, "Unable to read user home directory ", null); 183 return fileName; 184 } 185 186 templates = CBUtility.readPropertyFile(configFile); 187 if (templates.size() == 0) 188 { 189 log.info("Initialising config file: " + configFile); 190 return configFile; 191 } 192 193 String temp = templates.getProperty(NUMTEMPLATES); 194 if (temp == null) 195 { 196 CBUtility.error(this, "Unable to read number of templates paramater from: " + configFile, null); 197 return configFile; 198 } 199 200 201 numTemplates = Integer.parseInt(temp); 202 203 for (int i = 0; i < numTemplates; i++) 204 { 205 temp = templates.getProperty(TEMPLATENAME + i); 206 loadops.addItem(temp); 207 } 208 String defaultName = templates.getProperty(DEFAULT); 210 loadops.setSelectedItem(defaultName); 211 212 return configFile; 213 } 214 215 public String getCurrentTemplateName() 216 { 217 String currentTemplateName = (String ) loadops.getSelectedItem(); 218 if (currentTemplateName == null) return ""; 219 return currentTemplateName; 220 } 221 222 228 229 public void save() 230 { 231 String currentTemplateName = getCurrentTemplateName(); 232 String templateName = (String ) JOptionPane.showInputDialog(this, CBIntText.get("Enter template name") + ":", CBIntText.get("Replace/Create Template"), JOptionPane.QUESTION_MESSAGE, null, null, currentTemplateName); 233 if (templateName == null) return; saveTemplateName(templateName); 235 loadops.setSelectedItem(templateName); } 237 238 public void saveTemplateName(String templateName) 239 { 240 templateName = templateName.replace('.', '-'); if (templateName == null) return; Container parent = getParent(); 243 saveContainerInfo(parent, templateName); 245 251 boolean we_already_got_one = false; 252 for (int i = 0; i < numTemplates; i++) 253 { 254 String test = (String ) templates.getProperty(TEMPLATENAME + i); 255 if ((test != null) && (test.equals(templateName))) 256 { 257 we_already_got_one = true; 258 break; 259 } 260 } 261 262 if (we_already_got_one == false) 263 { 264 templates.setProperty(TEMPLATENAME + numTemplates, templateName); 265 numTemplates++; 266 templates.setProperty(NUMTEMPLATES, Integer.toString(numTemplates)); 267 loadops.addItem(templateName); 268 } 269 270 CBUtility.writePropertyFile(configFile, templates, ""); 271 272 } 273 274 280 281 public void saveContainerInfo(Container myContainer, String templateName) 282 { 283 if (myContainer == null) 284 { 285 log.warning("Unexpected error in CBSaveLoadTemplate.save() - no parent found"); 286 return; 287 } Component[] components = myContainer.getComponents(); 289 for (int i = 0; i < components.length; i++) 290 { 291 Component c = components[i]; 292 293 saveComponent(c, i, templateName); 294 295 } 296 } 297 298 308 309 protected void saveComponent(Component c, int componentNo, String templateName) 310 { 311 if ((c instanceof JPanel) && (c != this)) { 313 saveContainerInfo((Container) c, templateName + "." + componentNo); 314 } 315 else if ((c instanceof JScrollPane) && (c != this)) 316 { 317 saveContainerInfo((Container) c, templateName + "." + componentNo); 318 } 319 else if ((c instanceof JViewport) && (c != this)) 320 { 321 saveContainerInfo((Container) c, templateName + "." + componentNo); 322 } 323 else 324 { 325 String saveText = getComponentText(c); 326 if (saveText != null) 327 templates.setProperty(templateName + "." + componentNo, saveText); 328 } 329 } 330 331 332 342 public String getComponentText(Component c) 343 { 344 if (c == null) return null; 346 if (illegalComponents.contains(c)) return null; 348 try 349 { 350 if (c instanceof JPasswordField) return ""; 352 else if (c instanceof JTextField) 353 return ((JTextField) c).getText(); 354 else if (c instanceof JTextArea) 355 return ((JTextArea) c).getText(); 356 else if (c instanceof TextField) 357 return ((TextField) c).getText(); 358 else if (c instanceof JToggleButton) 359 return String.valueOf(((JToggleButton) c).isSelected()); 360 else if (c instanceof CBJComboBox) 361 return ((CBJComboBox) c).getSelectedItem().toString(); 362 else 363 return null; } 365 catch (Exception e) 366 { 367 return null; 368 } } 370 371 372 377 378 public void load() 379 { 380 String templateName = getCurrentTemplateName(); 381 382 if (templateName.length() == 0) 383 { 384 CBUtility.error(this, CBIntText.get("No template selected!"), null); 385 return; 386 } 387 loadTemplateName(templateName); 388 } 389 390 public void loadTemplateName(String templateName) 391 { 392 Container parent = getParent(); 393 394 loadContainerInfo(parent, templateName); 395 } 396 397 public void loadContainerInfo(Container myContainer, String templateName) 398 { 399 if (myContainer == null) 400 { 401 log.warning("Unexpected error in CBSaveLoadTemplate.load() - no parent found"); 402 return; 403 } Component[] components = myContainer.getComponents(); 405 406 for (int i = 0; i < components.length; i++) 407 { 408 Component c = components[i]; 409 loadComponent(c, i, templateName); 410 } 411 } 412 413 414 424 425 protected void loadComponent(Component c, int componentNo, String templateName) 426 { 427 if (c instanceof JPanel) { 429 loadContainerInfo((Container) c, templateName + "." + componentNo); 430 } 431 else if ((c instanceof JScrollPane)) 432 { 433 loadContainerInfo((Container) c, templateName + "." + componentNo); 434 } 435 else if ((c instanceof JViewport)) 436 { 437 loadContainerInfo((Container) c, templateName + "." + componentNo); 438 } 439 else 440 { 441 String text = (String ) templates.get(templateName + "." + componentNo); if (text != null) { 444 loadComponentText(c, text); 445 } 446 } 447 } 448 449 public void loadComponentText(Component c, String text) 450 { 451 if (illegalComponents.contains(c)) return; 453 if (c instanceof JTextField) 454 ((JTextField) c).setText(text); 455 else if (c instanceof JTextArea) 456 ((JTextArea) c).setText(text); 457 else if (c instanceof TextField) 458 ((TextField) c).setText(text); 459 else if (c instanceof JToggleButton) 460 ((JToggleButton) c).setSelected("true".equalsIgnoreCase(text)); 461 else if (c instanceof CBJComboBox) 462 { 463 ((CBJComboBox) c).setSelectedItem(text); 464 } 465 } 466 467 public void delete() 468 { 469 String templateName = getCurrentTemplateName(); 470 if (templateName.length() == 0) 471 { 472 CBUtility.error(this, "No template selected!", null); 473 return; 474 } 475 476 Container parent = getParent(); 477 if (parent == null) 478 { 479 log.warning("Unexpected error in CBSaveLoadTemplate.delete() - no parent found"); 480 return; 481 } deleteComponentInfo(parent, templateName); 483 484 for (int i = 0; i < numTemplates; i++) 485 { 486 if (templateName.equals((String ) templates.get(TEMPLATENAME + i))) 487 { 488 templates.remove(TEMPLATENAME + i); 489 numTemplates--; 490 templates.put(NUMTEMPLATES, Integer.toString(numTemplates)); 491 loadops.removeItem(templateName); 492 493 for (int j = i + 1; j <= numTemplates; j++) 496 { 497 templateName = (String ) templates.get(TEMPLATENAME + j); 498 templates.put(TEMPLATENAME + (j - 1), templateName); 499 } 500 templates.remove(TEMPLATENAME + numTemplates); 501 502 break; 503 } 504 } 505 CBUtility.writePropertyFile(configFile, templates, null); 506 } 507 508 public void deleteComponentInfo(Container myContainer, String templateName) 509 { 510 Component[] components = myContainer.getComponents(); 511 512 513 for (int i = 0; i < components.length; i++) 515 { 516 if (components[i] instanceof JPanel) 517 deleteComponentInfo((Container) components[i], templateName + "." + i); 518 else if ((components[i] instanceof JScrollPane)) 519 deleteComponentInfo((Container) components[i], templateName + "." + i); 520 else if ((components[i] instanceof JViewport)) 521 deleteComponentInfo((Container) components[i], templateName + "." + i); 522 else 523 deleteComponentInfo(templateName + "." + i); } 525 526 try 527 { if (templates.getProperty("default") != null && templates.getProperty("default").equalsIgnoreCase(templateName)) 529 templates.remove("default"); 530 } 531 catch (Exception e) 532 { 533 log.log(Level.WARNING, "No default template. ", e); 534 } 535 } 536 537 538 544 545 public void deleteComponentInfo(String templateName) 546 { 547 templates.remove(templateName); 548 } 549 550 public void makeDefault() 551 { 552 templates.setProperty(DEFAULT, getCurrentTemplateName()); 553 CBUtility.writePropertyFile(configFile, templates, ""); 554 } 555 556 public void addIllegalComponent(Component c) 557 { 558 illegalComponents.add(c); 559 } 560 561 } | Popular Tags |