001: /*
002: * ChainBuilder ESB
003: * Visual Enterprise Integration
004: *
005: * Copyright (C) 2006 Bostech Corporation
006: *
007: * This program is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU General Public License as published by the
009: * Free Software Foundation; either version 2 of the License, or (at your option)
010: * 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 MERCHANTABILITY
014: * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
015: * for more details.
016: *
017: * You should have received a copy of the GNU General Public License along with
018: * this program; if not, write to the Free Software Foundation, Inc.,
019: * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
020: *
021: *
022: * $Id$
023: */
024: package com.bostechcorp.cbesb.ui.util.browser;
025:
026: import java.io.File;
027: import java.util.List;
028:
029: import org.eclipse.core.resources.IProject;
030: import org.eclipse.core.resources.ResourcesPlugin;
031: import org.eclipse.swt.SWT;
032: import org.eclipse.swt.custom.SashForm;
033: import org.eclipse.swt.events.MouseAdapter;
034: import org.eclipse.swt.events.MouseEvent;
035: import org.eclipse.swt.events.SelectionAdapter;
036: import org.eclipse.swt.events.SelectionEvent;
037: import org.eclipse.swt.layout.FillLayout;
038: import org.eclipse.swt.layout.FormAttachment;
039: import org.eclipse.swt.layout.FormData;
040: import org.eclipse.swt.layout.FormLayout;
041: import org.eclipse.swt.widgets.Button;
042: import org.eclipse.swt.widgets.Composite;
043: import org.eclipse.swt.widgets.Dialog;
044: import org.eclipse.swt.widgets.Display;
045: import org.eclipse.swt.widgets.Label;
046: import org.eclipse.swt.widgets.Shell;
047: import org.eclipse.swt.widgets.Table;
048: import org.eclipse.swt.widgets.TableItem;
049: import org.eclipse.swt.widgets.Text;
050: import org.eclipse.swt.widgets.Tree;
051: import org.eclipse.swt.widgets.TreeItem;
052: import org.eclipse.ui.plugin.AbstractUIPlugin;
053:
054: import com.bostechcorp.cbesb.common.i18n.I18N;
055: import com.bostechcorp.cbesb.common.i18n.Messages;
056: import com.bostechcorp.cbesb.common.util.macro.MacroUtil;
057: import com.bostechcorp.cbesb.ui.util.ProjectUtil;
058: import com.bostechcorp.cbesb.ui.util.resource.ESBResourceManager;
059: import com.bostechcorp.cbesb.ui.util.resource.SWTResourceManager;
060:
061: public class BaseFileBrowser extends Dialog {
062:
063: protected int result;
064:
065: protected Text tableText;
066:
067: protected Shell shell;
068:
069: protected Table fileTree;
070:
071: protected Tree folderTree;
072:
073: protected List list = null;
074:
075: protected Composite composite_2 = null;
076:
077: protected Button okButton;
078:
079: protected boolean flag = true;
080:
081: protected String prefix = "";
082:
083: protected String folder;
084:
085: protected String title = "";
086:
087: protected AbstractUIPlugin abstractUIPlugin;
088:
089: private String projectName = "";
090:
091: /**
092: * Create the dialog
093: *
094: * @param parent
095: * @param style
096: * @param page
097: */
098: public BaseFileBrowser(AbstractUIPlugin abstractUIPlugin,
099: Shell parent, Text tableText, String projectName,
100: String prefix, String folder, String title) {
101: super (parent, SWT.CLOSE | SWT.Resize);
102: this .tableText = tableText;
103: this .prefix = prefix;
104: this .folder = folder;
105: this .list = ProjectUtil.getRelatedProject(projectName);
106: this .title = title;
107: this .projectName = projectName;
108: this .abstractUIPlugin = abstractUIPlugin;
109: }
110:
111: /**
112: * @param parent
113: * @param title
114: */
115: public BaseFileBrowser(Shell parent, String title) {
116: super (parent, SWT.CLOSE | SWT.Resize);
117: this .title = title;
118: }
119:
120: /**
121: * Open the dialog
122: *
123: * @return the result
124: */
125: public int open() {
126: createContents();
127: shell.open();
128: shell.layout();
129: Display display = getParent().getDisplay();
130: while (!shell.isDisposed()) {
131: if (!display.readAndDispatch())
132: display.sleep();
133: }
134: return result;
135: }
136:
137: /**
138: * Create contents of the dialog
139: */
140: protected void createContents() {
141: shell = new Shell(getParent(), SWT.RESIZE | SWT.PRIMARY_MODAL
142: | SWT.TITLE | SWT.CLOSE);
143: shell.setText(I18N.getString(Messages.IDE_FILE_SEL));
144: shell.setLayout(new FillLayout());
145: shell.setSize(513, 474);
146: shell.setMaximized(false);
147: shell.setVisible(true);
148:
149: final Composite composite = new Composite(shell, SWT.NONE);
150: composite.setLayout(new FormLayout());
151:
152: SashForm sashForm;
153: sashForm = new SashForm(composite, SWT.NONE);
154: final FormData formData = new FormData();
155: formData.bottom = new FormAttachment(100, -37);
156: formData.right = new FormAttachment(100, -5);
157: formData.top = new FormAttachment(0, 45);
158: formData.left = new FormAttachment(0, 0);
159: sashForm.setLayoutData(formData);
160:
161: final Composite composite_1 = new Composite(sashForm, SWT.NONE);
162: composite_1.setLayout(new FillLayout());
163:
164: folderTree = new Tree(composite_1, SWT.BORDER);
165: folderTree.addSelectionListener(new SelectionAdapter() {
166: @Override
167: public void widgetSelected(SelectionEvent e) {
168: folderTreeListenser();
169: }
170: });
171:
172: composite_2 = new Composite(sashForm, SWT.NONE);
173: composite_2.setLayout(new FillLayout());
174:
175: fileTree = new Table(composite_2, SWT.BORDER);
176: fileTree.addMouseListener(new MouseAdapter() {
177: public void mouseDoubleClick(MouseEvent e) {
178: if (okButton.getEnabled())
179: okButtonListener();
180: }
181: });
182: fileTree.addSelectionListener(new SelectionAdapter() {
183: public void widgetSelected(final SelectionEvent e) {
184: fileTreeListenser();
185: }
186: });
187: sashForm.setWeights(new int[] { 268, 229 });
188:
189: okButton = new Button(composite, SWT.NONE);
190: okButton.addSelectionListener(new SelectionAdapter() {
191: public void widgetSelected(final SelectionEvent e) {
192: okButtonListener();
193: }
194: });
195: final FormData formData_2 = new FormData();
196: formData_2.bottom = new FormAttachment(100, -10);
197: formData_2.top = new FormAttachment(100, -32);
198: formData_2.right = new FormAttachment(100, -120);
199: formData_2.left = new FormAttachment(100, -175);
200: okButton.setLayoutData(formData_2);
201: okButton.setText(I18N.getString(Messages.OK));
202:
203: Button cancelButton;
204: cancelButton = new Button(composite, SWT.NONE);
205: cancelButton.addSelectionListener(new SelectionAdapter() {
206: public void widgetSelected(final SelectionEvent e) {
207: cancelButtonListener();
208: shell.dispose();
209:
210: }
211:
212: });
213: final FormData formData_3 = new FormData();
214: formData_3.bottom = new FormAttachment(100, -10);
215: formData_3.top = new FormAttachment(100, -32);
216: formData_3.right = new FormAttachment(okButton, 85, SWT.RIGHT);
217: formData_3.left = new FormAttachment(okButton, 36, SWT.DEFAULT);
218: cancelButton.setLayoutData(formData_3);
219: cancelButton.setText(I18N.getString(Messages.CANCEL));
220:
221: Label selectAFormatLabel;
222: selectAFormatLabel = new Label(composite, SWT.NONE);
223: selectAFormatLabel.setFont(SWTResourceManager.getFont("", 10,
224: SWT.BOLD));
225: final FormData formData_4 = new FormData();
226: formData_4.bottom = new FormAttachment(0, 40);
227: formData_4.right = new FormAttachment(100, -5);
228: formData_4.top = new FormAttachment(0, 5);
229: formData_4.left = new FormAttachment(0, 5);
230: selectAFormatLabel.setLayoutData(formData_4);
231: selectAFormatLabel.setText(title);
232: checkTreeTable();
233: folderTreeListenser();
234: okButton.setEnabled(false);
235:
236: //
237: }
238:
239: @Override
240: protected void checkSubclass() {
241: // Disable the check that prevents subclassing of SWT components
242: }
243:
244: protected void okButtonListener() {
245: IProject project = ResourcesPlugin.getWorkspace().getRoot()
246: .getProject(projectName);
247: String currentProject = project.getLocation().toString();
248: String selectLocation = fileTree.getSelection()[0].getData()
249: .toString().replace("\\", "/");
250: String currentWorkspace = project.getWorkspace().getRoot()
251: .getLocation().toString();
252: String displayedText = "";
253:
254: if (selectLocation.contains(currentProject)) {
255: displayedText = selectLocation.replace(
256: currentProject + "/", projectName + "::");
257:
258: } else {
259: String esbProjectName = selectLocation.split(currentProject
260: .substring(1, currentProject.lastIndexOf("/") + 1))[1]
261: .split("/")[0];
262:
263: displayedText = selectLocation.replace(currentWorkspace
264: + "/" + esbProjectName + "/", projectName + "::"
265: + esbProjectName + "::");
266:
267: }
268: displayedText = MacroUtil.changeToMacroPath(displayedText,
269: projectName);
270:
271: tableText.setText(displayedText);
272: shell.dispose();
273: }
274:
275: protected void fileTreeListenser() {
276: if (fileTree.getSelection() != null
277: && fileTree.getSelection().length > 0) {
278: if (fileTree.getSelection()[0].getData() instanceof File) {
279: if (((File) fileTree.getSelection()[0].getData())
280: .isFile()
281: && ((fileTree.getSelection()[0].getText()
282: .endsWith(prefix) || prefix
283: .equals(".*")) && folderTree
284: .getSelection()[0].getParentItem() != null)) {
285: okButton.setEnabled(true);
286: } else {
287: okButton.setEnabled(false);
288: }
289: } else if (fileTree.getSelection()[0].getData() instanceof List) {
290: okButton.setEnabled(true);
291: }
292:
293: }
294:
295: }
296:
297: public void folderTreeListenser() {
298:
299: okButton.setEnabled(false);
300: if (folderTree.getSelection() != null
301: && folderTree.getSelection().length > 0) {
302: fileTree.dispose();
303: fileTree = new Table(composite_2, SWT.BORDER);
304: fileTree.layout();
305: composite_2.layout();
306: fileTree.addMouseListener(new MouseAdapter() {
307: public void mouseDoubleClick(MouseEvent e) {
308: if (okButton.getEnabled())
309: okButtonListener();
310: }
311: });
312: fileTree.addSelectionListener(new SelectionAdapter() {
313: public void widgetSelected(final SelectionEvent e) {
314: fileTreeListenser();
315: }
316: });
317: TreeItem item = folderTree.getSelection()[0];
318: File file = (File) item.getData();
319: if (file == null || !file.exists())
320: return;
321: for (File subFile : file.listFiles()) {
322: if (subFile.isFile()) {
323: TableItem subItem = new TableItem(fileTree,
324: SWT.NONE);
325: subItem.setText(subFile.getName());
326: setFileImage(subItem);
327: subItem.setData(subFile);
328: }
329: }
330: }
331: }
332:
333: public void checkTreeTable() {
334: if (list != null) {
335: for (int i = 0; i < list.size(); i++) {
336: IProject project = (IProject) list.get(i);
337: TreeItem rootItem = new TreeItem(folderTree, SWT.None);
338: rootItem.setImage(ESBResourceManager.getPluginImage(
339: abstractUIPlugin, "icons/dialog/FOLDER.GIF"));
340: rootItem.setText(project.getName());
341: if (project.getLocation() != null) {
342: rootItem.setData(project.getLocation().toFile());
343: String firstFolder = folder;
344: String secondFolder = "";
345: if (folder.indexOf("/") != -1) {
346: firstFolder = folder.split("/")[0];
347: secondFolder = folder.split("/")[1];
348: }
349: TreeItem newItem = new TreeItem(rootItem, SWT.NONE);
350: newItem.setText("src");
351: newItem.setImage(ESBResourceManager
352: .getPluginImage(abstractUIPlugin,
353: "icons/dialog/FOLDER.GIF"));
354: newItem.setData(project.getFile("src")
355: .getLocation().toFile());
356: File file = project.getFile("src/" + firstFolder)
357: .getLocation().toFile();
358: TreeItem item_1 = new TreeItem(newItem, SWT.NONE);
359: item_1.setText(file.getName());
360: item_1.setImage(ESBResourceManager
361: .getPluginImage(abstractUIPlugin,
362: "icons/dialog/FOLDER.GIF"));
363: item_1.setData(file);
364: TreeItem item = item_1;
365: if (folder.indexOf("/") != -1) {
366: if (!secondFolder.equals("")) {
367: File file_2 = project.getFile(
368: "src/" + firstFolder + "/"
369: + secondFolder)
370: .getLocation().toFile();
371: TreeItem item_2 = new TreeItem(item_1,
372: SWT.NONE);
373: item_2.setText(file_2.getName());
374: item_2.setImage(ESBResourceManager
375: .getPluginImage(abstractUIPlugin,
376: "icons/dialog/FOLDER.GIF"));
377: item_2.setData(file_2);
378: item = item_2;
379: file = file_2;
380:
381: }
382: }
383: loopFolder(file, item);
384: if (secondFolder.equals("")) {
385: folderTree.setSelection(folderTree.getItem(0)
386: .getItem(0).getItem(0));
387:
388: } else {
389: folderTree.setSelection(folderTree.getItem(0)
390: .getItem(0).getItem(0).getItem(0));
391: }
392: }
393: }
394: cleanFolder(folderTree);
395:
396: }
397: }
398:
399: public void loopFolder(File file, TreeItem rootItem) {
400: if (!file.exists())
401: return;
402: for (File subFile : file.getAbsoluteFile().listFiles()) {
403: TreeItem item = null;
404: if (subFile.isDirectory()) {
405: item = new TreeItem(rootItem, SWT.NONE);
406: item.setText(subFile.getName());
407: item.setImage(ESBResourceManager.getPluginImage(
408: abstractUIPlugin, "icons/dialog/FOLDER.GIF"));
409: item.setData(subFile);
410: }
411: if (subFile.isFile()
412: && !subFile.getAbsolutePath().endsWith(".zip")) {
413: TableItem subItem = new TableItem(fileTree, SWT.NONE);
414: subItem.setText(subFile.getName());
415: setFileImage(subItem);
416: subItem.setData(subFile);
417: }
418: if (subFile.listFiles() != null) {
419: loopFolder(subFile, item);
420: }
421: }
422: }
423:
424: // public void handleZip(File file,TreeItem root) throws ZipException,
425: // IOException{
426:
427: public void setFileImage(TableItem item) {
428: if (item.getText().endsWith(prefix))
429: item.setImage(ESBResourceManager.getPluginImage(
430: abstractUIPlugin, "icons/dialog/sample.gif"));
431: else
432: item.setImage(ESBResourceManager.getPluginImage(
433: abstractUIPlugin, "icons/dialog/file.gif"));
434:
435: }
436:
437: //
438:
439: public void expandTreeNode(TreeItem item) {
440: if (item.getItemCount() > 0) {
441: item.setExpanded(true);
442: for (int i = 0; i < item.getItemCount(); i++) {
443: TreeItem subItem = item.getItem(i);
444: expandTreeNode(subItem);
445: }
446: }
447: }
448:
449: /**
450: * clean the tree which has invalid items ,such as .svn
451: *
452: * @param tree
453: */
454: public void cleanFolder(Tree tree) {
455: for (TreeItem item : tree.getItems()) {
456: item.setExpanded(true);
457: if (item.getText().startsWith(".")) {
458: item.dispose();
459: continue;
460: }
461: if (item.getItems().length != 0) {
462: cleanFolder(item);
463: }
464: }
465: }
466:
467: protected void cancelButtonListener() {
468: shell.dispose();
469: }
470:
471: /**
472: * clean the rootItem which has invalid items ,such as .svn
473: *
474: * @param rootItem
475: */
476: public void cleanFolder(TreeItem rootItem) {
477: for (TreeItem item : rootItem.getItems()) {
478: item.setExpanded(true);
479: if (item.getText().startsWith(".")) {
480: item.dispose();
481: continue;
482: }
483: if (item.getItems().length != 0) {
484: cleanFolder(item);
485: }
486: }
487: }
488: }
|