PageRenderTime 49ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 1ms

/projects/netbeans-7.3/openide.windows/src/org/openide/windows/DummyWindowManager.java

https://gitlab.com/essere.lab.public/qualitas.class-corpus
Java | 743 lines | 521 code | 145 blank | 77 comment | 107 complexity | bf5911442003556e460c2ceab3721e45 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.openide.windows;
  45. import java.awt.Frame;
  46. import java.awt.Image;
  47. import java.awt.Rectangle;
  48. import java.awt.event.WindowAdapter;
  49. import java.awt.event.WindowEvent;
  50. import java.beans.PropertyChangeListener;
  51. import java.beans.PropertyChangeSupport;
  52. import java.lang.ref.Reference;
  53. import java.lang.ref.WeakReference;
  54. import java.net.URL;
  55. import java.util.ArrayList;
  56. import java.util.Arrays;
  57. import java.util.Collections;
  58. import java.util.HashMap;
  59. import java.util.HashSet;
  60. import java.util.Iterator;
  61. import java.util.Map;
  62. import java.util.Set;
  63. import java.util.TreeMap;
  64. import java.util.WeakHashMap;
  65. import javax.swing.Action;
  66. import javax.swing.JFrame;
  67. import javax.swing.SwingUtilities;
  68. import org.openide.nodes.Node;
  69. import org.openide.util.Utilities;
  70. import org.openide.util.actions.SystemAction;
  71. /**
  72. * Trivial window manager that just keeps track of "workspaces" and "modes"
  73. * according to contract but does not really use them, and just opens all
  74. * top components in their own frames.
  75. * Useful in case core-windows.jar is not installed, e.g. in standalone usage.
  76. * @author Jesse Glick
  77. * @see "#29933"
  78. *
  79. */
  80. @SuppressWarnings("deprecation")
  81. final class DummyWindowManager extends WindowManager {
  82. private static final boolean VISIBLE = Boolean.parseBoolean(System.getProperty("org.openide.windows.DummyWindowManager.VISIBLE", "true"));
  83. private static final long serialVersionUID = 1L;
  84. private static Action[] DEFAULT_ACTIONS_CLONEABLE;
  85. private static Action[] DEFAULT_ACTIONS_NOT_CLONEABLE;
  86. private final Map<String,Workspace> workspaces;
  87. private transient Frame mw;
  88. private transient PropertyChangeSupport pcs;
  89. public DummyWindowManager() {
  90. workspaces = new TreeMap<String,Workspace>();
  91. createWorkspace("default", null).createMode(/*CloneableEditorSupport.EDITOR_MODE*/"editor", "editor", null); // NOI18N
  92. }
  93. public synchronized void addPropertyChangeListener(PropertyChangeListener l) {
  94. if (pcs == null) {
  95. pcs = new PropertyChangeSupport(this);
  96. }
  97. pcs.addPropertyChangeListener(l);
  98. }
  99. public synchronized void removePropertyChangeListener(PropertyChangeListener l) {
  100. if (pcs != null) {
  101. pcs.removePropertyChangeListener(l);
  102. }
  103. }
  104. protected TopComponent.Registry componentRegistry() {
  105. return new R();
  106. }
  107. private R registry() {
  108. return (R) getRegistry();
  109. }
  110. protected WindowManager.Component createTopComponentManager(TopComponent c) {
  111. return null; // Not used anymore.
  112. }
  113. public synchronized Workspace createWorkspace(String name, String displayName) {
  114. Workspace w = new W(name);
  115. workspaces.put(name, w);
  116. if (pcs != null) {
  117. pcs.firePropertyChange(PROP_WORKSPACES, null, null);
  118. pcs.firePropertyChange(PROP_CURRENT_WORKSPACE, null, null);
  119. }
  120. return w;
  121. }
  122. synchronized void delete(Workspace w) {
  123. workspaces.remove(w.getName());
  124. if (workspaces.isEmpty()) {
  125. createWorkspace("default", null); // NOI18N
  126. }
  127. if (pcs != null) {
  128. pcs.firePropertyChange(PROP_WORKSPACES, null, null);
  129. pcs.firePropertyChange(PROP_CURRENT_WORKSPACE, null, null);
  130. }
  131. }
  132. public synchronized Workspace findWorkspace(String name) {
  133. return workspaces.get(name);
  134. }
  135. public synchronized Workspace getCurrentWorkspace() {
  136. return workspaces.values().iterator().next();
  137. }
  138. public synchronized Workspace[] getWorkspaces() {
  139. return workspaces.values().toArray(new Workspace[0]);
  140. }
  141. public synchronized void setWorkspaces(Workspace[] ws) {
  142. if (ws.length == 0) {
  143. throw new IllegalArgumentException();
  144. }
  145. workspaces.clear();
  146. for (int i = 0; i < ws.length; i++) {
  147. workspaces.put(ws[i].getName(), ws[i]);
  148. }
  149. if (pcs != null) {
  150. pcs.firePropertyChange(PROP_WORKSPACES, null, null);
  151. pcs.firePropertyChange(PROP_CURRENT_WORKSPACE, null, null);
  152. }
  153. }
  154. public synchronized Frame getMainWindow() {
  155. if (mw == null) {
  156. mw = new JFrame("dummy"); // NOI18N
  157. }
  158. return mw;
  159. }
  160. public void updateUI() {
  161. }
  162. // Modes
  163. public Set<Mode> getModes() {
  164. Set<Mode> s = new HashSet<Mode>();
  165. for (Iterator<Workspace> it = new HashSet<Workspace>(workspaces.values()).iterator(); it.hasNext();) {
  166. Workspace w = it.next();
  167. s.addAll(w.getModes());
  168. }
  169. return s;
  170. }
  171. public Mode findMode(TopComponent tc) {
  172. for (Iterator<Mode> it = getModes().iterator(); it.hasNext();) {
  173. Mode m = it.next();
  174. if (Arrays.asList(m.getTopComponents()).contains(tc)) {
  175. return m;
  176. }
  177. }
  178. return null;
  179. }
  180. public Mode findMode(String name) {
  181. if (name == null) {
  182. return null;
  183. }
  184. for (Iterator<Mode> it = getModes().iterator(); it.hasNext();) {
  185. Mode m = it.next();
  186. if (name.equals(m.getName())) {
  187. return m;
  188. }
  189. }
  190. return null;
  191. }
  192. // PENDING Groups not supported.
  193. public TopComponentGroup findTopComponentGroup(String name) {
  194. return null;
  195. }
  196. //Not supported. Need to access PersistenceManager.
  197. public TopComponent findTopComponent(String tcID) {
  198. return null;
  199. }
  200. protected String topComponentID(TopComponent tc, String preferredID) {
  201. return preferredID;
  202. }
  203. protected Action[] topComponentDefaultActions(TopComponent tc) {
  204. // XXX It could be better to provide non-SystemAction instances.
  205. synchronized (DummyWindowManager.class) {
  206. //Bugfix #33557: Do not provide CloneViewAction when
  207. //TopComponent does not implement TopComponent.Cloneable
  208. if (tc instanceof TopComponent.Cloneable) {
  209. if (DEFAULT_ACTIONS_CLONEABLE == null) {
  210. DEFAULT_ACTIONS_CLONEABLE = loadActions(
  211. new String[] { "Save", // NOI18N
  212. "CloneView", // NOI18N
  213. null, "CloseView" // NOI18N
  214. }
  215. );
  216. }
  217. return DEFAULT_ACTIONS_CLONEABLE;
  218. } else {
  219. if (DEFAULT_ACTIONS_NOT_CLONEABLE == null) {
  220. DEFAULT_ACTIONS_NOT_CLONEABLE = loadActions(
  221. new String[] { "Save", // NOI18N
  222. null, "CloseView" // NOI18N
  223. }
  224. );
  225. }
  226. return DEFAULT_ACTIONS_NOT_CLONEABLE;
  227. }
  228. }
  229. }
  230. private static Action[] loadActions(String[] names) {
  231. ArrayList<Action> arr = new ArrayList<Action>();
  232. ClassLoader loader = Thread.currentThread().getContextClassLoader();
  233. for (int i = 0; i < names.length; i++) {
  234. if (names[i] == null) {
  235. arr.add(null);
  236. continue;
  237. }
  238. try {
  239. Class<? extends SystemAction> sa = Class.forName("org.openide.actions." + names[i] + "Action", true, loader).asSubclass(SystemAction.class);
  240. arr.add(SystemAction.get(sa)); // NOI18N
  241. } catch (ClassNotFoundException e) {
  242. // ignore it, missing org-openide-actions.jar
  243. }
  244. }
  245. return arr.toArray(new Action[0]);
  246. }
  247. protected boolean topComponentIsOpened(TopComponent tc) {
  248. return tc.isShowing() || registry().opened.contains(tc);
  249. }
  250. protected void topComponentActivatedNodesChanged(TopComponent tc, Node[] nodes) {
  251. registry().setActivatedNodes(tc, nodes);
  252. }
  253. protected void topComponentIconChanged(TopComponent tc, Image icon) {
  254. JFrame f = (JFrame) SwingUtilities.getAncestorOfClass(JFrame.class, tc);
  255. if (f != null) {
  256. f.setIconImage(icon);
  257. }
  258. }
  259. protected void topComponentToolTipChanged(TopComponent tc, String tooltip) {
  260. // No op.
  261. }
  262. protected void topComponentDisplayNameChanged(TopComponent tc, String displayName) {
  263. JFrame f = (JFrame) SwingUtilities.getAncestorOfClass(JFrame.class, tc);
  264. if (f != null) {
  265. f.setTitle(displayName);
  266. }
  267. }
  268. protected void topComponentHtmlDisplayNameChanged(TopComponent tc, String htmlDisplayName) {
  269. // no operarion, html looks ugly in frame titles
  270. }
  271. protected void topComponentOpen(TopComponent tc) {
  272. JFrame f = (JFrame) SwingUtilities.getAncestorOfClass(JFrame.class, tc);
  273. if (f == null) {
  274. f = new JFrame(tc.getName());
  275. Image icon = tc.getIcon();
  276. if (icon != null) {
  277. f.setIconImage(icon);
  278. }
  279. f.getContentPane().add(tc);
  280. f.pack();
  281. final java.lang.ref.WeakReference<TopComponent> ref = new java.lang.ref.WeakReference<TopComponent>(tc);
  282. f.addWindowListener(
  283. new WindowAdapter() {
  284. public void windowClosing(WindowEvent ev) {
  285. TopComponent tc = ref.get();
  286. if (tc == null) {
  287. return;
  288. }
  289. tc.close();
  290. }
  291. public void windowActivated(WindowEvent e) {
  292. TopComponent tc = ref.get();
  293. if (tc == null) {
  294. return;
  295. }
  296. tc.requestActive();
  297. }
  298. }
  299. );
  300. }
  301. if (!tc.isShowing()) {
  302. componentOpenNotify(tc);
  303. componentShowing(tc);
  304. if (VISIBLE) {
  305. f.setVisible(true);
  306. }
  307. registry().open(tc);
  308. }
  309. }
  310. protected void topComponentClose(TopComponent tc) {
  311. if( !tc.canClose() )
  312. return;
  313. componentHidden(tc);
  314. componentCloseNotify(tc);
  315. JFrame f = (JFrame) SwingUtilities.getAncestorOfClass(JFrame.class, tc);
  316. if (f != null) {
  317. if (VISIBLE) {
  318. f.setVisible(false);
  319. }
  320. tc.getParent().remove(tc);
  321. }
  322. registry().close(tc);
  323. java.util.Iterator it = workspaces.values().iterator();
  324. while (it.hasNext()) {
  325. W w = (W) it.next();
  326. w.close(tc);
  327. }
  328. }
  329. protected void topComponentRequestVisible(TopComponent tc) {
  330. JFrame f = (JFrame) SwingUtilities.getAncestorOfClass(JFrame.class, tc);
  331. if (f != null) {
  332. if (VISIBLE) {
  333. f.setVisible(true);
  334. }
  335. }
  336. }
  337. protected void topComponentRequestActive(TopComponent tc) {
  338. JFrame f = (JFrame) SwingUtilities.getAncestorOfClass(JFrame.class, tc);
  339. if (f != null) {
  340. f.toFront();
  341. }
  342. registry().setActive(tc);
  343. activateComponent(tc);
  344. }
  345. protected void topComponentRequestAttention(TopComponent tc) {
  346. //TODO what to do here?
  347. }
  348. protected void topComponentCancelRequestAttention(TopComponent tc) {
  349. //TODO what to do here?
  350. }
  351. @Override
  352. public boolean isEditorTopComponent(TopComponent tc) {
  353. Mode md = findMode(tc);
  354. if (md != null && isEditorMode(md)) {
  355. return true;
  356. }
  357. return false;
  358. }
  359. @Override
  360. public boolean isOpenedEditorTopComponent(TopComponent tc) {
  361. Mode md = findMode(tc);
  362. if (md != null && isEditorMode(md)) {
  363. return tc.isOpened();
  364. }
  365. return super.isOpenedEditorTopComponent(tc);
  366. }
  367. @Override
  368. public boolean isEditorMode(Mode mode) {
  369. if( null == mode )
  370. return false;
  371. return "editor".equals(mode.getName());
  372. }
  373. private final class W implements Workspace {
  374. private static final long serialVersionUID = 1L;
  375. private final String name;
  376. private final Map<String,Mode> modes = new HashMap<String,Mode>();
  377. private final Map<TopComponent,Mode> modesByComponent = new WeakHashMap<TopComponent,Mode>();
  378. private transient PropertyChangeSupport pcs;
  379. public W(String name) {
  380. this.name = name;
  381. }
  382. public void activate() {
  383. }
  384. public synchronized void addPropertyChangeListener(PropertyChangeListener list) {
  385. if (pcs == null) {
  386. pcs = new PropertyChangeSupport(this);
  387. }
  388. pcs.addPropertyChangeListener(list);
  389. }
  390. public synchronized void removePropertyChangeListener(PropertyChangeListener list) {
  391. if (pcs != null) {
  392. pcs.removePropertyChangeListener(list);
  393. }
  394. }
  395. public void remove() {
  396. DummyWindowManager.this.delete(this);
  397. }
  398. public synchronized Mode createMode(String name, String displayName, URL icon) {
  399. Mode m = new M(name);
  400. modes.put(name, m);
  401. if (pcs != null) {
  402. pcs.firePropertyChange(PROP_MODES, null, null);
  403. }
  404. return m;
  405. }
  406. public synchronized Set<Mode> getModes() {
  407. return new HashSet<Mode>(modes.values());
  408. }
  409. public synchronized Mode findMode(String name) {
  410. return modes.get(name);
  411. }
  412. public synchronized Mode findMode(TopComponent c) {
  413. return modesByComponent.get(c);
  414. }
  415. synchronized void dock(Mode m, TopComponent c) {
  416. modesByComponent.put(c, m);
  417. }
  418. public Rectangle getBounds() {
  419. return Utilities.getUsableScreenBounds();
  420. }
  421. public String getName() {
  422. return name;
  423. }
  424. public String getDisplayName() {
  425. return getName();
  426. }
  427. public void close(TopComponent tc) {
  428. java.util.Iterator it = modes.values().iterator();
  429. while (it.hasNext()) {
  430. M m = (M) it.next();
  431. m.close(tc);
  432. }
  433. }
  434. private final class M implements Mode {
  435. private static final long serialVersionUID = 1L;
  436. private final String name;
  437. private final Set<TopComponent> components = new HashSet<TopComponent>();
  438. public M(String name) {
  439. this.name = name;
  440. }
  441. public void close(TopComponent tc) {
  442. components.remove(tc);
  443. }
  444. /* Not needed:
  445. private transient PropertyChangeSupport pcs;
  446. public synchronized void addPropertyChangeListener(PropertyChangeListener list) {
  447. if (pcs == null) {
  448. pcs = new PropertyChangeSupport(this);
  449. }
  450. pcs.addPropertyChangeListener(list);
  451. }
  452. public synchronized void removePropertyChangeListener(PropertyChangeListener list) {
  453. if (pcs != null) {
  454. pcs.removePropertyChangeListener(list);
  455. }
  456. }
  457. */
  458. public void addPropertyChangeListener(PropertyChangeListener l) {
  459. }
  460. public void removePropertyChangeListener(PropertyChangeListener l) {
  461. }
  462. public boolean canDock(TopComponent tc) {
  463. return true;
  464. }
  465. public synchronized boolean dockInto(TopComponent c) {
  466. if (components.add(c)) {
  467. Mode old = findMode(c);
  468. if ((old != null) && (old != this) && old instanceof M) {
  469. synchronized (old) {
  470. ((M) old).components.remove(c);
  471. }
  472. }
  473. dock(this, c);
  474. }
  475. return true;
  476. }
  477. public String getName() {
  478. return name;
  479. }
  480. public String getDisplayName() {
  481. return getName();
  482. }
  483. public Image getIcon() {
  484. return null;
  485. }
  486. public synchronized TopComponent[] getTopComponents() {
  487. return components.toArray(new TopComponent[0]);
  488. }
  489. public Workspace getWorkspace() {
  490. return W.this;
  491. }
  492. public synchronized Rectangle getBounds() {
  493. return W.this.getBounds();
  494. }
  495. public void setBounds(Rectangle s) {
  496. }
  497. public TopComponent getSelectedTopComponent() {
  498. TopComponent[] tcs = components.toArray(new TopComponent[0]);
  499. return (tcs.length > 0) ? tcs[0] : null;
  500. }
  501. }
  502. }
  503. private static final class R implements TopComponent.Registry {
  504. private Reference<TopComponent> active = new WeakReference<TopComponent>(null);
  505. private final Set<TopComponent> opened;
  506. private Node[] nodes;
  507. private PropertyChangeSupport pcs;
  508. public R() {
  509. opened = new HashSet<TopComponent>() {
  510. @Override
  511. public Iterator<TopComponent> iterator() {
  512. HashSet<TopComponent> copy = new HashSet<TopComponent>();
  513. Iterator<TopComponent> it = super.iterator();
  514. while (it.hasNext()) {
  515. TopComponent topComponent = it.next();
  516. copy.add(topComponent);
  517. }
  518. return copy.iterator();
  519. }
  520. };
  521. nodes = new Node[0];
  522. }
  523. public synchronized void addPropertyChangeListener(PropertyChangeListener l) {
  524. if (pcs == null) {
  525. pcs = new PropertyChangeSupport(this);
  526. }
  527. pcs.addPropertyChangeListener(l);
  528. }
  529. public synchronized void removePropertyChangeListener(PropertyChangeListener l) {
  530. if (pcs != null) {
  531. pcs.removePropertyChangeListener(l);
  532. }
  533. }
  534. synchronized void open(TopComponent tc) {
  535. opened.add(tc);
  536. if (pcs != null) {
  537. pcs.firePropertyChange(PROP_TC_OPENED, null, tc);
  538. pcs.firePropertyChange(PROP_OPENED, null, null);
  539. }
  540. }
  541. synchronized void close(TopComponent tc) {
  542. opened.remove(tc);
  543. if (pcs != null) {
  544. pcs.firePropertyChange(PROP_TC_CLOSED, null, tc);
  545. pcs.firePropertyChange(PROP_OPENED, null, null);
  546. }
  547. if (getActive() == tc) {
  548. setActive(null);
  549. }
  550. }
  551. public synchronized Set<TopComponent> getOpened() {
  552. return Collections.unmodifiableSet(opened);
  553. }
  554. synchronized void setActive(TopComponent tc) {
  555. active = new WeakReference<TopComponent>(tc);
  556. Node[] _nodes = (tc == null) ? new Node[0] : tc.getActivatedNodes();
  557. if (_nodes != null) {
  558. nodes = _nodes;
  559. if (pcs != null) {
  560. pcs.firePropertyChange(PROP_ACTIVATED_NODES, null, null);
  561. }
  562. }
  563. if (pcs != null) {
  564. pcs.firePropertyChange(PROP_ACTIVATED, null, null);
  565. pcs.firePropertyChange(PROP_CURRENT_NODES, null, null);
  566. }
  567. }
  568. synchronized void setActivatedNodes(TopComponent tc, Node[] _nodes) {
  569. if (tc == getActive()) {
  570. if (_nodes != null) {
  571. nodes = _nodes;
  572. if (pcs != null) {
  573. pcs.firePropertyChange(PROP_ACTIVATED_NODES, null, null);
  574. }
  575. }
  576. if (pcs != null) {
  577. pcs.firePropertyChange(PROP_CURRENT_NODES, null, null);
  578. }
  579. }
  580. }
  581. public TopComponent getActivated() {
  582. return getActive();
  583. }
  584. public Node[] getActivatedNodes() {
  585. return nodes;
  586. }
  587. public synchronized Node[] getCurrentNodes() {
  588. if (getActive() != null) {
  589. return getActive().getActivatedNodes();
  590. } else {
  591. return null;
  592. }
  593. }
  594. private TopComponent getActive() {
  595. return active.get();
  596. }
  597. }
  598. }