001: package com.salmonllc.examples.example15;
002:
003: //The Salmon Open Framework for Internet Applications (SOFIA)
004: //Copyright (C) 1999 - 2002, Salmon LLC
005: //
006: //This program is free software; you can redistribute it and/or
007: //modify it under the terms of the GNU General Public License version 2
008: //as published by the Free Software Foundation;
009: //
010: //This program is distributed in the hope that it will be useful,
011: //but WITHOUT ANY WARRANTY; without even the implied warranty of
012: //MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
013: //GNU General Public License for more details.
014: //
015: //You should have received a copy of the GNU General Public License
016: //along with this program; if not, write to the Free Software
017: //Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
018: //
019: //For more information please visit http://www.salmonllc.com
020:
021: import com.salmonllc.sql.*;
022: import com.salmonllc.swing.PopupManager;
023:
024: import javax.swing.*;
025: import java.awt.event.ActionListener;
026: import java.awt.event.ActionEvent;
027: import java.awt.event.KeyAdapter;
028: import java.awt.event.KeyEvent;
029: import java.awt.*;
030: import java.net.URL;
031: import java.net.MalformedURLException;
032:
033: /*
034: * This is a panel that contains the search text edit and buttons for search, add, delete, save.
035: */
036:
037: public class ButtonPanel extends JPanel implements ActionListener {
038: DataStoreProxy _ds;
039: JTextField _search;
040: JButton _searchButton, _addButton, _deleteButton, _saveButton,
041: _helpButton;
042: MainPanel _pan;
043:
044: /**
045: * Creates a new Search Panel
046: */
047: public ButtonPanel(DataStoreProxy ds, MainPanel pan, String iconURL) {
048: _ds = ds;
049: _pan = pan;
050: //Build the search bar
051:
052: try {
053: setLayout(new BoxLayout(this , BoxLayout.X_AXIS));
054: add(_search = new JTextField(25));
055: add(Box.createRigidArea(new Dimension(5, 0)));
056: add(_searchButton = new JButton("Search", new ImageIcon(
057: new URL(iconURL + "search.gif"))));
058:
059: add(Box.createRigidArea(new Dimension(20, 0)));
060: add(_addButton = new JButton("Add", new ImageIcon(new URL(
061: iconURL + "add.gif"))));
062: add(_deleteButton = new JButton("Delete", new ImageIcon(
063: new URL(iconURL + "delete.gif"))));
064: add(_saveButton = new JButton("Save", new ImageIcon(
065: new URL(iconURL + "save.gif"))));
066: add(Box.createRigidArea(new Dimension(20, 0)));
067: add(_helpButton = new JButton("Help", new ImageIcon(
068: new URL(iconURL + "help.gif"))));
069: Dimension fieldSize = new Dimension(150, 25);
070: _search.setMaximumSize(fieldSize);
071: _search.setMinimumSize(fieldSize);
072:
073: _searchButton.setName("btn.search");
074: _addButton.setName("btn.add");
075: _deleteButton.setName("btn.delete");
076: _saveButton.setName("btn.save");
077: _helpButton.setName("btn.help");
078:
079: setSaveEnabled(false);
080: setDeleteEnabled(true);
081: } catch (MalformedURLException ex) {
082: }
083:
084: //Add listeners
085: _search.addKeyListener(new KeyAdapter() {
086: public void keyPressed(KeyEvent e) {
087: if (e.getKeyCode() == KeyEvent.VK_ENTER)
088: _pan.loadData(_search.getText());
089: }
090: });
091: _searchButton.addActionListener(this );
092: _addButton.addActionListener(this );
093: _deleteButton.addActionListener(this );
094: _saveButton.addActionListener(this );
095: _helpButton.addActionListener(this );
096: _deleteButton.setEnabled(false);
097:
098: }
099:
100: /**
101: * Invoked when an the user clicks the search button.
102: */
103: public void actionPerformed(ActionEvent e) {
104: PopupManager.getPopupManager().hideWindow();
105: if (e.getSource() == _searchButton)
106: _pan.loadData(_search.getText());
107: else if (e.getSource() == _deleteButton)
108: _ds.deleteRow();
109: else if (e.getSource() == _addButton)
110: _ds.insertRow();
111: else if (e.getSource() == _saveButton)
112: _pan.saveData();
113: else if (e.getSource() == _helpButton)
114: _pan.showHelp();
115: }
116:
117: public void grabFocus() {
118: _search.grabFocus();
119: }
120:
121: public void setSaveEnabled(boolean enabled) {
122: _saveButton.setEnabled(enabled);
123: }
124:
125: public void setDeleteEnabled(boolean enabled) {
126: _deleteButton.setEnabled(enabled);
127: }
128:
129: public boolean buttonOK(String nameIn) {
130: Component[] comp = getComponents();
131: for (int i = 0; i < comp.length; i++) {
132: String name = comp[i].getName();
133: if (name != null && name.equals(nameIn))
134: return comp[i].isVisible() && comp[i].isEnabled();
135: }
136: return false;
137: }
138: }
|