package isql;
import javax.swing.JTabbedPane;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JTextArea;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JOptionPane; // for debugging
import javax.swing.JDialog; // for debugging
import java.util.Map;
import java.util.Vector;
import java.util.Iterator;
import javax.swing.JTable;
import javax.swing.table.*;
import javax.swing.table.DefaultTableModel;
import javax.swing.event.TableModelEvent;
import java.awt.*;
import java.awt.event.*;
import javax.swing.text.*;
import javax.swing.*;
/**
* TabbedPane used to house the textareas of the tool.
*
* $Author: rahul_kumar $
* $Id: SQLTabbedPane.java,v 1.7 2004/01/03 11:55:30 rahul_kumar Exp $
* RK added on 20031228 20:43:58 - removed errorArea since not used.
*/
public class SQLTabbedPane extends JPanel {
/** input for commands, text output, error (remove), history/scrap
* text areas */
JTextArea inputArea, outputArea, scrapArea;
JTabbedPane tabbedPane;
SmartDocument _sd;
JTable _jt;
//JPanel windowpanel;
InternalWindowPanel windowpanel;
SQLForm _form; // passed in tempo so tht we can bind keys
/** offset for input area */
public final static int OFFSET_INPUT = 0;
/** offset for output area tab */
public final static int OFFSET_OUTPUT = 1;
/** offset for history area tab */
public final static int OFFSET_HISTORY = 2;
/** offset for jtable area */
public final static int OFFSET_JTABLE = 3;
/** offset for frame area */
public final static int OFFSET_FRAME = 4;
public SQLTabbedPane(SQLForm form) {
ImageIcon icon = new ImageIcon("images/middle.gif");
tabbedPane = new JTabbedPane();
_form = form;
inputArea = new JTextArea("select * from ");
inputArea.addKeyListener(new TabKeyListener(inputArea));
outputArea = new JTextArea();
outputArea.setFont(new Font("Monospaced", Font.PLAIN, 12));
scrapArea = new JTextArea("Your old SQLs come here.");
Component panel1 = makeScrollablePanel(inputArea);
tabbedPane.addTab("Input", icon, panel1, "Enter SQL Statement here");
tabbedPane.setSelectedIndex(OFFSET_INPUT);
inputArea.setLineWrap (true);
inputArea.setWrapStyleWord (true);
inputArea.selectAll();
Component panel2 = makeScrollablePanel(outputArea);
tabbedPane.addTab("Output", icon, panel2, "View Results on SQL ");
/*
* RK added on 20031228 20:34:57
Component panel3 = makeScrollablePanel(errorArea);
tabbedPane.addTab("Error", icon, panel3, "View Error Messages");
// setEditable works only after setText. next 2 lines work only in given order
outputArea.setText ("OutPut Area !");
outputArea.setEditable (false);
*/
Component panel4 = makeScrollablePanel(scrapArea);
tabbedPane.addTab("History", icon, panel4, "OLD SQL statements");
/*
TableSorter sorter = new TableSorter(dataModel);
JTable tableView = new JTable(sorter);
sorter.addMouseListenerToHeaderInTable(tableView);
*/
/*
TableSorter sorter = new TableSorter(new DefaultTableModel());
_jt = new JTable(sorter);
sorter.addMouseListenerToHeaderInTable(_jt);
*/
// above didnt work
_jt = new JTable(new DefaultTableModel());
//_jt.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
_jt.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
_jt.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
_jt.setColumnSelectionAllowed( true );
// Added on 20011216 to allow cut-paste between apps
ExcelAdapter myd = new ExcelAdapter(_jt);
//
Component panel5 = makeScrollablePanel(_jt);
tabbedPane.addTab("Table", icon, panel5, "View Tabular result");
windowpanel = new InternalWindowPanel(_form);
JScrollPane jswp = new JScrollPane(windowpanel);
jswp.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
tabbedPane.addTab("Frames", icon, jswp, "View Tables");
// load key maps RK 20011029
javax.swing.Action[] act = TextAction.augmentList(inputArea.getActions(),
new Actions(_form).getActions());
//Keymap km = inputArea.getKeymap();
// I think i only need to load this once. it has loaded into all
// JTextComponents !!
JTextComponent.loadKeymap(inputArea.getKeymap(), KeyBindings.defaultBindings, act);
JTextComponent.loadKeymap(outputArea.getKeymap(), KeyBindings.defaultBindings, act);
JTextComponent.loadKeymap(scrapArea.getKeymap(), KeyBindings.defaultBindings, act);
// we now need to retrieve all actions and check if bound by
// user, then bind to key. User should be able to print
// bindings.
loadActions(_jt);
setLayout(new GridLayout(1, 1));
add(tabbedPane);
}
/** makes a given component scrollable. */
protected Component makeScrollablePanel(Component tArea) {
JScrollPane panel = new JScrollPane(tArea);
return panel;
}
/** clearInputArea - called by clear button
* when user wants to type a fresh SQL
*/
public void clearInputArea () {
inputArea.setText("");
}
/*
public void clearErrorArea () {
//errorArea.setText("");
}
*/
public void clearOutputArea () {
outputArea.setEditable (true);
outputArea.setText("");
outputArea.setEditable (false);
}
public void makeInputAreaVisible() {
tabbedPane.setSelectedIndex(OFFSET_INPUT);
}
public void makeOutputAreaVisible() {
tabbedPane.setSelectedIndex(OFFSET_OUTPUT);
}
/*
public void makeErrorAreaVisible() {
tabbedPane.setSelectedIndex(2);
}
*/
public void makeFramesVisible() {
tabbedPane.setSelectedIndex(OFFSET_FRAME);
}
public void appendInputArea (String str) {
inputArea.append(str);
}
public void insertInputArea (String str) {
inputArea.insert(str,inputArea.getCaretPosition());
}
public void appendOutputArea (String str) {
outputArea.append(str);
}
/*
public void appendErrorArea (String str) {
//errorArea.append(str);
}
*/
public void setErrorArea (String str) {
//errorArea.setText(str);
}
public String getSQL () {
String selec = "";
switch (tabbedPane.getSelectedIndex()) {
case OFFSET_INPUT: selec = inputArea.getText();
break;
case OFFSET_HISTORY: selec = scrapArea.getText();
break;
}
return (selec);
}
public void updateTable(TableModel tm){
_jt.setModel(tm);
_jt.tableChanged(new TableModelEvent(tm));
// added 20011123 RK
TableColumnModel cm = _jt.getColumnModel();
if (cm==null){
System.err.println( "table is null");
return;
}
if (tm instanceof TableMap){
TableMap tmap = (TableMap) tm;
int cc = _jt.getColumnCount();
for( int i = 0; i < cc; i++ ){
TableColumn tc = cm.getColumn(i);
int cw = 4*tmap.getWidth(i+1);
if (cw < 75) cw=75;
if (cw > 200) cw=200;
tc.setPreferredWidth( cw );
}
}
}
public void makeTableAreaVisible() {
tabbedPane.setSelectedIndex(OFFSET_JTABLE);
}
public void makeHistoryAreaVisible() {
tabbedPane.setSelectedIndex(OFFSET_HISTORY);
}
/** getSelection() - find which tab is visible and get the selected
* text from that tab. This is for the RUN_SELECTED function
* If nothing selected then NullPointer thrown
*/
public String getSelection() {
/*
//Component c;
JTextArea c;
if ((c=(JTextArea)tabbedPane.getSelectedComponent()) != null) {
return c.getSelectedText();
}
return ("CCCC");
*/
String selec = " ";
switch (tabbedPane.getSelectedIndex()) {
case OFFSET_INPUT: selec = inputArea.getSelectedText();
break;
case OFFSET_HISTORY: selec = scrapArea.getSelectedText();
break;
case -1: selec = "No Selected Tab"; //cant happen
break;
default:
// This tab doesnt support selection
// this can happen and should throw something
selec = "";
}
if (selec == null)
throw new NullPointerException("NPE: Nothin' Selected");
return (selec); // this will not happen since there are tabs
}
/**putSQLInScrapArea (String OldSQL)
* place the old statement in scrap area so user can cut=paste it back
* and reuse it. We hate to see people retyping long statements
*/
public void putSQLInScrapArea (String OldSQL) {
scrapArea.append("\n" + OldSQL);
}
public void setDocument(SmartDocument doc){
_sd = doc;
inputArea.setDocument(_sd);
//scrapArea.setDocument(_sd);
}
public void setAbbreviationTable(Map htabbr){
_sd.setAbbreviationTable(htabbr);
}
public void setTabTable(String[] arrTab){
_sd.setTabTable(arrTab);
}
public void updateTabTable(String[] arrTab){
String [] oldTab = _sd.getTabTable();
String[] newTab = new String[ arrTab.length + oldTab.length ];
System.arraycopy(oldTab,0,newTab,0,oldTab.length);
System.arraycopy(arrTab,0,newTab,oldTab.length,arrTab.length);
_sd.setTabTable(newTab);
}
public String getOutputText(){
return outputArea.getText();
}
public String getInputText(){
return inputArea.getText();
}
/*
public String getErrorText(){
//return errorArea.getText();
}
*/
public String getHistoryText(){
return scrapArea.getText();
}
public TableModel getTableText(){
return _jt.getModel();
}
public JTabbedPane getTabbedPane(){
return tabbedPane;
}
public InternalWindowPanel getWindowPanel(){
return (InternalWindowPanel)windowpanel;
}
public Component getActualComponent (int index){
switch (index){
case OFFSET_INPUT: return inputArea;
case OFFSET_OUTPUT: return outputArea;
//case 2: return errorArea;
case OFFSET_HISTORY: return scrapArea;
case OFFSET_FRAME: return _jt;
}
System.err.println( "getActualComponent couldnt handle "+index);
return null;
}
/** loads actions for tabbed pane, this could be generalized to take
* a JComponent; requires jdk1.3, pls comment off if you have 1.2.
*/
public void loadActions(JTable jt){
/* requires JDK1.3 - if you use 1.2.2 then comment off this
* method
* */
Map actions = TableActions.getActionMap(_form, jt);
Iterator it = actions.keySet().iterator();
while (it.hasNext()){
String s=(String) it.next();
AbstractAction a = (AbstractAction) actions.get(s);
jt.getActionMap().put(s, a);
}
/*
jt.getActionMap().put(Actions.gotoInput, new Actions.GotoInput(_form));
jt.getActionMap().put(Actions.gotoOutput, new Actions.GotoOutput(_form));
jt.getActionMap().put(Actions.gotoHistory, new Actions.GotoHistory(_form));
jt.getActionMap().put(TableActions.deleteAction, new TableActions.DeleteAction(_form, jt));
jt.getActionMap().put(TableActions.updateAction, new TableActions.UpdateAction(_form, jt));
jt.getActionMap().put(TableActions.insertAction, new TableActions.InsertAction(_form, jt));
jt.getActionMap().put(TableActions.searchAction, new TableActions.SearchAction(_form, jt));
jt.getActionMap().put(TableActions.sortAction, new TableActions.SortAction(_form, jt));
jt.getActionMap().put(TableActions.reversesortAction, new TableActions.ReverseSortAction(_form, jt));
*/
// this should come from a file, so user can customize
jt.getInputMap().put(KeyStroke.getKeyStroke("alt 1"),Actions.gotoInput);
jt.getInputMap().put(KeyStroke.getKeyStroke("alt 2"),Actions.gotoOutput);
jt.getInputMap().put(KeyStroke.getKeyStroke("alt 3"),Actions.gotoHistory);
jt.getInputMap().put(KeyStroke.getKeyStroke("control DELETE"),
TableActions.deleteAction);
jt.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_DELETE,0),
TableActions.deleteAction);
jt.getInputMap().put(KeyStroke.getKeyStroke("alt U"),
TableActions.updateAction);
jt.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_INSERT,0),
TableActions.insertAction);
jt.getInputMap().put(KeyStroke.getKeyStroke('/'),
TableActions.searchAction);
jt.getInputMap().put(KeyStroke.getKeyStroke('o'),
TableActions.sortAction);
jt.getInputMap().put(KeyStroke.getKeyStroke('O'),
TableActions.reversesortAction);
jt.getInputMap().put(KeyStroke.getKeyStroke("control O"),
TableActions.asksortAction);
//Map bindings = _form.TNS.getBindingsForTable();
Map bindings = _form.getBindings().getBindingsForTable();
//IsqlUtil.bindToTable(bindings, jt);
_form.getBindings().bindToTable(bindings, jt);
// now we should get all the keys in the jt action map, check in
// a hashtable if they have been bound by user, and then bind
// them
}
public static void main(String[] args) {
JFrame frame = new JFrame("SQLTabbedPane");
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {System.exit(0);}
});
frame.getContentPane().add(new SQLTabbedPane(null),
BorderLayout.CENTER);
frame.setSize(400, 125);
frame.setVisible(true);
}
// Collections.sort( tm.getDataVector(), new Comparator (column) {
// }
// then set data with sorted vectot and fire event
/*
private Comparator TableComparator = new Comparator(int column) {
public int compare(Object o1, Object o2) {
Object c1 = ((Vector) o1).get(column);
Object c2 = ((Vector) o2).get(column);
return c1.compareTo(c2);
}
};
*/
}
/*
// only listens for Control N. needs to be replaced with keymap
class TabKeyListener implements KeyListener {
private JTextArea jt;
public TabKeyListener(JTextArea jta){
jt = jta;
}
public void keyTyped(KeyEvent e){}
public void keyPressed(KeyEvent e){
if (e.isControlDown()){
switch(e.getKeyCode()) {
//case e.VK_N:
case java.awt.event.KeyEvent.VK_N:
int pos = jt.getCaretPosition();
if (pos==0) return;
String s = getLastWord(pos);
if (s==null) return;
// check if there is a match
String m = getLastMatchingWord(pos-s.length(),s);
if (m==null) return; // no match found
jt.replaceRange(m, pos-s.length(),pos);
break;
}
}
}
public void keyReleased(KeyEvent e){}
private String getLastWord( int offset) {
String lastline;
int spos = 0;
try {
lastline = jt.getText(0, offset);
int i = offset -1;
while (i-- > spos){
if (Character.isWhitespace(lastline.charAt(i)))
break;
}
// i contains the last space
i++;
if (i<0)i=0; // added since space in beg throws SOB ex
return lastline.substring(i);
} catch (BadLocationException e){
return null;
}
}
private String getLastMatchingWord (int offset, String patt){
try {
String line = jt.getText(0, offset);
int where = line.lastIndexOf(patt);
if (where < 0) return null;
int i = where;
//String line = jt.getText(where, offset);
while (i++ < offset){
if (Character.isWhitespace(line.charAt(i)))
break;
}
return line.substring(where,i);
} catch (BadLocationException e){ return null; }
// for cases where user may send a space of whitespace. we dont
// want to have lotsof validations for such freak cases
catch (StringIndexOutOfBoundsException e){ return null; }
}
}
*/
|