public virtual void OnMouseOver(Graphics2D g2d, Rectangle cell, U guess, U gold)
                {
                    // Compute values
                    int x = (int)(cell.GetLocation().x + cell.GetWidth() / 5.0);
                    int y = (int)(cell.GetLocation().y + cell.GetHeight() / 5.0);
                    // Compute the text
                    int value = this._enclosing._enclosing.confTable[Pair.MakePair(guess, gold)];

                    if (value == null)
                    {
                        value = 0;
                    }
                    string text = "Guess: " + guess.ToString() + "\n" + "Gold: " + gold.ToString() + "\n" + "Value: " + value;
                    // Set the font
                    Font bak = g2d.GetFont();

                    g2d.SetFont(bak.DeriveFont(bak.GetSize() * 2.0f));
                    // Render
                    g2d.SetColor(Color.White);
                    g2d.Fill(cell);
                    g2d.SetColor(Color.Black);
                    foreach (string line in text.Split("\n"))
                    {
                        g2d.DrawString(line, x, y += g2d.GetFontMetrics().GetHeight());
                    }
                    // Reset
                    g2d.SetFont(bak);
                }
                protected override void PaintComponent(Graphics g)
                {
                    base.PaintComponent(g);
                    // Dimensions
                    Graphics2D g2d = (Graphics2D)g.Create();

                    g.SetFont(new Font("Arial", Font.Plain, 10));
                    int width      = this.GetWidth();
                    int height     = this.GetHeight();
                    int cellWidth  = width / this.columnCount;
                    int cellHeight = height / this.rowCount;
                    int xOffset    = (width - (this.columnCount * cellWidth)) / 2;
                    int yOffset    = (height - (this.rowCount * cellHeight)) / 2;
                    // Get label index
                    IList <U> labels = this._enclosing._enclosing.UniqueLabels().Stream().Collect(Collectors.ToList());
                    // Get color gradient
                    int maxDiag    = 0;
                    int maxOffdiag = 0;

                    foreach (KeyValuePair <Pair <U, U>, int> entry in this._enclosing._enclosing.confTable)
                    {
                        if (entry.Key.first == entry.Key.second)
                        {
                            maxDiag = Math.Max(maxDiag, entry.Value);
                        }
                        else
                        {
                            maxOffdiag = Math.Max(maxOffdiag, entry.Value);
                        }
                    }
                    // Render the grid
                    float[] hsb = new float[3];
                    for (int row = 0; row < this.rowCount; row++)
                    {
                        for (int col = 0; col < this.columnCount; col++)
                        {
                            // Position
                            int   x       = xOffset + (col * cellWidth);
                            int   y       = yOffset + (row * cellHeight);
                            float xCenter = xOffset + (col * cellWidth) + cellWidth / 3.0f;
                            float yCenter = yOffset + (row * cellHeight) + cellHeight / 2.0f;
                            // Get text + Color
                            string text;
                            Color  bg = Color.White;
                            if (row == 0 && col == 0)
                            {
                                text = "V guess | gold >";
                            }
                            else
                            {
                                if (row == 0)
                                {
                                    text = labels[col - 1].ToString();
                                }
                                else
                                {
                                    if (col == 0)
                                    {
                                        text = labels[row - 1].ToString();
                                    }
                                    else
                                    {
                                        // Set value
                                        int count = this._enclosing._enclosing.confTable[Pair.MakePair(labels[row - 1], labels[col - 1])];
                                        if (count == null)
                                        {
                                            count = 0;
                                        }
                                        text = string.Empty + count;
                                        // Get color
                                        if (row == col)
                                        {
                                            double percentGood = ((double)count) / ((double)maxDiag);
                                            hsb = Color.RGBtoHSB((int)(255 - (255.0 * percentGood)), (int)(255 - (255.0 * percentGood / 2.0)), (int)(255 - (255.0 * percentGood)), hsb);
                                            bg  = Color.GetHSBColor(hsb[0], hsb[1], hsb[2]);
                                        }
                                        else
                                        {
                                            double percentBad = ((double)count) / ((double)maxOffdiag);
                                            hsb = Color.RGBtoHSB((int)(255 - (255.0 * percentBad / 2.0)), (int)(255 - (255.0 * percentBad)), (int)(255 - (255.0 * percentBad)), hsb);
                                            bg  = Color.GetHSBColor(hsb[0], hsb[1], hsb[2]);
                                        }
                                    }
                                }
                            }
                            // Draw
                            Rectangle cell = new Rectangle(x, y, cellWidth, cellHeight);
                            g2d.SetColor(bg);
                            g2d.Fill(cell);
                            g2d.SetColor(Color.Black);
                            g2d.DrawString(text, xCenter, yCenter);
                            this.cells.Add(cell);
                        }
                    }
                    // Mouse over
                    if (this.selectedCell != null && this.selectedCell.x > 0 && this.selectedCell.y > 0)
                    {
                        int       index = this.selectedCell.x + (this.selectedCell.y * this.columnCount);
                        Rectangle cell  = this.cells[index];
                        this.OnMouseOver(g2d, cell, labels[this.selectedCell.y - 1], labels[this.selectedCell.x - 1]);
                    }
                    // Clean up
                    g2d.Dispose();
                }
