KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > directory > ldapstudio > browser > common > widgets > entryeditor > EntryEditorWidgetUniversalListener


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.widgets.entryeditor;
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.BulkModificationEvent;
27 import org.apache.directory.ldapstudio.browser.core.events.EmptyValueAddedEvent;
28 import org.apache.directory.ldapstudio.browser.core.events.EmptyValueDeletedEvent;
29 import org.apache.directory.ldapstudio.browser.core.events.EntryModificationEvent;
30 import org.apache.directory.ldapstudio.browser.core.events.EntryUpdateListener;
31 import org.apache.directory.ldapstudio.browser.core.events.EventRegistry;
32 import org.apache.directory.ldapstudio.browser.core.events.ValueAddedEvent;
33 import org.apache.directory.ldapstudio.browser.core.events.ValueDeletedEvent;
34 import org.apache.directory.ldapstudio.browser.core.events.ValueModifiedEvent;
35 import org.apache.directory.ldapstudio.browser.core.events.ValueRenamedEvent;
36 import org.apache.directory.ldapstudio.browser.core.model.IAttribute;
37 import org.apache.directory.ldapstudio.browser.core.model.IValue;
38 import org.eclipse.jface.viewers.StructuredSelection;
39 import org.eclipse.jface.viewers.TreeViewer;
40 import org.eclipse.swt.events.MouseAdapter;
41 import org.eclipse.swt.events.MouseEvent;
42 import org.eclipse.swt.events.MouseListener;
43 import org.eclipse.swt.events.SelectionAdapter;
44 import org.eclipse.swt.events.SelectionEvent;
45 import org.eclipse.swt.events.SelectionListener;
46
47
48 /**
49  * The EntryEditorWidgetUniversalListener manages all events for the entry editor widget.
50  *
51  * @author <a HREF="mailto:dev@directory.apache.org">Apache Directory Project</a>
52  * @version $Rev$, $Date$
53  */

