最大的網站源碼資源下載站,
以前我有一個微型的便攜式電子地址薄。我一直認為它很不錯,直到有一天它停止運行了。銷售該產品的人員無法找回我的聯系地址名冊,卻提議更換一臺。這時候我才知道數據的重要性。這個閃亮的小發明與存儲在它里面的數據相比根本就不值一提。public class invokatrondocument
extends properties
{
public static final string package = "package";
public static final string superclass = "superclass";
public static final string interfaces = "interfaces";
}
string package =document.getproperty(invokatrondocument.package);

  它只有一個頁面,右上角沒有圖片。我們想輸入更多的信息,并提供一個很好的圖片。換句話說,我們希望定制這個向導。
  我們來分析一下這個向導。請打開invokatronwizard.java文件。請注意這個類是如何擴展wizard并實現inewwizard接口的。你應該理解它里面的很多方法。為了定制向導,我們簡單地調用或重載其中的某些方法。下面是一些重要的方法:
  生命周期方法
  我們應該重載這些方法,把初始化和析構(destruction)代碼插入向導中:
  · constructor(構造函數):向導實例化的時候、在eclipse給它傳遞信息之前調用。向導的一般初始化實現。通常你希望調用"美化方法"(后面有描述)并設置對話框的默認值。
  · init(iworkbench workbench, istructuredselection editorselection): eclipse調用它為向導提供工作臺的信息。請重載它,保存iworkbench和對象的句柄供以后使用。如果它是一個編輯器向導而不是新向導,我們最好把當前的編輯器選項作為第二個參數。
  · dispose():eclipse調用它執行清理工作。重載它來清除向導使用的資源。
  · finalize():清除代碼,可能使用dispose()代替。
  美化方法
  這些方法都是用于裝飾向導窗體的。
  · setwindowtitle(string title):設置窗體的標題行字符串。
  · setdefaultpageimagedescriptor(imagedescriptor image):用于提供顯示在向導的所有頁面右上方的圖片。
  · settitlebarcolor(rgb color):指定標題欄用什么顏色。
  按鈕方法
  這些方法控制著向導按鈕的實用性和行為。
  · boolean canfinish():重載它用于指定finish(完成)按鈕是否激活(根據向導的狀態)。
  · boolean performfinish():重載它來實現向導的根本的業務邏輯。如果向導沒有完成(錯誤的條件),就返回false。
  · boolean performcancel():重載它,在用戶點擊cancel(取消)按鈕的時候進行清除操作。如果向導不能終止,則返回false。
  · boolean ishelpavailable():重載它用于指定help(幫助)按鈕是否可視。
  · boolean needspreviousandnextbuttons():重載它來指定previous(前一步)和next(后一步)按鈕是否可視。
  · boolean needsprogressmonitor():重載它來指定進度條部件是否可視。當點擊finish按鈕調用performfinish()方法的時候,它就會出現。
  頁面方法
  這些方法控制著頁面的外觀。
  · addpages():向導顯示的時候調用。重載它給向導插入新頁面。
  · createpagecontrols(composite pagecontainer):eclipse調用它來實例化所有的向導頁面(用前面的addpages()方法已經添加的頁面)。重載它給向導添加持續可視的窗體小部件(除頁面之外的部件)。
  · iwizardpage getstartingpage():重載它來檢測哪個頁面是向導的第一個頁面。
  · iwizardpage getnextpage(iwizardpage nextpage):在默認情況下,點擊next按鈕將進入addpages()所提供的數組中的下一個頁面。你可能希望根據用戶選擇進入不同的頁面。重載它來計算后一個頁面。
  · iwizardpage getpreviouspage(iwizardpage previouspage):與getnextpage()類似,用于計算前一個頁面。
  · int getpagecount():檢索addpages()添加的頁面的數量。在典型情況下,你不必重載它,除非你希望顯示頁面的數量和形式。
  其它有用的方法
  這些都是有用的輔助方法:
  · setdialogsettings(idialogsettings settings):你可以載入對話框的狀態,并通過在init()中調用這個方法來設置這些值。在典型情況下,這些設置可以作為向導字段的默認值。請查看dialogsettings類了解更詳細的信息。
  · idialogsettings getdialogsettings():當我們需要數據的時候,就調用這個方法來檢索它。在performfinish()的對話框的末尾,你再次可以把數據保存到文件中。
  · iwizardcontainer getcontainer():對于檢索shell、運行的后臺線程、刷新窗口等非常有用。
  向導頁面方法
  你已經看到了,向導是由一個或多個頁面組成的。這些頁面擴展了wizardpage類,并實現了iwizardpage接口。為了定制單獨的頁面,你必須了解很多方法。下面是一些重要的方法:
  · constructor:用于實例化頁面。
  · dispose():重載它用于實現清除代碼。
  · createcontrol(composite parent):重載它來給頁面添加控件。
  · iwizard getwizard():用于獲取父向導對象。對于調用getdialogsettings()是有用處的。
  · settitle(string title):調用它來設置顯示在向導標題區域中的字符串。
  · setdescription(string description):調用它來提供標題下面顯示的文本內容。
  · setimagedescriptor(imagedescriptor image):調用它來提供頁面右上方出現的圖片(用于代替默認的圖片)。
  · setmessage(string message):調用它來顯示描述字符串下方的消息文本。這些文本是用于警告或提示用戶的。
  · seterrormessage(string error):調用它來高亮度顯示描述字符串下方的消息文本。它一般意味著向導不能繼續,除非錯誤被修正。 
  · setpagecomplete(boolean complete):如果為true,next按鈕就可視。
  · performhelp():重載它來提供內容敏感的幫助信息。當點擊help按鈕的時候向導會調用它。
  編寫向導的代碼
  有了這些方法之后,我們就能夠開發出具有極大的靈活性的向導了。我們現在修改以前建立的invokatron向導,給它添加一個頁面來請求用戶輸入初始的文檔數據。我們還給向導添加了一個圖片。新代碼是粗體的:
