001    // GraphLab Project: http://graphlab.sharif.edu
002    // Copyright (C) 2008 Mathematical Science Department of Sharif University of Technology
003    // Distributed under the terms of the GNU General Public License (GPL): http://www.gnu.org/licenses/
004    package graphlab.ui.components.utils;
005    
006    import graphlab.platform.attribute.NotifiableAttributeSet;
007    import graphlab.platform.extension.Extension;
008    import graphlab.ui.components.gpropertyeditor.GPropertyEditor;
009    
010    import javax.swing.*;
011    import java.awt.*;
012    import java.awt.event.ActionEvent;
013    import java.awt.event.ActionListener;
014    import java.awt.event.KeyAdapter;
015    import java.awt.event.KeyEvent;
016    
017    public class GAttrFrame extends javax.swing.JDialog {
018        /**
019         *
020         */
021        private static final long serialVersionUID = 722213016758300314L;
022        private NotifiableAttributeSet atr;
023    
024        /**
025         * Creates new form GAttrFrame
026         */
027        public GAttrFrame(java.awt.Frame parent, NotifiableAttributeSet atr, boolean modal) {
028            super(parent, modal);
029            this.atr = atr;
030            initComponents();
031        }
032    
033        /**
034         * @return the return status of this dialog - true-> the ok presses, false-> cancelled by user
035         */
036        public boolean getReturnStatus() {
037            return status;
038        }
039    
040        GPropertyEditor table;
041    
042        private void initComponents() {
043            setAlwaysOnTop(true);
044            JPanel buttonPanel = new JPanel();
045            JButton cancelButton = new JButton("cancel");
046            table = new GPropertyEditor();
047            table.connect(atr);
048            JButton okButton = new JButton("ok");
049            setDefaultCloseOperation(HIDE_ON_CLOSE);
050    
051            addWindowListener(new java.awt.event.WindowAdapter() {
052                public void windowClosing(java.awt.event.WindowEvent evt) {
053                    closeDialog();
054                }
055            });
056    
057    
058            okButton.addActionListener(new ActionListener() {
059                public void actionPerformed(ActionEvent evt) {
060                    finished(true);
061                }
062            });
063            cancelButton.addActionListener(new ActionListener() {
064                public void actionPerformed(ActionEvent evt) {
065                    finished(false);
066                }
067            });
068    
069            okButton.addKeyListener(new KeyAdapter() {
070                public void keyTyped(KeyEvent e) {
071                    if (e.getKeyCode() == KeyEvent.VK_ENTER) {
072                        finished(true);
073                    }
074                }
075            });
076            buttonPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
077            buttonPanel.add(okButton);
078            buttonPanel.add(cancelButton);
079            add(buttonPanel, java.awt.BorderLayout.SOUTH);
080            add(table, java.awt.BorderLayout.CENTER);
081            okButton.setSelected(true);
082            validate();
083            okButton.setSelected(true);
084            pack();
085            okButton.grabFocus();
086        }
087    
088        private boolean status = false;
089        private boolean finished = false;
090    
091        private void closeDialog() {
092            finished = true;
093        }
094    
095        private void finished(boolean status) {
096            this.status = status;
097            if (status)
098                if (table.getTable().isEditing()) {
099                    table.getTable().getCellEditor().stopCellEditing();
100                }
101            closeDialog();
102            dispose();
103        }
104    
105        public static GAttrFrame showEditDialog(NotifiableAttributeSet input) {
106            return showEditDialog(input, true);
107        }
108    
109        /**
110         * Shows a Property editor to edit the attributes in the input.
111         * the modal is like the modal in JDialog
112         */
113        public static GAttrFrame showEditDialog(NotifiableAttributeSet input, boolean modal) {
114            String title = "Edit Attributes";
115            if (input instanceof Extension) {
116                title = ((Extension) input).getName();
117            }
118            GAttrFrame gAttrFrame = new GAttrFrame(new JFrame(), input, modal);
119            gAttrFrame.setTitle(title);
120            gAttrFrame.setVisible(true);
121            return gAttrFrame;
122        }
123    
124        /**
125         * return the GProertyEditor which is the main editor of NotifiableAttributeSet
126         */
127        public GPropertyEditor getPropertyEditor() {
128            return table;
129        }
130    }