All Classes Namespaces Files Functions Variables
PackageWizardPage.java
Go to the documentation of this file.
1 package javafxmlpackage.wizards;
2 
3 import java.util.regex.Matcher;
4 import java.util.regex.Pattern;
5 
6 import org.eclipse.core.resources.IContainer;
7 import org.eclipse.core.resources.IResource;
8 import org.eclipse.core.resources.ResourcesPlugin;
9 import org.eclipse.core.runtime.Path;
10 import org.eclipse.jface.dialogs.IDialogPage;
11 import org.eclipse.jface.viewers.ISelection;
12 import org.eclipse.jface.viewers.IStructuredSelection;
13 import org.eclipse.jface.window.Window;
14 import org.eclipse.jface.wizard.WizardPage;
15 import org.eclipse.swt.SWT;
16 import org.eclipse.swt.events.SelectionAdapter;
17 import org.eclipse.swt.events.SelectionEvent;
18 import org.eclipse.swt.layout.GridData;
19 import org.eclipse.swt.layout.GridLayout;
20 import org.eclipse.swt.widgets.Button;
21 import org.eclipse.swt.widgets.Composite;
22 import org.eclipse.swt.widgets.Label;
23 import org.eclipse.swt.widgets.Text;
24 import org.eclipse.ui.dialogs.ContainerSelectionDialog;
25 
26 /**
27  * Wizard, joka luo jyu ohj2 kurssin mukaisen javaFXML packagen.
28  *
29  */
30 
31 public class PackageWizardPage extends WizardPage {
32  private Text containerText;
33  private Text packageName;
34  private Text fileName;
35  private ISelection selection;
36 
37  /**
38  * Constructor for PackageNewWizardPage.
39  * @param selection s
40 
41  */
42  public PackageWizardPage(ISelection selection) {
43  super("wizardPage");
44  setTitle("Uusi JavaFXML Package");
45  setDescription("Luo uusi package");
46  this.selection = selection;
47  }
48 
49  /**
50  * @see IDialogPage#createControl(Composite)
51  */
52  @Override
53  public void createControl(Composite parent) {
54  Composite container = new Composite(parent, SWT.NULL);
55 
56  GridLayout layout = new GridLayout();
57 
58  container.setLayout(layout);
59  layout.numColumns = 3;
60 
61  layout.verticalSpacing = 9;
62  Label label = new Label(container, SWT.NULL);
63  label.setText("&Project src folder:");
64 
65  containerText = new Text(container, SWT.BORDER | SWT.SINGLE);
66  GridData gd = new GridData(GridData.FILL_HORIZONTAL);
67  containerText.setLayoutData(gd);
68 
69  containerText.addModifyListener( e -> dialogChanged());
70 
71 
72  Button button = new Button(container, SWT.PUSH);
73  button.setText("Browse...");
74 
75  button.addSelectionListener(new SelectionAdapter() {
76  @Override
77  public void widgetSelected(SelectionEvent e) {
78  handleBrowse();
79  }
80  });
81 
82  label = new Label(container, SWT.NULL);
83  label.setText("&Packagen nimi:");
84  packageName = new Text(container, SWT.BORDER | SWT.SINGLE);
85 
86  gd = new GridData(GridData.FILL_HORIZONTAL);
87  packageName.setLayoutData(gd);
88  packageName.addModifyListener( e -> dialogChanged());
89 
90 
91  label = new Label(container, SWT.NULL); // tyhjä labeli, koska gridissä 3 saraketta
92 
93  label = new Label(container, SWT.NULL);
94  label.setText("&Ohjelman nimi:");
95 
96  fileName = new Text(container, SWT.BORDER | SWT.SINGLE);
97  gd = new GridData(GridData.FILL_HORIZONTAL);
98  fileName.setLayoutData(gd);
99 
100  fileName.addModifyListener( e -> dialogChanged());
101 
102  initialize();
103  dialogChanged();
104  setControl(container);
105  }
106 
107  /**
108  * Tests if the current workbench selection is a suitable container to use.
109  */
110 
111  private void initialize() {
112  if (selection != null && selection.isEmpty() == false
113  && selection instanceof IStructuredSelection) {
114  IStructuredSelection ssel = (IStructuredSelection) selection;
115  if (ssel.size() > 1)
116  return;
117  Object obj = ssel.getFirstElement();
118 
119  if (obj instanceof IResource) {
120  IContainer container;
121  String pathi = "";
122  if (obj instanceof IContainer) {
123  container = (IContainer) obj;
124  pathi = container.getFullPath().toString();
125  if (!pathi.matches(".*/src/"))
126  pathi = container.getFullPath().toString() +"/src/";
127  }
128  else {
129  container = ((IResource) obj).getParent();
130  pathi = container.getFullPath().toString();
131 
132  Pattern p = Pattern.compile("^(.*/src)(.*)");
133  Matcher m = p.matcher(pathi);
134  if (m.find()) {
135  pathi = m.group(1);
136  if (!m.group(2).isEmpty()) {
137  String mg = m.group(2).replace('/', '.');
138  packageName.setText(m.group(2).replace('/', '.'));
139  if (mg.charAt(0) == '.')
140  packageName.setText(mg.substring(1));
141  else packageName.setText(mg);
142  }
143  }
144  }
145 
146  containerText.setText(pathi);
147  }
148  else {
149  // toi objecti voi ny olla myös joku class org.eclipse.jdt.internal.core.* classi
150  // joihin ei ny pääse käsiks, joten yritetää arvata sen palauttamasta stringistä missä mahtais source folderi olla :)
151 
152  String stringi = obj.toString();
153 
154 
155  Pattern p = Pattern.compile("(.*)src \\[in ([^\\]]+)\\]*.");
156  Matcher m = p.matcher(stringi);
157 
158  if (m.find()) {
159  containerText.setText("/"+m.group(2)+"/src/");
160  if (!m.group(1).isEmpty()) {
161  String[] splitti = m.group(1).split(" ");
162  // jooh eli ku katotaa stringistä vaa tota pathia, nii pitää karsii erikoistapauksia pois
163  // manuaalisesti......... ei hyvä
164  if (!splitti[0].matches(".*\\.java$") && !splitti[0].matches("^\\[.*"))
165  packageName.setText(splitti[0]);
166  }
167  }
168 
169  }
170  }
171  }
172 
173  /**
174  * Uses the standard container selection dialog to choose the new value for
175  * the container field.
176  */
177 
178  protected void handleBrowse() {
179  ContainerSelectionDialog dialog = new ContainerSelectionDialog(
180  getShell(), ResourcesPlugin.getWorkspace().getRoot(), false,
181  "Select project src folder");
182  if (dialog.open() == Window.OK) {
183  Object[] result = dialog.getResult();
184  if (result.length == 1) {
185  containerText.setText(((Path) result[0]).toString());
186  }
187  }
188  }
189 
190  /**
191  * Valitetaan virheistä jne..
192  */
193 
194  private void dialogChanged() {
195  IResource container = ResourcesPlugin.getWorkspace().getRoot()
196  .findMember(new Path(getContainerName()));
197  String fileN = getFileName();
198  String packageN = getPackageName();
199 
200  if (getContainerName().length() == 0) {
201  updateStatus("Projektin src folderi puuttuu");
202  return;
203  }
204 
205  if (container == null
206  || (container.getType() & (IResource.PROJECT | IResource.FOLDER)) == 0) {
207  updateStatus("Projektin src folderi puuttuu");
208  return;
209  }
210  if (!container.isAccessible()) {
211  updateStatus("Ei oikeuksia projektin src folderiin");
212  return;
213  }
214  if (fileN.length() == 0 || packageN.length() == 0) {
215  updateStatus("Paketin tai ohjelman nimi määrittämättä");
216  return;
217  }
218 
219 
220  if (!Pattern.matches("[a-zA-ZäöåÅÄÖ0-9]+", fileN) || !Pattern.matches("[a-zA-ZäöåÅÄÖ/.0-9]+", packageN)) {
221  updateStatus("Ohjelman tai packagen nimessä ei voi olla merkkejä");
222  return;
223  }
224 
225  if (packageN.lastIndexOf('.') == packageN.length() - 1) {
226  updateStatus("Packagen nimi ei voi loppua pisteeseen");
227  return;
228  }
229 
230  if (packageN.indexOf('.') == 0) {
231  updateStatus("Packagen nimi ei voi alkaa pisteellä");
232  return;
233  }
234 
235  if (fileN.lastIndexOf('.') != -1) {
236  updateStatus("Ohjelman nimessä ei saa olla pistettä");
237  return;
238  }
239 
240  updateStatus(null);
241  }
242 
243  private void updateStatus(String message) {
244  setErrorMessage(message);
245  setPageComplete(message == null);
246  }
247 
248  /**
249  * @return src:n polku
250  */
251  public String getContainerName() {
252  return containerText.getText();
253  }
254 
255  /**
256  * @return ohjelman nimi
257  */
258  public String getFileName() {
259  return fileName.getText();
260  }
261 
262  /**
263  * @return paketin nimi
264  */
265  public String getPackageName() {
266  return packageName.getText();
267  }
268 }