~ubuntu-branches/ubuntu/trusty/freeguide/trusty

« back to all changes in this revision

Viewing changes to src/freeguide/plugins/program/freeguide/lib/fgspecific/GrabberController.java

  • Committer: Bazaar Package Importer
  • Author(s): Daniel Watkins
  • Date: 2008-09-07 15:49:32 UTC
  • mfrom: (1.2.6 upstream) (4.1.2 squeeze)
  • Revision ID: james.westby@ubuntu.com-20080907154932-2jvgv76btq068fe0
Tags: 0.10.9-1
* New upstream release. (Closes: #492789)
* Moved package from contrib to main. (Closes: #492544)
* Added lintian override for 'build-depends-without-arch-dep ant', as ant is
  used in the clean target.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
package freeguide.plugins.program.freeguide.lib.fgspecific;
2
 
 
3
 
import freeguide.common.gui.ExecutorDialog;
4
 
 
5
 
import freeguide.common.lib.fgspecific.Application;
6
 
 
7
 
import freeguide.common.plugininterfaces.IModuleGrabber;
8
 
 
9
 
import freeguide.plugins.program.freeguide.FreeGuide;
10
 
import freeguide.plugins.program.freeguide.viewer.MainController;
11
 
 
12
 
import java.awt.event.ActionEvent;
13
 
import java.awt.event.ActionListener;
14
 
import java.awt.event.WindowAdapter;
15
 
import java.awt.event.WindowEvent;
16
 
 
17
 
import java.nio.channels.ClosedByInterruptException;
18
 
 
19
 
import java.util.Iterator;
20
 
import java.util.logging.Level;
21
 
 
22
 
import javax.swing.JButton;
23
 
import javax.swing.JDialog;
24
 
import javax.swing.JFrame;
25
 
import javax.swing.JProgressBar;
26
 
 
27
 
/**
28
 
 * Class for run specified grabber and display progress dialog.
29
 
 *
30
 
 * @author Alex Buloichik (alex73 at zaval.org)
31
 
 */
32
 
public class GrabberController
33
 
{
34
 
    protected ExecutorDialog progressDialog;
35
 
    protected JProgressBar secondProgressBar;
36
 
    protected boolean wasError;
37
 
    protected Thread grabberThread;
38
 
 
39
 
    /**
40
 
     * Show grabber dialog when grabbing running, or start grabbing in
41
 
     * new thread.
42
 
     *
43
 
     * @param controller DOCUMENT ME!
44
 
     */
45
 
    public void activate( final MainController controller )
46
 
    {
47
 
        synchronized( this )
48
 
        {
49
 
            if( progressDialog != null )
50
 
            {
51
 
                // Show dialog
52
 
                progressDialog.bringToForeground(  );
53
 
            }
54
 
            else
55
 
            {
56
 
                // Start new grabbing
57
 
                grabberThread =
58
 
                    new Thread(  )
59
 
                        {
60
 
                            public void run(  )
61
 
                            {
62
 
                                FreeGuide.log
63
 
                                .finest( "start grabbing" );
64
 
 
65
 
                                try
66
 
                                {
67
 
                                    grab( 
68
 
                                        controller.getApplicationFrame(  ),
69
 
                                        controller.mainFrame.getProgressBar(  ),
70
 
                                        controller.mainFrame
71
 
                                        .getForegroundButton(  ) );
72
 
                                    controller.viewer.onDataChanged(  );
73
 
                                    MainController.remindersReschedule(  );
74
 
                                }
75
 
                                catch( Exception ex )
76
 
                                {
77
 
                                    ex.printStackTrace(  );
78
 
                                }
79
 
 
80
 
                                FreeGuide.log.finest( "stop grabbing" );
81
 
                            }
82
 
                        };
83
 
                grabberThread.start(  );
84
 
            }
85
 
        }
86
 
    }
87
 
 
88
 
    /**
89
 
     * DOCUMENT_ME!
90
 
     *
91
 
     * @param owner DOCUMENT_ME!
92
 
     * @param secondProgressBar DOCUMENT ME!
93
 
     * @param foregroundButton DOCUMENT ME!
94
 
     */
95
 
    public void grab( 
96
 
        final JFrame owner, final JProgressBar secondProgressBar,
97
 
        final JButton foregroundButton )
98
 
    {
99
 
        this.secondProgressBar = secondProgressBar;
100
 
 
101
 
        synchronized( this )
102
 
        {
103
 
            wasError = false;
104
 
            progressDialog = new ExecutorDialog( 
105
 
                    owner, secondProgressBar, foregroundButton );
106
 
            progressDialog.setStepCount( 1 );
107
 
            progressDialog.setStepNumber( 0 );
108
 
 
109
 
            progressDialog.getCancelButton(  ).addActionListener( 
110
 
                new ActionListener(  )
111
 
                {
112
 
                    public void actionPerformed( ActionEvent evt )
113
 
                    {
114
 
                        synchronized( GrabberController.this )
115
 
                        {
116
 
                            grabberThread.interrupt(  );
117
 
 
118
 
                            // leave dialog when details open or was error
119
 
                            if( progressDialog != null )
120
 
                            {
121
 
                                progressDialog.dispose(  );
122
 
                                progressDialog = null;
123
 
                            }
124
 
                        }
125
 
                    }
126
 
                } );
127
 
 
128
 
            progressDialog.addWindowListener( 
129
 
                new WindowAdapter(  )
130
 
                {
131
 
                    public void windowClosing( WindowEvent e )
132
 
                    {
133
 
                        if( !grabberThread.isAlive(  ) )
134
 
                        {
135
 
                            if( progressDialog != null )
136
 
                            {
137
 
                                progressDialog.dispose(  );
138
 
                                progressDialog = null;
139
 
                            }
140
 
                        }
141
 
                    }
142
 
                } );
143
 
        }
144
 
 
145
 
        new Thread(  )
146
 
            {
147
 
                public void run(  )
148
 
                {
149
 
                    progressDialog.setVisible( true );
150
 
                }
151
 
            }.start(  );
152
 
 
153
 
        if( MainController.config.activeGrabberIDs.size(  ) == 0 )
154
 
        {
155
 
            wasError = true;
156
 
            progressDialog.showNoGrabberMessage(  );
157
 
 
158
 
        }
159
 
        else
160
 
        {
161
 
            Iterator it = MainController.config.activeGrabberIDs
162
 
                .iterator(  );
163
 
 
164
 
            while( it.hasNext(  ) )
165
 
            {
166
 
                String grabberID = (String)it.next(  );
167
 
 
168
 
                try
169
 
                {
170
 
                    IModuleGrabber grabber =
171
 
                        (IModuleGrabber)PluginsManager.getModuleByID( 
172
 
                            grabberID );
173
 
 
174
 
                    if( grabber == null )
175
 
                    {
176
 
                        FreeGuide.log.warning( 
177
 
                            "There is no grabber " + grabberID );
178
 
 
179
 
                        continue;
180
 
 
181
 
                    }
182
 
 
183
 
                    if( Thread.interrupted(  ) )
184
 
                    {
185
 
                        break;
186
 
                    }
187
 
 
188
 
                    final StoragePipe pipe = new StoragePipe(  );
189
 
 
190
 
                    wasError = !grabber.grabData( 
191
 
                            progressDialog, progressDialog, pipe );
192
 
 
193
 
                    pipe.finish(  );
194
 
 
195
 
                    if( Thread.interrupted(  ) )
196
 
                    {
197
 
                        break;
198
 
                    }
199
 
                }
200
 
                catch( ClosedByInterruptException ex )
201
 
                {
202
 
                    break;
203
 
                }
204
 
                catch( InterruptedException ex )
205
 
                {
206
 
                    break;
207
 
                }
208
 
                catch( Throwable ex )
209
 
                {
210
 
                    wasError = true;
211
 
 
212
 
                    if( progressDialog != null )
213
 
                    {
214
 
                        if( ex instanceof Exception )
215
 
                        {
216
 
                            progressDialog.error( 
217
 
                                "Error grab data by grabber '" + grabberID
218
 
                                + "'", (Exception)ex );
219
 
                        }
220
 
                        else
221
 
                        {
222
 
                            progressDialog.error( 
223
 
                                "Error grab data by grabber '" + grabberID
224
 
                                + "': " + ex.getClass(  ).getName(  ) );
225
 
                        }
226
 
                    }
227
 
 
228
 
                    FreeGuide.log.log( 
229
 
                        Level.WARNING,
230
 
                        "Error grab data by grabber '" + grabberID, ex );
231
 
                }
232
 
            }
233
 
        }
234
 
 
235
 
        synchronized( this )
236
 
        {
237
 
            if( progressDialog != null )
238
 
            {
239
 
                if( !wasError )
240
 
                {
241
 
                    if( !progressDialog.isLogVisible(  ) )
242
 
                    {
243
 
                        progressDialog.dispose(  );
244
 
                        progressDialog = null;
245
 
                    }
246
 
                    else
247
 
                    {
248
 
                        progressDialog.setDefaultCloseOperation( 
249
 
                            JDialog.DISPOSE_ON_CLOSE );
250
 
                        progressDialog.setCloseLabel(  );
251
 
                        progressDialog.setProgressMessage( 
252
 
                            null,
253
 
                            Application.getInstance(  )
254
 
                                       .getLocalizedMessage( 
255
 
                                "ExecutionDialog.Finish.OK" ) );
256
 
                        progressDialog.bringToForeground(  );
257
 
                    }
258
 
                }
259
 
                else
260
 
                {
261
 
                    progressDialog.setDefaultCloseOperation( 
262
 
                        JDialog.DISPOSE_ON_CLOSE );
263
 
                    progressDialog.setCloseLabel(  );
264
 
                    progressDialog.setProgressMessage( 
265
 
                        null,
266
 
                        Application.getInstance(  )
267
 
                                   .getLocalizedMessage( 
268
 
                            "ExecutionDialog.Finish.Error" ) );
269
 
                    progressDialog.bringToForeground(  );
270
 
                    progressDialog.showDetails(  );
271
 
                }
272
 
            }
273
 
        }
274
 
 
275
 
        if( progressDialog != null )
276
 
        {
277
 
            progressDialog.disableBackgroundButton(  );
278
 
        }
279
 
    }
280
 
}