001: package net.sourceforge.squirrel_sql.client.gui.db;
002:
003: /*
004: * Copyright (C) 2001-2004 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.BorderLayout;
022: import java.awt.Container;
023: import java.awt.Dimension;
024: import java.awt.event.MouseAdapter;
025: import java.awt.event.MouseEvent;
026:
027: import javax.swing.JList;
028: import javax.swing.JScrollPane;
029: import javax.swing.event.InternalFrameAdapter;
030: import javax.swing.event.InternalFrameEvent;
031: import javax.swing.event.ListDataEvent;
032: import javax.swing.event.ListDataListener;
033:
034: import net.sourceforge.squirrel_sql.fw.gui.BasePopupMenu;
035: import net.sourceforge.squirrel_sql.fw.gui.GUIUtils;
036: import net.sourceforge.squirrel_sql.fw.gui.ToolBar;
037: import net.sourceforge.squirrel_sql.fw.util.BaseException;
038: import net.sourceforge.squirrel_sql.fw.util.ICommand;
039: import net.sourceforge.squirrel_sql.fw.util.StringManager;
040: import net.sourceforge.squirrel_sql.fw.util.StringManagerFactory;
041: import net.sourceforge.squirrel_sql.fw.util.log.ILogger;
042: import net.sourceforge.squirrel_sql.fw.util.log.LoggerController;
043:
044: import net.sourceforge.squirrel_sql.client.gui.BaseInternalFrame;
045:
046: abstract class BaseListInternalFrame extends BaseInternalFrame {
047: protected interface IUserInterfaceFactory {
048: ToolBar getToolBar();
049:
050: BasePopupMenu getPopupMenu();
051:
052: JList getList();
053:
054: String getWindowTitle();
055:
056: ICommand getDoubleClickCommand();
057:
058: void enableDisableActions();
059: }
060:
061: /** Logger for this class. */
062: private static ILogger s_log = LoggerController
063: .createLogger(BaseListInternalFrame.class);
064:
065: private IUserInterfaceFactory _uiFactory;
066:
067: /** Popup menu for the list. */
068: private BasePopupMenu _popupMenu;
069:
070: /** Toolbar for window. */
071: private ToolBar _toolBar;
072:
073: private boolean _hasBeenBuilt;
074:
075: private boolean _hasBeenSized = false;
076:
077: /** Internationalized strings for this class. */
078: private static final StringManager s_stringMgr = StringManagerFactory
079: .getStringManager(BaseListInternalFrame.class);
080:
081: public BaseListInternalFrame(IUserInterfaceFactory uiFactory) {
082: super ("", true, true);
083: if (uiFactory == null) {
084: throw new IllegalArgumentException(
085: "Null IUserInterfaceFactory passed");
086: }
087: _uiFactory = uiFactory;
088:
089: createUserInterface();
090: }
091:
092: public void updateUI() {
093: super .updateUI();
094: if (_hasBeenBuilt) {
095: _hasBeenSized = false;
096: privateResize();
097: }
098: }
099:
100: protected IUserInterfaceFactory getUserInterfaceFactory() {
101: return _uiFactory;
102: }
103:
104: protected void setToolBar(ToolBar tb) {
105: final Container content = getContentPane();
106: if (_toolBar != null) {
107: content.remove(_toolBar);
108: }
109: if (tb != null) {
110: content.add(tb, BorderLayout.NORTH);
111: }
112: _toolBar = tb;
113: }
114:
115: /**
116: * Process a mouse press event in this list. If this event is a trigger
117: * for a popup menu then display the popup menu.
118: *
119: * @param evt The mouse event being processed.
120: */
121: private void mousePress(MouseEvent evt) {
122: if (evt.isPopupTrigger()) {
123: if (_popupMenu == null) {
124: _popupMenu = _uiFactory.getPopupMenu();
125: }
126: _popupMenu.show(evt);
127: }
128: }
129:
130: private void privateResize() {
131: if (!_hasBeenSized) {
132: if (_toolBar != null) {
133: _hasBeenSized = true;
134: Dimension windowSize = getSize();
135: int rqdWidth = _toolBar.getPreferredSize().width + 15;
136: if (rqdWidth > windowSize.width) {
137: windowSize.width = rqdWidth;
138: setSize(windowSize);
139: }
140: }
141: }
142: }
143:
144: private void createUserInterface() {
145: // This is a tool window.
146: GUIUtils.makeToolWindow(this , true);
147:
148: setDefaultCloseOperation(HIDE_ON_CLOSE);
149:
150: // Pane to add window content to.
151: final Container content = getContentPane();
152: content.setLayout(new BorderLayout());
153:
154: String winTitle = _uiFactory.getWindowTitle();
155: if (winTitle != null) {
156: setTitle(winTitle);
157: }
158:
159: // Put toolbar at top of window.
160: setToolBar(_uiFactory.getToolBar());
161:
162: // The main list for window.
163: final JList list = _uiFactory.getList();
164:
165: // Allow list to scroll.
166: final JScrollPane sp = new JScrollPane();
167: sp.setViewportView(list);
168: sp.setPreferredSize(new Dimension(100, 100));
169:
170: // List in the centre of the window.
171: content.add(sp, BorderLayout.CENTER);
172:
173: // Add mouse listener for displaying popup menu.
174: list.addMouseListener(new MouseAdapter() {
175: public void mousePressed(MouseEvent evt) {
176: mousePress(evt);
177: }
178:
179: public void mouseReleased(MouseEvent evt) {
180: mousePress(evt);
181: }
182: });
183:
184: // Add a listener to handle doubleclick events in the list.
185: list.addMouseListener(new MouseAdapter() {
186: public void mouseClicked(MouseEvent evt) {
187: if (evt.getClickCount() == 2) {
188: ICommand cmd = _uiFactory.getDoubleClickCommand();
189: if (cmd != null) {
190: try {
191: cmd.execute();
192: } catch (BaseException ex) {
193: // i18n[BaseListInternalFrame.error.execdoubleclick=Error occured executing doubleclick event]
194: s_log
195: .error(
196: s_stringMgr
197: .getString("BaseListInternalFrame.error.execdoubleclick"),
198: ex);
199: }
200: }
201: }
202: }
203: });
204:
205: // Add listener to listen for items added/removed from list.
206: list.getModel().addListDataListener(new ListDataListener() {
207: public void intervalAdded(ListDataEvent evt) {
208: list.setSelectedIndex(evt.getIndex0()); // select the one just added.
209: _uiFactory.enableDisableActions();
210: }
211:
212: public void intervalRemoved(ListDataEvent evt) {
213: int nextIdx = evt.getIndex0();
214: int lastIdx = list.getModel().getSize() - 1;
215: if (nextIdx > lastIdx) {
216: nextIdx = lastIdx;
217: }
218: list.setSelectedIndex(nextIdx);
219: _uiFactory.enableDisableActions();
220: }
221:
222: public void contentsChanged(ListDataEvent evt) {
223: // Unused.
224: }
225: });
226:
227: // When window opened ensure it is wide enough to display the toolbar.
228: // There is a bug in JDK1.2 where internalFrameOpened() doesn't get
229: // called so we've used a workaround. The workaround doesn't work in
230: // JDK1.3.
231: addInternalFrameListener(new InternalFrameAdapter() {
232: //private boolean _hasBeenActivated = false;
233: //public void internalFrameActivated(InternalFrameEvent evt)
234: //{
235: //if (!_hasBeenActivated)
236: //{
237: //// _hasBeenActivated = true;
238: // privateResize();
239: //}
240: //list.requestFocus();
241: //}
242:
243: public void internalFrameOpened(InternalFrameEvent evt) {
244: privateResize();
245: }
246:
247: });
248:
249: validate();
250:
251: }
252: }
|