예제 #1
0
        protected string RenderSearchResults()
        {
            Color  color1 = Color.Transparent;
            Color  color2 = Color.Transparent;
            bool   ok     = true;
            string s      = this.parameters["_Color1", null];

            if (!String.IsNullOrEmpty(s))
            {
                long c = ColorParser.Parse(s);
                if (c == ColorParser.InvalidColor)
                {
                    ok = false;
                }
                else
                {
                    color1 = ColorTransform.IntToRgb((int)c);
                }
            }
            s = this.parameters["_Color2", null];
            if (!String.IsNullOrEmpty(s))
            {
                long c = ColorParser.Parse(s);
                if (c == ColorParser.InvalidColor)
                {
                    ok = false;
                }
                else
                {
                    color2 = ColorTransform.IntToRgb((int)c);
                }
            }

            HtmlRenderer html = new HtmlRenderer();

            if (!ok)
            {
                search.EmptyResultTable(html, "One of the colors is invalid");
                return(html.ToString());
            }
            if ((color1 == Color.Transparent) && (color2 == Color.Transparent))
            {
                search.EmptyResultTable(html, "One of the colors must be specified");
                return(html.ToString());
            }

            PaletteManager     mgr    = new PaletteManager(Utils.ConnectionString);
            List <PaletteItem> result = null;
            string             msg    = null;

            try
            {
                DateTime start = DateTime.Now;

                result = mgr.Find(color1, color2,
                                  (ColorSearchWidth)this.parameters.Get("_Comparison", (long)ColorSearchWidth.Wide),
                                  this.parameters.Get("_ShowSimilar", false),
                                  1000);

                if (result != null)
                {
                    if (result.Count > 0)
                    {
                        PaletteListSortMode mode = (PaletteListSortMode)this.parameters.Get("_Sort", (long)PaletteListSortMode.Title);
                        PaletteManager.Sort(result, mode, mode == PaletteListSortMode.ClusterColor ? mgr.LoadClusters() : null);
                    }
                    else
                    {
                        result = null;
                    }
                }
                double sec = DateTime.Now.Subtract(start).TotalSeconds;
                if (result != null)
                {
                    msg = String.Format("{0} item(s) found, {1} seconds", result.Count, sec.ToString("#####0.0##"));
                }
            }
            catch
            {
                result = null;
            }
            if (result == null)
            {
                search.EmptyResultTable(html, "No palette(s) found or error occured");
                return(html.ToString());
            }

            bool paging = !this.parameters.Get("_ShowAll", false);

            int first     = 0;
            int last      = result.Count - 1;
            int pageCount = 0;
            int page      = 0;

            if (paging)
            {
                pageCount = result.Count / search.PageSize;
                if ((pageCount * search.PageSize) < result.Count)
                {
                    pageCount++;
                }

                page = (int)this.parameters.Get("Page", 1L);

                if (page < 1)
                {
                    page = 1;
                }
                if (page > pageCount)
                {
                    page = pageCount;
                }

                this.parameters.Set("Page", (long)page);

                first = (page - 1) * search.PageSize;
                last  = first + search.PageSize;
                if (last >= result.Count)
                {
                    last = result.Count - 1;
                }
            }

            html.Add("<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\">");

            if (msg != null)
            {
                html.Add("<tr><td colspan=\"2\" align=\"center\" style=\"font-size:small;color:#909090;padding-bottom:2px;\">");
                html.Text(msg);
                html.Add("</td></tr>");
            }

            string key   = Utils.GetTimeKey(60);
            bool   black = (BackgroundColor)this.parameters.Get("BgColor", (long)BackgroundColor.White) == BackgroundColor.Black;

            if (paging)
            {
                search.RenderPager(html, page, pageCount);
            }

            for (int i = first; i <= last; i++)
            {
                search.RenderPaletteItem(html, result[i], key, black);
            }

            if (paging)
            {
                search.RenderPager(html, page, pageCount);
            }

            html.Add("</table>");

            return(html.ToString());
        }