KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > directory > ldapstudio > browser > ui > editors > searchresult > 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.ui.editors.searchresult;
22
23
24 import org.apache.directory.ldapstudio.valueeditors.ValueEditorManager;
25 import org.eclipse.jface.viewers.CellEditor;
26 import org.eclipse.jface.viewers.TableViewer;
27 import org.eclipse.swt.SWT;
28 import org.eclipse.swt.events.FocusEvent;
29 import org.eclipse.swt.events.FocusListener;
30 import org.eclipse.swt.events.KeyEvent;
31 import org.eclipse.swt.events.KeyListener;
32 import org.eclipse.swt.widgets.TableItem;
33
34
35 public abstract class AbstractOpenEditorAction extends AbstractSearchResultListenerAction implements FocusListener,
36     KeyListener
37 {
38
39     protected SearchResultEditorActionGroup actionGroup;
40
41     protected ValueEditorManager valueEditorManager;
42
43     protected TableViewer viewer;
44
45     protected SearchResultEditorCursor cursor;
46
47     protected CellEditor cellEditor;
48
49     private boolean isActive;
50
51
52     protected AbstractOpenEditorAction( TableViewer viewer, SearchResultEditorCursor cursor,
53         SearchResultEditorActionGroup actionGroup, ValueEditorManager valueEditorManager )
54     {
55         super( cursor, "Editor", null, null );
56         this.actionGroup = actionGroup;
57         this.viewer = viewer;
58         this.cursor = cursor;
59         this.valueEditorManager = valueEditorManager;
60         this.isActive = false;
61     }
62
63
64     public CellEditor getCellEditor()
65     {
66         return this.cellEditor;
67     }
68
69
70     public void run()
71     {
72         this.activateEditor();
73     }
74
75
76     private void activateEditor()
77     {
78
79         Object JavaDoc element = cursor.getRow().getData();
80         String JavaDoc property = ( String JavaDoc ) this.viewer.getColumnProperties()[cursor.getColumn()];
81
82         if ( !this.viewer.isCellEditorActive() && viewer.getCellModifier().canModify( element, property ) )
83         {
84
85             // check if attribute exists
86
/*
87              * if(element instanceof ISearchResult) { ISearchResult result =
88              * (ISearchResult)element; IAttribute attribute =
89              * result.getAttribute(property); if(attribute == null) {
90              * EventRegistry.suspendEventFireingInCurrentThread(); try {
91              * attribute = result.getEntry().createAttribute(property,
92              * this); System.out.println("activateEditor(): created
93              * attribute " + attribute); } catch (ModelModificationException
94              * e) { } EventRegistry.resumeEventFireingInCurrentThread(); } }
95              */

96
97             // set cell editor to viewer
98
for ( int i = 0; i < this.viewer.getCellEditors().length; i++ )
99             {
100                 this.viewer.getCellEditors()[i] = this.cellEditor;
101             }
102
103             // add listener for end of editing
104
if ( this.cellEditor.getControl() != null )
105             {
106                 this.cellEditor.getControl().addFocusListener( this );
107                 this.cellEditor.getControl().addKeyListener( this );
108             }
109
110             // deactivate cursor
111
this.cursor.setVisible( false );
112
113             // deactivate global actions
114
this.actionGroup.deactivateGlobalActionHandlers();
115
116             // start editing
117
this.isActive = true;
118             this.viewer.editElement( element, cursor.getColumn() );
119
120             viewer.setSelection( null, true );
121             viewer.getTable().setSelection( new TableItem[0] );
122
123             if ( !this.viewer.isCellEditorActive() )
124             {
125                 this.editorClosed();
126             }
127         }
128         else
129         {
130             this.valueEditorManager.setUserSelectedValueEditor( null );
131         }
132     }
133
134
135     private void editorClosed()
136     {
137
138         // check empty attribute
139
/*
140          * Object element = cursor.getRow().getData(); String property =
141          * (String)this.viewer.getColumnProperties()[cursor.getColumn()];
142          * if(element instanceof ISearchResult) { ISearchResult result =
143          * (ISearchResult)element; IAttribute attribute =
144          * result.getAttribute(property); if(attribute != null &&
145          * attribute.getValueSize() == 0) {
146          * EventRegistry.suspendEventFireingInCurrentThread(); try {
147          * result.getEntry().deleteAttribute(attribute, this);
148          * System.out.println("activateEditor(): deleted attribute " +
149          * attribute); } catch (ModelModificationException e) { }
150          * EventRegistry.resumeEventFireingInCurrentThread(); } }
151          */

152
153         // clear active flag
154
this.isActive = false;
155
156         // remove cell editors from viewer to prevend auto-editing
157
for ( int i = 0; i < this.viewer.getCellEditors().length; i++ )
158         {
159             this.viewer.getCellEditors()[i] = null;
160         }
161
162         // remove listener
163
if ( this.cellEditor.getControl() != null )
164         {
165             this.cellEditor.getControl().removeFocusListener( this );
166             this.cellEditor.getControl().removeKeyListener( this );
167         }
168
169         // activate global actions
170
this.actionGroup.activateGlobalActionHandlers();
171
172         this.valueEditorManager.setUserSelectedValueEditor( null );
173
174         // activate cursor
175
cursor.setVisible( true );
176         viewer.refresh();
177         cursor.redraw();
178         cursor.getDisplay().asyncExec( new Runnable JavaDoc()
179         {
180             public void run()
181             {
182                 cursor.setFocus();
183             }
184         } );
185
186     }
187
188
189     public void focusGained( FocusEvent e )
190     {
191     }
192
193
194     public void focusLost( FocusEvent e )
195     {
196         this.editorClosed();
197     }
198
199
200     public void keyPressed( KeyEvent e )
201     {
202         if ( e.character == SWT.ESC && e.stateMask == SWT.NONE )
203         {
204             e.doit = false;
205         }
206     }
207
208
209     public void keyReleased( KeyEvent e )
210     {
211     }
212
213
214     public boolean isActive()
215     {
216         return isActive;
217     }
218
219 }
220
Popular Tags