`
phinecos
  • 浏览: 342455 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

Azureus源码剖析(五)

 
阅读更多

这篇说说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;
}
}

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics