View Javadoc

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  package org.apache.directory.server.ldap.gui;
21  
22  
23  import java.awt.BorderLayout;
24  import java.net.InetSocketAddress;
25  import java.util.Map;
26  
27  import javax.swing.JFrame;
28  import javax.swing.JPanel;
29  import javax.swing.JDialog;
30  import javax.swing.JScrollPane;
31  import javax.swing.JTable;
32  import javax.swing.JButton;
33  
34  import org.apache.directory.server.ldap.LdapService;
35  import org.apache.directory.server.ldap.LdapSession;
36  import org.apache.directory.shared.ldap.message.AbandonableRequest;
37  import javax.swing.JTextArea;
38  import javax.swing.event.ListSelectionEvent;
39  import javax.swing.event.ListSelectionListener;
40  
41  
42  public class OutstandingRequestsDialog extends JDialog
43  {
44      private static final long serialVersionUID = -3777123348215825711L;
45      private static final AbandonableRequest[] EMPTY_REQUEST_ARRAY = new AbandonableRequest[0];
46      private JPanel jContentPane;
47      private JPanel jPanel;
48      private JScrollPane jScrollPane;
49      private JTable jTable;
50      private JPanel jPanel1;
51      private JButton jButton;
52  
53      final LdapSession session;
54      final LdapService ldapService;
55  
56      private JPanel jPanel2;
57      private JTextArea jTextArea;
58      private JButton jButton1;
59      private JButton jButton2;
60  
61  
62      /**
63       * This is the default constructor
64       * @param owner the owning frame
65       * @param session the MINA IoSession to get outstanding requests for
66       * @param sessionRegistry the session registry
67       */
68      public OutstandingRequestsDialog( JFrame owner, LdapSession session, LdapService ldapService )
69      {
70          super( owner, true );
71          this.session = session;
72          this.ldapService = ldapService;
73  
74          StringBuffer buf = new StringBuffer();
75          buf.append( "Outstanding Requests: " );
76          buf.append( ( ( InetSocketAddress ) session.getIoSession().getRemoteAddress() ).getHostName() );
77          buf.append( ":" );
78          buf.append( ( ( InetSocketAddress ) session.getIoSession().getRemoteAddress() ).getPort() );
79          setTitle( buf.toString() );
80          initialize();
81      }
82  
83  
84      /**
85       * This method initializes this
86       */
87      private void initialize()
88      {
89          this.setSize( 549, 341 );
90          this.setContentPane( getJContentPane() );
91      }
92  
93  
94      /**
95       * This method initializes jContentPane
96       * 
97       * @return javax.swing.JPanel
98       */
99      private JPanel getJContentPane()
100     {
101         if ( jContentPane == null )
102         {
103             jContentPane = new JPanel();
104             jContentPane.setLayout( new BorderLayout() );
105             jContentPane.add( getJPanel(), java.awt.BorderLayout.CENTER );
106         }
107         return jContentPane;
108     }
109 
110 
111     /**
112      * This method initializes jPanel    
113      *     
114      * @return javax.swing.JPanel    
115      */
116     private JPanel getJPanel()
117     {
118         if ( jPanel == null )
119         {
120             jPanel = new JPanel();
121             jPanel.setLayout( new BorderLayout() );
122             jPanel.add( getJScrollPane(), java.awt.BorderLayout.CENTER );
123             jPanel.add( getJPanel1(), java.awt.BorderLayout.SOUTH );
124             jPanel.add( getJPanel2(), java.awt.BorderLayout.NORTH );
125         }
126         return jPanel;
127     }
128 
129 
130     /**
131      * This method initializes jScrollPane    
132      *     
133      * @return javax.swing.JScrollPane    
134      */
135     private JScrollPane getJScrollPane()
136     {
137         if ( jScrollPane == null )
138         {
139             jScrollPane = new JScrollPane();
140             jScrollPane.setViewportView( getJTable() );
141         }
142         return jScrollPane;
143     }
144 
145 
146     /**
147      * This method initializes jTable    
148      *     
149      * @return javax.swing.JTable    
150      */
151     private JTable getJTable()
152     {
153         if ( jTable == null )
154         {
155             jTable = new JTable();
156         }
157 
158         setRequestsModel();
159         jTable.getSelectionModel().addListSelectionListener( new ListSelectionListener()
160         {
161             public void valueChanged( ListSelectionEvent e )
162             {
163                 int row = jTable.getSelectedRow();
164                 if ( row > -1 )
165                 {
166                     jButton2.setEnabled( true );
167                     AbandonableRequest req = ( ( OutstandingRequestsModel ) jTable.getModel() )
168                         .getAbandonableRequest( row );
169                     jTextArea.setText( req.toString() );
170                     jTextArea.setEnabled( true );
171                 }
172                 else
173                 {
174                     jButton2.setEnabled( false );
175                     jTextArea.setText( "" );
176                     jTextArea.setEnabled( false );
177                 }
178             }
179         } );
180         return jTable;
181     }
182 
183 
184     private void setRequestsModel()
185     {
186         AbandonableRequest[] requests;
187         Map<Integer, AbandonableRequest> reqsMap = session.getOutstandingRequests();
188         
189         if ( reqsMap != null )
190         {
191             requests = new AbandonableRequest[reqsMap.size()];
192             //noinspection unchecked
193             requests = ( AbandonableRequest[] ) reqsMap.values().toArray( requests );
194         }
195         else
196         {
197             requests = EMPTY_REQUEST_ARRAY;
198         }
199 
200         jTable.setModel( new OutstandingRequestsModel( requests ) );
201     }
202 
203 
204     /**
205      * This method initializes jPanel1    
206      *     
207      * @return javax.swing.JPanel    
208      */
209     private JPanel getJPanel1()
210     {
211         if ( jPanel1 == null )
212         {
213             jPanel1 = new JPanel();
214             jPanel1.add( getJButton(), null );
215             jPanel1.add( getJButton1(), null );
216         }
217         return jPanel1;
218     }
219 
220 
221     /**
222      * This method initializes jButton    
223      *     
224      * @return javax.swing.JButton    
225      */
226     private JButton getJButton()
227     {
228         if ( jButton == null )
229         {
230             jButton = new JButton();
231             jButton.setText( "Done" );
232             jButton.addActionListener( new java.awt.event.ActionListener()
233             {
234                 public void actionPerformed( java.awt.event.ActionEvent e )
235                 {
236                     OutstandingRequestsDialog.this.setVisible( false );
237                     OutstandingRequestsDialog.this.dispose();
238                 }
239             } );
240         }
241         return jButton;
242     }
243 
244 
245     /**
246      * This method initializes jPanel2    
247      *     
248      * @return javax.swing.JPanel    
249      */
250     private JPanel getJPanel2()
251     {
252         if ( jPanel2 == null )
253         {
254             jPanel2 = new JPanel();
255             jPanel2.setLayout( new BorderLayout() );
256             jPanel2.setBorder( javax.swing.BorderFactory.createTitledBorder( null, "Request",
257                 javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION,
258                 javax.swing.border.TitledBorder.DEFAULT_POSITION, null, null ) );
259             jPanel2.add( getJButton2(), java.awt.BorderLayout.WEST );
260             jPanel2.add( getJTextArea(), java.awt.BorderLayout.CENTER );
261         }
262         return jPanel2;
263     }
264 
265 
266     /**
267      * This method initializes jTextArea    
268      *     
269      * @return javax.swing.JTextArea    
270      */
271     private JTextArea getJTextArea()
272     {
273         if ( jTextArea == null )
274         {
275             jTextArea = new JTextArea();
276         }
277 
278         jTextArea.setEnabled( false );
279         jTextArea.setEditable( false );
280         return jTextArea;
281     }
282 
283 
284     /**
285      * This method initializes jButton1    
286      *     
287      * @return javax.swing.JButton    
288      */
289     private JButton getJButton1()
290     {
291         if ( jButton1 == null )
292         {
293             jButton1 = new JButton();
294             jButton1.setText( "Refresh" );
295             jButton1.addActionListener( new java.awt.event.ActionListener()
296             {
297                 public void actionPerformed( java.awt.event.ActionEvent e )
298                 {
299                     setRequestsModel();
300                     jTextArea.setText( "" );
301                     jTextArea.setEnabled( false );
302                     jButton2.setEnabled( false );
303                 }
304             } );
305         }
306         return jButton1;
307     }
308 
309 
310     /**
311      * This method initializes jButton2    
312      *     
313      * @return javax.swing.JButton    
314      */
315     private JButton getJButton2()
316     {
317         if ( jButton2 == null )
318         {
319             jButton2 = new JButton();
320             jButton2.setText( "Abandon" );
321             jButton2.setEnabled( false );
322             jButton2.addActionListener( new java.awt.event.ActionListener()
323             {
324                 public void actionPerformed( java.awt.event.ActionEvent e )
325                 {
326                     int row = jTable.getSelectedRow();
327                     AbandonableRequest req = ( ( OutstandingRequestsModel ) jTable.getModel() )
328                         .getAbandonableRequest( row );
329                     req.abandon();
330                     session.abandonOutstandingRequest( req.getMessageId() );
331                     setRequestsModel();
332                 }
333             } );
334         }
335         return jButton2;
336     }
337 } //  @jve:decl-index=0:visual-constraint="10,10"