DANCE WITH JAVA

          開發出高質量的系統

          常用鏈接

          統計

          積分與排名

          好友之家

          最新評論

          給自定義Dialog加入保留對話框值的功能

          有的時候我們需要這樣的功能,記下Dialog中的值,當再次打開這個Dialog的時候,還原這些值。這就需要把這些Dialog的值保存起來。Dialog的IDialogSettings類提供了這個功能。下邊是使用方法:
          在上例"自定義Dialog"的代碼上加入三個函數:
          public void saveState(){
                  
          if (text.getText() == null || text.getText().equals("")){
                      
          return ;
                  }

                  IDialogSettings topSettings 
          = getTopSettings();
                  IDialogSettings settings 
          =  topSettings.getSection("TestDialog");
                  
          if(settings == null)settings = topSettings.addNewSection("TestDialog");
                  settings.put(
          "value", text.getText());
                  
          try{
                      topSettings.save(
          "content/system.xml");
                  }
          catch(IOException e){
                      System.out.println(e.getMessage());
                  }

              }

              
          public void restoreState(){
                  IDialogSettings topSettings 
          = getTopSettings();
                  IDialogSettings settings 
          =  topSettings.getSection("TestDialog");
                  
          if(settings == nullreturn;
                  
          if (text.getText() == null || text.getText().equals("")){
                      text.setText(settings.get(
          "value"));
                  }

              }

              
          public IDialogSettings getTopSettings(){
                  IDialogSettings topSettings 
          = new DialogSettings("system");
                  
          try{
                      topSettings.load(
          "content/system.xml");
                  }
          catch(IOException e){
                      System.out.println(e.getMessage());
                  }

                  
          return topSettings;
              }

          然后在覆寫的buttonPressed函數中調用save
          protected void buttonPressed(int button){
                  saveState();
              }
          在createDialogArea函數中加入取出數據
          protected Control createDialogArea(Composite parent) {
                  Composite container 
          = (Composite) super.createDialogArea(parent);
                  container.setLayout(
          new RowLayout());
                  text 
          = new Text(container, SWT.BORDER);
                  text.setLayoutData(
          new RowData(100,-1));
                  
          //加入這一句
                  if (text.getText() == null || text.getText().equals("")){
                      restoreState();
                  }

                  
          return container;
              }
          最后,記得建立需要的文件,在當前workspace下建立文件夾content,然后在文件夾下建立system.xml文件。當然你也可以利用程序來實現。
          運行一下看看吧,是不是記住了上次填入的內容.
          source下載:http://www.aygfsteel.com/Files/dreamstone/jface-record.rar

          posted on 2007-08-09 18:13 dreamstone 閱讀(2546) 評論(1)  編輯  收藏 所屬分類: SWT和插件開發

          評論

          # re: 給自定義Dialog加入保留對話框值的功能 2007-10-25 06:34 QuandeLiu

          謝謝你,寫的非常好 .

          簡單易懂,又很實用  回復  更多評論   

          主站蜘蛛池模板: 平舆县| 临漳县| 汨罗市| 炎陵县| 银川市| 桐城市| 长顺县| 澄城县| 资源县| 石泉县| 兴和县| 栾川县| 山阳县| 永平县| 威信县| 衡山县| 河南省| 无锡市| 新巴尔虎左旗| 天峻县| 贵南县| 民丰县| 古蔺县| 甘孜县| 沅江市| 临安市| 宾川县| 寻乌县| 黑龙江省| 黄大仙区| 西盟| 桂东县| 江华| 高要市| 军事| 德化县| 永顺县| 正宁县| 深泽县| 南靖县| 五河县|