PageRenderTime 27ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/projects/netbeans-7.3/autoupdate.ui/src/org/netbeans/modules/autoupdate/ui/wizards/OperationWizardModel.java

https://gitlab.com/essere.lab.public/qualitas.class-corpus
Java | 531 lines | 418 code | 45 blank | 68 comment | 84 complexity | 34a72093a9b6e172f544285413fb61be MD5 | raw file
  1. /*
  2. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
  3. *
  4. * Copyright 1997-2012 Oracle and/or its affiliates. All rights reserved.
  5. *
  6. * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
  7. * Other names may be trademarks of their respective owners.
  8. *
  9. * The contents of this file are subject to the terms of either the GNU
  10. * General Public License Version 2 only ("GPL") or the Common
  11. * Development and Distribution License("CDDL") (collectively, the
  12. * "License"). You may not use this file except in compliance with the
  13. * License. You can obtain a copy of the License at
  14. * http://www.netbeans.org/cddl-gplv2.html
  15. * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
  16. * specific language governing permissions and limitations under the
  17. * License. When distributing the software, include this License Header
  18. * Notice in each file and include the License file at
  19. * nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this
  20. * particular file as subject to the "Classpath" exception as provided
  21. * by Oracle in the GPL Version 2 section of the License file that
  22. * accompanied this code. If applicable, add the following below the
  23. * License Header, with the fields enclosed by brackets [] replaced by
  24. * your own identifying information:
  25. * "Portions Copyrighted [year] [name of copyright owner]"
  26. *
  27. * Contributor(s):
  28. *
  29. * The Original Software is NetBeans. The Initial Developer of the Original
  30. * Software is Sun Microsystems, Inc. Portions Copyright 1997-2008 Sun
  31. * Microsystems, Inc. All Rights Reserved.
  32. *
  33. * If you wish your version of this file to be governed by only the CDDL
  34. * or only the GPL Version 2, indicate your decision by adding
  35. * "[Contributor] elects to include this software in this distribution
  36. * under the [CDDL or GPL Version 2] license." If you do not indicate a
  37. * single choice of license, a recipient has the option to distribute
  38. * your version of this file under either the CDDL, the GPL Version 2 or
  39. * to extend the choice of license to its licensees as provided above.
  40. * However, if you add GPL Version 2 code and therefore, elected the GPL
  41. * Version 2 license, then the option applies only if the new code is
  42. * made subject to such option by the copyright holder.
  43. */
  44. package org.netbeans.modules.autoupdate.ui.wizards;
  45. import java.awt.Dimension;
  46. import java.util.ArrayList;
  47. import java.util.Arrays;
  48. import java.util.Collection;
  49. import java.util.Collections;
  50. import java.util.HashMap;
  51. import java.util.HashSet;
  52. import java.util.List;
  53. import java.util.Set;
  54. import java.util.SortedMap;
  55. import java.util.TreeMap;
  56. import java.util.logging.Level;
  57. import java.util.logging.Logger;
  58. import javax.swing.JButton;
  59. import javax.swing.SwingUtilities;
  60. import org.netbeans.api.autoupdate.OperationContainer;
  61. import org.netbeans.api.autoupdate.OperationContainer.OperationInfo;
  62. import org.netbeans.api.autoupdate.OperationException;
  63. import org.netbeans.api.autoupdate.OperationSupport;
  64. import org.netbeans.api.autoupdate.UpdateElement;
  65. import org.netbeans.api.autoupdate.UpdateManager;
  66. import org.netbeans.api.autoupdate.UpdateUnit;
  67. import org.netbeans.modules.autoupdate.ui.Containers;
  68. import org.netbeans.modules.autoupdate.ui.Utilities;
  69. import org.openide.WizardDescriptor;
  70. import org.openide.awt.Mnemonics;
  71. import org.openide.util.NbBundle;
  72. /**
  73. *
  74. * @author Jiri Rechtacek
  75. */
  76. public abstract class OperationWizardModel {
  77. private Set<UpdateElement> primaryElements;
  78. private Set<UpdateElement> requiredElements = null;
  79. private Set<UpdateElement> customHandledElements = null;
  80. private Set<UpdateElement> allElements = null;
  81. private HashMap<UpdateElement, Collection<UpdateElement>> required2primary = new HashMap<UpdateElement, Collection<UpdateElement>> ();
  82. private JButton originalCancel = null;
  83. private JButton originalNext = null;
  84. private JButton originalFinish = null;
  85. private boolean reconized = false;
  86. static Dimension PREFFERED_DIMENSION = new Dimension (530, 400);
  87. private static int MAX_TO_REPORT = 3;
  88. static String MORE_BROKEN_PLUGINS = "OperationWizardModel_MoreBrokenPlugins"; // NOI18N
  89. private TreeMap<String, Set<UpdateElement>> dep2plugins = null;
  90. abstract OperationType getOperation ();
  91. abstract OperationContainer getBaseContainer ();
  92. abstract OperationContainer<OperationSupport> getCustomHandledContainer ();
  93. public static enum OperationType {
  94. /** Install <code>UpdateElement</code> */
  95. INSTALL,
  96. /** Uninstall <code>UpdateElement</code> */
  97. UNINSTALL,
  98. /** Update installed <code>UpdateElement</code> to newer version. */
  99. UPDATE,
  100. /** Rollback installed <code>UpdateElement</code> to previous version. */
  101. REVERT,
  102. /** Enable <code>UpdateElement</code> */
  103. ENABLE,
  104. /** Disable <code>UpdateElement</code> */
  105. DISABLE,
  106. /** Install or update <code>UpdateElement</code> from local NBM. */
  107. LOCAL_DOWNLOAD
  108. }
  109. public Set<UpdateElement> getPrimaryUpdateElements () {
  110. if (primaryElements == null) {
  111. primaryElements = new HashSet<UpdateElement> ();
  112. for (OperationInfo<?> info : getBaseInfos ()) {
  113. primaryElements.add (info.getUpdateElement ());
  114. }
  115. }
  116. return primaryElements;
  117. }
  118. public boolean hasRequiredUpdateElements () {
  119. return ! getRequiredUpdateElements ().isEmpty ();
  120. }
  121. public Set<UpdateElement> getRequiredUpdateElements () {
  122. if (requiredElements == null) {
  123. requiredElements = new HashSet<UpdateElement> ();
  124. dep2plugins = new TreeMap<String, Set<UpdateElement>> ();
  125. for (OperationInfo<?> info : getBaseInfos ()) {
  126. Set<UpdateElement> reqs = info.getRequiredElements ();
  127. Set<String> broken = info.getBrokenDependencies ();
  128. if (! broken.isEmpty()) {
  129. for (String brokenDep : broken) {
  130. // pay special attention to missing JDK
  131. if (brokenDep.toLowerCase ().startsWith ("package")) {
  132. brokenDep = "package";
  133. }
  134. if (dep2plugins.get (brokenDep) == null) {
  135. dep2plugins.put (brokenDep, new HashSet<UpdateElement> ());
  136. }
  137. dep2plugins.get (brokenDep).add (info.getUpdateElement ());
  138. }
  139. if (dep2plugins.keySet ().size () >= MAX_TO_REPORT) {
  140. dep2plugins.put (MORE_BROKEN_PLUGINS, null);
  141. break;
  142. }
  143. }
  144. for (UpdateElement el : reqs) {
  145. if (required2primary.get (el) == null) {
  146. required2primary.put (el, new HashSet<UpdateElement> ());
  147. }
  148. required2primary.get (el).add (info.getUpdateElement ());
  149. }
  150. requiredElements.addAll (reqs);
  151. }
  152. Collection<UpdateElement> pending = new HashSet<UpdateElement> ();
  153. for (UpdateElement el : requiredElements) {
  154. if (el != null && el.getUpdateUnit () != null && el.getUpdateUnit ().isPending ()) {
  155. pending.add (el);
  156. }
  157. }
  158. if (! pending.isEmpty ()) {
  159. Logger.getLogger (OperationWizardModel.class.getName ()).log (Level.INFO, "Required UpdateElements " + pending +
  160. " cannot be in pending state.");
  161. requiredElements.removeAll (pending);
  162. }
  163. // add requiredElements to container
  164. addRequiredElements (requiredElements);
  165. // remove primary elements
  166. requiredElements.removeAll (getPrimaryUpdateElements ());
  167. }
  168. return requiredElements;
  169. }
  170. public boolean hasBrokenDependencies () {
  171. return ! getBrokenDependency2Plugins ().isEmpty ();
  172. }
  173. public boolean hasCustomComponents () {
  174. return ! getCustomHandledContainer ().listAll ().isEmpty ();
  175. }
  176. public boolean hasStandardComponents () {
  177. return ! getBaseContainer ().listAll ().isEmpty ();
  178. }
  179. public Set<UpdateElement> getCustomHandledComponents () {
  180. if (customHandledElements == null) {
  181. customHandledElements = new HashSet<UpdateElement> ();
  182. for (OperationInfo<?> info : getCustomHandledInfos ()) {
  183. customHandledElements.add (info.getUpdateElement ());
  184. customHandledElements.addAll (info.getRequiredElements ());
  185. }
  186. }
  187. return customHandledElements;
  188. }
  189. private List<OperationInfo<OperationSupport>> getCustomHandledInfos () {
  190. return getCustomHandledContainer ().listAll ();
  191. }
  192. @SuppressWarnings({"unchecked"})
  193. private List<OperationInfo> getBaseInfos () {
  194. return getBaseContainer ().listAll ();
  195. }
  196. public SortedMap<String, Set<UpdateElement>> getBrokenDependency2Plugins () {
  197. if (dep2plugins != null) {
  198. return dep2plugins;
  199. }
  200. dep2plugins = new TreeMap<String, Set<UpdateElement>> ();
  201. for (OperationInfo<?> info : getBaseInfos ()) {
  202. Set<String> broken = info.getBrokenDependencies ();
  203. if (! broken.isEmpty()) {
  204. for (String brokenDep : broken) {
  205. // pay special attention to missing JDK
  206. if (brokenDep.toLowerCase ().startsWith ("package")) {
  207. brokenDep = "package";
  208. }
  209. if (dep2plugins.get (brokenDep) == null) {
  210. dep2plugins.put (brokenDep, new HashSet<UpdateElement> ());
  211. }
  212. dep2plugins.get (brokenDep).add (info.getUpdateElement ());
  213. }
  214. if (dep2plugins.keySet ().size () >= MAX_TO_REPORT) {
  215. dep2plugins.put (MORE_BROKEN_PLUGINS, null);
  216. break;
  217. }
  218. }
  219. }
  220. return dep2plugins;
  221. }
  222. public Collection<UpdateElement> findPrimaryPlugins (UpdateElement el) {
  223. Collection<UpdateElement> res = new HashSet<UpdateElement> (Collections.singleton (el));
  224. if (required2primary.containsKey (el)) {
  225. res = required2primary.get (el);
  226. }
  227. return res;
  228. }
  229. public Set<UpdateElement> getAllUpdateElements () {
  230. if (allElements == null) {
  231. allElements = new HashSet<UpdateElement> (getPrimaryUpdateElements ());
  232. allElements.addAll (getRequiredUpdateElements ());
  233. assert allElements.size () == getPrimaryUpdateElements ().size () + getRequiredUpdateElements ().size () :
  234. "Primary [" + getPrimaryUpdateElements ().size () + "] plus " +
  235. "Required [" + getRequiredUpdateElements ().size () + "] is All [" + allElements.size () + "] ";
  236. }
  237. return allElements;
  238. }
  239. public Set<UpdateElement> getAllVisibleUpdateElements () {
  240. Set <UpdateElement> visible = new HashSet <UpdateElement> ();
  241. visible.addAll(getPrimaryVisibleUpdateElements());
  242. visible.addAll(getRequiredVisibleUpdateElements());
  243. return visible;
  244. }
  245. public Set<UpdateElement> getPrimaryVisibleUpdateElements() {
  246. Set <UpdateElement> primary = getPrimaryUpdateElements();
  247. Set <UpdateElement> visible = getVisibleUpdateElements(primary, false, getOperation());
  248. return visible;
  249. }
  250. public Set<UpdateElement> getRequiredVisibleUpdateElements () {
  251. Set <UpdateElement> required = getRequiredUpdateElements();
  252. Set <UpdateElement> visible = getVisibleUpdateElements(required, true, getOperation());
  253. return visible;
  254. }
  255. private static Set<UpdateElement> getVisibleUpdateElements (Set<UpdateElement> all, boolean canBeEmpty, OperationType operationType) {
  256. if (Utilities.modulesOnly () || OperationType.LOCAL_DOWNLOAD == operationType) {
  257. return all;
  258. } else if (OperationType.UPDATE == operationType) {
  259. Set<UpdateElement> visible = new HashSet<UpdateElement>();
  260. Set<UpdateUnit> visibleUnits = new HashSet<UpdateUnit>();
  261. for (UpdateElement el : all) {
  262. if (visibleUnits.contains(el.getUpdateUnit())) {
  263. continue;
  264. }
  265. if (UpdateManager.TYPE.KIT_MODULE == el.getUpdateUnit().getType()) {
  266. visible.add(el);
  267. visibleUnits.add(el.getUpdateUnit());
  268. } else {
  269. UpdateUnit visibleAncestor = el.getUpdateUnit().getVisibleAncestor();
  270. if (visibleAncestor != null) {
  271. visibleUnits.add(visibleAncestor);
  272. visible.add(visibleAncestor.getInstalled());
  273. } else {
  274. // a fallback
  275. visible.add(el);
  276. visibleUnits.add(el.getUpdateUnit());
  277. }
  278. }
  279. }
  280. if (visible.isEmpty () && ! canBeEmpty) {
  281. // in Downloaded tab may become all NBMs are hidden
  282. visible = all;
  283. }
  284. return visible;
  285. } else {
  286. Set<UpdateElement> visible = new HashSet<UpdateElement> ();
  287. for (UpdateElement el : all) {
  288. if (UpdateManager.TYPE.KIT_MODULE == el.getUpdateUnit ().getType ()) {
  289. visible.add (el);
  290. }
  291. }
  292. if (visible.isEmpty () && ! canBeEmpty) {
  293. // in Downloaded tab may become all NBMs are hidden
  294. visible = all;
  295. }
  296. return visible;
  297. }
  298. }
  299. // XXX Hack in WizardDescriptor
  300. public void modifyOptionsForDoClose (WizardDescriptor wd) {
  301. modifyOptionsForDoClose (wd, false);
  302. }
  303. // XXX Hack in WizardDescriptor
  304. public void modifyOptionsForFailed (final WizardDescriptor wd) {
  305. recognizeButtons (wd);
  306. SwingUtilities.invokeLater (new Runnable () {
  307. @Override
  308. public void run () {
  309. wd.setOptions (new JButton [] { getOriginalCancel (wd) });
  310. }
  311. });
  312. }
  313. // XXX Hack in WizardDescriptor
  314. public void modifyOptionsForDoClose (final WizardDescriptor wd, final boolean canCancel) {
  315. recognizeButtons (wd);
  316. final JButton b = getOriginalFinish (wd);
  317. Mnemonics.setLocalizedText (b, getBundle ("InstallUnitWizardModel_Buttons_Close"));
  318. SwingUtilities.invokeLater (new Runnable () {
  319. int cnt;
  320. @Override
  321. public void run () {
  322. b.requestFocus();
  323. if (cnt++ > 0) {
  324. return;
  325. }
  326. b.setDefaultCapable(true);
  327. final JButton[] arr = canCancel ? new JButton [] { b, getOriginalCancel (wd) } : new JButton [] { b };
  328. wd.setOptions (arr);
  329. wd.setClosingOptions(arr);
  330. SwingUtilities.invokeLater(this);
  331. }
  332. });
  333. }
  334. // XXX Hack in WizardDescriptor
  335. public void modifyOptionsForStartWizard (WizardDescriptor wd) {
  336. recognizeButtons (wd);
  337. removeFinish (wd);
  338. Mnemonics.setLocalizedText (getOriginalNext (wd), NbBundle.getMessage (InstallUnitWizardModel.class,
  339. "InstallUnitWizardModel_Buttons_MnemonicNext", getBundle ("InstallUnitWizardModel_Buttons_Next")));
  340. }
  341. public void modifyOptionsForContinue (final WizardDescriptor wd, boolean canFinish) {
  342. if (canFinish) {
  343. recognizeButtons (wd);
  344. final JButton b = getOriginalFinish (wd);
  345. Mnemonics.setLocalizedText (b, getBundle ("InstallUnitWizardModel_Buttons_Close"));
  346. SwingUtilities.invokeLater (new Runnable () {
  347. @Override
  348. public void run () {
  349. wd.setOptions (new JButton [] {b});
  350. }
  351. });
  352. } else {
  353. recognizeButtons (wd);
  354. removeFinish (wd);
  355. Mnemonics.setLocalizedText (getOriginalNext (wd), NbBundle.getMessage (InstallUnitWizardModel.class,
  356. "InstallUnitWizardModel_Buttons_MnemonicNext", getBundle ("InstallUnitWizardModel_Buttons_Next")));
  357. }
  358. }
  359. // XXX Hack in WizardDescriptor
  360. public void modifyOptionsForDoOperation (WizardDescriptor wd) {
  361. recognizeButtons (wd);
  362. removeFinish (wd);
  363. switch (getOperation ()) {
  364. case LOCAL_DOWNLOAD :
  365. if (Containers.forUpdateNbms ().listAll ().isEmpty ()) {
  366. Mnemonics.setLocalizedText (getOriginalNext (wd), getBundle ("InstallUnitWizardModel_Buttons_Install"));
  367. } else {
  368. Mnemonics.setLocalizedText (getOriginalNext (wd), getBundle ("InstallUnitWizardModel_Buttons_Update"));
  369. }
  370. break;
  371. case INSTALL :
  372. Mnemonics.setLocalizedText (getOriginalNext (wd), getBundle ("InstallUnitWizardModel_Buttons_Install"));
  373. break;
  374. case UPDATE :
  375. Mnemonics.setLocalizedText (getOriginalNext (wd), getBundle ("InstallUnitWizardModel_Buttons_Update"));
  376. break;
  377. case UNINSTALL :
  378. Mnemonics.setLocalizedText (getOriginalNext (wd), getBundle ("UninstallUnitWizardModel_Buttons_Uninstall"));
  379. break;
  380. case ENABLE :
  381. Mnemonics.setLocalizedText (getOriginalNext (wd), getBundle ("UninstallUnitWizardModel_Buttons_TurnOn"));
  382. break;
  383. case DISABLE :
  384. Mnemonics.setLocalizedText (getOriginalNext (wd), getBundle ("UninstallUnitWizardModel_Buttons_TurnOff"));
  385. break;
  386. default:
  387. assert false : "Unknown operationType " + getOperation ();
  388. }
  389. }
  390. // XXX Hack in WizardDescriptor
  391. public JButton getCancelButton (WizardDescriptor wd) {
  392. return getOriginalCancel (wd);
  393. }
  394. // XXX Hack in WizardDescriptor
  395. public void modifyOptionsForDisabledCancel (final WizardDescriptor wd) {
  396. recognizeButtons (wd);
  397. Object [] options = wd.getOptions ();
  398. final List<JButton> newOptionsL = new ArrayList<JButton> ();
  399. List<Object> optionsL = Arrays.asList (options);
  400. for (Object o : optionsL) {
  401. assert o instanceof JButton : o + " instanceof JButton";
  402. if (o instanceof JButton) {
  403. JButton b = (JButton) o;
  404. if (b.equals (getOriginalCancel (wd))) {
  405. JButton disabledCancel = new JButton (b.getText ());
  406. disabledCancel.setEnabled (false);
  407. newOptionsL.add (disabledCancel);
  408. } else {
  409. newOptionsL.add (b);
  410. }
  411. }
  412. }
  413. SwingUtilities.invokeLater (new Runnable () {
  414. @Override
  415. public void run () {
  416. wd.setOptions (newOptionsL.toArray ());
  417. }
  418. });
  419. }
  420. public void doCleanup (boolean cancel) throws OperationException {
  421. getBaseContainer ().removeAll ();
  422. getCustomHandledContainer ().removeAll ();
  423. }
  424. private void recognizeButtons (WizardDescriptor wd) {
  425. if (! reconized) {
  426. Object [] options = wd.getOptions ();
  427. assert options != null : "options: " + options;
  428. assert options.length >= 4 : Arrays.asList (options) + " has lenght 4";
  429. assert options [1] instanceof JButton : options [1] + " instanceof JButton";
  430. originalNext = (JButton) options [1];
  431. assert options [2] instanceof JButton : options [2] + " instanceof JButton";
  432. originalFinish = (JButton) options [2];
  433. assert options [3] instanceof JButton : options [3] + " instanceof JButton";
  434. originalCancel = (JButton) options [3];
  435. reconized = true;
  436. }
  437. }
  438. private JButton getOriginalNext (WizardDescriptor wd) {
  439. return originalNext;
  440. }
  441. private JButton getOriginalCancel (WizardDescriptor wd) {
  442. return originalCancel;
  443. }
  444. private JButton getOriginalFinish (WizardDescriptor wd) {
  445. return originalFinish;
  446. }
  447. private void removeFinish (final WizardDescriptor wd) {
  448. Object [] options = wd.getOptions ();
  449. final List<JButton> newOptionsL = new ArrayList<JButton> ();
  450. List<Object> optionsL = Arrays.asList (options);
  451. for (Object o : optionsL) {
  452. assert o instanceof JButton : o + " instanceof JButton";
  453. if (o instanceof JButton) {
  454. JButton b = (JButton) o;
  455. if (! b.equals (originalFinish)) {
  456. newOptionsL.add (b);
  457. }
  458. }
  459. }
  460. SwingUtilities.invokeLater (new Runnable () {
  461. @Override
  462. public void run () {
  463. wd.setOptions (newOptionsL.toArray ());
  464. }
  465. });
  466. }
  467. private void addRequiredElements (Set<UpdateElement> elems) {
  468. OperationContainer baseContainer = getBaseContainer();
  469. OperationContainer customContainer = getCustomHandledContainer();
  470. for (UpdateElement el : elems) {
  471. if (el == null || el.getUpdateUnit () == null) {
  472. Logger.getLogger (OperationWizardModel.class.getName ()).log (Level.INFO, "UpdateElement " + el + " cannot be null"
  473. + (el == null ? "" : " or UpdateUnit " + el.getUpdateUnit () + " cannot be null"));
  474. continue;
  475. }
  476. if (UpdateManager.TYPE.CUSTOM_HANDLED_COMPONENT == el.getUpdateUnit ().getType ()) {
  477. customContainer.add (el);
  478. } else {
  479. baseContainer.add (el);
  480. }
  481. }
  482. }
  483. private String getBundle (String key) {
  484. return NbBundle.getMessage (InstallUnitWizardModel.class, key);
  485. }
  486. }