KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > emf > codegen > presentation > JETPropertyPage


1 /**
2  * <copyright>
3  *
4  * Copyright (c) 2002-2004 IBM Corporation and others.
5  * All rights reserved. This program and the accompanying materials
6  * are made available under the terms of the Eclipse Public License v1.0
7  * which accompanies this distribution, and is available at
8  * http://www.eclipse.org/legal/epl-v10.html
9  *
10  * Contributors:
11  * IBM - Initial API and implementation
12  *
13  * </copyright>
14  *
15  * $Id: JETPropertyPage.java,v 1.3 2005/06/08 06:23:43 nickb Exp $
16  */

17 package org.eclipse.emf.codegen.presentation;
18
19
20 import java.util.List JavaDoc;
21
22 import org.eclipse.core.resources.IContainer;
23 import org.eclipse.core.resources.IProject;
24 import org.eclipse.core.runtime.CoreException;
25 import org.eclipse.core.runtime.NullProgressMonitor;
26 import org.eclipse.jdt.core.IJavaProject;
27 import org.eclipse.swt.SWT;
28 import org.eclipse.swt.layout.GridData;
29 import org.eclipse.swt.layout.GridLayout;
30 import org.eclipse.swt.widgets.Composite;
31 import org.eclipse.swt.widgets.Control;
32 import org.eclipse.swt.widgets.Event;
33 import org.eclipse.swt.widgets.Label;
34 import org.eclipse.swt.widgets.Listener;
35 import org.eclipse.swt.widgets.Text;
36 import org.eclipse.ui.dialogs.PropertyPage;
37
38 import org.eclipse.emf.codegen.jet.IJETNature;
39 import org.eclipse.emf.codegen.jet.JETNature;
40
41
42 public class JETPropertyPage extends PropertyPage implements Listener
43 {
44   protected IProject project;
45
46   protected Text templateContainerField;
47   protected Text javaSourceField;
48
49   protected List JavaDoc oldTemplateContainers;
50   protected List JavaDoc oldTemplateSourceContainers;
51   protected List JavaDoc newTemplateContainers;
52   protected List JavaDoc newTemplateSourceContainers;
53   protected IContainer oldJavaSoureContainer;
54   protected IContainer newJavaSourceContainer;
55
56   protected Control createContents(Composite parent)
57   {
58     Control control = null;
59
60     project = getJETProject();
61     if (project != null)
62     {
63       IJETNature jetNature = JETNature.getRuntime(project);
64       if (jetNature != null)
65       {
66         // container specification group
67
Composite containerGroup = new Composite(parent, SWT.NONE);
68         GridLayout layout = new GridLayout();
69         layout.numColumns = 2;
70         containerGroup.setLayout(layout);
71         containerGroup.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.GRAB_HORIZONTAL));
72
73         // New Template Container Label
74
Label templateContainerLabel = new Label(containerGroup, SWT.CHECK);
75         templateContainerLabel.setText(CodeGenUIPlugin.getPlugin().getString("_UI_TemplateContainer_label"));
76
77         // New Template Container Entryfield
78
templateContainerField = new Text(containerGroup, SWT.BORDER);
79         GridData data = new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.GRAB_HORIZONTAL);
80         templateContainerField.setLayoutData(data);
81
82         oldTemplateContainers = jetNature.getTemplateContainers();
83         oldTemplateSourceContainers = jetNature.getTemplateSourceContainers();
84         templateContainerField.setText(JETNature.getContainers(project, oldTemplateContainers, oldTemplateSourceContainers));
85         templateContainerField.addListener(SWT.Modify, this);
86
87         // New Template Container Label
88
Label sourceContainerLabel = new Label(containerGroup, SWT.CHECK);
89         sourceContainerLabel.setText(CodeGenUIPlugin.getPlugin().getString("_UI_SourceContainer_label"));
90
91         // New Template Container Entryfield
92
javaSourceField = new Text(containerGroup, SWT.BORDER);
93         data = new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.GRAB_HORIZONTAL);
94         javaSourceField.setLayoutData(data);
95
96         oldJavaSoureContainer = jetNature.getJavaSourceContainer();
97         javaSourceField.setText( oldJavaSoureContainer.getProjectRelativePath().toString());
98         javaSourceField.addListener(SWT.Modify, this);
99
100         control = containerGroup;
101       }
102       else
103       {
104         Label closedProjectLabel = new Label(parent, SWT.NONE);
105         closedProjectLabel.setText(CodeGenUIPlugin.getPlugin().getString("_UI_WebSettingsNotAvaiable_message"));
106         control = closedProjectLabel;
107       }
108     }
109     return control;
110   }
111
112   /**
113    * Returns the highlighted project in the workbench.
114    */