Example #3
0
        private static IDictionary <string, FieldInfo> FillOptionsImpl(object[] instances, Type[] classes, Properties options, bool ensureAllOptions, bool isBootstrap)
        {
            // Print usage, if applicable
            if (!isBootstrap)
            {
                if (Sharpen.Runtime.EqualsIgnoreCase("true", options.GetProperty("usage", "false")) || Sharpen.Runtime.EqualsIgnoreCase("true", options.GetProperty("help", "false")))
                {
                    ICollection <Type> allClasses = new HashSet <Type>();
                    Java.Util.Collections.AddAll(allClasses, classes);
                    if (instances != null)
                    {
                        foreach (object o in instances)
                        {
                            allClasses.Add(o.GetType());
                        }
                    }
                    System.Console.Error.WriteLine(Usage(Sharpen.Collections.ToArray(allClasses, new Type[0])));
                    System.Environment.Exit(0);
                }
            }
            //--Create Class->Object Mapping
            IDictionary <Type, object> class2object = new Dictionary <Type, object>();

            if (instances != null)
            {
                for (int i = 0; i < classes.Length; ++i)
                {
                    System.Diagnostics.Debug.Assert(instances[i].GetType() == classes[i]);
                    class2object[classes[i]] = instances[i];
                    Type mySuper = instances[i].GetType().BaseType;
                    while (mySuper != null && !mySuper.Equals(typeof(object)))
                    {
                        if (!class2object.Contains(mySuper))
                        {
                            class2object[mySuper] = instances[i];
                        }
                        mySuper = mySuper.BaseType;
                    }
                }
            }
            //--Get Fillable Options
            IDictionary <string, FieldInfo>          canFill  = new Dictionary <string, FieldInfo>();
            IDictionary <string, Pair <bool, bool> > required = new Dictionary <string, Pair <bool, bool> >();
            /* <exists, is_set> */
            IDictionary <string, string> interner = new Dictionary <string, string>();

            foreach (Type c in classes)
            {
                FieldInfo[] fields;
                try
                {
                    fields = ScrapeFields(c);
                }
                catch (Exception e)
                {
                    Redwood.Util.Debug("Could not check fields for class: " + c.FullName + "  (caused by " + e.GetType() + ": " + e.Message + ')');
                    continue;
                }
                bool someOptionFilled = false;
                bool someOptionFound  = false;
                foreach (FieldInfo f in fields)
                {
                    ArgumentParser.Option o = f.GetAnnotation <ArgumentParser.Option>();
                    if (o != null)
                    {
                        someOptionFound = true;
                        //(check if field is static)
                        if ((f.GetModifiers() & Modifier.Static) == 0 && instances == null)
                        {
                            continue;
                        }
                        someOptionFilled = true;
                        //(required marker)
                        Pair <bool, bool> mark = Pair.MakePair(false, false);
                        if (o.Required())
                        {
                            mark = Pair.MakePair(true, false);
                        }
                        //(add main name)
                        string name = o.Name().ToLower();
                        if (name.IsEmpty())
                        {
                            name = f.Name.ToLower();
                        }
                        if (canFill.Contains(name))
                        {
                            string name1 = canFill[name].DeclaringType.GetCanonicalName() + '.' + canFill[name].Name;
                            string name2 = f.DeclaringType.GetCanonicalName() + '.' + f.Name;
                            if (!name1.Equals(name2))
                            {
                                Redwood.Util.RuntimeException("Multiple declarations of option " + name + ": " + name1 + " and " + name2);
                            }
                            else
                            {
                                Redwood.Util.Err("Class is in classpath multiple times: " + canFill[name].DeclaringType.GetCanonicalName());
                            }
                        }
                        canFill[name]  = f;
                        required[name] = mark;
                        interner[name] = name;
                        //(add alternate names)
                        if (!o.Alt().IsEmpty())
                        {
                            foreach (string alt in o.Alt().Split(" *, *"))
                            {
                                alt = alt.ToLower();
                                if (canFill.Contains(alt) && !alt.Equals(name))
                                {
                                    throw new ArgumentException("Multiple declarations of option " + alt + ": " + canFill[alt] + " and " + f);
                                }
                                canFill[alt] = f;
                                if (mark.first)
                                {
                                    required[alt] = mark;
                                }
                                interner[alt] = name;
                            }
                        }
                    }
                }
                //(check to ensure that something got filled, if any @Option annotation was found)
                if (someOptionFound && !someOptionFilled)
                {
                    Redwood.Util.Warn("found @Option annotations in class " + c + ", but didn't set any of them (all options were instance variables and no instance given?)");
                }
            }
            //--Fill Options
            foreach (KeyValuePair <object, object> entry in options)
            {
                string rawKeyStr = entry.Key.ToString();
                string key       = rawKeyStr.ToLower();
                // (get values)
                string value = entry.Value.ToString();
                System.Diagnostics.Debug.Assert(value != null);
                FieldInfo target = canFill[key];
                // (mark required option as fulfilled)
                Pair <bool, bool> mark = required[key];
                if (mark != null && mark.first)
                {
                    required[key] = Pair.MakePair(true, true);
                }
                // (fill the field)
                if (target != null)
                {
                    // (case: declared option)
                    FillField(class2object[target.DeclaringType], target, value);
                }
                else
                {
                    if (ensureAllOptions)
                    {
                        // (case: undeclared option)
                        // split the key
                        int lastDotIndex = rawKeyStr.LastIndexOf('.');
                        if (lastDotIndex < 0)
                        {
                            Redwood.Util.Err("Unrecognized option: " + key);
                            continue;
                        }
                        if (!rawKeyStr.StartsWith("log."))
                        {
                            // ignore Redwood options
                            string className = Sharpen.Runtime.Substring(rawKeyStr, 0, lastDotIndex);
                            // get the class
                            Type clazz = null;
                            try
                            {
                                clazz = ClassLoader.GetSystemClassLoader().LoadClass(className);
                            }
                            catch (Exception)
                            {
                                Redwood.Util.Err("Could not set option: " + entry.Key + "; either the option is mistyped, not defined, or the class " + className + " does not exist.");
                            }
                            // get the field
                            if (clazz != null)
                            {
                                string fieldName = Sharpen.Runtime.Substring(rawKeyStr, lastDotIndex + 1);
                                try
                                {
                                    target = clazz.GetField(fieldName);
                                }
                                catch (Exception)
                                {
                                    Redwood.Util.Err("Could not set option: " + entry.Key + "; no such field: " + fieldName + " in class: " + className);
                                }
                                if (target != null)
                                {
                                    Redwood.Util.Log("option overrides " + target + " to '" + value + '\'');
                                    FillField(class2object[target.DeclaringType], target, value);
                                }
                                else
                                {
                                    Redwood.Util.Err("Could not set option: " + entry.Key + "; no such field: " + fieldName + " in class: " + className);
                                }
                            }
                        }
                    }
                }
            }
            //--Ensure Required
            bool good = true;

            foreach (KeyValuePair <string, Pair <bool, bool> > entry_1 in required)
            {
                string            key  = entry_1.Key;
                Pair <bool, bool> mark = entry_1.Value;
                if (mark.first && !mark.second)
                {
                    Redwood.Util.Err("Missing required option: " + interner[key] + "   <in class: " + canFill[key].DeclaringType + '>');
                    required[key] = Pair.MakePair(true, true);
                    //don't duplicate error messages
                    good = false;
                }
            }
            if (!good)
            {
                throw new Exception("Specified properties are not parsable or not valid!");
            }
            //System.exit(1);
            return(canFill);
        }