001: /*
002: * Copyright (c) 2000, Jacob Smullyan.
003: *
004: * This is part of SkunkDAV, a WebDAV client. See http://skunkdav.sourceforge.net/
005: * for the latest version.
006: *
007: * SkunkDAV is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License as published
009: * by the Free Software Foundation; either version 2, or (at your option)
010: * any later version.
011: *
012: * SkunkDAV 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: * General Public License for more details.
016: *
017: * You should have received a copy of the GNU General Public License
018: * along with SkunkDAV; see the file COPYING. If not, write to the Free
019: * Software Foundation, 59 Temple Place - Suite 330, Boston, MA
020: * 02111-1307, USA.
021: */
022:
023: package org.skunk.dav.client.gui;
024:
025: import java.awt.Component;
026: import java.awt.Font;
027: import java.awt.FontMetrics;
028: import java.util.StringTokenizer;
029: import javax.swing.DefaultListCellRenderer;
030: import javax.swing.JList;
031: import org.skunk.dav.client.DAVConstants;
032: import org.skunk.swing.TreeNodeChooser;
033: import org.skunk.trace.Debug;
034:
035: //for test
036: import javax.swing.tree.DefaultMutableTreeNode;
037: import javax.swing.tree.TreeNode;
038: import javax.swing.tree.TreePath;
039: import javax.swing.JOptionPane;
040: import org.skunk.config.Configurator;
041: import org.skunk.config.InMemoryConfigStore;
042:
043: public class DAVFileChooser extends TreeNodeChooser {
044: //this shouldn't be hard-coded -- TO BE DONE
045: private int maximumLabelLength = 320;
046:
047: public DAVFileChooser(DAVTreeModel model) {
048: super (model);
049: setRootVisible(false);
050: setNodeSeparator(DAVConstants.DAV_FILE_SEPARATOR);
051: setComboBoxCellRenderer(new DAVFileComboRenderer());
052: }
053:
054: protected int getMaximumLabelLength() {
055: return maximumLabelLength;
056: }
057:
058: protected void setMaximumLabelLength(int maximumLabelLength) {
059: this .maximumLabelLength = maximumLabelLength;
060: }
061:
062: private class DAVFileComboRenderer extends DefaultListCellRenderer {
063: public Component getListCellRendererComponent(JList list,
064: Object value, int index, boolean isSelected,
065: boolean cellHasFocus) {
066: String formattedValue = format(value).toString();
067: Component c = super .getListCellRendererComponent(list,
068: formattedValue, index, isSelected, cellHasFocus);
069: //Debug.trace(this, Debug.DP5, "renderer preferred size: "+c.getPreferredSize());
070: Font daFont = c.getFont();
071: FontMetrics daMetrics = c.getFontMetrics(daFont);
072: int diff = daMetrics.stringWidth(formattedValue)
073: - maximumLabelLength;
074: if (diff >= 0) {
075: int garbage = 1; //trash this many tokens
076: while (diff >= 0) {
077: StringBuffer formatBuffer = new StringBuffer(".../");
078: StringTokenizer st = new StringTokenizer(
079: formattedValue,
080: DAVConstants.DAV_FILE_SEPARATOR);
081: for (int i = 0; st.hasMoreTokens(); i++) {
082: String thing = st.nextToken();
083: if (i <= garbage)
084: continue;
085: formatBuffer.append(thing).append(
086: DAVConstants.DAV_FILE_SEPARATOR);
087: }
088: garbage++;
089: formattedValue = formatBuffer.toString();
090: diff = daMetrics.stringWidth(formattedValue)
091: - maximumLabelLength;
092: }
093: return super
094: .getListCellRendererComponent(list,
095: formattedValue, index, isSelected,
096: cellHasFocus);
097: }
098: return c;
099: }
100:
101: private Object format(Object value) {
102: if (value instanceof DAVTreeNode)
103: return ((DAVTreeNode) value).getDAVFile().getFullName();
104: if (value == null)
105: return "";
106: return value;
107: }
108: }
109:
110: public ChoiceStruct getChoiceStruct() {
111: Object lastPathComponent = getSelectedPath()
112: .getLastPathComponent();
113: if (lastPathComponent == null
114: || !(lastPathComponent instanceof DAVTreeNode)) {
115: return null;
116: }
117: DAVTreeNode parentNode = (DAVTreeNode) lastPathComponent;
118: Object leafNode = getSelectedLeaf();
119:
120: return new ChoiceStruct(
121: parentNode,
122: getEntryFieldText(),
123: ((leafNode != null && leafNode instanceof DAVTreeNode) ? (DAVTreeNode) leafNode
124: : null));
125: }
126:
127: public static class ChoiceStruct {
128: private DAVTreeNode parentNode;
129: private String entryText;
130: private DAVTreeNode selectedLeaf;
131: private String newPath;
132:
133: private ChoiceStruct(DAVTreeNode parentNode, String entryText,
134: DAVTreeNode selectedLeaf) {
135: this .parentNode = parentNode;
136: this .entryText = entryText;
137: this .selectedLeaf = selectedLeaf;
138: setNewPath();
139: }
140:
141: public DAVTreeNode getParentNode() {
142: return this .parentNode;
143: }
144:
145: public void setParentNode(DAVTreeNode parentNode) {
146: this .parentNode = parentNode;
147: }
148:
149: public String getFilename() {
150: return this .entryText;
151: }
152:
153: public String getFilePath() {
154: return this .newPath;
155: }
156:
157: public DAVTreeNode getSelectedLeaf() {
158: return this .selectedLeaf;
159: }
160:
161: private void setNewPath() {
162: if (parentNode != null) {
163: StringBuffer sb = new StringBuffer(parentNode
164: .getDAVFile().getFullName());
165: if (entryText != null)
166: sb.append(entryText);
167: newPath = sb.toString();
168: } else
169: newPath = null;
170: }
171:
172: }
173:
174: public static class Test {
175: public static void main(String[] args) {
176: DefaultMutableTreeNode rootNode = new DefaultMutableTreeNode(
177: "oink");
178: DAVTreeModel model = new DAVTreeModel(rootNode, true);
179: //throwaway configurator, just to get the ServerData class not to barf
180: Configurator.setConfigurator(new Configurator(
181: new InMemoryConfigStore()));
182: ServerData sd = new ServerData("support.skunk.org", 8888,
183: "/", "jsmullyan", "");
184: model.addConnectionNode(sd, null);
185: //let it load
186: while (model.getChildCount(rootNode) < 1) {
187: try {
188: Thread.sleep(333);
189: } catch (InterruptedException ie) {
190: }
191: }
192: DAVFileChooser chooser = new DAVFileChooser(model);
193: if (args.length > 0 && args[0].equals("visible"))
194: chooser.setRootVisible(true);
195: chooser.setCurrentPath(new TreePath(model
196: .getPathToRoot((TreeNode) model.getChild(rootNode,
197: 0))));
198: JOptionPane.showMessageDialog(null, chooser);
199: System.exit(0);
200: }
201: }
202: }
203:
204: /* $Log: DAVFileChooser.java,v $
205: /* Revision 1.3 2001/06/05 01:09:53 smulloni
206: /* fixed bug wtih failed connection attempts, which used to set the
207: /* connection flag as if the connection had succeeded.
208: /*
209: /* Revision 1.2 2001/01/03 20:11:31 smulloni
210: /* the DAVFileChooser now replaces JFileChooser for remote file access.
211: /* DAVMethod now has a protocol property.
212: /*
213: /* Revision 1.1 2001/01/03 00:30:35 smulloni
214: /* a number of modifications along the way to replacing JFileChooser with
215: /* something more suitable for remote (virtual) files.
216: /* */
|