View Javadoc

1   /*
2    *   Copyright 2004 The Apache Software Foundation
3    *
4    *   Licensed under the Apache License, Version 2.0 (the "License");
5    *   you may not use this file except in compliance with the License.
6    *   You may obtain a copy of the License at
7    *
8    *       http://www.apache.org/licenses/LICENSE-2.0
9    *
10   *   Unless required by applicable law or agreed to in writing, software
11   *   distributed under the License is distributed on an "AS IS" BASIS,
12   *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *   See the License for the specific language governing permissions and
14   *   limitations under the License.
15   *
16   */
17  package org.apache.ldap.server.db.gui;
18  
19  
20  import org.apache.ldap.common.message.LockableAttributesImpl;
21  
22  import javax.naming.directory.Attributes;
23  import javax.swing.*;
24  import java.awt.*;
25  import java.awt.event.ActionEvent;
26  import java.awt.event.ActionListener;
27  import java.awt.event.MouseAdapter;
28  import java.awt.event.MouseEvent;
29  
30  
31  /***
32   * Allows for operations on entries.
33   * 
34   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
35   * @version $Rev: 159259 $
36   */
37  public class AddEntryDialog extends JDialog implements ActionListener
38  {
39      private static final long serialVersionUID = 3544671793504663604L;
40  
41      private JPanel m_namePnl = new JPanel();
42      private JPanel m_attrPnl = new JPanel();
43      private JPanel m_buttonPnl = new JPanel();
44      private JPanel m_rdnPnl = new JPanel();
45      private JPanel m_dnPnl = new JPanel();
46      private JLabel m_rdnLbl = new JLabel();
47      private JComboBox m_rdnChoice = new JComboBox();
48      private JTextField m_dnText = new JTextField();
49      private JScrollPane m_attrScrollPnl = new JScrollPane();
50      private JTable m_attrTbl = new JTable();
51      private JButton m_doneBut = new JButton();
52      private JButton m_cancelBut = new JButton();
53      private JPopupMenu m_popup;
54  
55      private Attributes m_childEntry = new LockableAttributesImpl();
56  
57  
58      /***
59       * Creates new entry addition dialog.
60       *  
61       * @param parent the parent frame
62       * @param modal whether or not to go modal on the dialog
63       */
64      public AddEntryDialog( Frame parent, boolean modal )
65      {
66          super( parent, modal );
67          m_childEntry.put( "objectClass", "top" );
68          initGUI();
69      }
70  
71  
72      /*** 
73       * This method is called from within the constructor to initialize the form.
74       */
75      private void initGUI()
76      {
77          addWindowListener( new java.awt.event.WindowAdapter(  )
78          {
79              public void windowClosing( java.awt.event.WindowEvent evt )
80              {
81                  closeDialog( );
82              }
83          } );
84          pack(  );
85          setBounds( new java.awt.Rectangle( 0, 0, 447, 364 ) );
86          setTitle( "Add New Entry" );
87          getContentPane(  ).setLayout( new java.awt.GridBagLayout(  ) );
88          getContentPane(  ).add( 
89              m_namePnl,
90              new java.awt.GridBagConstraints( 
91                  0,
92                  0,
93                  1,
94                  1,
95                  1.0,
96                  0.0,
97                  java.awt.GridBagConstraints.NORTH,
98                  java.awt.GridBagConstraints.HORIZONTAL,
99                  new java.awt.Insets( 5, 5, 5, 5 ),
100                 0,
101                 0 ) );
102         getContentPane(  ).add( 
103             m_attrPnl,
104             new java.awt.GridBagConstraints( 
105                 0,
106                 1,
107                 1,
108                 1,
109                 1.0,
110                 1.0,
111                 java.awt.GridBagConstraints.CENTER,
112                 java.awt.GridBagConstraints.BOTH,
113                 new java.awt.Insets( 5, 5, 5, 5 ),
114                 0,
115                 0 ) );
116         getContentPane(  ).add( 
117             m_buttonPnl,
118             new java.awt.GridBagConstraints( 
119                 0,
120                 2,
121                 1,
122                 1,
123                 1.0,
124                 0.05,
125                 java.awt.GridBagConstraints.CENTER,
126                 java.awt.GridBagConstraints.HORIZONTAL,
127                 new java.awt.Insets( 0, 0, 0, 20 ),
128                 0,
129                 0 ) );
130         m_namePnl.setBorder( 
131             javax.swing.BorderFactory.createTitledBorder( 
132                 javax.swing.BorderFactory.createLineBorder( 
133                     new java.awt.Color( 153, 153, 153 ),
134                     1 ),
135                 "Naming",
136                 javax.swing.border.TitledBorder.LEADING,
137                 javax.swing.border.TitledBorder.TOP,
138                 new java.awt.Font( "SansSerif", 0, 14 ),
139                 new java.awt.Color( 60, 60, 60 ) ) );
140         m_namePnl.setLayout( 
141             new javax.swing.BoxLayout( m_namePnl, javax.swing.BoxLayout.Y_AXIS ) );
142         m_namePnl.add( m_rdnPnl );
143         m_namePnl.add( m_dnPnl );
144         m_rdnLbl.setText( "Rdn:" );
145         m_rdnPnl.setLayout( new java.awt.GridBagLayout() );
146         m_rdnPnl.add( 
147             m_rdnChoice,
148             new java.awt.GridBagConstraints( 
149                 1,
150                 0,
151                 1,
152                 1,
153                 1.0,
154                 0.0,
155                 java.awt.GridBagConstraints.WEST,
156                 java.awt.GridBagConstraints.NONE,
157                 new java.awt.Insets( 0, 10, 0, 0 ),
158                 0,
159                 0 ) );
160         m_rdnPnl.add( 
161             m_rdnLbl,
162             new java.awt.GridBagConstraints( 
163                 0,
164                 0,
165                 1,
166                 1,
167                 0.0,
168                 0.0,
169                 java.awt.GridBagConstraints.WEST,
170                 java.awt.GridBagConstraints.NONE,
171                 new java.awt.Insets( 0, 10, 0, 0 ),
172                 0,
173                 0 ) );
174         m_dnPnl.setLayout( new java.awt.GridBagLayout(  ) );
175         m_dnPnl.add( 
176             m_dnText,
177             new java.awt.GridBagConstraints( 
178                 1,
179                 0,
180                 1,
181                 1,
182                 1.0,
183                 0.0,
184                 java.awt.GridBagConstraints.WEST,
185                 java.awt.GridBagConstraints.HORIZONTAL,
186                 new java.awt.Insets( 0, 5, 0, 0 ),
187                 0,
188                 0 ) );
189         m_dnText.setText( "unknown" );
190         m_dnText.setEditable( false );
191         m_dnText.setBorder( 
192             javax.swing.BorderFactory.createTitledBorder( 
193                 javax.swing.BorderFactory.createLineBorder( 
194                     new java.awt.Color( 153, 153, 153 ),
195                     1 ),
196                 "Dn",
197                 javax.swing.border.TitledBorder.LEADING,
198                 javax.swing.border.TitledBorder.TOP,
199                 new java.awt.Font( "SansSerif", 0, 14 ),
200                 new java.awt.Color( 60, 60, 60 ) ) );
201         m_rdnChoice.setEditable( true );
202         m_rdnChoice.setMaximumRowCount( 6 );
203         
204         m_rdnChoice.setSize( new java.awt.Dimension( 130, 24 ) );
205         m_attrPnl.setLayout( new java.awt.BorderLayout(  ) );
206         m_attrPnl.add( m_attrScrollPnl, java.awt.BorderLayout.CENTER );
207         m_attrScrollPnl.getViewport(  ).add( m_attrTbl );
208         m_attrTbl.setBounds( new java.awt.Rectangle( 78, 60, 32, 32 ) );
209         m_attrTbl.setCellSelectionEnabled( true );
210         
211         m_doneBut.setText( "Done" );
212         m_buttonPnl.setLayout( 
213             new java.awt.FlowLayout( java.awt.FlowLayout.RIGHT, 10, 5 ) );
214         m_buttonPnl.add( m_doneBut );
215         m_buttonPnl.add( m_cancelBut );
216         m_cancelBut.setText( "Cancel" );
217         m_cancelBut.addActionListener( new ActionListener () 
218         {
219             public void actionPerformed( ActionEvent a_evt )
220             {
221                 closeDialog();
222             }
223         } );
224         m_attrScrollPnl.setBorder( 
225             javax.swing.BorderFactory.createTitledBorder( 
226                 javax.swing.BorderFactory.createLineBorder( 
227                     new java.awt.Color( 153, 153, 153 ),
228                     1 ),
229                 "Attributes",
230                 javax.swing.border.TitledBorder.LEADING,
231                 javax.swing.border.TitledBorder.TOP,
232                 new java.awt.Font( "SansSerif", 0, 14 ),
233                 new java.awt.Color( 60, 60, 60 ) ) );
234 
235         m_attrTbl.setModel( new AttributesTableModel( m_childEntry, 
236             null, null, true ) );
237     
238         //
239         // Build the table's popup menu
240         //
241 
242         m_popup = new JPopupMenu();
243         JMenuItem l_menuItem = new JMenuItem( "Add" );
244         l_menuItem.setActionCommand( "Add" );
245         l_menuItem.addActionListener( this );
246         m_popup.add( l_menuItem );
247         l_menuItem = new JMenuItem( "Delete" );
248         l_menuItem.setActionCommand( "Delete" );
249         l_menuItem.addActionListener( this );
250         m_popup.add( l_menuItem );
251     
252         // Add listener to components that can bring up popup menus.
253         m_attrTbl.addMouseListener( new PopupListener() );
254         
255         setUpEditor( m_attrTbl );
256     }
257 
258 
259     private void setUpEditor( JTable l_table ) 
260     {
261         //Set up the editor for the integer cells.
262         final JTextField l_textField = new JTextField();
263 
264         
265         DefaultCellEditor l_textEditor = 
266             new DefaultCellEditor( l_textField ) 
267         {
268             private static final long serialVersionUID = 3256727286014554675L;
269 
270             //Override DefaultCellEditor's getCellEditorValue method
271             //to return an Integer, not a String:
272             public Object getCellEditorValue() 
273             {
274                 System.out.println( "Editor returning '" + l_textField.getText()
275                     + "'" );
276                 return l_textField.getText();
277             }
278         };
279         
280         l_table.setDefaultEditor( String.class, l_textEditor );
281     }
282 
283 
284 
285     class PopupListener extends MouseAdapter 
286     {
287         public void mousePressed( MouseEvent e ) 
288         {
289             maybeShowPopup( e );
290         }
291 
292         
293         public void mouseReleased( MouseEvent e ) 
294         {
295             maybeShowPopup( e );
296         }
297 
298       
299         private void maybeShowPopup( MouseEvent e ) 
300         {
301             if ( e.isPopupTrigger() ) 
302             {
303                 m_popup.show( e.getComponent(), e.getX(), e.getY() );
304             }
305         }
306     }
307 
308 
309     public void actionPerformed( ActionEvent a_event )
310     {
311         String l_cmd = a_event.getActionCommand();
312         AttributesTableModel l_model = ( AttributesTableModel ) 
313             m_attrTbl.getModel();
314         int l_row = m_attrTbl.getSelectedRow();
315         System.out.println( l_cmd );
316         
317         if ( l_row >= l_model.getRowCount() || l_row < 0 )
318         {
319             JOptionPane.showMessageDialog( this, 
320                 "Row needs to be selected to apply operation" );
321         }
322         
323         if ( l_cmd.equals( "Add" ) )
324         {
325              l_model.insert( l_row, "xxxx", "xxxx" );
326         }
327         else if ( l_cmd.equals( "Delete" ) )
328         {
329             l_model.delete( l_row );
330         }
331         else 
332         {
333             JOptionPane.showMessageDialog( this, 
334                 "Unrecognized action - abandoning action processing." );
335         }
336     }
337 
338 
339     /*** Closes the dialog */
340     private void closeDialog()
341     {
342         setVisible( false );
343         dispose();
344     }
345 
346 
347     public void setParentDn( String dn )
348     {
349         m_dnText.setText( dn );
350     }
351     
352     
353     public Attributes getChildEntry()
354     {
355         return m_childEntry;
356     }
357     
358     
359     public String getChildDn()
360     {
361         return m_dnText.getText();
362     }
363 }