Feature request and a little component

Request new features and improvements you want to see in WebLaF.
I will answer any request so don't hesitate and post it!

Feature request and a little component

Postby derdudele » Fri Nov 09, 2012 11:14 am

Hi,

I would love to see the Method setEditable ,cause I want to display the User some Informations but he should not be able to edit the WebToogleButtons, in the WebButtonGroup class.



And now a little, a bit dirty, code for a Progress Component. May you like it. Have fun.

Code: Select all

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.geom.Arc2D;
import java.awt.geom.Rectangle2D;

import javax.swing.JLabel;

import com.alee.managers.tooltip.TooltipManager;

public class ProgressLabel extends JLabel {

   private Color progessColor = new Color(37, 174, 235);
   private Color backgroundColor = new Color(34, 43, 45);
   private float thickness = 4f;
   private long value = 0;
   private long max = 100;
   private String unit = "%";

   private boolean showValue = true;

   public ProgressLabel() {
      this.init();
   }

   public TaskyProgressLabel(String text) {
      this.setText(text);
      this.init();
   }

   private void init() {
      this.setMinimumSize(new Dimension(100, 100));
      this.setPreferredSize(new Dimension(100, 100));
      this.setSize(new Dimension(100, 100));
      this.refreshTooltip();
   }

   public void setProgressColor(Color c) {
      this.progessColor = c;
   }

   public Color getProgressColor() {
      return this.progessColor;
   }
   
   public void setBackgroundColor(Color c){
      this.backgroundColor = c;
   }
   
   public Color getBackgroundColor(){
      return this.backgroundColor;
   }

   public void setValue(long value) {
      this.value = value;
      this.refreshTooltip();
   }

   public long getValue() {
      return this.value;
   }

   public void setMaximum(long max){
      this.max = max;
   }
   
   public long getMaximum(){
      return this.max;
   }
   
   @Override
   public void setText(String text) {
      super.setText(text);
      this.refreshTooltip();
   }

   private void refreshTooltip() {
      TooltipManager.removeTooltips(this);
      TooltipManager.setTooltip(this, this.getValue() + this.unit + " "
            + this.getText());
   }

   public void setShowValue(boolean val) {
      this.showValue = val;
   }

   public boolean isShowValue() {
      return this.showValue;
   }
   
   public void setUnit(String unit){
      this.unit = unit;
   }
   
   public String getUnit(){
      return this.unit;
   }

   @Override
   public void paint(Graphics g) {
      Graphics2D g2 = (Graphics2D) g;
      g.clearRect(0, 0, getWidth(), getHeight());
      g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
            RenderingHints.VALUE_ANTIALIAS_ON);
      g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
            RenderingHints.VALUE_TEXT_ANTIALIAS_LCD_VRGB);
      Arc2D progress = new Arc2D.Double();
      g2.setStroke(new BasicStroke(this.thickness));
      g2.setColor(this.backgroundColor);
      progress.setArcByCenter(getWidth() / 2.d, getWidth() / 2.d,
            (getWidth() - this.thickness-1) / 2.d, 270, 360, Arc2D.OPEN);
      g2.draw(progress);

      if (value > 0) {
         //g2.setStroke(new BasicStroke(this.thickness + 1f));
         double degree = Math.floor((this.value / (double) this.max) * 360);
         progress.setArcByCenter(getWidth() / 2.d, getWidth() / 2.d,
               (getWidth() - this.thickness-1) / 2.d, 270, degree,
               Arc2D.OPEN);

         g2.setColor(this.progessColor);
         g2.draw(progress);
      }

      if (showValue) {
         g2.setColor(this.getForeground());
         g2.setFont(this.getFont());
         FontMetrics metrics = g2.getFontMetrics();
         String text = value + " " + this.unit;
         Rectangle2D line = metrics.getStringBounds(text, g2);
         g2.drawString(text,
               (int) (((getWidth()) / 2) - (line.getWidth() / 2)),
               (int) (((getHeight() - 3) / 2) + (line.getHeight() / 2)));
      }
   }
}

derdudele
 
