001: /**
002: *
003: */package com.bostechcorp.cbesb.ui.util.browser;
004:
005: import java.io.File;
006:
007: import org.eclipse.core.runtime.Path;
008: import org.eclipse.swt.SWT;
009: import org.eclipse.swt.widgets.FileDialog;
010: import org.eclipse.swt.widgets.Shell;
011: import org.eclipse.swt.widgets.Text;
012:
013: import com.bostechcorp.cbesb.ui.util.AuthenticationUtil;
014:
015: /**
016: * @author LPS
017: *
018: */
019: public class SystemFileBrowse {
020:
021: private static String lastSystemLocation;
022: private String initialPath;
023: private String[] filerNames;
024: private String[] filerExtensions;
025:
026: /**
027: * @param initialPath
028: * @param filerNames
029: * @param filerExtensions
030: */
031: public SystemFileBrowse(String initialPath, String[] filerNames,
032: String[] filerExtensions) {
033: super ();
034: this .initialPath = initialPath;
035: this .filerNames = filerNames;
036: this .filerExtensions = filerExtensions;
037: }
038:
039: public void systemFileBrowseToTextBox(Text destination) {
040: if (destination == null)
041: return;
042: FileDialog dialog = new FileDialog(destination.getShell(),
043: SWT.OPEN);
044: if (lastSystemLocation != null) {
045: dialog.setFilterPath(lastSystemLocation);
046: } else {
047: dialog.setFilterPath(getPro_path());
048: }
049: dialog.setFilterNames(this .filerNames);
050: dialog.setFilterExtensions(this .filerExtensions);
051: String result = dialog.open();
052: if (result == null) {
053: //destination.setText("");
054: result = "";
055: }
056: destination.setText(result);
057: if (result.lastIndexOf("\\") != -1) {
058: lastSystemLocation = result.substring(0, result
059: .lastIndexOf("\\"));
060: } else {
061: lastSystemLocation = null;
062: }
063: }
064:
065: private String getPro_path() {
066: return initialPath;
067: }
068:
069: /**
070: *
071: * @param destination - the textbox to contain the selected filename
072: */
073: public void systemMultipleFileBrowseToTextBox(Text destination) {
074: if (destination == null)
075: return;
076: FileDialog dialog = new FileDialog(destination.getShell(),
077: SWT.MULTI);
078: if (lastSystemLocation != null) {
079: dialog.setFilterPath(lastSystemLocation);
080: } else {
081: dialog.setFilterPath(getPro_path());
082: }
083: dialog.setFilterNames(this .filerNames);
084: dialog.setFilterExtensions(this .filerExtensions);
085:
086: String result = dialog.open();
087: if (result == null) {
088: result = "";
089: } else {
090: //File file = new File(result);
091: Path path = new Path(result);
092: path = (Path) path.removeFileExtension();
093: path = (Path) path.removeLastSegments(1);
094:
095: String[] fileNames = dialog.getFileNames();
096: String tempResult = " ";
097: if (fileNames != null)//just another extra caution
098: {
099: for (int i = 0; i < fileNames.length; i++) {
100: tempResult += path.toString()
101: + "/"
102: + fileNames[i]
103: + AuthenticationUtil.MULTIPLE_FILE_SEPARATOR
104: + "\n ";
105: }
106: }
107: result = tempResult;
108: }
109:
110: destination.setText(result);
111:
112: if (result.lastIndexOf("\\") != -1) {
113: lastSystemLocation = result.substring(0, result
114: .lastIndexOf("\\"));
115: } else {
116: lastSystemLocation = null;
117: }
118: }
119:
120: public void systemFileBrowseToTextBox(Text destination, String p,
121: String fileName) {
122: if (destination == null)
123: return;
124: FileDialog dialog = new FileDialog(destination.getShell(),
125: SWT.OPEN);
126: if (lastSystemLocation != null) {
127: dialog.setFilterPath(lastSystemLocation);
128: } else {
129: dialog.setFilterPath(p);
130: }
131: dialog.setFileName(fileName);
132: dialog.setFilterNames(this .filerNames);
133: dialog.setFilterExtensions(this .filerExtensions);
134: String result = dialog.open();
135:
136: if (result == null) {
137: //destination.setText("");
138: result = "";
139: }
140: destination.setText(result);
141: if (result.lastIndexOf("\\") != -1) {
142: lastSystemLocation = result.substring(0, result
143: .lastIndexOf("\\"));
144: } else {
145: lastSystemLocation = null;
146: }
147:
148: }
149:
150: public String systemFilebrowseToString(Shell shell) {
151: String destination = "";
152: FileDialog dialog = new FileDialog(shell, SWT.MULTI);
153: if (lastSystemLocation != null) {
154: dialog.setFilterPath(lastSystemLocation);
155: } else {
156: dialog.setFilterPath(getPro_path());
157: }
158: dialog.setFilterNames(this .filerNames);
159: dialog.setFilterExtensions(this .filerExtensions);
160:
161: String result = dialog.open();
162: if (result == null) {
163: result = "";
164: } else {
165: //File file = new File(result);
166: Path path = new Path(result);
167: path = (Path) path.removeFileExtension();
168: path = (Path) path.removeLastSegments(1);
169:
170: String[] fileNames = dialog.getFileNames();
171: String tempResult = "";
172: if (fileNames != null)//just another extra caution
173: {
174: for (int i = 0; i < fileNames.length; i++) {
175: tempResult += path.toOSString() + File.separator
176: + fileNames[i];
177: }
178: }
179: result = tempResult;
180: }
181:
182: destination = result;
183:
184: if (result.lastIndexOf("\\") != -1) {
185: lastSystemLocation = result.substring(0, result
186: .lastIndexOf("\\"));
187: } else {
188: lastSystemLocation = null;
189: }
190: return destination;
191: }
192: }
|