RedWing
Erfahrenes Mitglied
Hi,
update:
@Tom:
Würde sich bestimmt gut als JavaTutorial machen
Gruß
RedWing
update:
Code:
/**reated on 23.12.2004@20:18:59
*
* TODO Licence info
*
*/
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.KeyEvent;
import java.awt.event.KeyAdapter;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipException;
import java.util.zip.ZipFile;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTextPane;
import javax.swing.JTree;
import javax.swing.filechooser.FileFilter;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreeModel;
import javax.swing.tree.TreePath;
import javax.swing.text.StyleConstants;
import javax.swing.text.Style;
import com.Ostermiller.Syntax.Lexer.JavaLexer;
import com.Ostermiller.Syntax.Lexer.CLexer;
import com.Ostermiller.Syntax.Lexer.Lexer;
import com.Ostermiller.Syntax.Lexer.JavaToken;
import com.Ostermiller.Syntax.Lexer.CToken;
import com.Ostermiller.Syntax.Lexer.Token;
/**
* @author Administrator
*
* TODO Explain me
*
*/
public class ArchiveViewer extends JFrame {
/**
* Comment for <code>serialVersionUID</code>
*/
private static final long serialVersionUID = 3979269140316238387L;
private JTree tree;
private final static String APP_TITLE = "ArchiveViewer";
private JButton btn;
private JTextPane textPane;
private JProgressBar progressBar;
private JFileChooser fileChooser;
private DefaultTreeModel model;
private ZipFile theZipFile;
private SyntaxHighLighter highlighter;
public ArchiveViewer() {
super(APP_TITLE);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setResizable(false);
JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
JPanel leftPanel = new JPanel(new BorderLayout());
tree = new JTree((TreeModel) null);
tree.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
TreePath path = tree.getSelectionPath();
if (path == null)
return;
tree.scrollPathToVisible(path);
Object[] elements = path.getPath();
String fileName = elements[elements.length - 1].toString();
StringBuilder builder = new StringBuilder();
for (int i = 1; i < elements.length - 1; i++) {
builder.append(elements[i]);
builder.append('/');
}
builder.append(fileName);
ZipEntry entry = findEntryByName(builder.toString());
if (entry == null)
return;
try {
if(fileName.endsWith(".java"))
highlighter = new JavaSyntaxHighLighter(theZipFile.getInputStream(entry), textPane);
else if(fileName.endsWith(".cpp") || fileName.endsWith(".c")
|| fileName.endsWith(".cc")
|| fileName.endsWith(".c++")
|| fileName.endsWith(".h")
|| fileName.endsWith(".hpp"))
highlighter = new CSyntaxHighLighter(theZipFile.getInputStream(entry), textPane);
textPane.read(theZipFile.getInputStream(entry), fileName);
highlighter.highlight();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
});
btn = new JButton("select archive");
progressBar = new JProgressBar();
btn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (fileChooser == null) {
fileChooser = new JFileChooser();
fileChooser.setFileFilter(new FileFilter() {
public boolean accept(File f) {
if (f.isDirectory()) {
return true;
}
String name = f.getName();
return name.matches(".*.(zip|jar)");
}
public String getDescription() {
return "Supported Archiv extensions (zip,jar)";
}
});
}
if (fileChooser.showOpenDialog(ArchiveViewer.this) == JFileChooser.APPROVE_OPTION) {
final File file = fileChooser.getSelectedFile();
if (file.isFile()) {
System.out.println(file);
new Thread() {
{
setPriority(MIN_PRIORITY);
}
public void run() {
btn.setEnabled(false);
setTitle(APP_TITLE + ": " + file.getName());
processArchive(file);
btn.setEnabled(true);
}
}.start();
}
}
}
});
Container c = getContentPane();
leftPanel.add(new JScrollPane(tree) {
{
setPreferredSize(new Dimension(180, 400));
}
}, BorderLayout.NORTH);
leftPanel.add(btn, BorderLayout.CENTER);
leftPanel.add(progressBar, BorderLayout.SOUTH);
textPane = new JTextPane() {
public void setSize(Dimension d) {
if (d.width < getParent().getSize().width)
d.width = getParent().getSize().width;
super.setSize(d);
}
public boolean getScrollableTracksViewportWidth() {
return false;
}
};
splitPane.add(leftPanel);
splitPane.add(new JPanel(new BorderLayout()) {
{
setPreferredSize(new Dimension(500, 400));
add(new JScrollPane(textPane), BorderLayout.CENTER);
}
});
c.add(splitPane);
pack();
setVisible(true);
textPane.setEditable(false);
}
/**
* @param file
*/
protected void processArchive(File file) {
try {
model = new ArchiveTreeModel(new ZipFile(file));
} catch (ZipException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
tree.setModel(model);
}
public static void main(String[] args) {
new ArchiveViewer();
}
private ZipEntry findEntryByName(String name) {
ZipEntry entry = null;
for (Enumeration e = theZipFile.entries(); e
.hasMoreElements();) {
entry = (ZipEntry)e.nextElement();
if (entry.getName().equals(name)) {
return entry;
}
}
return null;
}
class ArchiveTreeModel extends DefaultTreeModel {
/**
* Comment for <code>serialVersionUID</code>
*/
private static final long serialVersionUID = 3258689897023093049L;
public ArchiveTreeModel(ZipFile zipFile) {
super(new DefaultMutableTreeNode("/"));
/* */
theZipFile = zipFile;
progressBar.setMinimum(0);
progressBar.setMaximum(zipFile.size());
int value = 0;
/* */
Enumeration entries = zipFile.entries();
while (entries.hasMoreElements()) {
ZipEntry entry = (ZipEntry) entries.nextElement();
String name = entry.getName();
String[] elements = name.split("/");
DefaultMutableTreeNode currentNode = (DefaultMutableTreeNode) getRoot();
/* */
progressBar.setValue(++value);
/* */
for (int i = 0; i < elements.length; i++) {
String token = elements[i];
DefaultMutableTreeNode nextNode = findNode(currentNode,
token);
if (nextNode == null) {
nextNode = new DefaultMutableTreeNode(token);
currentNode.add(nextNode);
}
currentNode = nextNode;
}
}
}
private DefaultMutableTreeNode findNode(DefaultMutableTreeNode parent,
String name) {
Enumeration e = parent.children();
while (e.hasMoreElements()) {
DefaultMutableTreeNode element = (DefaultMutableTreeNode) e
.nextElement();
if (element.getUserObject().equals(name)) {
return element;
}
}
return null;
}
}
}
class JavaSyntaxHighLighter extends SyntaxHighLighter{
public JavaSyntaxHighLighter(InputStream in, JTextPane pane){
this.pane = pane;
reader = new InputStreamReader(in);
lexer = new JavaLexer(reader);
}
public void highlight(){
JavaToken token = null;
initializeStyles();
try{
while((token = (JavaToken)lexer.getNextToken()) != null){
if(token.isReservedWord()){
pane.getStyledDocument().setCharacterAttributes(token.getCharBegin(),
token.getCharEnd() - token.getCharBegin(),
pane.getStyle("Khaki"), true);
}
if(token.isLiteral()){
pane.getStyledDocument().setCharacterAttributes(token.getCharBegin(),
token.getCharEnd() - token.getCharBegin(),
pane.getStyle("Coral"), true);
}
if(token.isComment()){
pane.getStyledDocument().setCharacterAttributes(token.getCharBegin(),
token.getCharEnd() - token.getCharBegin(),
pane.getStyle("NavyBlue"), true);
}
}
}
catch(IOException e){ System.out.println("IO Exception :(");}
}
}
class CSyntaxHighLighter extends SyntaxHighLighter{
public CSyntaxHighLighter(InputStream in, JTextPane pane){
this.pane = pane;
reader = new InputStreamReader(in);
lexer = new CLexer(reader);
}
public void highlight(){
CToken token = null;
initializeStyles();
try{
while((token = (CToken)lexer.getNextToken()) != null){
if(token.isReservedWord()){
pane.getStyledDocument().setCharacterAttributes(token.getCharBegin(),
token.getCharEnd() - token.getCharBegin(),
pane.getStyle("Khaki"), true);
}
if(token.isLiteral()){
pane.getStyledDocument().setCharacterAttributes(token.getCharBegin(),
token.getCharEnd() - token.getCharBegin(),
pane.getStyle("Coral"), true);
}
if(token.isComment()){
pane.getStyledDocument().setCharacterAttributes(token.getCharBegin(),
token.getCharEnd() - token.getCharBegin(),
pane.getStyle("NavyBlue"), true);
}
if(token.isPreProcessor()){
pane.getStyledDocument().setCharacterAttributes(token.getCharBegin(),
token.getCharEnd() - token.getCharBegin(),
pane.getStyle("Violet"),true);
}
}
}
catch(IOException e){ System.out.println("IO Exception :(");}
}
}
abstract class SyntaxHighLighter{
protected JTextPane pane = null;
protected Lexer lexer = null;
protected InputStreamReader reader = null;
protected void initializeStyles(){
Style style = pane.addStyle("Khaki", null);
StyleConstants.setForeground(style, new Color(220,210,140));
style = pane.addStyle("NavyBlue", null);
StyleConstants.setForeground(style, new Color(0,0,196));
style = pane.addStyle("Coral", null);
StyleConstants.setForeground(style, new Color(205,91,69));
style = pane.addStyle("Violet", null);
StyleConstants.setForeground(style, new Color(208,32,144));
}
abstract public void highlight();
}
@Tom:
Würde sich bestimmt gut als JavaTutorial machen
Gruß
RedWing