1 11 package org.eclipse.debug.internal.ui.views.expression; 12 13 import java.util.Iterator ; 14 import java.util.Map ; 15 16 import org.eclipse.debug.core.DebugException; 17 import org.eclipse.debug.core.DebugPlugin; 18 import org.eclipse.debug.core.model.IExpression; 19 import org.eclipse.debug.core.model.IValue; 20 import org.eclipse.debug.core.model.IVariable; 21 import org.eclipse.debug.internal.ui.DebugUIPlugin; 22 import org.eclipse.debug.internal.ui.IInternalDebugUIConstants; 23 import org.eclipse.debug.internal.ui.VariablesViewModelPresentation; 24 import org.eclipse.debug.internal.ui.views.DebugUIViewsMessages; 25 import org.eclipse.debug.internal.ui.views.variables.IndexedVariablePartition; 26 import org.eclipse.debug.internal.ui.views.variables.VariablesView; 27 import org.eclipse.debug.internal.ui.views.variables.VariablesViewer; 28 import org.eclipse.debug.ui.IDebugModelPresentation; 29 import org.eclipse.debug.ui.IDebugUIConstants; 30 import org.eclipse.debug.ui.IValueDetailListener; 31 import org.eclipse.jface.dialogs.IDialogSettings; 32 import org.eclipse.jface.viewers.StructuredViewer; 33 import org.eclipse.jface.viewers.ViewerFilter; 34 import org.eclipse.swt.SWT; 35 import org.eclipse.swt.custom.SashForm; 36 import org.eclipse.swt.custom.StyledText; 37 import org.eclipse.swt.events.SelectionEvent; 38 import org.eclipse.swt.events.SelectionListener; 39 import org.eclipse.swt.graphics.Color; 40 import org.eclipse.swt.graphics.GC; 41 import org.eclipse.swt.graphics.Image; 42 import org.eclipse.swt.graphics.Point; 43 import org.eclipse.swt.layout.GridData; 44 import org.eclipse.swt.layout.GridLayout; 45 import org.eclipse.swt.widgets.Composite; 46 import org.eclipse.swt.widgets.Control; 47 import org.eclipse.swt.widgets.Display; 48 import org.eclipse.swt.widgets.Tree; 49 import org.eclipse.swt.widgets.TreeItem; 50 import org.eclipse.ui.IViewPart; 51 import org.eclipse.ui.IWorkbenchPage; 52 import org.eclipse.ui.IWorkbenchPartSite; 53 import org.eclipse.ui.PartInitException; 54 55 63 public class ExpressionInformationControl extends PopupInformationControl { 64 private static final int[] DEFAULT_SASH_WEIGHTS = new int[] {90, 10}; 65 private static final String SASH_KEY = "SASH_WEIGHT"; 67 private IWorkbenchPage page; 68 private IExpression exp; 69 private VariablesViewer viewer; 70 private IDebugModelPresentation modelPresentation; 71 private StyledText valueDisplay; 72 private SashForm sashForm; 73 private Tree tree; 74 75 84 public ExpressionInformationControl(IWorkbenchPage page, IExpression exp, String commandId) { 85 super(page.getWorkbenchWindow().getShell(), DebugUIViewsMessages.ExpressionInformationControl_5, commandId); this.page = page; 87 this.exp = exp; 88 } 89 90 93 public void setInformation(String information) { 94 VariablesView view = getViewToEmulate(); 95 viewer.getContentProvider(); 96 if (view != null) { 97 StructuredViewer structuredViewer = (StructuredViewer) view.getViewer(); 98 if (structuredViewer != null) { 99 ViewerFilter[] filters = structuredViewer.getFilters(); 100 for (int i = 0; i < filters.length; i++) { 101 viewer.addFilter(filters[i]); 102 } 103 } 104 ((RemoteExpressionsContentProvider)viewer.getContentProvider()).setShowLogicalStructure(view.isShowLogicalStructure()); 105 Map map = view.getPresentationAttributes(exp.getModelIdentifier()); 106 Iterator iterator = map.keySet().iterator(); 107 while (iterator.hasNext()) { 108 String key = (String )iterator.next(); 109 modelPresentation.setAttribute(key, map.get(key)); 110 } 111 } 112 viewer.setInput(new Object []{exp}); 113 viewer.expandToLevel(2); 114 } 115 116 private VariablesView getViewToEmulate() { 117 VariablesView expressionsView = (VariablesView)page.findView(IDebugUIConstants.ID_EXPRESSION_VIEW); 118 if (expressionsView != null && expressionsView.isVisible()) { 119 return expressionsView; 120 } 121 VariablesView variablesView = (VariablesView)page.findView(IDebugUIConstants.ID_VARIABLE_VIEW); 122 if (variablesView != null && variablesView.isVisible()) { 123 return variablesView; 124 } 125 if (expressionsView != null) { 126 return expressionsView; 127 } 128 return variablesView; 129 } 130 131 134 protected int[] getInitialSashWeights() { 135 IDialogSettings settings = getDialogSettings(); 136 int[] sashes = new int[2]; 137 try { 138 sashes[0] = settings.getInt(SASH_KEY+"_ONE"); sashes[1] = settings.getInt(SASH_KEY+"_TWO"); return sashes; 141 } catch (NumberFormatException nfe) { 142 } 143 144 return DEFAULT_SASH_WEIGHTS; 145 } 146 147 private void updateValueDisplay(IValue val) { 148 IValueDetailListener valueDetailListener = new IValueDetailListener() { 149 public void detailComputed(IValue value, final String result) { 150 Display.getDefault().asyncExec(new Runnable () { 151 public void run() { 152 if (!valueDisplay.isDisposed()) { 153 String text = result; 154 int max = DebugUIPlugin.getDefault().getPreferenceStore().getInt(IInternalDebugUIConstants.PREF_MAX_DETAIL_LENGTH); 155 if (max > 0 && result.length() > max) { 156 text = result.substring(0, max) + "..."; } 158 valueDisplay.setText(text); 159 } 160 } 161 }); 162 } 163 }; 164 modelPresentation.computeDetail(val, valueDetailListener); 165 } 166 167 170 protected Control createControl(Composite parent) { 171 Composite composite = new Composite(parent, parent.getStyle()); 172 GridLayout layout = new GridLayout(); 173 composite.setLayout(layout); 174 composite.setLayoutData(new GridData(GridData.FILL_BOTH)); 175 176 sashForm = new SashForm(composite, parent.getStyle()); 177 sashForm.setOrientation(SWT.VERTICAL); 178 sashForm.setLayoutData(new GridData(GridData.FILL_BOTH)); 179 180 page = DebugUIPlugin.getActiveWorkbenchWindow().getActivePage(); 181 VariablesView view = getViewToEmulate(); 182 IWorkbenchPartSite site = null; 183 if (view != null) { 184 site = view.getSite(); 185 } else { 186 site = page.getActivePart().getSite(); 187 } 188 189 viewer = new VariablesViewer(sashForm, SWT.NO_TRIM, null); 190 viewer.setContentProvider(new ExpressionPopupContentProvider(viewer, site, view)); 191 modelPresentation = new VariablesViewModelPresentation(); 192 viewer.setLabelProvider(modelPresentation); 193 194 valueDisplay = new StyledText(sashForm, SWT.NO_TRIM | SWT.WRAP | SWT.V_SCROLL); 195 valueDisplay.setEditable(false); 196 197 tree = viewer.getTree(); 198 tree.addSelectionListener(new SelectionListener() { 199 public void widgetSelected(SelectionEvent e) { 200 try { 201 TreeItem[] selections = tree.getSelection(); 202 Object data = selections[selections.length-1].getData(); 203 204 IValue val = null; 205 if (data instanceof IndexedVariablePartition) { 206 return; 208 } 209 if (data instanceof IVariable) { 210 val = ((IVariable)data).getValue(); 211 } else if (data instanceof IExpression) { 212 val = ((IExpression)data).getValue(); 213 } 214 if (val == null) { 215 return; 216 } 217 218 updateValueDisplay(val); 219 } catch (DebugException ex) { 220 DebugUIPlugin.log(ex); 221 } 222 223 } 224 225 public void widgetDefaultSelected(SelectionEvent e) { 226 } 227 }); 228 229 Color background = parent.getDisplay().getSystemColor(SWT.COLOR_INFO_BACKGROUND); 230 Color foreground = parent.getDisplay().getSystemColor(SWT.COLOR_INFO_FOREGROUND); 231 tree.setForeground(foreground); 232 tree.setBackground(background); 233 composite.setForeground(foreground); 234 composite.setBackground(background); 235 valueDisplay.setForeground(foreground); 236 valueDisplay.setBackground(background); 237 238 sashForm.setWeights(DEFAULT_SASH_WEIGHTS); 240 241 return tree; 242 } 243 244 247 public Point computeSizeHint() { 248 Point persistedSize = getInitialSize(); 249 if (persistedSize != null) { 250 return persistedSize; 251 } 252 253 int height = 0; 254 int width = 0; 255 int itemCount = 0; 256 257 TreeItem[] items = tree.getItems(); 258 GC gc = new GC(tree); 259 for (int i=0; i<items.length; i++) { 260 width = Math.max (width, calculateWidth(items[i], gc)); 261 itemCount++; 262 263 TreeItem[] children = items[i].getItems(); 265 for (int j = 0; j < children.length; j++) { 266 width = Math.max(width, calculateWidth(children[j], gc)); 267 itemCount++; 268 } 269 270 } 271 gc.dispose (); 272 width += 40; 274 height = itemCount * tree.getItemHeight() + 90; 275 if (width > 300) { 276 width = 300; 277 } 278 if (height > 300) { 279 height = 300; 280 } 281 return shell.computeSize(width, height, true); 282 } 283 284 private int calculateWidth (TreeItem item, GC gc) { 285 int width = 0; 286 Image image = item.getImage (); 287 String text = item.getText (); 288 if (image != null) width = image.getBounds ().width + 2; 289 if (text != null && text.length () > 0) width += gc.stringExtent (text).x; 290 return width; 291 } 292 293 296 public boolean hasContents() { 297 return (viewer != null); 298 } 299 300 303 protected void performCommand() { 304 DebugPlugin.getDefault().getExpressionManager().addExpression(exp); 305 306 exp = null; 309 310 IViewPart part = page.findView(IDebugUIConstants.ID_EXPRESSION_VIEW); 311 if (part == null) { 312 try { 313 page.showView(IDebugUIConstants.ID_EXPRESSION_VIEW); 314 } catch (PartInitException e) { 315 } 316 } else { 317 page.bringToTop(part); 318 } 319 } 320 323 public void dispose() { 324 super.dispose(); 325 if (modelPresentation != null) { 326 modelPresentation.dispose(); 327 } 328 329 if (exp != null) 333 exp.dispose(); 334 } 335 } 336 | Popular Tags |