KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > directory > ldapstudio > browser > ui > dialogs > EncoderDecoderDialog


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.dialogs;
22
23
24 import org.apache.directory.ldapstudio.browser.common.widgets.BaseWidgetUtils;
25 import org.apache.directory.ldapstudio.browser.core.utils.LdifUtils;
26 import org.eclipse.jface.dialogs.Dialog;
27 import org.eclipse.jface.dialogs.IDialogConstants;
28 import org.eclipse.swt.SWT;
29 import org.eclipse.swt.events.ModifyEvent;
30 import org.eclipse.swt.events.ModifyListener;
31 import org.eclipse.swt.layout.GridData;
32 import org.eclipse.swt.widgets.Composite;
33 import org.eclipse.swt.widgets.Control;
34 import org.eclipse.swt.widgets.Label;
35 import org.eclipse.swt.widgets.Shell;
36 import org.eclipse.swt.widgets.Text;
37
38
39 public class EncoderDecoderDialog extends Dialog
40 {
41
42     public static final String JavaDoc DIALOG_TITLE = "LDAP Encoder/Decoder";
43
44     private Text iso88591Text;
45
46     private Text iso88591HexText;
47
48     private Text utf8Text;
49
50     private Text utf8HexText;
51
52     private Text base64Text;
53
54     private Text errorText;
55
56     private boolean inModify = false;
57
58
59     public EncoderDecoderDialog( Shell parentShell )
60     {
61         super( parentShell );
62         super.setShellStyle( super.getShellStyle() | SWT.RESIZE );
63     }
64
65
66     protected void configureShell( Shell shell )
67     {
68         super.configureShell( shell );
69         shell.setText( DIALOG_TITLE );
70         //shell.setImage( BrowserUIPlugin.getDefault().getImage( BrowserUIConstants.IMG_IMAGEEDITOR ) );
71
}
72
73
74     protected Control createDialogArea( Composite parent )
75     {
76
77         Composite composite2 = ( Composite ) super.createDialogArea( parent );
78         GridData gd1 = new GridData( GridData.FILL_BOTH );
79         gd1.widthHint = convertHorizontalDLUsToPixels( IDialogConstants.MINIMUM_MESSAGE_AREA_WIDTH );
80         gd1.heightHint = convertVerticalDLUsToPixels( IDialogConstants.MINIMUM_MESSAGE_AREA_WIDTH );
81         composite2.setLayoutData( gd1 );
82
83         Composite composite = BaseWidgetUtils.createColumnContainer( composite2, 2, 1 );
84         composite.setLayoutData( new GridData( GridData.FILL_BOTH ) );
85
86         Label iso8859Label = new Label( composite, SWT.NONE );
87         iso8859Label.setText( "ISO-8859-1:" );
88         iso88591Text = new Text( composite, SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL );
89         GridData gd = new GridData( GridData.FILL_BOTH );
90         iso88591Text.setLayoutData( gd );
91
92         Label iso8859HexLabel = new Label( composite, SWT.NONE );
93         iso8859HexLabel.setText( "ISO-8859-1 Hex:" );
94         iso88591HexText = new Text( composite, SWT.BORDER | SWT.READ_ONLY );
95         iso88591HexText.setLayoutData( new GridData( GridData.FILL_HORIZONTAL ) );
96
97         Label utf8Label = new Label( composite, SWT.NONE );
98         utf8Label.setText( "UTF-8:" );
99         utf8Text = new Text( composite, SWT.BORDER );
100         utf8Text.setLayoutData( new GridData( GridData.FILL_HORIZONTAL ) );
101
102         Label utf8HexLabel = new Label( composite, SWT.NONE );
103         utf8HexLabel.setText( "UTF-8 Hex:" );
104         utf8HexText = new Text( composite, SWT.BORDER | SWT.READ_ONLY );
105         utf8HexText.setLayoutData( new GridData( GridData.FILL_HORIZONTAL ) );
106
107         Label base64Label = new Label( composite, SWT.NONE );
108         base64Label.setText( "BASE-64:" );
109         base64Text = new Text( composite, SWT.BORDER );
110         base64Text.setLayoutData( new GridData( GridData.FILL_HORIZONTAL ) );
111
112         errorText = new Text( composite, SWT.BORDER | SWT.READ_ONLY );
113         gd = new GridData( GridData.FILL_HORIZONTAL );
114         gd.horizontalSpan = 2;
115         errorText.setLayoutData( gd );
116
117         iso88591Text.addModifyListener( new ModifyListener()
118         {
119             public void modifyText( ModifyEvent e )
120             {
121                 if ( !inModify )
122                 {
123                     inModify = true;
124                     try
125                     {
126                         String JavaDoc iso = iso88591Text.getText();
127                         byte[] isoHex = iso.getBytes( "ISO-8859-1" );
128                         byte[] utf8 = LdifUtils.utf8encode( iso );
129                         String JavaDoc utf8String = new String JavaDoc( utf8, "ISO-8859-1" );
130                         String JavaDoc base64 = LdifUtils.base64encode( utf8 );
131
132                         iso88591HexText.setText( LdifUtils.hexEncode( isoHex ) );
133                         utf8Text.setText( utf8String );
134                         utf8HexText.setText( LdifUtils.hexEncode( utf8 ) );
135                         base64Text.setText( base64 );
136                         errorText.setText( "" );
137                     }
138                     catch ( Exception JavaDoc ex )
139                     {
140                         errorText.setText( ex.getMessage() );
141                         ex.printStackTrace();
142                     }
143                     finally
144                     {
145                         inModify = false;
146                     }
147                 }
148             }
149         } );
150
151         utf8Text.addModifyListener( new ModifyListener()
152         {
153             public void modifyText( ModifyEvent e )
154             {
155                 if ( !inModify )
156                 {
157                     inModify = true;
158                     try
159                     {
160                         String JavaDoc utf8String = utf8Text.getText();
161                         byte[] utf8 = utf8String.getBytes( "ISO-8859-1" );
162                         String JavaDoc iso = LdifUtils.utf8decode( utf8 );
163                         byte[] isoHex = iso.getBytes( "ISO-8859-1" );
164                         String JavaDoc base64 = LdifUtils.base64encode( utf8 );
165
166                         iso88591Text.setText( iso );
167                         iso88591HexText.setText( LdifUtils.hexEncode( isoHex ) );
168                         utf8HexText.setText( LdifUtils.hexEncode( utf8 ) );
169                         base64Text.setText( base64 );
170                         errorText.setText( "" );
171                     }
172                     catch ( Exception JavaDoc ex )
173                     {
174                         errorText.setText( ex.getMessage() );
175                         ex.printStackTrace();
176                     }
177                     finally
178                     {
179                         inModify = false;
180                     }
181                 }
182             }
183         } );
184
185         base64Text.addModifyListener( new ModifyListener()
186         {
187             public void modifyText( ModifyEvent e )
188             {
189                 if ( !inModify )
190                 {
191                     inModify = true;
192                     try
193                     {
194                         String JavaDoc base64 = base64Text.getText();
195                         byte[] utf8 = LdifUtils.base64decodeToByteArray( base64 );
196                         String JavaDoc utf8String = new String JavaDoc( utf8, "ISO-8859-1" );
197                         String JavaDoc iso = LdifUtils.utf8decode( utf8 );
198                         byte[] isoHex = iso.getBytes( "ISO-8859-1" );
199
200                         iso88591Text.setText( iso );
201                         iso88591HexText.setText( LdifUtils.hexEncode( isoHex ) );
202                         utf8Text.setText( utf8String );
203                         utf8HexText.setText( LdifUtils.hexEncode( utf8 ) );
204                         errorText.setText( "" );
205                     }
206                     catch ( Exception JavaDoc ex )
207                     {
208                         errorText.setText( ex.getMessage() );
209                         ex.printStackTrace();
210                     }
211                     finally
212                     {
213                         inModify = false;
214                     }
215                 }
216             }
217         } );
218
219         return composite;
220     }
221
222 }
223
Popular Tags