eclipse plugin development part 2

Upload: sumio-tasaka

Post on 11-Jul-2015

132 views

Category:

Documents


0 download

DESCRIPTION

田坂澄生, Eclipse プラグイン開発, JavaWORLD, 2004 3 月, pp.36-77

TRANSCRIPT

Part 2Eclipse UI org.eclipse. ui.xxxxxx UI Eclipse 2.1.2 Java 2 SDK 1.4.2 Windows 2000

Tutorial for Eclipse Plugin Development

Part 1 Eclipse

1 UI org.eclipse.ui.IWorkbenchWindow org.eclipse.ui.IWorkbenchPage implements

1

2004 March

JavaWorld 045

IWorkbenchPage 1 IWorkbenchPage openEditor org.eclipse.ui.IViewPart o r g . e c l i p se.ui.IEditorPart IWorkbenchPage getViewsgetEditors

org.eclipse.ui.part.ViewPart

Part 1 Part 1

Part 12

Eclipse

org.eclipse.ui.views API IViewPart 1IWorkbenchPage

1 com.samples.myview 2 ID Java Part 1 3 plugin.xml Java

IWorkbench workbench = PlatformUI.getWorkbench(); IWorkbenchWindow workbenchWindow = workbench.getActiveWorkbenchWindow(); IWorkbenchPage page = workbenchWindow.getActivePage(); try { page.openEditor(file); } catch (PartInitException e) { e.printStackTrace(); }

3

046 JavaWorld

2004 March

Part 2 Tutorial for Eclipse Plugin Development1 2 3

4

5

org.eclipse.ui.views 4 plugin.xml 5 org.eclipse.ui.views Part14 ID

org.eclipse.ui.views org.eclipse.ui.views categoryview Part 1 5 plug in.xml XML category category id com.samples. myview name UI

2004 March

JavaWorld 047

MyView 6 view view id com.sam ples.myview.simpleview name UI category

Java SimpleView Part 1 6 Java

SimpleView. java Myview plugin.xml plugin.xml 2 plugin ID runtime JAR 2 plugin.xml

Eclipse

com.samples.myview class Java

6category

048 JavaWorld

2004 March

Part 2 Tutorial for Eclipse Plugin Development

requires ID extension org.eclipse.ui.views point org.eclipse.ui.views org.eclipse.ui.views categoryview category name id parentCategory / ID view name UI id category class IViewPartimplements org.eclipse.ui.part.ViewPart icon fastViewWidthRatio

SimpleView. java 3 SimpleView createPart Control org.eclipse.swt.widgets.Label parent org.eclipse.swt.widgets.Composite setLayout SimpleView org. eclipse.ui.ISelectionListenerimplements 3SimpleView SimpleView.java

package com.samples.myview; import import import import import import import import import org.eclipse.core.resources.IFile; org.eclipse.jface.viewers.ISelection; org.eclipse.jface.viewers.IStructuredSelection; org.eclipse.swt.SWT; org.eclipse.swt.widgets.Composite; org.eclipse.swt.widgets.Label; org.eclipse.ui.ISelectionListener; org.eclipse.ui.IWorkbenchPart; org.eclipse.ui.part.ViewPart;

public class SimpleView extends ViewPart implements ISelectionListener { private Label label; public SimpleView() { super(); } public void createPartControl(Composite parent) { label = new Label(parent, SWT.WRAP); label.setText(""); getViewSite().getPage().addSelectionListener(this); selectionChanged( null, getSite().getPage().getSelection()); } public void setFocus() { label.setFocus(); } public void selectionChanged( IWorkbenchPart part, ISelection selection) { if (selection != null && selection instanceof IStructuredSelection) { IStructuredSelection structured = (IStructuredSelection)selection; Object o = structured.getFirstElement(); if (o != null && o instanceof IFile) { IFile file = (IFile)o; label.setText("" + file.getName()); return; } } label.setText(""); } }

Java SimpleView

2004 March

JavaWorld 049

selectionChanged

Part 17 Eclipse MyView

Eclipse 1

org.eclipse.ui.action Sets API org.eclipse.ui.IWorkbenchWind owActionDelegateorg.eclipse.ui.IWork benchWindowPulldownDelegate 9

