KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > directory > ldapstudio > browser > common > dialogs > HexDialog


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.common.dialogs;
22
23
24 import java.io.ByteArrayOutputStream JavaDoc;
25 import java.io.File JavaDoc;
26 import java.io.FileInputStream JavaDoc;
27 import java.io.FileNotFoundException JavaDoc;
28 import java.io.FileOutputStream JavaDoc;
29 import java.io.IOException JavaDoc;
30
31 import org.apache.directory.ldapstudio.browser.common.BrowserCommonActivator;
32 import org.apache.directory.ldapstudio.browser.common.BrowserCommonConstants;
33 import org.eclipse.core.runtime.IStatus;
34 import org.eclipse.core.runtime.Status;
35 import org.eclipse.jface.dialogs.Dialog;
36 import org.eclipse.jface.dialogs.IDialogConstants;
37 import org.eclipse.jface.resource.JFaceResources;
38 import org.eclipse.swt.SWT;
39 import org.eclipse.swt.layout.GridData;
40 import org.eclipse.swt.widgets.Composite;
41 import org.eclipse.swt.widgets.Control;
42 import org.eclipse.swt.widgets.FileDialog;
43 import org.eclipse.swt.widgets.Shell;
44 import org.eclipse.swt.widgets.Text;
45
46
47 public class HexDialog extends Dialog
48 {
49
50     public static final String JavaDoc DIALOG_TITLE = "Hex Editor";
51
52     public static final double MAX_WIDTH = 550.0;
53
54     public static final double MAX_HEIGHT = 550.0;
55
56     public static final int LOAD_BUTTON_ID = 9998;
57
58     public static final int SAVE_BUTTON_ID = 9999;
59
60     private byte[] currentData;
61
62     private byte[] returnData;
63
64     private Text hexText;
65
66
67     public HexDialog( Shell parentShell, byte[] initialData )
68     {
69         super( parentShell );
70         super.setShellStyle( super.getShellStyle() | SWT.RESIZE );
71         this.currentData = initialData;
72     }
73
74
75     public boolean close()
76     {
77         return super.close();
78     }
79
80
81     protected void buttonPressed( int buttonId )
82     {
83         if ( buttonId == IDialogConstants.OK_ID )
84         {
85             this.returnData = this.currentData;
86         }
87         else if ( buttonId == SAVE_BUTTON_ID )
88         {
89             FileDialog fileDialog = new FileDialog( getShell(), SWT.SAVE );
90             fileDialog.setText( "Save Data" );
91             // fileDialog.setFilterExtensions(new String[]{"*.jpg"});
92
String JavaDoc returnedFileName = fileDialog.open();
93             if ( returnedFileName != null )
94             {
95                 try
96                 {
97                     File JavaDoc file = new File JavaDoc( returnedFileName );
98                     FileOutputStream JavaDoc out = new FileOutputStream JavaDoc( file );
99                     out.write( currentData );
100                     out.flush();
101                     out.close();
102                 }
103                 catch ( FileNotFoundException JavaDoc e )
104                 {
105                     BrowserCommonActivator.getDefault().getExceptionHandler().handleException(
106                         new Status( IStatus.ERROR, BrowserCommonActivator.PLUGIN_ID, IStatus.ERROR, "Can't write to file", e ) );
107                 }
108                 catch ( IOException JavaDoc e )
109                 {
110                     BrowserCommonActivator.getDefault().getExceptionHandler().handleException(
111                         new Status( IStatus.ERROR, BrowserCommonActivator.PLUGIN_ID, IStatus.ERROR, "Can't write to file", e ) );
112                 }
113             }
114         }
115         else if ( buttonId == LOAD_BUTTON_ID )
116         {
117             FileDialog fileDialog = new FileDialog( getShell(), SWT.OPEN );
118             fileDialog.setText( "Load Data" );
119             String JavaDoc returnedFileName = fileDialog.open();
120             if ( returnedFileName != null )
121             {
122                 try
123                 {
124                     File JavaDoc file = new File JavaDoc( returnedFileName );
125                     FileInputStream JavaDoc in = new FileInputStream JavaDoc( file );
126                     ByteArrayOutputStream JavaDoc out = new ByteArrayOutputStream JavaDoc( ( int ) file.length() );
127                     byte[] buf = new byte[4096];
128                     int len;
129                     while ( ( len = in.read( buf ) ) > 0 )
130                     {
131                         out.write( buf, 0, len );
132                     }
133                     this.currentData = out.toByteArray();
134                     hexText.setText( toFormattedHex( this.currentData ) );
135                     out.close();
136                     in.close();
137                 }
138                 catch ( FileNotFoundException JavaDoc e )
139                 {
140                     BrowserCommonActivator.getDefault().getExceptionHandler().handleException(
141                         new Status( IStatus.ERROR, BrowserCommonActivator.PLUGIN_ID, IStatus.ERROR, "Can't read file", e ) );
142                 }
143                 catch ( IOException JavaDoc e )
144                 {
145                     BrowserCommonActivator.getDefault().getExceptionHandler().handleException(
146                         new Status( IStatus.ERROR, BrowserCommonActivator.PLUGIN_ID, IStatus.ERROR, "Can't read file", e ) );
147                 }
148             }
149         }
150         else
151         {
152             this.returnData = null;
153         }
154
155         super.buttonPressed( buttonId );
156     }
157
158
159     protected void configureShell( Shell shell )
160     {
161         super.configureShell( shell );
162         shell.setText( DIALOG_TITLE );
163         shell.setImage( BrowserCommonActivator.getDefault().getImage( BrowserCommonConstants.IMG_HEXEDITOR ) );
164     }
165
166
167     protected void createButtonsForButtonBar( Composite parent )
168     {
169         createButton( parent, LOAD_BUTTON_ID, "Load Data...", false );
170         createButton( parent, SAVE_BUTTON_ID, "Save Data...", false );
171         createButton( parent, IDialogConstants.OK_ID, IDialogConstants.OK_LABEL, false );
172         createButton( parent, IDialogConstants.CANCEL_ID, IDialogConstants.CANCEL_LABEL, false );
173     }
174
175
176     protected Control createDialogArea( Composite parent )
177     {
178         // create composite
179
Composite composite = ( Composite ) super.createDialogArea( parent );
180
181         hexText = new Text( composite, SWT.MULTI | SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL | SWT.READ_ONLY );
182         hexText.setFont( JFaceResources.getFont( JFaceResources.TEXT_FONT ) );
183
184         hexText.setText( toFormattedHex( this.currentData ) );
185         // GridData gd = new GridData(GridData.GRAB_HORIZONTAL |
186
// GridData.HORIZONTAL_ALIGN_FILL);
187
GridData gd = new GridData( GridData.FILL_BOTH );
188         gd.widthHint = convertHorizontalDLUsToPixels( ( int ) ( IDialogConstants.MINIMUM_MESSAGE_AREA_WIDTH * 1.6 ) );
189         gd.heightHint = convertHorizontalDLUsToPixels( IDialogConstants.MINIMUM_MESSAGE_AREA_WIDTH / 2 );
190         hexText.setLayoutData( gd );
191
192         applyDialogFont( composite );
193         return composite;
194     }
195
196
197     private String JavaDoc toFormattedHex( byte[] data )
198     {
199         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
200         for ( int i = 0; i < data.length; i++ )
201         {
202             int b = ( int ) data[i];
203             if ( b < 0 )
204                 b = 256 + b;
205             String JavaDoc s = Integer.toHexString( b );
206             if ( s.length() == 1 )
207                 s = "0" + s;
208             sb.append( s ).append( " " );
209             if ( ( i + 1 ) % 8 == 0 )
210                 sb.append( " " );
211
212             if ( i == data.length - 1 )
213             {
214                 while ( ( i + 1 ) % 16 != 0 )
215                 {
216                     sb.append( " " );
217                     if ( ( i + 1 ) % 8 == 0 )
218                         sb.append( " " );
219                     i++;
220                 }
221                 sb.append( " " );
222             }
223
224             if ( ( i + 1 ) % 16 == 0 )
225             {
226                 sb.append( " " );
227                 for ( int x = i - 16 + 1; x <= i && x < data.length; x++ )
228                 {
229                     if ( data[x] > 32 && data[x] < 127 )
230                         sb.append( ( char ) data[x] );
231                     else
232                         sb.append( '.' );
233                     if ( ( x + 1 ) % 8 == 0 )
234                         sb.append( " " );
235                 }
236             }
237
238             if ( ( i + 1 ) % 16 == 0 )
239             {
240                 sb.append( "\r\n" );
241             }
242         }
243         return sb.toString();
244     }
245
246
247     public byte[] getData()
248     {
249         return this.returnData;
250     }
251 }
252
Popular Tags