001    /*
002     *  Licensed to the Apache Software Foundation (ASF) under one
003     *  or more contributor license agreements.  See the NOTICE file
004     *  distributed with this work for additional information
005     *  regarding copyright ownership.  The ASF licenses this file
006     *  to you under the Apache License, Version 2.0 (the
007     *  "License"); you may not use this file except in compliance
008     *  with the License.  You may obtain a copy of the License at
009     *  
010     *    http://www.apache.org/licenses/LICENSE-2.0
011     *  
012     *  Unless required by applicable law or agreed to in writing,
013     *  software distributed under the License is distributed on an
014     *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015     *  KIND, either express or implied.  See the License for the
016     *  specific language governing permissions and limitations
017     *  under the License. 
018     *  
019     */
020    package org.apache.directory.server.core.partition.impl.btree.gui;
021    
022    
023    import java.awt.Frame;
024    import java.awt.event.ActionEvent;
025    import java.awt.event.ActionListener;
026    import java.awt.event.MouseAdapter;
027    import java.awt.event.MouseEvent;
028    
029    import javax.swing.DefaultCellEditor;
030    import javax.swing.JButton;
031    import javax.swing.JComboBox;
032    import javax.swing.JDialog;
033    import javax.swing.JLabel;
034    import javax.swing.JMenuItem;
035    import javax.swing.JOptionPane;
036    import javax.swing.JPanel;
037    import javax.swing.JPopupMenu;
038    import javax.swing.JScrollPane;
039    import javax.swing.JTable;
040    import javax.swing.JTextField;
041    
042    import org.apache.directory.shared.ldap.constants.SchemaConstants;
043    import org.apache.directory.shared.ldap.entry.DefaultServerEntry;
044    import org.apache.directory.shared.ldap.entry.ServerEntry;
045    import org.apache.directory.shared.ldap.schema.SchemaManager;
046    import org.slf4j.Logger;
047    import org.slf4j.LoggerFactory;
048    
049    
050    /**
051     * Allows for operations on entries.
052     * 
053     * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
054     * @version $Rev: 927404 $
055     */
056    public class AddEntryDialog extends JDialog implements ActionListener
057    {
058        private static final Logger log = LoggerFactory.getLogger( AddEntryDialog.class );
059    
060        private static final long serialVersionUID = 3544671793504663604L;
061    
062        private JPanel m_namePnl = new JPanel();
063        private JPanel m_attrPnl = new JPanel();
064        private JPanel m_buttonPnl = new JPanel();
065        private JPanel m_rdnPnl = new JPanel();
066        private JPanel m_dnPnl = new JPanel();
067        private JLabel m_rdnLbl = new JLabel();
068        private JComboBox m_rdnChoice = new JComboBox();
069        private JTextField m_dnText = new JTextField();
070        private JScrollPane m_attrScrollPnl = new JScrollPane();
071        private JTable m_attrTbl = new JTable();
072        private JButton m_doneBut = new JButton();
073        private JButton m_cancelBut = new JButton();
074        private JPopupMenu m_popup;
075    
076        private ServerEntry childEntry = null;
077    
078    
079        /**
080         * Creates new entry addition dialog.
081         *  
082         * @param parent the parent frame
083         * @param modal whether or not to go modal on the dialog
084         */
085        public AddEntryDialog(Frame parent, boolean modal, SchemaManager schemaManager )
086        {
087            super( parent, modal );
088            childEntry = new DefaultServerEntry( schemaManager );
089            childEntry.put( SchemaConstants.OBJECT_CLASS_AT, SchemaConstants.TOP_OC );
090            initGUI();
091        }
092    
093    
094        /** 
095         * This method is called from within the constructor to initialize the form.
096         */
097        private void initGUI()
098        {
099            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    }