KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > directory > ldapstudio > browser > core > jobs > OpenConnectionsJob


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  *
19  */

20
21 package org.apache.directory.ldapstudio.browser.core.jobs;
22
23
24 import java.util.ArrayList JavaDoc;
25 import java.util.Arrays JavaDoc;
26 import java.util.List JavaDoc;
27
28 import org.apache.directory.ldapstudio.browser.core.BrowserCoreMessages;
29 import org.apache.directory.ldapstudio.browser.core.events.ConnectionUpdateEvent;
30 import org.apache.directory.ldapstudio.browser.core.events.EventRegistry;
31 import org.apache.directory.ldapstudio.browser.core.model.IConnection;
32
33
34 public class OpenConnectionsJob extends AbstractAsyncBulkJob
35 {
36
37     private IConnection[] connections;
38
39
40     public OpenConnectionsJob( IConnection connection )
41     {
42         this( new IConnection[]
43             { connection } );
44     }
45
46
47     public OpenConnectionsJob( IConnection[] connections )
48     {
49         this.connections = connections;
50         setName( connections.length == 1 ? BrowserCoreMessages.jobs__open_connections_name_1
51             : BrowserCoreMessages.jobs__open_connections_name_n );
52     }
53
54
55     protected IConnection[] getConnections()
56     {
57         return new IConnection[0];
58     }
59
60
61     protected Object JavaDoc[] getLockedObjects()
62     {
63         List JavaDoc l = new ArrayList JavaDoc();
64         l.addAll( Arrays.asList( connections ) );
65         return l.toArray();
66     }
67
68
69     protected String JavaDoc getErrorMessage()
70     {
71         return connections.length == 1 ? BrowserCoreMessages.jobs__open_connections_error_1
72             : BrowserCoreMessages.jobs__open_connections_error_n;
73     }
74
75
76     protected void executeBulkJob( ExtendedProgressMonitor monitor )
77     {
78
79         monitor.beginTask( " ", connections.length * 6 + 1 ); //$NON-NLS-1$
80
monitor.reportProgress( " " ); //$NON-NLS-1$
81

82         for ( int i = 0; i < connections.length; i++ )
83         {
84             if ( connections[i].canOpen() )
85             {
86
87                 monitor.setTaskName( BrowserCoreMessages.bind( BrowserCoreMessages.jobs__open_connections_task,
88                     new String JavaDoc[]
89                         { this.connections[i].getName() } ) );
90                 monitor.worked( 1 );
91
92                 connections[i].open( monitor );
93             }
94         }
95     }
96
97
98     protected void runNotification()
99     {
100         for ( int i = 0; i < connections.length; i++ )
101         {
102             if ( connections[i].isOpened() )
103             {
104                 EventRegistry.fireConnectionUpdated( new ConnectionUpdateEvent( connections[i],
105                     ConnectionUpdateEvent.EventDetail.CONNECTION_OPENED ), this );
106             }
107             else
108             {
109                 EventRegistry.fireConnectionUpdated( new ConnectionUpdateEvent( connections[i],
110                     ConnectionUpdateEvent.EventDetail.CONNECTION_CLOSED ), this );
111             }
112         }
113     }
114
115 }
116
Popular Tags