KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > lucane > applications > sqlnavigator > QueryPanel


1 package org.lucane.applications.sqlnavigator;
2
3 import java.awt.*;
4 import java.awt.event.*;
5 import javax.swing.*;
6
7 import org.lucane.client.widgets.DialogBox;
8
9 import java.util.Vector JavaDoc;
10
11
12 /**
13  * Reusable object allowing sql query edition
14  * Has history and coloration.
15  */

16 class QueryPanel extends JPanel implements ActionListener, KeyListener
17 {
18     public Vector JavaDoc history;
19     
20     Navigator parent;
21     JTextPane jtaQuery;
22     JButton jbtPrevious;
23     JButton jbtNext;
24     JButton jbtClean;
25     JButton jbtExecute;
26     
27     transient Syntaxer syntaxer;
28     
29     int histindex;
30     
31     
32     /**
33      * Constructor
34      *
35      * @param parent the parent Navigator
36      * (used for displaying results)
37      */

38     public QueryPanel(Navigator parent)
39     {
40         this.parent = parent;
41         
42         
43         jtaQuery = new JTextPane();
44         jtaQuery.addKeyListener(this);
45         
46         syntaxer = null;
47         
48         
49         history = new Vector JavaDoc();
50         histindex = 0;
51         
52         setLayout(new BorderLayout());
53                         
54         jbtPrevious = new JButton("<<");
55         jbtPrevious.addActionListener(this);
56         jbtNext = new JButton(">>");
57         jbtNext.addActionListener(this);
58         jbtClean = new JButton(tr("clear"));
59         jbtClean.addActionListener(this);
60         jbtExecute = new JButton(tr("execute"));
61         jbtExecute.addActionListener(this);
62         
63         add(new JScrollPane(jtaQuery), BorderLayout.CENTER);
64         
65         JPanel jp = new JPanel();
66         jp.setLayout(new GridLayout(4, 1));
67         jp.add(jbtPrevious);
68         jp.add(jbtNext);
69         jp.add(jbtClean);
70         jp.add(jbtExecute);
71         
72         add(jp, BorderLayout.EAST);
73     }
74
75
76     /**
77      * Interface KeyListener
78      */

79     public void keyPressed(KeyEvent e) {}
80     public void keyTyped(KeyEvent e) {}
81     public void keyReleased(KeyEvent e)
82     {
83         if(e == null ||
84             (e.getKeyChar() != KeyEvent.VK_BACK_SPACE && e.getKeyChar() != KeyEvent.VK_DELETE)
85             )
86         {
87             syntaxer = new Syntaxer(jtaQuery);
88
89             if(e == null)
90                 syntaxer.forcing();
91   
92             syntaxer.start();
93         }
94     }
95     
96     /**
97      * ActionListener Interface.
98      * Handle click on a button
99      */

100     public void actionPerformed(ActionEvent ae)
101     {
102         /* history : previous */
103         if(ae.getSource() == jbtPrevious && histindex > 0)
104         {
105             jtaQuery.setText((String JavaDoc)history.elementAt(--histindex));
106             parent.write(tr("history1") + (histindex+1) + " " + tr("history2") + " " +history.size());
107             keyReleased(null);
108         }
109         
110         /* history : next */
111         else if(ae.getSource() == jbtNext && histindex < history.size()-1)
112         {
113             jtaQuery.setText((String JavaDoc)history.elementAt(++histindex));
114             parent.write(tr("history1") + (histindex+1) + " " + tr("history2") + " " +history.size());
115             keyReleased(null);
116         }
117         
118         /* clear */
119         else if(ae.getSource() == jbtClean)
120         {
121             jtaQuery.setText("");
122         }
123         
124         /* execute query */
125         else if(ae.getSource() == jbtExecute)
126         {
127             try
128             {
129                 SqlResult sr = parent.plugin.executeQuery(jtaQuery.getText());
130                 parent.result.setVisible(false);
131                 parent.result.removeAll();
132                         
133                 JScrollPane sp = parent.plugin.getScrollPane(sr);
134         
135                 if(sp == null)
136                     sp = new JScrollPane();
137         
138                 parent.result.add(sp, BorderLayout.CENTER);
139
140                 parent.result.setVisible(true);
141                 history.addElement(jtaQuery.getText());
142                 histindex = history.size()-1;
143             }
144             catch(Exception JavaDoc e)
145             {
146                 parent.write(tr("error.result.read"));
147                 parent.write(e.getMessage());
148                 DialogBox.error(tr("error.result.read") + "\n" + e.getMessage());
149             }
150         }
151     }
152     
153     private String JavaDoc tr(String JavaDoc s)
154     {
155         return parent.plugin.tr(s);
156     }
157 }
158
Popular Tags