Posts: 5
Joined: Thu Oct 25, 2012 8:21 pm

Re: Feature request and a little component

Postby mgarin » Fri Nov 09, 2012 12:34 pm

derdudele wrote:I would love to see the Method setEditable ,cause I want to display the User some Informations but he should not be able to edit the WebToogleButtons, in the WebButtonGroup class.

I did not really understand where you want to see "setEditable" method and how is it connected with WebToggleButton and WebButtonGroup components...

derdudele wrote:And now a little, a bit dirty, code for a Progress Component. May you like it. Have fun.

Nice one :)

I can give you a few useful tips concerning this component:
1. Extend paintComponent, not paint - you will lose a lot of performance using paint due to repaint optimizations made inside the JComponent paint-methods structure
2. Repaint the component when some of its values changes
3. Use basic draw methods instead of drawing custom shapes where its possible - in this case use drawArc instead of creating an additional Arc2D shape and painting it - basic draw methods area always work much faster

You can also see the modifications i have done to your component using the tips above and some ready-to-use WebLaF methods and utilities (this example is runnable!):
Code: Select all
import com.alee.extended.panel.GroupPanel;
import com.alee.extended.statusbar.WebMemoryBar;
import com.alee.extended.window.TestFrame;
import com.alee.laf.WebLookAndFeel;
import com.alee.laf.label.WebLabel;
import com.alee.managers.tooltip.TooltipManager;
import com.alee.managers.tooltip.WebCustomTooltip;
import com.alee.utils.LafUtils;
import com.alee.utils.swing.Timer;

import javax.swing.*;
import javax.swing.plaf.basic.BasicLabelUI;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.Rectangle2D;

public class ArcProgressLabel extends JLabel
{
    private Color progessColor = new Color ( 37, 174, 235 );
    private Color backgroundColor = new Color ( 34, 43, 45 );
    private int thickness = 6;
    private int value = 0;
    private int max = 100;
    private String unit = "%";

    private WebLabel tooltipLabel;
    private WebCustomTooltip tooltip;

    private boolean showValue = true;

    public ArcProgressLabel ()
    {
        super ();
        init ();
    }

    public ArcProgressLabel ( String text )
    {
        super ();
        setText ( text );
        init ();
    }

    private void init ()
    {
        setUI ( new BasicLabelUI ()
        {
            protected void paintEnabledText ( JLabel l, Graphics g, String s, int textX, int textY )
            {
                //
            }

            protected void paintDisabledText ( JLabel l, Graphics g, String s, int textX, int textY )
            {
                //
            }
        } );
        setBorder ( BorderFactory.createEmptyBorder ( thickness * 2, thickness * 2, thickness * 2, thickness * 2 ) );
        refreshTooltip ();
    }

    public void setProgressColor ( Color c )
    {
        progessColor = c;
        repaint ();
    }

    public Color getProgressColor ()
    {
        return progessColor;
    }

    public void setBackgroundColor ( Color c )
    {
        backgroundColor = c;
        repaint ();
    }

    public Color getBackgroundColor ()
    {
        return backgroundColor;
    }

    public void setValue ( int value )
    {
        this.value = value;
        refreshTooltip ();
        repaint ();
    }

    public int getValue ()
    {
        return value;
    }

    public void setMaximum ( int max )
    {
        this.max = max;
        repaint ();
    }

    public int getMaximum ()
    {
        return max;
    }

    public void setText ( String text )
    {
        super.setText ( text );
        refreshTooltip ();
    }

    private void refreshTooltip ()
    {
        if ( tooltip == null )
        {
            tooltipLabel = new WebLabel ();
            tooltip = TooltipManager.setTooltip ( this, tooltipLabel );
        }
        tooltipLabel.setText ( getValue () + unit + " " + getText () );
        tooltip.updateLocation ();
    }

    public void setShowValue ( boolean val )
    {
        showValue = val;
        repaint ();
    }

    public boolean isShowValue ()
    {
        return showValue;
    }

    public void setUnit ( String unit )
    {
        this.unit = unit;
        repaint ();
    }

    public String getUnit ()
    {
        return unit;
    }

