KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > directory > ldapstudio > browser > common > actions > proxy > BrowserActionProxy


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  *
19  */

20
21 package org.apache.directory.ldapstudio.browser.common.actions.proxy;
22
23
24 import org.apache.directory.ldapstudio.browser.common.BrowserCommonActivator;
25 import org.apache.directory.ldapstudio.browser.common.actions.BrowserAction;
26 import org.apache.directory.ldapstudio.browser.common.actions.SelectionUtils;
27 import org.apache.directory.ldapstudio.browser.core.events.BookmarkUpdateEvent;
28 import org.apache.directory.ldapstudio.browser.core.events.BookmarkUpdateListener;
29 import org.apache.directory.ldapstudio.browser.core.events.ConnectionUpdateEvent;
30 import org.apache.directory.ldapstudio.browser.core.events.ConnectionUpdateListener;
31 import org.apache.directory.ldapstudio.browser.core.events.EntryModificationEvent;
32 import org.apache.directory.ldapstudio.browser.core.events.EntryUpdateListener;
33 import org.apache.directory.ldapstudio.browser.core.events.EventRegistry;
34 import org.apache.directory.ldapstudio.browser.core.events.SearchUpdateEvent;
35 import org.apache.directory.ldapstudio.browser.core.events.SearchUpdateListener;
36 import org.eclipse.jface.action.Action;
37 import org.eclipse.jface.viewers.ISelection;
38 import org.eclipse.jface.viewers.ISelectionChangedListener;
39 import org.eclipse.jface.viewers.ISelectionProvider;
40 import org.eclipse.jface.viewers.SelectionChangedEvent;
41 import org.eclipse.jface.viewers.StructuredSelection;
42
43
44 public abstract class BrowserActionProxy extends Action implements ISelectionChangedListener, EntryUpdateListener,
45     SearchUpdateListener, BookmarkUpdateListener, ConnectionUpdateListener
46 {
47
48     protected BrowserAction action;
49
50     protected ISelectionProvider selectionProvider;
51
52
53     protected BrowserActionProxy( ISelectionProvider selectionProvider, BrowserAction action, int style )
54     {
55         super( action.getText(), style );
56         this.selectionProvider = selectionProvider;
57         this.action = action;
58
59         super.setImageDescriptor( action.getImageDescriptor() );
60         super.setActionDefinitionId( action.getCommandId() );
61
62         this.selectionProvider.addSelectionChangedListener( this );
63         // PlatformUI.getWorkbench().getActiveWorkbenchWindow().getSelectionService().addSelectionListener(this);
64

65         EventRegistry.addConnectionUpdateListener( this, BrowserCommonActivator.getDefault().getEventRunner() );
66         EventRegistry.addEntryUpdateListener( this, BrowserCommonActivator.getDefault().getEventRunner() );
67         EventRegistry.addSearchUpdateListener( this, BrowserCommonActivator.getDefault().getEventRunner() );
68         EventRegistry.addBookmarkUpdateListener( this, BrowserCommonActivator.getDefault().getEventRunner() );
69
70         this.updateAction();
71
72     }
73
74
75     protected BrowserActionProxy( ISelectionProvider selectionProvider, BrowserAction action )
76     {
77         this( selectionProvider, action, Action.AS_PUSH_BUTTON );
78     }
79
80
81     public void dispose()
82     {
83         EventRegistry.removeConnectionUpdateListener( this );
84         EventRegistry.removeEntryUpdateListener( this );
85         EventRegistry.removeSearchUpdateListener( this );
86         EventRegistry.removeBookmarkUpdateListener( this );
87         this.selectionProvider.removeSelectionChangedListener( this );
88         // PlatformUI.getWorkbench().getActiveWorkbenchWindow().getSelectionService().removeSelectionListener(this);
89

90         this.action.dispose();
91         this.action = null;
92     }
93
94
95     public boolean isDisposed()
96     {
97         return this.action == null;
98     }
99
100
101     public final void entryUpdated( EntryModificationEvent entryModificationEvent )
102     {
103         if ( !this.isDisposed() )
104         {
105             this.action.entryUpdated( entryModificationEvent );
106             this.updateAction();
107         }
108     }
109
110
111     public void searchUpdated( SearchUpdateEvent searchUpdateEvent )
112     {
113         if ( !this.isDisposed() )
114         {
115             this.action.searchUpdated( searchUpdateEvent );
116             this.updateAction();
117         }
118     }
119
120
121     public void bookmarkUpdated( BookmarkUpdateEvent bookmarkUpdateEvent )
122     {
123         if ( !this.isDisposed() )
124         {
125             this.action.bookmarkUpdated( bookmarkUpdateEvent );
126             this.updateAction();
127         }
128     }
129
130
131     public final void connectionUpdated( ConnectionUpdateEvent connectionUpdateEvent )
132     {
133         if ( !this.isDisposed() )
134         {
135             this.action.connectionUpdated( connectionUpdateEvent );
136             this.updateAction();
137         }
138     }
139
140
141     public void inputChanged( Object JavaDoc input )
142     {
143         if ( !this.isDisposed() )
144         {
145             this.action.setInput( input );
146             this.selectionChanged( new SelectionChangedEvent( this.selectionProvider, new StructuredSelection() ) );
147             // this.updateAction();
148
}
149     }
150
151
152     public void selectionChanged( SelectionChangedEvent event )
153     {
154         if ( !this.isDisposed() )
155         {
156             ISelection selection = event.getSelection();
157
158             this.action.setSelectedConnections( SelectionUtils.getConnections( selection ) );
159
160             this.action.setSelectedBrowserViewCategories( SelectionUtils.getBrowserViewCategories( selection ) );
161             this.action.setSelectedEntries( SelectionUtils.getEntries( selection ) );
162             this.action.setSelectedBrowserEntryPages( SelectionUtils.getBrowserEntryPages( selection ) );
163             this.action.setSelectedSearchResults( SelectionUtils.getSearchResults( selection ) );
164             this.action.setSelectedBrowserSearchResultPages( SelectionUtils.getBrowserSearchResultPages( selection ) );
165             this.action.setSelectedBookmarks( SelectionUtils.getBookmarks( selection ) );
166
167             this.action.setSelectedSearches( SelectionUtils.getSearches( selection ) );
168
169             this.action.setSelectedAttributes( SelectionUtils.getAttributes( selection ) );
170             this.action.setSelectedAttributeHierarchies( SelectionUtils.getAttributeHierarchie( selection ) );
171             this.action.setSelectedValues( SelectionUtils.getValues( selection ) );
172             this.updateAction();
173         }
174     }
175
176
177     public void updateAction()
178     {
179         if ( !this.isDisposed() )
180         {
181             this.setText( this.action.getText() );
182             this.setToolTipText( this.action.getText() );
183             this.setEnabled( this.action.isEnabled() );
184             this.setImageDescriptor( this.action.getImageDescriptor() );
185         }
186     }
187
188
189     public void run()
190     {
191         if ( !this.isDisposed() )
192         {
193             this.action.run();
194         }
195     }
196
197
198     public BrowserAction getAction()
199     {
200         return action;
201     }
202
203 }
204
Popular Tags