Overview概述
Java GUI toolkits have always been a controversial topic. The same type of debates also happened in other languages such as Smalltalk. In fact there exist such debates on every platform-independent language. It is especially prominent in Java because Java is the most dominating language today.
Java GUI 工具包一直是一個倍受爭議的話題。同樣的爭論也發生在其他編程語言如Smalltalk。實際上每個平臺無關的語言都存在著這樣的爭論。Java作為當前最受廣泛使用的編程語言而尤為突出。
So what’s the controversy? Basically, it is a dispute between people who support emulated components(or widgets, or controls) and those who support native components. There are two camps among Java developers, one of which advocates for emulated components as in Swing, and the other native components as in SWT.
這場爭論在支持模擬組件(如widgets和control,在下文中也稱之為仿造組件)和支持本地組件(在下文中也稱之為原生組件)的人們之間展開,于是Java開發者形成了兩個不同的陣營,提倡使用模擬組件的Swing,和提倡使用原生組件的SWT。[戰爭從來都在相似的人們之間展開,因為它們想要一個東西]
History歷史
There are many stories about around the dispute on the Internet. You should probably have already heard of them. One of them can help you to understand the whole picture. Let’s start with this one, in which Amy Fowler from Swing team is one of the protagonists.
Internet上有許多圍繞這一爭論的故事。你可能已經聽說過它們中的大多數了,其中之一有助于讓你理清頭緒,讓我們就從這里開始,Amy Fowler是Swing陣營的一個倡導者。
Back in 1990s, there were three companies, which developed their products using Smalltalk. They are IBM, Digital Talk and Parc-Place. IBM and Digital Talk adopted native components, while Parc-Place believed that emulation is the way to go. At the very beginning, all things went well until IBM overtook the others. The other two then merged to form a new company named Objectshare. Then a huge battle erupted when they tried to merge their products into one. The native and emulated crowd fought the battle nearly to death. When Amy Fowler from Parc-place, who insisted on emulated components, finally won the victory, IBM had got all of their accounts, because the two companies did nothing but quarrel for an entire year. The share price of the company, went down to under 1 dollar a share and were pulled from NASDAQ because of incorrect financial reportings.
回到上個世紀90年代,曾幾何時有3家龐大的Smalltalk公司——IBM、Parc-Place和 Digitalk。在90年代初期3家公司的市場份額大致相等,生活是美好的。Parc-Place采用仿窗口部件(emulated widgets)的設計(即Swing的設計),IBM和Digitalk則采用原生窗口部件(native widgets)。后來IBM壓倒了另外兩家,因此他們打算合并成一家,假設叫做Parc-Place Digitalk。隨后當他們試圖將他們的產品融合到一個叫做Jigsaw的計劃中時爆發了一場大戰,計劃由于政治原因失敗了(開發人員實際上已經能讓它運轉起來),就因為原生和仿造兩派的死戰。Amy贏得了精神上的勝利,不過IBM贏得了他們所有的生意,因為這兩家公司在一整年里除了吵架什么都沒做。當塵埃落定之后PPD(Parc-Place Digitalk當時已改名為Objectshare,跟Windscale改名為Sellafield的原因相同——讓人們淡忘之前發生的災難)的股票價格從60美元掉到了低于1美元1股。他們因為偽報收入被NASDAQ摘牌,從此消失。

