001: package net.sourceforge.squirrel_sql.plugins.favs;
002:
003: /*
004: * Copyright (C) 2001 Colin Bell
005: * colbell@users.sourceforge.net
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License
009: * as published by the Free Software Foundation; either version 2
010: * of the License, or any later version.
011: *
012: * This program 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
015: * GNU General Public License for more details.
016: *
017: * You should have received a copy of the GNU General Public License
018: * along with this program; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
020: */
021: import java.beans.PropertyChangeSupport;
022: import java.io.Serializable;
023: import java.util.ArrayList;
024: import java.util.Iterator;
025: import java.util.List;
026:
027: import net.sourceforge.squirrel_sql.client.util.IdentifierFactory;
028: import net.sourceforge.squirrel_sql.fw.id.IHasIdentifier;
029: import net.sourceforge.squirrel_sql.fw.id.IIdentifier;
030: import net.sourceforge.squirrel_sql.fw.persist.IValidatable;
031: import net.sourceforge.squirrel_sql.fw.persist.ValidationException;
032: import net.sourceforge.squirrel_sql.fw.util.StringManager;
033: import net.sourceforge.squirrel_sql.fw.util.StringManagerFactory;
034:
035: /**
036: * This class represents a folder within which queries that can be stored.
037: */
038: public final class Folder implements Cloneable, Serializable,
039: IHasIdentifier, IValidatable /*, IHasName*/{
040:
041: private static final long serialVersionUID = 1L;
042:
043: private static final StringManager s_stringMgr = StringManagerFactory
044: .getStringManager(Folder.class);
045:
046: private static final String EMPTY_STRING = "";
047:
048: /**
049: * This interface defines locale specific strings. This should be
050: * replaced with a property file.
051: */
052: private interface i18n {
053: // i18n[favs.nameMustNotBeBlank=Name cannot be blank.]
054: static String ERR_BLANK_NAME = s_stringMgr
055: .getString("favs.nameMustNotBeBlank");
056: }
057:
058: public interface IPropertyNames {
059: String ID = "Identifier";
060: String NAME = "Name";
061: String SUB_FOLDERS = "SubFolders";
062: }
063:
064: /** The <CODE>IIdentifier</CODE> that uniquely identifies this object. */
065: private IIdentifier _id;
066:
067: /** Name. */
068: private String _name;
069:
070: /** Folders that this object contains. */
071: private List<Folder> _subFolders = new ArrayList<Folder>();
072:
073: /** Object to handle property change events. */
074: private transient PropertyChangeSupport _propChgNotifier = null;
075:
076: /**
077: * Default ctor.
078: */
079: public Folder() {
080: this (IdentifierFactory.getInstance().createIdentifier(),
081: EMPTY_STRING);
082: }
083:
084: /**
085: * Ctor specifying this objects attributes.
086: *
087: * @param id Uniquely identifies this object.
088: * @param name Name of this folder.
089: */
090: public Folder(IIdentifier id, String name) {
091: super ();
092: _id = id != null ? id : IdentifierFactory.getInstance()
093: .createIdentifier();
094: _name = getString(name);
095: }
096:
097: /**
098: * Two <CODE>Folder</CODE> objects are considered equal if their ID's are
099: * identical.
100: *
101: * @return <CODE>true</CODE> if this objects is equal to the passed one.
102: */
103: public boolean equals(Object rhs) {
104: boolean rc = false;
105: if (rhs != null && rhs.getClass().equals(getClass())) {
106: rc = ((Folder) rhs).getIdentifier().equals(getIdentifier());
107: }
108: return rc;
109: }
110:
111: /**
112: * Return a copy of this object.
113: */
114: public Object clone() {
115: try {
116: return super .clone();
117: } catch (CloneNotSupportedException ex) {
118: throw new InternalError(ex.getMessage()); // Impossible.
119: }
120: }
121:
122: public synchronized int hashCode() {
123: return getIdentifier().hashCode();
124: }
125:
126: public String toString() {
127: return getName();
128: }
129:
130: /**
131: * Returns <CODE>true</CODE> if this object is valid.<P>
132: * Implementation for <CODE>IPersistable</CODE>.
133: */
134: public synchronized boolean isValid() {
135: return _name.trim().length() > 0;
136: }
137:
138: public IIdentifier getIdentifier() {
139: return _id;
140: }
141:
142: public void setIdentifier(IIdentifier id) {
143: _id = id;
144: }
145:
146: public String getName() {
147: return _name;
148: }
149:
150: public void setName(String name) throws ValidationException {
151: String data = getString(name);
152: if (data.length() == 0) {
153: throw new ValidationException(i18n.ERR_BLANK_NAME);
154: }
155: if (_name != data) {
156: final String oldValue = _name;
157: _name = data;
158: getPropertyChangeNotifier().firePropertyChange(
159: IPropertyNames.NAME, oldValue, _name);
160: }
161: }
162:
163: //public Folder getSubFolder(int idx) throws IndexOutOfBoundsException {
164: /// return (Folder)_folders.get(idx);
165: //}
166:
167: public void addSubFolder(Folder subFolder)
168: throws IllegalArgumentException {
169: if (subFolder == null) {
170: throw new IllegalArgumentException("Null Folder passed");
171: }
172: _subFolders.add(subFolder);
173: }
174:
175: public boolean removeSubFolder(Folder subFolder)
176: throws IllegalArgumentException {
177: if (subFolder == null) {
178: throw new IllegalArgumentException("Null Folder passed");
179: }
180: return _subFolders.remove(subFolder);
181: }
182:
183: public Iterator<Folder> subFolders() {
184: return _subFolders.iterator();
185: }
186:
187: public Folder[] getSubFolders() {
188: return _subFolders.toArray(new Folder[_subFolders.size()]);
189: }
190:
191: public Folder getSubFolder(int idx)
192: throws ArrayIndexOutOfBoundsException {
193: return _subFolders.get(idx);
194: }
195:
196: public void setSubFolders(Folder[] value) {
197: _subFolders.clear();
198: if (value != null) {
199: for (int i = 0; i < value.length; ++i) {
200: _subFolders.add(value[i]);
201: }
202: }
203: }
204:
205: public void setSubFolder(int idx, Folder value)
206: throws ArrayIndexOutOfBoundsException {
207: _subFolders.set(idx, value);
208: }
209:
210: private PropertyChangeSupport getPropertyChangeNotifier() {
211: if (_propChgNotifier == null) {
212: _propChgNotifier = new PropertyChangeSupport(this );
213: }
214: return _propChgNotifier;
215: }
216:
217: private String getString(String data) {
218: return data != null ? data.trim() : "";
219: }
220: }
|