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