1 /*
2  * JaLingo, http://jalingo.sourceforge.net/
3  *
4  * Copyright (c) 2002-2006 Oleksandr Shyshko
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20 
21 package ja.lingo.application.util.progress;
22 
23 import info.clearthought.layout.TableLayout;
24 import ja.centre.gui.concurrent.EdtWrapper;
25 
26 import javax.swing.*;
27 import java.awt.*;
28 import java.awt.geom.RoundRectangle2D;
29 
30 class ProgressComponent {
31     private JLabel label;
32     private JProgressBar progressBar;
33 
34     private JPanel gui;
35 
36     private ITitledMonitor controller;
37 
ProgressComponent()38     public ProgressComponent() {
39         label = new JLabel();
40 
41         progressBar = new JProgressBar();
42         progressBar.setStringPainted( true );
43 
44         final IMonitor delegate = new ProgressBarMonitor( progressBar );
45 
46         controller = (ITitledMonitor) EdtWrapper.nonWaiting( new ITitledMonitor() {
47             public void setTitle( String title ) {
48                 label.setText( title );
49             }
50             public void setText( String text ) {
51                 progressBar.setString( text );
52             }
53 
54             public void start( int minimum, int maximum ) {
55                 delegate.start( minimum, maximum );
56             }
57             public void update( int value ) {
58                 delegate.update( value );
59             }
60             public void finish() {
61                 delegate.finish();
62             }
63         } );
64 
65         gui = new JPanel() {
66             public Dimension getPreferredSize() {
67                 return new Dimension( 300, (int) super.getPreferredSize().getHeight() );
68             }
69 
70             protected void paintComponent( Graphics g ) {
71                 Graphics2D g2 = (Graphics2D) g;
72 
73                 Rectangle bounds = getBounds();
74 
75                 RoundRectangle2D.Double round = new RoundRectangle2D.Double( 0, 0,
76                         bounds.getWidth() - 1, bounds.getHeight() - 1,
77                         10, 10 );
78 
79                 Object antialiasing = g2.getRenderingHint( RenderingHints.KEY_ANTIALIASING );
80                 g2.setRenderingHint( RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON );
81 
82                 g2.setColor( getBackground() );
83                 g2.fill( round );
84                 g2.setColor( Color.GRAY );
85                 g2.draw( round );
86 
87                 g2.setRenderingHint( RenderingHints.KEY_ANTIALIASING, antialiasing );
88             }
89         };
90 
91         gui.setLayout( new TableLayout( new double[][] {
92                 { TableLayout.FILL },
93                 { TableLayout.PREFERRED, 5, TableLayout.PREFERRED },
94         } ) );
95 
96         gui.add( label, "0, 0" );
97         gui.add( progressBar, "0, 2" );
98 
99         gui.setBorder( BorderFactory.createEmptyBorder( 5, 5, 5, 5 ) );
100         gui.setOpaque( false );
101     }
102 
getGui()103     public JComponent getGui() {
104         return gui;
105     }
106 
getController()107     public ITitledMonitor getController() {
108         return controller;
109     }
110 
main( String[] args )111     public static void main( String[] args ) {
112         ProgressComponent progress = new ProgressComponent();
113 
114         JPanel content = new JPanel( new TableLayout( new double[][] {
115                 { TableLayout.FILL },
116                 { TableLayout.FILL }
117         }) );
118         content.add( progress.getGui(), "0, 0, center, center" );
119         content.setBackground( Color.RED );
120 
121         JFrame frame = new JFrame( "123" );
122         frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
123         frame.setContentPane( content );
124         frame.setSize( 400, 100 );
125         frame.setLocationRelativeTo( null );
126         frame.setVisible( true );
127 
128         progress.getController().setTitle( "123" );
129     }
130 }
131 
132