[Title] Advanced Declaration View for HTML Editor:
authorhyukmin kwon <hyukmin0530.kwon@samsung.com>
Wed, 15 May 2013 02:01:02 +0000 (11:01 +0900)
committerhyukmin kwon <hyukmin0530.kwon@samsung.com>
Wed, 15 May 2013 02:01:02 +0000 (11:01 +0900)
[Desc.] Displaying javaScript and CSS file source function is added.
[Issue] REDMINE-9269

org.tizen.web.advanceddeclarationview/META-INF/MANIFEST.MF
org.tizen.web.advanceddeclarationview/src/org/tizen/web/advanceddeclarationview/ui/HTMLDeclarationPage.java

index b7feca2..cc81dc0 100644 (file)
@@ -12,7 +12,8 @@ Require-Bundle: org.eclipse.ui,
  org.eclipse.wst.sse.ui,
  org.eclipse.jst.pagedesigner,
  org.eclipse.wst.html.core,
- org.eclipse.wst.xml.core;bundle-version="1.1.602"
+ org.eclipse.wst.xml.core;bundle-version="1.1.602",
+ org.eclipse.wst.sse.core
 Bundle-ActivationPolicy: lazy
 Bundle-RequiredExecutionEnvironment: JavaSE-1.6
 Import-Package: 
@@ -22,6 +23,7 @@ Import-Package:
  org.eclipse.jst.pagedesigner,
  org.eclipse.ui.editors.text,
  org.eclipse.ui.texteditor,
+ org.eclipse.wst.common.uriresolver.internal.provisional,
  org.eclipse.wst.css.core.internal.provisional.contenttype,
  org.eclipse.wst.css.ui,
  org.eclipse.wst.html.core.internal.provisional.contenttype,
index 72c90c7..45e33a9 100644 (file)
@@ -1,5 +1,10 @@
 package org.tizen.web.advanceddeclarationview.ui;
 
+import java.io.BufferedReader;
+import java.io.FileReader;
+import java.io.IOException;
+import java.nio.CharBuffer;
+
 import org.eclipse.jface.action.IMenuListener;
 import org.eclipse.jface.action.IMenuManager;
 import org.eclipse.jface.action.MenuManager;
@@ -8,6 +13,7 @@ import org.eclipse.jface.preference.IPreferenceStore;
 import org.eclipse.jface.preference.PreferenceConverter;
 import org.eclipse.jface.resource.ColorRegistry;
 import org.eclipse.jface.resource.JFaceResources;
+import org.eclipse.jface.text.Document;
 import org.eclipse.jface.text.IDocument;
 import org.eclipse.jface.text.TextSelection;
 import org.eclipse.jface.text.source.CompositeRuler;
@@ -61,6 +67,7 @@ import org.eclipse.wst.sse.ui.internal.provisional.style.LineStyleProvider;
 import org.eclipse.wst.xml.core.internal.document.AttrImpl;
 import org.eclipse.wst.xml.core.internal.document.ElementImpl;
 import org.eclipse.wst.xml.core.internal.document.NodeImpl;
+import org.eclipse.wst.common.uriresolver.internal.provisional.URIResolverPlugin;
 
 @SuppressWarnings("restriction")
 public class HTMLDeclarationPage extends DeclarationPage implements ISelectionListener,