54 public class EntryEditorWidgetUniversalListener implements EntryUpdateListener
55 {
56
57     /** The tree viewer */
58     protected TreeViewer viewer;
59
60     /** The action used to start the default value editor */
61     protected OpenDefaultEditorAction startEditAction;
62
63     /** This listener starts the value editor when pressing enter */
64     protected SelectionListener viewerSelectionListener = new SelectionAdapter()
65     {
66         /**
67          * {@inheritDoc}
68          */

69         public void widgetSelected( SelectionEvent e )
70         {
71         }
72
73
74         /**
75          * {@inheritDoc}
76          *
77          * This implementation starts the value editor.
78          */

79         public void widgetDefaultSelected( SelectionEvent e )
80         {
81             if ( startEditAction.isEnabled() )
82                 startEditAction.run();
83         }
84     };
85
86     /** This listener starts the value editor or expands/collapses the selected attribute */
87     protected MouseListener viewerMouseListener = new MouseAdapter()
88     {
89         /**
90          * {@inheritDoc}
91          *
92          * This implementation starts the value editor or expands/collapses the selected attribute.
93          */

94         public void mouseDoubleClick( MouseEvent e )
95         {
96             if ( startEditAction.isEnabled() )
97             {
98                 startEditAction.run();
99             }
100
101             IAttribute[] attributes = SelectionUtils.getAttributes( viewer.getSelection() );
102             IValue[] values = SelectionUtils.getValues( viewer.getSelection() );
103             if ( attributes.length == 1 && values.length == 0 )
104             {
105                 if ( viewer.getExpandedState( attributes[0] ) )
106                 {
107                     viewer.collapseToLevel( attributes[0], 1 );
108                 }
109                 else
110                 {
111                     viewer.expandToLevel( attributes[0], 1 );
112                 }
113             }
114         }
115
116         /**
117          * {@inheritDoc}
118          */

119         public void mouseDown( MouseEvent e )
120         {
121         }
122
123         /**
124          * {@inheritDoc}
125          */

126         public void mouseUp( MouseEvent e )
127         {
128         }
129     };
130
131
132     /**
133      * Creates a new instance of EntryEditorWidgetUniversalListener.
134      *
135      * @param treeViewer the tree viewer
136      * @param startEditAction the action used to start the default value editor
137      */

138     public EntryEditorWidgetUniversalListener( TreeViewer treeViewer, OpenDefaultEditorAction startEditAction )
139     {
140         this.startEditAction = startEditAction;
141         this.viewer = treeViewer;
142
143         // register listeners
144
viewer.getTree().addSelectionListener( viewerSelectionListener );
145         viewer.getTree().addMouseListener( viewerMouseListener );
146         EventRegistry.addEntryUpdateListener( this, BrowserCommonActivator.getDefault().getEventRunner() );
147     }
148
149
150     /**
151      * Disposes this universal listener.
152      */

153     public void dispose()
154     {
155         if ( viewer != null )
156         {
157             EventRegistry.removeEntryUpdateListener( this );
158
159             startEditAction = null;
160             viewer = null;
161         }
162     }
163
164
165     /**
166      * {@inheritDoc}
167      *
168      * This implementation refreshes the viewer and selects a value depending
169      * on the event.
170      */

171     public void entryUpdated( EntryModificationEvent event )
172     {
173
174         if ( viewer == null || viewer.getTree() == null || viewer.getTree().isDisposed() || viewer.getInput() == null
175             || ( event.getModifiedEntry() != viewer.getInput() && !( event instanceof BulkModificationEvent ) ) )
176         {
177             return;
178         }
179
180         // force closing of cell editors
181
if ( viewer.isCellEditorActive() )
182         {
183             viewer.cancelEditing();
184         }
185
186         // refresh
187
viewer.refresh();
188
189         // selection value
190
if ( event instanceof ValueAddedEvent )
191         {
192             // select the vadded value
193
ValueAddedEvent vaEvent = ( ValueAddedEvent ) event;
194             viewer.setSelection( new StructuredSelection( vaEvent.getAddedValue() ), true );
195             viewer.refresh();
196         }
197         else if ( event instanceof ValueDeletedEvent )
198         {
199             // select another value of the deleted attribute
200
ValueDeletedEvent vdEvent = ( ValueDeletedEvent ) event;
201             if ( viewer.getSelection().isEmpty() && vdEvent.getDeletedValue().getAttribute().getValueSize() > 0 )
202             {
203                 viewer.setSelection(
204                     new StructuredSelection( vdEvent.getDeletedValue().getAttribute().getValues()[0] ), true );
205             }
206         }
207         else if ( event instanceof EmptyValueAddedEvent )
208         {
209             // select the added value and start editing
210
EmptyValueAddedEvent evaEvent = ( EmptyValueAddedEvent ) event;
211             viewer.setSelection( new StructuredSelection( evaEvent.getAddedValue() ), true );
212             if ( startEditAction.isEnabled() )
213             {
214                 startEditAction.run();
215             }
216         }
217         else if ( event instanceof EmptyValueDeletedEvent )
218         {
219             // select another value of the deleted attribute
220
EmptyValueDeletedEvent evdEvent = ( EmptyValueDeletedEvent ) event;
221             if ( viewer.getSelection().isEmpty() && evdEvent.getDeletedValue().getAttribute().getValueSize() > 0 )
222             {
223                 viewer.setSelection(
224                     new StructuredSelection( evdEvent.getDeletedValue().getAttribute().getValues()[0] ), true );
225             }
226         }
227         else if ( event instanceof ValueModifiedEvent )
228         {
229             // select the modified value
230
ValueModifiedEvent vmEvent = ( ValueModifiedEvent ) event;
231             viewer.setSelection( new StructuredSelection( vmEvent.getNewValue() ), true );
232         }
233         else if ( event instanceof ValueRenamedEvent )
234         {
235             // select the renamed value
236
ValueRenamedEvent vrEvent = ( ValueRenamedEvent ) event;
237             viewer.setSelection( new StructuredSelection( vrEvent.getNewValue() ), true );
238         }
239     }
240
241 }
242
Popular Tags