001: /*
002: * Copyright (C) 2003-2006 Gerd Wagner
003: * colbell@users.sourceforge.net
004: *
005: * This library is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU Lesser General Public
007: * License as published by the Free Software Foundation; either
008: * version 2.1 of the License, or (at your option) any later version.
009: *
010: * This library 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 GNU
013: * Lesser General Public License for more details.
014: *
015: * You should have received a copy of the GNU Lesser General Public
016: * License along with this library; if not, write to the Free Software
017: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
018: */
019: package net.sourceforge.squirrel_sql.client.gui;
020:
021: import net.sourceforge.squirrel_sql.client.gui.session.BaseSessionInternalFrame;
022: import net.sourceforge.squirrel_sql.fw.id.IIdentifier;
023:
024: import java.util.HashMap;
025: import java.util.Vector;
026: import java.util.List;
027: import java.util.ArrayList;
028:
029: /**
030: * Allows to acces open Session windows by Session and by opening sequence.
031: */
032: public class SessionWindowsHolder {
033: HashMap<IIdentifier, List<BaseSessionInternalFrame>> _framesBySessionIdentifier = new HashMap<IIdentifier, List<BaseSessionInternalFrame>>();
034: HashMap<BaseSessionInternalFrame, IIdentifier> _sessionIdentifierByFrame = new HashMap<BaseSessionInternalFrame, IIdentifier>();
035: Vector<BaseSessionInternalFrame> _framesInOpeningSequence = new Vector<BaseSessionInternalFrame>();
036:
037: public int addFrame(IIdentifier sessionIdentifier,
038: BaseSessionInternalFrame internalFrame) {
039: List<BaseSessionInternalFrame> windowList = _framesBySessionIdentifier
040: .get(sessionIdentifier);
041: if (windowList == null) {
042: windowList = new ArrayList<BaseSessionInternalFrame>();
043: _framesBySessionIdentifier.put(sessionIdentifier,
044: windowList);
045: }
046: windowList.add(internalFrame);
047:
048: _framesInOpeningSequence.add(internalFrame);
049:
050: _sessionIdentifierByFrame.put(internalFrame, sessionIdentifier);
051:
052: return windowList.size();
053: }
054:
055: public BaseSessionInternalFrame[] getFramesOfSession(
056: IIdentifier sessionIdentifier) {
057: List<BaseSessionInternalFrame> list = _framesBySessionIdentifier
058: .get(sessionIdentifier);
059:
060: if (null == list) {
061: return new BaseSessionInternalFrame[0];
062: } else {
063: return list.toArray(new BaseSessionInternalFrame[list
064: .size()]);
065: }
066: }
067:
068: public void removeWindow(BaseSessionInternalFrame internalFrame) {
069: IIdentifier sessionIdentifier = _sessionIdentifierByFrame
070: .get(internalFrame);
071:
072: if (null == sessionIdentifier) {
073: throw new IllegalArgumentException("Unknown Frame "
074: + internalFrame.getTitle());
075: }
076:
077: List<BaseSessionInternalFrame> framesOfSession = _framesBySessionIdentifier
078: .get(sessionIdentifier);
079: framesOfSession.remove(internalFrame);
080:
081: _framesInOpeningSequence.remove(internalFrame);
082: _sessionIdentifierByFrame.remove(internalFrame);
083: }
084:
085: public void removeAllWindows(IIdentifier sessionId) {
086: BaseSessionInternalFrame[] framesOfSession = getFramesOfSession(sessionId);
087:
088: for (int i = 0; i < framesOfSession.length; i++) {
089: _framesInOpeningSequence.remove(framesOfSession[i]);
090: _sessionIdentifierByFrame.remove(framesOfSession[i]);
091: }
092:
093: _framesBySessionIdentifier.remove(sessionId);
094: }
095:
096: public BaseSessionInternalFrame getNextSessionWindow(
097: BaseSessionInternalFrame sessionWindow) {
098: int nextIx = _framesInOpeningSequence.indexOf(sessionWindow) + 1;
099:
100: if (nextIx < _framesInOpeningSequence.size()) {
101: return _framesInOpeningSequence.get(nextIx);
102: } else {
103: if (1 < _framesInOpeningSequence.size()) {
104: return _framesInOpeningSequence.get(0);
105: } else {
106: return sessionWindow;
107: }
108: }
109: }
110:
111: public BaseSessionInternalFrame getPreviousSessionWindow(
112: BaseSessionInternalFrame sessionWindow) {
113: int prevIx = _framesInOpeningSequence.indexOf(sessionWindow) - 1;
114:
115: if (0 <= prevIx) {
116: return _framesInOpeningSequence.get(prevIx);
117: } else {
118:
119: if (1 < _framesInOpeningSequence.size()) {
120: return _framesInOpeningSequence
121: .get(_framesInOpeningSequence.size() - 1);
122: } else {
123: return sessionWindow;
124: }
125: }
126: }
127: }
|