At that time, AWT had just existed. Sun had built a basic set of portable control classes that mapped to native components on the different operating systems. However AWT was very buggy. It was beyond belief this was just poor code that could be fixed. Just then, Amy was hired by Sun, and she promised to solve all of the problems by doing a lightweight solution. Convinced, Sun management to make her the head of the GUI development. Amy then hired all her old Parc-Place friends and they set about creating Swing.
當時,AWT已經出現了。SUN當時已經建立了一套基本的可移植控件類,這些類映射到不同操作系統上的原生窗口組件(native widget),當時的AWT還滿是漏洞,遠不能稱為可靠,還需要SUN的coder們去修補。然后Amy被雇傭了,她承諾通過輕量級方案解決所有窗口組件的問題,以此說服SUN管理層讓她當了GUI開發部門的頭頭。隨后Amy雇傭了所有她過去在Parc-Place的舊朋友,讓他們來開發Swing。[不知道Amy是不是理想主義者,但我是,從我選擇Java那天起]
In IBM, Visual Age for Java were first written in Smalltalk, which used native components. Then they started to migrate them to a Java code. All of the IBM developers are the those Smalltalk guy, and they hated Swing for its emulated nature. They reluctantly built it using Swing. At that time, it is of no doubt that Swing was ugly, slow and buggy. Therefore they created a project to migrate the Smalltalk native components over to Java. This toolkit was later called SWT, which initially means Simple Widget Toolkit and later Standard Widget Toolkit. It was a success as they released a product called Visual Age Micro Edition. The folks later found that there were bugs in Swing reading windows events, which could cause memory leaks. So they took the decision that SWT and AWT/Swing should not co-exist and then put the toolkit into Eclipse which is a tools platform derived from the early Visual Age.
在IBM,VisualAge for Java最初是用Smalltalk(用的是原生窗口組件)寫的,當將這些工具向Java代碼庫遷移時,他們需要一套窗口組件。IBM這邊的開發人員都是原來搞Smalltalk的那一批人,他們對管理層要求用Swing來構建WebSphere Studio工具都非常不情愿。“Swing是個可怕的充滿缺陷的怪獸“。因此開始了一個新的項目,把他們的Smalltalk原生窗口組件移植到Java上去。這個工具集后來被成為SWT,S開始是Simple的縮寫,不過后來變成了Standard的縮寫。這個項目獲得了成功,被運用在發布的 VisualAge Micro Edition產品中。他們當時發現在Swing讀事件隊列的時候用了一種可能留下內存漏洞的方式,而不得不采用他們自己的查詢 Windows事件隊列的循環,以糾正這個錯誤。這促成了他們關于SWT和 AWT/Swing不能共存的決定。他們把這個工具包放到了Eclipse中,這是一個來自于早期Visual Age的工具平臺。

