1 package com.ca.directory.jxplorer.editor; 2 3 import com.ca.commons.cbutil.*; 4 import com.ca.directory.jxplorer.JXplorer; 5 import com.ca.directory.jxplorer.HelpIDs; 6 import com.ca.commons.naming.DN; 7 import javax.swing.*; 8 import java.awt.*; 9 import java.awt.event.*; 10 import java.io.*; 11 import java.util.Arrays ; 12 import java.util.logging.Logger ; 13 import java.util.logging.Level ; 14 15 16 17 23 24 public class basicbinaryeditor extends JDialog 25 implements abstractbinaryeditor 26 { 27 protected editablebinary editMe = null; 28 protected JTextArea field; 29 protected CBButton btnLoad, btnSave, btnView, btnOK, btnCancel, btnHelp, btnEdit; 30 protected Frame frame; protected CBPanel display; protected boolean editHex; protected StringBuffer hex; protected byte[] bytes; protected byte[] oldBytes; protected DN currentDN = null; 38 39 private static final String NODATAMSG = "No data available"; 41 43 44 private String viewCommand = "rundll32.exe cryptext.dll,CryptExtOpenCER "; 46 47 private final static Logger log = Logger.getLogger(basicbinaryeditor.class.getName()); 48 49 50 54 55 public basicbinaryeditor(Frame owner) 56 { 57 this(owner, false); 58 } 59 60 61 62 67 68 public basicbinaryeditor(Frame owner, boolean viewable) 69 { 70 super(owner); 71 setModal(true); 72 setTitle(CBIntText.get("Binary Data")); 73 74 editHex = false; 75 76 display = new CBPanel(); 77 78 addMainViewComponent(); 79 80 btnView = new CBButton(CBIntText.get("View"), CBIntText.get("")); 81 btnView.addActionListener(new ActionListener() { 82 public void actionPerformed(ActionEvent e) { 83 view(); 84 }}); 85 86 btnLoad = new CBButton(CBIntText.get("Load"), CBIntText.get("Click here to load an external file.")); 87 btnLoad.addActionListener(new ActionListener() { 88 public void actionPerformed(ActionEvent e) { 89 load(); 90 }}); 91 92 btnSave = new CBButton(CBIntText.get("Save"), CBIntText.get("Click here to save the date to an external file.")); 93 btnSave.addActionListener(new ActionListener() { 94 public void actionPerformed(ActionEvent e) { 95 save(); 96 }}); 97 98 btnOK = new CBButton(CBIntText.get("OK"), CBIntText.get("Click here to make the changes (remember to click Submit in the table editor).")); 99 btnOK.addActionListener(new ActionListener() { 100 public void actionPerformed(ActionEvent e) { 101 setValue(); 102 }}); 103 104 btnEdit = new CBButton(CBIntText.get("Edit"), CBIntText.get("Edit the file data in Hex.")); 105 btnEdit.addActionListener(new ActionListener() { 106 public void actionPerformed(ActionEvent e) { 107 setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR)); btnEdit.setEnabled(false); field.setText(bytes2HexString(bytes)); field.setEnabled(true); editHex = true; setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR)); }}); 114 115 btnCancel = new CBButton(CBIntText.get("Cancel"), CBIntText.get("Click here to exit.")); 116 btnCancel.addActionListener(new ActionListener() { 117 public void actionPerformed(ActionEvent e) { 118 quit(); 119 }}); 120 121 JPanel buttonPanel = new JPanel(); 122 buttonPanel.add(btnLoad); 123 buttonPanel.add(btnSave); 124 125 if (viewable) 126 buttonPanel.add(btnView); 127 128 buttonPanel.add(btnEdit); 129 buttonPanel.add(btnOK); 130 buttonPanel.add(btnCancel); 131 buttonPanel.add(addHelp()); display.add(buttonPanel); 133 134 display.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke("ENTER"), "enter"); 136 display.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke("ESCAPE"), "escape"); 137 display.getActionMap().put("enter", new MyAction(CBAction.ENTER)); 138 display.getActionMap().put("escape", new MyAction(CBAction.ESCAPE)); 139 140 getContentPane().add(display); 141 setSize(435,300); 142 } 143 144 145 156 private class MyAction extends CBAction 157 { 158 162 public MyAction(int key) 163 { 164 super(key); 165 } 166 167 172 public void actionPerformed(ActionEvent e) 173 { 174 if (getKey() == ESCAPE) 175 quit(); 176 else if (getKey() == ENTER) 177 setValue(); 178 } 179 } 180 181 182 183 186 187 public void addMainViewComponent() 188 { 189 field = new JTextArea(); 190 field.setLineWrap(true); field.setEnabled(false); field.setDisabledTextColor(Color.black); 194 final JScrollPane scrollPane = new JScrollPane(field); 195 scrollPane.setPreferredSize(new Dimension(310,60));; 196 scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER); 198 display.makeHeavy(); 199 display.addln(scrollPane); 200 display.makeLight(); 201 } 202 203 204 205 206 210 211 public CBButton addHelp() 212 { 213 btnHelp = new CBButton(CBIntText.get("Help"), CBIntText.get("Click here for Help.")); CBHelpSystem.useDefaultHelp(btnHelp, HelpIDs.ATTR_BINARY); 215 return btnHelp; 216 } 217 218 219 220 221 228 229 public void setValue(editablebinary editMe) 230 { 231 editHex = false; 233 this.editMe = editMe; 234 235 bytes = editMe.getValue(); 236 237 oldBytes = bytes; 239 if(bytes == null || bytes.length == 0) 240 field.setText(NODATAMSG); else 242 { 243 setButtons(true); 244 245 byte[] shortBytes; 246 if (bytes.length < 1000) 247 shortBytes = bytes; 248 else 249 { 250 shortBytes = new byte[1000]; 251 System.arraycopy(bytes,0,shortBytes,0,1000); 252 } 253 field.setText(bytes2HexString(shortBytes)); } 255 } 256 257 258 259 263 264 protected void load() 265 { 266 editHex = false; 268 CBCache.cleanCache(currentDN.toString()); 270 JFileChooser chooser = new JFileChooser(JXplorer.getProperty("binary.homeDir")); 271 if (chooser.showOpenDialog(frame) != JFileChooser.APPROVE_OPTION) 272 return; 273 File file = chooser.getSelectedFile(); 274 JXplorer.setProperty("binary.homeDir", chooser.getSelectedFile().getParent()); 275 276 try 277 { 278 FileInputStream input = new FileInputStream(file); 279 280 int length = (int)file.length(); 281 if (length > 0) 282 { 283 setButtons(true); 284 285 bytes = new byte[length]; 286 int read = input.read(bytes); 287 288 byte[] shortBytes; 289 if (bytes.length < 1000) 290 shortBytes = bytes; 291 else 292 { 293 shortBytes = new byte[1000]; 294 System.arraycopy(bytes,0,shortBytes,0,1000); } 296 297 if (read > 0) 298 field.setText(bytes2HexString(shortBytes)); } 300 input.close(); 301 } 302 catch(IOException e) 303 { 304 log.log(Level.WARNING, "Error opening the file!", e); 305 } 306 307 } 308 309 310 311 314 315 protected void save() 316 { 317 JFileChooser chooser = new JFileChooser(); 318 if (chooser.showSaveDialog(frame) != JFileChooser.APPROVE_OPTION) 319 return; 320 321 File file = chooser.getSelectedFile(); 322 323 try 324 { 325 FileOutputStream output = new FileOutputStream(file); 326 if(editHex) 327 output.write(hexString2bytes(field.getText())); else 329 output.write(bytes); 331 output.close(); 332 } 333 catch(IOException e) 334 { 335 log.log(Level.WARNING, "Error writing the file!", e); 336 } 337 } 338 339 340 341 345 346 public void view() 347 { 348 } 350 351 352 353 357 358 public void setViewCommand(String viewCommand) 359 { 360 this.viewCommand = viewCommand; 361 } 362 363 364 365 369 370 public void setButtons(boolean enabled) 371 { 372 btnOK.setEnabled(enabled); 373 btnEdit.setEnabled(enabled); 374 btnSave.setEnabled(enabled); 375 } 376 377 378 379 383 384 public boolean isChanged() 385 { 386 if(Arrays.equals(bytes, oldBytes)==false) 387 return true; 388 return false; 389 } 390 391 392 393 396 397 public boolean isValid() 398 { 399 String newvalue = field.getText(); 400 return newvalue.length() != 0; 401 } 402 403 404 405 414 415 public byte[] getNewValue() 416 { 417 if(editHex) { 419 String newvalue = field.getText(); 420 421 if (newvalue!=null && newvalue.length()!=0) 422 return hexString2bytes(newvalue); 423 else 424 return null; 425 } 426 else 427 return bytes; } 429 430 431 432 436 437 public byte[] getOldValue() 438 { 439 return oldBytes; 440 } 441 442 443 444 447 448 public void setValue() 449 { 450 if (isChanged()) 451 editMe.setValue(getNewValue()); 452 quit(); 453 } 454 455 456 457 462 463 public String bytes2HexString(byte[] bytesForConversion) 464 { 465 if (bytesForConversion!=null) 466 { 467 setEnabled(true); 468 469 hex = new StringBuffer (bytesForConversion.length*2); 470 471 try 472 { 473 for (int i=0; i<bytesForConversion.length; i++) 474 { 475 hex.append(CBParse.byte2Hex(bytesForConversion[i])); 476 } 477 return hex.toString(); 478 } 479 catch (Exception e) 480 { 481 setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR)); CBUtility.error("Problem parsing byte to hex: " + e); 483 } 484 } 485 486 setButtons(false); 488 return NODATAMSG; } 490 491 492 493 498 499 public byte[] hexString2bytes(String forConversion) 500 { 501 char charForConversion[] = forConversion.toCharArray(); 502 503 byte[] bytesConverted = new byte[charForConversion.length/2]; 504 505 int a = 0; 507 try 508 { 509 for (int i=0;i<charForConversion.length; i=i+2) { 511 bytesConverted[a] = CBParse.hex2Byte(charForConversion[i], charForConversion[i+1]); 512 a++; 513 } 514 } 515 catch (Exception e) 516 { 517 CBUtility.error("Problem parsing hex to byte: " + e); 518 } 519 return bytesConverted; 520 } 521 522 523 524 527 528 public void quit() 529 { 530 setVisible(false); 531 dispose(); 532 } 533 534 535 536 541 542 public void setDN(DN dn) 543 { 544 currentDN = dn; 545 } 546 } | Popular Tags |