public class invokatronwizard extends wizard
implements inewwizard {
 private invokatronwizardpage page;
 private invokatronwizardpage2 page2;
 private iselection selection;
 public invokatronwizard() {
  super();
  setneedsprogressmonitor(true);
  imagedescriptor image =abstractuiplugin.imagedescriptorfromplugin("invokatron", "icons/invokatronicon32.gif");
  setdefaultpageimagedescriptor(image);
 }
 public void init(iworkbench workbench,istructuredselection selection) {
  this.selection = selection;
 }

public void addpages() {
 page=new invokatronwizardpage(selection);
 addpage(page);
 page2 = new invokatronwizardpage2(selection);
 addpage(page2);
}
public boolean performfinish() {
 //首先把所有的頁面數據保存在變量中
 final string containername = page.getcontainername();
 final string filename =page.getfilename();
 final invokatrondocument properties = new invokatrondocument();
 properties.setproperty(invokatrondocument.package,page2.getpackage());
 properties.setproperty(invokatrondocument.superclass,page2.getsuperclass());
 properties.setproperty(invokatrondocument.interfaces,page2.getinterfaces());
 //現在調用完成(finish)方法
 irunnablewithprogress op =new irunnablewithprogress() {
  public void run(iprogressmonitor monitor)
  throws invocationtargetexception {
   try {
    dofinish(containername, filename,properties,monitor);
   } catch (coreexception e) {
    throw new invocationtargetexception(e);
   } finally {
    monitor.done();
   }
  }
 };
 try {
  getcontainer().run(true, false, op);
 } catch (interruptedexception e) {
  return false;
 } catch (invocationtargetexception e) {
  throwable realexception =e.gettargetexception();
  messagedialog.openerror(getshell(),"error",realexception.getmessage());
  return false;
 }
 return true;
}
private void dofinish(string containername,string filename, properties properties,
iprogressmonitor monitor)
throws coreexception {
 // 建立一個示例文件
 monitor.begintask("creating " + filename, 2);
 iworkspaceroot root = resourcesplugin.getworkspace().getroot();
 iresource resource = root.findmember(new path(containername));
 if (!resource.exists() || !(resource instanceof icontainer)) {
 throwcoreexception("container /"" + containername + "/" does not exist.");
}
icontainer container =(icontainer)resource;
final ifile ifile = container.getfile(new path(filename));
final file file =ifile.getlocation().tofile();
try {
 outputstream os = new fileoutputstream(file, false);
 properties.store(os, null);
 os.close();
} catch (ioexception e) {
 e.printstacktrace();
 throwcoreexception("error writing to file " + file.tostring());
}
//確保項目已經刷新了,該文件在eclipse api 之外建立
container.refreshlocal(iresource.depth_infinite, monitor);
monitor.worked(1);
monitor.settaskname("opening file for editing...");
getshell().getdisplay().asyncexec(new runnable() {
 public void run() {
  iworkbenchpage page =platformui.getworkbench().getactiveworkbenchwindow().getactivepage();
  try {
   ide.openeditor(page,ifile,true);
  } catch (partinitexception e) {
  }
 }
});
monitor.worked(1);
}
private void throwcoreexception(string message) throws coreexception {
 istatus status =new status(istatus.error,"invokatron",istatus.ok,message,null);
 throw new coreexception(status);
}
}
  編寫新的向導頁面的代碼
  下一步,我們編寫invokatronwizardpage2。它的整個類都是全新的:
