001: package isql;
002:
003: import javax.swing.JTabbedPane;
004: import javax.swing.ImageIcon;
005: import javax.swing.JLabel;
006: import javax.swing.JTextArea;
007: import javax.swing.JPanel;
008: import javax.swing.JFrame;
009: import javax.swing.JScrollPane;
010: import javax.swing.JOptionPane; // for debugging
011: import javax.swing.JDialog; // for debugging
012: import java.util.Map;
013: import java.util.Vector;
014: import java.util.Iterator;
015: import javax.swing.JTable;
016: import javax.swing.table.*;
017: import javax.swing.table.DefaultTableModel;
018: import javax.swing.event.TableModelEvent;
019:
020: import java.awt.*;
021: import java.awt.event.*;
022: import javax.swing.text.*;
023: import javax.swing.*;
024:
025: /**
026: * TabbedPane used to house the textareas of the tool.
027: *
028: * $Author: rahul_kumar $
029: * $Id: SQLTabbedPane.java,v 1.7 2004/01/03 11:55:30 rahul_kumar Exp $
030: * RK added on 20031228 20:43:58 - removed errorArea since not used.
031: */
032: public class SQLTabbedPane extends JPanel {
033: /** input for commands, text output, error (remove), history/scrap
034: * text areas */
035: JTextArea inputArea, outputArea, scrapArea;
036: JTabbedPane tabbedPane;
037: SmartDocument _sd;
038: JTable _jt;
039: //JPanel windowpanel;
040: InternalWindowPanel windowpanel;
041: SQLForm _form; // passed in tempo so tht we can bind keys
042:
043: /** offset for input area */
044: public final static int OFFSET_INPUT = 0;
045: /** offset for output area tab */
046: public final static int OFFSET_OUTPUT = 1;
047: /** offset for history area tab */
048: public final static int OFFSET_HISTORY = 2;
049: /** offset for jtable area */
050: public final static int OFFSET_JTABLE = 3;
051: /** offset for frame area */
052: public final static int OFFSET_FRAME = 4;
053:
054: public SQLTabbedPane(SQLForm form) {
055: ImageIcon icon = new ImageIcon("images/middle.gif");
056: tabbedPane = new JTabbedPane();
057: _form = form;
058:
059: inputArea = new JTextArea("select * from ");
060: inputArea.addKeyListener(new TabKeyListener(inputArea));
061: outputArea = new JTextArea();
062: outputArea.setFont(new Font("Monospaced", Font.PLAIN, 12));
063: scrapArea = new JTextArea("Your old SQLs come here.");
064: Component panel1 = makeScrollablePanel(inputArea);
065: tabbedPane.addTab("Input", icon, panel1,
066: "Enter SQL Statement here");
067: tabbedPane.setSelectedIndex(OFFSET_INPUT);
068: inputArea.setLineWrap(true);
069: inputArea.setWrapStyleWord(true);
070: inputArea.selectAll();
071:
072: Component panel2 = makeScrollablePanel(outputArea);
073: tabbedPane.addTab("Output", icon, panel2,
074: "View Results on SQL ");
075:
076: /*
077: * RK added on 20031228 20:34:57
078: Component panel3 = makeScrollablePanel(errorArea);
079: tabbedPane.addTab("Error", icon, panel3, "View Error Messages");
080: // setEditable works only after setText. next 2 lines work only in given order
081: outputArea.setText ("OutPut Area !");
082: outputArea.setEditable (false);
083: */
084:
085: Component panel4 = makeScrollablePanel(scrapArea);
086: tabbedPane
087: .addTab("History", icon, panel4, "OLD SQL statements");
088:
089: /*
090: TableSorter sorter = new TableSorter(dataModel);
091: JTable tableView = new JTable(sorter);
092: sorter.addMouseListenerToHeaderInTable(tableView);
093: */
094:
095: /*
096: TableSorter sorter = new TableSorter(new DefaultTableModel());
097: _jt = new JTable(sorter);
098: sorter.addMouseListenerToHeaderInTable(_jt);
099: */
100: // above didnt work
101: _jt = new JTable(new DefaultTableModel());
102: //_jt.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
103: _jt.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
104: _jt
105: .setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
106: _jt.setColumnSelectionAllowed(true);
107:
108: // Added on 20011216 to allow cut-paste between apps
109: ExcelAdapter myd = new ExcelAdapter(_jt);
110: //
111:
112: Component panel5 = makeScrollablePanel(_jt);
113: tabbedPane.addTab("Table", icon, panel5, "View Tabular result");
114:
115: windowpanel = new InternalWindowPanel(_form);
116: JScrollPane jswp = new JScrollPane(windowpanel);
117: jswp
118: .setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
119: tabbedPane.addTab("Frames", icon, jswp, "View Tables");
120:
121: // load key maps RK 20011029
122: javax.swing.Action[] act = TextAction.augmentList(inputArea
123: .getActions(), new Actions(_form).getActions());
124: //Keymap km = inputArea.getKeymap();
125: // I think i only need to load this once. it has loaded into all
126: // JTextComponents !!
127: JTextComponent.loadKeymap(inputArea.getKeymap(),
128: KeyBindings.defaultBindings, act);
129: JTextComponent.loadKeymap(outputArea.getKeymap(),
130: KeyBindings.defaultBindings, act);
131: JTextComponent.loadKeymap(scrapArea.getKeymap(),
132: KeyBindings.defaultBindings, act);
133: // we now need to retrieve all actions and check if bound by
134: // user, then bind to key. User should be able to print
135: // bindings.
136: loadActions(_jt);
137: setLayout(new GridLayout(1, 1));
138: add(tabbedPane);
139: }
140:
141: /** makes a given component scrollable. */
142: protected Component makeScrollablePanel(Component tArea) {
143: JScrollPane panel = new JScrollPane(tArea);
144: return panel;
145: }
146:
147: /** clearInputArea - called by clear button
148: * when user wants to type a fresh SQL
149: */
150: public void clearInputArea() {
151: inputArea.setText("");
152: }
153:
154: /*
155: public void clearErrorArea () {
156: //errorArea.setText("");
157: }
158: */
159: public void clearOutputArea() {
160: outputArea.setEditable(true);
161: outputArea.setText("");
162: outputArea.setEditable(false);
163: }
164:
165: public void makeInputAreaVisible() {
166: tabbedPane.setSelectedIndex(OFFSET_INPUT);
167: }
168:
169: public void makeOutputAreaVisible() {
170: tabbedPane.setSelectedIndex(OFFSET_OUTPUT);
171: }
172:
173: /*
174: public void makeErrorAreaVisible() {
175: tabbedPane.setSelectedIndex(2);
176: }
177: */
178: public void makeFramesVisible() {
179: tabbedPane.setSelectedIndex(OFFSET_FRAME);
180: }
181:
182: public void appendInputArea(String str) {
183: inputArea.append(str);
184: }
185:
186: public void insertInputArea(String str) {
187: inputArea.insert(str, inputArea.getCaretPosition());
188: }
189:
190: public void appendOutputArea(String str) {
191: outputArea.append(str);
192: }
193:
194: /*
195: public void appendErrorArea (String str) {
196: //errorArea.append(str);
197: }
198: */
199: public void setErrorArea(String str) {
200: //errorArea.setText(str);
201: }
202:
203: public String getSQL() {
204: String selec = "";
205: switch (tabbedPane.getSelectedIndex()) {
206: case OFFSET_INPUT:
207: selec = inputArea.getText();
208: break;
209: case OFFSET_HISTORY:
210: selec = scrapArea.getText();
211: break;
212: }
213: return (selec);
214: }
215:
216: public void updateTable(TableModel tm) {
217: _jt.setModel(tm);
218: _jt.tableChanged(new TableModelEvent(tm));
219: // added 20011123 RK
220: TableColumnModel cm = _jt.getColumnModel();
221: if (cm == null) {
222: System.err.println("table is null");
223: return;
224: }
225: if (tm instanceof TableMap) {
226: TableMap tmap = (TableMap) tm;
227: int cc = _jt.getColumnCount();
228: for (int i = 0; i < cc; i++) {
229: TableColumn tc = cm.getColumn(i);
230: int cw = 4 * tmap.getWidth(i + 1);
231: if (cw < 75)
232: cw = 75;
233: if (cw > 200)
234: cw = 200;
235: tc.setPreferredWidth(cw);
236: }
237: }
238:
239: }
240:
241: public void makeTableAreaVisible() {
242: tabbedPane.setSelectedIndex(OFFSET_JTABLE);
243: }
244:
245: public void makeHistoryAreaVisible() {
246: tabbedPane.setSelectedIndex(OFFSET_HISTORY);
247: }
248:
249: /** getSelection() - find which tab is visible and get the selected
250: * text from that tab. This is for the RUN_SELECTED function
251: * If nothing selected then NullPointer thrown
252: */
253: public String getSelection() {
254: /*
255: //Component c;
256: JTextArea c;
257: if ((c=(JTextArea)tabbedPane.getSelectedComponent()) != null) {
258: return c.getSelectedText();
259: }
260: return ("CCCC");
261: */
262: String selec = " ";
263: switch (tabbedPane.getSelectedIndex()) {
264: case OFFSET_INPUT:
265: selec = inputArea.getSelectedText();
266: break;
267: case OFFSET_HISTORY:
268: selec = scrapArea.getSelectedText();
269: break;
270: case -1:
271: selec = "No Selected Tab"; //cant happen
272: break;
273: default:
274: // This tab doesnt support selection
275: // this can happen and should throw something
276: selec = "";
277: }
278: if (selec == null)
279: throw new NullPointerException("NPE: Nothin' Selected");
280:
281: return (selec); // this will not happen since there are tabs
282: }
283:
284: /**putSQLInScrapArea (String OldSQL)
285: * place the old statement in scrap area so user can cut=paste it back
286: * and reuse it. We hate to see people retyping long statements
287: */
288: public void putSQLInScrapArea(String OldSQL) {
289: scrapArea.append("\n" + OldSQL);
290: }
291:
292: public void setDocument(SmartDocument doc) {
293: _sd = doc;
294: inputArea.setDocument(_sd);
295: //scrapArea.setDocument(_sd);
296: }
297:
298: public void setAbbreviationTable(Map htabbr) {
299: _sd.setAbbreviationTable(htabbr);
300: }
301:
302: public void setTabTable(String[] arrTab) {
303: _sd.setTabTable(arrTab);
304: }
305:
306: public void updateTabTable(String[] arrTab) {
307: String[] oldTab = _sd.getTabTable();
308: String[] newTab = new String[arrTab.length + oldTab.length];
309: System.arraycopy(oldTab, 0, newTab, 0, oldTab.length);
310: System.arraycopy(arrTab, 0, newTab, oldTab.length,
311: arrTab.length);
312: _sd.setTabTable(newTab);
313: }
314:
315: public String getOutputText() {
316: return outputArea.getText();
317: }
318:
319: public String getInputText() {
320: return inputArea.getText();
321: }
322:
323: /*
324: public String getErrorText(){
325: //return errorArea.getText();
326: }
327: */
328: public String getHistoryText() {
329: return scrapArea.getText();
330: }
331:
332: public TableModel getTableText() {
333: return _jt.getModel();
334: }
335:
336: public JTabbedPane getTabbedPane() {
337: return tabbedPane;
338: }
339:
340: public InternalWindowPanel getWindowPanel() {
341: return (InternalWindowPanel) windowpanel;
342: }
343:
344: public Component getActualComponent(int index) {
345: switch (index) {
346: case OFFSET_INPUT:
347: return inputArea;
348: case OFFSET_OUTPUT:
349: return outputArea;
350: //case 2: return errorArea;
351: case OFFSET_HISTORY:
352: return scrapArea;
353: case OFFSET_FRAME:
354: return _jt;
355: }
356: System.err
357: .println("getActualComponent couldnt handle " + index);
358: return null;
359: }
360:
361: /** loads actions for tabbed pane, this could be generalized to take
362: * a JComponent; requires jdk1.3, pls comment off if you have 1.2.
363: */
364:
365: public void loadActions(JTable jt) {
366: /* requires JDK1.3 - if you use 1.2.2 then comment off this
367: * method
368: * */
369: Map actions = TableActions.getActionMap(_form, jt);
370: Iterator it = actions.keySet().iterator();
371: while (it.hasNext()) {
372: String s = (String) it.next();
373: AbstractAction a = (AbstractAction) actions.get(s);
374: jt.getActionMap().put(s, a);
375: }
376:
377: /*
378: jt.getActionMap().put(Actions.gotoInput, new Actions.GotoInput(_form));
379: jt.getActionMap().put(Actions.gotoOutput, new Actions.GotoOutput(_form));
380: jt.getActionMap().put(Actions.gotoHistory, new Actions.GotoHistory(_form));
381: jt.getActionMap().put(TableActions.deleteAction, new TableActions.DeleteAction(_form, jt));
382: jt.getActionMap().put(TableActions.updateAction, new TableActions.UpdateAction(_form, jt));
383: jt.getActionMap().put(TableActions.insertAction, new TableActions.InsertAction(_form, jt));
384: jt.getActionMap().put(TableActions.searchAction, new TableActions.SearchAction(_form, jt));
385: jt.getActionMap().put(TableActions.sortAction, new TableActions.SortAction(_form, jt));
386: jt.getActionMap().put(TableActions.reversesortAction, new TableActions.ReverseSortAction(_form, jt));
387: */
388:
389: // this should come from a file, so user can customize
390: jt.getInputMap().put(KeyStroke.getKeyStroke("alt 1"),
391: Actions.gotoInput);
392: jt.getInputMap().put(KeyStroke.getKeyStroke("alt 2"),
393: Actions.gotoOutput);
394: jt.getInputMap().put(KeyStroke.getKeyStroke("alt 3"),
395: Actions.gotoHistory);
396: jt.getInputMap().put(KeyStroke.getKeyStroke("control DELETE"),
397: TableActions.deleteAction);
398: jt.getInputMap().put(
399: KeyStroke.getKeyStroke(KeyEvent.VK_DELETE, 0),
400: TableActions.deleteAction);
401: jt.getInputMap().put(KeyStroke.getKeyStroke("alt U"),
402: TableActions.updateAction);
403: jt.getInputMap().put(
404: KeyStroke.getKeyStroke(KeyEvent.VK_INSERT, 0),
405: TableActions.insertAction);
406: jt.getInputMap().put(KeyStroke.getKeyStroke('/'),
407: TableActions.searchAction);
408: jt.getInputMap().put(KeyStroke.getKeyStroke('o'),
409: TableActions.sortAction);
410: jt.getInputMap().put(KeyStroke.getKeyStroke('O'),
411: TableActions.reversesortAction);
412: jt.getInputMap().put(KeyStroke.getKeyStroke("control O"),
413: TableActions.asksortAction);
414:
415: //Map bindings = _form.TNS.getBindingsForTable();
416: Map bindings = _form.getBindings().getBindingsForTable();
417: //IsqlUtil.bindToTable(bindings, jt);
418: _form.getBindings().bindToTable(bindings, jt);
419: // now we should get all the keys in the jt action map, check in
420: // a hashtable if they have been bound by user, and then bind
421: // them
422:
423: }
424:
425: public static void main(String[] args) {
426: JFrame frame = new JFrame("SQLTabbedPane");
427:
428: frame.addWindowListener(new WindowAdapter() {
429: public void windowClosing(WindowEvent e) {
430: System.exit(0);
431: }
432: });
433:
434: frame.getContentPane().add(new SQLTabbedPane(null),
435: BorderLayout.CENTER);
436: frame.setSize(400, 125);
437: frame.setVisible(true);
438: }
439: // Collections.sort( tm.getDataVector(), new Comparator (column) {
440: // }
441: // then set data with sorted vectot and fire event
442: /*
443: private Comparator TableComparator = new Comparator(int column) {
444: public int compare(Object o1, Object o2) {
445: Object c1 = ((Vector) o1).get(column);
446: Object c2 = ((Vector) o2).get(column);
447: return c1.compareTo(c2);
448: }
449: };
450: */
451:
452: }
453: /*
454: // only listens for Control N. needs to be replaced with keymap
455: class TabKeyListener implements KeyListener {
456: private JTextArea jt;
457: public TabKeyListener(JTextArea jta){
458: jt = jta;
459: }
460:
461: public void keyTyped(KeyEvent e){}
462:
463: public void keyPressed(KeyEvent e){
464: if (e.isControlDown()){
465: switch(e.getKeyCode()) {
466: //case e.VK_N:
467: case java.awt.event.KeyEvent.VK_N:
468: int pos = jt.getCaretPosition();
469: if (pos==0) return;
470: String s = getLastWord(pos);
471: if (s==null) return;
472: // check if there is a match
473: String m = getLastMatchingWord(pos-s.length(),s);
474: if (m==null) return; // no match found
475: jt.replaceRange(m, pos-s.length(),pos);
476: break;
477: }
478: }
479:
480: }
481: public void keyReleased(KeyEvent e){}
482: private String getLastWord( int offset) {
483: String lastline;
484: int spos = 0;
485: try {
486: lastline = jt.getText(0, offset);
487:
488: int i = offset -1;
489: while (i-- > spos){
490: if (Character.isWhitespace(lastline.charAt(i)))
491: break;
492: }
493: // i contains the last space
494: i++;
495: if (i<0)i=0; // added since space in beg throws SOB ex
496: return lastline.substring(i);
497: } catch (BadLocationException e){
498: return null;
499: }
500: }
501: private String getLastMatchingWord (int offset, String patt){
502: try {
503: String line = jt.getText(0, offset);
504: int where = line.lastIndexOf(patt);
505: if (where < 0) return null;
506:
507: int i = where;
508: //String line = jt.getText(where, offset);
509: while (i++ < offset){
510: if (Character.isWhitespace(line.charAt(i)))
511: break;
512: }
513: return line.substring(where,i);
514: } catch (BadLocationException e){ return null; }
515: // for cases where user may send a space of whitespace. we dont
516: // want to have lotsof validations for such freak cases
517: catch (StringIndexOutOfBoundsException e){ return null; }
518: }
519:
520: }
521: */
|