West Farm
          吾本布衣,生于川北,躬耕于代碼的田地上。
          posts - 16,  comments - 15,  trackbacks - 0
          Important:該類有諸多不盡人意的地方,讀者如果要用,請自行完善,但是至少值得一試。
          原代碼是一個老外寫的在桌面右下角彈出消息提示的一個類,我進行了改寫。
          任何改進,希望你能不吝分享。



          import org.eclipse.swt.SWT;
          import org.eclipse.swt.graphics.Color;
          import org.eclipse.swt.graphics.Font;
          import org.eclipse.swt.graphics.FontData;
          import org.eclipse.swt.graphics.GC;
          import org.eclipse.swt.graphics.Image;
          import org.eclipse.swt.graphics.Point;
          import org.eclipse.swt.graphics.Rectangle;
          import org.eclipse.swt.layout.FillLayout;
          import org.eclipse.swt.widgets.Composite;
          import org.eclipse.swt.widgets.Display;
          import org.eclipse.swt.widgets.Event;
          import org.eclipse.swt.widgets.Label;
          import org.eclipse.swt.widgets.Listener;
          import org.eclipse.swt.widgets.Scrollable;
          import org.eclipse.swt.widgets.Shell;
          import org.eclipse.wb.swt.SWTResourceManager;

          /**
           * 具有淡入淡出效果且不需要用戶點擊關閉的消息提示框。
           * 
          @author ggfan@amarsoft
           *
           
          */
          public class Notifier {
              
          private static final int DISPLAY_TIME = 2000;
              
              
          private static final int FADE_TIMER = 50;
              
              
          private static final int FADE_IN_STEP = 30;
              
              
          private static final int FADE_OUT_STEP = 8;

              
          private static final int FINAL_ALPHA = 225;

              
          public static int DEFAULT_WIDTH = 150;
              
              
          public static  int DEFAULT_HEIGHT = 60;

              
          private static Color _titleFgColor = SWTResourceManager.getColor(407397);

              
          private static Color _fgColor = _titleFgColor;

              
          private static Color _bgFgGradient = SWTResourceManager.getColor(226239249);

              
          private static Color _bgBgGradient = SWTResourceManager.getColor(177211243);

              
          private static Color _borderColor = SWTResourceManager.getColor(407397);

              
          private static Image _oldImage;
              
          // TODO Scrollable可能不合適
              public static void notify(Scrollable scrollable, final String msg) {

                  
          final Shell parentShell = scrollable.getShell();
                  
          final Shell newShell = new Shell(parentShell, SWT.NO_FOCUS | SWT.NO_TRIM);
                  newShell.setLayout(
          new FillLayout());
                  newShell.setForeground(_fgColor);
                  newShell.setBackground(_bgBgGradient);
                  newShell.setBackgroundMode(SWT.INHERIT_FORCE);
                  scrollable.addDisposeListener(new DisposeListener(){
              public void widgetDisposed(DisposeEvent e) {
          newShell.dispose();
              }
          });
                  
          final Composite inner = new Composite(newShell, SWT.NONE);
                  FillLayout layout 
          = new FillLayout();
                  layout.marginWidth 
          = 20;
                  layout.marginHeight 
          = 20;
                  inner.setLayout(layout);

                  newShell.addListener(SWT.Resize, 
          new Listener() {
                      
          public void handleEvent(Event event) {
                          
          try {
                              Rectangle rect 
          = newShell.getClientArea();
                              Image newImage 
          = new Image(Display.getDefault(), Math.max(1, rect.width), rect.height);
                              GC gc 
          = new GC(newImage);
                              
          // 背景
                              gc.setForeground(_bgFgGradient);
                              gc.setBackground(_bgBgGradient);
                              gc.fillGradientRectangle(rect.x, rect.y, rect.width, rect.height, 
          true);
                              
          // 邊框
                              gc.setLineWidth(2);
                              gc.setForeground(_borderColor);
                              gc.drawRectangle(rect.x 
          + 1, rect.y + 1, rect.width - 2, rect.height - 2);

                              gc.dispose();
                              newShell.setBackgroundImage(newImage);
                              
          if (_oldImage != null) {
                                  _oldImage.dispose();
                              }
                              _oldImage 
          = newImage;
                          } 
          catch (Exception err) {
                              err.printStackTrace();
                          }
                      }
                  });

                  Label text 
          = new Label(inner, SWT.WRAP | SWT.CENTER);
                  Font tf 
          = text.getFont();
                  FontData tfd 
          = tf.getFontData()[0];
                  tfd.setStyle(SWT.BOLD);
                  tfd.height 
          = 8;
                  text.setFont(SWTResourceManager.getFont(tfd.getName(), 
          8, SWT.NORMAL));
                  text.setForeground(_fgColor);
                  text.setText(msg);

                  newShell.setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);

                  
          if (Display.getDefault().getActiveShell() == null || Display.getDefault().getActiveShell().getMonitor() == null) { 
                      
          return
                  }
                  newShell.setLocation(computePoint(scrollable));
                  newShell.setAlpha(
          0);
                  newShell.setVisible(
          true);

                  fadeIn(newShell);
              }
              
              
          // TODO 當有滾動條出現的時候是否能夠居中?
              private static Point computePoint(Scrollable scrollable) {
                  Point p 
          = scrollable.toDisplay(scrollable.getClientArea().x, scrollable.getClientArea().y);
                  
          int w = scrollable.getClientArea().width;
                  
          int h = scrollable.getClientArea().height;
                  p.x 
          += w / 2 - DEFAULT_WIDTH / 2 ;
                  p.y 
          += h / 2 - DEFAULT_HEIGHT / 2
                  
          return p;
              }

              
          private static void fadeIn(final Shell _shell) {
                  Runnable run 
          = new Runnable() {
                      @Override
                      
          public void run() {
                          
          try {
                              
          if (_shell == null || _shell.isDisposed()) {
                                  
          return;
                              }

                              
          int cur = _shell.getAlpha();
                              cur 
          += FADE_IN_STEP;

                              
          if (cur > FINAL_ALPHA) {
                                  _shell.setAlpha(FINAL_ALPHA);
                                  startTimer(_shell);
                                  
          return;
                              }

                              _shell.setAlpha(cur);
                              Display.getDefault().timerExec(FADE_TIMER, 
          this);
                          } 
          catch (Exception err) {
                              err.printStackTrace();
                          }
                      }
                  };
                  Display.getDefault().timerExec(FADE_TIMER, run);
              }

              
          private static void startTimer(final Shell _shell) {
                  Runnable run 
          = new Runnable() {

                      @Override
                      
          public void run() {
                          
          try {
                              
          if (_shell == null || _shell.isDisposed()) {
                                  
          return;
                              }

                              fadeOut(_shell);
                          } 
          catch (Exception err) {
                              err.printStackTrace();
                          }
                      }

                  };
                  Display.getDefault().timerExec(DISPLAY_TIME, run);

              }

              
          private static void fadeOut(final Shell _shell) {
                  
          final Runnable run = new Runnable() {

                      @Override
                      
          public void run() {
                          
          try {
                              
          if (_shell == null || _shell.isDisposed()) {
                                  
          return;
                              }

                              
          int cur = _shell.getAlpha();
                              cur 
          -= FADE_OUT_STEP;

                              
          if (cur <= 0) {
                                  _shell.setAlpha(
          0);
                                   
          if (_oldImage != null) {
                                       _oldImage.dispose();
                                   }
                                  _shell.dispose();
                                  
          return;
                              }

                              _shell.setAlpha(cur);

                              Display.getDefault().timerExec(FADE_TIMER, 
          this);

                          } 
          catch (Exception err) {
                              err.printStackTrace();
                          }
                      }

                  };
                  Display.getDefault().timerExec(FADE_TIMER, run);

              }
          }
          posted on 2011-10-12 10:48 West Farmer 閱讀(3271) 評論(6)  編輯  收藏 所屬分類: Eclipse-RCP

          FeedBack:
          # re: SWT:淡入淡出消息提示框,無需用戶干預。
          2011-10-12 18:04 | [西部農民]
          bug fix:
          scrollable.addDisposeListener(new DisposeListener(){
          public void widgetDisposed(DisposeEvent e) {
          newShell.dispose();
          }
          });  回復  更多評論
            
          # re: SWT:淡入淡出消息提示框,無需用戶干預。[未登錄]
          2013-02-27 16:12 | Sam
          當多個消息提示框同時顯示,會出現整個程序無響應的問題,我是在Eclipse RCP里用的,不知道的什么原因  回復  更多評論
            
          # re: SWT:淡入淡出消息提示框,無需用戶干預。
          2013-03-01 14:58 | @shrinkAshirt
          bug fixed:
          scrollable.addDisposeListener(new DisposeListener() {

          @Override
          public void widgetDisposed(DisposeEvent arg0) {
          // TODO Auto-generated method stub
          newShell.dispose();
          }
          });  回復  更多評論
            
          # re: SWT:淡入淡出消息提示框,無需用戶干預。
          2013-03-01 15:29 | @shrinkAshirt
          傳入scrollable干什么
            回復  更多評論
            
          # re: SWT:淡入淡出消息提示框,無需用戶干預。
          2013-03-14 00:13 | [西部農民]
          多個彈出框同時出現應該是不支持的,就像QQ的消息通知。是要一個一個顯示的,這應該和UI應用的線程模式有關。

          @Sam
            回復  更多評論
            
          # re: SWT:淡入淡出消息提示框,無需用戶干預。
          2015-10-29 14:59 | 李堯
          博主,怎么調用啊,寫全代碼吧,沒法用  回復  更多評論
            

          <2011年10月>
          2526272829301
          2345678
          9101112131415
          16171819202122
          23242526272829
          303112345

          常用鏈接

          留言簿

          隨筆分類

          隨筆檔案

          相冊

          搜索

          •  

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 甘南县| 南康市| 徐闻县| 芦山县| 敦化市| 博兴县| 苍山县| 新邵县| 桂东县| 五莲县| 江山市| 高陵县| 汶川县| 阜南县| 萨嘎县| 雷州市| 治多县| 上栗县| 三亚市| 同仁县| 镶黄旗| 五莲县| 昭觉县| 新闻| 凤城市| 荆州市| 湄潭县| 融水| 泰兴市| 响水县| 祁阳县| 东明县| 茌平县| 如皋市| 霸州市| 乌什县| 开平市| 甘孜| 田林县| 明水县| 时尚|