001: /*
002: * The contents of this file are subject to the Mozilla Public License
003: * Version 1.1 (the "License"); you may not use this file except in
004: * compliance with the License. You may obtain a copy of the License at
005: * http://www.mozilla.org/MPL/
006: *
007: * Software distributed under the License is distributed on an "AS IS"
008: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
009: * License for the specific language governing rights and limitations
010: * under the License.
011: *
012: * The Original Code is iSQL-Viewer, A Mutli-Platform Database Tool.
013: *
014: * The Initial Developer of the Original Code is iSQL-Viewer, A Mutli-Platform Database Tool.
015: * Portions created by Mark A. Kobold are Copyright (C) 2000-2007. All Rights Reserved.
016: *
017: * Contributor(s):
018: * Mark A. Kobold [mkobold <at> isqlviewer <dot> com].
019: *
020: * If you didn't download this code from the following link, you should check
021: * if you aren't using an obsolete version: http://www.isqlviewer.com
022: */
023: package org.isqlviewer.bookmarks;
024:
025: import java.awt.datatransfer.DataFlavor;
026: import java.awt.datatransfer.Transferable;
027: import java.awt.datatransfer.UnsupportedFlavorException;
028: import java.io.IOException;
029: import java.io.Serializable;
030: import java.sql.SQLException;
031:
032: import org.isqlviewer.sql.embedded.EmbeddedDatabase;
033: import org.isqlviewer.util.BasicUtilities;
034:
035: /**
036: * TODO Add BookmarkReference JavaDoc inforamation
037: * <p>
038: *
039: * @author Mark A. Kobold <mkobold at isqlviewer dot com>
040: * @version 1.0
041: */
042: public class BookmarkReference implements Transferable, Serializable {
043:
044: /**
045: * Standard data-transfer flavor for DnD operations.
046: */
047: public static final DataFlavor BOOKMARK_REFERENCE_FLAVOR = new DataFlavor(
048: BookmarkReference.class, "iSQL-Viewer Bookmark Reference");
049:
050: private static final long serialVersionUID = 3377871201547716160L;
051: private long id = -1;
052: private String name = null;
053: private BookmarkFolder folder = null;
054: private boolean favorite = false;
055: private ColorLabel colorLabel = null;
056:
057: @Override
058: public boolean equals(Object other) {
059:
060: if (other instanceof BookmarkReference) {
061: BookmarkReference reference = (BookmarkReference) other;
062: return id == reference.id;
063: }
064: return false;
065: }
066:
067: @Override
068: public int hashCode() {
069:
070: return (int) (id ^ (id >>> 32));
071: }
072:
073: @Override
074: public String toString() {
075:
076: return name;
077: }
078:
079: /**
080: * Determine if this flag is a favourite.
081: * <p>
082: * If a bookmark has the favourite flag set to true, then the bookmark will also be made available into a special
083: * favourites folder.
084: *
085: * @return if this bookmark is a favourite bookmark.
086: */
087: public boolean isFavorite() {
088:
089: return favorite;
090: }
091:
092: /**
093: * Modifies the favourite flag for this bookmark instance.
094: * <p>
095: *
096: * @param favorite setting to enable/disable the favourite status of this bookmark.
097: */
098: public void setFavorite(boolean favorite) {
099:
100: this .favorite = favorite;
101: }
102:
103: /**
104: * @param folder the folder to set
105: */
106: public void setFolder(BookmarkFolder folder) {
107:
108: this .folder = folder;
109: }
110:
111: /**
112: * @return the id
113: */
114: public long getId() {
115:
116: return id;
117: }
118:
119: /**
120: * @param id the id to set
121: */
122: public void setId(long id) {
123:
124: this .id = id;
125: }
126:
127: /**
128: * Gets the user defined name for this bookmark.
129: * <p>
130: *
131: * @return the conical name for this bookmark as defined by the user.
132: */
133: public String getName() {
134:
135: return name == null ? "" : name;
136: }
137:
138: /**
139: * Sets the user defined name for this bookmark.
140: * <p>
141: * If a <tt>null</tt> name is provided, then a empty string will be inferred as the intended value of the new
142: * name.
143: *
144: * @param name of the bookmark to be identified by the user.
145: */
146: public void setName(String name) {
147:
148: this .name = name == null ? "" : name;
149: }
150:
151: /**
152: * Gets the path of this folder.
153: * <p>
154: *
155: * @return UNIX like path of the folder that contains this bookmark.
156: */
157: public String getPath() {
158:
159: return folder == null ? "/" : folder.getPath();
160: }
161:
162: /**
163: * Gets the label color for this bookmark.
164: * <p>
165: * Allows specific client-side coloring for particular bookmarks; inspired from Finder.
166: *
167: * @return the colorLabel to color the label; can be null;
168: */
169: public ColorLabel getColorLabel() {
170:
171: return colorLabel;
172: }
173:
174: /**
175: * Sets the label color color for displaying this bookmark.
176: * <p>
177: *
178: * @param colorLabel the colorLabel to set; use null to turn of
179: */
180: public void setColorLabel(ColorLabel labelColor) {
181:
182: this .colorLabel = labelColor;
183: }
184:
185: public Object getTransferData(DataFlavor flavor)
186: throws UnsupportedFlavorException, IOException {
187:
188: if (flavor != null) {
189: if (BOOKMARK_REFERENCE_FLAVOR.equals(flavor)) {
190: return this ;
191: }
192: Bookmark bookmark = null;
193: EmbeddedDatabase database = EmbeddedDatabase
194: .getSharedInstance();
195: try {
196: bookmark = database.getBookmark(this );
197: bookmark.setFolder(getFolder());
198: } catch (SQLException sourceError) {
199: IOException wrappedError = new IOException(sourceError
200: .getMessage());
201: BasicUtilities.wrapThrowable(sourceError, wrappedError);
202: throw wrappedError;
203: }
204: if (Bookmark.BOOKMARK_FLAVOR.equals(flavor)) {
205: return bookmark;
206: } else if (flavor.isFlavorTextType()) {
207: return bookmark.getCommandText();
208: }
209:
210: }
211: throw new UnsupportedFlavorException(flavor);
212: }
213:
214: public DataFlavor[] getTransferDataFlavors() {
215:
216: return new DataFlavor[] { BOOKMARK_REFERENCE_FLAVOR,
217: Bookmark.BOOKMARK_FLAVOR, DataFlavor.stringFlavor };
218: }
219:
220: public boolean isDataFlavorSupported(DataFlavor flavor) {
221:
222: if (flavor != null) {
223: if (BOOKMARK_REFERENCE_FLAVOR.equals(flavor)) {
224: return true;
225: } else if (Bookmark.BOOKMARK_FLAVOR.equals(flavor)) {
226: return true;
227: } else if (flavor.isFlavorTextType()) {
228: return true;
229: }
230: }
231: return false;
232: }
233:
234: /**
235: * Protected call for getting a reference to this bookmarks parent folder.
236: * <p>
237: * Bookmarks publicly should be always be manipulated from the folder object that owns it.
238: *
239: * @return reference to the owning folder.
240: */
241: public BookmarkFolder getFolder() {
242:
243: return folder;
244: }
245:
246: }
|