Source code: com/vinculum/processeditor/CreateVCMPage1.java
1 /* * ** ** BEGIN LICENSE BLOCK * ** **
2 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
3 *
4 * The contents of this file are subject to the Mozilla Public License Version
5 * 1.1 (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
7 * http://www.mozilla.org/MPL/
8 *
9 * Software distributed under the License is distributed on an "AS IS" basis,
10 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11 * for the specific language governing rights and limitations under the
12 * License.
13 *
14 * The Original Code is Vinculum Open Source.
15 *
16 * The Initial Developer of the Original Code is
17 * Gerard Toonstra.
18 * Portions created by the Initial Developer are Copyright (C) 2003
19 * the Initial Developer. All Rights Reserved.
20 *
21 * Contributor(s):
22 *
23 * Alternatively, the contents of this file may be used under the terms of
24 * either the GNU General Public License Version 2 or later (the "GPL"), or
25 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
26 * in which case the provisions of the GPL or the LGPL are applicable instead
27 * of those above. If you wish to allow use of your version of this file only
28 * under the terms of either the GPL or the LGPL, and not to allow others to
29 * use your version of this file under the terms of the MPL, indicate your
30 * decision by deleting the provisions above and replace them with the notice
31 * and other provisions required by the GPL or the LGPL. If you do not delete
32 * the provisions above, a recipient may use your version of this file under
33 * the terms of any one of the MPL, the GPL or the LGPL.
34 *
35 * ** ** * END LICENSE BLOCK * ** **
36 */
37
38 /***************************************************************************
39 $RCSfile: CreateVCMPage1.java,v $ - description
40 -------------------
41 begin : $Date: 2003/08/26 18:49:21 $
42 copyright : Vinculum (C) 2002
43 author : $Author: chiraz $
44 ***************************************************************************/
45
46 /* $Log: CreateVCMPage1.java,v $
47 /* Revision 1.1 2003/08/26 18:49:21 chiraz
48 /* added the VCM page Java object
49 /*
50 /* Revision 1.1.1.1 2003/07/08 08:02:08 chiraz
51 /* egg
52 /* */
53
54 package com.vinculum.processeditor;
55
56 import java.io.ByteArrayInputStream;
57 import java.io.ByteArrayOutputStream;
58 import java.io.InputStream;
59 import java.io.ObjectOutputStream;
60
61 import org.eclipse.core.resources.IFile;
62 import org.eclipse.jface.resource.ImageDescriptor;
63 import org.eclipse.jface.viewers.IStructuredSelection;
64 import org.eclipse.swt.SWT;
65 import org.eclipse.swt.events.SelectionEvent;
66 import org.eclipse.swt.events.SelectionListener;
67 import org.eclipse.swt.layout.GridData;
68 import org.eclipse.swt.layout.GridLayout;
69 import org.eclipse.swt.widgets.Button;
70 import org.eclipse.swt.widgets.Composite;
71 import org.eclipse.swt.widgets.Group;
72 import org.eclipse.swt.widgets.Label;
73 import org.eclipse.ui.IWorkbench;
74 import org.eclipse.ui.IWorkbenchPage;
75 import org.eclipse.ui.IWorkbenchWindow;
76 import org.eclipse.ui.dialogs.WizardNewFileCreationPage;
77
78 import com.vinculum.processeditor.model.ProcessDiagram;
79 import com.vinculum.processeditor.model.ProcessDiagramFactory;
80
81 /**
82 * @author chilan
83 *
84 */
85 public class CreateVCMPage1
86 extends WizardNewFileCreationPage
87 implements SelectionListener
88 {
89 private IWorkbench workbench;
90 private static int exampleCount = 1;
91 private Button model1 = null;
92 private Button model2 = null;
93 private int modelSelected = 1;
94
95 public CreateVCMPage1(IWorkbench aWorkbench, IStructuredSelection selection)
96 {
97 super("sampleLogicPage1", selection); //$NON-NLS-1$
98 this.setTitle(ProcessMessages.CreateVCMPage1_Title);
99 this.setDescription(ProcessMessages.CreateVCMPage1_Description);
100 this.setImageDescriptor(ImageDescriptor.createFromFile(getClass(),"icons/logic.gif")); //$NON-NLS-1$
101 this.workbench = aWorkbench;
102 }
103
104 public void createControl(Composite parent)
105 {
106 super.createControl(parent);
107 this.setFileName("emptyModel" + exampleCount + ".logic"); //$NON-NLS-2$//$NON-NLS-1$
108
109 Composite composite = (Composite)getControl();
110
111 new Label(composite,SWT.NONE);
112
113 // sample section generation group
114 Group group = new Group(composite,SWT.NONE);
115 group.setLayout(new GridLayout());
116 group.setText(ProcessMessages.CreateVCMPage1_ModelNames_GroupName);
117 group.setLayoutData(new GridData(GridData.GRAB_HORIZONTAL | GridData.HORIZONTAL_ALIGN_FILL));
118
119 // sample section generation checkboxes
120 model1 = new Button(group,SWT.RADIO);
121 model1.setText(ProcessMessages.CreateVCMPage1_ModelNames_EmptyModelName);
122 model1.addSelectionListener(this);
123 model1.setSelection(true);
124
125 model2 = new Button(group,SWT.RADIO);
126 model2.setText(ProcessMessages.CreateVCMPage1_ModelNames_FourBitAdderModelName);
127 model2.addSelectionListener(this);
128
129 new Label(composite,SWT.NONE);
130
131 setPageComplete(validatePage());
132 }
133
134 protected InputStream getInitialContents()
135 {
136 ProcessDiagram pd = new ProcessDiagram();
137 if (modelSelected == 2)
138 pd = (ProcessDiagram)ProcessDiagramFactory.createLargeModel();
139 ByteArrayInputStream bais = null;
140 try
141 {
142 ByteArrayOutputStream baos = new ByteArrayOutputStream();
143 ObjectOutputStream oos = new ObjectOutputStream(baos);
144 oos.writeObject(pd);
145 oos.flush();
146 oos.close();
147 baos.close();
148 bais = new ByteArrayInputStream(baos.toByteArray());
149 bais.close();
150 }
151 catch(Exception e)
152 {
153 e.printStackTrace();
154 }
155 return bais;
156 }
157
158 public boolean finish()
159 {
160 IFile newFile = createNewFile();
161 if (newFile == null)
162 return false; // ie.- creation was unsuccessful
163
164 // Since the file resource was created fine, open it for editing
165 // iff requested by the user
166 try
167 {
168 IWorkbenchWindow dwindow = workbench.getActiveWorkbenchWindow();
169 IWorkbenchPage page = dwindow.getActivePage();
170 if (page != null)
171 page.openEditor(newFile);
172 }
173 catch (org.eclipse.ui.PartInitException e)
174 {
175 e.printStackTrace();
176 return false;
177 }
178 exampleCount++;
179 return true;
180 }
181
182 /**
183 * @see org.eclipse.swt.events.SelectionListener#widgetSelected(SelectionEvent)
184 */
185 public void widgetSelected(SelectionEvent e)
186 {
187 if( e.getSource() == model1 )
188 {
189 modelSelected = 1;
190 setFileName("emptyModel" + exampleCount + ".logic"); //$NON-NLS-2$//$NON-NLS-1$
191 }
192 else
193 {
194 modelSelected = 2;
195 setFileName("fourBitAdder" + exampleCount + ".logic"); //$NON-NLS-2$//$NON-NLS-1$
196 }
197 }
198
199 /**
200 * Empty method
201 */
202 public void widgetDefaultSelected(SelectionEvent e)
203 {
204 }
205 }