Skip to content

Commit 6667733

Browse files
committed
#3 Use Java Default Icon instead of image
1 parent 8e384d4 commit 6667733

File tree

36 files changed

+216
-237
lines changed

36 files changed

+216
-237
lines changed

BinaryInternalsViewer/src/main/java/org/freeinternals/biv/JSplitPaneFile.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
import org.freeinternals.commonlib.core.FileFormat;
2424
import org.freeinternals.biv.plugin.PluginManager;
2525
import org.freeinternals.commonlib.ui.JBinaryViewer;
26-
import org.freeinternals.commonlib.ui.JFrameTool;
26+
import org.freeinternals.commonlib.ui.UITool;
2727
import org.freeinternals.commonlib.ui.JPanelForTree;
2828
import org.freeinternals.commonlib.ui.JTreeCellRenderer;
2929
import org.freeinternals.commonlib.ui.JTreeNodeFileComponent;
@@ -123,6 +123,6 @@ private void treeSelectionChanged(final TreeSelectionEvent evt) {
123123
}
124124

125125
private void treeDoubleClickPopup(JPanel panel, String title) {
126-
JFrameTool.showPopup(this.topLevelFrame, panel, title);
126+
UITool.showPopup(this.topLevelFrame, panel, title);
127127
}
128128
}

BinaryInternalsViewer/src/main/java/org/freeinternals/biv/Main.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
import javax.swing.SwingUtilities;
3030
import javax.swing.UIManager;
3131
import org.freeinternals.biv.plugin.PluginManager;
32-
import org.freeinternals.commonlib.ui.JFrameTool;
32+
import org.freeinternals.commonlib.ui.UITool;
3333
import org.freeinternals.format.FileFormatException;
3434

3535
/**
@@ -48,7 +48,7 @@ private Main() {
4848
this.setTitle("Binary Internals Viewer " + PluginManager.getPlugedExtensions());
4949
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
5050

51-
JFrameTool.centerJFrame(this);
51+
UITool.centerJFrame(this);
5252
this.createMenu();
5353
this.filedropPanel.setBackground(Color.WHITE);
5454
this.filedropPanel.setLayout(new BorderLayout());
@@ -96,7 +96,7 @@ public void actionPerformed(final ActionEvent e) {
9696
menuFile.add(menuItem_FileOpen);
9797

9898
// File --> Close
99-
final JMenuItem menuItem_FileClose = new JMenuItem("Close");
99+
final JMenuItem menuItem_FileClose = new JMenuItem("Close", UIManager.getIcon("InternalFrame.iconifyIcon"));
100100
menuItem_FileClose.setMnemonic(KeyEvent.VK_C);
101101
menuItem_FileClose.addActionListener(new ActionListener() {
102102

@@ -173,12 +173,10 @@ public void actionPerformed(final ActionEvent e) {
173173
}
174174
});
175175
menuHelp.add(menuItem_HelpAbout);
176-
177176
}
178177

179178
private void enalbeFileDrop(){
180179
// only the 1st file are handled
181-
//new FileDrop(System.out, this.dropPanel, new FileDrop.Listener() {
182180
new FileDrop(this.filedropPanel, new FileDrop.Listener() {
183181

184182
public void filesDropped(java.io.File[] files) {

CommonLib/src/main/java/org/freeinternals/commonlib/util/Tool.java renamed to CommonLib/src/main/java/org/freeinternals/commonlib/core/BytesTool.java

Lines changed: 98 additions & 125 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* Copyright 2007, FreeInternals.org. All rights reserved.
55
* Use is subject to license terms.
66
*/
7-
package org.freeinternals.commonlib.util;
7+
package org.freeinternals.commonlib.core;
88

99
import java.io.File;
1010
import java.io.IOException;
@@ -16,114 +16,28 @@
1616
import java.util.logging.Logger;
1717
import java.util.zip.ZipEntry;
1818
import java.util.zip.ZipFile;
19-
import javax.swing.tree.DefaultMutableTreeNode;
20-
import org.freeinternals.commonlib.ui.JTreeNodeFileComponent;
2119

