PageRenderTime 41ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/projects/netbeans-7.3/core.windows/src/org/netbeans/core/windows/RecentViewList.java

https://gitlab.com/essere.lab.public/qualitas.class-corpus
Java | 169 lines | 90 code | 16 blank | 63 comment | 23 complexity | 24f264622d31bf509815b6f20bcbd4c3 MD5 | raw file
  1. /*
  2. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
  3. *
  4. * Copyright 1997-2010 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-2006 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.core.windows;
  45. import java.lang.ref.Reference;
  46. import java.util.Iterator;
  47. import java.util.Set;
  48. import org.openide.windows.TopComponent;
  49. import org.openide.windows.WindowManager;
  50. import java.beans.PropertyChangeEvent;
  51. import java.beans.PropertyChangeListener;
  52. import java.lang.ref.WeakReference;
  53. import java.util.ArrayList;
  54. import java.util.List;
  55. /**
  56. * Instance of this class keeps list of (weak references to) recently activated TopComponents.
  57. *
  58. * @author Marek Slama
  59. */
  60. final class RecentViewList implements PropertyChangeListener {
  61. private static RecentViewList instance;
  62. /** List of TopComponents (weak references are used). First is most recently
  63. * activated. */
  64. private List<Reference<TopComponent>> tcWeakList = new ArrayList<Reference<TopComponent>>(20);
  65. public RecentViewList (WindowManager wm) {
  66. // Starts listening on Registry to be notified about activated TopComponent.
  67. wm.getRegistry().addPropertyChangeListener(this);
  68. }
  69. /** Used to get array for view and for persistence */
  70. public TopComponent [] getTopComponents() {
  71. List<TopComponent> tcList = new ArrayList<TopComponent>(tcWeakList.size());
  72. clean();
  73. for (int i = 0; i < tcWeakList.size(); i++) {
  74. Reference<TopComponent> w = tcWeakList.get(i);
  75. TopComponent tc = w.get();
  76. if ((tc != null) && tc.isOpened()) {
  77. tcList.add(tc);
  78. }
  79. }
  80. return tcList.toArray(new TopComponent[tcList.size()]);
  81. }
  82. /** Used to set initial values from persistence */
  83. public void setTopComponents(TopComponent [] tcs) {
  84. tcWeakList.clear();
  85. for (int i = 0; i < tcs.length; i++) {
  86. Reference<TopComponent> wr = new WeakReference<TopComponent>(tcs[i]);
  87. tcWeakList.add(wr);
  88. }
  89. }
  90. public void propertyChange(PropertyChangeEvent evt) {
  91. if (TopComponent.Registry.PROP_ACTIVATED.equals(evt.getPropertyName())) {
  92. TopComponent tc = (TopComponent) evt.getNewValue();
  93. if (tc != null) {
  94. //Update list
  95. clean();
  96. Reference<TopComponent> w = find(tc);
  97. if (w != null) {
  98. //Rearrange, put to first place
  99. tcWeakList.remove(w);
  100. tcWeakList.add(0,w);
  101. } else {
  102. Reference<TopComponent> wr = new WeakReference<TopComponent>(tc);
  103. tcWeakList.add(0,wr);
  104. }
  105. // #69486: ensure all components are listed
  106. fillList(TopComponent.getRegistry().getOpened());
  107. }
  108. }
  109. }
  110. /** Clean gc'ed TopComponents from list */
  111. private void clean () {
  112. int i = 0;
  113. while (i < tcWeakList.size()) {
  114. WeakReference w = (WeakReference) tcWeakList.get(i);
  115. TopComponent tc = (TopComponent) w.get();
  116. //TopComponent was gc'ed
  117. if (tc == null) {
  118. tcWeakList.remove(w);
  119. } else {
  120. i++;
  121. }
  122. }
  123. }
  124. /** Returns weak reference to given TopComponent if present.
  125. * Otherwise returns null. */
  126. private Reference<TopComponent> find (TopComponent tc) {
  127. for (int i = 0; i < tcWeakList.size(); i++) {
  128. Reference<TopComponent> w = tcWeakList.get(i);
  129. TopComponent c = w.get();
  130. if (tc == c) {
  131. return w;
  132. }
  133. }
  134. return null;
  135. }
  136. /** Fills list of weak references with TCs that are in given
  137. * input list but are not yet contained in list of weak references.
  138. */
  139. private void fillList(Set<TopComponent> openedTCs) {
  140. Reference<TopComponent> wr;
  141. for (TopComponent curTC: openedTCs) {
  142. if (find(curTC) == null) {
  143. if (tcWeakList.size() > 1) {
  144. wr = new WeakReference<TopComponent>(curTC);
  145. tcWeakList.add(1,wr);
  146. } else {
  147. wr = new WeakReference<TopComponent>(curTC);
  148. tcWeakList.add(wr);
  149. }
  150. }
  151. }
  152. }
  153. }