|
1 /******************************************************************************* |
|
2 * Copyright (c) 2004, 2009 Tasktop Technologies and others. |
|
3 * All rights reserved. This program and the accompanying materials |
|
4 * are made available under the terms of the Eclipse Public License v1.0 |
|
5 * which accompanies this distribution, and is available at |
|
6 * http://www.eclipse.org/legal/epl-v10.html |
|
7 * |
|
8 * Contributors: |
|
9 * Tasktop Technologies - initial API and implementation |
|
10 *******************************************************************************/ |
|
11 |
|
12 package com.nokia.carbide.remoteconnections.internal.ui.mylyn; |
|
13 |
|
14 import java.util.Set; |
|
15 |
|
16 import org.eclipse.core.runtime.IProgressMonitor; |
|
17 import org.eclipse.core.runtime.IStatus; |
|
18 import org.eclipse.core.runtime.Status; |
|
19 import org.eclipse.core.runtime.jobs.Job; |
|
20 import org.eclipse.swt.widgets.Display; |
|
21 import org.eclipse.swt.widgets.Shell; |
|
22 import org.eclipse.swt.widgets.TreeItem; |
|
23 |
|
24 /** |
|
25 * @author Mik Kersten |
|
26 * @author Steffen Pingel |
|
27 */ |
|
28 public class SwtUtil { |
|
29 |
|
30 public static final long FADE_RESCHEDULE_DELAY = 80; |
|
31 |
|
32 public static final int FADE_IN_INCREMENT = 15; |
|
33 |
|
34 public static final int FADE_OUT_INCREMENT = -20; |
|
35 |
|
36 public static void collectItemData(TreeItem[] items, Set<Object> allVisible) { |
|
37 for (TreeItem item : items) { |
|
38 allVisible.add(item.getData()); |
|
39 collectItemData(item.getItems(), allVisible); |
|
40 } |
|
41 } |
|
42 |
|
43 public static FadeJob fastFadeIn(Shell shell, IFadeListener listener) { |
|
44 return new FadeJob(shell, 2 * FADE_IN_INCREMENT, FADE_RESCHEDULE_DELAY, listener); |
|
45 } |
|
46 |
|
47 public static FadeJob fadeIn(Shell shell, IFadeListener listener) { |
|
48 return new FadeJob(shell, FADE_IN_INCREMENT, FADE_RESCHEDULE_DELAY, listener); |
|
49 } |
|
50 |
|
51 public static FadeJob fadeOut(Shell shell, IFadeListener listener) { |
|
52 return new FadeJob(shell, FADE_OUT_INCREMENT, FADE_RESCHEDULE_DELAY, listener); |
|
53 } |
|
54 |
|
55 public static class FadeJob extends Job { |
|
56 |
|
57 private final Shell shell; |
|
58 |
|
59 private final int increment; |
|
60 |
|
61 private volatile boolean stopped; |
|
62 |
|
63 private volatile int currentAlpha; |
|
64 |
|
65 private final long delay; |
|
66 |
|
67 private final IFadeListener fadeListener; |
|
68 |
|
69 public FadeJob(Shell shell, int increment, long delay, IFadeListener fadeListener) { |
|
70 super("Fading"); |
|
71 if (increment < -255 || increment == 0 || increment > 255) { |
|
72 throw new IllegalArgumentException("-255 <= increment <= 255 && increment != 0"); //$NON-NLS-1$ |
|
73 } |
|
74 if (delay < 1) { |
|
75 throw new IllegalArgumentException("delay must be > 0"); //$NON-NLS-1$ |
|
76 } |
|
77 this.currentAlpha = shell.getAlpha(); |
|
78 this.shell = shell; |
|
79 this.increment = increment; |
|
80 this.delay = delay; |
|
81 this.fadeListener = fadeListener; |
|
82 |
|
83 setSystem(true); |
|
84 schedule(delay); |
|
85 } |
|
86 |
|
87 @Override |
|
88 protected void canceling() { |
|
89 stopped = true; |
|
90 } |
|
91 |
|
92 private void reschedule() { |
|
93 if (stopped) { |
|
94 return; |
|
95 } |
|
96 schedule(delay); |
|
97 } |
|
98 |
|
99 public void cancelAndWait(final boolean setAlpha) { |
|
100 if (stopped) { |
|
101 return; |
|
102 } |
|
103 cancel(); |
|
104 Display.getDefault().syncExec(new Runnable() { |
|
105 public void run() { |
|
106 if (setAlpha) { |
|
107 shell.setAlpha(getLastAlpha()); |
|
108 } |
|
109 } |
|
110 }); |
|
111 } |
|
112 |
|
113 @Override |
|
114 protected IStatus run(IProgressMonitor monitor) { |
|
115 if (stopped) { |
|
116 return Status.OK_STATUS; |
|
117 } |
|
118 |
|
119 currentAlpha += increment; |
|
120 if (currentAlpha <= 0) { |
|
121 currentAlpha = 0; |
|
122 } else if (currentAlpha >= 255) { |
|
123 currentAlpha = 255; |
|
124 } |
|
125 |
|
126 Display.getDefault().syncExec(new Runnable() { |
|
127 public void run() { |
|
128 if (stopped) { |
|
129 return; |
|
130 } |
|
131 |
|
132 if (shell.isDisposed()) { |
|
133 stopped = true; |
|
134 return; |
|
135 } |
|
136 |
|
137 shell.setAlpha(currentAlpha); |
|
138 |
|
139 if (fadeListener != null) { |
|
140 fadeListener.faded(shell, currentAlpha); |
|
141 } |
|
142 } |
|
143 }); |
|
144 |
|
145 if (currentAlpha == 0 || currentAlpha == 255) { |
|
146 stopped = true; |
|
147 } |
|
148 |
|
149 reschedule(); |
|
150 return Status.OK_STATUS; |
|
151 } |
|
152 |
|
153 private int getLastAlpha() { |
|
154 return (increment < 0) ? 0 : 255; |
|
155 } |
|
156 |
|
157 } |
|
158 |
|
159 public static interface IFadeListener { |
|
160 |
|
161 public void faded(Shell shell, int alpha); |
|
162 |
|
163 } |
|
164 |
|
165 } |