KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > directory > ldapstudio > ldifeditor > editor > actions > AbstractOpenValueEditorAction


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.ldifeditor.editor.actions;
22
23
24 import org.apache.directory.ldapstudio.browser.core.internal.model.DummyConnection;
25 import org.apache.directory.ldapstudio.browser.core.model.IConnection;
26 import org.apache.directory.ldapstudio.browser.core.model.ldif.LdifPart;
27 import org.apache.directory.ldapstudio.browser.core.model.ldif.lines.LdifAttrValLine;
28 import org.apache.directory.ldapstudio.browser.core.model.ldif.lines.LdifControlLine;
29 import org.apache.directory.ldapstudio.browser.core.model.ldif.lines.LdifDeloldrdnLine;
30 import org.apache.directory.ldapstudio.browser.core.model.ldif.lines.LdifDnLine;
31 import org.apache.directory.ldapstudio.browser.core.model.ldif.lines.LdifNewrdnLine;
32 import org.apache.directory.ldapstudio.browser.core.model.ldif.lines.LdifNewsuperiorLine;
33 import org.apache.directory.ldapstudio.browser.core.model.ldif.lines.LdifValueLineBase;
34 import org.apache.directory.ldapstudio.browser.core.model.schema.Schema;
35 import org.apache.directory.ldapstudio.ldifeditor.editor.LdifEditor;
36 import org.apache.directory.ldapstudio.valueeditors.AbstractDialogValueEditor;
37 import org.apache.directory.ldapstudio.valueeditors.IValueEditor;
38 import org.apache.directory.ldapstudio.valueeditors.ValueEditorManager;
39 import org.eclipse.jface.text.BadLocationException;
40 import org.eclipse.jface.text.IDocument;
41
42
43 public abstract class AbstractOpenValueEditorAction extends AbstractLdifAction
44 {
45
46     protected ValueEditorManager valueEditorManager;
47
48     protected IValueEditor valueEditor;
49
50
51     public AbstractOpenValueEditorAction( LdifEditor editor )
52     {
53         super( "Edit Value", editor );
54         valueEditorManager = editor.getValueEditorManager();
55     }
56
57
58     public Object JavaDoc getValueEditor()
59     {
60         return valueEditor;
61     }
62
63
64     protected void doRun()
65     {
66
67         LdifPart[] parts = getSelectedLdifParts();
68         if ( parts.length == 1 && ( parts[0] instanceof LdifValueLineBase ) )
69         {
70             LdifValueLineBase line = ( LdifValueLineBase ) parts[0];
71
72             String JavaDoc attributeDescription = getAttributeDescription();
73             Object JavaDoc rawValue = getValueEditorRawValue();
74
75             if ( valueEditor instanceof AbstractDialogValueEditor )
76             {
77                 AbstractDialogValueEditor cellEditor = ( AbstractDialogValueEditor ) valueEditor;
78                 cellEditor.setValue( rawValue );
79                 cellEditor.activate();
80                 Object JavaDoc newValue = cellEditor.getValue();
81
82                 if ( newValue != null && newValue instanceof String JavaDoc || newValue instanceof byte[] )
83                 {
84                     IDocument document = editor.getDocumentProvider().getDocument( editor.getEditorInput() );
85
86                     LdifValueLineBase newLine;
87                     if ( line instanceof LdifControlLine )
88                     {
89                         LdifControlLine oldControlLine = ( LdifControlLine ) line;
90                         if ( newValue instanceof String JavaDoc )
91                         {
92                             newLine = LdifControlLine.create( oldControlLine.getUnfoldedOid(), oldControlLine
93                                 .getUnfoldedCriticality(), ( String JavaDoc ) newValue );
94                         }
95                         else
96                         {
97                             newLine = LdifControlLine.create( oldControlLine.getUnfoldedOid(), oldControlLine
98                                 .getUnfoldedCriticality(), ( byte[] ) newValue );
99                         }
100                     }
101                     else
102                     {
103                         if ( newValue instanceof String JavaDoc )
104                         {
105                             newLine = LdifAttrValLine.create( attributeDescription, ( String JavaDoc ) newValue );
106                         }
107                         else
108                         {
109                             newLine = LdifAttrValLine.create( attributeDescription, ( byte[] ) newValue );
110                         }
111                     }
112
113                     try
114                     {
115                         document.replace( line.getOffset(), line.getLength(), newLine.toFormattedString() );
116                     }
117                     catch ( BadLocationException e )
118                     {
119                         e.printStackTrace();
120                     }
121
122                 }
123             }
124         }
125     }
126
127
128     protected IConnection getConnection()
129     {
130         return editor.getConnection() != null ? editor.getConnection() : new DummyConnection( Schema.DEFAULT_SCHEMA );
131     }
132
133
134     protected Object JavaDoc getValueEditorRawValue()
135     {
136         Object JavaDoc rawValue = null;
137         Object JavaDoc value = getValue();
138         if ( value != null )
139         {
140             IConnection connection = getConnection();
141             rawValue = valueEditor.getRawValue( connection, value );
142         }
143
144         return rawValue;
145     }
146
147
148     protected Object JavaDoc getValue()
149     {
150         LdifPart[] parts = getSelectedLdifParts();
151         Object JavaDoc oldValue = null;
152         if ( parts.length == 1 && ( parts[0] instanceof LdifValueLineBase ) )
153         {
154             LdifValueLineBase line = ( LdifValueLineBase ) parts[0];
155             oldValue = line.getValueAsObject();
156
157             if ( line instanceof LdifControlLine )
158             {
159                 oldValue = ( ( LdifControlLine ) line ).getUnfoldedControlValue();
160             }
161
162         }
163         return oldValue;
164     }
165
166
167     protected String JavaDoc getAttributeDescription()
168     {
169         String JavaDoc attributeDescription = null;
170         LdifPart[] parts = getSelectedLdifParts();
171         if ( parts.length == 1 && ( parts[0] instanceof LdifValueLineBase ) )
172         {
173             LdifValueLineBase line = ( LdifValueLineBase ) parts[0];
174
175             if ( line instanceof LdifControlLine )
176             {
177                 attributeDescription = "";
178             }
179             else
180             {
181                 attributeDescription = line.getUnfoldedLineStart();
182             }
183         }
184         return attributeDescription;
185     }
186
187
188     protected boolean isEditableLineSelected()
189     {
190         LdifPart[] parts = getSelectedLdifParts();
191         boolean b = parts.length == 1
192             && ( parts[0] instanceof LdifAttrValLine || parts[0] instanceof LdifDnLine
193                 || parts[0] instanceof LdifControlLine || parts[0] instanceof LdifNewrdnLine
194                 || parts[0] instanceof LdifDeloldrdnLine || parts[0] instanceof LdifNewsuperiorLine );
195         return b;
196     }
197
198 }
199
Popular Tags