[17] | 1 | package hozo.sparql.gui;
|
---|
[9] | 2 |
|
---|
| 3 | import java.awt.BorderLayout;
|
---|
| 4 | import java.awt.event.ActionEvent;
|
---|
| 5 | import java.awt.event.ActionListener;
|
---|
| 6 | import java.util.ArrayList;
|
---|
| 7 | import java.util.List;
|
---|
| 8 | import java.util.Map;
|
---|
| 9 |
|
---|
| 10 | import javax.swing.JOptionPane;
|
---|
| 11 | import javax.swing.JPanel;
|
---|
| 12 | import javax.swing.JSplitPane;
|
---|
| 13 | import javax.swing.JTable;
|
---|
| 14 | import javax.swing.JTextArea;
|
---|
| 15 | import javax.swing.JScrollPane;
|
---|
| 16 | import javax.swing.JButton;
|
---|
| 17 | import javax.swing.table.DefaultTableModel;
|
---|
| 18 |
|
---|
| 19 | import com.hp.hpl.jena.rdf.model.RDFNode;
|
---|
| 20 |
|
---|
[17] | 21 | import hozo.sparql.EndpointSettingsManager;
|
---|
| 22 | import hozo.sparql.SparqlAccessorFactory;
|
---|
| 23 | import hozo.sparql.SparqlQueryListener;
|
---|
| 24 | import hozo.sparql.SparqlResultListener;
|
---|
| 25 | import hozo.sparql.SparqlResultSet;
|
---|
| 26 | import hozo.sparql.ThreadedSparqlAccessor;
|
---|
[9] | 27 |
|
---|
| 28 | public class SparqlSearchPanel extends JPanel {
|
---|
| 29 |
|
---|
| 30 | private static final long serialVersionUID = 1L;
|
---|
| 31 | private JPanel executePanel = null; // @jve:decl-index=0:visual-constraint="428,142"
|
---|
| 32 | private JTextArea queryTextArea = null;
|
---|
| 33 | private JPanel footerPanel = null;
|
---|
| 34 | private JScrollPane resultListScrollPane = null; // @jve:decl-index=0:visual-constraint="153,224"
|
---|
| 35 | private JTable resultList = null; // @jve:decl-index=0:visual-constraint="369,21"
|
---|
| 36 | private JButton runQueryButton = null; // @jve:decl-index=0:visual-constraint="390,64"
|
---|
| 37 | private JSplitPane mainSplitPane = null;
|
---|
| 38 |
|
---|
| 39 | private SparqlAccessorForm parent;
|
---|
| 40 |
|
---|
| 41 | private DefaultTableModel tableModel;
|
---|
| 42 |
|
---|
| 43 | private ThreadedSparqlAccessor sa;
|
---|
| 44 |
|
---|
| 45 |
|
---|
| 46 | /**
|
---|
| 47 | * This is the default constructor
|
---|
| 48 | */
|
---|
| 49 | public SparqlSearchPanel(SparqlAccessorForm parent) {
|
---|
| 50 | super();
|
---|
| 51 | initialize();
|
---|
| 52 | this.setParent(parent);
|
---|
| 53 | }
|
---|
| 54 |
|
---|
| 55 | /**
|
---|
| 56 | * This method initializes this
|
---|
| 57 | *
|
---|
| 58 | * @return void
|
---|
| 59 | */
|
---|
| 60 | private void initialize() {
|
---|
| 61 | this.setSize(300, 200);
|
---|
| 62 | this.setLayout(new BorderLayout());
|
---|
| 63 | this.add(getExecutePanel(), BorderLayout.NORTH);
|
---|
| 64 | this.add(getMainSplitPane(), BorderLayout.CENTER);
|
---|
| 65 | }
|
---|
| 66 |
|
---|
| 67 | private JSplitPane getMainSplitPane(){
|
---|
| 68 | if (mainSplitPane == null){
|
---|
| 69 | mainSplitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, getQueryScrollPane(), getFooterPanel());
|
---|
| 70 | mainSplitPane.setDividerLocation(200);
|
---|
| 71 | }
|
---|
| 72 |
|
---|
| 73 | return mainSplitPane;
|
---|
| 74 | }
|
---|
| 75 |
|
---|
| 76 | /**
|
---|
| 77 | * This method initializes keywordPanel
|
---|
| 78 | *
|
---|
| 79 | * @return javax.swing.JPanel
|
---|
| 80 | */
|
---|
| 81 | private JPanel getExecutePanel() {
|
---|
| 82 | if (executePanel == null) {
|
---|
| 83 | executePanel = new JPanel();
|
---|
| 84 | executePanel.setLayout(new BorderLayout());
|
---|
| 85 | executePanel.add(getRunQueryButton(), BorderLayout.EAST);
|
---|
| 86 |
|
---|
| 87 | SparqlBuilderPanel bulider = new SparqlBuilderPanel(this);
|
---|
| 88 | executePanel.add(bulider,BorderLayout.CENTER);
|
---|
| 89 | }
|
---|
| 90 | return executePanel;
|
---|
| 91 | }
|
---|
| 92 |
|
---|
| 93 | public void setQueryText(String query){
|
---|
| 94 | this.getQueryTextField().setText(query);
|
---|
| 95 | }
|
---|
| 96 |
|
---|
| 97 | /**
|
---|
| 98 | * This method initializes keywordTextField
|
---|
| 99 | *
|
---|
| 100 | * @return javax.swing.JTextField
|
---|
| 101 | */
|
---|
| 102 | private JTextArea getQueryTextField() {
|
---|
| 103 | if (queryTextArea == null) {
|
---|
| 104 | queryTextArea = new JTextArea();
|
---|
| 105 | }
|
---|
| 106 | return queryTextArea;
|
---|
| 107 | }
|
---|
| 108 |
|
---|
| 109 | /**
|
---|
| 110 | * 讀懃エ「繝ッ繝シ繝峨r蜿門セ励☆繧� |
---|
| 111 | * @return
|
---|
| 112 | */
|
---|
| 113 | public String getFindWord(){
|
---|
| 114 | return getQueryTextField().getText();
|
---|
| 115 | }
|
---|
| 116 |
|
---|
| 117 | private SparqlResultListener createSparqlResultListener2(){
|
---|
| 118 | return new SparqlResultListener() {
|
---|
| 119 |
|
---|
| 120 | @Override
|
---|
| 121 | public void resultReceived(SparqlResultSet result) {
|
---|
| 122 | // 邨先棡繧偵∪縺壹�list縺ォ霑ス蜉
|
---|
| 123 | setProcessing(false);
|
---|
| 124 | setResults(result);
|
---|
| 125 | }
|
---|
| 126 |
|
---|
| 127 | @Override
|
---|
| 128 | public void uncaughtException(Thread t, Throwable e) {
|
---|
| 129 | // TODO 閾ェ蜍慕函謌舌&繧後◆繝。繧ス繝�ラ繝サ繧ケ繧ソ繝� |
---|
| 130 | JOptionPane.showMessageDialog(getParent(), "Execute error:"+e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
|
---|
| 131 | setProcessing(false);
|
---|
| 132 | }
|
---|
| 133 | };
|
---|
| 134 | }
|
---|
| 135 |
|
---|
| 136 | /**
|
---|
| 137 | * This method initializes footerPanel
|
---|
| 138 | *
|
---|
| 139 | * @return javax.swing.JPanel
|
---|
| 140 | */
|
---|
| 141 | private JPanel getFooterPanel() {
|
---|
| 142 | if (footerPanel == null) {
|
---|
| 143 | footerPanel = new JPanel();
|
---|
| 144 | footerPanel.setLayout(new BorderLayout());
|
---|
| 145 | footerPanel.add(getResultListScrollPane(), BorderLayout.CENTER);
|
---|
| 146 | }
|
---|
| 147 | return footerPanel;
|
---|
| 148 | }
|
---|
| 149 |
|
---|
| 150 | /**
|
---|
| 151 | * This method initializes resultListScrollPane
|
---|
| 152 | *
|
---|
| 153 | * @return javax.swing.JScrollPane
|
---|
| 154 | */
|
---|
| 155 | private JScrollPane getResultListScrollPane() {
|
---|
| 156 | if (resultListScrollPane == null) {
|
---|
| 157 | resultListScrollPane = new JScrollPane(getResultList());
|
---|
| 158 | }
|
---|
| 159 | return resultListScrollPane;
|
---|
| 160 | }
|
---|
| 161 |
|
---|
| 162 | /**
|
---|
| 163 | * This method initializes resultList
|
---|
| 164 | *
|
---|
| 165 | * @return javax.swing.JList
|
---|
| 166 | */
|
---|
| 167 | private JTable getResultList() {
|
---|
| 168 | if (resultList == null) {
|
---|
| 169 | resultList = new JTable();
|
---|
| 170 | }
|
---|
| 171 | return resultList;
|
---|
| 172 | }
|
---|
| 173 |
|
---|
| 174 | public void setResults(SparqlResultSet result){
|
---|
| 175 | List<Map<String, RDFNode>> list = result.getDefaultResult();
|
---|
| 176 |
|
---|
| 177 | if (list.size() == 0){
|
---|
| 178 | tableModel = new DefaultTableModel();
|
---|
| 179 | getResultList().setModel(tableModel);
|
---|
| 180 | return;
|
---|
| 181 | }
|
---|
| 182 |
|
---|
| 183 | // header繧サ繝�ヨ
|
---|
| 184 | Map<String, RDFNode> columns = list.get(0);
|
---|
| 185 | List<String> clm = new ArrayList<String>();
|
---|
| 186 | for (String key : columns.keySet()){
|
---|
| 187 | clm.add(key);
|
---|
| 188 | }
|
---|
| 189 | tableModel = new DefaultTableModel(clm.toArray(new String[]{}), 0);
|
---|
| 190 |
|
---|
| 191 |
|
---|
| 192 | // 荳ュ霄ォ繧サ繝�ヨ
|
---|
| 193 | for (Map<String, RDFNode> r : list){
|
---|
| 194 | List<Object> row = new ArrayList<Object>();
|
---|
| 195 | for (String key : clm){
|
---|
| 196 | RDFNode node = r.get(key);
|
---|
| 197 | row.add(node);
|
---|
| 198 | }
|
---|
| 199 | tableModel.addRow(row.toArray(new Object[]{}));
|
---|
| 200 | }
|
---|
| 201 |
|
---|
| 202 | getResultList().setModel(tableModel);
|
---|
| 203 |
|
---|
| 204 | parent.setResults(result);
|
---|
| 205 | }
|
---|
| 206 |
|
---|
| 207 | private boolean processing = false;
|
---|
| 208 | private JScrollPane queryScrollPane = null;
|
---|
| 209 |
|
---|
| 210 | private void setProcessing(boolean processing){
|
---|
| 211 | this. processing = processing;
|
---|
| 212 | getRunQueryButton().setEnabled(!processing);
|
---|
| 213 | getResultList().setEnabled(!processing);
|
---|
| 214 |
|
---|
| 215 | parent.setProcessing(processing);
|
---|
| 216 | }
|
---|
| 217 |
|
---|
| 218 | private boolean isProcessing(){
|
---|
| 219 | return this.processing;
|
---|
| 220 | }
|
---|
| 221 |
|
---|
| 222 | /**
|
---|
| 223 | * This method initializes runQueryButton
|
---|
| 224 | *
|
---|
| 225 | * @return javax.swing.JButton
|
---|
| 226 | */
|
---|
| 227 | private JButton getRunQueryButton() {
|
---|
| 228 | if (runQueryButton == null) {
|
---|
| 229 | runQueryButton = new JButton("Run Query");
|
---|
| 230 | runQueryButton.addActionListener(new ActionListener() {
|
---|
| 231 |
|
---|
| 232 | @Override
|
---|
| 233 | public void actionPerformed(ActionEvent e) {
|
---|
| 234 | if (isProcessing()){
|
---|
| 235 | return;
|
---|
| 236 | }
|
---|
| 237 | setProcessing(true);
|
---|
| 238 | sa = SparqlAccessorFactory.createSparqlAccessor(EndpointSettingsManager.instance.getSetting(parent.getCurrentEndPoint()), new SparqlQueryListener() {
|
---|
| 239 |
|
---|
| 240 | @Override
|
---|
| 241 | public void sparqlExecuted(String query) {
|
---|
| 242 | parent.addLogText("----------------");
|
---|
| 243 | parent.addLogText(query);
|
---|
| 244 | }
|
---|
| 245 | });
|
---|
| 246 | sa.executeQuery(getFindWord(), createSparqlResultListener2());
|
---|
| 247 | }
|
---|
| 248 | });
|
---|
| 249 | }
|
---|
| 250 | return runQueryButton;
|
---|
| 251 | }
|
---|
| 252 |
|
---|
| 253 | /**
|
---|
| 254 | * This method initializes queryScrollPane
|
---|
| 255 | *
|
---|
| 256 | * @return javax.swing.JScrollPane
|
---|
| 257 | */
|
---|
| 258 | private JScrollPane getQueryScrollPane() {
|
---|
| 259 | if (queryScrollPane == null) {
|
---|
| 260 | queryScrollPane = new JScrollPane(getQueryTextField());
|
---|
| 261 | }
|
---|
| 262 | return queryScrollPane;
|
---|
| 263 | }
|
---|
| 264 |
|
---|
| 265 | public SparqlAccessorForm getSparqlAccessorForm() {
|
---|
| 266 | return parent;
|
---|
| 267 | }
|
---|
| 268 |
|
---|
| 269 | public void setParent(SparqlAccessorForm parent) {
|
---|
| 270 | this.parent = parent;
|
---|
| 271 | }
|
---|
| 272 |
|
---|
| 273 |
|
---|
| 274 | }
|
---|