[Title] InstallManager can display package description richer than
authoryongsung1.kim <yongsung1.kim@samsung.com>
Thu, 14 Mar 2013 03:41:31 +0000 (12:41 +0900)
committeryongsung1.kim <yongsung1.kim@samsung.com>
Thu, 14 Mar 2013 03:41:31 +0000 (12:41 +0900)
before.
[Desc.] InstallManager can support html format package description
partially. (eg. bold style, color style)
[Issue]

Change-Id: I6a39f54bde503f69a172f361eeb34a7493737430

InstallManager_java/src/org/tizen/installmanager/ui/page/PackageListPage.java

index 4433030..bb6f0e6 100644 (file)
@@ -39,6 +39,8 @@ import org.eclipse.jface.viewers.CheckboxTreeViewer;
 import org.eclipse.jface.viewers.ColumnViewerToolTipSupport;
 import org.eclipse.jface.viewers.ICheckStateListener;
 import org.eclipse.swt.SWT;
+import org.eclipse.swt.custom.StyleRange;
+import org.eclipse.swt.custom.StyledText;
 import org.eclipse.swt.events.MouseEvent;
 import org.eclipse.swt.events.MouseMoveListener;
 import org.eclipse.swt.events.MouseTrackAdapter;
@@ -50,7 +52,6 @@ import org.eclipse.swt.layout.GridData;
 import org.eclipse.swt.widgets.Composite;
 import org.eclipse.swt.widgets.Display;
 import org.eclipse.swt.widgets.Label;
-import org.eclipse.swt.widgets.Text;
 import org.eclipse.swt.widgets.Tree;
 import org.eclipse.swt.widgets.TreeColumn;
 import org.eclipse.swt.widgets.TreeItem;
@@ -72,7 +73,7 @@ import org.tizen.installmanager.ui.model.Item.InstallState;
  */
 public abstract class PackageListPage extends PageTemplate {
 
-       private Text mDescriptionText;
+       private StyledText mDescriptionText;
        protected Tree tree;
        private Label lblSelectPackage;
        protected Label lblRequiredSize;
@@ -123,7 +124,7 @@ public abstract class PackageListPage extends PageTemplate {
                tree.addMouseTrackListener(new MouseTrackAdapter() {
                        @Override
                        public void mouseExit(MouseEvent arg0) {
-                               mDescriptionText.setText("Position your mouse pointer over an item to view its description.");
+                               setComponentDescriptionText("Position your mouse pointer over an item to view its description.");
                                mDescriptionText.setForeground(new Color(null, 96, 96, 96));
                        }
                });
@@ -140,12 +141,12 @@ public abstract class PackageListPage extends PageTemplate {
                                        String description = viewController.getDescription(packageName);
 
                                        if (!description.isEmpty()) {
-                                               mDescriptionText.setText(description);
+                                               setComponentDescriptionText(description);
                                        } else {
-                                               mDescriptionText.setText("\"" + packageName + "\" category.");
+                                               setComponentDescriptionText("\"" + packageName + "\" category.");
                                        }
                                } else {
-                                       mDescriptionText.setText("Position your mouse pointer over an item to view its description.");
+                                       setComponentDescriptionText("Position your mouse pointer over an item to view its description.");
                                        mDescriptionText.setForeground(new Color(null, 96, 96, 96));
                                }
                        }
@@ -217,11 +218,11 @@ public abstract class PackageListPage extends PageTemplate {
        }
        
        private void setDescriptionText(Composite composite) {
-               mDescriptionText = new Text(
+               mDescriptionText = new StyledText(
                                composite,
                                SWT.BORDER | SWT.READ_ONLY | SWT.WRAP | SWT.H_SCROLL | SWT.CANCEL | SWT.MULTI);
                mDescriptionText.setForeground(new Color(null, 96, 96, 96));
-               mDescriptionText.setText("Position your mouse pointer over an item to view its description.");
+               setComponentDescriptionText("Position your mouse pointer over an item to view its description.");
                mDescriptionText.setEditable(false);
                mDescriptionText.setEnabled(false);
                
@@ -237,6 +238,94 @@ public abstract class PackageListPage extends PageTemplate {
                        mDescriptionText.setBounds(0, 287, 447, 45);                    
                }
        }
+       
+       private void setComponentDescriptionText(String text) {
+               StringBuffer newText = new StringBuffer();
+               ArrayList<StyleRange> styles = new ArrayList<StyleRange>();
+               StyleRange currentStyle = null;
+               int styleCount = 0;
+
+               for (int i = 0; i < text.length(); i++) {
+                       char c = text.charAt(i);
+                       if (c == '<' && text.indexOf('>', i) != -1) {
+                               int startIdx = i;
+                               int endIdx = text.indexOf('>', startIdx);
+
+                               String tag = text.substring(startIdx, endIdx + 1);
+                               if (tag.equals("<b>")) {
+                                       if (currentStyle == null) {
+                                               currentStyle = new StyleRange();
+                                               currentStyle.start = newText.length();
+                                       }
+                                       currentStyle.fontStyle |= SWT.BOLD;
+
+                                       styleCount++;
+                                       i += (tag.length() - 1);
+                               } else if (tag.equals("</b>") && currentStyle != null) {
+                                       styleCount--;
+
+                                       if (styleCount == 0) {
+                                               currentStyle.length = newText.length()
+                                                               - currentStyle.start;
+                                               styles.add(currentStyle);
+                                               currentStyle = null;
+                                       }
+
+                                       i += (tag.length() - 1);
+                               } else if (tag.matches("<font color=\"#[0-9a-fA-F]+\">")) {
+                                       int hexStartIdx = tag.indexOf('#');
+                                       int r = -1, g = -1, b = -1;
+                                       try {
+                                               r = Integer
+                                                               .parseInt(tag.substring(hexStartIdx + 1,
+                                                                               hexStartIdx + 3), 16);
+                                               g = Integer
+                                                               .parseInt(tag.substring(hexStartIdx + 3,
+                                                                               hexStartIdx + 5), 16);
+                                               b = Integer
+                                                               .parseInt(tag.substring(hexStartIdx + 5,
+                                                                               hexStartIdx + 7), 16);
+                                       } catch (IndexOutOfBoundsException e) {
+                                               r = -1;
+                                               g = -1;
+                                               b = -1;
+                                       } catch (NumberFormatException e) {
+                                               r = -1;
+                                               g = -1;
+                                               b = -1;
+                                       }
+
+                                       if (currentStyle == null) {
+                                               currentStyle = new StyleRange();
+                                               currentStyle.start = newText.length();
+                                       }
+                                       if (r != -1 && g != -1 && b != -1) {
+                                               currentStyle.foreground = new Color(null, r, g, b);
+                                       }
+
+                                       styleCount++;
+                                       i += (tag.length() - 1);
+                               } else if (tag.equals("</font>") && currentStyle != null) {
+                                       styleCount--;
+
+                                       if (styleCount == 0) {
+                                               currentStyle.length = newText.length()
+                                                               - currentStyle.start;
+                                               styles.add(currentStyle);
+                                               currentStyle = null;
+                                       }
+
+                                       i += (tag.length() - 1);
+                               } else {
+                                       newText.append(c);
+                               }
+                       } else {
+                               newText.append(c);
+                       }
+               }
+               mDescriptionText.setText(newText.toString());
+               mDescriptionText.setStyleRanges(styles.toArray(new StyleRange[0]));
+       }
 
        protected void setNextBtnEnabledAboutCheckedPackageCount() {
                if (getCheckedPackageNumber() > 0) {