Eclipse

7 MyView OK 8 web.xml 7

8

1 org.eclipse.ui.actionSets org.eclipse.ui.viewActions org.eclipse.ui.popupMenus org.eclipse.ui.editorActions

050 JavaWorld

2004 March

Part 2 Tutorial for Eclipse Plugin Development

com.samples.myactionset plugin.xml org.eclipse. ui.actionSets

9

plugin.xml extension actio nSet org.eclipse.ui.actionSets actionSet label Eclipse label id com.samples.myactionset

t a r g e t I D c o m . s a m p l e s . myactionset com.samples.myactionset part id org.ecli pse.ui.views.ResourceNavigator menu 4 group1 option1 actionSet actionSetaction UI 5 1

actionSet visible visible org.eclipse.ui.actionSetPartA ssociations 4menu

2004 March

JavaWorld 051

plugin.xml 9 plugin.xmlgroup1 5 style toggle enablesFor 1 selection 5- java 1 6 style push

java selectionenablement enablement selection visibility enablement actionSet 3 action 7 style radio 1 menubarPath testmenu/group1 testmenu/option1

Eclipse

enablement 5

org.eclipse. ui.IWorkbenchActionConstants menumenubarPath window/ additional menu 7

6

052 JavaWorld

2004 March

Part 2 Tutorial for Eclipse Plugin Development

barPath window/testmenu/xxx plugin.xml org.eclipse.ui.actionSets T P class CD-ROM IWorkbenchWindowActionDelegate implements run ToggleActionDelegate 8

org.eclipse.ui. viewActionsplugin.xml 10 2 com.sam ples.myviewaction org.eclipse.ui. viewActions viewContribution viewContributiontargetID

10

8 ToggleActionDelegate run

