001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * Portions Copyrighted 2007 Sun Microsystems, Inc.
027: */
028: package org.netbeans.modules.php.rt.providers.impl.ftp.nodes;
029:
030: import java.util.Collections;
031: import java.util.Comparator;
032: import java.util.LinkedList;
033: import java.util.List;
034: import org.netbeans.api.progress.ProgressHandle;
035: import org.netbeans.api.progress.ProgressHandleFactory;
036: import org.netbeans.modules.php.rt.providers.impl.ftp.FtpHostImpl;
037: import org.netbeans.modules.php.rt.providers.impl.ftp.ftpclient.impl.FtpConnection;
038: import org.netbeans.modules.php.rt.providers.impl.ftp.ftpclient.FtpException;
039: import org.netbeans.modules.php.rt.providers.impl.ftp.ftpclient.FtpFileInfo;
040: import org.netbeans.modules.php.rt.providers.impl.ftp.ftpclient.FtpClientLoginException;
041: import org.netbeans.modules.php.rt.providers.impl.ftp.ftpclient.impl.FtpFileInfoComparator;
042: import org.netbeans.modules.php.rt.providers.impl.ftp.ftpclient.ui.FtpDialogs;
043: import org.netbeans.modules.php.rt.spi.providers.Host;
044: import org.openide.nodes.Children;
045: import org.openide.nodes.Node;
046: import org.openide.util.NbBundle;
047:
048: /**
049: *
050: * @author avk
051: */
052: public class FtpBaseObjectNodeChildren extends Children.Keys {
053:
054: private static final String RETRIEVE_NODE_PROGRESS_TITLE = "LBL_Retrieve_Node_Progress_Title"; // NOI18N
055: static final String LBL_FTP_PATH_NOT_READABLE = "LBL_FtpPathNotReadable"; // NOI18N
056:
057: private static final String FTP_OUTPUT_TAB_TITLE = "LBL_OutputTabTitle";
058:
059: public FtpBaseObjectNodeChildren() {
060: }
061:
062: @Override
063: protected void addNotify() {
064: updateKeys();
065: }
066:
067: protected Node[] createNodes(Object key) {
068: FtpFileInfo file = null;
069: FtpHostImpl ftpHost = null;
070: Host host = getNode().getLookup().lookup(Host.class);
071:
072: if (key instanceof FtpFileInfo) {
073: file = (FtpFileInfo) key;
074: }
075:
076: if (host instanceof FtpHostImpl) {
077: ftpHost = (FtpHostImpl) host;
078: }
079:
080: return createChildNodes(file, ftpHost);
081: }
082:
083: public void removeKeys() {
084: setKeys(Collections.emptyList());
085: }
086:
087: public void updateKeys() {
088: String progressTitle = NbBundle.getMessage(
089: FtpBaseObjectNodeChildren.class,
090: RETRIEVE_NODE_PROGRESS_TITLE, getNode()
091: .getDisplayName());
092: ProgressHandle progress = ProgressHandleFactory
093: .createHandle(progressTitle); // NOI18N
094: progress.start();
095:
096: List<FtpFileInfo> fileList = null;
097:
098: // TODO replace FtpFileInfo with common interface
099: FtpFileInfo file = getNode().getLookup().lookup(
100: FtpFileInfo.class);
101:
102: try {
103: if (file == null || file.isDirectory()) {
104: Host host = getNode().getLookup().lookup(Host.class);
105: if (host instanceof FtpHostImpl) {
106: FtpHostImpl ftpHost = (FtpHostImpl) host;
107:
108: try {
109: fileList = loadChildren(file, ftpHost);
110: setNodeIcon();
111: } catch (FtpException ex) {
112: setNodeErrorIcon();
113: }
114: }
115: } // else means that there is no Host in parent node lookup
116: if (fileList == null) {
117: fileList = Collections.emptyList();
118: }
119: setKeys(fileList);
120: } finally {
121: progress.finish();
122: }
123: }
124:
125: private void setNodeIcon() {
126: Node node = getNode();
127: if (node instanceof FileInfoNode) {
128: ((FileInfoNode) node).setIcon();
129: }
130: if (node instanceof FtpDirectoryObjectNode) {
131: ((FtpDirectoryObjectNode) node).setIcon();
132: }
133: }
134:
135: private void setNodeErrorIcon() {
136: Node node = getNode();
137: if (node instanceof FileInfoNode) {
138: ((FileInfoNode) node).setErrorIcon();
139: }
140: if (node instanceof FtpDirectoryObjectNode) {
141: ((FtpDirectoryObjectNode) node).setErrorIcon();
142: }
143: }
144:
145: private Node[] createChildNodes(FtpFileInfo file,
146: FtpHostImpl ftpHost) {
147: if (file != null && ftpHost != null) {
148: FtpBaseObjectNode child = null;
149: if (file.isDirectory()) {
150: child = new FtpDirectoryObjectNode(ftpHost, file);
151: } else {
152: child = new FtpFileObjectNode(ftpHost, file);
153: }
154: return new Node[] { child };
155: }
156: return null;
157: }
158:
159: private FtpConnection.FtpLogger createFtpLogger(FtpHostImpl host) {
160: String outputTabTitle = NbBundle.getMessage(
161: FtpBaseObjectNodeChildren.class, FTP_OUTPUT_TAB_TITLE,
162: host.getDisplayName());
163:
164: return new FtpConnection.OutputTabFtpLogger(outputTabTitle);
165: }
166:
167: private FtpConnection createConnection(FtpHostImpl ftpHost)
168: throws FtpException {
169: FtpConnection conn = null;
170: String ftpServer = (String) ftpHost
171: .getProperty(FtpHostImpl.FTP_SERVER);
172: FtpConnection.FtpLogger ftpLogger = createFtpLogger(ftpHost);
173:
174: boolean retry = true;
175: Throwable problem = null;
176: while (retry) {
177: String ftpUserName = (String) ftpHost
178: .getProperty(FtpHostImpl.FTP_USER_NAME);
179: char[] ftpPassword = (char[]) ftpHost
180: .getProperty(FtpHostImpl.FTP_PASSWORD);
181: try {
182: if (conn != null) {
183: conn.closeServer();
184: }
185: conn = FtpConnection.createConnection(ftpServer,
186: ftpLogger);
187: conn
188: .login(ftpUserName, String
189: .copyValueOf(ftpPassword));
190: retry = false;
191: problem = null;
192: } catch (FtpClientLoginException lex) {
193: problem = lex;
194: retry = FtpDialogs.retryLoginDialog(ftpHost);
195: }
196: }
197: if (problem != null) {
198: throw new FtpException(problem.getMessage());
199: }
200: return conn;
201: }
202:
203: private List<FtpFileInfo> loadChildren(FtpFileInfo dirInfo,
204: FtpHostImpl ftpHost) throws FtpException {
205: List<FtpFileInfo> resultList = new LinkedList<FtpFileInfo>();
206: FtpConnection conn = null;
207: try {
208: String ftpDirectory = FtpHostImpl.Helper
209: .getFtpInitialDir(ftpHost);
210:
211: String dirToLoad = dirInfo != null ? dirInfo.getFullName()
212: : ftpDirectory;
213:
214: // connect to ftp and create nodes for file children
215: conn = createConnection(ftpHost);
216: cdFolder(conn, dirToLoad);
217:
218: List<FtpFileInfo> fileList = new LinkedList<FtpFileInfo>(
219: conn.list());
220: for (FtpFileInfo test : fileList) {
221: if (!skipFile(test)) {
222: resultList.add(test);
223: }
224: }
225: } catch (FtpException ex) {
226: throw ex;
227: } finally {
228: if (conn != null) {
229: conn.close();
230: }
231: }
232:
233: sortList(resultList);
234: return resultList;
235: }
236:
237: private void cdFolder(FtpConnection conn, String path)
238: throws FtpException {
239: try {
240: if (path != null) {
241: conn.cd(path);
242: }
243: } catch (FtpException ex) {
244: String message = NbBundle.getMessage(
245: FtpBaseObjectNodeChildren.class,
246: LBL_FTP_PATH_NOT_READABLE, path);
247: conn.getLogger().logError(message);
248: throw ex;
249: }
250: }
251:
252: private void sortList(List<FtpFileInfo> list) {
253: Collections.sort(list, comparator);
254: }
255:
256: private boolean skipFile(FtpFileInfo file) {
257: //if (file.isLink()) {
258: // return true;
259: //}
260: return false;
261: }
262:
263: private static Comparator<FtpFileInfo> comparator = new FtpFileInfoComparator();
264: }
|