KickJava   Java API By Example, From Geeks To Geeks.

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


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.actions.BrowserAction;
25 import org.apache.directory.ldapstudio.valueeditors.ValueEditorManager;
26 import org.eclipse.jface.viewers.CellEditor;
27 import org.eclipse.jface.viewers.TreeViewer;
28 import org.eclipse.swt.SWT;
29 import org.eclipse.swt.events.FocusEvent;
30 import org.eclipse.swt.events.FocusListener;
31 import org.eclipse.swt.events.KeyEvent;
32 import org.eclipse.swt.events.KeyListener;
33
34
35 /**
36  * The base class for all value editor actions of the entry editor widget.
37  * It manages activation and closing of value editors.
38  *
39  * @author <a HREF="mailto:dev@directory.apache.org">Apache Directory Project</a>
40  * @version $Rev$, $Date$
41  */

42 public abstract class AbstractOpenEditorAction extends BrowserAction implements FocusListener, KeyListener
43 {
44
45     /** The action group. */
46     protected EntryEditorWidgetActionGroup actionGroup;
47
48     /** The value editor manager. */
49     protected ValueEditorManager valueEditorManager;
50
51     /** The viewer. */
52     protected TreeViewer viewer;
53
54     /** The cell editor. */
55     protected CellEditor cellEditor;
56
57
58     /**
59      * Creates a new instance of AbstractOpenEditorAction.
60      *
61      * @param viewer the viewer
62      * @param actionGroup the action group
63      * @param valueEditorManager the value editor manager
64      */

65     protected AbstractOpenEditorAction( TreeViewer viewer, EntryEditorWidgetActionGroup actionGroup,
66         ValueEditorManager valueEditorManager )
67     {
68         this.viewer = viewer;
69         this.actionGroup = actionGroup;
70         this.valueEditorManager = valueEditorManager;
71     }
72
73
74     /**
75      * {@inheritDoc}
76      */

77     public void dispose()
78     {
79         valueEditorManager = null;
80         actionGroup = null;
81         viewer = null;
82         cellEditor = null;
83         super.dispose();
84     }
85
86
87     /**
88      * Gets the cell editor.
89      *
90      * @return the cell editor
91      */

92     public CellEditor getCellEditor()
93     {
94         return cellEditor;
95     }
96
97
98     /**
99      * {@inheritDoc}
100      */

101     public void run()
102     {
103         activateEditor();
104     }
105
106
107     /**
108      * Activates the editor.
109      */

110     private void activateEditor()
111     {
112
113         if ( !viewer.isCellEditorActive()
114             && getSelectedValues().length == 1
115             && getSelectedAttributes().length == 0
116             && viewer.getCellModifier().canModify( getSelectedValues()[0],
117                 EntryEditorWidgetTableMetadata.VALUE_COLUMN_NAME ) )
118         {
119
120             // set cell editor to viewer
121
viewer.getCellEditors()[EntryEditorWidgetTableMetadata.VALUE_COLUMN_INDEX] = cellEditor;
122
123             // add listener for end of editing
124
if ( cellEditor.getControl() != null )
125             {
126                 cellEditor.getControl().addFocusListener( this );
127                 cellEditor.getControl().addKeyListener( this );
128             }
129
130             // deactivate global actions
131
if ( actionGroup != null )
132             {
133                 actionGroup.deactivateGlobalActionHandlers();
134             }
135
136             // start editing
137
viewer.editElement( getSelectedValues()[0], EntryEditorWidgetTableMetadata.VALUE_COLUMN_INDEX );
138
139             if ( !viewer.isCellEditorActive() )
140             {
141                 editorClosed();
142             }
143         }
144         else
145         {
146             valueEditorManager.setUserSelectedValueEditor( null );
147         }
148     }
149
150
151     /**
152      * Editor closed.
153      */

154     private void editorClosed()
155     {
156
157         // remove cell editors from viewer to prevend auto-editing
158
for ( int i = 0; i < viewer.getCellEditors().length; i++ )
159         {
160             viewer.getCellEditors()[i] = null;
161         }
162
163         // remove listener
164
if ( cellEditor.getControl() != null )
165         {
166             cellEditor.getControl().removeFocusListener( this );
167             cellEditor.getControl().removeKeyListener( this );
168         }
169
170         // activate global actions
171
if ( actionGroup != null )
172         {
173             actionGroup.activateGlobalActionHandlers();
174         }
175
176         // reset custom value editor and set selection to notify all
177
// openeditoractions to update their enabled state.
178
valueEditorManager.setUserSelectedValueEditor( null );
179         viewer.setSelection( viewer.getSelection() );
180     }
181
182
183     /**
184      * {@inheritDoc}
185      */

186     public void focusGained( FocusEvent e )
187     {
188     }
189
190
191     /**
192      * {@inheritDoc}
193      */

194     public void focusLost( FocusEvent e )
195     {
196         editorClosed();
197     }
198
199
200     /**
201      * {@inheritDoc}
202      */

203     public void keyPressed( KeyEvent e )
204     {
205         if ( e.character == SWT.ESC && e.stateMask == SWT.NONE )
206         {
207             e.doit = false;
208         }
209     }
210
211
212     /**
213      * {@inheritDoc}
214      */

215     public void keyReleased( KeyEvent e )
216     {
217     }
218
219 }
220
Popular Tags