public void run(IAction action) { MessageDialog.openInformation( window.getShell(), "MyActionSet", "");

2004 March

JavaWorld 053

ID ID org.eclipse.ui. views.ResourceNavigator viewContribution menuaction menuaction menu action CDROMplugin.xml class org.eclipse.ui.IViewActionDelegate implements

plugin.xml objectContribution Java viewerContribution com.samples.mypopu pmenus org.eclipse.ui.popupMenus 2 objectContribution plugin. xml 9 objectContribution objectClass org.eclipse.core.resources.IFile nameFilter *.java java 9objectContribution

Eclipse

class run plugin.xml java

org.eclipse.ui.pop upMenus

054 JavaWorld

2004 March

Part 2 Tutorial for Eclipse Plugin Development

menu action actionclass com.samples.mypopupmenus. PopupMenuActionDelegate org.eclipse.ui.IObjectAction Delegateimplements PopupMenuActionDelegate MessageDialog run Java 11 viewerContribution plugin.xml 10 viewerContribution targetID ID ID org.eclipse.ui.views.TaskList actionclass IViewActionDelegateimplements 10viewerContribution

11

12

org.eclipse. ui.IEditorActionDelegateimplements org.eclipse.ui.viewActions IViewActionDelegate

2004 March

JavaWorld 055

13

2

org.eclipse.ui.perspectives 13 com. samples.myperspective

org.eclipse.ui. perspectives plugin.xml extension 11 org.eclipse.ui.perspectives extension perspective name class org.eclipse.ui.IPerspective Factoryimplements implements MyPers pectiveFactory createInitialLayout

Eclipse

12 3 org.eclipse.ui.editorActions

JDTPDE Java

createInitialL ayout 12 getEditorArea 11 extension

056 JavaWorld

2004 March

Part 2 Tutorial for Eclipse Plugin Development

editor area createFolder 12- createFolder String folderId ID left int relationship refID IPageLayout.LEFT float ratio 0.3f String refId ID editorArea add View org.eclipse. ui.IPageLayout addNewWizardShor tcut/addShowViewShortcut createInitialLayout addNewWizardShortcut addShowViewShortcut

14

org.eclipse.ui.perspectiveExtensions com.samples.myperspective org.eclipse.ui. 12 MyPerspectiveFactory createInitialLayout

public void createInitialLayout(IPageLayout layout) { // String editorArea = layout.getEditorArea(); // IFolderLayout left = layout.createFolder("left", IPageLayout.LEFT, 0.3f, editorArea); // left.addView(IPageLayout.ID_RES_NAV); left.addView(IPageLayout.ID_OUTLINE); // layout.addNewWizardShortcut("org.eclipse.ui.wizards.new.file"); // layout.addShowViewShortcut(IPageLayout.ID_RES_NAV); layout.addShowViewShortcut(IPageLayout.ID_OUTLINE); layout.addShowViewShortcut(IPageLayout.ID_TASK_LIST); }

2004 March

JavaWorld 057

15

perspectiveExtensions plugin.xmlperspectiveExt ension targetID org.eclipse.ui. resourcePerspective 13 actionSet id ID ID org.eclipse.de bug.ui.launchActionSet 14 viewShortcut

16

id ID ID org.eclipse.jdt.ui.PackageExplorer 15 newWizardShortcut id org. eclipse.pde.ui.NewProjectWizard 16 view relative

13perspectiveExtension

relationship 2 relationship stack 17

058 JavaWorld

2004 March

Part 2 Tutorial for Eclipse Plugin Development

Eclipse IPage Layout ID 1

17

Eclipse org.eclipse.ui.newWizards org.eclipse.ui.importWizards org.eclipse.ui.exportWizards org.eclip se.jface.wizard.WizardDialog org.eclipse.jface.wizard. IWizard org.eclipse.jface.wizard.IWizard Page 3 1 18

com.samples. mywizard org.eclipse. ui.newWizards plugin.xmlcategory

2004 March

JavaWorld 059

18

IWizardPage imple ments org.eclipse.ui.dialogs

name parentCategory wizard 14 name name icon description

2 WizardNew FileCreationPage 15 Text FileCreatePage TextFileCreatePage 15- WizardPage setDescription setErrorMessage setImageDescriptor setMessage setPageComplete setPreviousPage setTitle 15- setTitle setDescription TextFileCreatePage getInitialContents 15-

Eclipse

18 category ID selection class class Java NewText FileWizard plugin.xml NewTextFileWizard

14wizard

2 WizardNewFileCreationPage WizardNewFolderMainPage WizardNewProjectCreationPage WizardImportPage WizardExportPage

060 JavaWorld

2004 March

Part 2 Tutorial for Eclipse Plugin Development

finish 15- NewTextFileWizard perform Finish TextFileCreatePage NewTextFileWizard Java performFinish init init 16- IWorkbench IStructuredSelection workbenchselection Wizard addPages addPages TextFile C r e a t e P a g e addPage 16- performFinish 16- TextFileCreatePage finish NewTextFileWizard 19

15 TextFileCreatePage

public class TextFileCreatePage extends WizardNewFileCreationPage { private IWorkbench workbench; public TextFileCreatePage( IWorkbench workbench, IStructuredSelection selection) { super("TextFileCreatePage1", selection); this.setTitle(""); this.setDescription(""); this.workbench = workbench; } protected InputStream getInitialContents() { StringBuffer strbuf = new StringBuffer(); strbuf.append(getFileName()); return new ByteArrayInputStream(strbuf.toString().getBytes()); } public boolean finish() { IFile newFile = createNewFile(); if (newFile == null) return false; try { IWorkbenchWindow dwindow = workbench.getActiveWorkbenchWindow(); IWorkbenchPage page = dwindow.getActivePage(); if (page != null) page.openEditor(newFile); } catch (PartInitException e) { e.printStackTrace(); return false; } return true; } }

16 NewTextFileWizard

public class NewTextFileWizard extends Wizard implements INewWizard { private TextFileCreatePage mainPage; private IStructuredSelection selection; private IWorkbench workbench; public NewTextFileWizard() {} public void init( IWorkbench workbench, IStructuredSelection selection) { this.workbench = workbench; this.selection = selection; setWindowTitle(""); } public void addPages() { mainPage = new TextFileCreatePage(workbench, selection); addPage(mainPage); } public boolean performFinish() { return mainPage.finish(); } }

19

2004 March

JavaWorld 061