The above story should have given you an overview of the history of the three, especially SWT. Now you might think, the reason IBM did to create SWT was valid and Swing should follow the way SWT has been on. Actually this opinion is very superficial. When you dwell upon to the nature of Java, you will find that it is not that simple as you expect.
你應該已經從上述的故事中對三者的歷史有了大概的了解,尤其是SWT。現在你也許會覺得,IBM創建SWT的理由是合理的而Swing應該沿用SWT采用的方式。這樣的觀點是片面的,當你深入了解到Java的本質之后,你會發現其實并不像你想象的那么簡單。
Prerequisite先決條件
What is the essential feature of Java, which has influenced the decision in the toolkit design? Or what is the prerequisite of a java GUI toolkit?
什么才是Java本質的,影響到工具集設計的特征呢?或者說,什么才是Java GUI工具集設計的先決條件呢?
The answer comes from one of Sun's promises about Java, write once, run anywhere. It is one of the java's advantages over other languages. Before Java was created, software portability was a nightmare especially to those who wanted to support multiple platforms. It is especially true in modern days when the Internet is so popular. People from different parts of the world are working on different platforms. And it is very common for a software vendor to support multiple operating systems. Java’s write-once-run-anywhere promise was obvious a relief to developers. It can greatly improve software development productivity.
答案來自于Sun對Java的承諾之一:write once, run anywhere(一次編寫,隨處運行)。這是Java不同于其他語言的優勢所在。在Java被創建之前,軟件的跨平臺性能是開發者,特別是那些希望對多平臺提供支持的開發者的夢魘。在當今的生活中Internet的使用已經相當的普遍了,在世界不同角落的人們在不同的平臺上工作著。軟件提供商為不同的操作系統提供支持是再平凡不過的事情。Java的write-once-run-anywhere(WORA)承諾顯然減輕了開發者的負擔,極大地提高了軟件開發的生產力。
However to write a portable applications, you should use those standard libraries, which provide platform-independent APIs. These standard libraries include language support, common utilities, networking, I/O and GUI toolkit, etc. So when Sun started to design the GUI toolkit, the first thing it should consider is a well-designed platform-independent API. AWT and Swing were designed carefully in such a way to avoid platform incompatibility. SWT on the contrary was initially designed without this portability in mind. It was first designed for an IDE, Visual Age for Java. And at that time, it seemed that Windows was their first priority. Therefore SWT API is more akin those of Windows. In general SWT is not as portable as Swing. Although Steve Northover, the father of SWT, argued that SWT is platform independent, you can easily find many Windows API inheritances.
然而編寫跨平臺的應用程序,你必須使用支持平臺無關性的標準庫。這些標準庫包括語言支持,公共用途,網絡,I/O和GUI工具集等。所以當Sun開始設計GUI工具集的時候,首要任務就是考慮一個設計良好的平臺無關的API。AWT和Swing都被小心地設計以保證平臺兼容性。SWT則相反,它在設計之初并不以擴展性為原則,它為一個專有的IDE Visual Age for Java而設計,Windows作為這個IDE的首選運行環境擁有很高的優先級考量。SWT API類似于WIndows,通常它并不如Swing的擴展性好,盡管Steve Northover,SWT之父,辯稱SWT是平臺無關的,你可以很容易地發現許多Windows API的痕跡。[Windows API的痕跡,并不是一個程序的問題,卻是一個跨平臺程序的問題,因為多數用linux或mac的人,對Windows上能提供的東西并不感興趣。]
Differences區別
GUI application is one of the major types of softwares. So Java GUI library should be standardized and integrated into the JRE platform. However different operating systems have different GUI styles and component sets. There are some components which exist on all the platforms and have similar look and feels. These common components such as button, label, textfield, checkbox, etc are also called standard components. Different gui toolkits provide different set of components. Same component from different toolkits may have different look and feel. GUI toolkit usually follows different principle when choosing component types and features to implement. When examining a gui toolkit, we generally have two different levels of concerns. One is component types, the other is component features.
GUI應用程序是軟件的一種主要類型,所以Java的GUI庫應該是標準化并被集成到JRE平臺中的。然而不同的操作系統有不同的GUi風格和組件集。有一些組件在所以平臺上有相似的觀感。這些共有組件如按鈕,標簽,文本域,單選框等被稱為標準組件。不同的GUI工具集提供了不同的組件集。GUI工具集總是遵循不同的原則來選擇組件類型和特征以實現。考察一個工具集,有兩個不同的要素:組件類型和組件特征。
Terms
First let me illustrate two mathematical concepts to you: LCD and . LCD means least common denominator. means greatest common denominator. Look at the figure below. There are three different sets standing for different Oses. The intersecting part is LCD of the three, while the union is of the three.
首先讓我圖解兩個數學概念:最大公約數和最小公倍數。如下圖,三個集合代表不同的操作系統。相交的部分是最大公約數,合并的部分是最小公倍數。

