basicbinaryeditor.java
来自「JAVA开源LDAP浏览器jxplorer的源码!」· Java 代码 · 共 546 行 · 第 1/2 页
JAVA
546 行
JXplorer.setProperty("binary.homeDir", chooser.getSelectedFile().getParent());
try
{
FileInputStream input = new FileInputStream(file);
int length = (int)file.length();
if (length > 0)
{
setButtons(true);
bytes = new byte[length];
int read = input.read(bytes);
byte[] shortBytes;
if (bytes.length < 1000)
shortBytes = bytes;
else
{
shortBytes = new byte[1000];
System.arraycopy(bytes,0,shortBytes,0,1000); //TE: copies first 1000 elements only.
}
if (read > 0)
field.setText(bytes2HexString(shortBytes)); //TE: converts the shortened array to a hex string then set it in the text area.
}
input.close();
}
catch(IOException e)
{
log.log(Level.WARNING, "Error opening the file!", e);
}
}
/**
* Save binary data to the file.
*/
protected void save()
{
JFileChooser chooser = new JFileChooser();
if (chooser.showSaveDialog(frame) != JFileChooser.APPROVE_OPTION)
return;
File file = chooser.getSelectedFile();
try
{
FileOutputStream output = new FileOutputStream(file);
if(editHex)
output.write(hexString2bytes(field.getText())); //TE: if the user is in edit mode there may be changes to the data therefore get it from the text area.
else
output.write(bytes); //TE: the user is not in edit mode therefore no changes have been made ...'bytes' can be used.
output.close();
}
catch(IOException e)
{
log.log(Level.WARNING, "Error writing the file!", e);
}
}
/**
* Method purely for extending, the intention is to launch the file in
* its default viewer using CBLauncher.launchProgram.
*/
public void view()
{
//TE: Extend this...odDocumentDOCEditor & odSpreadSheetXLSEditor.
}
/**
* Set the command for the binary viewer.
* The string must have a space at the end, as the filename will be appended.
*/
public void setViewCommand(String viewCommand)
{
this.viewCommand = viewCommand;
}
/**
* Enables or disables the ok, save & edit buttons depending on the boolean value.
* @param enabled if true buttons are enabled, if false buttons are disabled.
*/
public void setButtons(boolean enabled)
{
btnOK.setEnabled(enabled);
btnEdit.setEnabled(enabled);
btnSave.setEnabled(enabled);
}
/**
* Checks if the value in the editor has changed.
* @return true if data has changed, false if not.
*/
public boolean isChanged()
{
if(Arrays.equals(bytes, oldBytes)==false)
return true;
return false;
}
/**
* Returns true if the current value of the attribute is valid (i.e. not null).
*/
public boolean isValid()
{
String newvalue = field.getText();
return newvalue.length() != 0;
}
/**
* Returns a new value. If the whole file is displayed (i.e. if the user has
* clicked on 'edit', the new value will be retrieved from the text area. If the
* editor is only displaying the first 1000 characters the new value is retrieved
* from its source (in this case the bytes array). The idea behind this
* is that the only time a value can be edited/changed is when the user has clicked the
* edit button. Therefore this is the only time that we need to get a new value.
* @return byte[] a value that is to be set in the table editor.
*/
public byte[] getNewValue()
{
if(editHex) //TE: hex edit mode flag. If it is true the new value is taken from the text area (i.e. it may have been edited).
{
String newvalue = field.getText();
if (newvalue!=null && newvalue.length()!=0)
return hexString2bytes(newvalue);
else
return null;
}
else
return bytes; //TE: the user is not in edit mode therefore no changes have been made to the bytes array.
}
/**
* This will return a structure containing the current values of the attribute.
* @return old value.
*/
public byte[] getOldValue()
{
return oldBytes;
}
/**
* Sets the value in the table editor.
*/
public void setValue()
{
if (isChanged())
editMe.setValue(getNewValue());
quit();
}
/**
* Converts a byte to hex then to a string.
* @param bytesForConversion value to be converted.
* @return string representation of hex.
*/
public String bytes2HexString(byte[] bytesForConversion)
{
if (bytesForConversion!=null)
{
setEnabled(true);
hex = new StringBuffer(bytesForConversion.length*2);
try
{
for (int i=0; i<bytesForConversion.length; i++)
{
hex.append(CBParse.byte2Hex(bytesForConversion[i]));
}
return hex.toString();
}
catch (Exception e)
{
setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR)); //TE: just in case it fails and the cursor is still set to wait.
CBUtility.error("Problem parsing byte to hex: " + e);
}
}
setButtons(false); //TE: disables ok & edit buttons.
return NODATAMSG; //TE: set the text area to an empty string if there is no value in the directory.
}
/**
* Converts a string to hex then to a byte array.
* @param forConversion value to be converted.
* @return bytesConverted byte representation of the hex.
*/
public byte[] hexString2bytes(String forConversion)
{
char charForConversion[] = forConversion.toCharArray();
byte[] bytesConverted = new byte[charForConversion.length/2];
int a = 0; //TE: loop counter.
try
{
for (int i=0;i<charForConversion.length; i=i+2) //TE: increments by two b/c two chars need to be parsed at a time.
{
bytesConverted[a] = CBParse.hex2Byte(charForConversion[i], charForConversion[i+1]);
a++;
}
}
catch (Exception e)
{
CBUtility.error("Problem parsing hex to byte: " + e);
}
return bytesConverted;
}
/**
* Shuts the window.
*/
public void quit()
{
setVisible(false);
dispose();
}
/**
* Sets the dn of the entry being modified.
* @param dn the DN of the entry being modified.
*
*/
public void setDN(DN dn)
{
currentDN = dn;
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?