KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > directory > ldapstudio > browser > ui > editors > searchresult > AbstractSearchResultListenerAction


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.ui.editors.searchresult;
22
23
24 import org.apache.directory.ldapstudio.browser.common.BrowserCommonActivator;
25 import org.apache.directory.ldapstudio.browser.common.actions.SelectionUtils;
26 import org.apache.directory.ldapstudio.browser.core.events.EntryModificationEvent;
27 import org.apache.directory.ldapstudio.browser.core.events.EntryUpdateListener;
28 import org.apache.directory.ldapstudio.browser.core.events.EventRegistry;
29 import org.apache.directory.ldapstudio.browser.core.model.AttributeHierarchy;
30 import org.apache.directory.ldapstudio.browser.core.model.ISearch;
31 import org.apache.directory.ldapstudio.browser.core.model.ISearchResult;
32
33 import org.eclipse.jface.action.Action;
34 import org.eclipse.jface.resource.ImageDescriptor;
35 import org.eclipse.jface.viewers.ISelection;
36 import org.eclipse.jface.viewers.ISelectionChangedListener;
37 import org.eclipse.jface.viewers.ISelectionProvider;
38 import org.eclipse.jface.viewers.SelectionChangedEvent;
39
40
41 public abstract class AbstractSearchResultListenerAction extends Action implements ISelectionChangedListener,
42     EntryUpdateListener
43 {
44
45     protected ISelectionProvider selectionProvider;
46
47     protected ISearch selectedSearch;
48
49     protected ISearchResult selectedSearchResult;
50
51     protected AttributeHierarchy selectedAttributeHierarchie;
52
53     protected String JavaDoc selectedProperty;
54
55
56     AbstractSearchResultListenerAction( ISelectionProvider selectionProvider, String JavaDoc title, ImageDescriptor image,
57         String JavaDoc command, int style )
58     {
59         super( title, style );
60         super.setText( title );
61         super.setToolTipText( title );
62         super.setImageDescriptor( image );
63         super.setActionDefinitionId( command );
64         super.setEnabled( false );
65
66         this.selectionProvider = selectionProvider;
67
68         this.init();
69     }
70
71
72     AbstractSearchResultListenerAction( ISelectionProvider selectionProvider, String JavaDoc title, ImageDescriptor image,
73         String JavaDoc command )
74     {
75         this( selectionProvider, title, image, command, Action.AS_PUSH_BUTTON );
76     }
77
78
79     private void init()
80     {
81         this.selectionProvider.addSelectionChangedListener( this );
82         EventRegistry.addEntryUpdateListener( this, BrowserCommonActivator.getDefault().getEventRunner() );
83
84         this.selectedSearch = null;
85         this.selectedSearchResult = null;
86         this.selectedAttributeHierarchie = null;
87         this.selectedProperty = null;
88     }
89
90
91     public void selectionChanged( SelectionChangedEvent event )
92     {
93
94         ISelection selection = event.getSelection();
95
96         ISearchResult[] searchResults = SelectionUtils.getSearchResults( selection );
97         AttributeHierarchy[] ah = SelectionUtils.getAttributeHierarchie( selection );
98         String JavaDoc[] selectedProperties = SelectionUtils.getProperties( selection );
99
100         if ( searchResults.length == 1 )
101         {
102             this.selectedSearchResult = searchResults[0];
103             this.selectedSearch = this.selectedSearchResult.getSearch();
104         }
105         else
106         {
107             this.selectedSearchResult = null;
108             this.selectedSearch = null;
109         }
110
111         if ( ah.length == 1 )
112         {
113             this.selectedAttributeHierarchie = ah[0];
114         }
115         else
116         {
117             this.selectedAttributeHierarchie = null;
118         }
119
120         if ( selectedProperties.length == 1 )
121         {
122             this.selectedProperty = selectedProperties[0];
123         }
124         else
125         {
126             this.selectedProperty = null;
127         }
128
129         this.updateEnabledState();
130     }
131
132
133     public final void entryUpdated( EntryModificationEvent event )
134     {
135         this.updateEnabledState();
136     }
137
138
139     protected abstract void updateEnabledState();
140
141
142     public void dispose()
143     {
144         EventRegistry.removeEntryUpdateListener( this );
145         this.selectionProvider.removeSelectionChangedListener( this );
146
147         this.selectedSearchResult = null;
148         this.selectedAttributeHierarchie = null;
149         this.selectedSearch = null;
150         this.selectedProperty = null;
151     }
152
153 }
154
Popular Tags