2220
/**
2321
* Utility Class.
2422
*
2523
* @author Amos Shi
2624
*/
27-
public final class Tool {
25+
public final class BytesTool {
2826

29-
/**
30-
* Returns byte array from the {@code file}
31-
*
32-
* @param file The file
33-
* @return Byte array of the {@code file}, or {@code null} if error
34-
* happened.
35-
*/
36-
public static byte[] readFileAsBytes(final File file) {
37-
byte[] fileBytes = null;
38-
try {
39-
fileBytes = Files.readAllBytes(Paths.get(file.getAbsolutePath()));
40-
} catch (IOException ex) {
41-
Logger.getLogger(Tool.class.getName()).log(Level.SEVERE, null, ex);
42-
}
43-
44-
return fileBytes;
45-
}
46-
47-
/**
48-
* Read byte array from {@code zipFile} of entry {@code zipEntry}
49-
*
50-
* @param zipFile The {@code jar} or {@code zip} file
51-
* @param zipEntry The entry to be read
52-
* @return Byte array of the class file, or {@code null} if error happened.
53-
* @throws java.io.IOException Error happened when reading the zip file
54-
*/
55-
public static byte[] readZipEntryAsBytes(final ZipFile zipFile, final ZipEntry zipEntry) throws IOException {
56-
if (zipFile == null) {
57-
throw new IllegalArgumentException("Parameter 'zipFile' is null.");
58-
}
59-
if (zipEntry == null) {
60-
throw new IllegalArgumentException("Parameter 'zipEntry' is null.");
61-
}
62-
63-
final long fileSize = zipEntry.getSize();
64-
final byte contents[] = new byte[(int) fileSize];
65-
ByteBuffer byteBuf = ByteBuffer.allocate(contents.length);
66-
InputStream is = null;
67-
int bytesRead = 0;
68-
int bytesAll = 0;
27+
public static boolean isByteArrayEmpty(byte[] buff, int startPos, int length) {
28+
boolean result = false;
6929

70-
try {
71-
is = zipFile.getInputStream(zipEntry);
72-
while (true) {
73-
bytesRead = is.read(contents);
74-
if (bytesRead != -1) {
75-
byteBuf.put(contents, 0, bytesRead);
76-
bytesAll += bytesRead;
77-
} else {
30+
if (buff[startPos] == 0x00 || buff[startPos] == ((byte) 0xFF)) {
31+
result = true;
32+
for (int i = 1; i <= length; i++) {
33+
if (buff[startPos + i] != buff[startPos]) {
34+
result = false;
7835
break;
7936
}
8037
}
81-
} catch (IOException ex) {
82-
Logger.getLogger(Tool.class.getName()).log(Level.SEVERE, null, ex);
83-
throw ex;
84-
}
85-
86-
if (bytesAll == fileSize) {
87-
return byteBuf.array();
88-
} else {
89-
throw new IOException(String.format(
90-
"File read error: expected = %d bytes, result = %d bytes.\nzipFile = %s\nzipEntry = %s",
91-
fileSize,
92-
byteBuf.array().length,
93-
zipFile.getName(),
94-
zipEntry.getName()));
95-
}
96-
}
97-
98-
/**
99-
* Get a string for the {@code hex} view of byte array {@code data}.
100-
*
101-
* @param data Byte array
102-
* @return A string representing the {@code hex} version of {@code data}
103-
*/
104-
public static String getByteDataHexView(final byte[] data) {
105-
if (data == null) {
106-
return "";
107-
}
108-
if (data.length < 1) {
109-
return "";
110-
}
111-
112-
final StringBuilder sb = new StringBuilder(data.length * 5);
113-
final int length = data.length;
114-
int i;
115-
int lineBreakCounter = 0;
116-
for (i = 0; i < length; i++) {
117-
sb.append(String.format(" %02X", data[i]));
118-
lineBreakCounter++;
119-
if (lineBreakCounter == 16) {
120-
sb.append('\n');
121-
lineBreakCounter = 0;
122-
}
12338
}
124-
sb.append('\n');
12539

126-
return sb.toString();
40+
return result;
12741
}
12842

12943
/**
@@ -195,44 +109,103 @@ public static boolean isByteArraySame(byte[] bin1, byte[] bin2, int start) {
195109
}
196110

197111
/**
198-
*
199-
* @param parentNode
200-
* @param lastEnd
201-
* @param diff
202-
* @param buff
203-
* @param buffStartPos
112+
* Get a string for the {@code hex} view of byte array {@code data}.
113+
*
114+
* @param data Byte array
115+
* @return A string representing the {@code hex} version of {@code data}
204116
*/
205-
public static void generateTreeNode_Diff(
206-
DefaultMutableTreeNode parentNode,
207-
int lastEnd,
208-
int diff,
209-
byte[] buff, int buffStartPos) {
210-
String diffStr;
211-
212-
if (Tool.isBuffEmpty(buff, lastEnd - buffStartPos, diff - 1)) {
213-
diffStr = String.format("Empty [0x%04X, 0x%04X] length = %d", lastEnd, lastEnd + diff - 1, diff);
214-
} else {
215-
diffStr = String.format("Unknown [0x%04X, 0x%04X] length = %d", lastEnd, lastEnd + diff - 1, diff);
117+
public static String getByteDataHexView(final byte[] data) {
118+
if (data == null) {
119+
return "";
120+
}
121+
if (data.length < 1) {
122+
return "";
123+
}
124+
125+
final StringBuilder sb = new StringBuilder(data.length * 5);
126+
final int length = data.length;
127+
int i;
128+
int lineBreakCounter = 0;
129+
for (i = 0; i < length; i++) {
130+
sb.append(String.format(" %02X", data[i]));
131+
lineBreakCounter++;
132+
if (lineBreakCounter == 16) {
133+
sb.append('\n');
134+
lineBreakCounter = 0;
135+
}
216136
}
217-
parentNode.add(new DefaultMutableTreeNode(new JTreeNodeFileComponent(
218-
lastEnd,
219-
diff,
220-
diffStr)));
137+
sb.append('\n');
138+
139+
return sb.toString();
221140
}
222141

223-
private static boolean isBuffEmpty(byte[] buff, int startPos, int length) {
224-
boolean result = false;
142+
/**
143+
* Returns byte array from the {@code file}
144+
*
145+
* @param file The file
146+
* @return Byte array of the {@code file}, or {@code null} if error
147+
* happened.
148+
*/
149+
public static byte[] readFileAsBytes(final File file) {
150+
byte[] fileBytes = null;
151+
try {
152+
fileBytes = Files.readAllBytes(Paths.get(file.getAbsolutePath()));
153+
} catch (IOException ex) {
154+
Logger.getLogger(BytesTool.class.getName()).log(Level.SEVERE, null, ex);
155+
}
225156

226-
if (buff[startPos] == 0x00 || buff[startPos] == ((byte) 0xFF)) {
227-
result = true;
228-
for (int i = 1; i <= length; i++) {
229-
if (buff[startPos + i] != buff[startPos]) {
230-
result = false;
157+
return fileBytes;
158+
}
159+
160+
/**
161+
* Read byte array from {@code zipFile} of entry {@code zipEntry}
162+
*
163+
* @param zipFile The {@code jar} or {@code zip} file
164+
* @param zipEntry The entry to be read
165+
* @return Byte array of the class file, or {@code null} if error happened.
166+
* @throws java.io.IOException Error happened when reading the zip file
167+
*/
168+
public static byte[] readZipEntryAsBytes(final ZipFile zipFile, final ZipEntry zipEntry) throws IOException {
169+
if (zipFile == null) {
170+
throw new IllegalArgumentException("Parameter 'zipFile' is null.");
171+
}
172+
if (zipEntry == null) {
173+
throw new IllegalArgumentException("Parameter 'zipEntry' is null.");
174+
}
175+
176+
final long fileSize = zipEntry.getSize();
177+
final byte contents[] = new byte[(int) fileSize];
178+
ByteBuffer byteBuf = ByteBuffer.allocate(contents.length);
179+
InputStream is = null;
180+
int bytesRead = 0;
181+
int bytesAll = 0;
182+
183+
try {
184+
is = zipFile.getInputStream(zipEntry);
185+
while (true) {
186+
bytesRead = is.read(contents);
187+
if (bytesRead != -1) {
188+
byteBuf.put(contents, 0, bytesRead);
189+
bytesAll += bytesRead;
190+
} else {
231191
break;
232192
}
233193
}
194+
} catch (IOException ex) {
195+
Logger.getLogger(BytesTool.class.getName()).log(Level.SEVERE, null, ex);
196+
throw ex;
234197
}
235198

236-
return result;
199+
if (bytesAll == fileSize) {
200+
return byteBuf.array();
201+
} else {
202+
throw new IOException(String.format(
203+
"File read error: expected = %d bytes, result = %d bytes.\nzipFile = %s\nzipEntry = %s",
204+
fileSize,
205+
byteBuf.array().length,
206+
zipFile.getName(),
207+
zipEntry.getName()));
208+
}
237209
}
210+
238211
}

CommonLib/src/main/java/org/freeinternals/commonlib/core/FileFormat.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
import java.util.SortedMap;
1414
import java.util.TreeMap;
1515
import javax.swing.tree.DefaultMutableTreeNode;
16-
import org.freeinternals.commonlib.util.Tool;
1716
import org.freeinternals.format.FileFormatException;
1817

1918
/**
@@ -44,7 +43,7 @@ public FileFormat(final File file) throws IOException, FileFormatException {
4443
throw new FileFormatException(
4544
String.format("The file content is empty.\nname = %s", file.getPath()));
4645
}
47-
this.fileByteArray = Tool.readFileAsBytes(file);
46+
this.fileByteArray = BytesTool.readFileAsBytes(file);
4847
}
4948

5049
/**

CommonLib/src/main/java/org/freeinternals/commonlib/core/PosDataInputStream.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
import java.io.EOFException;
1111
import java.io.IOException;
1212
import java.math.BigInteger;
13-
import org.freeinternals.commonlib.util.Tool;
1413

1514
/**
1615
*
@@ -478,7 +477,7 @@ public int backwardTo(byte[] b) {
478477
PosByteArrayInputStream posIn = ((PosByteArrayInputStream) this.in);
479478
byte[] buf = posIn.getBuf();
480479
for (int i = posIn.getPos() - b.length; i > -1; i--) {
481-
if (Tool.isByteArraySame(b, buf, i)) {
480+
if (BytesTool.isByteArraySame(b, buf, i)) {
482481
result = i;
483482
break;
484483
}

CommonLib/src/main/java/org/freeinternals/commonlib/ui/JPanelForTree.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ private void toolbar_CollapseAll() {
163163
}
164164

165165
private void toolbar_ShowDetails() {
166-
JFrameTool.showPopup(this.topLevelFrame, this.details_panel, this.details_title);
166+
UITool.showPopup(this.topLevelFrame, this.details_panel, this.details_title);
167167
}
168168

169169
private void tree_SelectionChanged(final TreeSelectionEvent e) {

0 commit comments

Comments
 (0)