1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/tasklist.core/src/org/netbeans/modules/tasklist/core/ToggleViewAction.java Sun Aug 03 18:14:18 2003 +0000
1.3 @@ -0,0 +1,118 @@
1.4 +/*
1.5 + * Sun Public License Notice
1.6 + *
1.7 + * The contents of this file are subject to the Sun Public License
1.8 + * Version 1.0 (the "License"). You may not use this file except in
1.9 + * compliance with the License. A copy of the License is available at
1.10 + * http://www.sun.com/
1.11 + *
1.12 + * The Original Code is NetBeans. The Initial Developer of the Original
1.13 + * Code is Sun Microsystems, Inc. Portions Copyright 1997-2003 Sun
1.14 + * Microsystems, Inc. All Rights Reserved.
1.15 + */
1.16 +package org.netbeans.modules.tasklist.core;
1.17 +
1.18 +import java.beans.PropertyChangeEvent;
1.19 +import java.beans.PropertyChangeListener;
1.20 +import javax.swing.SwingUtilities;
1.21 +import org.netbeans.modules.tasklist.core.*;
1.22 +import org.openide.awt.Actions;
1.23 +
1.24 +import org.openide.util.HelpCtx;
1.25 +import org.openide.util.NbBundle;
1.26 +import org.openide.util.actions.BooleanStateAction;
1.27 +import org.openide.util.actions.CallableSystemAction;
1.28 +import org.openide.windows.Mode;
1.29 +import org.openide.windows.TopComponent;
1.30 +import org.openide.windows.WindowManager;
1.31 +import org.openide.windows.Workspace;
1.32 +
1.33 +/**
1.34 + * Shows/closes a view.
1.35 + *
1.36 + * @author Tim Lebedkov
1.37 + */
1.38 +public abstract class ToggleViewAction extends BooleanStateAction implements
1.39 +PropertyChangeListener {
1.40 + private boolean block;
1.41 + private String mode = "output"; // NOI18N
1.42 +
1.43 + public void setBooleanState(final boolean value) {
1.44 + super.setBooleanState(value);
1.45 + if (block)
1.46 + return;
1.47 + block = true;
1.48 + SwingUtilities.invokeLater(new Runnable() {
1.49 + public void run() {
1.50 + toggleView(value);
1.51 + block = false;
1.52 + }
1.53 + });
1.54 + }
1.55 +
1.56 + /**
1.57 + * Should return the view to be shown
1.58 + *
1.59 + * @return the view
1.60 + */
1.61 + protected abstract TopComponent getView();
1.62 +
1.63 + /**
1.64 + * Returns true if the view is visible (on the current workspace).
1.65 + * This method could be overriden to don't create the view if it is not
1.66 + * visible.
1.67 + *
1.68 + * @return true = the view is opened on the current workspace
1.69 + */
1.70 + protected boolean isViewOpened() {
1.71 + return getView().isOpened(WindowManager.getDefault().
1.72 + getCurrentWorkspace());
1.73 + }
1.74 +
1.75 + /**
1.76 + * Closes/opens the view
1.77 + *
1.78 + * @param visible true = open the view
1.79 + */
1.80 + private void toggleView(boolean visible) {
1.81 + if (visible == isViewOpened())
1.82 + return;
1.83 +
1.84 + TopComponent view = getView();
1.85 + Workspace workspace = WindowManager.getDefault().
1.86 + getCurrentWorkspace();
1.87 + if (!visible) {
1.88 + Mode mode = workspace.findMode(view);
1.89 + if (mode != null)
1.90 + this.mode = mode.getName();
1.91 + view.close();
1.92 + } else if (visible) {
1.93 + Mode mode = workspace.findMode(this.mode);
1.94 + if (mode != null) {
1.95 + mode.dockInto(view);
1.96 + }
1.97 + view.open(workspace);
1.98 + view.requestVisible();
1.99 + view.requestFocus();
1.100 + }
1.101 + }
1.102 +
1.103 + protected void initialize() {
1.104 + super.initialize();
1.105 + WindowManager.getDefault().addPropertyChangeListener(this);
1.106 + WindowManager.getDefault().getRegistry().addPropertyChangeListener(this);
1.107 + putProperty(PROP_BOOLEAN_STATE, Boolean.valueOf(isViewOpened()));
1.108 + }
1.109 +
1.110 + public HelpCtx getHelpCtx() {
1.111 + return HelpCtx.DEFAULT_HELP;
1.112 + }
1.113 +
1.114 + public void propertyChange(PropertyChangeEvent e) {
1.115 + String p = e.getPropertyName();
1.116 + if (p.equals(TopComponent.Registry.PROP_OPENED) ||
1.117 + p.equals(WindowManager.PROP_CURRENT_WORKSPACE)) {
1.118 + super.setBooleanState(isViewOpened());
1.119 + }
1.120 + }
1.121 +}