OLD | NEW |
(Empty) | |
| 1 /******************************************************************************* |
| 2 * Copyright (c) 2012 xored software, Inc. All rights reserved. This program and
the accompanying |
| 3 * materials are made available under the terms of the Eclipse Public License v1
.0 which accompanies |
| 4 * this distribution, and is available at http://www.eclipse.org/legal/epl-v10.h
tml Contributors: |
| 5 * xored software, Inc. - initial API and implementation (Yuri Strot) |
| 6 ******************************************************************************/ |
| 7 package com.xored.glance.internal.ui.panels; |
| 8 |
| 9 import com.xored.glance.ui.panels.SearchPanel; |
| 10 import com.xored.glance.ui.sources.Match; |
| 11 import com.xored.glance.ui.utils.UIUtils; |
| 12 |
| 13 import org.eclipse.jface.action.ContributionItem; |
| 14 import org.eclipse.jface.action.IContributionItem; |
| 15 import org.eclipse.jface.action.IStatusLineManager; |
| 16 import org.eclipse.jface.action.StatusLineLayoutData; |
| 17 import org.eclipse.jface.action.StatusLineManager; |
| 18 import org.eclipse.swt.SWT; |
| 19 import org.eclipse.swt.SWTException; |
| 20 import org.eclipse.swt.custom.CLabel; |
| 21 import org.eclipse.swt.events.DisposeEvent; |
| 22 import org.eclipse.swt.events.DisposeListener; |
| 23 import org.eclipse.swt.events.FocusEvent; |
| 24 import org.eclipse.swt.events.FocusListener; |
| 25 import org.eclipse.swt.graphics.Image; |
| 26 import org.eclipse.swt.widgets.Composite; |
| 27 import org.eclipse.swt.widgets.Control; |
| 28 import org.eclipse.swt.widgets.Label; |
| 29 import org.eclipse.swt.widgets.Shell; |
| 30 import org.eclipse.ui.IWorkbenchWindow; |
| 31 import org.eclipse.ui.PlatformUI; |
| 32 import org.eclipse.ui.internal.WorkbenchWindow; |
| 33 import org.eclipse.ui.keys.IBindingService; |
| 34 import org.eclipse.ui.texteditor.IStatusField; |
| 35 import org.eclipse.ui.texteditor.IStatusFieldExtension; |
| 36 |
| 37 /** |
| 38 * @author Yuri Strot |
| 39 */ |
| 40 @SuppressWarnings("restriction") |
| 41 public class SearchStatusLine extends SearchPanel { |
| 42 |
| 43 private class SearchItem extends ContributionItem implements IStatusField, ISt
atusFieldExtension { |
| 44 |
| 45 @Override |
| 46 public void dispose() { |
| 47 try { |
| 48 fireClose(); |
| 49 } catch (SWTException ex) { |
| 50 // ignore it -- window closing |
| 51 } |
| 52 } |
| 53 |
| 54 @Override |
| 55 public void fill(Composite parent) { |
| 56 Label separator = new Label(parent, SWT.SEPARATOR); |
| 57 createContent(parent); |
| 58 setLayoutData(separator); |
| 59 } |
| 60 |
| 61 public SearchStatusLine getSearchPanel() { |
| 62 return SearchStatusLine.this; |
| 63 } |
| 64 |
| 65 @Override |
| 66 public void setErrorImage(Image image) { |
| 67 setImage(image); |
| 68 } |
| 69 |
| 70 @Override |
| 71 public void setErrorText(String text) { |
| 72 setText(text); |
| 73 } |
| 74 |
| 75 @Override |
| 76 public void setImage(Image image) { |
| 77 } |
| 78 |
| 79 @Override |
| 80 public void setText(String text) { |
| 81 } |
| 82 |
| 83 @Override |
| 84 public void setToolTipText(String string) { |
| 85 setText(string); |
| 86 } |
| 87 |
| 88 } |
| 89 |
| 90 private static final String DEFAULT_MATCH_LABEL = "no matches"; |
| 91 |
| 92 public static SearchStatusLine getSearchLine(IWorkbenchWindow window) { |
| 93 IStatusLineManager manager = getManager(window); |
| 94 if (manager != null) { |
| 95 IContributionItem[] items = manager.getItems(); |
| 96 for (IContributionItem item : items) { |
| 97 if (item instanceof SearchItem) { |
| 98 return ((SearchItem) item).getSearchPanel(); |
| 99 } |
| 100 } |
| 101 } |
| 102 return new SearchStatusLine(window); |
| 103 } |
| 104 |
| 105 public static IWorkbenchWindow getWindow(Control control) { |
| 106 Shell shell = control.getShell(); |
| 107 IWorkbenchWindow[] windows = PlatformUI.getWorkbench().getWorkbenchWindows()
; |
| 108 for (IWorkbenchWindow window : windows) { |
| 109 if (shell.equals(window.getShell())) { |
| 110 return window; |
| 111 } |
| 112 } |
| 113 return null; |
| 114 } |
| 115 |
| 116 private static IStatusLineManager getManager(IWorkbenchWindow window) { |
| 117 if (window != null) { |
| 118 WorkbenchWindow ww = (WorkbenchWindow) window; |
| 119 return ww.getActionBars().getStatusLineManager(); |
| 120 } |
| 121 return null; |
| 122 } |
| 123 |
| 124 private int matchCount; |
| 125 private String matchText = DEFAULT_MATCH_LABEL; |
| 126 private CLabel matchLabel; |
| 127 private SearchItem item; |
| 128 private final IWorkbenchWindow window; |
| 129 |
| 130 private SearchStatusLine(IWorkbenchWindow window) { |
| 131 this.window = window; |
| 132 init(); |
| 133 } |
| 134 |
| 135 @Override |
| 136 public void allFound(final Match[] matches) { |
| 137 super.allFound(matches); |
| 138 matchCount = matches.length; |
| 139 updateInfo(); |
| 140 } |
| 141 |
| 142 @Override |
| 143 public void closePanel() { |
| 144 if (item != null) { |
| 145 fireClose(); |
| 146 IStatusLineManager manager = getManager(); |
| 147 if (manager != null) { |
| 148 manager.remove(item); |
| 149 manager.update(false); |
| 150 } |
| 151 item = null; |
| 152 } |
| 153 } |
| 154 |
| 155 @Override |
| 156 public void createContent(Composite parent) { |
| 157 super.createContent(parent); |
| 158 StatusLineLayoutData data = new StatusLineLayoutData(); |
| 159 data.widthHint = getPreferedWidth(); |
| 160 data.heightHint = getPreferredHeight(); |
| 161 getControl().setLayoutData(data); |
| 162 createMatchLabel(parent); |
| 163 } |
| 164 |
| 165 public IWorkbenchWindow getWindow() { |
| 166 return window; |
| 167 } |
| 168 |
| 169 @Override |
| 170 public boolean isApplicable(Control control) { |
| 171 return window.equals(getWindow(control)); |
| 172 } |
| 173 |
| 174 @Override |
| 175 protected Control createText(Composite parent, int style) { |
| 176 Control textControl = super.createText(parent, style); |
| 177 textControl.addFocusListener(new FocusListener() { |
| 178 @Override |
| 179 public void focusGained(FocusEvent e) { |
| 180 setKeyFilter(false); |
| 181 } |
| 182 |
| 183 @Override |
| 184 public void focusLost(FocusEvent e) { |
| 185 setKeyFilter(true); |
| 186 } |
| 187 }); |
| 188 textControl.addDisposeListener(new DisposeListener() { |
| 189 @Override |
| 190 public void widgetDisposed(DisposeEvent e) { |
| 191 setKeyFilter(true); |
| 192 } |
| 193 }); |
| 194 return textControl; |
| 195 } |
| 196 |
| 197 protected void setKeyFilter(boolean enabled) { |
| 198 IBindingService service = (IBindingService) PlatformUI.getWorkbench().getSer
vice( |
| 199 IBindingService.class); |
| 200 if (service != null) { |
| 201 service.setKeyFilterEnabled(enabled); |
| 202 } |
| 203 } |
| 204 |
| 205 @Override |
| 206 protected void textEmpty() { |
| 207 super.textEmpty(); |
| 208 matchText = DEFAULT_MATCH_LABEL; |
| 209 matchLabel.setText(matchText); |
| 210 } |
| 211 |
| 212 private void createMatchLabel(Composite parent) { |
| 213 Label separator = new Label(parent, SWT.SEPARATOR); |
| 214 setLayoutData(separator); |
| 215 matchLabel = new CLabel(parent, SWT.SHADOW_NONE); |
| 216 StatusLineLayoutData data = new StatusLineLayoutData(); |
| 217 data.widthHint = getTextWidth(parent, 10) + 15; |
| 218 data.heightHint = getPreferredHeight(); |
| 219 matchLabel.setLayoutData(data); |
| 220 matchLabel.setText(matchText); |
| 221 } |
| 222 |
| 223 private IStatusLineManager getManager() { |
| 224 return getManager(window); |
| 225 } |
| 226 |
| 227 private void init() { |
| 228 item = new SearchItem(); |
| 229 IStatusLineManager manager = getManager(); |
| 230 if (manager != null) { |
| 231 manager.remove(item); |
| 232 manager.appendToGroup(StatusLineManager.BEGIN_GROUP, item); |
| 233 manager.update(true); |
| 234 } |
| 235 } |
| 236 |
| 237 private void setLayoutData(Label separator) { |
| 238 StatusLineLayoutData data = new StatusLineLayoutData(); |
| 239 data.heightHint = getPreferredHeight(); |
| 240 separator.setLayoutData(data); |
| 241 } |
| 242 |
| 243 private void updateInfo() { |
| 244 StringBuffer buffer = new StringBuffer(); |
| 245 |
| 246 if (matchCount == 0) { |
| 247 buffer.append(DEFAULT_MATCH_LABEL); |
| 248 } else { |
| 249 buffer.append(matchCount); |
| 250 } |
| 251 |
| 252 matchText = buffer.toString(); |
| 253 UIUtils.asyncExec(matchLabel, new Runnable() { |
| 254 |
| 255 @Override |
| 256 public void run() { |
| 257 matchLabel.setText(matchText); |
| 258 } |
| 259 }); |
| 260 } |
| 261 |
| 262 } |
OLD | NEW |