这篇说说GUI方面,就以打开种子文件这个窗口为例,我对其代码进行了精简,拿出了一个基本的骨架。
首先来看基本的消息主循环部分:
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->finalDisplaydisplay=newDisplay();
invoke(null);//创建窗口的主代码
while(stTorrentWindow!=null&&!stTorrentWindow.bClosed)
{//窗口创建完成且没有关闭
if(!display.readAndDispatch())
{
display.sleep();
}
}
display.dispose();
这里运用了单例模式来表示窗口,考虑到线程同步性,在静态工厂方法中使用了synchronized 关键字
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->privatestaticTorrentWindowstTorrentWindow=null;
publicsynchronizedstaticfinalvoidinvoke(Shellparent)
{
if(stTorrentWindow==null)
{//第一次创建窗口
stTorrentWindow=newTorrentWindow(parent);
}
else
{//激活已经创建的窗口
if(stTorrentWindow.shell!=null)
{
stTorrentWindow.shell.forceActive();
}
}
}
privateTorrentWindow(finalShellparent)
{
openWindow(parent);
}
真正的窗口创建工作是在openWindow方法中完成的,下面给出部分核心代码:
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->privatevoidopenWindow(Shellparent)
{
GridDatagridData;
shell=ShellFactory.createShell(parent,SWT.RESIZE|SWT.DIALOG_TRIM);
shell.setText("打开Torrent");
GridLayoutlayout=newGridLayout();
shell.setLayout(layout);
shell.addListener(SWT.Resize,newListener()
{
publicvoidhandleEvent(Evente){
}
});
//Torrents
//========
CompositecButtons=newComposite(shell,SWT.NONE);
RowLayoutrLayout=newRowLayout(SWT.HORIZONTAL);
rLayout.marginBottom=0;
rLayout.marginLeft=0;
rLayout.marginRight=0;
rLayout.marginTop=0;
cButtons.setLayout(rLayout);
//ButtonsfortableTorrents
ButtonbrowseTorrent=newButton(cButtons,SWT.PUSH);
browseTorrent.setText("添加文件");
browseTorrent.addListener(SWT.Selection,newListener(){
publicvoidhandleEvent(Eventarg0){
FileDialogfDialog=newFileDialog(shell,SWT.OPEN|SWT.MULTI);
fDialog.setFilterExtensions(newString[]{
"*.torrent",
"*.tor",
FILE_WILDCARD
});
fDialog.setFilterNames(newString[]{
"*.torrent",
"*.tor",
FILE_WILDCARD
});
fDialog.setText("选择Torrent文件");
StringfileName=fDialog.open();
if(fileName!=null)
{
//addTorrents(fDialog.getFilterPath(),fDialog.getFileNames());
}
}
});
setGridData(cButtons,GridData.FILL_HORIZONTAL,browseTorrent,MIN_BUTTON_HEIGHT);
ButtonbrowseURL=newButton(cButtons,SWT.PUSH);
browseURL.setText("从URL添加");
browseURL.addListener(SWT.Selection,newListener(){
publicvoidhandleEvent(Evente){
browseURL();
}
});
ButtonbrowseFolder=newButton(cButtons,SWT.PUSH);
browseFolder.setText("从文件夹添加");
browseFolder.addListener(SWT.Selection,newListener(){
publicvoidhandleEvent(Evente)
{
DirectoryDialogfDialog=newDirectoryDialog(shell,SWT.NULL);
fDialog.setMessage("选择Torrent文件所在目录");
Stringpath=fDialog.open();
if(path!=null)
{
addTorrents(path,null);
}
}
});
GroupgTorrentsArea=newGroup(shell,SWT.NONE);
gridData=newGridData(GridData.FILL_HORIZONTAL);
gTorrentsArea.setLayoutData(gridData);
layout=newGridLayout();
gTorrentsArea.setLayout(layout);
gTorrentsArea.setText("Torrent文件");
CompositecTorrentList=newComposite(gTorrentsArea,SWT.NONE);
gridData=newGridData(GridData.FILL_HORIZONTAL);
cTorrentList.setLayoutData(gridData);
createTorrentListArea(cTorrentList);
//关闭窗口
shell.addDisposeListener(newDisposeListener()
{
publicvoidwidgetDisposed(DisposeEvente)
{
if(!bClosed)
close(false,true);
}
});
shell.addListener(SWT.Traverse,newListener()
{
publicvoidhandleEvent(Evente)
{
if(e.detail==SWT.TRAVERSE_ESCAPE)
{
close(true,true);
}
}
});
shell.open();//显示窗口
}
这里最重要的如何创建Shell的:
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->shell=ShellFactory.createShell(parent,SWT.RESIZE|SWT.DIALOG_TRIM);
下面就来看看ShellFactory的代码,主要是在ShellManager中加入新创建的Shell,如果此Shell已经创建过,则不再次加入
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->publicfinalclassShellFactory
{
publicstaticShellcreateShell(finalShellparent,finalintstyles)
{
returngetRegistedShell(newShell(parent,styles));
}
privatestaticShellgetRegistedShell(finalShelltoRegister)
{
if(null==toRegister)
returnnull;
ShellManager.sharedManager().addWindow(toRegister);
returntoRegister;
}
}
最后来看ShellManager是如何管理Shell的:
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->publicclassShellManager
{
privatestaticShellManagerinstance;
privatefinalCollectionshells=newArrayList();//被管理的Shell
privatefinalListaddHandlers=newLinkedList();//加入Shell时调用
privatefinalListremoveHandlers=newLinkedList();//删除Shell时调用
static
{
instance=newShellManager();
}
/**
*<p>Getstheapplication'ssharedshellmanager</p>
*<p>ThisShellManagerhasnobearingonotherShellManagerinstances</p>
*<p><b>Note</b>:ThismethodmustbeinvokedbytheSWTdisplaythread</p>
*@return
*/
publicstaticfinalShellManagersharedManager()
{//静态工厂方法
returninstance;
}
publicstaticbooleanverifyShellRect(Shellshell,booleanbAdjustIfInvalid)
{//验证窗口矩阵的合法性
booleanbMetricsOk;
try{
bMetricsOk=false;
PointptTopLeft=shell.getLocation();
Monitor[]monitors=shell.getDisplay().getMonitors();
for(intj=0;j<monitors.length&&!bMetricsOk;j++){
Rectanglebounds=monitors[j].getBounds();
bMetricsOk=bounds.contains(ptTopLeft);
}
}catch(NoSuchMethodErrore){
Rectanglebounds=shell.getDisplay().getBounds();
bMetricsOk=shell.getBounds().intersects(bounds);
}
if(!bMetricsOk&&bAdjustIfInvalid){
centreWindow(shell);
}
returnbMetricsOk;
}
publicstaticvoidcentreWindow(Shellshell)
{//窗口居中
RectangledisplayArea;//areatocenterin
try{
displayArea=shell.getMonitor().getClientArea();
}catch(NoSuchMethodErrore){
displayArea=shell.getDisplay().getClientArea();
}
RectangleshellRect=shell.getBounds();
if(shellRect.height>displayArea.height){
shellRect.height=displayArea.height;
}
if(shellRect.width>displayArea.width-50){
shellRect.width=displayArea.width;
}
shellRect.x=displayArea.x+(displayArea.width-shellRect.width)/2;
shellRect.y=displayArea.y+(displayArea.height-shellRect.height)/2;
shell.setBounds(shellRect);
}
/**
*Addsashelltotheshellmanager.Iftheshellisalreadymanaged,itisnotaddedagain.
*<p><b>Note</b>:ThismethodmustbeinvokedbytheSWTdisplaythread</p>
*@paramshellASWTShell
*/
publicfinalvoidaddWindow(finalShellshell)
{//加入新窗口
//Debug.out("Invokedbythread"+Thread.currentThread().getName());
if(shells.contains(shell)){return;}
shells.add(shell);
notifyAddListeners(shell);
shell.addDisposeListener(newDisposeListener()
{
publicvoidwidgetDisposed(DisposeEventevent)
{
try
{
removeWindow(shell);
}
catch(Exceptione)
{
//Logger.log(newLogEvent(LogIDs.GUI,"removeWindow",e));
}
}
});
shell.addListener(SWT.Show,newListener()
{
publicvoidhandleEvent(Eventevent)
{
verifyShellRect(shell,false);
}
});
}
/**
*Removesashellfromtheshellmanager
*<p><b>Note</b>:ThismethodmustbeinvokedbytheSWTdisplaythread</p>
*@paramshellASWTShell
*/
publicfinalvoidremoveWindow(Shellshell)
{//删除窗口
shells.remove(shell);
notifyRemoveListeners(shell);
}
/**
*<p>GetstheshellsmanagedbythemanagerasanIterator</p>
*<p>Theorderinwhichtheshellswereaddedareretained.</p>
*<p><b>Note</b>:ThismethodmustbeinvokedbytheSWTdisplaythread</p>
*@returnTheiterator
*/
publicfinalIteratorgetWindows()
{
returnshells.iterator();
}
/**
*GetswhethertheShellManagermanagesnoshells
*@returnTrueifShellManagerisempty
*/
publicfinalbooleanisEmpty()
{
returnshells.isEmpty();
}
/**
*GetsthenumberofshellstheShellManagermanages
*@returnThenumber
*/
publicfinalintgetSize()
{
returnshells.size();
}
/**
*<p>InvokesthehandleEventmethodspecifiedbytheSWTlistenerforeachmanagedshell</p>
*<p>Theevent'swidgetissettothereferenceoftheshellinvokingit</p>
*@paramcommandAcommandimplementedasaSWTListener
*/
publicfinalvoidperformForShells(finalListenercommand)
{
Iteratoriter=shells.iterator();
for(inti=0;i<shells.size();i++)
{
ShellaShell=(Shell)iter.next();
Eventevt=newEvent();
evt.widget=aShell;
evt.data=this;
command.handleEvent(evt);
}
}
/**
*Getsthesetofmanagedshells
*@returnTheset
*/
protectedfinalCollectiongetManagedShellSet()
{
returnshells;
}
//events
/**
*<p>AddsalistenerthatwillbeinvokedwhenashellhasbeenaddedtotheShellManager</p>
*<p>Thelistenerandtheshellwillautomaticallyberemovedwhentheshellisdisposed</p>
*@paramlistenerASWTListener
*/
publicfinalvoidaddWindowAddedListener(Listenerlistener)
{
addHandlers.add(listener);
}
/**
*RemovesalistenerthatwillbeinvokedwhenashellhasbeenaddedtotheShellManager
*@paramlistenerASWTListener
*/
publicfinalvoidremoveWindowAddedListener(Listenerlistener)
{
addHandlers.remove(listener);
}
/**
*AddsalistenerthatwillbeinvokedwhenashellhasbeenremovedfromtheShellManager
*@paramlistenerASWTListener
*/
publicfinalvoidaddWindowRemovedListener(Listenerlistener)
{
removeHandlers.add(listener);
}
/**
*RemovesalistenerthatwillbeinvokedwhenashellhasbeenremovedfromtheShellManager
*@paramlistenerASWTListener
*/
publicfinalvoidremoveWindowRemovedListener(Listenerlistener)
{
removeHandlers.remove(listener);
}
/**
*NotifiestheWindowAddedListenerhandlers
*@paramsenderASWTshellthat"sends"theevents
*/
protectedfinalvoidnotifyAddListeners(Shellsender)
{
Iteratoriter=addHandlers.iterator();
for(inti=0;i<addHandlers.size();i++)
{
((Listener)iter.next()).handleEvent(getSWTEvent(sender));
}
}
/**
*NotifiestheWindowRemovedListenerhandlers
*@paramsenderASWTshellthat"sends"theevents
*/
protectedfinalvoidnotifyRemoveListeners(Shellsender)
{
Iteratoriter=removeHandlers.iterator();
for(inti=0;i<removeHandlers.size();i++)
{
((Listener)iter.next()).handleEvent(getSWTEvent(sender));
}
}
/**
*<p>GetsageneratedSWTEventbasedontheshell</p>
*<p>Thewidgetfieldoftheeventshouldbesettotheshell</p>
*@paramshellASWTShell
*@returnTheevent
*/
protectedEventgetSWTEvent(Shellshell)
{
Evente=newEvent();
e.widget=shell;
e.item=shell;
returne;
}
}
分享到:
相关推荐
一个基于JAVA的多torrent下载程序,可以手动设置某个torrent的优先权,加入了irc聊天室,增加了一些基本的irc命令,可以看见在线人数和ID,支持多tracker url,对于多tracker发布的torrent可自动切换,并可以手动...
P2P源码 Azureus 2.5.0.2(JAVA)
基于java的开发源码-P2P源码 Azureus 2.5.0.2(JAVA).zip 基于java的开发源码-P2P源码 Azureus 2.5.0.2(JAVA).zip 基于java的开发源码-P2P源码 Azureus 2.5.0.2(JAVA).zip 基于java的开发源码-P2P源码 Azureus 2.5....
一个基于JAVA的多torrent下载程序,可以手动设置某个torrent的优先权,加入了irc聊天室,增加了一些基本的irc命令,可以看见在线人数和ID,支持多tracker url,对于多tracker发布的torrent可自动切换,并可以手动...
P2P源码 Azureus 2.5.0.2(JAVA).Java源码学习
java源码:P2P源码 Azureus 2.5.0.2(JAVA).rar
Azureus2.3.0.4 eclipse 工程。包含commons-cli-1.0.jar,swt.jar。以及SWT中的DLL文件。并删除了其中的oax。
azureus 是一个linux下很不错的bit下载客户端软件,这是最新版,下载解压即可用!
P2P源码 Azureus 2.5.0.2(JAVA).zip
基于Java的源码-P2P源码 Azureus 2.5.0.2(JAVA).zip
基于Java的实例源码-P2P源码 Azureus 2.5.0.2(JAVA).zip
基于Java的实例开发源码-P2P源码 Azureus 2.5.0.2(JAVA).zip
基于Java的P2P源码 Azureus 2.5.0.2(JAVA).zip
基于java的P2P源码 Azureus 2.5.0.2(JAVA).zip
一个基于JAVA的多torrent下载程序,可以手动设置某个torrent的优先权,加入了irc聊天室,增加了一些基本的irc命令,可以看见在线人数和ID,支持多tracker url,对于多tracker发布的torrent可自动切换,并可以手动...
azureus实现了基于p2p的文件上传,下载功能,这是java版的bit orrent。
一个程基于Java的多torrent下载序,可以手动设置某个torrent的优先权,加入了irc聊天室,增加了一些基本的irc命令,可以看见在线人数和ID,支持多tracker url,对于多tracker发布的torrent可自动切换,并可以手动...
azureus破解版 azureus破解版 azureus破解版
p2p应用,java开源客户端源代码,俗称毒蛙,现在已改为vuze.
免责声明:资料部分来源于合法的互联网渠道收集和整理,部分自己学习积累成果,供大家学习参考与交流。收取的费用仅用于收集和整理资料耗费时间的酬劳。 本人尊重原创作者或出版方,资料版权归原作者或出版方所有,...