KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.util.Iterator JavaDoc;
25
26 import org.apache.directory.ldapstudio.browser.core.internal.model.Attribute;
27 import org.apache.directory.ldapstudio.browser.core.model.AttributeHierarchy;
28 import org.apache.directory.ldapstudio.browser.core.model.IAttribute;
29 import org.apache.directory.ldapstudio.browser.core.model.ISearchResult;
30 import org.apache.directory.ldapstudio.browser.core.model.ModelModificationException;
31 import org.apache.directory.ldapstudio.browser.core.model.schema.SchemaUtils;
32 import org.apache.directory.ldapstudio.browser.ui.BrowserUIConstants;
33 import org.apache.directory.ldapstudio.valueeditors.ValueEditorManager;
34 import org.eclipse.jface.dialogs.MessageDialog;
35 import org.eclipse.jface.viewers.ICellModifier;
36 import org.eclipse.jface.viewers.TableViewer;
37 import org.eclipse.swt.widgets.Item;
38
39
40 public class SearchResultEditorCellModifier implements ICellModifier
41 {
42
43     private TableViewer viewer;
44
45     private ValueEditorManager valueEditorManager;
46
47
48     public SearchResultEditorCellModifier( TableViewer viewer, ValueEditorManager valueEditorManager )
49     {
50         this.viewer = viewer;
51         this.valueEditorManager = valueEditorManager;
52     }
53
54
55     public void dispose()
56     {
57         this.viewer = null;
58         this.valueEditorManager = null;
59     }
60
61
62     public boolean canModify( Object JavaDoc element, String JavaDoc property )
63     {
64
65         if ( element != null && element instanceof ISearchResult && property != null )
66         {
67             ISearchResult result = ( ISearchResult ) element;
68             AttributeHierarchy ah = result.getAttributeWithSubtypes( property );
69
70             // check DN
71
if ( BrowserUIConstants.DN.equals( property ) )
72             {
73                 return false;
74             }
75
76             // attribute dummy
77
if ( ah == null )
78             {
79                 try
80                 {
81                     ah = new AttributeHierarchy( result.getEntry(), property, new IAttribute[]
82                         { new Attribute( result.getEntry(), property ) } );
83                 }
84                 catch ( ModelModificationException e )
85                 {
86                     e.printStackTrace();
87                     return false;
88                 }
89             }
90
91             // check schema modifyable
92
boolean isOneModifyable = false;
93             for ( Iterator JavaDoc it = ah.iterator(); it.hasNext(); )
94             {
95                 IAttribute attribute = ( IAttribute ) it.next();
96                 if ( SchemaUtils.isModifyable( attribute.getAttributeTypeDescription() ) )
97                 {
98                     isOneModifyable = true;
99                     break;
100                 }
101             }
102             if ( !isOneModifyable )
103             {
104                 return false;
105             }
106
107             // check if property is valid for the entry
108
boolean isOneValid = false;
109             for ( Iterator JavaDoc it = ah.iterator(); it.hasNext(); )
110             {
111                 IAttribute attribute = ( IAttribute ) it.next();
112                 if ( attribute.isObjectClassAttribute() || attribute.isMustAttribute() || attribute.isMayAttribute() )
113                 {
114                     isOneValid = true;
115                     break;
116                 }
117             }
118             if ( !isOneValid )
119             {
120                 return false;
121             }
122
123             // call value editor
124
return this.valueEditorManager.getCurrentValueEditor( ah ).getRawValue( ah ) != null;
125         }
126         else
127         {
128             return false;
129         }
130     }
131
132
133     public Object JavaDoc getValue( Object JavaDoc element, String JavaDoc property )
134     {
135
136         if ( element != null && element instanceof ISearchResult && property != null )
137         {
138             ISearchResult result = ( ISearchResult ) element;
139             AttributeHierarchy ah = result.getAttributeWithSubtypes( property );
140
141             if ( !this.canModify( element, property ) )
142             {
143                 return null;
144             }
145
146             if ( ah == null )
147             {
148                 try
149                 {
150                     ah = new AttributeHierarchy( result.getEntry(), property, new IAttribute[]
151                         { new Attribute( result.getEntry(), property ) } );
152                 }
153                 catch ( ModelModificationException e )
154                 {
155                     e.printStackTrace();
156                     return null;
157                 }
158             }
159
160             return this.valueEditorManager.getCurrentValueEditor( ah ).getRawValue( ah );
161         }
162         else
163         {
164             return null;
165         }
166     }
167
168
169     public void modify( Object JavaDoc element, String JavaDoc property, Object JavaDoc newRawValue )
170     {
171
172         if ( element != null && element instanceof Item )
173         {
174             element = ( ( Item ) element ).getData();
175         }
176
177         if ( element != null && element instanceof ISearchResult && property != null )
178         {
179             ISearchResult result = ( ISearchResult ) element;
180             AttributeHierarchy ah = result.getAttributeWithSubtypes( property );
181
182             try
183             {
184                 // switch operation:
185
if ( ah == null && newRawValue != null )
186                 {
187                     this.valueEditorManager.getCurrentValueEditor( result.getEntry(), property ).createValue(
188                         result.getEntry(), property, newRawValue );
189                 }
190                 else if ( ah != null && newRawValue == null )
191                 {
192                     this.valueEditorManager.getCurrentValueEditor( ah ).deleteAttribute( ah );
193                 }
194                 else if ( ah != null && ah.size() == 1 && ah.getAttribute().getValueSize() == 1 && newRawValue != null )
195                 {
196                     this.valueEditorManager.getCurrentValueEditor( ah ).modifyValue( ah.getAttribute().getValues()[0],
197                         newRawValue );
198                 }
199             }
200             catch ( ModelModificationException mme )
201             {
202                 MessageDialog.openError( this.viewer.getTable().getShell(), "Error While Modifying Value", mme
203                     .getMessage() );
204             }
205         }
206     }
207
208 }
209
Popular Tags