public class invokatronwizardpage2 extends wizardpage {
 private text packagetext;
 private text superclasstext;
 private text interfacestext;
 private iselection selection;
 public invokatronwizardpage2(iselection selection) {
  super("wizardpage2");
  settitle("invokatron wizard");
  setdescription("this wizard creates a new"+" file with *.invokatron extension.");
  this.selection = selection;
 }
 private void updatestatus(string message) {
  seterrormessage(message);
  setpagecomplete(message == null);
 }
 public string getpackage() {
  return packagetext.gettext();
 }
 public string getsuperclass() {
  return superclasstext.gettext();
 }
 public string getinterfaces() {
  return interfacestext.gettext();
 }
public void createcontrol(composite parent) {
 composite controls =new composite(parent, swt.null);
 gridlayout layout = new gridlayout();
 controls.setlayout(layout);
 layout.numcolumns = 3;
 layout.verticalspacing = 9;
 label label =new label(controls, swt.null);
 label.settext("&package:");
 packagetext = new text(controls,swt.border | swt.single);
 griddata gd = new griddata(griddata.fill_horizontal);
 packagetext.setlayoutdata(gd);
 packagetext.addmodifylistener(
  new modifylistener() {
   public void modifytext(modifyevent e) {
    dialogchanged();
   }
  });
 label = new label(controls, swt.null);
 label.settext("blank = default package");
 label = new label(controls, swt.null);
 label.settext("&superclass:");
 superclasstext = new text(controls,swt.border | swt.single);
 gd = new griddata(griddata.fill_horizontal);
 superclasstext.setlayoutdata(gd);
 superclasstext.addmodifylistener(new modifylistener() {
  public void modifytext(modifyevent e) {
   dialogchanged();
  }
 });
 label = new label(controls, swt.null);
 label.settext("blank = object");
 label = new label(controls, swt.null);
 label.settext("&interfaces:");
 interfacestext = new text(controls,swt.border | swt.single);
 gd = new griddata(griddata.fill_horizontal);
 interfacestext.setlayoutdata(gd);
 interfacestext.addmodifylistener(
  new modifylistener() {
   public void modifytext(modifyevent e) {
    dialogchanged();
   }
  });
 label = new label(controls, swt.null);
 label.settext("separated by ','");
 dialogchanged();
 setcontrol(controls);
}
private void dialogchanged() {
 string apackage = getpackage();
 string asuperclass = getsuperclass();
 string interfaces = getinterfaces();
 string status = new packagevalidator().isvalid(apackage);
 if(status != null) {updatestatus(status);
  return;
 }
 status = new superclassvalidator().isvalid(asuperclass);
 if(status != null) {updatestatus(status);
  return;
}
status = new interfacesvalidator().isvalid(interfaces);
if(status != null) {updatestatus(status);
 return;
}
updatestatus(null);
}
}
  驗證類
  驗證可以在插件的用戶輸入數據的任何部分中進行。因此,把驗證代碼放入可重復使用的類中是有意義的,這樣就不用把它復制到多個位置。下面是一個驗證類的例子。
public class interfacesvalidator implements icelleditorvalidator
{
 public string isvalid(object value)
 {
  if( !( value instanceof string) )
   return null;
  string interfaces = ((string)value).trim();
  if( interfaces.equals(""))
   return null;
  string[] interfacearray = interfaces.split(",");
  for (int i = 0; i < interfacearray.length; i++)
  {
   istatus status = javaconventions.validatejavatypename(interfacearray[i]);
   if (status.getcode() != istatus.ok)
    return "validation of interface " + interfacearray[i] + ": " + status.getmessage();
  }
  return null;
 }
}

新聞熱點
疑難解答