1 19 20 package org.netbeans.modules.autoupdate; 21 22 import java.awt.Dialog ; 23 import java.awt.Component ; 24 import java.awt.Dimension ; 25 import java.beans.PropertyChangeListener ; 26 import java.beans.PropertyChangeEvent ; 27 import java.util.Collection ; 28 import java.util.HashMap ; 29 import java.util.Enumeration ; 30 import java.util.Iterator ; 31 import java.util.Set ; 32 import java.util.ArrayList ; 33 import java.util.List ; 34 import java.util.HashSet ; 35 import javax.swing.event.EventListenerList ; 36 import java.io.File ; 37 import java.util.logging.Level ; 38 import java.util.logging.Logger ; 39 import javax.swing.JPanel ; 40 41 import org.openide.WizardDescriptor; 42 import org.openide.NotifyDescriptor; 43 import org.openide.DialogDescriptor; 44 import org.openide.DialogDisplayer; 45 import org.openide.awt.StatusDisplayer; 46 import org.openide.util.HelpCtx; 47 import org.openide.util.NbBundle; 48 import org.openide.util.RequestProcessor; 49 50 55 public class Wizard extends Object implements WizardDescriptor.Iterator { 56 57 58 private Updates updates; 59 60 61 private static HashMap allUpdates = new HashMap (); 62 63 private static int errorType = Updates.NO_ERROR; 64 private static String errorMessage; 65 66 private static RequestProcessor processor; 67 private static final Logger LOG = Logger.getLogger ("org.netbeans.modules.autoupdate.Wizard"); 69 70 private WizardPanel[][] panels = new WizardPanel[][] { 71 { new StartPanel(), 72 new PropPanel(), 73 null, 74 new ConfigPanel(), 75 new DownloadPanel(), 76 new LastPanel() 77 }, 78 79 { null, 80 null, 81 new SelectPanel(), 82 null, 83 null, 84 null 85 } 86 }; 87 88 89 private int current = 0; 90 91 private int modulesOK = 0; 92 93 private boolean canceled = false; 94 95 96 private int wizardType = 0; 97 98 99 private WizardDescriptor wizardDescriptor; 100 101 private List auTypesWithCust = new ArrayList (); 102 103 private HashMap installNow = new HashMap (); 104 105 Wizard() { 106 this( null ); 107 } 108 109 110 Wizard( HashMap allUpd ) { 111 this( allUpd, 0 ); 112 } 113 114 private PropertyChangeListener updater_listener = 115 new PropertyChangeListener () { 116 public void propertyChange(PropertyChangeEvent event) { 117 if (event.getPropertyName().equals("FINISHED")) { 118 Wizard.this.updaterFinished(); 119 } 120 } 121 }; 122 123 private void updaterFinished() { 124 if ( SafeModule.write( installNow ) ) { 125 final String message = getBundle( "MSG_Install_Finished" ); StatusDisplayer.getDefault().setStatusText(message); 127 128 RequestProcessor.getDefault ().post (new Runnable () { 130 public void run () { 131 if (message.equals (StatusDisplayer.getDefault ().getStatusText ())) { 132 StatusDisplayer.getDefault ().setStatusText (""); } 134 } 135 }, 5000); 136 } 137 else 138 finishDialog( true ); 139 } 140 141 private void finishDialog(boolean all) { 142 if ( FinishDialog.showDialog() ) { 143 Collection modules = getAllModules(); 144 Iterator it = modules.iterator(); 145 while( it.hasNext() ) { 146 ModuleUpdate mu = (ModuleUpdate)it.next(); 147 148 if ( mu.isInstallApproved() && mu.isDownloadOK() && ( all || !mu.isSafeToInstall() ) ) { 149 if (!mu.isSafeToInstall () && mu.isToInstallDir ()) { 150 if ( !Downloader.tryMove( mu ) ) { 151 try { 152 mu.setToInstallDir( false ); 153 } catch (IllegalArgumentException iae) { 154 LOG.log(Level.WARNING, null, iae); 155 } 156 } 157 } 158 159 PreparedModules.getPrepared().addModule( mu ); 160 } 161 } 162 if ( FinishDialog.isRestart() ) { 163 PreparedModules.getPrepared().delete(); 164 Autoupdater.restart(); 165 } 166 else { 167 PreparedModules.getPrepared().write(); 168 } 169 } 170 else { 171 Downloader.deleteDownload(); 172 } 173 } 174 175 176 Wizard( HashMap allUpd, int wizardType ) { 177 178 this.wizardType = wizardType; 179 180 182 PropertyChangeListener listener = new PropertyChangeListener () { 183 public void propertyChange(PropertyChangeEvent event) { 184 if (event.getPropertyName().equals(DialogDescriptor.PROP_VALUE)) { 185 Object option = event.getNewValue(); 186 187 if (option == WizardDescriptor.FINISH_OPTION || 188 option == NotifyDescriptor.CANCEL_OPTION || 189 option == NotifyDescriptor.CLOSED_OPTION) { 190 if ( option != WizardDescriptor.FINISH_OPTION ) { 191 canceled = true; 192 Downloader.deleteDownload(); 193 } 194 getCurrent().end( true ); 195 Autoupdater.Support.deleteTempDir(); 196 allUpdates.clear(); 197 } 198 } 199 } 200 }; 201 202 Updates.reset(); 203 if ( allUpd != null ) { 204 int ist = 3; 205 206 allUpdates = allUpd; 207 current = ist; 208 if ( wizardType == 0 ) 209 panels[0][ist].start( true ); 210 } 211 212 wizardDescriptor = new WizardDescriptor( this ); wizardDescriptor.setModal( false ); 214 wizardDescriptor.setTitle(getBundle( "CTL_Wizard")); 215 wizardDescriptor.setTitleFormat (new java.text.MessageFormat (" {0}")); wizardDescriptor.setAdditionalOptions (new Object [] { }); 217 wizardDescriptor.addPropertyChangeListener(listener); 218 wizardDescriptor.putProperty ("WizardPanel_helpDisplayed", Boolean.FALSE); 219 wizardDescriptor.setOptions (new Object [] { 220 WizardDescriptor.PREVIOUS_OPTION, 221 WizardDescriptor.NEXT_OPTION, 222 WizardDescriptor.FINISH_OPTION, 223 NotifyDescriptor.CANCEL_OPTION } ); 224 } 225 226 227 static void go( HashMap allUpd ) { 228 go( allUpd, 0 ); 229 } 230 231 232 static Wizard go( HashMap allUpd, int wizardType ) { 233 234 Wizard wizard = new Wizard ( allUpd, wizardType ); 235 wizard.startDialog(); 236 237 return wizard; 238 } 239 240 void refreshUpdatePanel() { 241 if ( current == 3 ) 242 panels[0][current].start( true ); 243 } 244 245 246 public static void go() { 247 new Wizard ().startDialog (); 248 } 249 250 private void startDialog() { 251 Dialog dialog = DialogDisplayer.getDefault().createDialog( wizardDescriptor ); 252 253 canceled = false; 254 255 dialog.setVisible (true); 256 dialog.toFront(); 257 } 258 259 261 public String name() { 262 return getCurrent().getName(); 263 } 264 265 public WizardDescriptor.Panel current() { 266 267 return getCurrent(); 268 272 } 273 274 private WizardPanel getCurrent() { 275 276 return panels[ wizardType ][ current ] == null ? 277 panels[ 0 ][ current ] : panels[ wizardType ][ current ]; 278 } 279 280 public boolean hasNext() { 281 282 if ( current == 1 && getCurrent().nextPanelOffset() == -1 ) 283 return false; 284 else 285 return current < panels[wizardType].length - 1; 286 } 287 288 public boolean hasPrevious() { 289 290 return current > 0; 291 } 292 293 public void nextPanel() { 294 295 getCurrent().end( true ); 296 297 if ( current < 0 && wizardType == 0 ) 298 current = current + 2; 299 else if ( current == 0 && wizardType == 1 ) 300 current = 2; 301 else if ( current == 0 && wizardType == 0 && auTypesWithCust.size() == 0 ) 302 current = 3; 303 else if ( current == 1 && wizardType == 0 && auTypesWithCust.size() == 0 ) 304 current = current + 2; 305 else if ( current == 1 && wizardType == 0 && auTypesWithCust.size() > 0 ) 306 current = 1; 307 else if ( current == 3 && getCurrent().nextPanelOffset() == 2 ) 308 current = 5; 309 else 310 current ++; 311 312 getCurrent().start( true ); 313 314 } 316 317 public void previousPanel() { 318 319 getCurrent().end( false ); 320 321 switch ( current ) { 322 case 2: 323 if ( wizardType == 1 ) 324 current = 0; 325 else 326 current--; 327 break; 328 case 3: 329 if ( wizardType == 0 ) 330 current = 0; 331 else 332 current--; 333 break; 334 case 5: 335 current = 3; 336 break; 337 default: 338 current --; 339 break; 340 } 341 342 getCurrent().start( false ); 343 } 345 346 public synchronized void addChangeListener(javax.swing.event.ChangeListener listener) {} 347 public synchronized void removeChangeListener(javax.swing.event.ChangeListener listener) {} 348 349 static HashMap getAllUpdates() { 350 return allUpdates; 351 } 352 353 static void setAllUpdates(HashMap allUpd) { 354 allUpdates = allUpd; 355 } 356 357 static Collection getAllModules() { 358 Set ret = new HashSet (); 359 Iterator it = getAllUpdates().values().iterator(); 360 while (it.hasNext()) { 361 Object modules = ((Updates)it.next()).getModules(); 362 if (modules != null) { 363 ret.addAll((Collection )modules); 364 } 365 } 366 return ret; 367 } 368 369 static RequestProcessor getRequestProcessor() { 370 if ( processor == null ) 371 processor = new RequestProcessor(); 372 return processor; 373 } 374 375 private static String getBundle( String key ) { 376 return NbBundle.getMessage( Wizard.class, key ); 377 } 378 379 381 static interface Validator { 382 383 public void setValid( boolean valid ); 385 386 } 387 388 abstract class WizardPanel implements WizardDescriptor.Panel, Validator { 389 390 protected Dimension WIZARD_SIZE = new Dimension ( 550, 400 ); 391 392 protected boolean valid = true; 393 394 395 private EventListenerList listenerList = new EventListenerList (); 396 397 void start( boolean forward ) {} 398 399 void end( boolean forward ) { } 400 401 402 int nextPanelOffset() { 403 return 1; 404 } 405 406 abstract String getName(); 407 408 410 public void setValid(boolean valid) { 411 this.valid = valid; 412 fireChangeListenerStateChanged( this ); 413 } 414 415 417 public boolean isValid() { 418 return valid; 419 } 420 421 public HelpCtx getHelp() { 422 return null; 423 } 424 425 public abstract Component getComponent(); 426 427 public void readSettings( Object settings ) {} 428 429 public void storeSettings( Object settings ) {} 430 431 434 public synchronized void addChangeListener(javax.swing.event.ChangeListener listener) { 435 listenerList.add (javax.swing.event.ChangeListener .class, listener); 436 } 437 438 441 public synchronized void removeChangeListener(javax.swing.event.ChangeListener listener) { 442 listenerList.remove (javax.swing.event.ChangeListener .class, listener); 443 } 444 445 449 protected void fireChangeListenerStateChanged(java.lang.Object param1) { 450 javax.swing.event.ChangeEvent e = null; 451 Object [] listeners = listenerList.getListenerList (); 452 for (int i = listeners.length-2; i>=0; i-=2) { 453 if (listeners[i]==javax.swing.event.ChangeListener .class) { 454 if (e == null) 455 e = new javax.swing.event.ChangeEvent (param1); 456 ((javax.swing.event.ChangeListener )listeners[i+1]).stateChanged (e); 457 } 458 } 459 } 460 } 461 462 class StartPanel extends WizardPanel { 463 464 FirstPanel firstPanel; 465 466 String getName() { 467 return getBundle( "CTL_StartPanel"); 468 } 469 470 472 public void setValid( boolean valid ) { 473 wizardType = ((FirstPanel)getComponent ()).getWizardType(); 474 } 475 476 public Component getComponent() { 477 if (firstPanel == null) { 478 firstPanel = new FirstPanel( this ) { 479 public Dimension getPreferredSize() { 480 return WIZARD_SIZE; 481 } 482 }; 483 } 484 return firstPanel; 485 } 486 487 public HelpCtx getHelp() { 488 return null; 489 } 491 492 void end( boolean forward ) { 493 allUpdates.clear(); 494 auTypesWithCust.clear(); 495 resetErrorStore (); 496 497 Downloader.deleteDownload(); 498 if ( wizardType == 0 ) { 499 if ( !canceled ) { 500 int sum = 0; 501 Enumeration en = AutoupdateType.autoupdateTypes(); 502 while (en.hasMoreElements()) { 504 AutoupdateType at = (AutoupdateType)en.nextElement(); 505 boolean hasCust = false; 506 if (at.isEnabled()) { 507 try { 508 if (java.beans.Introspector.getBeanInfo(at.getClass()) 509 .getBeanDescriptor().getCustomizerClass() != null) { 510 auTypesWithCust.add(at); 511 sum ++; 512 hasCust = true; 513 } 514 } catch (Exception e) { 515 } 516 if (! hasCust) { 517 updates = at.connectForUpdates(); 518 updates.checkUpdates( this, at ); 519 int res = checkConnect(updates, at); 520 if ( res == ConnectingDialog.OK ) { 521 sum ++; 522 allUpdates.put(at, updates); 523 } else if ( res == ConnectingDialog.CANCEL ) { 524 current = -2; 525 return; 526 } 527 } 528 } 529 } 530 if (sum == 0){ 531 if (isErrorStored ()) { 532 ConnectingErrorDialog.showDialog(errorType, errorMessage); 533 } else { 534 ConnectingErrorDialog.showDialog(Updates.NO_SERVER_ERROR, null); 535 } 536 current = -2; 537 return; 538 } 539 } 540 } 541 } 542 } 543 544 class PropPanel extends WizardPanel { 545 546 LoginPanel lPanel; 547 AutoupdateType currentAT; 548 JPanel custPanel; 549 550 HelpCtx userHelp = null; 551 552 String getName() { 553 return getBundle( "CTL_StartPanel"); 554 } 555 556 558 public void setValid( boolean valid ) { 559 } 560 561 public Component getComponent() { 562 if (lPanel == null) { 563 lPanel = new LoginPanel( this ) { 564 public Dimension getPreferredSize() { 565 return WIZARD_SIZE; 566 } 567 }; 568 } 569 return lPanel; 570 } 571 572 public HelpCtx getHelp() { 573 if ( userHelp != null ) 574 return userHelp; 575 else 576 return null; 577 } 579 580 void start( boolean forward ) { 581 if ( custPanel != null ) 582 return; 583 currentAT = (AutoupdateType)auTypesWithCust.get(0); 584 userHelp = null; 585 try { 586 custPanel = (javax.swing.JPanel )(java.beans.Introspector.getBeanInfo(currentAT.getClass()) 587 .getBeanDescriptor().getCustomizerClass().newInstance()); 588 ((LoginPanel)getComponent ()).setCustomizer(custPanel, currentAT); 589 userHelp = (HelpCtx) custPanel.getClientProperty( "Autoupdate_HelpID" ); } catch (Exception e) { 591 } 592 } 593 594 void end( boolean forward ) { 595 if ( wizardType == 0 ) { 597 Downloader.deleteDownload(); 598 if ( !canceled && forward ) { 599 updates = currentAT.connectForUpdates(); 600 updates.checkUpdates( this, currentAT ); 601 int res = checkConnect(updates, currentAT); 602 if ( res == ConnectingDialog.OK ) 603 allUpdates.put(currentAT, updates); 604 else if ( res == ConnectingDialog.CANCEL ) { 605 if ( updates.isError() ) { 606 current = -1; 607 return; 608 } 609 else 610 current = -2; 611 } 612 } 613 auTypesWithCust.remove(0); 614 custPanel = null; 615 } 616 } 617 618 } 619 620 static void resetErrorStore () { 621 errorMessage = null; 622 errorType = Updates.NO_ERROR; 623 } 624 625 static private void storeError (int type, String msg) { 626 if (type == Updates.NO_ERROR) { 627 throw new IllegalArgumentException ("Type Updates.NO_ERROR is not error."); 628 } 629 errorType = type; 630 errorMessage = msg; 631 } 632 633 static boolean isErrorStored () { 634 return errorType != Updates.NO_ERROR; 635 } 636 637 static int getStoredErrorType () { 638 return errorType; 639 } 640 641 static String getStoredErrorMessage () { 642 return errorMessage; 643 } 644 645 static int checkConnect(Updates updates, AutoupdateType at) { 646 if (ConnectingDialog.isCanceled()) { 647 return ConnectingDialog.CANCEL; 648 } 649 else if (ConnectingDialog.isSkipped()) { 650 return ConnectingDialog.SKIP; 651 } 652 else if ( updates.isError() ) { 653 storeError (updates.getError(), updates.getErrorMessage ()); 654 return ConnectingDialog.SKIP; 655 } 656 else if ( updates.getModules() != null && updates.getModules().size() > 0 ) 657 Notification.performNotification( updates, at ); 658 return ConnectingDialog.OK; 659 } 660 661 class ConfigPanel extends WizardPanel { 662 663 private UpdatePanel updatePanel = null; 664 665 String getName() { 666 return getBundle( "CTL_ConfigPanel"); 667 } 668 669 public Component getComponent() { 670 if (updatePanel == null) { 671 updatePanel = new UpdatePanel( this ) { 672 public Dimension getPreferredSize() { 673 return WIZARD_SIZE; 674 } 675 }; 676 } 677 return updatePanel; 678 } 679 680 public HelpCtx getHelp() { 681 return null; 682 } 684 685 public boolean isValid() { 686 return valid; 687 } 688 689 void start( boolean forward ) { 690 valid = false; 691 if ( forward ) { 692 PreparedModules.readPrepared(); 693 if (wizardType == 0) { 694 Iterator atypes = allUpdates.keySet().iterator(); 695 while ( atypes.hasNext() ) { 696 AutoupdateType at = (AutoupdateType)atypes.next(); 697 Updates upd = (Updates)allUpdates.get( at ); 698 at.setLastTimeStamp( upd.getTimeStamp() ); 699 } 700 } 701 } 702 ((UpdatePanel)getComponent ()).setUpdates( wizardType ); 703 } 704 705 void end( boolean forward ) { 706 if ( forward ) { 707 ((UpdatePanel)getComponent ()).markSelectedModules(); 708 if ( !canceled ) 709 if ( !((UpdatePanel)getComponent ()).checkLicencies() ) 710 current--; 711 } 712 ((UpdatePanel)getComponent ()).removeListeners(); 713 } 714 715 int nextPanelOffset() { 716 if ( ((UpdatePanel)getComponent ()).modulesToDownload() == 0 ) 717 return 2; 718 else 719 return 1; 720 } 721 722 void setUpdates(HashMap allUpd) { 723 allUpdates.clear(); 724 allUpdates = allUpd; 725 } 726 727 } 728 729 class DownloadPanel extends WizardPanel { 730 731 private Downloader downloader; 732 private SignVerifier signVerifier; 733 734 private DownloadProgressPanel progressPanel = null; 735 736 private boolean isDownloadFinished; 737 738 String getName() { 739 return getBundle( wizardType == 0 ? "CTL_DownloadPanel" : "CTL_CopyPanel" ); } 741 742 public Component getComponent() { 743 if (progressPanel == null) { 744 progressPanel = new DownloadProgressPanel(this) { 745 public Dimension getPreferredSize() { 746 return WIZARD_SIZE; 747 } 748 }; 749 } 750 return ( Component )progressPanel; 751 } 752 753 public HelpCtx getHelp() { 754 return null; 755 } 757 758 void start( boolean forward ) { 759 valid = false; 760 isDownloadFinished = false; 761 progressPanel = null; 763 downloader = new Downloader( ((DownloadProgressPanel)getComponent ()), this, wizardType == 0 ); 764 downloader.doDownload(); 765 } 767 768 void end( boolean forward ) { 769 if ( !valid ) { 770 if ( !isDownloadFinished ) { 771 downloader.cancelDownload(); 772 } else { 773 if (signVerifier != null) { 774 signVerifier.cancelVerify( canceled ); 775 } 776 } 777 } 778 } 779 780 } 781 782 class LastPanel extends WizardPanel { 783 784 ResultsPanel resultsPanel; 785 786 String getName() { 787 return getBundle( wizardType == 0 ? "CTL_ResultsPanel" : "CTL_ResultsPanel_1"); } 789 790 public Component getComponent() { 791 if (resultsPanel == null) { 792 resultsPanel = new ResultsPanel( this ) { 793 public Dimension getPreferredSize() { 794 return WIZARD_SIZE; 795 } 796 }; 797 } 798 return resultsPanel; 799 } 800 801 public HelpCtx getHelp() { 802 return null; 803 } 805 806 void start( boolean forward ) { 807 modulesOK = ((ResultsPanel)getComponent ()).generateResults(); 808 } 809 810 void end( boolean forward ) { 811 if ( wizardDescriptor.getValue() == WizardDescriptor.FINISH_OPTION 812 && modulesOK > 0 ) { 813 814 Collection modules = getAllModules(); 815 Iterator it = modules.iterator(); 816 installNow = new HashMap (); 817 int otherCount = 0; 818 while( it.hasNext() ) { 819 ModuleUpdate mu = (ModuleUpdate)it.next(); 820 821 if ( mu.isDownloadOK() && Downloader.getNBM( mu ).exists() ) { 822 if ( !mu.isInstallApproved() ) { 823 Downloader.getNBM( mu ).delete(); 824 Downloader.getNBM( mu ).deleteOnExit(); 825 } else { 826 if ( mu.isSafeToInstall() ) { 827 if ( mu.isToInstallDir() ) 828 if ( !Downloader.tryMove( mu ) ) { 829 try { 830 mu.setToInstallDir( false ); 831 } catch (IllegalArgumentException iae) { 832 LOG.log(Level.WARNING, null, iae); 833 } 834 } 835 installNow.put( mu, Downloader.getMovedNBM( mu ) ); 836 } 837 else { 838 otherCount++; 839 } 840 } 841 } 842 } 843 844 Autoupdater.Support.deleteTempDir(); 845 modulesOK = 0; 846 847 if ( installNow.size() > 0 && otherCount == 0 ) { 848 File [] filesInstallNow = new File []{}; 849 filesInstallNow = (File [])installNow.values().toArray(filesInstallNow); 850 org.netbeans.updater.UpdaterFrame.runFromIDE (filesInstallNow, updater_listener, NbBundle.getBranding ()); 851 } 852 else if ( otherCount > 0 ) { 853 finishDialog( false ); 854 } 855 else { 856 DialogDisplayer.getDefault().notify( 857 new NotifyDescriptor.Message( 858 getBundle( "MSG_WizardNoNbms"), 859 NotifyDescriptor.WARNING_MESSAGE 860 ) 861 ); 862 } 863 } 864 } 865 } 866 867 869 class SelectPanel extends WizardPanel { 870 871 private SelectModulesPanel selectModulesPanel = null; 872 873 String getName() { 874 return getBundle( "CTL_SelectModulesPanel"); 875 } 876 877 public Component getComponent() { 878 if (selectModulesPanel == null) { 879 selectModulesPanel = new SelectModulesPanel( this ) { 880 public Dimension getPreferredSize() { 881 return WIZARD_SIZE; 882 } 883 }; 884 } 885 return selectModulesPanel; 886 } 887 888 public HelpCtx getHelp() { 889 return null; 890 } 892 893 void start( boolean forward ) { 894 valid = false; 895 if ( forward ) 896 ((SelectModulesPanel)getComponent ()).reset(); 897 else 898 setValid( true ); 899 } 900 901 void end( boolean forward ) { 902 903 if ( forward && !canceled ) { 904 updates = new XMLUpdates( ((SelectModulesPanel)getComponent ()).getFiles() ); 905 ((XMLUpdates)updates).checkDownloadedModules(); 906 allUpdates.put(this, updates); 907 } 908 } 909 910 } 911 912 } 913 | Popular Tags |