001    /**
002     * InstanceEditor.java
003     * jCOLIBRI2 framework. 
004     * @author Juan A. Recio-García.
005     * GAIA - Group for Artificial Intelligence Applications
006     * http://gaia.fdi.ucm.es
007     * 02/11/2007
008     */
009    package jcolibri.method.gui.editors;
010    
011    import java.awt.BorderLayout;
012    import java.awt.Container;
013    import java.awt.Dimension;
014    import java.awt.Frame;
015    import java.awt.event.ActionEvent;
016    import java.awt.event.ActionListener;
017    import java.util.Collection;
018    
019    import javax.swing.Icon;
020    import javax.swing.JButton;
021    import javax.swing.JComponent;
022    import javax.swing.JDialog;
023    
024    import jcolibri.datatypes.Instance;
025    import jcolibri.exception.OntologyAccessException;
026    import es.ucm.fdi.gaia.ontobridge.OntoBridge;
027    import es.ucm.fdi.gaia.ontobridge.test.gui.PnlSelectInstance;
028    
029    /**
030     * Parameter Editor for Instance values.
031     * 
032     * @author Juan A. Recio-Garcia
033     * @version 1.0
034     * @see jcolibri.method.gui.editors.ParameterEditor
035     */
036    public class InstanceEditor extends JButton implements ParameterEditor {
037            private static final long serialVersionUID = 1L;
038    
039            
040            private static Icon INSTANCE = new javax.swing.ImageIcon(jcolibri.util.FileIO.findFile("/es/ucm/fdi/gaia/ontobridge/test/gui/instance.gif"));
041            
042            JDialog ontoDialog;
043            PnlSelectInstance ontoPanel;
044            String selected;
045            
046            /**
047             *  Creates a new instance
048             */
049            public InstanceEditor()
050            {
051                    this.setText("...");
052                    
053                    ontoDialog = new JDialog((Frame)null, true);
054                    OntoBridge ob = jcolibri.util.OntoBridgeSingleton.getOntoBridge();
055                    ontoPanel = new PnlSelectInstance(ob);
056                    Container main = ontoDialog.getContentPane();
057                    main.setLayout(new BorderLayout());
058                    main.add(ontoPanel, BorderLayout.CENTER);
059                    
060                    JButton select = new JButton("Select Instance");
061                    select.addActionListener(new ActionListener(){
062                            public void actionPerformed(ActionEvent e) {
063                                    ontoDialog.setVisible(false);   
064                            }
065                    });
066                    main.add(select,BorderLayout.SOUTH);
067                    
068                    ontoDialog.pack();
069                    
070                    Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
071                    ontoDialog.setBounds((screenSize.width - ontoDialog.getWidth()) / 2,
072                            (screenSize.height - ontoDialog.getHeight()) / 2, 
073                            ontoDialog.getWidth(),
074                            ontoDialog.getHeight());
075                    
076                    this.addActionListener(new ActionListener(){
077                            public void actionPerformed(ActionEvent e) {
078                                    selectInstance();       
079                            }
080                    });
081            }
082            
083            
084            void selectInstance()
085            {
086                    ontoDialog.setVisible(true);
087                    selected = ontoPanel.getSelectedInstance();
088                    if(selected==null)
089                    {
090                            this.setText("...");
091                            this.setIcon(null);
092                    }
093                    else
094                    {
095                            this.setText(selected);
096                            this.setIcon(INSTANCE);
097                    }
098            }
099            
100    
101            /**
102             * Returns an Instance object
103             */
104            public Object getEditorValue() {
105                try {
106                    if(selected==null)
107                            return null;
108                    return new Instance(selected);
109                } catch (OntologyAccessException e) {
110                    org.apache.commons.logging.LogFactory.getLog(this.getClass()).error(e);
111                }
112                return null;
113            }
114    
115            /**
116             * Returns the JComponent
117             */
118            public JComponent getJComponent() {
119                    return (JComponent) this;
120            }
121    
122            /**
123             * Receives an Instance (or String) value
124             */
125            public void setEditorValue(Object value) {
126                    if(value==null)
127                    {
128                        this.setText("");
129                        this.setIcon(null);
130                        return;
131                    } 
132                    selected = value.toString();
133                    this.setText(selected);
134                    this.setIcon(INSTANCE);
135            }
136    
137            /**
138             * Does nothing
139             */
140            public void setAllowedValues(Collection<Object> allowedValues)
141            {  
142            }
143    }