|
1 package org.symbian.tools.tmw.previewer.preview; |
|
2 |
|
3 import java.net.URI; |
|
4 import java.util.Collection; |
|
5 |
|
6 import org.eclipse.core.resources.IFile; |
|
7 import org.eclipse.core.resources.IProject; |
|
8 import org.eclipse.core.resources.ResourcesPlugin; |
|
9 import org.eclipse.core.runtime.IPath; |
|
10 import org.eclipse.jface.action.Action; |
|
11 import org.eclipse.jface.action.IAction; |
|
12 import org.eclipse.jface.action.IToolBarManager; |
|
13 import org.eclipse.jface.viewers.ISelection; |
|
14 import org.eclipse.jface.viewers.ISelectionChangedListener; |
|
15 import org.eclipse.jface.viewers.ISelectionProvider; |
|
16 import org.eclipse.jface.viewers.StructuredSelection; |
|
17 import org.eclipse.swt.browser.Browser; |
|
18 import org.eclipse.swt.layout.GridData; |
|
19 import org.eclipse.swt.widgets.Composite; |
|
20 import org.eclipse.swt.widgets.Control; |
|
21 import org.eclipse.ui.actions.ActionFactory; |
|
22 import org.eclipse.ui.part.IPageSite; |
|
23 import org.eclipse.ui.part.Page; |
|
24 import org.symbian.tools.tmw.core.utilities.CoreUtil; |
|
25 import org.symbian.tools.tmw.previewer.Images; |
|
26 import org.symbian.tools.tmw.previewer.PreviewerPlugin; |
|
27 |
|
28 public abstract class AbstractPreviewPage extends Page implements IPreviewPage, |
|
29 ISelectionProvider { |
|
30 private final IAction refreshAction = new Action("Refresh") { |
|
31 public void run() { |
|
32 refresh(true); |
|
33 }; |
|
34 }; |
|
35 private final IAction toggleRefresh = new Action("Toggle Refresh", |
|
36 IAction.AS_RADIO_BUTTON) { |
|
37 public void run() { |
|
38 toggleRefresh(); |
|
39 }; |
|
40 }; |
|
41 |
|
42 protected IProject project; |
|
43 private Browser browser; |
|
44 private boolean toggleState = true; |
|
45 private final PreviewView previewView; |
|
46 private boolean needsRefresh = false; |
|
47 |
|
48 public AbstractPreviewPage(IProject project, PreviewView previewView) { |
|
49 this.project = project; |
|
50 this.previewView = previewView; |
|
51 } |
|
52 |
|
53 protected void toggleRefresh() { |
|
54 toggleState = !toggleState; |
|
55 toggleRefresh.setChecked(toggleState); |
|
56 previewView.setProjectAutorefresh(project, toggleState); |
|
57 toggleRefresh.setToolTipText(getToggleActionTooltip()); |
|
58 if (toggleState && needsRefresh) { |
|
59 refresh(true); |
|
60 } |
|
61 } |
|
62 |
|
63 @Override |
|
64 public void createControl(Composite parent) { |
|
65 browser = createBrowser(parent); |
|
66 browser.setLayoutData(new GridData(GridData.FILL_BOTH)); |
|
67 browser.setUrl(getURI().toASCIIString()); |
|
68 } |
|
69 |
|
70 protected abstract Browser createBrowser(Composite parent); |
|
71 |
|
72 private URI getURI() { |
|
73 return PreviewerPlugin.getDefault().getHttpPreviewer().previewProject( |
|
74 project, null); |
|
75 } |
|
76 |
|
77 @Override |
|
78 public Control getControl() { |
|
79 return browser; |
|
80 } |
|
81 |
|
82 @Override |
|
83 public void setFocus() { |
|
84 if (browser != null && !browser.isDisposed()) { |
|
85 browser.setFocus(); |
|
86 } |
|
87 } |
|
88 |
|
89 private boolean refreshScheduled = false; |
|
90 |
|
91 public synchronized void process(Collection<IFile> files) { |
|
92 if (!isDisposed() && !refreshScheduled && needsRefresh(files)) { |
|
93 asyncExec(new Runnable() { |
|
94 public void run() { |
|
95 refreshBrowser(); |
|
96 } |
|
97 }); |
|
98 refreshScheduled = true; |
|
99 } |
|
100 } |
|
101 |
|
102 protected void promptIfNeeded() { |
|
103 if (toggleState) { |
|
104 toggleState = previewView.promptUserToToggle(project, toggleState); |
|
105 toggleRefresh.setChecked(toggleState); |
|
106 } |
|
107 } |
|
108 |
|
109 private void asyncExec(Runnable runnable) { |
|
110 getControl().getDisplay().asyncExec(runnable); |
|
111 } |
|
112 |
|
113 private boolean needsRefresh(Collection<IFile> files) { |
|
114 for (IFile iFile : files) { |
|
115 if (iFile.getProject().equals(project)) { |
|
116 return true; |
|
117 } |
|
118 } |
|
119 return false; |
|
120 } |
|
121 |
|
122 protected synchronized void refresh(final boolean manual) { |
|
123 try { |
|
124 if (!isDisposed()) { |
|
125 final Control focusControl = browser.getDisplay() |
|
126 .getFocusControl(); |
|
127 if (manual && CoreUtil.isMac()) { |
|
128 browser.getParent().forceFocus(); |
|
129 } |
|
130 browser.setUrl(getURI().toASCIIString()); |
|
131 refreshAction.setImageDescriptor(PreviewerPlugin |
|
132 .getImageDescriptor(Images.GREEN_SYNC)); |
|
133 asyncExec(new Runnable() { |
|
134 public void run() { |
|
135 if (!manual && focusControl != null) { |
|
136 focusControl.setFocus(); |
|
137 } else if (manual) { |
|
138 browser.forceFocus(); |
|
139 } |
|
140 } |
|
141 }); |
|
142 refreshAction.setToolTipText("Refresh the preview browser"); |
|
143 } |
|
144 needsRefresh = false; |
|
145 } finally { |
|
146 refreshScheduled = false; |
|
147 } |
|
148 } |
|
149 |
|
150 @Override |
|
151 public void init(IPageSite pageSite) { |
|
152 super.init(pageSite); |
|
153 IToolBarManager toolBar = pageSite.getActionBars().getToolBarManager(); |
|
154 |
|
155 contributeToToolbar(toolBar); |
|
156 |
|
157 refreshAction.setImageDescriptor(PreviewerPlugin |
|
158 .getImageDescriptor(Images.GREEN_SYNC)); |
|
159 refreshAction.setToolTipText("Refresh the preview browser"); |
|
160 toolBar.add(refreshAction); |
|
161 |
|
162 toggleState = previewView.getProjectAutorefresh(project); |
|
163 |
|
164 toggleRefresh.setImageDescriptor(PreviewerPlugin |
|
165 .getImageDescriptor(Images.YELLOW_SYNC)); |
|
166 toggleRefresh.setToolTipText(getToggleActionTooltip()); |
|
167 toggleRefresh.setChecked(toggleState); |
|
168 toolBar.add(toggleRefresh); |
|
169 |
|
170 pageSite.getActionBars().setGlobalActionHandler( |
|
171 ActionFactory.REFRESH.getId(), refreshAction); |
|
172 getSite().setSelectionProvider(this); |
|
173 } |
|
174 |
|
175 protected void contributeToToolbar(IToolBarManager toolBar) { |
|
176 // Do nothing here |
|
177 } |
|
178 |
|
179 private String getToggleActionTooltip() { |
|
180 return toggleState ? "Disable preview autorefresh" |
|
181 : "Enable preview autorefresh"; |
|
182 } |
|
183 |
|
184 public void addSelectionChangedListener(ISelectionChangedListener listener) { |
|
185 // Do nothing |
|
186 } |
|
187 |
|
188 public ISelection getSelection() { |
|
189 return new StructuredSelection(project); |
|
190 } |
|
191 |
|
192 public void removeSelectionChangedListener( |
|
193 ISelectionChangedListener listener) { |
|
194 // Do nothing |
|
195 } |
|
196 |
|
197 public void setSelection(ISelection selection) { |
|
198 // Do nothing |
|
199 } |
|
200 |
|
201 public boolean isDisposed() { |
|
202 return browser != null && browser.isDisposed(); |
|
203 } |
|
204 |
|
205 private synchronized void refreshBrowser() { |
|
206 if (toggleState) { |
|
207 promptIfNeeded(); |
|
208 } |
|
209 if (toggleState) { |
|
210 refresh(false); |
|
211 } else { |
|
212 needsRefresh = true; |
|
213 refreshAction.setImageDescriptor(PreviewerPlugin |
|
214 .getImageDescriptor(Images.RED_SYNC)); |
|
215 refreshAction |
|
216 .setToolTipText("Refresh the preview browser (there are updated files)"); |
|
217 } |
|
218 } |
|
219 |
|
220 public IProject getProject() { |
|
221 return project; |
|
222 } |
|
223 |
|
224 public synchronized void projectRenamed(IPath newPath) { |
|
225 if (!isDisposed()) { |
|
226 project = ResourcesPlugin.getWorkspace().getRoot().getProject( |
|
227 newPath.lastSegment()); |
|
228 } |
|
229 } |
|
230 } |