1 20 21 package org.apache.directory.ldapstudio.browser.common.dialogs; 22 23 24 import java.io.ByteArrayOutputStream ; 25 import java.io.File ; 26 import java.io.FileInputStream ; 27 import java.io.FileNotFoundException ; 28 import java.io.FileOutputStream ; 29 import java.io.IOException ; 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 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 String returnedFileName = fileDialog.open(); 93 if ( returnedFileName != null ) 94 { 95 try 96 { 97 File file = new File ( returnedFileName ); 98 FileOutputStream out = new FileOutputStream ( file ); 99 out.write( currentData ); 100 out.flush(); 101 out.close(); 102 } 103 catch ( FileNotFoundException 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 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 returnedFileName = fileDialog.open(); 120 if ( returnedFileName != null ) 121 { 122 try 123 { 124 File file = new File ( returnedFileName ); 125 FileInputStream in = new FileInputStream ( file ); 126 ByteArrayOutputStream out = new ByteArrayOutputStream ( ( 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 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 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 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.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 toFormattedHex( byte[] data ) 198 { 199 StringBuffer sb = new StringBuffer (); 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 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 |