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