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    
005    package graphlab.ui.components.prefeditor;
006    
007    import graphlab.platform.core.BlackBoard;
008    import graphlab.platform.preferences.AbstractPreference;
009    import graphlab.ui.components.GFrame;
010    import graphlab.ui.components.gpropertyeditor.GPropertyEditor;
011    import graphlab.ui.components.utils.GFrameLocationProvider;
012    
013    import javax.swing.*;
014    import java.awt.event.ActionEvent;
015    import java.awt.event.ActionListener;
016    import java.util.HashMap;
017    import java.util.Vector;
018    
019    /**
020     * @author Rouzbeh Ebrahimi
021     * Email: ruzbehus@gmail.com
022     */
023    
024    public class GPrefPane extends GFrame {
025        // Variables declaration - do not modify
026        public HashMap<String, AbstractPreference> tabs;
027        private javax.swing.JList list;
028        private javax.swing.JScrollPane scrollPane;
029        private GPropertyEditor tabPane;
030        private JButton ok;
031        private JButton cancel;
032        private JButton apply;
033        private JLabel label;
034    // End of variables declaration
035    
036        /**
037         * Creates new form prefForm
038         */
039        public GPrefPane(BlackBoard bb, HashMap<String, AbstractPreference> items) {
040            super(bb);
041            java.awt.GridBagConstraints gridBagConstraints;
042            tabs = items;
043            tabPane = new GPropertyEditor();
044            scrollPane = new javax.swing.JScrollPane();
045            list = new JList();
046            ok = new JButton("Ok");
047            ok.addActionListener(new ActionListener() {
048                public void actionPerformed(ActionEvent e) {
049                    finished(true);
050    
051                }
052            });
053    
054            cancel = new JButton("Cancel");
055            cancel.addActionListener(new ActionListener() {
056                public void actionPerformed(ActionEvent e) {
057                    closeDialog();
058                }
059            });
060            apply = new JButton("Apply");
061            label = new JLabel("Preferences:");
062    
063            setLayout(new java.awt.GridBagLayout());
064    
065    //        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
066            setTab();
067            Vector<String> refined = new Vector<String>();
068            for (String s : items.keySet()) {
069                if (s != "Only Storable") {
070                    refined.add(s);
071                }
072            }
073            final String[] strs = refined.toArray(new String[refined.size()]);
074    
075            list.setModel(new javax.swing.AbstractListModel() {
076                String[] strings = strs;
077    
078                public int getSize() {
079                    return strings.length;
080                }
081    
082                public Object getElementAt(int i) {
083                    return strings[i];
084                }
085            });
086            list.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
087                public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
088                    jList1ValueChanged(evt);
089                }
090            });
091    
092            scrollPane.setViewportView(list);
093    
094            setList();
095            setCancel();
096    //        setApply();
097            setOk();
098    //        setLabel();
099    
100            String s = (String) list.getModel().getElementAt(0);
101            AbstractPreference ap = items.get(s);
102    //        GPropertyEditor gp = new GPropertyEditor();
103    //        gp.connect(ap.attributeSet);
104    //        gp.setVisible(true);
105            tabPane.connect(ap.attributeSet);
106    
107            tabPane.setVisible(true);
108            setSize(GFrameLocationProvider.getPrefSize());
109            setLocation(GFrameLocationProvider.getLocation());
110        }
111    
112        private void setList() {
113            java.awt.GridBagConstraints gridBagConstraints;
114            gridBagConstraints = new java.awt.GridBagConstraints();
115            gridBagConstraints.gridx = 0;
116            gridBagConstraints.gridy = 0;
117            gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
118            gridBagConstraints.ipadx = 100;
119            gridBagConstraints.ipady = 250;
120            gridBagConstraints.anchor = java.awt.GridBagConstraints.SOUTH;
121            gridBagConstraints.insets = new java.awt.Insets(10, 10, 10, 10);
122            add(scrollPane, gridBagConstraints);
123        }
124    
125        private void setLabel() {
126            java.awt.GridBagConstraints gridBagConstraints;
127            gridBagConstraints = new java.awt.GridBagConstraints();
128            gridBagConstraints.gridx = 0;
129            gridBagConstraints.gridy = 0;
130    //           gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
131            gridBagConstraints.ipadx = 20;
132            gridBagConstraints.ipady = 20;
133            gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
134            gridBagConstraints.weightx = 1.0;
135            gridBagConstraints.weighty = 1.0;
136            add(label, gridBagConstraints);
137        }
138    
139        private void setTab() {
140            java.awt.GridBagConstraints gridBagConstraints;
141            gridBagConstraints = new java.awt.GridBagConstraints();
142            gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
143            gridBagConstraints.gridx = 1;
144            gridBagConstraints.gridy = 0;
145            gridBagConstraints.ipadx = 250;
146            gridBagConstraints.ipady = 250;
147            gridBagConstraints.weightx = 6;
148            gridBagConstraints.weighty = 6;
149            gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
150            gridBagConstraints.insets = new java.awt.Insets(10, 10, 10, 10);
151            add(tabPane, gridBagConstraints);
152        }
153    
154        private void setCancel() {
155            java.awt.GridBagConstraints gridBagConstraints;
156            gridBagConstraints = new java.awt.GridBagConstraints();
157            gridBagConstraints.gridx = 1;
158            gridBagConstraints.gridy = 1;
159            gridBagConstraints.ipadx = 30;
160            gridBagConstraints.ipady = 8;
161            gridBagConstraints.fill = java.awt.GridBagConstraints.EAST;
162            gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
163            gridBagConstraints.insets = new java.awt.Insets(0, 5, 0, 5);
164    
165            add(cancel, gridBagConstraints);
166        }
167    
168        /*private void setApply(){
169            java.awt.GridBagConstraints gridBagConstraints;
170            gridBagConstraints = new java.awt.GridBagConstraints();
171            gridBagConstraints.gridx = 1;
172            gridBagConstraints.gridy = 1;
173            gridBagConstraints.ipadx = 30;
174            gridBagConstraints.ipady = 8;
175             gridBagConstraints.fill = java.awt.GridBagConstraints.CENTER;        
176            gridBagConstraints.anchor = java.awt.GridBagConstraints.CENTER;
177            gridBagConstraints.insets = new java.awt.Insets(0, 5, 0, 5);
178            add(apply, gridBagConstraints);
179        }*/
180        private void setOk() {
181            java.awt.GridBagConstraints gridBagConstraints;
182            gridBagConstraints = new java.awt.GridBagConstraints();
183            gridBagConstraints.gridx = 1;
184            gridBagConstraints.gridy = 1;
185            gridBagConstraints.ipadx = 30;
186            gridBagConstraints.ipady = 8;
187            gridBagConstraints.fill = java.awt.GridBagConstraints.WEST;
188            gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
189            gridBagConstraints.insets = new java.awt.Insets(0, 5, 0, 5);
190            add(ok, gridBagConstraints);
191        }
192    
193        private void jList1ValueChanged(javax.swing.event.ListSelectionEvent evt) {
194    //          tabPane=new gpropertyeditor();
195            String s = (String) list.getSelectedValue();
196            AbstractPreference ap = tabs.get(s);
197    //        GPropertyEditor gp = new GPropertyEditor();
198    //        gp.connect(ap.attributeSet);
199    //        gp.setVisible(true);
200            tabPane.connect(ap.attributeSet);
201            tabPane.setVisible(true);
202    
203            setTab();
204        }
205    
206        /**
207         * @param args the command line arguments
208         */
209    
210        private boolean status = false;
211        private boolean finished = false;
212    
213        private void closeDialog() {
214            finished = true;
215            dispose();
216        }
217    
218        private void finished(boolean status) {
219            this.status = status;
220            closeDialog();
221            dispose();
222        }
223    
224    
225    }
226