001: /*
002: * ChainBuilder ESB
003: * Visual Enterprise Integration
004: *
005: * Copyright (C) 2007 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:
025: package com.bostechcorp.cbesb.ui.ide.wizards;
026:
027: import java.io.File;
028: import java.util.Iterator;
029:
030: import org.eclipse.core.resources.IFile;
031: import org.eclipse.core.resources.IFolder;
032: import org.eclipse.core.resources.IProject;
033: import org.eclipse.core.resources.ResourcesPlugin;
034: import org.eclipse.core.runtime.IPath;
035: import org.eclipse.core.runtime.Path;
036: import org.eclipse.jface.viewers.IStructuredSelection;
037: import org.eclipse.jface.wizard.WizardPage;
038: import org.eclipse.swt.SWT;
039: import org.eclipse.swt.events.ModifyEvent;
040: import org.eclipse.swt.events.ModifyListener;
041: import org.eclipse.swt.events.SelectionAdapter;
042: import org.eclipse.swt.events.SelectionEvent;
043: import org.eclipse.swt.layout.GridData;
044: import org.eclipse.swt.layout.GridLayout;
045: import org.eclipse.swt.widgets.Button;
046: import org.eclipse.swt.widgets.Composite;
047: import org.eclipse.swt.widgets.DirectoryDialog;
048: import org.eclipse.swt.widgets.FileDialog;
049: import org.eclipse.swt.widgets.Label;
050: import org.eclipse.swt.widgets.Text;
051:
052: import com.bostechcorp.cbesb.common.i18n.I18N;
053: import com.bostechcorp.cbesb.common.i18n.Messages;
054: import com.bostechcorp.cbesb.common.util.macro.MacroUtil;
055: import com.bostechcorp.cbesb.ui.ide.Activator;
056: import com.bostechcorp.cbesb.ui.util.browser.BaseFileBrowser;
057:
058: /**
059: * The first page of the Extract Strings wizard displays Source File and
060: * Destination File text fields, each with a browse button to the right.
061: */
062: public class WSDLLocateWizardPage extends WizardPage {
063: private Text targetFileField;
064: private Text sourceFileField;
065:
066: private IPath initialSourcePath;
067:
068: public String pro_path = null;
069:
070: private String file_path = null;
071:
072: public String WSDL = "src/wsdl";
073:
074: public String format = "src/formats";
075:
076: private Label label_1;
077:
078: private Button button;
079:
080: private Button button_1;
081:
082: private String separator = "/";
083:
084: public String curr_pro_name = null;
085:
086: public WSDLLocateWizardPage() {
087: super (I18N.getString(Messages.IDE_WSDL_POSITION));
088: this .setTitle(I18N.getString(Messages.IDE_WSDL_POSITION));// "Create
089: // New X12
090: // Variant");
091: // //$NON-NLS-1$
092: this .setDescription(I18N
093: .getString(Messages.IDE_WSDL_POSITION_DES));// "Create
094: // a
095: // new
096: // x12
097: // variant");
098: // //$NON-NLS-1$
099: setPageComplete(false);
100:
101: }
102:
103: /**
104: * Creates the top level control for this dialog page under the given parent
105: * composite, then calls <code>setControl</code> so that the created
106: * control can be accessed via <code>getControl</code>
107: *
108: * @param parent
109: * the parent composite
110: */
111: public void createControl(Composite parent) {
112: Composite container = new Composite(parent, SWT.NULL);
113: final GridLayout gridLayout = new GridLayout();
114: gridLayout.marginTop = 10;
115: gridLayout.numColumns = 3;
116: container.setLayout(gridLayout);
117: setControl(container);
118:
119: // final Label label = new Label(container, SWT.NONE);
120: final GridData gridData = new GridData();
121: gridData.horizontalSpan = 3;
122: /*
123: * label.setLayoutData(gridData); label.setText( "Select the plugin.xml
124: * file " + "from which strings will be extracted.");
125: */
126:
127: label_1 = new Label(container, SWT.NONE);
128: final GridData gridData_1 = new GridData(SWT.FILL, SWT.CENTER,
129: false, false);
130: gridData_1.widthHint = 60;
131: label_1.setLayoutData(gridData_1);
132: label_1.setText(I18N.getString(Messages.IDE_WSDL_LABEL));
133:
134: sourceFileField = new Text(container, SWT.BORDER);
135: sourceFileField.addModifyListener(new ModifyListener() {
136: public void modifyText(ModifyEvent e) {
137: updatePageComplete();
138: }
139: });
140: final GridData gridData_2 = new GridData(SWT.FILL, SWT.CENTER,
141: true, false);
142: gridData_2.widthHint = 300;
143: sourceFileField.setLayoutData(gridData_2);
144:
145: button = new Button(container, SWT.NONE);
146: final GridData gridData_3 = new GridData(SWT.FILL, SWT.CENTER,
147: false, false);
148: gridData_3.widthHint = 78;
149: button.setLayoutData(gridData_3);
150: button.addSelectionListener(new SelectionAdapter() {
151: public void widgetSelected(SelectionEvent e) {
152: browseForSourcePro();
153:
154: }
155: });
156: button.setText(I18N.getString(Messages.IDE_BROWSE_WORKSPACE));
157: new Label(container, SWT.NONE);
158: new Label(container, SWT.NONE);
159:
160: button_1 = new Button(container, SWT.NONE);
161: final GridData gridData_4 = new GridData(SWT.FILL, SWT.CENTER,
162: false, false);
163: gridData_4.widthHint = 127;
164: button_1.setLayoutData(gridData_4);
165: button_1.addSelectionListener(new SelectionAdapter() {
166: public void widgetSelected(SelectionEvent e) {
167: browseForSourceFile();
168: }
169: });
170: button_1.setText(I18N.getString(Messages.IDE_BROWSE_FILE));
171:
172: final Label label_1 = new Label(container, SWT.NONE);
173: final GridData gridData_11 = new GridData(SWT.FILL, SWT.CENTER,
174: false, false);
175: label_1.setLayoutData(gridData_11);
176: label_1.setText(I18N.getString(Messages.IDE_WSDL_DESTINA));
177:
178: targetFileField = new Text(container, SWT.BORDER);
179: targetFileField.addModifyListener(new ModifyListener() {
180: public void modifyText(ModifyEvent e) {
181: updatePageComplete();
182: if (isPageComplete()) {
183: ImportedWSDLWizard.wsdl_fin_des = targetFileField
184: .getText();
185: }
186: }
187: });
188: final GridData gridData2 = new GridData(SWT.FILL, SWT.CENTER,
189: true, false);
190: gridData.widthHint = 263;
191: targetFileField.setLayoutData(gridData2);
192:
193: final Button browseButton = new Button(container, SWT.NONE);
194: final GridData gridData_5 = new GridData(SWT.FILL, SWT.CENTER,
195: false, false);
196: gridData_5.widthHint = 51;
197: browseButton.setLayoutData(gridData_5);
198: browseButton.addSelectionListener(new SelectionAdapter() {
199: public void widgetSelected(SelectionEvent e) {
200: browseForDestinationFile();
201: }
202: });
203: browseButton.setText(I18N.getString(Messages.IDE_BROWSE));
204:
205: initContents();
206: }
207:
208: /**
209: * Open a file browser dialog to locate a destination file
210: */
211: protected void browseForDestinationFile() {
212: IPath path = browseTarget(getDestinationLocation(), false);
213: if (path == null)
214: return;
215: targetFileField.setText(path.toString());
216: }
217:
218: public void browseForSourcePro() {
219: BaseFileBrowser browser = new BaseFileBrowser(Activator
220: .getDefault(), button.getShell(), sourceFileField,
221: curr_pro_name, ".wsdl", "wsdl", I18N
222: .getString(Messages.SELECT_WSDL_FILE));
223: browser.open();
224: }
225:
226: /**
227: * Open a file dialog for selecting a file
228: *
229: * @param path
230: * the initially selected file
231: * @param mustExist
232: * <code>true</code> if the selected file must already exist,
233: * else <code>false</code>
234: * @return the newly selected file or <code>null</code>
235: */
236: private IPath browseTarget(IPath path, boolean mustExist) {
237: DirectoryDialog dialog = new DirectoryDialog(getShell(),
238: SWT.OPEN);
239: /*
240: * if (path != null) { if (path.segmentCount() > 1)
241: * dialog.setFilterPath(path.removeLastSegments(1) .toOSString()); if
242: * (path.segmentCount() > 0) dialog.setFileName(path.lastSegment()); }
243: */
244: dialog.setFilterPath(path.toString());
245: // dialog.setFilterExtensions(new String[] { "*.xsd", });
246: String result = dialog.open();
247: if (result == null)
248: return null;
249: return new Path(result);
250: }
251:
252: /**
253: * Called by the wizard to initialize the receiver's cached selection.
254: *
255: * @param selection
256: * the selection or <code>null</code> if none
257: */
258: public void init(IStructuredSelection selection) {
259: if (!(selection instanceof IStructuredSelection))
260: return;
261: Iterator iter = ((IStructuredSelection) selection).iterator();
262: while (iter.hasNext()) {
263: Object item = (Object) iter.next();
264: if (item instanceof IFolder) {
265: IFolder file = (IFolder) item;
266: initialSourcePath = file.getLocation();
267:
268: break;
269: }
270: if (item instanceof IFile) {
271: IFile file = (IFile) item;
272: initialSourcePath = file.getLocation();
273: break;
274: }
275: if (item instanceof IProject) {
276: IProject file = (IProject) item;
277: initialSourcePath = file.getLocation();
278: break;
279: }
280: }
281:
282: }
283:
284: /**
285: * Called by <code>createControl</code> to initialize the receiver's
286: * content based upon the cached selection provided by the wizard.
287: */
288: private void initContents() {
289: if (initialSourcePath == null)
290: return;
291: IPath rootLoc = ResourcesPlugin.getWorkspace().getRoot()
292: .getLocation();
293: IPath path = initialSourcePath;
294: /* System.out.println("the path is "+initialSourcePath.toString()); */
295: if (rootLoc.isPrefixOf(path))
296: path = path.setDevice(null).removeFirstSegments(
297: rootLoc.segmentCount());
298: // sourceFileField.setText(path.toString());
299: if (path.toString().contains(separator)) {
300: int index = path.toString().indexOf(separator);
301: curr_pro_name = path.toString().substring(0, index);
302: } else
303: curr_pro_name = path.toString();
304: pro_path = rootLoc.toString() + separator + curr_pro_name;
305: ImportedWSDLWizard.projectName = curr_pro_name;
306: ImportedWSDLWizard.wsdl_abs__format_path_des = pro_path
307: + separator + format;
308: // System.out.println("the format path is
309: // "+ImportedWSDLWizard.wsdl_abs__format_path_des);
310: /*
311: * System.out.println("the proje path is "+pro_path);
312: * sourceFileField.setText(curr_pro_name);
313: */
314: updatePageComplete();
315: setMessage(null);
316: setErrorMessage(null);
317: }
318:
319: /**
320: * Update the current page complete state based on the field content.
321: */
322: private void updatePageComplete() {
323: setPageComplete(false);
324: file_path = MacroUtil.resolveBuiltinMacro(sourceFileField
325: .getText(), curr_pro_name);
326: File file = new File(file_path);
327: boolean flag = false;
328: if (file.canRead()) {
329: ImportedWSDLWizard.wsdl_abs_path = file_path;
330: ImportedWSDLWizard.wsdl_abs_path_des = pro_path + separator
331: + WSDL;
332: flag = true;
333: setErrorMessage(null);
334: } else {
335: if (!file_path.startsWith(curr_pro_name)) {
336: setErrorMessage(I18N.getString(Messages.IDE_FILE_NOT));
337: setPageComplete(false);
338: return;
339: } else {
340: String left = file_path.substring(curr_pro_name
341: .length() + 2, file_path.length());
342: if (left.contains("::")) {
343: String project = left.substring(0, left
344: .indexOf("::"));
345:
346: file_path = ResourcesPlugin.getWorkspace()
347: .getRoot().getLocation().toString()
348: + separator
349: + project
350: + separator
351: + left.substring(left.indexOf("::") + 2,
352: left.length());
353:
354: } else {
355:
356: file_path = pro_path + separator + left;
357:
358: }
359: File file1 = new File(file_path);
360: if (file1.canRead()) {
361: ImportedWSDLWizard.wsdl_abs_path = file_path;
362: ImportedWSDLWizard.wsdl_abs_path_des = pro_path
363: + separator + WSDL;
364: setErrorMessage(null);
365: flag = true;
366: } else {
367: setPageComplete(false);
368:
369: setErrorMessage(I18N
370: .getString(Messages.IDE_FILE_NOT));
371: return;
372: }
373:
374: }
375: }
376:
377: String path = targetFileField.getText();
378: String folder_path = path;
379: File tarFile = new File(folder_path);
380: if (tarFile.isDirectory()) {
381: if (folder_path
382: .startsWith(ImportedWSDLWizard.wsdl_abs__format_path_des)) {
383: flag = true;
384: setMessage(null);
385: setErrorMessage(null);
386: ImportedWSDLWizard.wsdl_fin_des = path;
387: } else {
388: setErrorMessage(I18N
389: .getString(Messages.IDE_WRO_PATH_TIP));
390: }
391: } else
392: setErrorMessage(I18N.getString(Messages.IDE_WRO_PATH));
393:
394: setPageComplete(flag);
395: }
396:
397: /**
398: * Open a file browser dialog to locate a source file
399: */
400: protected void browseForSourceFile() {
401: IPath path = browse(getSourceLocation(), false);
402: if (path == null)
403: return;
404: sourceFileField.setText(path.toString());
405: }
406:
407: /**
408: * Open a file dialog for selecting a file
409: *
410: * @param path
411: * the initially selected file
412: * @param mustExist
413: * <code>true</code> if the selected file must already exist,
414: * else <code>false</code>
415: * @return the newly selected file or <code>null</code>
416: */
417: private IPath browse(IPath path, boolean mustExist) {
418: /*
419: * FileDialog dialog = new FileDialog(getShell(), mustExist ? SWT.OPEN :
420: * SWT.SAVE);
421: */
422: FileDialog dialog = new FileDialog(getShell(), SWT.OPEN);
423: dialog.setFilterPath(path.toOSString());
424: dialog.setFilterExtensions(new String[] { "*.wsdl", });
425: String result = dialog.open();
426: if (result == null)
427: return null;
428: return new Path(result);
429: }
430:
431: /**
432: * Answer the source file location or <code>null</code> if unspecified
433: */
434: public IPath getSourceLocation() {
435: String text = pro_path + separator + WSDL;
436: sourceFileField.getText().trim();
437: if (text.length() == 0)
438: return null;
439: IPath path = new Path(text);
440: if (!path.isAbsolute())
441: path = ResourcesPlugin.getWorkspace().getRoot()
442: .getLocation().append(path);
443: return path;
444: }
445:
446: /**
447: * Answer the destination file location or <code>null</code> if
448: * unspecified
449: */
450: public IPath getDestinationLocation() {
451: IPath path = new Path(
452: ImportedWSDLWizard.wsdl_abs__format_path_des);
453: return path;
454: }
455:
456: /**
457: * @return the sourceFileField
458: */
459: public Text getSourceFileField() {
460: return sourceFileField;
461: }
462:
463: /**
464: * @return the targetFileField
465: */
466: public Text getTargetFileField() {
467: return targetFileField;
468: }
469:
470: }
|