001: package net.sourceforge.squirrel_sql.client.session.sqlfilter;
002:
003: /*
004: * Copyright (C) 2003 Maury Hammel
005: * mjhammel@users.sourceforge.net
006: *
007: * Adapted from SessionSQLPropertiesPanel.java by Colin Bell.
008: *
009: * This library is free software; you can redistribute it and/or
010: * modify it under the terms of the GNU Lesser General Public
011: * License as published by the Free Software Foundation; either
012: * version 2.1 of the License, or (at your option) any later version.
013: *
014: * This library is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
017: * Lesser General Public License for more details.
018: *
019: * You should have received a copy of the GNU Lesser General Public
020: * License along with this library; if not, write to the Free Software
021: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
022: */
023: import java.awt.Component;
024: import java.awt.GridBagConstraints;
025: import java.awt.GridBagLayout;
026: import java.awt.Insets;
027: import java.awt.event.ActionEvent;
028: import java.awt.event.ActionListener;
029: import java.util.SortedSet;
030:
031: import javax.swing.BorderFactory;
032: import javax.swing.BoxLayout;
033: import javax.swing.JButton;
034: import javax.swing.JComboBox;
035: import javax.swing.JLabel;
036: import javax.swing.JPanel;
037: import javax.swing.JScrollPane;
038: import javax.swing.JTextArea;
039:
040: import net.sourceforge.squirrel_sql.fw.util.StringManager;
041: import net.sourceforge.squirrel_sql.fw.util.StringManagerFactory;
042:
043: /**
044: * This panel allows the user to change the where clause for a Contents tab query.
045: *
046: * @author <A HREF="mailto:mjhammel@users.sourceforge.net">Maury Hammel</A>
047: */
048: public class OrderByClausePanel implements ISQLFilterPanel {
049: /** Internationalized strings for this class. */
050: private static final StringManager s_stringMgr = StringManagerFactory
051: .getStringManager(OrderByClausePanel.class);
052:
053: /** A class containing the information about the SQL filters. */
054: private SQLFilterClauses _sqlFilterClauses;
055:
056: /** The actual GUI panel that allows user to do the maintenance. */
057: private OrderByClauseSubPanel _myPanel;
058:
059: /**
060: * Create a new instance of an OrderByClausePanel
061: *
062: * @param columnList A list of column names for the database table.
063: * @param tableName The name of the database table that the filter
064: * information will apply to.
065: */
066: public OrderByClausePanel(SortedSet<String> columnList,
067: String tableName) {
068: super ();
069: _myPanel = new OrderByClauseSubPanel(columnList, tableName);
070: }
071:
072: /**
073: * Initialize the components of the OrderByClausePanel.
074: *
075: * @param sqlFilterClauses An instance of a class containing information
076: * about SQL filters already in place for the table.
077: *
078: * @throws IllegalArgumentException Thrown if an invalid argument is passed.
079: *
080: */
081: public void initialize(SQLFilterClauses sqlFilterClauses)
082: throws IllegalArgumentException {
083: if (sqlFilterClauses == null) {
084: throw new IllegalArgumentException(
085: "Null sqlFilterClauses passed");
086: }
087: _sqlFilterClauses = sqlFilterClauses;
088: _myPanel.loadData(_sqlFilterClauses);
089: }
090:
091: /**
092: * Returns the panel created by the class.
093: *
094: * @return Return an instance of a WhereClauseSubPanel.
095: *
096: */
097: public Component getPanelComponent() {
098: return _myPanel;
099: }
100:
101: /**
102: * Get the title of the panel.
103: *
104: * @return Return a string containing the title of the panl.
105: */
106: public String getTitle() {
107: return OrderByClauseSubPanel.OrderByClausePanelI18n.ORDER_BY_CLAUSE;
108: }
109:
110: /**
111: * Get the hint text associated with the panel.
112: *
113: * @return A String value containing the hint text associated with the
114: * panel.
115: *
116: */
117: public String getHint() {
118: return OrderByClauseSubPanel.OrderByClausePanelI18n.HINT;
119: }
120:
121: /**
122: * Update the current session with any changes to the SQL filter
123: * information.
124: */
125: public void applyChanges() {
126: _myPanel.applyChanges(_sqlFilterClauses);
127: }
128:
129: /** A private class that makes up the bulk of the GUI for the panel. */
130: private static final class OrderByClauseSubPanel extends JPanel {
131: private static final long serialVersionUID = 1L;
132:
133: /**
134: * This interface defines locale specific strings. This should be replaced with a property file.
135: */
136: interface OrderByClausePanelI18n {
137:
138: // These must not be internationalized since SQL tokens must be in
139: // English
140: String ASC = "ASC"; //No I18N
141: String DESC = "DESC"; //No I18N
142:
143: //[i18n[OrderByClausePanel.addLabel=Add]
144: String ADD = s_stringMgr
145: .getString("OrderByClausePanel.addLabel");
146: //[i18n[OrderByClausePanel.columnsLabel=Columns]
147: String COLUMNS = s_stringMgr
148: .getString("OrderByClausePanel.columnsLabel");
149: //[i18n[OrderByClausePanel.orderDirectionLabel=Order Direction]
150: String ORDER_DIRECTION = s_stringMgr
151: .getString("OrderByClausePanel.orderDirectionLabel");
152: //[i18n[OrderByClausePanel.orderByClauseLabel=Order By Clause]
153: String ORDER_BY_CLAUSE = s_stringMgr
154: .getString("OrderByClausePanel.orderByClauseLabel");
155: //[i18n[OrderByClausePanel.hint=Order by clause for the selected table]
156: String HINT = s_stringMgr
157: .getString("OrderByClausePanel.hint");
158: }
159:
160: /** A label to identify the column combo box. */
161: private JLabel _columnLabel = new JLabel(
162: OrderByClausePanelI18n.COLUMNS);
163: /** A JComboBox component containing a list of the names of the columns for the
164: * current table.
165: */
166: private JComboBox _columnCombo;
167: /** A label to identify the sort orders combo box. */
168: private JLabel _orderLabel = new JLabel(
169: OrderByClausePanelI18n.ORDER_DIRECTION);
170: /** A JComboBox containing a list of the valid sort orders.
171: */
172: private OrderCombo _orderCombo = new OrderCombo();
173: /** A button used to add information from the combo boxes and text fields into the
174: * Where clause text area.
175: */
176: private JButton _addButton = new JButton(
177: OrderByClausePanelI18n.ADD);
178: /** A text area used to contain all of the information for the Order By clause. */
179: private JTextArea _orderClauseArea = new JTextArea(10, 40);
180: /** The name of the database table the Where clause applies to. */
181: private String _tableName;
182:
183: /** A JPanel used for a bulk of the GUI elements of the panel.
184: * @param columnList A list of the column names for the table.
185: * @param tableName The name of the database table.
186: *
187: */
188: OrderByClauseSubPanel(SortedSet<String> columnList,
189: String tableName) {
190: super (new GridBagLayout());
191: _columnCombo = new JComboBox(columnList.toArray());
192: _tableName = tableName;
193: createUserInterface();
194: }
195:
196: /** Load existing clause information into the panel.
197: * @param sqlFilterClauses An instance of a class containing SQL Filter information for the current table.
198: *
199: */
200: void loadData(SQLFilterClauses sqlFilterClauses) {
201: _orderClauseArea.setText(sqlFilterClauses.get(
202: getClauseIdentifier(), _tableName));
203: }
204:
205: /** Update the current SQuirreL session with any changes to the SQL filter
206: * information.
207: * @param sqlFilterClauses An instance of a class containing SQL Filter information for the current table.
208: *
209: */
210: void applyChanges(SQLFilterClauses sqlFilterClauses) {
211: sqlFilterClauses.put(getClauseIdentifier(), _tableName,
212: _orderClauseArea.getText());
213: }
214:
215: /** Create the GUI elements for the panel */
216: private void createUserInterface() {
217: final GridBagConstraints gbc = new GridBagConstraints();
218: gbc.anchor = GridBagConstraints.WEST;
219: gbc.fill = GridBagConstraints.HORIZONTAL;
220:
221: gbc.gridx = 0;
222: gbc.gridy = 0;
223: add(createGeneralPanel(), gbc);
224: }
225:
226: /** Create a JPanel with GUI components.
227: * @return Returns a JPanel
228: *
229: */
230: private JPanel createGeneralPanel() {
231: JPanel pnl = new JPanel(new GridBagLayout());
232:
233: final GridBagConstraints gbc = new GridBagConstraints();
234: gbc.fill = GridBagConstraints.HORIZONTAL;
235: gbc.anchor = GridBagConstraints.WEST;
236: gbc.insets = new Insets(4, 4, 4, 4);
237: gbc.weightx = 10.0;
238:
239: gbc.gridx = 0;
240: gbc.gridy = 0;
241: JPanel columnPanel = new JPanel();
242: columnPanel.setLayout(new BoxLayout(columnPanel,
243: BoxLayout.Y_AXIS));
244: _columnLabel.setAlignmentX(Component.LEFT_ALIGNMENT);
245: columnPanel.add(_columnLabel);
246: _columnCombo.setAlignmentX(Component.LEFT_ALIGNMENT);
247: columnPanel.add(_columnCombo);
248: pnl.add(columnPanel, gbc);
249:
250: gbc.fill = GridBagConstraints.NONE;
251: gbc.gridx += 2;
252: gbc.weightx = 1.0;
253: JPanel orderPanel = new JPanel();
254: orderPanel.setLayout(new BoxLayout(orderPanel,
255: BoxLayout.Y_AXIS));
256: _orderLabel.setAlignmentX(Component.LEFT_ALIGNMENT);
257: orderPanel.add(_orderLabel);
258: _orderCombo.setAlignmentX(Component.LEFT_ALIGNMENT);
259: orderPanel.add(_orderCombo);
260: pnl.add(orderPanel, gbc);
261:
262: gbc.gridx += 2;
263: gbc.gridwidth = GridBagConstraints.REMAINDER;
264: _addButton.addActionListener(new ActionListener() {
265: public void actionPerformed(ActionEvent evt) {
266: addTextToClause();
267: }
268: });
269: pnl.add(_addButton, gbc);
270:
271: gbc.gridx = 0;
272: gbc.gridy++;
273: gbc.fill = GridBagConstraints.HORIZONTAL;
274: gbc.ipady = 4;
275: _orderClauseArea.setBorder(BorderFactory
276: .createEtchedBorder());
277: _orderClauseArea.setLineWrap(true);
278: JScrollPane sp = new JScrollPane(_orderClauseArea,
279: JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
280: JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
281: pnl.add(sp, gbc);
282:
283: return pnl;
284: }
285:
286: /** Combine the information entered in the combo boxes and add it
287: * to the Order By clause information.
288: */
289: private void addTextToClause() {
290: String column = (String) _columnCombo.getSelectedItem();
291: String order = (String) _orderCombo.getSelectedItem();
292: if (_orderClauseArea.getText().length() > 0) {
293: _orderClauseArea.append(",\n");
294: }
295: _orderClauseArea.append(column + " " + order);
296: }
297:
298: /** Erase all information for the current filter. */
299: public void clearFilter() {
300: _orderClauseArea.setText("");
301: }
302: }
303:
304: private static final class OrderCombo extends JComboBox {
305: private static final long serialVersionUID = 1L;
306:
307: OrderCombo() {
308: super ();
309: addItem(OrderByClauseSubPanel.OrderByClausePanelI18n.ASC);
310: addItem(OrderByClauseSubPanel.OrderByClausePanelI18n.DESC);
311: }
312: }
313:
314: /** Erase all information for the current filter. */
315: public void clearFilter() {
316: _myPanel.clearFilter();
317: }
318:
319: /** Get a value that uniquely identifies this SQL filter clause.
320: * @return Return a String value containing an identifing value.
321: *
322: */
323: public static String getClauseIdentifier() {
324: return OrderByClauseSubPanel.OrderByClausePanelI18n.ORDER_BY_CLAUSE;
325: }
326: }
|