国产一区二区精品久久_蜜桃狠狠狠狠狠狠狠狠狠_午夜视频精品_激情都市一区二区

當前位置:首頁 > 網站舊欄目 > 學習園地 > 設計軟件教程 > eclipse comparedialog reuse

eclipse comparedialog reuse
2010-01-14 22:59:58  作者:  來源:
最近由于工作需要,學習了ECLIPSE的COMPARE插件,并利用它進行了兩個String的比較.

相關資料:
http://wiki.eclipse.org/FAQ_How_do_I_create_a_Compare_dialog%3F
http://wiki.eclipse.org/FAQ_How_do_I_create_a_compare_editor%3F
http://wiki.eclipse.org/FAQ_How_can_I_use_and_extend_the_compare_infrastructure%3F

另外ECLIPSE本身有一些對COMPARE擴展得例子,可以從網站上下載.比如說org.eclipse.compare.examples, org.eclipse.compare.examples.xml兩個插件分別擴展了COMPARE插件使得可以對JAVA的PROPERTY文件和 XML文件進行比較.這兩個例子插件都是擴展COMPARE PLUGIN,使用EDITOR來顯示COMPARE結果.大家可以去看源代碼,這里就不多說了.

下面主要說一下我自己做的部分.
復用COMPAREDIALOG,來比較自己的東西,有兩種途徑:一種是繼承CompareEditorInput寫一個自己的INPUT,一種是實現CompareItem類.
我使用的是第一種途徑.
CompareConfiguration cc = new CompareConfiguration();
cc.setLeftLabel();
cc.setRightLabel();
CompareEditorInput finput = new MyCompareInput(cc);
首先建一個CompareConfiguration.這是個配置類,我們可以使用它來指定Comparedialog上顯示得一些東東.比如說我們可以設置用來顯示互相對比的兩個字符串的左右兩個文本框的label(如上所示).
然后我們可以用這個配置類來構建自己的INPUT類.比如說這里是MyCompareInput.
這個類繼承CompareEditorInput,這樣才能作為openCompareDialog得輸入.
CompareUI.openCompareDialog(finput);
這樣就可以打開ECLIPSE的COMPAREDIALOG進行實際的比較了.
在MyCompareInput中我們需要提供一個方法來接受傳入的要進行比較得兩個東西.比如說我們在工作區中選了兩個文件,想要比較它們.我們就可以實現一個setSelection方法接受傳入得selection,然后再來得到這兩個文件.
我這里用了一個setStrings方法,來傳入兩個要比較的字符串.
在MyCompareInput中最重要得要實現的方法是prepareInput,比較字符串就是在這個方法里去比較得.
這這個方法中,比較得任務是由Differencer類去完成.這個類ECLIPSE已經幫我們完成了.
Differencer d = new Differencer() {
protected Object visit(Object parent, int description,
Object ancestor, Object left, Object right) {
return new DiffNode((IDiffContainer) parent, description,
(ITypedElement) ancestor, (ITypedElement) left,
(ITypedElement) right);
}
};
使用時我們只需藥實例化這個類或者根據需要繼承這個類.
Differencer d = new Differencer();
實際得比較是用了Differencer的findDifferences方法.所以我們只需要用要進行比較的兩個字符串,分別構建一個實現ITypedElement接口的類,傳進findDifferences去就行了.
d.findDifferences(false, pm, null, null, fLeftResource,fRightResource);
fLeftResource,fRightResource就是我們實現了ITypedElement得源.作為要進行比較的源,我們還需要實現 IStreamContentAccessor接口.IStreamContentAccessor接口只有一個需要實現的方法createStream,ECLIPSE用它來把要比較的東西構建成一個輸入流.這樣COMPARE比較框架才能夠去一點一點的比較.下面是我實現得 fLeftResource的類.
class MyCompareNode extends BufferedContent implements ITypedElement {

private String fResource;

MyCompareNode (String resource) {
fResource = resource;
}

protected InputStream createStream() throws CoreException {
InputStream is = null;
is = new ByteArrayInputStream(fResource.getBytes());
return is;
}

public Image getImage() {
return null;
}

public String getName() {
return null;
}

public String getType() {
return ITypedElement.TEXT_TYPE;
}
}
針對不同的比較對象,createStream方法可以使用不同的方法來得到InputStream,只要能構建成InputStream, eclipse就能進行比較.另外getType方法可以返回三種類型:FOLDER_TYPE,TEXT_TYPE,UNKNOWN_TYPE.可以根 據需要自己選擇.
構建完prepareInput方法,我們就完成了全部工作.就可以利用COMPARE框架來比較我們自己的東西了.

另一種方法是實現CompareItem類.這個可以去看
http://wiki.eclipse.org/FAQ_How_do_I_create_a_Compare_dialog%3F
eclipsefaq中有個例子CompareStringsAction .
這里的CompareItem也是需要實現IStreamContentAccessor,ITypedElement兩個接口的.
public class CompareStringsAction implements IWorkbenchWindowActionDelegate {
private static final int SECS_PER_YR = 60 * 60 * 24 * 365;
private ResourceBundle messages = ResourceBundle
.getBundle("org.eclipse.faq.examples.actions.CompareMessages");
private final Random rand = new Random();
private IWorkbenchWindow window;
public void dispose() {
}
private long getRandomDate() {
return System.currentTimeMillis() - (1000 * ((long)rand.nextInt(SECS_PER_YR)));
}
private String getRandomString() {
int len = rand.nextInt(200);
StringBuffer buf = new StringBuffer(len);
for (int i = 0; i < len; i++) {
buf.append((char) ('a' + rand.nextInt(26)));
}
return buf.toString();
}
public void init(IWorkbenchWindow window) {
this.window = window;
}
/**
* Opens a dialog allowing the user to select one from a group of random
* strings. The resulting selection is then displayed in a message dialog
*/
public void run(IAction action) {
//create several random string editions
final int count = rand.nextInt(20) + 5;
CompareItem[] items = new CompareItem[count];
for (int i = 0; i < items.length; i++)
items[i] = new CompareItem("String " + i, getRandomString(),
getRandomDate());
EditionSelectionDialog dialog = new EditionSelectionDialog(window
.getShell(), messages);
ITypedElement result = dialog.selectEdition(items[0], items, null);
if (result == null)
return;
String value = ((CompareItem) result).getString();
MessageDialog.openInformation(window.getShell(), "Your selection is...",
value);
}
public void selectionChanged(IAction action, ISelection selection) {
}
}

class CompareItem
implements
IStreamContentAccessor,
ITypedElement,
IModificationDate {
private String contents, name;
private long time;
CompareItem(String name, String contents, long time) {
this.name = name;
this.contents = contents;
this.time = time;
}
CompareItem(String name, String contents) {
this(name, contents, System.currentTimeMillis());
}
public InputStream getContents() throws CoreException {
return new ByteArrayInputStream(contents.getBytes());
}
public Image getImage() {
return null;
}
public long getModificationDate() {
return time;
}
public String getName() {
return name;
}
public String getString() {
return contents;
}
public String getType() {
return ITypedElement.TEXT_TYPE;
}
}

安徽新華電腦學校專業職業規劃師為你提供更多幫助【在線咨詢