001: /*******************************************************************************
002: * Copyright (c) 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.ui.examples.views.properties.tabbed.logic;
011:
012: import java.io.ByteArrayInputStream;
013: import java.io.ByteArrayOutputStream;
014: import java.io.InputStream;
015: import java.io.ObjectOutputStream;
016:
017: import org.eclipse.swt.SWT;
018: import org.eclipse.swt.events.SelectionEvent;
019: import org.eclipse.swt.events.SelectionListener;
020: import org.eclipse.swt.layout.GridData;
021: import org.eclipse.swt.layout.GridLayout;
022: import org.eclipse.swt.widgets.Button;
023: import org.eclipse.swt.widgets.Composite;
024: import org.eclipse.swt.widgets.Group;
025: import org.eclipse.swt.widgets.Label;
026:
027: import org.eclipse.core.resources.IFile;
028: import org.eclipse.jface.resource.ImageDescriptor;
029: import org.eclipse.jface.viewers.IStructuredSelection;
030: import org.eclipse.ui.IWorkbench;
031: import org.eclipse.ui.IWorkbenchPage;
032: import org.eclipse.ui.IWorkbenchWindow;
033: import org.eclipse.ui.dialogs.WizardNewFileCreationPage;
034: import org.eclipse.ui.ide.IDE;
035:
036: import org.eclipse.gef.examples.logicdesigner.model.LogicDiagram;
037: import org.eclipse.gef.examples.logicdesigner.model.LogicDiagramFactory;
038:
039: public class LogicWizardPage1 extends WizardNewFileCreationPage
040: implements SelectionListener {
041:
042: private IWorkbench workbench;
043:
044: private static int exampleCount = 1;
045:
046: private Button model1 = null;
047:
048: private Button model2 = null;
049:
050: private int modelSelected = 1;
051:
052: public LogicWizardPage1(IWorkbench aWorkbench,
053: IStructuredSelection selection) {
054: super ("sampleLogicPage1", selection); //$NON-NLS-1$
055: this .setTitle("Tabbed Properties View Logic Example");//$NON-NLS-1$
056: this
057: .setDescription("Create a new Tabbed Properties View Logic Example file");//$NON-NLS-1$
058: this .setImageDescriptor(ImageDescriptor.createFromFile(
059: getClass(), "icons/logicbanner.gif")); //$NON-NLS-1$
060: this .workbench = aWorkbench;
061: }
062:
063: public void createControl(Composite parent) {
064: super .createControl(parent);
065: this
066: .setFileName("emptyModel" + exampleCount + ".tabbedpropertieslogic"); //$NON-NLS-2$//$NON-NLS-1$
067:
068: Composite composite = (Composite) getControl();
069:
070: // sample section generation group
071: Group group = new Group(composite, SWT.NONE);
072: group.setLayout(new GridLayout());
073: group.setText("Logic Model Samples"); //$NON-NLS-1$
074: group.setLayoutData(new GridData(GridData.GRAB_HORIZONTAL
075: | GridData.HORIZONTAL_ALIGN_FILL));
076:
077: // sample section generation checkboxes
078: model1 = new Button(group, SWT.RADIO);
079: model1.setText("E&mpty Model");//$NON-NLS-1$
080: model1.addSelectionListener(this );
081: model1.setSelection(true);
082:
083: model2 = new Button(group, SWT.RADIO);
084: model2.setText("F&our-bit Adder Model");//$NON-NLS-1$
085: model2.addSelectionListener(this );
086:
087: new Label(composite, SWT.NONE);
088:
089: setPageComplete(validatePage());
090: }
091:
092: protected InputStream getInitialContents() {
093: LogicDiagram ld = new LogicDiagram();
094: if (modelSelected == 2)
095: ld = (LogicDiagram) LogicDiagramFactory.createLargeModel();
096: ByteArrayInputStream bais = null;
097: try {
098: ByteArrayOutputStream baos = new ByteArrayOutputStream();
099: ObjectOutputStream oos = new ObjectOutputStream(baos);
100: oos.writeObject(ld);
101: oos.flush();
102: oos.close();
103: baos.close();
104: bais = new ByteArrayInputStream(baos.toByteArray());
105: bais.close();
106: } catch (Exception e) {
107: e.printStackTrace();
108: }
109: return bais;
110: }
111:
112: public boolean finish() {
113: IFile newFile = createNewFile();
114: if (newFile == null)
115: return false; // ie.- creation was unsuccessful
116:
117: // Since the file resource was created fine, open it for editing
118: // iff requested by the user
119: try {
120: IWorkbenchWindow dwindow = workbench
121: .getActiveWorkbenchWindow();
122: IWorkbenchPage page = dwindow.getActivePage();
123: if (page != null)
124: IDE.openEditor(page, newFile, true);
125: } catch (org.eclipse.ui.PartInitException e) {
126: e.printStackTrace();
127: return false;
128: }
129: exampleCount++;
130: return true;
131: }
132:
133: /**
134: * @see org.eclipse.swt.events.SelectionListener#widgetSelected(SelectionEvent)
135: */
136: public void widgetSelected(SelectionEvent e) {
137: if (e.getSource() == model1) {
138: modelSelected = 1;
139: setFileName("emptyModel" + exampleCount + ".tabbedpropertieslogic"); //$NON-NLS-2$//$NON-NLS-1$
140: } else {
141: modelSelected = 2;
142: setFileName("fourBitAdder" + exampleCount + ".tabbedpropertieslogic"); //$NON-NLS-2$//$NON-NLS-1$
143: }
144: }
145:
146: /**
147: * Empty method
148: */
149: public void widgetDefaultSelected(SelectionEvent e) {
150: //
151: }
152:
153: }
|