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.componentflow.componentFlowEditor.diagram.wizards;
025:
026: import java.io.File;
027: import java.io.IOException;
028: import java.io.InputStream;
029: import java.util.ArrayList;
030: import java.util.Enumeration;
031: import java.util.List;
032: import java.util.zip.ZipEntry;
033: import java.util.zip.ZipException;
034: import java.util.zip.ZipFile;
035:
036: import org.eclipse.core.resources.IProject;
037: import org.eclipse.swt.SWT;
038: import org.eclipse.swt.widgets.Display;
039: import org.eclipse.swt.widgets.Shell;
040: import org.eclipse.swt.widgets.TableItem;
041: import org.eclipse.swt.widgets.Text;
042: import org.eclipse.swt.widgets.Tree;
043: import org.eclipse.swt.widgets.TreeItem;
044:
045: import com.bostechcorp.cbesb.ui.componentflow.componentFlowEditor.diagram.part.ComponentFlowEditorDiagramEditorPlugin;
046: import com.bostechcorp.cbesb.ui.util.browser.BaseFileBrowser;
047: import com.bostechcorp.cbesb.ui.util.resource.ESBResourceManager;
048:
049: public class PaserFileBrowser extends BaseFileBrowser {
050: private Object[] fileList = new Object[1];
051:
052: /**
053: * Create the dialog
054: *
055: * @param parent
056: * @param style
057: * @param page
058: */
059: public PaserFileBrowser(Shell parent, Text tableText,
060: String projectName, String prefix, String folder,
061: String title) {
062: super (ComponentFlowEditorDiagramEditorPlugin.getInstance(),
063: parent, tableText, projectName, prefix, folder, title);
064: }
065:
066: /**
067: * Create the dialog
068: *
069: * @param parent
070: */
071:
072: /**
073: * Open the dialog
074: *
075: * @return the result
076: */
077:
078: public Object[] result() {
079: createContents();
080: shell.open();
081: shell.layout();
082: Display display = getParent().getDisplay();
083: while (!shell.isDisposed()) {
084: if (!display.readAndDispatch())
085: display.sleep();
086: }
087: return fileList;
088: }
089:
090: public int open() {
091: createContents();
092: shell.open();
093: shell.layout();
094: Display display = getParent().getDisplay();
095: while (!shell.isDisposed()) {
096: if (!display.readAndDispatch())
097: display.sleep();
098: }
099: return result;
100: }
101:
102: /**
103: * Create contents of the dialog
104: */
105: protected void createContents() {
106: super .createContents();
107: }
108:
109: @Override
110: protected void checkSubclass() {
111: // Disable the check that prevents subclassing of SWT components
112: }
113:
114: public void checkTreeTable() {
115: if (list != null) {
116: for (int i = 0; i < list.size(); i++) {
117: IProject project = (IProject) list.get(i);
118: TreeItem rootItem = new TreeItem(folderTree, SWT.None);
119: rootItem.setImage(ESBResourceManager.getPluginImage(
120: ComponentFlowEditorDiagramEditorPlugin
121: .getInstance(),
122: "icons/dialog/FOLDER.GIF"));
123: rootItem.setText(project.getName());
124: if (project.getLocation() != null) {
125: rootItem.setData(project.getLocation().toFile());
126: loopFolder(project.getLocation().toFile(), rootItem);
127: expandNode();
128: }
129: }
130: cleanFolder(folderTree);
131: }
132: }
133:
134: public void expandNode() {
135: Tree tree = folderTree;
136:
137: for (int k = 0; k < tree.getItemCount(); k++) {
138: TreeItem rootItem = tree.getItem(k);
139: rootItem.setExpanded(true);
140: for (int i = 0; i < rootItem.getItemCount(); i++) {
141: rootItem.getItem(i).setExpanded(true);
142: if (rootItem.getItem(i).getText().equals("src")) {
143: for (int j = 0; j < rootItem.getItem(i)
144: .getItemCount(); j++) {
145: if (rootItem.getItem(i).getItem(j).getText()
146: .equals(folder)) {
147: expandTreeNode(rootItem.getItem(i).getItem(
148: j));
149: if (flag) {
150: tree.setSelection(rootItem.getItem(i)
151: .getItem(j));
152: File file = (File) rootItem.getItem(i)
153: .getItem(j).getData();
154: for (File subFile : file.listFiles()) {
155: if (subFile.isFile()
156: && !subFile
157: .getAbsolutePath()
158: .endsWith(".zip")
159: && subFile.getName()
160: .toLowerCase()
161: .endsWith(".mdl")) {
162: TableItem subItem = new TableItem(
163: fileTree, SWT.NONE);
164: subItem.setText(subFile
165: .getName());
166: setFileImage(subItem);
167: subItem.setData(subFile);
168: }
169: }
170: flag = false;
171: }
172: } else {
173: rootItem.getItem(i).getItem(j).dispose();
174: j--;
175: }
176: }
177: } else {
178: rootItem.getItem(i).dispose();
179: i--;
180: }
181: }
182: }
183: }
184:
185: public void loopFolder(File file, TreeItem rootItem) {
186: for (File subFile : file.getAbsoluteFile().listFiles()) {
187: TreeItem item = null;
188: if (subFile.isDirectory()) {
189: if (subFile.getName().startsWith(".")) {
190: continue;
191: }
192: if (!subFile.getName().equals("segments")) {
193: System.out.println(subFile.getName());
194: item = new TreeItem(rootItem, SWT.NONE);
195: item.setText(subFile.getName());
196: item.setImage(ESBResourceManager.getPluginImage(
197: ComponentFlowEditorDiagramEditorPlugin
198: .getInstance(),
199: "icons/dialog/FOLDER.GIF"));
200: item.setData(subFile);
201: }
202: }
203: if (subFile.getAbsolutePath().endsWith(".zip")) {
204: try {
205: handleZip(subFile, rootItem);
206: } catch (ZipException e) {
207:
208: e.printStackTrace();
209: } catch (IOException e) {
210:
211: e.printStackTrace();
212: }
213: }
214: if (subFile.listFiles() != null) {
215: loopFolder(subFile, item);
216: }
217: }
218: }
219:
220: public void handleZip(File file, TreeItem root)
221: throws ZipException, IOException {
222: ZipFile zipFile = new ZipFile(file);
223: TreeItem item = new TreeItem(root, SWT.NONE);
224: item.setText(zipFile.getName().substring(
225: zipFile.getName().lastIndexOf("\\") + 1));
226: item.setImage(ESBResourceManager.getPluginImage(
227: ComponentFlowEditorDiagramEditorPlugin.getInstance(),
228: "icons/dialog/zip.ico"));
229: item.setData(file);
230: zipFile.close();
231:
232: }
233:
234: public void folderTreeListenser() {
235:
236: okButton.setEnabled(false);
237: if (folderTree.getSelection() != null
238: && folderTree.getSelection().length > 0) {
239: for (TableItem item : fileTree.getItems()) {
240: item.dispose();
241: }
242: TreeItem item = folderTree.getSelection()[0];
243: if (item.getText().endsWith(".zip")) {
244: try {
245: ZipFile zipFile = new ZipFile((File) item.getData());
246: for (Enumeration entries = zipFile.entries(); entries
247: .hasMoreElements();) {
248: ZipEntry entry = (ZipEntry) entries
249: .nextElement();
250: InputStream inStream = zipFile
251: .getInputStream(entry);
252: // String fileName =
253: // zipFile.getName().concat("/"+entry.getName());
254: String name = zipFile.getName().concat(
255: "/" + entry.getName());
256: makeZipItem(name, inStream, zipFile.getName());
257: }
258:
259: } catch (ZipException e1) {
260:
261: e1.printStackTrace();
262: } catch (IOException e1) {
263:
264: e1.printStackTrace();
265: }
266:
267: } else {
268: File file = (File) item.getData();
269: for (File subFile : file.listFiles()) {
270: if (subFile.isFile()
271: && subFile.getName().toLowerCase()
272: .endsWith(".mdl")) {
273: TableItem subItem = new TableItem(fileTree,
274: SWT.NONE);
275: subItem.setText(subFile.getName());
276: setFileImage(subItem);
277: subItem.setData(subFile);
278:
279: }
280: }
281: }
282: }
283:
284: }
285:
286: public void fileTreeListenser() {
287: if (fileTree.getSelection() != null
288: && fileTree.getSelection().length > 0) {
289: if (fileTree.getSelection()[0].getData() instanceof File) {
290: if (((File) fileTree.getSelection()[0].getData())
291: .isFile()
292: && (fileTree.getSelection()[0].getText()
293: .endsWith(prefix) && folderTree
294: .getSelection()[0].getParentItem() != null)) {
295: okButton.setEnabled(true);
296: fileList[0] = (File) fileTree.getSelection()[0]
297: .getData();
298: } else {
299: okButton.setEnabled(false);
300: }
301: } else if (fileTree.getSelection()[0].getData() instanceof List) {
302: okButton.setEnabled(true);
303: fileList[0] = (List) fileTree.getSelection()[0]
304: .getData();
305: }
306:
307: }
308: }
309:
310: // public void handleZip(File file,TreeItem root) throws ZipException,
311: // IOException{
312:
313: private void makeZipItem(String name, InputStream inStream,
314: String zip) {
315: if (name.indexOf("/messages/") != -1) {
316: TableItem subItem = new TableItem(fileTree, SWT.NONE);
317: subItem.setText(name.substring(name.lastIndexOf("/") + 1));
318: setFileImage(subItem);
319: List list = new ArrayList();
320: list.add(name);
321: list.add(inStream);
322: list.add(zip);
323: subItem.setData(list);
324: }
325:
326: }
327:
328: public void setFileImage(TableItem item) {
329: if (item.getText().endsWith(prefix))
330: item.setImage(ESBResourceManager.getPluginImage(
331: ComponentFlowEditorDiagramEditorPlugin
332: .getInstance(), "icons/dialog/sample.gif"));
333: else
334: item.setImage(ESBResourceManager.getPluginImage(
335: ComponentFlowEditorDiagramEditorPlugin
336: .getInstance(), "icons/dialog/file.gif"));
337:
338: }
339:
340: //
341:
342: public void expandTreeNode(TreeItem item) {
343: if (item.getItemCount() > 0) {
344: item.setExpanded(true);
345: for (int i = 0; i < item.getItemCount(); i++) {
346: TreeItem subItem = item.getItem(i);
347: expandTreeNode(subItem);
348: }
349: }
350: }
351:
352: /**
353: * clean the tree which has invalid items ,such as .svn
354: *
355: * @param tree
356: */
357: public void cleanFolder(Tree tree) {
358: for (TreeItem item : tree.getItems()) {
359: if (item.getText().startsWith(".")) {
360: item.dispose();
361: continue;
362: }
363: if (item.getItems().length != 0) {
364: cleanFolder(item);
365: }
366: }
367: }
368:
369: @Override
370: protected void cancelButtonListener() {
371:
372: // for(int i=0; i<fileList.length;i++){
373: fileList = null;
374: // }
375: super .cancelButtonListener();
376: }
377:
378: /**
379: * clean the rootItem which has invalid items ,such as .svn
380: *
381: * @param rootItem
382: */
383: public void cleanFolder(TreeItem rootItem) {
384: for (TreeItem item : rootItem.getItems()) {
385: if (item.getText().startsWith(".")) {
386: item.dispose();
387: continue;
388: }
389: if (item.getItems().length != 0) {
390: cleanFolder(item);
391: }
392: }
393: }
394: }
|