/*
* Created on 2004/08/07
* Author aki@www.xucker.jpn.org
* License Apache2.0 or Common Public License
*/
package org.jpn.xucker.wordcounter;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.RowData;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.program.Program;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
import org.jpn.xucker.commons.io.WordCountInputStream;
import org.jpn.xucker.commons.swt.ui.CopyTable;
import org.jpn.xucker.commons.swt.ui.FileListTable;
public class WordCountMain implements SelectionListener{
/**
* @param shell
*/
private FileListTable filelist1;
private Button button1;
private Table table1;
private double version=0.1;
private Image icon;
Shell shell;
CopyTable copyTable;
private String HOMEPAGE_URL="http://www.xucker.jpn.org/";
private String APP_NAME="WordCount";
public WordCountMain(Shell shell) {
WordCountMenu menu=new WordCountMenu(shell,this);
icon= new Image(Display.getCurrent(), FileListTable.class
.getClassLoader().getResourceAsStream(
"org/jpn/xucker/wordcounter/resource/icon.png"));
shell.setImage(icon);
this.shell=shell;
shell.setText(APP_NAME+" v"+version);
shell.setBounds(0,0,500,405);
RowLayout layout=new RowLayout();
shell.setLayout(layout);
//Label label=new Label(shell,SWT.NULL);
//label.setImage(fileImage);
filelist1=new FileListTable(shell,SWT.NULL);
RowData filelist1_data=new RowData();
filelist1_data.width=470;
filelist1_data.height=135;
filelist1.setLayoutData(filelist1_data);
button1=new Button(shell,SWT.NULL);
button1.setText("数える");
button1.addSelectionListener(this);
RowData button1_data=new RowData();
button1.setLayoutData(button1_data);
button1_data.width=485;
button1_data.height=30;
//Label label2=new Label(shell,SWT.NULL);
//label2.setImage(resultImage);
copyTable=new CopyTable(shell,SWT.HIDE_SELECTION);
table1=copyTable.getTable();
table1.setHeaderVisible(true);
table1.setLinesVisible(true);
table1.addSelectionListener(this);
RowData table1_data=new RowData();
table1_data.width=470;
table1_data.height=140;
table1.setLayoutData(table1_data);
TableColumn table1_column =new TableColumn(table1,SWT.HIDE_SELECTION);
table1_column.setText("ファイル名");
table1_column.setWidth(115);
TableColumn table1_column2 =new TableColumn(table1,SWT.NULL);
table1_column2.setText("単語数");
table1_column2.setWidth(50);
TableColumn table1_column3 =new TableColumn(table1,SWT.NULL);
table1_column3.setText("行数");
table1_column3.setWidth(50);
TableColumn table1_column5 =new TableColumn(table1,SWT.NULL);
table1_column5.setText("byte");
table1_column5.setWidth(60);
TableColumn table1_column6 =new TableColumn(table1,SWT.NULL);
table1_column6.setText("byte/行");
table1_column6.setWidth(55);
TableColumn table1_column4 =new TableColumn(table1,SWT.NULL);
table1_column4.setText("ディレクトリー");
table1_column4.setWidth(140);
}
public static void main(String[] args) {
Display display=new Display();
Shell shell=new Shell(display,SWT.CLOSE|SWT.MIN);
WordCountMain main=new WordCountMain(shell);
shell.open();
while(!shell.isDisposed()){
if (!display.readAndDispatch ()){
display.sleep ();
}
}
display.dispose();
}
public void widgetSelected(SelectionEvent event) {
Object target=event.getSource();
if(target==null){
//maybe not happen.
System.out.println("target==null");
}
//null1
else if(target==filelist1){
do_filelist1();
}
//null1
else if(target==button1){
do_count();
}
//null1
else if(target==table1){
do_table1();
}
}
/**
*
*/
//null1
public void do_filelist1(){
System.out.println("filelist1");
}
//null1
public void do_count(){
table1.removeAll();
button1.setEnabled(false);
String[] fileList=filelist1.getFiles();
for(int i=0;i<fileList.length;i++){
countFile(new File(fileList[i]));
}
button1.setEnabled(true);
}
/**
* @param file
*/
private void countFile(File file) {
if(file.isDirectory()){
String list[]=file.list();
for (int i = 0; i < list.length; i++) {
File newFile=new File(file,list[i]);
countFile(newFile);
}
}else{
System.out.println(file.getAbsolutePath());
try {
WordCountData data=new WordCountData();
long length=file.length();
WordCountInputStream wc=new WordCountInputStream(new FileInputStream(file));
BufferedInputStream input=new BufferedInputStream(wc);
int c;
long read=0;
while((c=input.read())!=-1){
//do nothing.
read++;
}
input.close();
data.setCountLine(wc.getCountLineSeparator());
data.setCountWord(wc.getCountWord());
data.setBytes(length);
data.setName(file.getName());
data.setDirectory(file.getParent());
addResult(data);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
//null1
public void do_table1(){
}
public void execExit(){
shell.close();
}
public void execAddFile(){
filelist1.execAddFile();
}
public void execAddFolder(){
filelist1.execAddFolder();
}
public void execCopy(){
copyTable.execCopy();
}
public void addResult(WordCountData data){
TableItem item=new TableItem(table1,SWT.NULL);
String values[]=new String[6];
values[0]=data.getName();
values[1]=""+data.getCountWord();
if(data.getBytes()!=0){
values[2]=""+(data.getCountLine()+1);
}else{
values[2]=""+data.getCountLine();
}
values[3]=""+data.getBytes();
values[4]=""+data.getBytesPerLine();
values[5]=data.getDirectory();
item.setText(values);
}
public void execHomepage(){
Program program=Program.findProgram("html");
if(program!=null){
program.execute(HOMEPAGE_URL);
}
}
public void execVersion() {
MessageBox box = new MessageBox(shell, SWT.OK | SWT.ICON_INFORMATION);
box.setMessage(APP_NAME+" v" + version + "\r\n\r\n"
+ "License CPL or Apache2.0\r\n (c)アッキー 2004\r\n"
+ HOMEPAGE_URL);
//int result=box.open();
//never use result int,for PMD
box.open();
}
/* (non-Javadoc)
* @see org.eclipse.swt.events.SelectionListener#widgetDefaultSelected(org.eclipse.swt.events.SelectionEvent)
*/
public void widgetDefaultSelected(SelectionEvent arg0) {
// TODO Auto-generated method stub
}
}
|