001: package net.sourceforge.squirrel_sql.client.session.mainpanel;
002:
003: /*
004: * Copyright (C) 2003 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 javax.swing.DefaultComboBoxModel;
022: import javax.swing.MutableComboBoxModel;
023: import javax.swing.event.ListDataListener;
024:
025: import net.sourceforge.squirrel_sql.fw.util.log.ILogger;
026: import net.sourceforge.squirrel_sql.fw.util.log.LoggerController;
027:
028: import java.util.ArrayList;
029:
030: /**
031: * TODO: JavaDoc
032: *
033: * @author Colin Bell
034: */
035: public class SQLHistoryComboBoxModel extends DefaultComboBoxModel {
036: /** Logger for this class. */
037: private static final ILogger s_log = LoggerController
038: .createLogger(SQLHistoryComboBoxModel.class);
039:
040: /** Shared data model. */
041: private static MutableComboBoxModel s_sharedDataModel;
042:
043: /** Actual data model. */
044: private MutableComboBoxModel _dataModel;
045:
046: /** The currently selected model. */
047: private Object _selectedObject;
048:
049: public SQLHistoryComboBoxModel(boolean useSharedModel) {
050: super ();
051: if (useSharedModel && s_sharedDataModel == null) {
052: throw new IllegalStateException(
053: "Shared instance has not been initialized");
054: }
055: _dataModel = useSharedModel ? s_sharedDataModel
056: : new DefaultComboBoxModel();
057: }
058:
059: public synchronized static void initializeSharedInstance(
060: Object[] data) {
061: if (s_sharedDataModel != null) {
062: s_log
063: .error("Shared data model has already been initialized");
064: } else {
065: s_sharedDataModel = new DefaultComboBoxModel(data);
066: }
067: }
068:
069: /**
070: * Is this model using the shared data model?
071: *
072: * @return <TT>true</TT> if this model is using the shared data model.
073: */
074: public boolean isUsingSharedDataModel() {
075: return _dataModel == s_sharedDataModel;
076: }
077:
078: /**
079: * Specify whether this model is usning the shared data model.
080: *
081: * @param use <TT>true</TT> use the shared model.
082: */
083: public synchronized void setUseSharedModel(boolean use) {
084: if (isUsingSharedDataModel() != use) {
085: _dataModel = use ? s_sharedDataModel
086: : duplicateSharedDataModel();
087: }
088: }
089:
090: /**
091: * Add an element to this model.
092: *
093: * This method is passed onto the data model that this data model is
094: * wrapped around.
095: *
096: * @param object The object to be added.
097: */
098: public void addElement(Object object) {
099: _dataModel.addElement(object);
100: }
101:
102: /**
103: * Add an item at a specified index.
104: *
105: * This method is passed onto the data model that this data model is
106: * wrapped around.
107: *
108: * @param object The object to be added.
109: * @param index The index to add it at.
110: */
111: public void insertElementAt(Object object, int index) {
112: _dataModel.insertElementAt(object, index);
113: }
114:
115: /**
116: * Remove the passed object from this collection.
117: *
118: * This method is passed onto the data model that this data model is
119: * wrapped around.
120: *
121: * @param object The object to be removed.
122: */
123: public void removeElement(Object object) {
124: _dataModel.removeElement(object);
125: }
126:
127: /**
128: * Remove the element from this collection at the passed index.
129: *
130: * This method is passed onto the data model that this data model is
131: * wrapped around.
132: *
133: * @param index The index to remove an element from.
134: */
135: public void removeElementAt(int index) {
136: _dataModel.removeElementAt(index);
137: }
138:
139: /**
140: * Retrieve the element currently selected. This is <EM>not</EM> passed
141: * on to the wrapped model as this model is responsible for keeping track
142: * of the currently selected item.
143: *
144: * @return The object currently selected.
145: */
146: public Object getSelectedItem() {
147: return _selectedObject;
148: }
149:
150: /**
151: * @see javax.swing.ComboBoxModel#setSelectedItem(java.lang.Object)
152: */
153: public void setSelectedItem(Object object) {
154: _selectedObject = object;
155: fireContentsChanged(this , -1, -1);
156: }
157:
158: /**
159: * This method is passed onto the data model that this data model is
160: * wrapped around.
161: */
162: public void addListDataListener(ListDataListener arg0) {
163: _dataModel.addListDataListener(arg0);
164: }
165:
166: /**
167: * This method is passed onto the data model that this data model is
168: * wrapped around.
169: */
170: public Object getElementAt(int arg0) {
171: return _dataModel.getElementAt(arg0);
172: }
173:
174: /**
175: * Retrieve the number of elements in this model.
176: *
177: * This method is passed onto the data model that this data model is
178: * wrapped around.
179: *
180: * @return Number of elements in this model.
181: */
182: public int getSize() {
183: return _dataModel.getSize();
184: }
185:
186: /**
187: * This method is passed onto the data model that this data model is
188: * wrapped around.
189: */
190: public void removeListDataListener(ListDataListener arg0) {
191: _dataModel.removeListDataListener(arg0);
192: }
193:
194: protected synchronized MutableComboBoxModel duplicateSharedDataModel() {
195: MutableComboBoxModel newModel = new DefaultComboBoxModel();
196: for (int i = 0, limit = s_sharedDataModel.getSize(); i < limit; ++i) {
197: SQLHistoryItem obj = (SQLHistoryItem) s_sharedDataModel
198: .getElementAt(i);
199: newModel.addElement(obj.clone());
200: }
201: return newModel;
202: }
203:
204: public ArrayList<SQLHistoryItem> getItems() {
205: ArrayList<SQLHistoryItem> ret = new ArrayList<SQLHistoryItem>();
206:
207: for (int i = 0; i < _dataModel.getSize(); i++) {
208: ret.add((SQLHistoryItem) _dataModel.getElementAt(i));
209: }
210:
211: return ret;
212:
213: }
214: }
|