001: /*
002: * @(#)FolderToolBar.java 10/6/2005
003: *
004: * Copyright 2002 - 2005 JIDE Software Inc. All rights reserved.
005: */
006: package com.jidesoft.plaf.basic;
007:
008: import com.jidesoft.utils.SystemInfo;
009:
010: import javax.swing.*;
011: import javax.swing.event.PopupMenuEvent;
012: import javax.swing.event.PopupMenuListener;
013: import javax.swing.filechooser.FileSystemView;
014: import java.awt.*;
015: import java.awt.event.ActionEvent;
016: import java.io.File;
017: import java.util.ArrayList;
018: import java.util.List;
019: import java.util.Locale;
020: import java.util.ResourceBundle;
021:
022: /**
023: * Manages the optional folder toolbar that sits above the folderview's tree view panel
024: */
025: class FolderToolBar extends JToolBar {
026: private final static FileSystemView _fsv = FileSystemView
027: .getFileSystemView();
028:
029: private JButton _deleteFolderBtn;
030: private JButton _newFolderBtn;
031: private JComboBox _recentFoldersList;
032:
033: private List<FolderToolBarListener> _listeners = new ArrayList(1);
034:
035: public FolderToolBar(boolean showRecentFolders,
036: List<String> recentFoldersList) {
037: setFloatable(false);
038: setupToolBar(showRecentFolders, recentFoldersList);
039:
040: }
041:
042: public void enableDelete() {
043: _deleteFolderBtn.setEnabled(true);
044: }
045:
046: public void disableDelete() {
047: _deleteFolderBtn.setEnabled(false);
048: }
049:
050: public void enableNewFolder() {
051: _newFolderBtn.setEnabled(true);
052: }
053:
054: public void disableNewFolder() {
055: _newFolderBtn.setEnabled(false);
056: }
057:
058: /**
059: * Creates the toolbar buttons and dropdown
060: */
061: private void setupToolBar(boolean showRecentFolders,
062: List<String> recentFoldersList) {
063:
064: // add to toolbar
065: if (showRecentFolders) {
066: _recentFoldersList = new JComboBox(
067: new DefaultComboBoxModel());
068: if (recentFoldersList != null
069: && recentFoldersList.size() > 0) {
070: _recentFoldersList.setModel(new DefaultComboBoxModel(
071: (recentFoldersList.toArray())));
072: }
073: _recentFoldersList.setEditable(false);
074: _recentFoldersList.setRenderer(new FileListCellRenderer());
075: _recentFoldersList
076: .addPopupMenuListener(new PopupMenuListener() {
077: private boolean m_wasCancelled = false;
078:
079: public void popupMenuWillBecomeVisible(
080: PopupMenuEvent e) {
081: m_wasCancelled = false;
082: }
083:
084: public void popupMenuWillBecomeInvisible(
085: PopupMenuEvent e) {
086: if (e.getSource() instanceof JComboBox) {
087: JComboBox box = (JComboBox) e
088: .getSource();
089:
090: Object selectedFile = box.getModel()
091: .getSelectedItem();
092: // if popup was not cancelled then select the folder
093: if (!m_wasCancelled
094: && selectedFile != null) {
095: // System.out.println("User selected file: " + selectedFile.getAbsolutePath());
096: if (selectedFile instanceof File) {
097: recentFolderSelected((File) selectedFile);
098: } else {
099: recentFolderSelected(new File(
100: "" + selectedFile));
101: }
102: }
103: }
104: }
105:
106: public void popupMenuCanceled(PopupMenuEvent e) {
107: m_wasCancelled = true;
108: }
109: });
110: _recentFoldersList
111: .setPrototypeDisplayValue("AAAAAAAAAAAAAAAAAA");
112: add(new JLabel("Recent: "));
113: add(_recentFoldersList);
114: addSeparator();
115: } else {
116: add(Box.createHorizontalGlue());
117: }
118:
119: JButton desktopBtn = new NoFocusButton(
120: new ToolBarAction(
121: null,
122: SystemInfo.isWindows() ? _fsv
123: .getSystemIcon(_fsv.getHomeDirectory())
124: : BasicFolderChooserIconsFactory
125: .getImageIcon(BasicFolderChooserIconsFactory.ToolBar.HOME)) {
126: public void actionPerformed(ActionEvent e) {
127: desktopButtonClicked();
128: }
129: });
130:
131: final ResourceBundle resourceBundle = FolderChooserResource
132: .getResourceBundle(Locale.getDefault());
133: desktopBtn
134: .setToolTipText(SystemInfo.isWindows() ? resourceBundle
135: .getString("FolderChooser.toolbar.desktop")
136: : resourceBundle
137: .getString("FolderChooser.toolbar.home"));
138: add(desktopBtn);
139:
140: if (SystemInfo.isWindows()) {
141: JButton myDocumentsBtn = new NoFocusButton(
142: new ToolBarAction(null, _fsv.getSystemIcon(_fsv
143: .getDefaultDirectory())) {
144: public void actionPerformed(ActionEvent e) {
145: myDocumentsButtonClicked();
146: }
147: });
148: myDocumentsBtn.setToolTipText(resourceBundle
149: .getString("FolderChooser.toolbar.mydocuments"));
150: add(myDocumentsBtn);
151: }
152: // dredge up appropriate icons
153: Icon deleteIcon = BasicFolderChooserIconsFactory
154: .getImageIcon(BasicFolderChooserIconsFactory.ToolBar.DELETE);
155:
156: _deleteFolderBtn = new NoFocusButton(new ToolBarAction(null,
157: deleteIcon) {
158: public void actionPerformed(ActionEvent e) {
159: deleteFolderButtonClicked();
160: }
161: });
162:
163: _deleteFolderBtn.setToolTipText(resourceBundle
164: .getString("FolderChooser.toolbar.delete"));
165:
166: Icon newFolderIcon = BasicFolderChooserIconsFactory
167: .getImageIcon(BasicFolderChooserIconsFactory.ToolBar.NEW);
168: _newFolderBtn = new NoFocusButton(new ToolBarAction(null,
169: newFolderIcon) {
170: public void actionPerformed(ActionEvent e) {
171: newFolderButtonClicked();
172: }
173: });
174:
175: _newFolderBtn.setToolTipText(resourceBundle
176: .getString("FolderChooser.toolbar.new"));
177:
178: addSeparator();
179: add(_deleteFolderBtn);
180: add(_newFolderBtn);
181: }
182:
183: private class FileListCellRenderer implements ListCellRenderer {
184: protected DefaultListCellRenderer m_defaultRenderer = new DefaultListCellRenderer();
185:
186: public Component getListCellRendererComponent(JList list,
187: Object value, int index, boolean isSelected,
188: boolean cellHasFocus) {
189: JLabel renderer = (JLabel) m_defaultRenderer
190: .getListCellRendererComponent(list, value, index,
191: isSelected, cellHasFocus);
192: File f = null;
193: if (value instanceof File) {
194: f = (File) value;
195: } else if (value != null) {
196: f = new File(value.toString());
197: }
198:
199: if (f != null && f.exists()) {
200: String text = _fsv.getSystemDisplayName(f);
201: Icon icon = _fsv.getSystemIcon(f);
202: renderer.setIcon(icon);
203: renderer.setText(text);
204: renderer.setToolTipText(f.getAbsolutePath());
205: } else {
206: String filePath = value == null ? "" : value.toString();
207: renderer.setText(filePath);
208: renderer.setToolTipText(filePath);
209: }
210: return renderer;
211: }
212: }
213:
214: // ----------------------------------------------------------------
215: // Listener methods
216: // ----------------------------------------------------------------
217: public void addListener(FolderToolBarListener listener) {
218: _listeners.add(listener);
219: }
220:
221: public void removeListener(FolderToolBarListener listener) {
222: _listeners.remove(listener);
223: }
224:
225: public void clearListeners() {
226: _listeners.clear();
227: }
228:
229: private void deleteFolderButtonClicked() {
230: for (FolderToolBarListener listener : _listeners) {
231: listener.deleteFolderButtonClicked();
232: }
233: }
234:
235: private void newFolderButtonClicked() {
236: for (FolderToolBarListener listener : _listeners) {
237: listener.newFolderButtonClicked();
238: }
239: }
240:
241: private void myDocumentsButtonClicked() {
242: for (FolderToolBarListener listener : _listeners) {
243: listener.myDocumentsButtonClicked();
244: }
245: }
246:
247: private void desktopButtonClicked() {
248: for (FolderToolBarListener listener : _listeners) {
249: listener.desktopButtonClicked();
250: }
251: }
252:
253: private void recentFolderSelected(File recentFolder) {
254: for (FolderToolBarListener listener : _listeners) {
255: listener.recentFolderSelected(recentFolder);
256: }
257: }
258:
259: public void setRecentList(List<String> recentFoldersList) {
260: _recentFoldersList.setModel(new DefaultComboBoxModel(
261: (recentFoldersList.toArray())));
262: }
263:
264: private abstract class ToolBarAction extends AbstractAction {
265: public ToolBarAction(String name, Icon icon) {
266: super (name, icon);
267: }
268: }
269:
270: static class NoFocusButton extends JButton {
271: public NoFocusButton(Action a) {
272: super (a);
273: setRequestFocusEnabled(false);
274: setFocusable(false);
275:
276: // on jdk1.6, the button size is wrong
277: Insets margin = getMargin();
278: margin.left = margin.top;
279: margin.right = margin.bottom;
280: setMargin(margin);
281: }
282: }
283: }
|