115   protected IProject getJETProject()
116   {
117     Object JavaDoc element = getElement();
118
119     if (element instanceof IProject)
120     {
121       return isJETProject((IProject) element) ? (IProject) element : null;
122     }
123     else if (element instanceof IJavaProject)
124     {
125       return isJETProject(((IJavaProject) element).getProject()) ? ((IJavaProject) element).getProject() : null;
126     }
127
128     return null;
129   }
130
131   /**
132    * Return whether this is a JET project.
133    */

134   protected static boolean isJETProject(IProject project)
135   {
136     try
137     {
138       return project.hasNature(IJETNature.NATURE_ID);
139     }
140     catch (CoreException e)
141     {
142       CodeGenUIPlugin.write(e);
143     }
144
145     return false;
146   }
147
148   public void handleEvent(Event event)
149   {
150     if (event.widget == templateContainerField)
151     {
152       IJETNature jetNature = JETNature.getRuntime(project);
153       if (jetNature != null)
154       {
155         try
156         {
157           newTemplateContainers = JETNature.getContainers(getJETProject(), templateContainerField.getText());
158           newTemplateSourceContainers = JETNature.getContainers(getJETProject(), templateContainerField.getText(), true);
159           // jetNature.setTemplateContainers(newTemplateContainers);
160
setErrorMessage(null);
161         }
162         catch (Exception JavaDoc exception)
163         {
164           setErrorMessage
165             (CodeGenUIPlugin.getPlugin().getString
166                ("_UI_CannotSetTemplateContainer_message", new String JavaDoc [] { exception.getLocalizedMessage() }));
167         }
168       }
169     }
170
171     if (event.widget == javaSourceField)
172     {
173       IJETNature jetNature = JETNature.getRuntime(project);
174       if (jetNature != null)
175       {
176         newJavaSourceContainer = JETNature.getContainer(getJETProject(), javaSourceField.getText());
177         if (newJavaSourceContainer.exists())
178         {
179           // jetNature.setJavaSourceContainer(newJavaSourceContainer);
180
setErrorMessage(null);
181         }
182         else
183         {
184           setErrorMessage
185             (CodeGenUIPlugin.getPlugin().getString
186                ("_UI_ContainerDoesNotExist_message", new String JavaDoc [] { newJavaSourceContainer.toString() }));
187         }
188       }
189     }
190   }
191
192   public boolean performOk()
193   {
194     performApply();
195     return super.performOk();
196   }
197
198   protected void performApply()
199   {
200     IJETNature jetNature = JETNature.getRuntime(project);
201     if (jetNature != null)
202     {
203       if (newTemplateContainers != null)
204       {
205         jetNature.setTemplateContainers
206           (newTemplateContainers, newTemplateSourceContainers == null ? newTemplateContainers : newTemplateSourceContainers);
207       }
208       if (newJavaSourceContainer != null)
209       {
210         jetNature.setJavaSourceContainer(newJavaSourceContainer);
211       }
212     }
213   }
214
215   protected void performDefaults()
216   {
217     IJETNature jetNature = JETNature.getRuntime(project);
218     if (jetNature != null)
219     {
220       try
221       {
222         ((JETNature)jetNature).setDefaults(new NullProgressMonitor());
223       }
224       catch(CoreException e)
225       {
226         CodeGenUIPlugin.write(e);
227       }
228     }
229   }
230 }
231
Popular Tags