@@ -124,6 +131,8 @@ IMenuListener, IPropertyChangeListener{
        private Color fBackgroundColor;
        private RGB fBackgroundColorRGB;
        
+       private String resourceAbsolutePath;
+       
                
        /**
         * Internal property change listener for handling changes in the editor's preferences.
@@ -533,8 +542,11 @@ IMenuListener, IPropertyChangeListener{
                                        if ("src".equalsIgnoreCase(attrImpl.getName())) {
                                                /** External JavaScript file or image file*/
                                                if ("script".equalsIgnoreCase(ownerElement.getTagName())) {
+                                                       resourceAbsolutePath = getAbsolutePath(model, attrImpl.getValue());
                                                        mode = MODE_JS_FILE_VIEW;
+                                                       
                                                } else if ("img".equalsIgnoreCase(ownerElement.getTagName())) {
+                                                       resourceAbsolutePath = getAbsolutePath(model, attrImpl.getValue());
                                                        mode = MODE_IMAGE_VIEW;
                                                } else {
                                                        mode = MODE_INVALID_CASE;
@@ -544,6 +556,7 @@ IMenuListener, IPropertyChangeListener{
                                                if ("link".equalsIgnoreCase(ownerElement.getTagName()) &&
                                                                "text/css".equalsIgnoreCase(ownerElement.getAttribute("type")) &&
                                                                "stylesheet".equalsIgnoreCase(ownerElement.getAttribute("rel"))) {
+                                                       resourceAbsolutePath = getAbsolutePath(model, attrImpl.getValue());
                                                        mode = MODE_CSS_FILE_VIEW;                                                                                                              
                                                } else {
                                                        mode = MODE_INVALID_CASE;
@@ -585,9 +598,13 @@ IMenuListener, IPropertyChangeListener{
                
                updateLayout(false);
        }
+       
+       private String getAbsolutePath(IStructuredModel model, String relPath) {
+               org.eclipse.wst.sse.core.internal.util.URIResolver resolver = model.getResolver();
+               return URIResolverPlugin.createResolver().resolve(resolver.getFileBaseLocation(), null, relPath);
+       }
 
        private void displayContents(IWorkbenchPart part, ISelection selection) {
-               boolean noContents = false;
                switch (mode) {
                        case  MODE_JS_DECLARATION:
                                
@@ -601,10 +618,10 @@ IMenuListener, IPropertyChangeListener{
                        case MODE_IMAGE_VIEW:
                                break;
                        case  MODE_JS_FILE_VIEW:
-                               
+                               displayJSfile();
                                break;
                        case MODE_CSS_FILE_VIEW:
-                               
+                               displayCSSfile();
                                break;
                        case MODE_INVALID_CASE:
                                break;  
@@ -613,6 +630,29 @@ IMenuListener, IPropertyChangeListener{
                }
        }
        
+       private void displayJSfile() {
+               /** TODO: apply multi-threads */
+               String source = readTextFileToString(resourceAbsolutePath);
+               if (source != null) {
+                       IDocument document= new Document(source);
+                       JavaScriptPlugin.getDefault().getJavaTextTools().setupJavaDocumentPartitioner(document, IJavaScriptPartitions.JAVA_PARTITIONING);                       
+                       fSrcViewer.setInput(document);
+
+               } else {
+                       
+               }               
+       }
+       
+       private void displayCSSfile() {
+               /** TODO: apply multi-threads */
+               String source = readTextFileToString(resourceAbsolutePath);
+               if (source != null) {
+                       fSrcViewer.getDocument().set(source);
+               } else {
+                       
+               }
+       }
+       
        /**
         * Sets the font for this viewer sustaining selection and scroll position.
         */
@@ -810,6 +850,40 @@ IMenuListener, IPropertyChangeListener{
        public HTMLDeclarationPage(final IViewSite parentViewSite) {
                parentSite = parentViewSite;
        }
+       /**
+        * Returns contents of a text file as a String
+        * @param filePath
+        * @return Text String or null if error exists 
+        */
+       private String readTextFileToString(String filePath) {
+               String returnStr = "";
+               BufferedReader reader = null;
+               CharBuffer cBuf = CharBuffer.allocate(20480);
+               
+               if (filePath != null) {
+                       try 
+                       {
+                               reader = new BufferedReader(new FileReader(filePath));
+                               
+                               cBuf.clear();
+                               while (reader.read(cBuf) >= 0) {
+                                       cBuf.flip();
+                                       returnStr = returnStr + cBuf.toString();
+                               }
+                       } catch (IOException e) {
+                               return null;
+                       } finally {
+                               try {
+                                       if (reader != null) {
+                                               reader.close();
+                                       }
+                               } catch (IOException e) {
+                                       return null;
+                               }
+                       }
+               }       
+               return returnStr;
+       }
 
        @Override
        public void propertyChange(PropertyChangeEvent event) {