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 閱讀(2545) 評論(1)  編輯  收藏 所屬分類: SWT和插件開發

          評論

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

          謝謝你,寫的非常好 .

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

          主站蜘蛛池模板: 永吉县| 林芝县| 淄博市| 讷河市| 桃江县| 湘潭县| 五寨县| 新化县| 红桥区| 台东县| 寿阳县| 海阳市| 阳谷县| 封丘县| 宁海县| 绍兴市| 读书| 赤峰市| 高邮市| 特克斯县| 太保市| 兴安县| 乌拉特中旗| 丰原市| 西峡县| 安国市| 庆阳市| 嘉祥县| 辽阳县| 南充市| 穆棱市| 永平县| 吉安县| 江津市| 满洲里市| 宁阳县| 东阿县| 临朐县| 高安市| 海宁市| 乳山市|