| 1 | package hozo.sparql.gui;
 | 
|---|
| 2 | 
 | 
|---|
| 3 | import java.awt.BorderLayout;
 | 
|---|
| 4 | import java.awt.Frame;
 | 
|---|
| 5 | import java.awt.event.ActionEvent;
 | 
|---|
| 6 | import java.awt.event.ActionListener;
 | 
|---|
| 7 | import java.awt.event.KeyEvent;
 | 
|---|
| 8 | import java.util.LinkedHashMap;
 | 
|---|
| 9 | import java.util.Map;
 | 
|---|
| 10 | 
 | 
|---|
| 11 | import javax.swing.AbstractAction;
 | 
|---|
| 12 | import javax.swing.DefaultListModel;
 | 
|---|
| 13 | import javax.swing.InputMap;
 | 
|---|
| 14 | import javax.swing.JButton;
 | 
|---|
| 15 | import javax.swing.JComponent;
 | 
|---|
| 16 | import javax.swing.JDialog;
 | 
|---|
| 17 | import javax.swing.JPanel;
 | 
|---|
| 18 | import javax.swing.JScrollPane;
 | 
|---|
| 19 | import javax.swing.KeyStroke;
 | 
|---|
| 20 | 
 | 
|---|
| 21 | import hozo.sparql.EndpointSettings;
 | 
|---|
| 22 | import hozo.sparql.util.CheckableList;
 | 
|---|
| 23 | import hozo.sparql.util.CheckableListItem;
 | 
|---|
| 24 | 
 | 
|---|
| 25 | public class EndpointSelector extends JDialog {
 | 
|---|
| 26 | 
 | 
|---|
| 27 |         /**
 | 
|---|
| 28 |          *
 | 
|---|
| 29 |          */
 | 
|---|
| 30 |         private static final long serialVersionUID = -5270916655051535400L;
 | 
|---|
| 31 |         private EndpointSettings[] settings;
 | 
|---|
| 32 |         private Map<String, EndpointSettings> settingNames;
 | 
|---|
| 33 | 
 | 
|---|
| 34 |         private JPanel jContentPane;
 | 
|---|
| 35 |         private CheckableList endpointList;
 | 
|---|
| 36 |         private JScrollPane endpointListScrollPane;
 | 
|---|
| 37 |         private JPanel buttonPane;
 | 
|---|
| 38 |         private JButton okButton;
 | 
|---|
| 39 |         private boolean valid = false;
 | 
|---|
| 40 | 
 | 
|---|
| 41 |         public EndpointSelector(Frame owner, EndpointSettings[] settings){
 | 
|---|
| 42 |                 super(owner);
 | 
|---|
| 43 | 
 | 
|---|
| 44 |                 this.settings = settings;
 | 
|---|
| 45 |                 this.settingNames = new LinkedHashMap<String, EndpointSettings>();
 | 
|---|
| 46 |                 this.setTitle("Select the target endpoints");
 | 
|---|
| 47 |                 for (EndpointSettings setting : settings){
 | 
|---|
| 48 |                         settingNames.put(setting.getEndpoint(), setting);
 | 
|---|
| 49 |                 }
 | 
|---|
| 50 | 
 | 
|---|
| 51 |                 initialize();
 | 
|---|
| 52 |         }
 | 
|---|
| 53 | 
 | 
|---|
| 54 |         public boolean isValid(){
 | 
|---|
| 55 |                 return valid;
 | 
|---|
| 56 |         }
 | 
|---|
| 57 | 
 | 
|---|
| 58 |         public EndpointSettings[] getSettings(){
 | 
|---|
| 59 |                 DefaultListModel model = (DefaultListModel)getEndpointList().getModel();
 | 
|---|
| 60 |                 for (int i=0; i<model.getSize(); i++){
 | 
|---|
| 61 |                         CheckableListItem item = (CheckableListItem)model.getElementAt(i);
 | 
|---|
| 62 |                         EndpointSettings ep = settingNames.get(item.text);
 | 
|---|
| 63 |                         if (ep != null){
 | 
|---|
| 64 |                                 ep.setTarget(item.selected);
 | 
|---|
| 65 |                         }
 | 
|---|
| 66 |                 }
 | 
|---|
| 67 | 
 | 
|---|
| 68 |                 return settings;
 | 
|---|
| 69 |         }
 | 
|---|
| 70 | 
 | 
|---|
| 71 | 
 | 
|---|
| 72 |         /**
 | 
|---|
| 73 |          * This method initializes this
 | 
|---|
| 74 |          *
 | 
|---|
| 75 |          * @return void
 | 
|---|
| 76 |          */
 | 
|---|
| 77 |         private void initialize() {
 | 
|---|
| 78 |                 this.setSize(400, 400);
 | 
|---|
| 79 |                 this.setContentPane(getJContentPane());
 | 
|---|
| 80 |                 this.setResizable(false);
 | 
|---|
| 81 |                 this.setModal(true);
 | 
|---|
| 82 |                 InputMap imap = getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
 | 
|---|
| 83 |                 imap.put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), "go");
 | 
