1 17 package org.apache.forrest.eclipse.wizards; 18 19 import org.eclipse.jface.wizard.WizardPage; 20 import org.eclipse.swt.widgets.*; 21 import org.eclipse.swt.layout.*; 22 import org.eclipse.swt.SWT; 23 import org.eclipse.core.resources.*; 24 import org.eclipse.core.runtime.Path; 25 import org.eclipse.swt.events.*; 26 import org.eclipse.ui.dialogs.ContainerSelectionDialog; 27 import org.eclipse.jface.viewers.*; 28 29 35 36 public class NewXdocPage extends WizardPage { 37 public String projectXDocsDir = "/src/documentation/content/xdocs"; 39 public String default_filename = "new_file.xml"; 40 public String required_file_extension = "xml"; 41 private Text containerText; 42 private Text fileText; 43 protected ISelection selection; 44 45 49 public NewXdocPage(ISelection selection) { 50 super("wizardPage"); 51 setTitle("XDoc File"); 52 setDescription("This wizard creates a new XDoc file with *.xml extension."); 53 this.selection = selection; 54 } 55 56 59 public void createControl(Composite parent) { 60 Composite container = new Composite(parent, SWT.NULL); 61 GridLayout layout = new GridLayout(); 62 container.setLayout(layout); 63 layout.numColumns = 3; 64 layout.verticalSpacing = 9; 65 Label label = new Label(container, SWT.NULL); 66 label.setText("&Container:"); 67 68 containerText = new Text(container, SWT.BORDER | SWT.SINGLE); 69 containerText.setText(projectXDocsDir); 70 GridData gd = new GridData(GridData.FILL_HORIZONTAL); 71 containerText.setLayoutData(gd); 72 containerText.addModifyListener(new ModifyListener() { 73 public void modifyText(ModifyEvent e) { 74 dialogChanged(); 75 } 76 }); 77 78 Button button = new Button(container, SWT.PUSH); 79 button.setText("Browse..."); 80 button.addSelectionListener(new SelectionAdapter() { 81 public void widgetSelected(SelectionEvent e) { 82 handleBrowse(); 83 } 84 }); 85 label = new Label(container, SWT.NULL); 86 label.setText("&File name:"); 87 88 fileText = new Text(container, SWT.BORDER | SWT.SINGLE); 89 gd = new GridData(GridData.FILL_HORIZONTAL); 90 fileText.setLayoutData(gd); 91 fileText.addModifyListener(new ModifyListener() { 92 public void modifyText(ModifyEvent e) { 93 dialogChanged(); 94 } 95 }); 96 initialize(); 97 dialogChanged(); 98 setControl(container); 99 } 100 101 105 106 private void initialize() { 107 if (selection!=null && selection.isEmpty()==false && selection instanceof IStructuredSelection) { 108 IStructuredSelection ssel = (IStructuredSelection)selection; 109 if (ssel.size()>1) return; 110 Object obj = ssel.getFirstElement(); 111 if (obj instanceof IResource) { 112 IContainer container; 113 if (obj instanceof IContainer) 114 container = (IContainer)obj; 115 else 116 container = ((IResource)obj).getParent(); 117 containerText.setText(container.getFullPath().toString()); 118 } 119 } 120 fileText.setText(default_filename); 121 } 122 123 127 128 private void handleBrowse() { 129 ContainerSelectionDialog dialog = 130 new ContainerSelectionDialog( 131 getShell(), 132 ResourcesPlugin.getWorkspace().getRoot(), 133 false, 134 "Select new file container"); 135 if (dialog.open() == ContainerSelectionDialog.OK) { 136 Object [] result = dialog.getResult(); 137 if (result.length == 1) { 138 containerText.setText(((Path)result[0]).toOSString()); 139 } 140 } 141 } 142 143 146 147 private void dialogChanged() { 148 String container = getContainerName(); 149 String fileName = getFileName(); 150 151 if (container.length() == 0) { 152 updateStatus("File container must be specified"); 153 return; 154 } 155 if (fileName.length() == 0) { 156 updateStatus("File name must be specified"); 157 return; 158 } 159 int dotLoc = fileName.lastIndexOf('.'); 160 if (dotLoc != -1) { 161 String ext = fileName.substring(dotLoc + 1); 162 if (ext.equalsIgnoreCase(required_file_extension) == false) { 163 updateStatus("File extension must be \"" + required_file_extension + "\""); 164 return; 165 } 166 } 167 updateStatus(null); 168 } 169 170 private void updateStatus(String message) { 171 setErrorMessage(message); 172 setPageComplete(message == null); 173 } 174 175 public String getContainerName() { 176 return containerText.getText(); 177 } 178 public String getFileName() { 179 return fileText.getText(); 180 } 181 } | Popular Tags |