001: /*
002: * The contents of this file are subject to the Mozilla Public License
003: * Version 1.1 (the "License"); you may not use this file except in
004: * compliance with the License. You may obtain a copy of the License at
005: * http://www.mozilla.org/MPL/
006: *
007: * Software distributed under the License is distributed on an "AS IS"
008: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
009: * License for the specific language governing rights and limitations
010: * under the License.
011: *
012: * The Original Code is iSQL-Viewer, A Mutli-Platform Database Tool.
013: *
014: * The Initial Developer of the Original Code is iSQL-Viewer, A Mutli-Platform Database Tool.
015: * Portions created by Mark A. Kobold are Copyright (C) 2000-2007. All Rights Reserved.
016: *
017: * Contributor(s):
018: * Mark A. Kobold [mkobold <at> isqlviewer <dot> com].
019: *
020: * If you didn't download this code from the following link, you should check
021: * if you aren't using an obsolete version: http://www.isqlviewer.com
022: */
023: package org.isqlviewer.ui;
024:
025: import java.awt.Component;
026: import java.awt.GridBagConstraints;
027: import java.awt.GridBagLayout;
028: import java.awt.event.ActionEvent;
029: import java.awt.event.ActionListener;
030: import java.text.NumberFormat;
031: import java.util.prefs.Preferences;
032:
033: import javax.swing.BorderFactory;
034: import javax.swing.Box;
035: import javax.swing.JButton;
036: import javax.swing.JComponent;
037: import javax.swing.JLabel;
038: import javax.swing.JMenuBar;
039: import javax.swing.JScrollPane;
040: import javax.swing.JTextField;
041: import javax.swing.ScrollPaneConstants;
042: import javax.swing.SwingConstants;
043: import javax.swing.event.DocumentEvent;
044: import javax.swing.event.DocumentListener;
045: import javax.swing.event.ListSelectionEvent;
046: import javax.swing.event.ListSelectionListener;
047:
048: import org.isqlviewer.swing.EnhancedTable;
049: import org.isqlviewer.swing.SwingUtilities;
050: import org.isqlviewer.swing.action.SwingEventManager;
051: import org.isqlviewer.swing.table.EnhancedTableModel;
052: import org.isqlviewer.swing.table.ResultSetTableModel;
053: import org.isqlviewer.ui.laf.SortableHeaderRenderer;
054: import org.isqlviewer.util.LocalMessages;
055:
056: /**
057: * @author Mark A. Kobold <mkobold at isqlviewer dot com>
058: * @version 1.0
059: */
060: public class DataGrid extends AbstractApplicationView implements
061: ListSelectionListener, ActionListener, DocumentListener {
062:
063: private NumberFormat nf = NumberFormat.getIntegerInstance();
064: private EnhancedTable table = null;
065: private JTextField txtFilter = new JTextField();
066: private JLabel lblFilter = new JLabel();
067: private JScrollPane jspTable = null;
068: private Component glue = Box.createHorizontalGlue();
069: private JLabel lblLocatr = new JLabel();
070: private JLabel lblSearch = new JLabel(SwingUtilities
071: .loadIconResource("search", 16));
072: private JButton btnHider = new JButton();
073: private JButton actionPageUp = new JButton("");
074: private JButton actionPageDown = new JButton("");
075: private LocalMessages messages = new LocalMessages(
076: "org.isqlviewer.ui.ResourceBundle");
077:
078: public void configureMenubar(JMenuBar menuBar) {
079:
080: // TODO Auto-generated method stub
081:
082: }
083:
084: public void disposeView(Preferences preferences) {
085:
086: // TODO Auto-generated method stub
087:
088: }
089:
090: public void initializeView() {
091:
092: // TODO Auto-generated method stub
093:
094: }
095:
096: public void doLayout(JComponent parentComponent,
097: Preferences preferences, SwingEventManager eventManager) {
098:
099: table = new EnhancedTable();
100: // TODO table.setDropTarget(new DropTarget(table, workbench.bookmarkDropper));
101: table.setFont(table.getFont().deriveFont(10f));
102: // table.putClientProperty(EnhancedTable.CLIENT_JPOPUP_MENU, workbench.tablePopupMenu);
103: table.setDragEnabled(true);
104: table.getSelectionModel().addListSelectionListener(this );
105: jspTable = new JScrollPane(table);
106: lblLocatr.setBorder(BorderFactory.createEtchedBorder());
107: btnHider.setIcon(new SortableHeaderRenderer.BasicArrowIcon(
108: SwingConstants.NORTH, 4));
109: btnHider.setBorderPainted(false);
110: btnHider.setFocusPainted(false);
111: btnHider.addActionListener(this );
112: // btnHider.setToolTipText(BasicUtilities.getString("Filter_Hide_Tip"));
113: actionPageUp.addActionListener(this );
114: actionPageUp.setBorderPainted(false);
115: actionPageUp.setFocusPainted(false);
116: // actionPageUp.setToolTipText(BasicUtilities.getString("View_Prev"));
117: actionPageDown.addActionListener(this );
118: actionPageDown.setBorderPainted(false);
119: actionPageDown.setFocusPainted(false);
120: // actionPageDown.setToolTipText(BasicUtilities.getString("View_Next"));
121: // txtFilter.setToolTipText(BasicUtilities.getString("Filter_Input_Tip"));
122: txtFilter.getDocument().addDocumentListener(this );
123: lblFilter.setFont(lblFilter.getFont().deriveFont(9.0f));
124:
125: Object UI_CONSTRAINT = null;
126:
127: parentComponent.setLayout(new GridBagLayout());
128: UI_CONSTRAINT = constrain(0, 0, 1, 1, 1.0, 0.0,
129: GridBagConstraints.CENTER,
130: GridBagConstraints.HORIZONTAL);
131: parentComponent.add(glue, UI_CONSTRAINT);
132: UI_CONSTRAINT = constrain(1, 0, 1, 1, 0.0, 0.0,
133: GridBagConstraints.EAST, GridBagConstraints.HORIZONTAL);
134: parentComponent.add(lblSearch, UI_CONSTRAINT);
135: UI_CONSTRAINT = constrain(2, 0, 1, 1, 0.35, 0.0,
136: GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL);
137: parentComponent.add(txtFilter, UI_CONSTRAINT);
138: UI_CONSTRAINT = constrain(3, 0, 1, 1, 0.0, 0.0,
139: GridBagConstraints.EAST, GridBagConstraints.NONE);
140: parentComponent.add(btnHider, UI_CONSTRAINT);
141: UI_CONSTRAINT = constrain(2, 1, 2, 1, 0.0, 0.0,
142: GridBagConstraints.WEST, GridBagConstraints.NONE);
143: parentComponent.add(lblFilter, UI_CONSTRAINT);
144: UI_CONSTRAINT = constrain(0, 2, 4, 1, 1.0, 1.0,
145: GridBagConstraints.CENTER, GridBagConstraints.BOTH);
146: parentComponent.add(jspTable, UI_CONSTRAINT);
147: UI_CONSTRAINT = constrain(1, 3, 4, 1, 1.0, 0.0,
148: GridBagConstraints.EAST, GridBagConstraints.NONE);
149: parentComponent.add(lblLocatr, UI_CONSTRAINT);
150: }
151:
152: public void setModel(EnhancedTableModel model) {
153:
154: table.setModel(model);
155: if (model.getTrueRowCount() >= 1) {
156: table.setRowSelectionInterval(0, 0);
157: }
158:
159: if (model.getPageCount() > 1) {
160: jspTable
161: .setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
162: jspTable
163: .setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
164: jspTable.setCorner(ScrollPaneConstants.UPPER_RIGHT_CORNER,
165: actionPageUp);
166: jspTable.setCorner(ScrollPaneConstants.LOWER_RIGHT_CORNER,
167: actionPageDown);
168: } else {
169: jspTable
170: .setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
171: jspTable
172: .setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
173: jspTable.setCorner(ScrollPaneConstants.UPPER_RIGHT_CORNER,
174: Box.createHorizontalStrut(0));
175: jspTable.setCorner(ScrollPaneConstants.LOWER_RIGHT_CORNER,
176: Box.createHorizontalStrut(0));
177: }
178: }
179:
180: public EnhancedTable getTable() {
181:
182: return table;
183: }
184:
185: public void clearFiltering() {
186:
187: if (table != null) {
188: ResultSetTableModel model = (ResultSetTableModel) table
189: .getModel();
190: model.clearFilter();
191: }
192: txtFilter.setText(null);
193: lblFilter.setText(null);
194: }
195:
196: /*
197: * (non-Javadoc)
198: *
199: * @see javax.swing.event.DocumentListener#changedUpdate(javax.swing.event.DocumentEvent)
200: */
201: public void changedUpdate(DocumentEvent e) {
202:
203: }
204:
205: /*
206: * (non-Javadoc)
207: *
208: * @see javax.swing.event.DocumentListener#insertUpdate(javax.swing.event.DocumentEvent)
209: */
210: public void insertUpdate(DocumentEvent e) {
211:
212: doFiltering();
213: }
214:
215: /*
216: * (non-Javadoc)
217: *
218: * @see javax.swing.event.DocumentListener#removeUpdate(javax.swing.event.DocumentEvent)
219: */
220: public void removeUpdate(DocumentEvent e) {
221:
222: doFiltering();
223: }
224:
225: private synchronized void doFiltering() {
226:
227: if (table != null) {
228: EnhancedTableModel model = (EnhancedTableModel) table
229: .getModel();
230: String filter = txtFilter.getText().trim();
231: int found = model.applyFilter(filter);
232: if (found >= 1) {
233: String txt = messages.format("datagrid.found_matches",
234: Integer.toString(found));
235: lblFilter.setText(txt);
236: } else {
237: if (filter.length() >= 1) {
238: lblFilter.setText(messages
239: .getMessage("datagrid.no_matches"));
240: } else {
241: lblFilter.setText(null);
242: }
243: }
244: if (table.getRowCount() >= 1) {
245: table.setRowSelectionInterval(0, 0);
246: }
247: table.invalidate();
248: }
249:
250: }
251:
252: /*
253: * (non-Javadoc)
254: *
255: * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
256: */
257: public void actionPerformed(ActionEvent e) {
258:
259: Object src = e.getSource();
260: if (src == btnHider) {
261: txtFilter.setVisible(!txtFilter.isVisible());
262: lblFilter.setVisible(!lblFilter.isVisible());
263: lblSearch.setVisible(!lblSearch.isVisible());
264: glue.setVisible(!glue.isVisible());
265: if (txtFilter.isVisible()) {
266: btnHider
267: .setIcon(new SortableHeaderRenderer.BasicArrowIcon(
268: SwingConstants.NORTH, 4));
269: // btnHider.setToolTipText(BasicUtilities.getString("Filter_Hide_Tip"));
270: } else {
271: btnHider
272: .setIcon(new SortableHeaderRenderer.BasicArrowIcon(
273: SwingConstants.SOUTH, 4));
274: // btnHider.setToolTipText(BasicUtilities.getString("Filter_Show_Tip"));
275: }
276: btnHider.getParent().validate();
277: btnHider.getParent().repaint();
278: } else if (src == actionPageDown) {
279: EnhancedTableModel model = (EnhancedTableModel) table
280: .getModel();
281: model.pageDown();
282: } else if (src == actionPageUp) {
283: EnhancedTableModel model = (EnhancedTableModel) table
284: .getModel();
285: model.pageUp();
286: }
287: }
288:
289: public void valueChanged(ListSelectionEvent e) {
290:
291: EnhancedTableModel model = (EnhancedTableModel) table
292: .getModel();
293: String str = nf.format(model.translateRow(table
294: .getSelectedRow()) + 1);
295: str = str.concat("/");
296: str = str.concat(nf.format(model.getTrueRowCount()));
297: str = str.concat(" [");
298: str = str.concat(nf.format(model.getPageOffset() + 1));
299: str = str.concat("/");
300: str = str.concat(nf.format(model.getPageCount()));
301: lblLocatr.setText(" ".concat(str).concat("] "));
302: }
303: }
|