KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > directory > ldapstudio > aciitemeditor > valueeditors > AttributeTypeAndValueValueEditor


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.aciitemeditor.valueeditors;
22
23
24 import org.apache.directory.ldapstudio.browser.common.dialogs.TextDialog;
25 import org.apache.directory.ldapstudio.browser.core.model.IConnection;
26 import org.apache.directory.ldapstudio.browser.core.model.IValue;
27 import org.apache.directory.ldapstudio.browser.core.model.schema.Schema;
28 import org.apache.directory.ldapstudio.valueeditors.AbstractDialogStringValueEditor;
29 import org.eclipse.swt.widgets.Shell;
30
31
32 /**
33  * Implementation of IValueEditor for attribute type and value.
34  *
35  * @author <a HREF="mailto:dev@directory.apache.org">Apache Directory Project</a>
36  * @version $Rev$, $Date$
37  */

38 public class AttributeTypeAndValueValueEditor extends AbstractDialogStringValueEditor
39 {
40
41     private static final String JavaDoc EMPTY = ""; //$NON-NLS-1$
42

43
44     /**
45      * {@inheritDoc}
46      *
47      * This implementation opens the AttributeTypeDialog.
48      */

49     public boolean openDialog( Shell shell )
50     {
51         Object JavaDoc value = getValue();
52         if ( value != null && value instanceof AttributeTypeAndValueValueEditorRawValueWrapper )
53         {
54             AttributeTypeAndValueValueEditorRawValueWrapper wrapper = ( AttributeTypeAndValueValueEditorRawValueWrapper ) value;
55             AttributeTypeAndValueDialog dialog = new AttributeTypeAndValueDialog( shell, wrapper.schema,
56                 wrapper.attributeType, wrapper.value );
57             if ( dialog.open() == TextDialog.OK && !EMPTY.equals( dialog.getAttributeType() )
58                 && !EMPTY.equals( dialog.getValue() ) )
59             {
60                 setValue( dialog.getAttributeType() + '=' + dialog.getValue() );
61                 return true;
62             }
63         }
64         return false;
65     }
66
67
68     /**
69      * {@inheritDoc}
70      *
71      * Returns an AttributeTypeAndValueValueEditorRawValueWrapper.
72      */

73     public Object JavaDoc getRawValue( IValue value )
74     {
75         return value != null ? getRawValue( value.getAttribute().getEntry().getConnection(), value.getStringValue() )
76             : null;
77     }
78
79
80     /**
81      * {@inheritDoc}
82      *
83      * Returns an AttributeTypeAndValueValueEditorRawValueWrapper.
84      */

85     public Object JavaDoc getRawValue( IConnection connection, Object JavaDoc value )
86     {
87         Schema schema = null;
88         if ( connection != null )
89         {
90             schema = connection.getSchema();
91         }
92         if ( schema == null || value == null || !( value instanceof String JavaDoc ) )
93         {
94             return null;
95         }
96
97         String JavaDoc atavValue = ( String JavaDoc ) value;
98         String JavaDoc[] atav = atavValue.split( "=", 2 ); //$NON-NLS-1$
99
String JavaDoc at = atav.length > 0 ? atav[0] : EMPTY;
100         String JavaDoc v = atav.length > 1 ? atav[1] : EMPTY;
101         AttributeTypeAndValueValueEditorRawValueWrapper wrapper = new AttributeTypeAndValueValueEditorRawValueWrapper(
102             schema, at, v );
103         return wrapper;
104     }
105
106     /**
107      * The AttributeTypeAndValueValueEditorRawValueWrapper is used to pass contextual
108      * information to the opened AttributeTypeAndValueDialog.
109      *
110      * @author <a HREF="mailto:dev@directory.apache.org">Apache Directory Project</a>
111      * @version $Rev$, $Date$
112      */

113     private class AttributeTypeAndValueValueEditorRawValueWrapper
114     {
115         /**
116          * The schema, used in AttributeTypeDialog to build the list
117          * with possible attribute types.
118          */

119         private Schema schema;
120
121         /** The attribute type, used as initial attribute type. */
122         private String JavaDoc attributeType;
123
124         /** The value, used as initial value. */
125         private String JavaDoc value;
126
127
128         /**
129          * Creates a new instance of AttributeTypeAndValueValueEditorRawValueWrapper.
130          *
131          * @param schema the schema
132          * @param attributeType the attribute type
133          * @param value the value
134          */

135         private AttributeTypeAndValueValueEditorRawValueWrapper( Schema schema, String JavaDoc attributeType, String JavaDoc value )
136         {
137             super();
138             this.schema = schema;
139             this.attributeType = attributeType;
140             this.value = value;
141         }
142     }
143
144 }
145
Popular Tags