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.gpropertyeditor.editors;
005    
006    import graphlab.ui.components.gpropertyeditor.EditingFinishedListener;
007    import graphlab.ui.components.gpropertyeditor.GBasicCellEditor;
008    import graphlab.ui.components.gpropertyeditor.GCellRenderer;
009    
010    import javax.swing.*;
011    import javax.swing.border.SoftBevelBorder;
012    import java.awt.*;
013    import static java.awt.GridBagConstraints.CENTER;
014    import static java.awt.GridBagConstraints.HORIZONTAL;
015    import java.awt.event.ActionEvent;
016    import java.awt.event.ActionListener;
017    
018    
019    /**
020     * User: root
021     */
022    public abstract class GDialogEditor<t> implements GBasicCellEditor, ActionListener {
023        EditingFinishedListener listener;
024    
025    
026        public void setEditingFinishedListener(EditingFinishedListener listener) {
027            this.listener = listener;
028        }
029    
030        JDialog d;
031        JButton ok, reset, cancel;
032        JPanel body;
033        t _value;
034    
035        public Component getEditorComponent(Object value) {
036            _value = (t) value;
037            d = new JDialog();
038            d.setAlwaysOnTop(true);
039            d.setModal(true);
040            //btm is the bottom of the editor where the ok/reset/cancel buttons placed there
041            JPanel btm = new JPanel();
042            //body is the body of editor which will be filled by a component that will be given from abstract method (JComponent getEditorComponent)
043            body = new JPanel();
044            //the buttons which will be put on btm
045            ok = new JButton("ok");
046            reset = new JButton("reset");
047            cancel = new JButton("cancel");
048    
049            ok.addActionListener(this);
050            reset.addActionListener(this);
051            cancel.addActionListener(this);
052    
053            btm.add(ok);
054            btm.add(reset);
055            btm.add(cancel);
056    
057            //filling the body
058            body.setLayout(new BorderLayout(2, 2));
059            body.add(getComponent(_value));
060            body.setBorder(new SoftBevelBorder(0));
061    
062            d.setLayout(new GridBagLayout());
063    
064            GridBagConstraints gbcc = new GridBagConstraints(0, 1, 2, 1, 1, 0, CENTER, HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0);
065            d.getContentPane().add(body, gbcc);
066    
067            GridBagConstraints gbc = new GridBagConstraints(0, 2, 2, 1, 1, 0, CENTER, HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0);
068            d.getContentPane().add(btm, gbc);
069    
070    
071            d.validate();
072            d.pack();
073            new Thread() {
074                public void run() {
075                    d.setVisible(true);
076                }
077            }.start();
078            return GCellRenderer.getRendererFor(value);
079        }
080    
081        public void cancelEditing() {
082            d.setVisible(false);
083        }
084    
085        abstract public JComponent getComponent(t initialValue);
086    
087        abstract public t getEditorValue();
088    
089        abstract public void setEditorValue(t value);
090    
091        public void actionPerformed(ActionEvent e) {
092            JButton src = (JButton) e.getSource();
093            if (src == ok) {
094                listener.editingFinished(getEditorValue());
095                d.setVisible(false);
096            }
097            if (src == cancel) {
098                listener.editingFinished(_value);
099                d.setVisible(false);
100            }
101            if (src == reset) {
102                setEditorValue(_value);
103            }
104        }
105    }