001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: /*
043: * EnsureEventState.java
044: *
045: * Created on July 19, 2004, 12:06 PM
046: */
047:
048: package org.netbeans.modules.uml.core.eventframework;
049:
050: import org.netbeans.modules.uml.core.coreapplication.ICoreProduct;
051: import org.netbeans.modules.uml.core.support.Debug;
052: import org.netbeans.modules.uml.core.support.umlsupport.ProductRetriever;
053:
054: /**
055: * EnsureEventState's whole purpose in life is to ensure that events
056: * either fire, or do not fire, dependent on the flag used to instantiate
057: * the object.
058: *
059: * @author Trey Spiva
060: * @see IEventDispatcher
061: * @see IEventDispatchController
062: */
063: public class EnsureEventState {
064: IEventDispatcher m_Dispatch = null;
065: IEventDispatchController m_Controller = null;
066: boolean m_OrigFlag = false;
067: boolean m_PlugEvents = false;
068:
069: /**
070: *EnsureEventState constructor. Calls the setPreventAllEvents( true )
071: * on the passed in IEventDispatcher.
072: */
073: public EnsureEventState(boolean plugEvents,
074: IEventDispatcher dispatcher) {
075: m_Dispatch = dispatcher;
076: m_PlugEvents = plugEvents;
077:
078: if (m_Dispatch != null) {
079: m_OrigFlag = m_Dispatch.getPreventAllEvents();
080: if (m_OrigFlag != m_PlugEvents) {
081: m_Dispatch.setPreventAllEvents(m_PlugEvents);
082: }
083: }
084: }
085:
086: /**
087: *
088: * Prevents all event from going out on any and every dispatcher that the
089: * passed in controller manages, IF plugEvents is set to true. Otherwise,
090: * this makes sure that the events are NOT plugged.
091: * If the passed in controller is 0, then
092: * the CoreProduct is retrieved, and the EventDispatchController on that
093: * product is used.
094: *
095: */
096: public EnsureEventState(boolean plugEvents) {
097: initializeWithController(plugEvents, null);
098: }
099:
100: /**
101: *
102: * Prevents all event from going out on any and every dispatcher that the
103: * passed in controller manages, IF plugEvents is set to true. Otherwise,
104: * this makes sure that the events are NOT plugged.
105: * If the passed in controller is 0, then
106: * the CoreProduct is retrieved, and the EventDispatchController on that
107: * product is used.
108: *
109: * @param controller[in] The controller to prevent / enable events on. If null, the
110: * controller on the CoreProduct is used.
111: *
112: */
113: public EnsureEventState(boolean plugEvents,
114: IEventDispatchController controller) {
115: initializeWithController(plugEvents, controller);
116: }
117:
118: /**
119: * Initializes the Event state when a controler is involved.
120: */
121: protected void initializeWithController(boolean plugEvents,
122: IEventDispatchController controller) {
123: m_PlugEvents = plugEvents;
124:
125: ICoreProduct coreProd = null;
126: if (controller != null) {
127: m_Controller = controller;
128: } else {
129: coreProd = ProductRetriever.retrieveProduct();
130: Debug.assertNotNull(coreProd);
131: if (coreProd != null) {
132: m_Controller = coreProd.getEventDispatchController();
133: }
134: }
135:
136: if (m_Controller != null) {
137: m_OrigFlag = m_Controller.getPreventAllEvents();
138:
139: if (m_OrigFlag != m_PlugEvents) {
140: m_Controller.setPreventAllEvents(m_PlugEvents);
141: }
142: }
143: }
144:
145: /**
146: * EnsureEventState desctructor. Calls the put_PreventAllEvents( false)
147: * on the encapuslated IEventDispatcher.
148: */
149:
150: public void unPlug() {
151: if (m_Dispatch != null) {
152: m_Dispatch.setPreventAllEvents(m_OrigFlag);
153: }
154:
155: if (m_Controller != null) {
156: m_Controller.setPreventAllEvents(m_OrigFlag);
157: }
158: }
159:
160: }
|