| 1 | package hozo.sparql.plugin.compare;
|
|---|
| 2 |
|
|---|
| 3 | import java.awt.GridBagLayout;
|
|---|
| 4 | import javax.swing.JPanel;
|
|---|
| 5 | import javax.swing.JTextField;
|
|---|
| 6 | import javax.swing.JButton;
|
|---|
| 7 |
|
|---|
| 8 | import java.awt.BorderLayout;
|
|---|
| 9 | import java.awt.Component;
|
|---|
| 10 | import java.awt.EventQueue;
|
|---|
| 11 | import java.awt.GridBagConstraints;
|
|---|
| 12 | import java.awt.Insets;
|
|---|
| 13 | import java.awt.event.ActionEvent;
|
|---|
| 14 | import java.awt.event.ActionListener;
|
|---|
| 15 | import java.io.File;
|
|---|
| 16 | import java.io.IOException;
|
|---|
| 17 |
|
|---|
| 18 | import javax.swing.JFileChooser;
|
|---|
| 19 | import javax.swing.JOptionPane;
|
|---|
| 20 | import javax.swing.JScrollPane;
|
|---|
| 21 | import javax.swing.JTextArea;
|
|---|
| 22 | import javax.swing.JLabel;
|
|---|
| 23 | import javax.swing.event.DocumentEvent;
|
|---|
| 24 | import javax.swing.event.DocumentListener;
|
|---|
| 25 | import javax.swing.text.BadLocationException;
|
|---|
| 26 | import javax.swing.text.Document;
|
|---|
| 27 | import javax.swing.text.Element;
|
|---|
| 28 |
|
|---|
| 29 | import hozo.sparql.SparqlQueryListener;
|
|---|
| 30 |
|
|---|
| 31 | public class CompareSubjectPanel extends JPanel {
|
|---|
| 32 |
|
|---|
| 33 | private static final long serialVersionUID = 1L;
|
|---|
| 34 | private JTextField epPropsTextField = null; // @jve:decl-index=0:visual-constraint="542,195"
|
|---|
| 35 | private JButton endpointsButton = null;
|
|---|
| 36 | private JTextField resourceCsvField = null;
|
|---|
| 37 | private JButton wordsButton = null;
|
|---|
| 38 | private JScrollPane logScrollPane = null;
|
|---|
| 39 | private JTextArea logTextArea = null;
|
|---|
| 40 | private JLabel epPropLabel = null;
|
|---|
| 41 | private JLabel resourceCsvLabel = null;
|
|---|
| 42 | private JButton executeButton = null;
|
|---|
| 43 | private JPanel optPanel = null;
|
|---|
| 44 | private JLabel outputLabel = null;
|
|---|
| 45 | private JTextField outputTextField = null;
|
|---|
| 46 | private JButton outputRefButton = null;
|
|---|
| 47 | private Component parent;
|
|---|
| 48 |
|
|---|
| 49 | /**
|
|---|
| 50 | * This is the default constructor
|
|---|
| 51 | */
|
|---|
| 52 | public CompareSubjectPanel(Component parent) {
|
|---|
| 53 | super();
|
|---|
| 54 | initialize();
|
|---|
| 55 | this.parent= parent;
|
|---|
| 56 | }
|
|---|
| 57 |
|
|---|
| 58 | /**
|
|---|
| 59 | * This method initializes this
|
|---|
| 60 | *
|
|---|
| 61 | * @return void
|
|---|
| 62 | */
|
|---|
| 63 | private void initialize() {
|
|---|
| 64 | GridBagConstraints gridBagConstraints111 = new GridBagConstraints();
|
|---|
| 65 | gridBagConstraints111.gridx = 2;
|
|---|
| 66 | gridBagConstraints111.insets = new Insets(0, 0, 0, 10);
|
|---|
| 67 | gridBagConstraints111.gridy = 3;
|
|---|
| 68 | GridBagConstraints gridBagConstraints10 = new GridBagConstraints();
|
|---|
| 69 | gridBagConstraints10.fill = GridBagConstraints.HORIZONTAL;
|
|---|
| 70 | gridBagConstraints10.gridy = 3;
|
|---|
| 71 | gridBagConstraints10.weightx = 1.0;
|
|---|
| 72 | gridBagConstraints10.insets = new Insets(5, 10, 5, 0);
|
|---|
| 73 | gridBagConstraints10.gridx = 1;
|
|---|
| 74 | GridBagConstraints gridBagConstraints9 = new GridBagConstraints();
|
|---|
| 75 | gridBagConstraints9.gridx = 0;
|
|---|
| 76 | gridBagConstraints9.gridy = 3;
|
|---|
| 77 | outputLabel = new JLabel("Output File");
|
|---|
| 78 | GridBagConstraints gridBagConstraints8 = new GridBagConstraints();
|
|---|
| 79 | gridBagConstraints8.gridx = 0;
|
|---|
| 80 | gridBagConstraints8.gridwidth = 3;
|
|---|
| 81 | gridBagConstraints8.fill = GridBagConstraints.HORIZONTAL;
|
|---|
| 82 | gridBagConstraints8.gridy = 4;
|
|---|
| 83 | GridBagConstraints gridBagConstraints5 = new GridBagConstraints();
|
|---|
| 84 | gridBagConstraints5.gridx = 0;
|
|---|
| 85 | gridBagConstraints5.insets = new Insets(0, 10, 0, 0);
|
|---|
| 86 | gridBagConstraints5.gridy = 2;
|
|---|
| 87 | resourceCsvLabel = new JLabel();
|
|---|
| 88 | resourceCsvLabel.setText("resource csv File");
|
|---|
| 89 | GridBagConstraints gridBagConstraints4 = new GridBagConstraints();
|
|---|
| 90 | gridBagConstraints4.gridx = 0;
|
|---|
| 91 | gridBagConstraints4.insets = new Insets(0, 10, 0, 0);
|
|---|
| 92 | gridBagConstraints4.gridy = 0;
|
|---|
| 93 | epPropLabel = new JLabel();
|
|---|
| 94 | epPropLabel.setText("Endpoint-property List File");
|
|---|
| 95 | GridBagConstraints gridBagConstraints3 = new GridBagConstraints();
|
|---|
| 96 | gridBagConstraints3.fill = GridBagConstraints.BOTH;
|
|---|
| 97 | gridBagConstraints3.gridy = 5;
|
|---|
| 98 | gridBagConstraints3.weightx = 1.0;
|
|---|
| 99 | gridBagConstraints3.weighty = 1.0;
|
|---|
| 100 | gridBagConstraints3.insets = new Insets(10, 10, 10, 10);
|
|---|
| 101 | gridBagConstraints3.gridwidth = 3;
|
|---|
| 102 | gridBagConstraints3.gridx = 0;
|
|---|
| 103 | GridBagConstraints gridBagConstraints2 = new GridBagConstraints();
|
|---|
| 104 | gridBagConstraints2.gridx = 2;
|
|---|
| 105 | gridBagConstraints2.insets = new Insets(0, 0, 0, 10);
|
|---|
| 106 | gridBagConstraints2.gridy = 2;
|
|---|
| 107 | GridBagConstraints gridBagConstraints11 = new GridBagConstraints();
|
|---|
| 108 | gridBagConstraints11.fill = GridBagConstraints.HORIZONTAL;
|
|---|
| 109 | gridBagConstraints11.gridy = 2;
|
|---|
| 110 | gridBagConstraints11.weightx = 1.0;
|
|---|
| 111 | gridBagConstraints11.insets = new Insets(5, 10, 5, 0);
|
|---|
| 112 | gridBagConstraints11.gridx = 1;
|
|---|
| 113 | GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
|
|---|
| 114 | gridBagConstraints1.fill = GridBagConstraints.NONE;
|
|---|
| 115 | gridBagConstraints1.gridy = 0;
|
|---|
| 116 | gridBagConstraints1.insets = new Insets(10, 0, 0, 10);
|
|---|
| 117 | gridBagConstraints1.gridx = 2;
|
|---|
| 118 | GridBagConstraints gridBagConstraints = new GridBagConstraints();
|
|---|
| 119 | gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
|
|---|
| 120 | gridBagConstraints.gridy = 0;
|
|---|
| 121 | gridBagConstraints.insets = new Insets(14, 10, 5, 0);
|
|---|
| 122 | gridBagConstraints.gridx = 1;
|
|---|
| 123 | this.setLayout(new GridBagLayout());
|
|---|
| 124 | this.add(getEpPropsTextField(), gridBagConstraints);
|
|---|
| 125 | this.add(getEndpointsButton(), gridBagConstraints1);
|
|---|
| 126 | this.add(getResourceCsvField(), gridBagConstraints11);
|
|---|
| 127 | this.add(getWordsButton(), gridBagConstraints2);
|
|---|
| 128 | this.add(getLogScrollPane(), gridBagConstraints3);
|
|---|
| 129 | this.add(epPropLabel, gridBagConstraints4);
|
|---|
| 130 | this.add(resourceCsvLabel, gridBagConstraints5);
|
|---|
| 131 | this.add(getOptPanel(), gridBagConstraints8);
|
|---|
| 132 | this.add(outputLabel, gridBagConstraints9);
|
|---|
| 133 | this.add(getOutputTextField(), gridBagConstraints10);
|
|---|
| 134 | this.add(getOutputRefButton(), gridBagConstraints111);
|
|---|
| 135 | }
|
|---|
| 136 |
|
|---|
| 137 | /**
|
|---|
| 138 | * This method initializes epPropsTextField
|
|---|
| 139 | *
|
|---|
| 140 | * @return javax.swing.JTextField
|
|---|
| 141 | */
|
|---|
| 142 | private JTextField getEpPropsTextField() {
|
|---|
| 143 | if (epPropsTextField == null) {
|
|---|
| 144 | epPropsTextField = new JTextField();
|
|---|
| 145 | epPropsTextField.setEditable(false);
|
|---|
| 146 | }
|
|---|
| 147 | return epPropsTextField;
|
|---|
| 148 | }
|
|---|
| 149 |
|
|---|
| 150 | /**
|
|---|
| 151 | * This method initializes endpointsButton
|
|---|
| 152 | *
|
|---|
| 153 | * @return javax.swing.JButton
|
|---|
| 154 | */
|
|---|
| 155 | private JButton getEndpointsButton() {
|
|---|
| 156 | if (endpointsButton == null) {
|
|---|
| 157 | endpointsButton = new JButton("Ref");
|
|---|
| 158 | endpointsButton.addActionListener(new ActionListener() {
|
|---|
| 159 |
|
|---|
| 160 | @Override
|
|---|
| 161 | public void actionPerformed(ActionEvent arg0) {
|
|---|
| 162 | selectFile(getEpPropsTextField(), JFileChooser.OPEN_DIALOG);
|
|---|
| 163 | validateExecute();
|
|---|
| 164 | }
|
|---|
| 165 | });
|
|---|
| 166 | }
|
|---|
| 167 | return endpointsButton;
|
|---|
| 168 | }
|
|---|
| 169 |
|
|---|
| 170 | /**
|
|---|
| 171 | * This method initializes resourceCsvField
|
|---|
| 172 | *
|
|---|
| 173 | * @return javax.swing.JTextField
|
|---|
| 174 | */
|
|---|
| 175 | private JTextField getResourceCsvField() {
|
|---|
| 176 | if (resourceCsvField == null) {
|
|---|
| 177 | resourceCsvField = new JTextField();
|
|---|
| 178 | resourceCsvField.setEditable(false);
|
|---|
| 179 | }
|
|---|
| 180 | return resourceCsvField;
|
|---|
| 181 | }
|
|---|
| 182 |
|
|---|
| 183 | /**
|
|---|
| 184 | * This method initializes wordsButton
|
|---|
| 185 | *
|
|---|
| 186 | * @return javax.swing.JButton
|
|---|
| 187 | */
|
|---|
| 188 | private JButton getWordsButton() {
|
|---|
| 189 | if (wordsButton == null) {
|
|---|
| 190 | wordsButton = new JButton("Ref");
|
|---|
| 191 | wordsButton.addActionListener(new ActionListener() {
|
|---|
| 192 |
|
|---|
| 193 | @Override
|
|---|
| 194 | public void actionPerformed(ActionEvent arg0) {
|
|---|
| 195 | selectFile(getResourceCsvField(), JFileChooser.OPEN_DIALOG);
|
|---|
| 196 | validateExecute();
|
|---|
| 197 | }
|
|---|
| 198 | });
|
|---|
| 199 | }
|
|---|
| 200 | return wordsButton;
|
|---|
| 201 | }
|
|---|
| 202 |
|
|---|
| 203 | /**
|
|---|
| 204 | * This method initializes logScrollPane
|
|---|
| 205 | *
|
|---|
| 206 | * @return javax.swing.JScrollPane
|
|---|
| 207 | */
|
|---|
| 208 | private JScrollPane getLogScrollPane() {
|
|---|
| 209 | if (logScrollPane == null) {
|
|---|
| 210 | logScrollPane = new JScrollPane();
|
|---|
| 211 | logScrollPane.setViewportView(getLogTextArea());
|
|---|
| 212 | }
|
|---|
| 213 | return logScrollPane;
|
|---|
| 214 | }
|
|---|
| 215 |
|
|---|
| 216 | /**
|
|---|
| 217 | * This method initializes logTextArea
|
|---|
| 218 | *
|
|---|
| 219 | * @return javax.swing.JTextArea
|
|---|
| 220 | */
|
|---|
| 221 | private JTextArea getLogTextArea() {
|
|---|
| 222 | if (logTextArea == null) {
|
|---|
| 223 | logTextArea = new JTextArea();
|
|---|
| 224 | logTextArea.getDocument().addDocumentListener(new DocumentListener() {
|
|---|
| 225 |
|
|---|
| 226 | @Override
|
|---|
| 227 | public void removeUpdate(DocumentEvent arg0) {
|
|---|
| 228 | }
|
|---|
| 229 |
|
|---|
| 230 | @Override
|
|---|
| 231 | public void changedUpdate(DocumentEvent arg0) {
|
|---|
| 232 | }
|
|---|
| 233 | @Override
|
|---|
| 234 | public void insertUpdate(DocumentEvent e) {
|
|---|
| 235 | final Document doc = logTextArea.getDocument();
|
|---|
| 236 | final Element root = doc.getDefaultRootElement();
|
|---|
| 237 | if(root.getElementCount() <= 100){ // TODO 100縺ッ繝吶ち譖ク縺� |
|---|
| 238 | return;
|
|---|
| 239 | }
|
|---|
| 240 | EventQueue.invokeLater(new Runnable() {
|
|---|
| 241 | @Override
|
|---|
| 242 | public void run() {
|
|---|
| 243 | removeLines(doc, root);
|
|---|
| 244 | }
|
|---|
| 245 | });
|
|---|
| 246 | logTextArea.setCaretPosition(doc.getLength());
|
|---|
| 247 | }
|
|---|
| 248 | private void removeLines(Document doc, Element root) {
|
|---|
| 249 | Element fl = root.getElement(0);
|
|---|
| 250 | try{
|
|---|
| 251 | doc.remove(0, fl.getEndOffset());
|
|---|
| 252 | }catch(BadLocationException ble) {
|
|---|
| 253 | System.out.println(ble);
|
|---|
| 254 | }
|
|---|
| 255 | }
|
|---|
| 256 | });
|
|---|
| 257 | }
|
|---|
| 258 | return logTextArea;
|
|---|
| 259 | }
|
|---|
| 260 |
|
|---|
| 261 | void addLogText(String log){
|
|---|
| 262 | String[] logs = log.split("\n");
|
|---|
| 263 | for (String l : logs){
|
|---|
| 264 | // getLogTextArea().append((getLogTextArea().getDocument().getLength() > 0) ? "\n" + l : l);
|
|---|
| 265 | getLogTextArea().append(l);
|
|---|
| 266 | }
|
|---|
| 267 | if (log.endsWith("\n")){
|
|---|
| 268 | getLogTextArea().append("\n");
|
|---|
| 269 | }
|
|---|
| 270 |
|
|---|
| 271 | getLogTextArea().setCaretPosition(getLogTextArea().getDocument().getLength());
|
|---|
| 272 | }
|
|---|
| 273 |
|
|---|
| 274 |
|
|---|
| 275 | /**
|
|---|
| 276 | * This method initializes executeButton
|
|---|
| 277 | *
|
|---|
| 278 | * @return javax.swing.JButton
|
|---|
| 279 | */
|
|---|
| 280 | private JButton getExecuteButton() {
|
|---|
| 281 | if (executeButton == null) {
|
|---|
| 282 | executeButton = new JButton("Execute");
|
|---|
| 283 | executeButton.setEnabled(false);
|
|---|
| 284 | executeButton.addActionListener(new ActionListener() {
|
|---|
| 285 |
|
|---|
| 286 | @Override
|
|---|
| 287 | public void actionPerformed(ActionEvent arg0) {
|
|---|
| 288 | if (getExecuteButton().getText().equals("Execute")){
|
|---|
| 289 | setProcessing(true);
|
|---|
| 290 | doCompare();
|
|---|
| 291 | } else {
|
|---|
| 292 | executeButton.setEnabled(false);
|
|---|
| 293 | doStop();
|
|---|
| 294 | }
|
|---|
| 295 | }
|
|---|
| 296 | });
|
|---|
| 297 | }
|
|---|
| 298 | return executeButton;
|
|---|
| 299 | }
|
|---|
| 300 |
|
|---|
| 301 | /**
|
|---|
| 302 | * This method initializes optPanel
|
|---|
| 303 | *
|
|---|
| 304 | * @return javax.swing.JPanel
|
|---|
| 305 | */
|
|---|
| 306 | private JPanel getOptPanel() {
|
|---|
| 307 | if (optPanel == null) {
|
|---|
| 308 | optPanel = new JPanel();
|
|---|
| 309 | optPanel.setLayout(new BorderLayout());
|
|---|
| 310 | optPanel.add(getExecuteButton(), BorderLayout.EAST);
|
|---|
| 311 | }
|
|---|
| 312 | return optPanel;
|
|---|
| 313 | }
|
|---|
| 314 |
|
|---|
| 315 | private void selectFile(JTextField tf, int dialogType){
|
|---|
| 316 | JFileChooser fileChooser = new JFileChooser("./");
|
|---|
| 317 | fileChooser.setDialogType(dialogType);
|
|---|
| 318 | // 繝輔ぃ繧、繝ォ驕ク謚樒オ先棡蜿門セ� |
|---|
| 319 | int result = fileChooser.showOpenDialog(this);
|
|---|
| 320 | File file = fileChooser.getSelectedFile();
|
|---|
| 321 | if (result == JFileChooser.CANCEL_OPTION || file == null) {
|
|---|
| 322 | // 繧ュ繝」繝ウ繧サ繝ォ謚シ荳九√∪縺溘�縲√ヵ繧。繧、繝ォ驕ク謚槭↑縺励�縺溘a菴輔b縺励↑縺� |
|---|
| 323 | return;
|
|---|
| 324 | }
|
|---|
| 325 |
|
|---|
| 326 | try {
|
|---|
| 327 | tf.setText(file.getCanonicalPath());
|
|---|
| 328 | } catch (IOException e) {
|
|---|
| 329 | e.printStackTrace();
|
|---|
| 330 | }
|
|---|
| 331 | }
|
|---|
| 332 |
|
|---|
| 333 | private void validateExecute(){
|
|---|
| 334 | boolean enable = true;
|
|---|
| 335 | if (getEpPropsTextField().getText().isEmpty() ||
|
|---|
| 336 | getResourceCsvField().getText().isEmpty() ||
|
|---|
| 337 | getOutputTextField().getText().isEmpty()){
|
|---|
| 338 | enable = false;
|
|---|
| 339 | }
|
|---|
| 340 | getExecuteButton().setEnabled(enable);
|
|---|
| 341 | }
|
|---|
| 342 |
|
|---|
| 343 | CompareSubject compare = null;
|
|---|
| 344 |
|
|---|
| 345 | private void doCompare(){
|
|---|
| 346 | compare = new CompareSubject(new File(getResourceCsvField().getText()), new File(getEpPropsTextField().getText()), new SparqlQueryListener() {
|
|---|
| 347 |
|
|---|
| 348 | @Override
|
|---|
| 349 | public void sparqlExecuted(String query) {
|
|---|
| 350 | addLogText(query);
|
|---|
| 351 | }
|
|---|
| 352 | });
|
|---|
| 353 |
|
|---|
| 354 | compare.outputResult(new File(getOutputTextField().getText()), new CompareResultListener() {
|
|---|
| 355 |
|
|---|
| 356 | @Override
|
|---|
| 357 | public void uncaughtException(Thread thread, Throwable e) {
|
|---|
| 358 | JOptionPane.showMessageDialog(parent, "Execute error:"+e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
|
|---|
| 359 | setProcessing(false);
|
|---|
| 360 | }
|
|---|
| 361 |
|
|---|
| 362 | @Override
|
|---|
| 363 | public void resultReceived(boolean result) {
|
|---|
| 364 | setProcessing(false);
|
|---|
| 365 | }
|
|---|
| 366 | });
|
|---|
| 367 | }
|
|---|
| 368 |
|
|---|
| 369 | private void doStop(){
|
|---|
| 370 | if (compare != null){
|
|---|
| 371 | compare.stop();
|
|---|
| 372 | }
|
|---|
| 373 | }
|
|---|
| 374 |
|
|---|
| 375 |
|
|---|
| 376 | private void setProcessing(boolean isProcess){
|
|---|
| 377 | this.getEndpointsButton().setEnabled(!isProcess);
|
|---|
| 378 | this.getWordsButton().setEnabled(!isProcess);
|
|---|
| 379 | this.getOutputRefButton().setEnabled(!isProcess);
|
|---|
| 380 | if (isProcess){
|
|---|
| 381 | this.getExecuteButton().setText("Stop");
|
|---|
| 382 | } else {
|
|---|
| 383 | this.getExecuteButton().setText("Execute");
|
|---|
| 384 | }
|
|---|
| 385 | this.getExecuteButton().setEnabled(true);
|
|---|
| 386 | }
|
|---|
| 387 |
|
|---|
| 388 | /**
|
|---|
| 389 | * This method initializes outputTextField
|
|---|
| 390 | *
|
|---|
| 391 | * @return javax.swing.JTextField
|
|---|
| 392 | */
|
|---|
| 393 | private JTextField getOutputTextField() {
|
|---|
| 394 | if (outputTextField == null) {
|
|---|
| 395 | outputTextField = new JTextField();
|
|---|
| 396 | outputTextField.setEditable(false);
|
|---|
| 397 | }
|
|---|
| 398 | return outputTextField;
|
|---|
| 399 | }
|
|---|
| 400 |
|
|---|
| 401 | /**
|
|---|
| 402 | * This method initializes RefButton
|
|---|
| 403 | *
|
|---|
| 404 | * @return javax.swing.JButton
|
|---|
| 405 | */
|
|---|
| 406 | private JButton getOutputRefButton() {
|
|---|
| 407 | if (outputRefButton == null) {
|
|---|
| 408 | outputRefButton = new JButton("Ref");
|
|---|
| 409 | outputRefButton.addActionListener(new ActionListener() {
|
|---|
| 410 |
|
|---|
| 411 | @Override
|
|---|
| 412 | public void actionPerformed(ActionEvent e) {
|
|---|
| 413 | selectFile(getOutputTextField(), JFileChooser.SAVE_DIALOG);
|
|---|
| 414 | validateExecute();
|
|---|
| 415 | }
|
|---|
| 416 | });
|
|---|
| 417 | }
|
|---|
| 418 | return outputRefButton;
|
|---|
| 419 | }
|
|---|
| 420 |
|
|---|
| 421 | } // @jve:decl-index=0:visual-constraint="10,10"
|
|---|