|---|
| 84 |                 getRootPane().getActionMap().put("go", new AbstractAction() {
 | 
|---|
| 85 |                         /**
 | 
|---|
| 86 |                          * 
 | 
|---|
| 87 |                          */
 | 
|---|
| 88 |                         private static final long serialVersionUID = -4361937227644962283L;
 | 
|---|
| 89 | 
 | 
|---|
| 90 |                         @Override
 | 
|---|
| 91 |                         public void actionPerformed(ActionEvent e) {
 | 
|---|
| 92 |                                 valid = true;
 | 
|---|
| 93 |                                 close();
 | 
|---|
| 94 |                         }
 | 
|---|
| 95 |                 });
 | 
|---|
| 96 |         }
 | 
|---|
| 97 | 
 | 
|---|
| 98 |         private JPanel getJContentPane() {
 | 
|---|
| 99 |                 if (jContentPane == null){
 | 
|---|
| 100 |                         jContentPane = new JPanel();
 | 
|---|
| 101 |                         jContentPane.setLayout(new BorderLayout());
 | 
|---|
| 102 | //                      jContentPane.add(new JLabel("Select the target endpoints"), BorderLayout.NORTH);
 | 
|---|
| 103 |                         jContentPane.add(getEndpointListScrollPane(), BorderLayout.CENTER);
 | 
|---|
| 104 |                         jContentPane.add(getButtonPane(), BorderLayout.SOUTH);
 | 
|---|
| 105 |                 }
 | 
|---|
| 106 | 
 | 
|---|
| 107 |                 return jContentPane;
 | 
|---|
| 108 | 
 | 
|---|
| 109 |         }
 | 
|---|
| 110 | 
 | 
|---|
| 111 |         private CheckableList getEndpointList(){
 | 
|---|
| 112 |                 if (endpointList == null){
 | 
|---|
| 113 |                         DefaultListModel model = new DefaultListModel();
 | 
|---|
| 114 |                         for (EndpointSettings setting : settings){
 | 
|---|
| 115 |                                 model.addElement(new CheckableListItem(setting.getEndpoint(), setting.isTarget()));
 | 
|---|
| 116 |                         }
 | 
|---|
| 117 | 
 | 
|---|
| 118 |                         endpointList = new CheckableList(model);
 | 
|---|
| 119 |                 }
 | 
|---|
| 120 |                 return endpointList;
 | 
|---|
| 121 |         }
 | 
|---|
| 122 | 
 | 
|---|
| 123 |         private JScrollPane getEndpointListScrollPane(){
 | 
|---|
| 124 |                 if (endpointListScrollPane == null){
 | 
|---|
| 125 |                         endpointListScrollPane = new JScrollPane(getEndpointList());
 | 
|---|
| 126 |                 }
 | 
|---|
| 127 |                 return endpointListScrollPane;
 | 
|---|
| 128 |         }
 | 
|---|
| 129 | 
 | 
|---|
| 130 |         private JPanel getButtonPane(){
 | 
|---|
| 131 |                 if (buttonPane == null){
 | 
|---|
| 132 |                         buttonPane = new JPanel();
 | 
|---|
| 133 |                         buttonPane.setLayout(new BorderLayout());
 | 
|---|
| 134 |                         buttonPane.add(getOKButton(), BorderLayout.EAST);
 | 
|---|
| 135 |                 }
 | 
|---|
| 136 | 
 | 
|---|
| 137 |                 return buttonPane;
 | 
|---|
| 138 |         }
 | 
|---|
| 139 |         private JButton getOKButton(){
 | 
|---|
| 140 |                 if (okButton == null){
 | 
|---|
| 141 |                         okButton = new JButton("OK");
 | 
|---|
| 142 |                         okButton.addActionListener(new ActionListener() {
 | 
|---|
| 143 | 
 | 
|---|
| 144 |                                 @Override
 | 
|---|
| 145 |                                 public void actionPerformed(ActionEvent arg0) {
 | 
|---|
| 146 |                                         valid = true;
 | 
|---|
| 147 |                                         close();
 | 
|---|
| 148 |                                 }
 | 
|---|
| 149 |                         });
 | 
|---|
| 150 |                 }
 | 
|---|
| 151 |                 return okButton;
 | 
|---|
| 152 |         }
 | 
|---|
| 153 | 
 | 
|---|
| 154 | 
 | 
|---|
| 155 | 
 | 
|---|
| 156 |         private void close(){
 | 
|---|
| 157 |                 this.setVisible(false);
 | 
|---|
| 158 |         }
 | 
|---|
| 159 | 
 | 
|---|
| 160 | }
 | 
|---|