plugins/org.symbian.tools.tmw.debug/src/org/symbian/tools/wrttools/debug/internal/model/SymbianDebugModelPresentation.java
changeset 471 06589bf52fa7
parent 470 d4809db37847
child 472 bd9f2d7c64a6
equal deleted inserted replaced
470:d4809db37847 471:06589bf52fa7
     1 package org.symbian.tools.wrttools.debug.internal.model;
       
     2 
       
     3 import java.util.Collection;
       
     4 
       
     5 import org.chromium.debug.core.model.Value;
       
     6 import org.chromium.sdk.JsValue;
       
     7 import org.chromium.sdk.JsVariable;
       
     8 import org.eclipse.core.filesystem.IFileStore;
       
     9 import org.eclipse.core.resources.IFile;
       
    10 import org.eclipse.core.resources.IResource;
       
    11 import org.eclipse.core.resources.IStorage;
       
    12 import org.eclipse.debug.core.model.IBreakpoint;
       
    13 import org.eclipse.debug.core.model.ILineBreakpoint;
       
    14 import org.eclipse.debug.core.model.IValue;
       
    15 import org.eclipse.debug.ui.IDebugModelPresentation;
       
    16 import org.eclipse.debug.ui.IValueDetailListener;
       
    17 import org.eclipse.jface.viewers.LabelProvider;
       
    18 import org.eclipse.swt.graphics.Image;
       
    19 import org.eclipse.ui.IEditorInput;
       
    20 import org.eclipse.ui.IFileEditorInput;
       
    21 import org.eclipse.ui.PartInitException;
       
    22 import org.eclipse.ui.ide.FileStoreEditorInput;
       
    23 import org.eclipse.ui.ide.IDE;
       
    24 import org.eclipse.ui.part.FileEditorInput;
       
    25 import org.eclipse.wst.jsdt.core.JavaScriptCore;
       
    26 import org.eclipse.wst.jsdt.internal.ui.javaeditor.JarEntryEditorInput;
       
    27 import org.eclipse.wst.jsdt.ui.JavaScriptUI;
       
    28 
       
    29 @SuppressWarnings("restriction")
       
    30 public class SymbianDebugModelPresentation extends LabelProvider implements
       
    31 		IDebugModelPresentation {
       
    32     private static final int DETAILS_DEPTH = 2;
       
    33 
       
    34 	public void setAttribute(String attribute, Object value) {
       
    35 	}
       
    36 
       
    37 	@Override
       
    38 	public Image getImage(Object element) {
       
    39 		// use default image
       
    40 		return null;
       
    41 	}
       
    42 
       
    43 	@Override
       
    44 	public String getText(Object element) {
       
    45 		// use default label text
       
    46 		return null;
       
    47 	}
       
    48 
       
    49     public void computeDetail(IValue value, IValueDetailListener listener) {
       
    50         String detail = ""; //$NON-NLS-1$
       
    51         if (value instanceof Value) {
       
    52             // Avoid quoting string JavaScript values by getting the value
       
    53             // string
       
    54             // from the underlying JsValue.
       
    55             final JsValue jsValue = ((Value) value).getJsValue();
       
    56             detail = printJSON(jsValue, 0);
       
    57         }
       
    58 
       
    59         listener.detailComputed(value, detail);
       
    60     }
       
    61 
       
    62     private String printJSON(final JsValue value, int depth) {
       
    63         if (depth < DETAILS_DEPTH) {
       
    64             switch (value.getType()) {
       
    65             case TYPE_OBJECT:
       
    66                 return printObject(value, depth);
       
    67             case TYPE_ARRAY:
       
    68                 return printArray(value, depth);
       
    69             }
       
    70         }
       
    71         if (depth > 0) {
       
    72             if (value.getType() == JsValue.Type.TYPE_STRING) {
       
    73                 return "\"" + value.getValueString() + "\"";
       
    74             }
       
    75         }
       
    76         return value.getValueString();
       
    77     }
       
    78 
       
    79     private String printArray(JsValue value, int depth) {
       
    80         final StringBuilder builder = new StringBuilder("{ ");
       
    81         String sep = "";
       
    82 
       
    83         Collection<? extends JsVariable> properties = value.asObject().asArray().toSparseArray().values();
       
    84 
       
    85         for (JsVariable jsVariable : properties) {
       
    86             builder.append(sep).append(printJSON(jsVariable.getValue(), depth + 1));
       
    87             sep = ", ";
       
    88         }
       
    89 
       
    90         builder.append(" }");
       
    91         return builder.toString();
       
    92     }
       
    93 
       
    94     private String printObject(final JsValue value, final int depth) {
       
    95         final StringBuilder builder = new StringBuilder("{ ");
       
    96 
       
    97         String sep = "";
       
    98 
       
    99         Collection<? extends JsVariable> properties = value.asObject().getProperties();
       
   100         for (JsVariable jsVariable : properties) {
       
   101             builder.append(sep).append(jsVariable.getName()).append(" : ");
       
   102             builder.append(printJSON(jsVariable.getValue(), depth + 1));
       
   103             sep = ", ";
       
   104         }
       
   105 
       
   106         builder.append(" }");
       
   107         return builder.toString();
       
   108     }
       
   109 
       
   110     public IEditorInput getEditorInput(Object element) {
       
   111 		return toEditorInput(element);
       
   112 	}
       
   113 
       
   114     public static IEditorInput toEditorInput(Object element) {
       
   115 		if (element instanceof IFile) {
       
   116 			return new FileEditorInput((IFile) element);
       
   117 		}
       
   118 		if (element instanceof IFileStore) {
       
   119 			return new FileStoreEditorInput((IFileStore) element);
       
   120 		}
       
   121         if (element instanceof IStorage) {
       
   122             return new JarEntryEditorInput((IStorage) element);
       
   123         }
       
   124 
       
   125 		if (element instanceof ILineBreakpoint) {
       
   126 			return new FileEditorInput((IFile) ((ILineBreakpoint) element)
       
   127 					.getMarker().getResource());
       
   128 		}
       
   129 
       
   130 		return null;
       
   131 	}
       
   132 
       
   133 	public String getEditorId(IEditorInput input, Object element) {
       
   134 		if (input instanceof IFileEditorInput) {
       
   135 			IFile file;
       
   136 			if (element instanceof IFile) {
       
   137 				file = (IFile) element;
       
   138 			} else if (element instanceof IBreakpoint) {
       
   139 				IBreakpoint breakpoint = (IBreakpoint) element;
       
   140 				IResource resource = breakpoint.getMarker().getResource();
       
   141 				// Can the breakpoint resource be folder or project? Better
       
   142 				// check for it.
       
   143 				if (resource instanceof IFile == false) {
       
   144 					return null;
       
   145 				}
       
   146 				file = (IFile) resource;
       
   147 			} else {
       
   148 				return null;
       
   149 			}
       
   150 
       
   151 			// Pick the editor based on the file extension, taking user
       
   152 			// preferences into account.
       
   153 			try {
       
   154 				return IDE.getEditorDescriptor(file).getId();
       
   155 			} catch (PartInitException e) {
       
   156 				// TODO(peter.rybin): should it really be the default case?
       
   157 				// There might be no virtual project.
       
   158 				return null;
       
   159 			}
       
   160 		} else {
       
   161             if (element instanceof IStorage) {
       
   162                 IStorage store = (IStorage) element;
       
   163                 if (JavaScriptCore.isJavaScriptLikeFileName(store.getName())) {
       
   164                     return JavaScriptUI.ID_CU_EDITOR;
       
   165 				} else {
       
   166 					return "org.eclipse.wst.html.core.htmlsource.source";
       
   167 				}
       
   168 			}
       
   169 		}
       
   170 		return null;
       
   171 	}
       
   172 }