001: /*
002: * Beryl - A web platform based on XML, XSLT and Java
003: * This file is part of the Beryl XML GUI
004: *
005: * Copyright (C) 2004 Wenzel Jakob <wazlaf@tigris.org>
006: *
007: * This program 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 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 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 program; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-3107 USA
020: */
021:
022: package org.beryl.gui.swing;
023:
024: import java.awt.Dimension;
025: import java.awt.Font;
026: import java.awt.Rectangle;
027: import java.beans.PropertyChangeEvent;
028: import java.beans.PropertyChangeListener;
029: import java.util.HashSet;
030: import java.util.Iterator;
031:
032: import javax.swing.JComponent;
033: import javax.swing.ListModel;
034: import javax.swing.LookAndFeel;
035: import javax.swing.Scrollable;
036: import javax.swing.UIManager;
037: import javax.swing.event.ListSelectionEvent;
038: import javax.swing.event.ListSelectionListener;
039:
040: import org.beryl.gui.swing.plaf.IconViewUI;
041:
042: /**
043: * JIconView - A simple Java icon view widget
044: * @version 2.0
045: * @author Wenzel Jakob
046: */
047:
048: public class JIconView extends JComponent implements Scrollable {
049: private static final String uiClassID = "IconViewUI";
050: public static final String MODEL_PROPERTY = "model";
051: public static final String XCELLSIZE_PROPERTY = "xCellSize";
052: public static final String YCELLSIZE_PROPERTY = "yCellSize";
053: public static final String ICONTEXTSPACING_PROPERTY = "iconTextSpacing";
054: public static final String HANDCURSORENABLED_PROPERTY = "handCursorEnabled";
055:
056: private ListModel model = null;
057: private int xCellSize = 0;
058: private int yCellSize = 0;
059: private int iconTextSpacing = 0;
060: private int selectedIndex = -1;
061: private boolean handCursorEnabled = false;
062: private HashSet listeners = null;
063:
064: public JIconView() {
065: this (null);
066: }
067:
068: public JIconView(ListModel model) {
069: super ();
070: setModel(model);
071: setFont(new Font("SansSerif", Font.PLAIN, 10));
072: xCellSize = yCellSize = 80;
073: iconTextSpacing = 3;
074: listeners = new HashSet();
075: initialize();
076: updateUI();
077: }
078:
079: /* ========== Initialisation ========== */
080:
081: public static void initialize() {
082: setLookAndFeelProperties(UIManager.getLookAndFeel());
083: UIManager
084: .addPropertyChangeListener(new PropertyChangeListener() {
085: public void propertyChange(PropertyChangeEvent event) {
086: if ("lookAndFeel".equals(event
087: .getPropertyName())) {
088: setLookAndFeelProperties((LookAndFeel) event
089: .getNewValue());
090: }
091: }
092: });
093: }
094:
095: private static void setLookAndFeelProperties(LookAndFeel lookAndFeel) {
096: UIManager.put("IconViewUI",
097: "org.beryl.gui.swing.plaf.IconViewUI");
098: }
099:
100: /* ========== Model ========== */
101:
102: public void setModel(ListModel model) {
103: ListModel old = this .model;
104: this .model = model;
105: firePropertyChange(MODEL_PROPERTY, old, model);
106: }
107:
108: public ListModel getModel() {
109: return model;
110: }
111:
112: /* ========== Spacing ========== */
113:
114: public int getXCellSize() {
115: return xCellSize;
116: }
117:
118: public int getYCellSize() {
119: return yCellSize;
120: }
121:
122: public int getIconTextSpacing() {
123: return iconTextSpacing;
124: }
125:
126: public void setXCellSize(int xCellSize) {
127: int old = xCellSize;
128: this .xCellSize = xCellSize;
129: firePropertyChange(XCELLSIZE_PROPERTY, old, xCellSize);
130: }
131:
132: public void setYCellSize(int yCellSize) {
133: int old = yCellSize;
134: this .yCellSize = yCellSize;
135: firePropertyChange(YCELLSIZE_PROPERTY, old, yCellSize);
136: }
137:
138: public void setIconTextSpacing(int iconTextSpacing) {
139: int old = iconTextSpacing;
140: this .iconTextSpacing = iconTextSpacing;
141: firePropertyChange(ICONTEXTSPACING_PROPERTY, old,
142: iconTextSpacing);
143: }
144:
145: /* ========== Cursor ========== */
146:
147: public boolean getHandCursorEnabled() {
148: return handCursorEnabled;
149: }
150:
151: public void setHandCursorEnabled(boolean enabled) {
152: boolean old = handCursorEnabled;
153: handCursorEnabled = enabled;
154: firePropertyChange(ICONTEXTSPACING_PROPERTY, old,
155: handCursorEnabled);
156:
157: }
158:
159: /* ========== Listeners ========== */
160:
161: public void addListSelectionListener(ListSelectionListener listener) {
162: listeners.add(listener);
163: }
164:
165: public void removeListSelectionListener(
166: ListSelectionListener listener) {
167: listeners.remove(listener);
168: }
169:
170: /* ========== Selection ========== */
171:
172: public int getSelectedIndex() {
173: return selectedIndex;
174: }
175:
176: public void setSelectedIndex(int selectedIndex) {
177: if (model != null) {
178: try {
179: if (selectedIndex < model.getSize()
180: && selectedIndex >= -1) {
181: this .selectedIndex = selectedIndex;
182: ListSelectionEvent event = new ListSelectionEvent(
183: this , selectedIndex, selectedIndex, false);
184: for (Iterator i = listeners.iterator(); i.hasNext();) {
185: ((ListSelectionListener) i.next())
186: .valueChanged(event);
187: }
188:
189: }
190: } catch (ArrayIndexOutOfBoundsException e) {
191: }
192: }
193: }
194:
195: /* ========== Scrollable ========== */
196:
197: public Dimension getPreferredScrollableViewportSize() {
198: return getPreferredSize();
199: }
200:
201: public int getScrollableUnitIncrement(Rectangle visibleRect,
202: int orientation, int direction) {
203: return 1;
204: }
205:
206: public int getScrollableBlockIncrement(Rectangle visibleRect,
207: int orientation, int direction) {
208: return 1;
209: }
210:
211: public boolean getScrollableTracksViewportWidth() {
212: return true;
213: }
214:
215: public boolean getScrollableTracksViewportHeight() {
216: return false;
217: }
218:
219: /* ========== UI ========== */
220:
221: public IconViewUI getUI() {
222: return (IconViewUI) ui;
223: }
224:
225: public void setUI(IconViewUI ui) {
226: super .setUI(ui);
227: }
228:
229: public void updateUI() {
230: setUI((IconViewUI) UIManager.getUI(this ));
231: invalidate();
232: }
233:
234: public String getUIClassID() {
235: return uiClassID;
236: }
237: }
|