Eclipse Preference scope

Preference是Eclipse平臺內置支持的幾種持久化(記憶先前的狀態)的機制中的一種(其它的還有DialogSettings,IMemento)。本篇主要針對Preference的Scope(範圍)進行總結。

Preference持久化文件的後綴爲".prefs"。

The Eclipse runtime defines three so-called scopes. The scope defines how the preference data is stored and how it is changeable.

Table 1. Eclipse Preference scope(Table1來自Lars Vogel的博客)

Scope Description Instance scope If the user runs the same program twice, the settings between the two programs may be different. Configuration scope If the user runs the same program twice then the settings between the two programs are the same. Default scope Default values which can not be changed. Supplied via configuration files in plug-ins and product definitions.   下面通過一些比較淺顯的註釋以及源碼分析來分別瞭解一下各個Scope。
INSTANCE - The instance scope can also be thought of as "workspace". That is, the preferences which are stored in this scope are stored per workspace, or per running instance of Eclipse. This scope corresponds to the default location of preferences in Eclipse 2.1. The instance area is derived from the location returned byorg.eclipse.core.runtime.Platform#getInstanceLocation(). Preferences stored in this scope will not be available to other running instances of Eclipse。
instance scope按照工作空間或平臺的運行實例存儲的,每個運行實例所存儲的值可能是不一樣的,不同實例之間只使用自己的值,無法與其它實例共享。
存儲位置位於運行實例的如下位置:{workspace_path}\.metadata\.plugins\org.eclipse.core.runtime\.settings\{qualifier}.prefs
這點,我們可以通過閱讀下面幾個類去分析一下,限於時間,在此不分析源碼了。所涉及類如下:org.eclipse.core.internal.preferences.InstancePreferences
org.eclipse.core.internal.runtime.DataArea
org.eclipse.core.internal.preferences.EclipsePreferences

configuration scope -The configuration scope is used to store preferences on a per configuration level. Being able store preferences per configuration means that all workspaces share the same preferences. For instance, if you are an Eclipse developer and you have a single Eclipse install but you have multiple workspaces for different projects/branches that you are working on, the preferences stored in the configuration scope will be shared between these workspaces. The configuration area is derived from the location returned byorg.eclipse.core.runtime.Platform#getConfigurationLocation()
configuration scope 各個工作空間將共享它們。例如,如果用戶具有平臺的單個安裝,但是運行幾個不同的工作空間,則配置級別範圍的首選項將在工作空間之間共享。
存儲位置位於程序安裝目錄的如下位置:{configuration}\.settings\{qualifier}.prefs

Default scope表示首選項的缺省值。它們不是由平臺更改或存儲的。但是,這些值來源於隨插件的產品或主要功能部件一起存儲的文件。

另外,常用的Scope還有一個是project Scope,針對特定工程的設置,存儲位置位於
{project_path}\.settings\{qualifier}.prefs

這幾個Scope在eclipse中均有相應的類對應,分別爲:InstanceScope,ConfigurationScope,DefaultScope,ProjectScope。
在後續的博文中,將提及這些Scope的應用。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章