001: package net.sourceforge.squirrel_sql.client.session.mainpanel;
002:
003: /*
004: * Copyright (C) 2003 Colin Bell
005: * colbell@users.sourceforge.net
006: *
007: * This library is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU Lesser General Public
009: * License as published by the Free Software Foundation; either
010: * version 2.1 of the License, or (at your option) any later version.
011: *
012: * This library is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this library; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
020: */
021: import java.awt.Component;
022: import java.awt.event.ActionEvent;
023: import java.awt.event.ActionListener;
024:
025: import javax.swing.*;
026: import javax.swing.plaf.basic.BasicComboBoxRenderer;
027:
028: import net.sourceforge.squirrel_sql.fw.gui.MemoryComboBox;
029:
030: /**
031: * This combobox holds the history of SQL statments executed.
032: *
033: * @author <A HREF="mailto:colbell@users.sourceforge.net">Colin Bell</A>
034: */
035: public class SQLHistoryComboBox extends MemoryComboBox {
036: public SQLHistoryComboBox(boolean useSharedModel) {
037: super ();
038: setModel(new SQLHistoryComboBoxModel(useSharedModel));
039: setRenderer(new Renderer());
040:
041: addActionListener(new ActionListener() {
042: public void actionPerformed(ActionEvent e) {
043: updateToolTip();
044: }
045: });
046: }
047:
048: public SQLHistoryComboBoxModel getTypedModel() {
049: return (SQLHistoryComboBoxModel) getModel();
050: }
051:
052: public void setUseSharedModel(boolean use) {
053: getTypedModel().setUseSharedModel(use);
054: }
055:
056: public boolean isUsingSharedDataModel() {
057: return getTypedModel().isUsingSharedDataModel();
058: }
059:
060: /**
061: * Ensure that the tooltip is still displayed when the combobox is
062: * collapsed.
063: */
064: private void updateToolTip() {
065: String tt = " ";
066: SQLHistoryItem shi = (SQLHistoryItem) getSelectedItem();
067: if (shi != null) {
068: tt = formatToolTip(shi.getSQL());
069: }
070: Component[] comps = SQLHistoryComboBox.this .getComponents();
071: for (int i = 0; i < comps.length; ++i) {
072: if (comps[i] instanceof JComponent) {
073: ((JComponent) comps[i]).setToolTipText(tt);
074: }
075: }
076: }
077:
078: private String formatToolTip(String tt) {
079: final StringBuffer buf = new StringBuffer();
080: buf.append("<HTML><PRE>");
081:
082: if (200 < tt.length()) {
083: buf.append(tt.substring(0, 200)).append(" ...");
084: } else {
085: buf.append(tt);
086: }
087:
088: buf.append("</PRE></HTML>");
089: return buf.toString();
090: }
091:
092: public void dispose() {
093: // The SQLHistoryComboBoxModel has a static member that prevents garbage collection.
094: // Therefore we need to remove the model.
095: setModel(new DefaultComboBoxModel());
096: }
097:
098: /**
099: * Renderer for this combobox. It displays the entire SQL for the current
100: * line as the tooltip. We use the HTML <PRE> tag in order to linebreak the
101: * SQL in the tooltip.
102: */
103: private final class Renderer extends BasicComboBoxRenderer {
104: public Component getListCellRendererComponent(JList list,
105: Object value, int index, boolean isSelected,
106: boolean cellHasFocus) {
107: if (isSelected) {
108: setBackground(list.getSelectionBackground());
109: setForeground(list.getSelectionForeground());
110: if (index != -1) {
111: final String tt = ((SQLHistoryItem) value).getSQL();
112: final String text = formatToolTip(tt);
113: list.setToolTipText(text);
114: }
115: } else {
116: setBackground(list.getBackground());
117: setForeground(list.getForeground());
118: }
119:
120: setFont(list.getFont());
121: setText((value == null) ? "" : value.toString());
122:
123: return this;
124: }
125: }
126: }
|