001: package vela.view;
002:
003: /*
004: * Copyright 2007 Madhav Pulipaka
005: *
006: * This file is part of Vela.
007:
008: * Vela is free software; you can redistribute it and/or modify
009: * it under the terms of the GNU General Public License as published by
010: * the Free Software Foundation; either version 2 of the License, or
011: * (at your option) any later version.
012: *
013: * Vela is distributed in the hope that it will be useful,
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
016: * GNU General Public License for more details.
017: *
018: * You should have received a copy of the GNU General Public License
019: * along with Vela; if not, write to the Free Software
020: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
021: */
022: import java.awt.Color;
023: import java.awt.Component;
024: import java.awt.Container;
025: import java.awt.Cursor;
026: import java.awt.Dimension;
027: import java.awt.FlowLayout;
028: import java.awt.BorderLayout;
029: import java.awt.Font;
030: import java.awt.Frame;
031: import java.awt.Toolkit;
032: import java.awt.event.ActionEvent;
033: import java.awt.event.ActionListener;
034: import java.awt.event.ItemEvent;
035: import java.awt.event.ItemListener;
036: import java.awt.event.KeyEvent;
037: import java.awt.event.KeyListener;
038:
039: import javax.swing.BorderFactory;
040: import javax.swing.JButton;
041: import javax.swing.JComboBox;
042: import javax.swing.JDialog;
043: import javax.swing.JEditorPane;
044: import javax.swing.JLabel;
045: import javax.swing.JPanel;
046: import javax.swing.JScrollPane;
047: import javax.swing.JTextArea;
048: import javax.swing.JTextPane;
049: import javax.swing.JToolTip;
050: import javax.swing.border.Border;
051: import javax.swing.event.DocumentEvent;
052: import javax.swing.event.DocumentListener;
053: import javax.swing.text.Position;
054: import vela.model.*;
055: import vela.common.Constants;
056:
057: public class DataFilter extends JDialog implements ActionListener,
058: KeyListener, ItemListener, Constants {
059:
060: private static final int WIDTH = 400;
061: private static final int HEIGHT = 200;
062: JComboBox colNames;
063: JEditorPane tpFilterString;
064: DBObject tableObj;
065: Frame parentFrame;
066: String title;
067:
068: public DataFilter(Frame frame, DBObject tableObj, String title) {
069: super (frame, title, true);
070: this .tableObj = tableObj;
071: this .title = title;
072: parentFrame = frame;
073: Container container = getContentPane();
074: JButton btnApply = new JButton("Apply");
075: JButton btnCancel = new JButton("Cancel");
076: JButton btnClear = new JButton("Clear");
077: btnApply.setActionCommand("Apply");
078: btnCancel.setActionCommand("Cancel");
079: btnClear.setActionCommand("Clear");
080: Dimension screenSize = Toolkit.getDefaultToolkit()
081: .getScreenSize();
082: setBounds((int) (screenSize.getWidth() / 2) - (WIDTH / 2),
083: (int) (screenSize.getHeight() / 2) - (HEIGHT / 2),
084: WIDTH, HEIGHT);
085: btnApply.addActionListener(this );
086: btnClear.addActionListener(this );
087: btnCancel.addActionListener(this );
088: JPanel southPanel = new JPanel(
089: new FlowLayout(FlowLayout.CENTER));
090: southPanel.add(btnApply);
091: southPanel.add(btnClear);
092: southPanel.add(btnCancel);
093:
094: JLabel help = new JLabel(" ? ");
095: help.setBorder(BorderFactory.createEtchedBorder());
096: help.setFont(new Font("Lucida Console", Font.BOLD, fontBig));
097: JLabel label = null;
098: if (title.startsWith("Filter")) {
099: label = new JLabel("Please enter the filter criteria.");
100: help
101: .setToolTipText("<HTML><FONT FACE=\"Verdana, sans-serif\" size=\"3\">Enter SQL \"WHERE\" clause as Filter Criteria.<BR>Please do not include \"WHERE\" in the filter criteria.<BR>Filter criteria is not case sensitive.</Font></HTML>");
102: } else {
103: label = new JLabel("Please enter the sorting criteria.");
104: label.setToolTipText("");
105: help
106: .setToolTipText("<HTML><FONT FACE=\"Verdana, sans-serif\" size=\"3\">Enter list of columns separated by \",\" as sort criteria.<BR>To sort in descending order of a column include \"DESC\" after the column name.<BR>For example sort criteria \"COLUMN1, COLUMN2 DESC\" will sort the table data<BR>in ascending order for COLUMN1 and in descending order for COLUMN2.<BR>Sort criteria is not case sensitive.</Font></HTML>");
107: }
108: label.setFont(new Font(fontFamily, Font.BOLD, fontBig));
109: container.add(southPanel, BorderLayout.SOUTH);
110: JPanel northPanel = new JPanel(new BorderLayout());
111: northPanel.add(label);
112: northPanel.add(help, BorderLayout.EAST);
113: container.add(northPanel, BorderLayout.NORTH);
114:
115: btnApply.setFont(new Font("Verdana", Font.BOLD, 12));
116: //btnApply.setBackground(new Color(0,0,100));
117: //btnApply.setForeground(new Color(0,0,128));
118:
119: btnCancel.setFont(new Font("Verdana", Font.BOLD, 12));
120: //btnCancel.setBackground(new Color(0,0,100));
121: //btnCancel.setForeground(new Color(0,0,128));
122:
123: btnClear.setFont(new Font("Verdana", Font.BOLD, 12));
124: //btnClear.setBackground(new Color(0,0,100));
125: //btnClear.setForeground(new Color(0,0,128));
126:
127: //JPanel centerPanel = new JPanel();
128: //centerPanel.setBorder(BorderFactory.createEtchedBorder(1));
129: //add(centerPanel);
130: tpFilterString = new JEditorPane();
131: tpFilterString.setFont(new Font(fontFamily, Font.BOLD,
132: fontSmall));
133: tpFilterString.setLayout(null);
134: tpFilterString.addKeyListener(this );
135: container.add(new JScrollPane(tpFilterString));
136: colNames = new JComboBox();
137: tpFilterString.add(colNames);
138: colNames.setVisible(false);
139: colNames.addItemListener(this );
140: colNames.setSize(new Dimension(100, 0));
141: colNames.setBorder(null);
142: colNames.setOpaque(false);
143: tpFilterString.setText("");
144: if (tableObj.getTableFilter() != null
145: && tableObj.getTableFilter().trim().length() > 0
146: && title.startsWith("Filter"))
147: tpFilterString.setText(tableObj.getTableFilter());
148: else if (tableObj.getTableSort() != null
149: && tableObj.getTableSort().trim().length() > 0
150: && title.startsWith("Sort"))
151: tpFilterString.setText(tableObj.getTableSort());
152: }
153:
154: public void actionPerformed(ActionEvent ae) {
155: setCursor(new Cursor(Cursor.WAIT_CURSOR));
156: try {
157: if (ae.getActionCommand().equalsIgnoreCase("Apply")) {
158: if (title.startsWith("Filter"))
159: tableObj.setTableFilter(tpFilterString.getText()
160: .trim());
161: else
162: tableObj.setTableSort(tpFilterString.getText()
163: .trim());
164: setVisible(false);
165: dispose();
166: } else if (ae.getActionCommand().equalsIgnoreCase("Cancel")) {
167: setVisible(false);
168: dispose();
169: } else if (ae.getActionCommand().equalsIgnoreCase("Clear")) {
170: tpFilterString.setText("");
171: }
172: } finally {
173: setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
174: }
175: }
176:
177: public void keyPressed(KeyEvent e) {
178:
179: }
180:
181: public void keyReleased(KeyEvent e) {
182: if (e.getKeyCode() == KeyEvent.VK_SPACE && e.isControlDown()) {
183: /*System.out.println("Key Released.");
184: colNames.setVisible(true);
185: colNames.setLocation(tpFilterString.getCaret().getMagicCaretPosition());*/
186: }
187: }
188:
189: public void keyTyped(KeyEvent e) {
190:
191: }
192:
193: public void itemStateChanged(ItemEvent e) {
194: setCursor(new Cursor(Cursor.WAIT_CURSOR));
195: try {
196: if (e.getStateChange() == ItemEvent.SELECTED) {
197: int caretPos = tpFilterString.getCaretPosition();
198: String currentText = tpFilterString.getText();
199: String textBefore = currentText.substring(0, caretPos);
200: String textAfter = currentText.substring(caretPos);
201: tpFilterString.setText(textBefore
202: + colNames.getSelectedItem() + textAfter);
203: colNames.setVisible(false);
204: }
205: } finally {
206: setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
207: }
208: }
209: }
|