001: package com.bostechcorp.cbesb.ui.ide.wizards;
002:
003: import java.io.File;
004:
005: import org.eclipse.core.resources.IContainer;
006: import org.eclipse.core.resources.IResource;
007: import org.eclipse.core.resources.ResourcesPlugin;
008: import org.eclipse.jdt.core.IJavaProject;
009: import org.eclipse.jdt.core.IPackageFragment;
010: import org.eclipse.jdt.core.JavaCore;
011: import org.eclipse.jdt.core.JavaModelException;
012: import org.eclipse.jdt.ui.JavaUI;
013: import org.eclipse.jface.viewers.ISelection;
014: import org.eclipse.jface.viewers.IStructuredSelection;
015: import org.eclipse.jface.window.Window;
016: import org.eclipse.jface.wizard.IWizardPage;
017: import org.eclipse.jface.wizard.WizardPage;
018: import org.eclipse.swt.SWT;
019: import org.eclipse.swt.events.ModifyEvent;
020: import org.eclipse.swt.events.ModifyListener;
021: import org.eclipse.swt.events.SelectionAdapter;
022: import org.eclipse.swt.events.SelectionEvent;
023: import org.eclipse.swt.layout.GridData;
024: import org.eclipse.swt.layout.GridLayout;
025: import org.eclipse.swt.widgets.Button;
026: import org.eclipse.swt.widgets.Composite;
027: import org.eclipse.swt.widgets.Label;
028: import org.eclipse.swt.widgets.Text;
029: import org.eclipse.ui.IWorkbench;
030: import org.eclipse.ui.dialogs.SelectionDialog;
031:
032: import com.bostechcorp.cbesb.common.i18n.I18N;
033: import com.bostechcorp.cbesb.common.i18n.Messages;
034:
035: public class CustomCodeWizardPojoPage extends WizardPage {
036:
037: private Text textMethod;
038:
039: private Text textName;
040:
041: private Text packageText;
042:
043: private Text sourceFolderText;
044:
045: private ISelection selection;
046:
047: private IWorkbench workbench;
048:
049: private Label methodLabel;
050:
051: private CustomCodeWizardPojoPage _this ;
052:
053: private String delimiter = File.separator;
054:
055: /**
056: * Constructor for TransformerFormatCreationWizardPage.
057: *
058: * @param workbench
059: * @param selection
060: */
061: public CustomCodeWizardPojoPage(IWorkbench workbench,
062: ISelection selection) {
063: super ("PojoPage");
064: this .selection = selection;
065: this .workbench = workbench;
066: _this = this ;
067: setTitle(I18N.getString(Messages.CUSTOM_CODE_JAVA_PAGE));// "Choose
068: setDescription(I18N
069: .getString(Messages.CUSTOM_CODE_MAIN_PAGE_TIP));// "Choose
070:
071: }
072:
073: public void createControl(Composite parent) {
074:
075: final Composite composite = new Composite(parent, SWT.NONE);
076: final GridLayout gridLayout = new GridLayout();
077: gridLayout.marginTop = 5;
078: gridLayout.verticalSpacing = 10;
079: gridLayout.numColumns = 3;
080: composite.setLayout(gridLayout);
081:
082: final Label typeLabel = new Label(composite, SWT.NONE);
083: typeLabel.setText(I18N.getString(Messages.SOURCE_FOLDER));
084:
085: sourceFolderText = new Text(composite, SWT.READ_ONLY
086: | SWT.BORDER);
087: final GridData gridData = new GridData(SWT.FILL, SWT.CENTER,
088: true, false, 2, 1);
089: gridData.widthHint = 311;
090: sourceFolderText.setLayoutData(gridData);
091:
092: sourceFolderText.setText(((CustomCodeWizard) _this .getWizard())
093: .getProjectFromSelection()
094: + delimiter + "src" + delimiter + "java");
095: final Label languageLabel = new Label(composite, SWT.NONE);
096: languageLabel.setLayoutData(new GridData());
097: languageLabel.setText(I18N.getString(Messages.PACKAGE));
098:
099: packageText = new Text(composite, SWT.BORDER);
100: packageText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER,
101: true, false));
102:
103: final Button packageButton = new Button(composite, SWT.NONE);
104: packageButton.setLayoutData(new GridData(SWT.FILL, SWT.CENTER,
105: false, false));
106: packageButton.setText(I18N.getString(Messages.WIZARD_BROWSE));
107: packageButton.addSelectionListener(new SelectionAdapter() {
108: @Override
109: public void widgetSelected(SelectionEvent e) {
110:
111: // JavaUI.createPackageDialog(packageButton.getShell(),,SWT.NONE);
112: if (sourceFolderText.getText().equals(""))
113: return;
114: String project = "";
115: String filter = "";
116:
117: if (sourceFolderText.getText().contains(delimiter)) {
118: project = sourceFolderText.getText().substring(
119: 0,
120: sourceFolderText.getText().indexOf(
121: delimiter));
122: filter = sourceFolderText.getText().substring(
123: project.length() + 1);
124: } else {
125: project = sourceFolderText.getText();
126: ;
127: }
128: IJavaProject javaProject = JavaCore
129: .create(ResourcesPlugin.getWorkspace()
130: .getRoot().getProject(project));
131: SelectionDialog dialog = null;
132: try {
133: // if(!filter.equals("")){
134: dialog = JavaUI.createPackageDialog(packageButton
135: .getShell(), javaProject
136: .findPackageFragmentRoot(ResourcesPlugin
137: .getWorkspace().getRoot()
138: .getProject(project)
139: .getFile(filter).getFullPath()));
140: // }
141: // else {
142: // dialog =
143: // JavaUI.createPackageDialog(packageButton.getShell(),
144: // javaProject,IJavaElementSearchConstants.CONSIDER_REQUIRED_PROJECTS);
145: // // }
146: } catch (JavaModelException e1) {
147: //TODO handle Exception
148: }
149: if (dialog.open() != Window.OK) {
150: return;
151: }
152: IPackageFragment pck = (IPackageFragment) dialog
153: .getResult()[0];
154: if (pck != null) {
155: packageText.setText(pck.getElementName());
156: }
157: }
158:
159: });
160: final Label nameLabel = new Label(composite, SWT.NONE);
161: nameLabel.setLayoutData(new GridData());
162: nameLabel.setText(I18N.getString(Messages.NAME));
163:
164: textName = new Text(composite, SWT.BORDER);
165: textName.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true,
166: false, 2, 1));
167: textName.addModifyListener(new ModifyListener() {
168:
169: public void modifyText(ModifyEvent e) {
170:
171: dialogChanged();
172: }
173:
174: });
175:
176: methodLabel = new Label(composite, SWT.NONE);
177: methodLabel.setLayoutData(new GridData());
178: methodLabel.setText(I18N.getString(Messages.METHOD));
179: methodLabel.setVisible(false);
180:
181: textMethod = new Text(composite, SWT.BORDER);
182: textMethod.setLayoutData(new GridData(SWT.FILL, SWT.CENTER,
183: true, false, 2, 1));
184: textMethod.setVisible(false);
185: textMethod.addModifyListener(new ModifyListener() {
186:
187: public void modifyText(ModifyEvent e) {
188:
189: dialogChanged();
190: }
191:
192: });
193: initialize();
194: setControl(composite);
195: dialogChanged();
196: }
197:
198: /**
199: * Ensures that both text fields are set.
200: */
201:
202: public void dialogChanged() {
203: if (sourceFolderText.getText().equals("")) {
204: setPageComplete(false);
205: updateStatus(I18N
206: .getString(Messages.CUSTOM_CODE_SOURCE_FOLDER_REQUIRED));
207: return;
208: }
209: if (textName.getText().equals("")) {
210: setPageComplete(false);
211: updateStatus(I18N.getString(Messages.NAME_REQ));
212: return;
213: }
214: if (textMethod.isVisible() && textMethod.getText().equals("")) {
215: setPageComplete(false);
216: updateStatus(I18N.getString(Messages.METHOD_REQUIRED));
217: return;
218: }
219: updateStatus(null);
220: setPageComplete(true);
221: }
222:
223: public void updateStatus(String message) {
224: setErrorMessage(message);
225: }
226:
227: public IWizardPage getNextPage() {
228:
229: return null;
230: }
231:
232: private void initialize() {
233: if (selection != null && selection.isEmpty() == false
234: && selection instanceof IStructuredSelection) {
235: IStructuredSelection ssel = (IStructuredSelection) selection;
236: if (ssel.size() > 1)
237: return;
238: Object obj = ssel.getFirstElement();
239: if (obj instanceof IResource) {
240: IContainer container;
241: if (obj instanceof IContainer)
242: container = (IContainer) obj;
243: else
244: container = ((IResource) obj).getParent();
245: // targetfileNameText.setText(container.getFullPath().toString());
246: }
247: }
248: }
249:
250: public boolean finish() {
251: return false;
252: }
253:
254: public String getPath() {
255: String location = ResourcesPlugin.getWorkspace().getRoot()
256: .getLocation().toOSString();
257: String packagePath = getPackageName().replace('.',
258: File.separatorChar);
259: return location + File.separator + sourceFolderText.getText()
260: + File.separator + packagePath;
261: }
262:
263: public String getName() {
264: return textName.getText();
265: }
266:
267: public String getMethod() {
268: return textMethod.getText();
269: }
270:
271: public String getPackageName() {
272: return packageText.getText();
273: }
274:
275: public void changeMethodVisable(boolean flag) {
276: methodLabel.setVisible(flag);
277: textMethod.setVisible(flag);
278: }
279:
280: public String getProjectName() {
281: String project = "";
282: if (sourceFolderText.getText().contains(File.separator)) {
283: project = sourceFolderText.getText().substring(0,
284: sourceFolderText.getText().indexOf(File.separator));
285: } else {
286: project = sourceFolderText.getText();
287: ;
288: }
289: return project;
290: }
291:
292: public Text getTextMethod() {
293: return textMethod;
294: }
295:
296: }
|