    protected void paintComponent ( Graphics g )
    {
        Graphics2D g2d = ( Graphics2D ) g;

        // Super call for cleanup
        super.paintComponent ( g2d );

        // Antialias
        Object aa = LafUtils.setupAntialias ( g2d );

        // Sizes
        int length = Math.min ( getWidth (), getHeight () );
        int x = getWidth () / 2 - length / 2;
        int y = getHeight () / 2 - length / 2;

        // Background arc
        g2d.setStroke ( new BasicStroke ( thickness ) );
        g2d.setColor ( backgroundColor );
        g2d.drawArc ( x + thickness / 2, y + thickness / 2, length - thickness - 1, length - thickness - 1, 0, 360 );

        // Value arc
        if ( value > 0 )
        {
            int degree = Math.round ( ( float ) Math.floor ( ( value / ( float ) max ) * 360 ) );
            g2d.setStroke ( new BasicStroke ( thickness - 3, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND ) );
            g2d.setColor ( progessColor );
            g2d.drawArc ( x + thickness / 2, y + thickness / 2, length - thickness - 1, length - thickness - 1, 270, degree );
        }

        // Restoring old antialias state to avoid text antialiasing
        LafUtils.restoreAntialias ( g2d, aa );

        // Progress text
        if ( showValue )
        {
            // System text hints for best text painting mode
            LafUtils.setupSystemTextHints ( g2d );

            // Painting progress text
            g2d.setColor ( getForeground () );
            g2d.setFont ( getFont () );
            FontMetrics metrics = g2d.getFontMetrics ();
            String text = value + " " + unit;
            Rectangle2D line = metrics.getStringBounds ( text, g2d );
            g2d.drawString ( text, ( int ) ( ( ( getWidth () ) / 2 ) - ( line.getWidth () / 2 ) ),
                    ( int ) ( ( ( getHeight () - 3 ) / 2 ) + ( line.getHeight () / 2 ) ) );
        }
    }

    public Dimension getPreferredSize ()
    {
        Dimension ps = super.getPreferredSize ();
        int max = Math.max ( Math.max ( ps.width, ps.height ), 100 );
        return new Dimension ( max, max );
    }

    public static void main ( String[] args )
    {
        // L&F
        WebLookAndFeel.install ();

        // Simple test frame
        final ArcProgressLabel arcProgressLabel = new ArcProgressLabel ( "Test" );
        new TestFrame ( new GroupPanel ( 30, false, new WebMemoryBar (), arcProgressLabel ), 50 );

        // Progress timer
        new Timer ( 30, new ActionListener ()
        {
            private boolean increasing = true;

            public void actionPerformed ( ActionEvent e )
            {
                if ( increasing )
                {
                    int v = arcProgressLabel.getValue () + 1;
                    arcProgressLabel.setValue ( v );
                    if ( v == 100 )
                    {
                        increasing = false;
                    }
                }
                else
                {
                    int v = arcProgressLabel.getValue () - 1;
                    arcProgressLabel.setValue ( v );
                    if ( v == 0 )
                    {
                        increasing = true;
                    }
                }
            }
        } ).start ();
    }
}
User avatar
mgarin
Site Admin
 
Posts: 223
Joined: Mon Apr 16, 2012 1:01 pm
Location: Russia, Saint-Petersburg

Re: Feature request and a little component

Postby derdudele » Fri Nov 09, 2012 5:08 pm

First of all thx for your advices ^^.

mgarin wrote:I did not really understand where you want to see "setEditable" method and how is it connected with WebToggleButton and WebButtonGroup components...


Okey, i'll try to explain my problem more cleary.

I add some WebToogleButton 's to a WebButtonGroup.
What I want is to set the hole group not editable. I tried this with setEnabled(false) but the user still could use the buttons and if I tried the same function in the Buttons like button1.setEnabeld(false); the button is grayed out. What I want is that the buttons looks normal but the should not be editable.
Code: Select all
WebButtonGroup group = new WebButtonGroup(true, button1,
            button2, button3, button5);
group.setEnabled(false);
button1.setEnabled(false);
derdudele
 