Component Types and Features組件類型和特征
Now let’s examine the component types and features of the three Java GUI toolkits, AWT, SWT and Swing.
現在讓我們來考察Java GUI工具集AWT,SWT和Swing的組件類型和特征
AWT
AWT component set complies LCD principle, which means AWT only has the common set of components which exist on all platforms. So you cannot find advanced components such as table or tree in AWT, because these components are not supported on some other platforms. As to feature set per component, AWT complies LCD too. It can only support those features available from all platforms. For example, AWT buttons is not able to be attached with an icon, because on Motif platform, button is not supposed to have an icon.
AWT組件集遵循最大公約數原則,即AWT只擁有所有平臺上都存在的組件的公有集合。所以你在AWT中無法獲取如表或樹等高級組件,因為它們在某些平臺上不支持。AWT的組件特征同樣遵循這一原則。它只提高平臺上公有的特征。例如AWT按鈕不能附著圖片,因為在Motif平臺上,按鈕是不支持圖片的。
Since its poor component and feature support, AWT did not attract developers much. And it is deprecated by Sun. It is only there to ensure backward compatibility and support Swing.
由于它低劣的組件集和特征,AWT無法吸引開發者。它是Sun不推薦使用的,只是為了確保向下兼容和支持Swing。
SWT
One of SWT’s initial goal is to provide a more rich component set than AWT. It adopts (greatest common denominator) principle to provide a union set of components that appear on every platform. The idea is that if a component type exists on the platform, then SWT will encapsulate it using java code and JNI call. If a component does not exist on the platform, it then emulates the component by extending and drawing a Composite. A SWT Composite is similar to AWT Canvas. By this way, SWT provides a richer component set than AWT. It is worth to point out that SWT JNI wrapper is different from AWT. Its emulation is also different from Swing.
SWT最初的目標之一是為了提供比AWT更為豐富的組件集。它遵循最小公倍數原則以提供一個各個平臺上包含的組件的并集。思路是如果一個組件在某個平臺上包含,那么SWT就會包裝它并用java代碼和JNI來調用它。如果一個組件在某一平臺上不存在,它就會用繼承并繪制Composite的方式來模擬組件。一個SWT Composite類似于AWT的Canvas。以這種方式,SWT提供了較AWT更為豐富的組件集。值得指出的是SWT的JNI封裝不同于AWT,它的模擬也不同于Swing。
As to component feature set, SWT is similar to AWT. They complies to LCD. In early SWT versions, SWT button did not support icon attachment for the same reason as AWT. But later, many of those missing features were made up using emulations. But still, there are features which cannot be implemented purely by emulation. SWT has its components completely controlled native operating system. It is hard to extend. Only features like some graphic decoration can customized by emulating. So strictly speaking, SWT component feature set can not be as rich as Swing due to its difficult to extend.
在組件特征方面,SWT類似于AWT。它遵循最大公約數原則。在早期的SWT版本中,SWT按鈕因為和AWT同樣的原因不支持附著圖片。在之后的版本中,許多缺失的特征采用模擬的方式補全。但仍有許多特征無法采用純粹的模擬實現。SWT將組件的控制交給本地操作系統。它難以擴展。只有例如圖形裝飾等特征可以借助模擬繪制來自定義實現。所以嚴格意義上將,SWT組件的組件集和特征因其難于擴展而不如Swing來得豐富。
Swing
Swing is the most powerful and flexible of the three. With respect to component types, Swing complies to greatest common denominator. Because Swing controls all of the GUI system and it is very extensible and flexible, Swing can almost create any component you could imagine. The only limitation of Swing is its AWT containers. In Swing you still can not implement real transparent or irregular-shaped windows, because Swing depends on those AWT top containers including Applet, Window, Frame and Dialog etc. Except these niches, Swing has implemented almost all the standard components on every platform.
Swing是三者中最強大和靈活的。在組件類型上,它遵循最小公約數原則。由于Swing可以控制自身GUI系統的全部并有很好的可擴展和靈活性,它幾乎可以創建所有你想象得到的組件。唯一的限制是它的AWT容器。在Swing中你還不能跨平臺地實現真正的透明化和不規則矩形窗口,因為Swing依賴于AWT頂層容器例如Applet, Window, Frame and Dialog等。除此之外,Swing幾乎實現了所有平臺上的標準組件。[都說Java7中能夠實現,也有一些跡象看來接近,期待吧]
As to component feature set, Swing complies to greatest common denominator. It has most of the component features available on every platform. What's more, you can extend the existing Swing components and add more features to them.
在組件特征上,Swing遵循最小公倍數原則。它擁有所有平臺上可提供的組件特征。不僅如此,你還可以繼承已有的Swing組件并添加新的特性。
(To be continued ...)