root/BH13SPARQLBuilder/src/hozo/sparql/plugin/compare/ComparePanel.java @ 122

リビジョン 17, 16.5 KB (コミッタ: kozaki, 11 年 前)

Commitし直します.

  • 属性 svn:mime-type の設定値 text/plain
行番号 
1package hozo.sparql.plugin.compare;
2
3import java.awt.GridBagLayout;
4import javax.swing.JPanel;
5import javax.swing.JTextField;
6import javax.swing.JButton;
7
8import java.awt.BorderLayout;
9import java.awt.Component;
10import java.awt.Dimension;
11import java.awt.EventQueue;
12import java.awt.FlowLayout;
13import java.awt.GridBagConstraints;
14import java.awt.Insets;
15import java.awt.event.ActionEvent;
16import java.awt.event.ActionListener;
17import java.io.File;
18import java.io.IOException;
19
20import javax.swing.ButtonGroup;
21import javax.swing.JFileChooser;
22import javax.swing.JOptionPane;
23import javax.swing.JScrollPane;
24import javax.swing.JSeparator;
25import javax.swing.JTextArea;
26import javax.swing.JLabel;
27import javax.swing.JRadioButton;
28import javax.swing.SwingConstants;
29import javax.swing.event.DocumentEvent;
30import javax.swing.event.DocumentListener;
31import javax.swing.text.BadLocationException;
32import javax.swing.text.Document;
33import javax.swing.text.Element;
34
35import hozo.sparql.SparqlAccessor;
36import hozo.sparql.SparqlQueryListener;
37
38public class ComparePanel extends JPanel {
39
40        private static final long serialVersionUID = 1L;
41        private JTextField endpointsTextField = null;  //  @jve:decl-index=0:visual-constraint="542,195"
42        private JButton endpointsButton = null;
43        private JTextField wordsTextField = null;
44        private JButton wordsButton = null;
45        private JScrollPane logScrollPane = null;
46        private JTextArea logTextArea = null;
47        private JLabel endpointsLabel = null;
48        private JLabel wordsLabel = null;
49        private JButton executeButton = null;
50        private JPanel optPanel = null;
51        private JPanel matchPanel = null;
52        private JRadioButton matchFullRadioButton = null;
53        private JRadioButton matchPartRadioButton = null;
54        private JRadioButton findSubjectRadioButton = null;
55        private JRadioButton findObjectRadioButton = null;
56        private JRadioButton findLabelObjectRadioButton = null;
57        private JSeparator separator = null;
58        private JLabel outputLabel = null;
59        private JTextField outputTextField = null;
60        private JButton outputRefButton = null;
61        private Component parent;
62
63        /**
64         * This is the default constructor
65         */
66        public ComparePanel(Component parent) {
67                super();
68                initialize();
69                this.parent= parent;
70        }
71
72        /**
73         * This method initializes this
74         *
75         * @return void
76         */
77        private void initialize() {
78                GridBagConstraints gridBagConstraints111 = new GridBagConstraints();
79                gridBagConstraints111.gridx = 2;
80                gridBagConstraints111.insets = new Insets(0, 0, 0, 10);
81                gridBagConstraints111.gridy = 3;
82                GridBagConstraints gridBagConstraints10 = new GridBagConstraints();
83                gridBagConstraints10.fill = GridBagConstraints.HORIZONTAL;
84                gridBagConstraints10.gridy = 3;
85                gridBagConstraints10.weightx = 1.0;
86                gridBagConstraints10.insets = new Insets(5, 10, 5, 0);
87                gridBagConstraints10.gridx = 1;
88                GridBagConstraints gridBagConstraints9 = new GridBagConstraints();
89                gridBagConstraints9.gridx = 0;
90                gridBagConstraints9.gridy = 3;
91                outputLabel = new JLabel("Output File");
92                GridBagConstraints gridBagConstraints8 = new GridBagConstraints();
93                gridBagConstraints8.gridx = 0;
94                gridBagConstraints8.gridwidth = 3;
95                gridBagConstraints8.fill = GridBagConstraints.HORIZONTAL;
96                gridBagConstraints8.gridy = 4;
97                GridBagConstraints gridBagConstraints5 = new GridBagConstraints();
98                gridBagConstraints5.gridx = 0;
99                gridBagConstraints5.insets = new Insets(0, 10, 0, 0);
100                gridBagConstraints5.gridy = 2;
101                wordsLabel = new JLabel();
102                wordsLabel.setText("Word List File");
103                GridBagConstraints gridBagConstraints4 = new GridBagConstraints();
104                gridBagConstraints4.gridx = 0;
105                gridBagConstraints4.insets = new Insets(0, 10, 0, 0);
106                gridBagConstraints4.gridy = 0;
107                endpointsLabel = new JLabel();
108                endpointsLabel.setText("Endpoint List File");
109                GridBagConstraints gridBagConstraints3 = new GridBagConstraints();
110                gridBagConstraints3.fill = GridBagConstraints.BOTH;
111                gridBagConstraints3.gridy = 5;
112                gridBagConstraints3.weightx = 1.0;
113                gridBagConstraints3.weighty = 1.0;
114                gridBagConstraints3.insets = new Insets(10, 10, 10, 10);
115                gridBagConstraints3.gridwidth = 3;
116                gridBagConstraints3.gridx = 0;
117                GridBagConstraints gridBagConstraints2 = new GridBagConstraints();
118                gridBagConstraints2.gridx = 2;
119                gridBagConstraints2.insets = new Insets(0, 0, 0, 10);
120                gridBagConstraints2.gridy = 2;
121                GridBagConstraints gridBagConstraints11 = new GridBagConstraints();
122                gridBagConstraints11.fill = GridBagConstraints.HORIZONTAL;
123                gridBagConstraints11.gridy = 2;
124                gridBagConstraints11.weightx = 1.0;
125                gridBagConstraints11.insets = new Insets(5, 10, 5, 0);
126                gridBagConstraints11.gridx = 1;
127                GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
128                gridBagConstraints1.fill = GridBagConstraints.NONE;
129                gridBagConstraints1.gridy = 0;
130                gridBagConstraints1.insets = new Insets(10, 0, 0, 10);
131                gridBagConstraints1.gridx = 2;
132                GridBagConstraints gridBagConstraints = new GridBagConstraints();
133                gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
134                gridBagConstraints.gridy = 0;
135                gridBagConstraints.insets = new Insets(14, 10, 5, 0);
136                gridBagConstraints.gridx = 1;
137                this.setLayout(new GridBagLayout());
138                this.add(getEndpointsTextField(), gridBagConstraints);
139                this.add(getEndpointsButton(), gridBagConstraints1);
140                this.add(getWordsTextField(), gridBagConstraints11);
141                this.add(getWordsButton(), gridBagConstraints2);
142                this.add(getLogScrollPane(), gridBagConstraints3);
143                this.add(endpointsLabel, gridBagConstraints4);
144                this.add(wordsLabel, gridBagConstraints5);
145                this.add(getOptPanel(), gridBagConstraints8);
146                this.add(outputLabel, gridBagConstraints9);
147                this.add(getOutputTextField(), gridBagConstraints10);
148                this.add(getOutputRefButton(), gridBagConstraints111);
149        }
150
151        /**
152         * This method initializes endpointsTextField   
153         *     
154         * @return javax.swing.JTextField       
155         */
156        private JTextField getEndpointsTextField() {
157                if (endpointsTextField == null) {
158                        endpointsTextField = new JTextField();
159                        endpointsTextField.setEditable(false);
160                }
161                return endpointsTextField;
162        }
163
164        /**
165         * This method initializes endpointsButton     
166         *     
167         * @return javax.swing.JButton 
168         */
169        private JButton getEndpointsButton() {
170                if (endpointsButton == null) {
171                        endpointsButton = new JButton("Ref");
172                        endpointsButton.addActionListener(new ActionListener() {
173                               
174                                @Override
175                                public void actionPerformed(ActionEvent arg0) {
176                                        selectFile(getEndpointsTextField(), JFileChooser.OPEN_DIALOG);
177                                        validateExecute();
178                                }
179                        });
180                }
181                return endpointsButton;
182        }
183
184        /**
185         * This method initializes wordsTextField       
186         *     
187         * @return javax.swing.JTextField       
188         */
189        private JTextField getWordsTextField() {
190                if (wordsTextField == null) {
191                        wordsTextField = new JTextField();
192                        wordsTextField.setEditable(false);
193                }
194                return wordsTextField;
195        }
196
197        /**
198         * This method initializes wordsButton 
199         *     
200         * @return javax.swing.JButton 
201         */
202        private JButton getWordsButton() {
203                if (wordsButton == null) {
204                        wordsButton = new JButton("Ref");
205                        wordsButton.addActionListener(new ActionListener() {
206                               
207                                @Override
208                                public void actionPerformed(ActionEvent arg0) {
209                                        selectFile(getWordsTextField(), JFileChooser.OPEN_DIALOG);
210                                        validateExecute();
211                                }
212                        });
213                }
214                return wordsButton;
215        }
216
217        /**
218         * This method initializes logScrollPane       
219         *     
220         * @return javax.swing.JScrollPane     
221         */
222        private JScrollPane getLogScrollPane() {
223                if (logScrollPane == null) {
224                        logScrollPane = new JScrollPane();
225                        logScrollPane.setViewportView(getLogTextArea());
226                }
227                return logScrollPane;
228        }
229
230        /**
231         * This method initializes logTextArea 
232         *     
233         * @return javax.swing.JTextArea       
234         */
235        private JTextArea getLogTextArea() {
236                if (logTextArea == null) {
237                        logTextArea = new JTextArea();
238                        logTextArea.getDocument().addDocumentListener(new DocumentListener() {
239                               
240                                @Override
241                                public void removeUpdate(DocumentEvent arg0) {
242                                }
243                               
244                                @Override
245                                public void changedUpdate(DocumentEvent arg0) {
246                                }
247                                @Override
248                                public void insertUpdate(DocumentEvent e) {
249                                        final Document doc = logTextArea.getDocument();
250                                        final Element root = doc.getDefaultRootElement();
251                                        if(root.getElementCount() <= 100){ // TODO 100縺ッ繝吶ち譖ク縺�
252                                                return;
253                                        }
254                                        EventQueue.invokeLater(new Runnable() {
255                                                @Override
256                                                public void run() {
257                                                        removeLines(doc, root);
258                                                }
259                                        });
260                                        logTextArea.setCaretPosition(doc.getLength());
261                                }
262                                private void removeLines(Document doc, Element root) {
263                                        Element fl = root.getElement(0);
264                                        try{
265                                                doc.remove(0, fl.getEndOffset());
266                                        }catch(BadLocationException ble) {
267                                                System.out.println(ble);
268                                        }
269                                }
270                        });
271                }
272                return logTextArea;
273        }
274       
275        void addLogText(String log){
276                String[] logs = log.split("\n");
277                for (String l : logs){
278//                      getLogTextArea().append((getLogTextArea().getDocument().getLength() > 0) ? "\n" + l : l);
279                        getLogTextArea().append(l);
280                }
281                if (log.endsWith("\n")){
282                        getLogTextArea().append("\n");
283                }
284               
285                getLogTextArea().setCaretPosition(getLogTextArea().getDocument().getLength());
286        }
287
288
289        /**
290         * This method initializes executeButton       
291         *     
292         * @return javax.swing.JButton 
293         */
294        private JButton getExecuteButton() {
295                if (executeButton == null) {
296                        executeButton = new JButton("Execute");
297                        executeButton.setEnabled(false);
298                        executeButton.addActionListener(new ActionListener() {
299                               
300                                @Override
301                                public void actionPerformed(ActionEvent arg0) {
302                                        if (getExecuteButton().getText().equals("Execute")){
303                                                setProcessing(true);
304                                                doCompare();
305                                        } else {
306                                                executeButton.setEnabled(false);
307                                                doStop();
308                                        }
309                                }
310                        });
311                }
312                return executeButton;
313        }
314
315        /**
316         * This method initializes optPanel     
317         *     
318         * @return javax.swing.JPanel   
319         */
320        private JPanel getOptPanel() {
321                if (optPanel == null) {
322                        optPanel = new JPanel();
323                        optPanel.setLayout(new BorderLayout());
324                        optPanel.add(getExecuteButton(), BorderLayout.EAST);
325                        optPanel.add(getMatchPanel(), BorderLayout.CENTER);
326                }
327                return optPanel;
328        }
329
330        /**
331         * This method initializes matchPanel   
332         *     
333         * @return javax.swing.JPanel   
334         */
335        private JPanel getMatchPanel() {
336                if (matchPanel == null) {
337                        FlowLayout flowLayout = new FlowLayout();
338                        flowLayout.setVgap(0);
339                        matchPanel = new JPanel();
340                        matchPanel.setLayout(flowLayout);
341                        matchPanel.add(getMatchFullRadioButton(), null);
342                        matchPanel.add(getMatchPartRadioButton(), null);
343                        matchPanel.add(getSeparator(), null);
344                        matchPanel.add(getFindSubjectRadioButton(), null);
345                        matchPanel.add(getFindObjectRadioButton(), null);
346                        matchPanel.add(getFindLabelObjectRadioButton(), null);
347                        ButtonGroup gp1 = new ButtonGroup();
348                        gp1.add(getMatchFullRadioButton());
349                        gp1.add(getMatchPartRadioButton());
350                        getMatchFullRadioButton().setSelected(true);
351                        ButtonGroup gp2 = new ButtonGroup();
352                        gp2.add(getFindSubjectRadioButton());
353                        gp2.add(getFindObjectRadioButton());
354                        gp2.add(getFindLabelObjectRadioButton());
355                        getFindSubjectRadioButton().setSelected(true);
356                }
357                return matchPanel;
358        }
359       
360        private int getFindType(){
361                if (getFindSubjectRadioButton().isSelected()){
362                        return SparqlAccessor.FIND_TARGET_SUBJECT;
363                }
364                if (getFindObjectRadioButton().isSelected()){
365                        return SparqlAccessor.FIND_TARGET_OBJECT;
366                }
367                if (getFindLabelObjectRadioButton().isSelected()){
368                        return SparqlAccessor.FIND_TARGET_SPECIFIC_OBJECT;
369                }
370                return SparqlAccessor.FIND_TARGET_ALL;
371        }
372       
373        private boolean isFullMatch(){
374                return getMatchFullRadioButton().isSelected();
375        }
376
377        private void selectFile(JTextField tf, int dialogType){
378                JFileChooser fileChooser = new JFileChooser("./");
379                fileChooser.setDialogType(dialogType);
380                // 繝輔ぃ繧、繝ォ驕ク謚樒オ先棡蜿門セ�
381                int result = fileChooser.showOpenDialog(this);
382                File file = fileChooser.getSelectedFile();
383                if (result == JFileChooser.CANCEL_OPTION || file == null) {
384                        // 繧ュ繝」繝ウ繧サ繝ォ謚シ荳九€√∪縺溘�縲√ヵ繧。繧、繝ォ驕ク謚槭↑縺励�縺溘a菴輔b縺励↑縺�
385                        return;
386                }
387               
388                try {
389                        tf.setText(file.getCanonicalPath());
390                } catch (IOException e) {
391                        e.printStackTrace();
392                }
393        }
394       
395        private void validateExecute(){
396                boolean enable = true;
397                if (getEndpointsTextField().getText().isEmpty() ||
398                                getWordsTextField().getText().isEmpty() ||
399                                getOutputTextField().getText().isEmpty()){
400                        enable = false;
401                }
402                getExecuteButton().setEnabled(enable);
403        }
404
405        Compare compare = null;
406       
407        private void doCompare(){
408                compare = new Compare(new File(getWordsTextField().getText()), new File(getEndpointsTextField().getText()), new SparqlQueryListener() {
409                       
410                        @Override
411                        public void sparqlExecuted(String query) {
412                                addLogText(query);
413                        }
414                });
415
416                compare.outputResult(getFindType(), isFullMatch(), new File(getOutputTextField().getText()), new CompareResultListener() {
417
418                        @Override
419                        public void uncaughtException(Thread thread, Throwable e) {
420                                JOptionPane.showMessageDialog(parent, "Execute error:"+e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
421                                setProcessing(false);
422                        }
423
424                        @Override
425                        public void resultReceived(boolean result) {
426                                setProcessing(false);
427                        }
428                });
429        }
430       
431        private void doStop(){
432                if (compare != null){
433                        compare.stop();
434                }
435        }
436       
437
438        private void setProcessing(boolean isProcess){
439                this.getEndpointsButton().setEnabled(!isProcess);
440                this.getWordsButton().setEnabled(!isProcess);
441                this.getOutputRefButton().setEnabled(!isProcess);
442                this.getMatchFullRadioButton().setEnabled(!isProcess);
443                this.getMatchPartRadioButton().setEnabled(!isProcess);
444                this.getFindSubjectRadioButton().setEnabled(!isProcess);
445                this.getFindObjectRadioButton().setEnabled(!isProcess);
446                this.getFindLabelObjectRadioButton().setEnabled(!isProcess);
447                if (isProcess){
448                        this.getExecuteButton().setText("Stop");
449                } else {
450                        this.getExecuteButton().setText("Execute");
451                }
452                this.getExecuteButton().setEnabled(true);
453        }
454       
455        /**
456         * This method initializes matchFullRadioButton
457         *     
458         * @return javax.swing.JRadioButton     
459         */
460        private JRadioButton getMatchFullRadioButton() {
461                if (matchFullRadioButton == null) {
462                        matchFullRadioButton = new JRadioButton("Full Match");
463                }
464                return matchFullRadioButton;
465        }
466
467        /**
468         * This method initializes matchPartRadioButton
469         *     
470         * @return javax.swing.JRadioButton     
471         */
472        private JRadioButton getMatchPartRadioButton() {
473                if (matchPartRadioButton == null) {
474                        matchPartRadioButton = new JRadioButton("Part Match");
475                }
476                return matchPartRadioButton;
477        }
478
479        /**
480         * This method initializes findSubjectRadioButton       
481         *     
482         * @return javax.swing.JRadioButton     
483         */
484        private JRadioButton getFindSubjectRadioButton() {
485                if (findSubjectRadioButton == null) {
486                        findSubjectRadioButton = new JRadioButton("Find Subject");
487                }
488                return findSubjectRadioButton;
489        }
490
491        /**
492         * This method initializes findObjectRadioButton       
493         *     
494         * @return javax.swing.JRadioButton     
495         */
496        private JRadioButton getFindObjectRadioButton() {
497                if (findObjectRadioButton == null) {
498                        findObjectRadioButton = new JRadioButton("Find All Object");
499                }
500                return findObjectRadioButton;
501        }
502
503        /**
504         * This method initializes findLabelObjectRadioButton   
505         *     
506         * @return javax.swing.JRadioButton     
507         */
508        private JRadioButton getFindLabelObjectRadioButton() {
509                if (findLabelObjectRadioButton == null) {
510                        findLabelObjectRadioButton = new JRadioButton("Find Label Object");
511                }
512                return findLabelObjectRadioButton;
513        }
514
515        /**
516         * This method initializes separator   
517         *     
518         * @return JSeparator   
519         */
520        private JSeparator getSeparator() {
521                if (separator == null) {
522                        separator = new JSeparator(SwingConstants.VERTICAL);
523                        separator.setPreferredSize(new Dimension(5, 20));
524                }
525                return separator;
526        }
527
528        /**
529         * This method initializes outputTextField     
530         *     
531         * @return javax.swing.JTextField       
532         */
533        private JTextField getOutputTextField() {
534                if (outputTextField == null) {
535                        outputTextField = new JTextField();
536                        outputTextField.setEditable(false);
537                }
538                return outputTextField;
539        }
540
541        /**
542         * This method initializes RefButton   
543         *     
544         * @return javax.swing.JButton 
545         */
546        private JButton getOutputRefButton() {
547                if (outputRefButton == null) {
548                        outputRefButton = new JButton("Ref");
549                        outputRefButton.addActionListener(new ActionListener() {
550                               
551                                @Override
552                                public void actionPerformed(ActionEvent e) {
553                                        selectFile(getOutputTextField(), JFileChooser.SAVE_DIALOG);
554                                        validateExecute();
555                                }
556                        });
557                }
558                return outputRefButton;
559        }
560       
561}  //  @jve:decl-index=0:visual-constraint="10,10"
Note: リポジトリブラウザについてのヘルプは TracBrowser を参照してください。