Posts: 5
Joined: Thu Oct 25, 2012 8:21 pm

Re: Feature request and a little component

Postby mgarin » Fri Nov 09, 2012 5:29 pm

Under "editable" you mean "enabled"? :)
That is two different things - buttons can be enabled/disabled but they are not editable.
Some text field might be editable or not, but not buttons.

If "enabled" is what you mean - one more question...

I add some WebToogleButton 's to a WebButtonGroup.
What I want is to set the hole group not editable. I tried this with setEnabled(false) but the user still could use the buttons and if I tried the same function in the Buttons like button1.setEnabeld(false); the button is grayed out. What I want is that the buttons looks normal but the should not be editable.


(1) You want that group sets all inner buttons disabled/enabled when it is made enabled/disabled itself right?
(2) Or you are looking for an additional button state when buttons cannot be pressed but they do not look like disabled?

So you are looking for 1st or 2nd option? :)
User avatar
mgarin
Site Admin
 
Posts: 223
Joined: Mon Apr 16, 2012 1:01 pm
Location: Russia, Saint-Petersburg

Re: Feature request and a little component

Postby derdudele » Fri Nov 09, 2012 5:33 pm

the 1st and the 2nd both together would be great ^^.
derdudele
 
Posts: 5
Joined: Thu Oct 25, 2012 8:21 pm

Re: Feature request and a little component

Postby mgarin » Fri Nov 09, 2012 5:52 pm

Well, 1st option is indeed a good improvement, i have added it and it will be availabl with the next commit.

But the disabled behavior and enabled view - is a strange state for any component, i don't really see how it can be used...
User avatar
mgarin
Site Admin
 
Posts: 223
Joined: Mon Apr 16, 2012 1:01 pm
Location: Russia, Saint-Petersburg

Re: Feature request and a little component

Postby derdudele » Fri Nov 09, 2012 6:44 pm

Well in my opinion it makes sense.
I got a little database filled with tasks.
These tasks get there id from one number range but types of tasks are different for example work tasks, private tasks and so on.

So if I put these id in a text field I would love to show the user which kind of task it is.
derdudele
 
Posts: 5
Joined: Thu Oct 25, 2012 8:21 pm

Re: Feature request and a little component

Postby mgarin » Fri Nov 09, 2012 6:54 pm

In case you want to decorate the current tasks status somehow - you don't need the toggle button, use a simple label inside a decorated panel. Toggle button usage might be used in case you can change that status.

There is also a lot of other ways to decorate simple text.

And anyway it seems that combobox is a better component for this case, than a toggle for each status... Ofcourse if i understood the example correctly.
User avatar
mgarin
Site Admin
 
Posts: 223
Joined: Mon Apr 16, 2012 1:01 pm
Location: Russia, Saint-Petersburg

Re: Feature request and a little component

Postby derdudele » Fri Nov 09, 2012 7:30 pm

I attached a screenshot, you may see why I would love to us your WebToggleButton instead of a combobox. :D
Attachments
tasky.jpg
UI - i'm working on
tasky.jpg (49.62 KiB) Viewed 10765 times
derdudele
 
Posts: 5
Joined: Thu Oct 25, 2012 8:21 pm

Re: Feature request and a little component

Postby mgarin » Fri Nov 09, 2012 10:42 pm

Looks indeed awesome.
Well, i will think about it, but not sure there is good way to do it.
User avatar
mgarin
Site Admin
 
Posts: 223
Joined: Mon Apr 16, 2012 1:01 pm
Location: Russia, Saint-Petersburg

Re: Feature request and a little component

Postby mgarin » Tue Nov 13, 2012 5:08 pm

I have commited WebButtonGroup change for setEnabled method into SVN.
Preview build will be also available on the site in about an hour.

Yet, i cannot say anything specific about the second request.
User avatar
mgarin
Site Admin
 
Posts: 223
Joined: Mon Apr 16, 2012 1:01 pm
Location: Russia, Saint-Petersburg


Return to Suggestions

Who is online

Users browsing this forum: No registered users and 2 guests

cron