Changes for page The Plug-in Architecture of Eclipse
Last modified by cds on 2025/01/30 12:03
Summary
-
Page properties (1 modified, 0 added, 0 removed)
-
Objects (1 modified, 0 added, 0 removed)
Details
- Page properties
-
- Content
-
... ... @@ -137,10 +137,12 @@ 137 137 1. ((( 138 138 Your {{code language="none"}}TableViewPart{{/code}} contains a still empty method {{code language="none"}}createPartControl{{/code}}. This method will be responsible for creating the user interface components of your view. Add the following code to create the table we want to display: 139 139 140 -{{code title="createPartControl(...)" language="java"}}140 +{{code language="java"}} 141 141 Table table = new Table(parent, SWT.BORDER); 142 +table.setHeaderVisible(true); 142 142 TableColumn column = new TableColumn(table, SWT.NONE); 143 143 column.setWidth(80); 145 +column.setText("Tape Data"); 144 144 tableViewer = new TableViewer(table); 145 145 {{/code}} 146 146 ))) ... ... @@ -147,7 +147,7 @@ 147 147 1. ((( 148 148 The {{code language="none"}}setFocus{{/code}} method controls what happens when your part gets the focus. Make sure the focus will then automatically be set to the table by adding the following code: 149 149 150 -{{code title="setFocus(...)"}}152 +{{code}} 151 151 tableViewer.getControl().setFocus(); 152 152 {{/code}} 153 153 ))) ... ... @@ -172,7 +172,7 @@ 172 172 1. ((( 173 173 Create a class {{code language="none"}}TuringTape{{/code}} in a new package {{code language="none"}}de.cau.cs.rtprak.login.simple.model{{/code}} with the following fields: 174 174 175 -{{code title="Fields" language="java"}}177 +{{code language="java"}} 176 176 private int headPosition = 1; 177 177 private StringBuffer text = new StringBuffer(); 178 178 {{/code}} ... ... @@ -182,7 +182,7 @@ 182 182 1. ((( 183 183 Add two constants to the class: 184 184 185 -{{code title="Constants" language="java"}}187 +{{code language="java"}} 186 186 public static final char START_CHAR = '\u25b7'; 187 187 public static final char BLANK_CHAR = '\u25fb'; 188 188 {{/code}} ... ... @@ -198,7 +198,7 @@ 198 198 1*. The method {{code language="none"}}getElements(){{/code}} must return an array of objects, where each object must contain all necessary data to be displayed in a single row of the table. The number of returned objects corresponds to the number of rows. 199 199 1*. Suppose the input element is an instance of {{code language="none"}}TuringTape{{/code}}. The result of {{code language="none"}}getElements(){{/code}} shall be an array of {{code language="none"}}TapeData{{/code}} elements. The size of the array shall be one more than the maximum of the tape head position and the length of the tape text. The index and character of each tape data element shall be filled with {{code language="none"}}i{{/code}} and the result of {{code language="none"}}turingTape.getCharacter(i){{/code}}, respectively, where {{code language="none"}}i{{/code}} is the array index of the element. 200 200 1. Create a class {{code language="none"}}TapeLabelProvider{{/code}} in the {{code language="none"}}de.cau.cs.rtprak.login.simple.views{{/code}} package that extends [[BaseLabelProvider>>url:http://help.eclipse.org/juno/topic/org.eclipse.platform.doc.isv/reference/api/org/eclipse/jface/viewers/BaseLabelProvider.html||shape="rect"]] and implements [[ITableLabelProvider>>url:http://help.eclipse.org/juno/topic/org.eclipse.platform.doc.isv/reference/api/org/eclipse/jface/viewers/ITableLabelProvider.html||shape="rect"]].\\ 201 -1*. Add a private field tape of type {{code language="none"}}TuringTape{{/code}} that is initialized from the constructor. 203 +1*. Add a private field {{code language="none"}}tape{{/code}} of type {{code language="none"}}TuringTape{{/code}} that is initialized from the constructor. 202 202 1*. Add fields {{code language="none"}}presentImage{{/code}} and {{code language="none"}}absentImage{{/code}} of type [[Image>>url:http://help.eclipse.org/juno/topic/org.eclipse.platform.doc.isv/reference/api/org/eclipse/swt/graphics/Image.html||shape="rect"]]. 203 203 1*. ((( 204 204 Initialize each image using the following code, where {{code language="none"}}path_to_image{{/code}} is {{code language="none"}}icons/head_present.gif{{/code}} and {{code language="none"}}icons/head_absent.gif{{/code}}, respectively: ... ... @@ -207,7 +207,7 @@ 207 207 image = Activator.imageDescriptorFromPlugin(Activator.PLUGIN_ID, "path_to_image").createImage(); 208 208 {{/code}} 209 209 ))) 210 -1*. Override the implementation of {{code language="none"}}dispose(){{/code}} in {{code language="none"}}TapeLabelProvider{{/code}} to dispose both images after calling {{code language="none"}}super.dispose(){{/code}}. 212 +1*. Override the implementation of {{code language="none"}}dispose(){{/code}} in {{code language="none"}}TapeLabelProvider{{/code}} to dispose both images after calling {{code language="none"}}super.dispose(){{/code}}. (Right-click in the source-code and click //Source// -> //Override/Implement Methods//.) 211 211 1*. In {{code language="none"}}getColumnImage(){{/code}} and {{code language="none"}}getColumnText(){{/code}}, first check whether the element is an instance of {{code language="none"}}TapeData{{/code}} and the column index is 0, and return {{code language="none"}}null{{/code}} otherwise. If the check passes, return the following:\\ 212 212 1**. {{code language="none"}}getColumnImage(){{/code}}: {{code language="none"}}presentImage{{/code}} if the index given by the tape data element equals the current value of {{code language="none"}}tape.getHeadPosition(){{/code}}, {{code language="none"}}absentImage{{/code}} otherwise. 213 213 1**. {{code language="none"}}getColumnText(){{/code}}: a {{code language="none"}}String{{/code}} containing the character of the tape data element. ... ... @@ -221,12 +221,335 @@ 221 221 {{/code}} 222 222 ))) 223 223 224 - 226 +== Use Simple Text Editor as Tape View Input == 225 225 228 +We will now add code to make the Tape view display the content of a currently active Simple Text Editor. 229 + 230 +1. ((( 231 +Add the following methods to {{code language="none"}}SimpleEditorPart{{/code}}: 232 + 233 +{{code language="java"}} 234 +/** 235 + * Returns the text that is currently displayed in the editor. 236 + * @return the currently displayed text 237 + */ 238 +public String getText() { 239 + return getDocumentProvider().getDocument(getEditorInput()).get(); 240 +} 241 +/** The listener that is currently registered for this editor. */ 242 +private IDocumentListener registeredListener; 243 +/** 244 + * Registers the given runnable as listener for changes to the text 245 + * of this editor. 246 + * @param runnable a runnable to register as text listener 247 + */ 248 +public void registerTextListener(final Runnable runnable) { 249 + registeredListener = new IDocumentListener() { 250 + public void documentAboutToBeChanged(DocumentEvent event) {} 251 + public void documentChanged(DocumentEvent event) { 252 + runnable.run(); 253 + } 254 + }; 255 + getDocumentProvider().getDocument(getEditorInput()) 256 + .addDocumentListener(registeredListener); 257 +} 258 +/** 259 + * Removes the last registered text listener. 260 + */ 261 +public void disposeTextListener() { 262 + if (registeredListener != null) { 263 + if (getDocumentProvider() != null) { 264 + getDocumentProvider().getDocument(getEditorInput()) 265 + .removeDocumentListener(registeredListener); 266 + } 267 + registeredListener = null; 268 + } 269 +} 270 +{{/code}} 271 +))) 272 +1. ((( 273 +Add the following code to {{code language="none"}}TapeViewPart{{/code}}: 274 + 275 +{{code language="java"}} 276 +/** The editor part that is currently set as input for the viewer. */ 277 +private SimpleEditorPart currentInput; 278 +/** 279 + * Sets the displayed text of the given editor part as input of the 280 + * viewer, if the editor part is a SimpleEditorPart. 281 + * @param part workbench part to set as input 282 + */ 283 +private void setInput(final IWorkbenchPart part) { 284 + if (part instanceof SimpleEditorPart && part != currentInput) { 285 + if (currentInput != null) { 286 + currentInput.disposeTextListener(); 287 + } 288 + currentInput = (SimpleEditorPart) part; 289 + Runnable runnable = new Runnable() { 290 + public void run() { 291 + tape.setText(new StringBuffer(currentInput.getText())); 292 + tableViewer.refresh(); 293 + } 294 + }; 295 + runnable.run(); 296 + currentInput.registerTextListener(runnable); 297 + } 298 +} 299 +{{/code}} 300 +))) 301 +1. ((( 302 +Add the following code to {{code language="none"}}createPartControl(){{/code}}: 303 + 304 +{{code language="java"}} 305 +IWorkbenchWindow workbenchWindow = getSite().getWorkbenchWindow(); 306 +IWorkbenchPage activePage = workbenchWindow.getActivePage(); 307 +if (activePage != null) { 308 + setInput(activePage.getActivePart()); 309 +} 310 +workbenchWindow.getPartService().addPartListener(new IPartListener() { 311 + public void partActivated(final IWorkbenchPart part) { 312 + setInput(part); 313 + } 314 + public void partDeactivated(final IWorkbenchPart part) {} 315 + public void partBroughtToTop(final IWorkbenchPart part) {} 316 + public void partClosed(final IWorkbenchPart part) {} 317 + public void partOpened(final IWorkbenchPart part) {} 318 +}); 319 +{{/code}} 320 +))) 321 + 322 +== Create Actions to Move the Tape Head == 323 + 324 +If we want to add buttons to the view's tool bar, we will have to ask its [[IToolbarManager>>url:http://help.eclipse.org/juno/topic/org.eclipse.platform.doc.isv/reference/api/org/eclipse/jface/action/IToolBarManager.html||shape="rect"]] to do that for us: 325 + 326 +1. ((( 327 +Get the tool bar manager using the following code: 328 + 329 +{{code language="java"}} 330 +IToolBarManager toolBarManager = getViewSite().getActionBars().getToolBarManager(); 331 +{{/code}} 332 +))) 333 +1. Add two actions to the toolbar manager by extending the class [[Action>>url:http://help.eclipse.org/juno/topic/org.eclipse.platform.doc.isv/reference/api/org/eclipse/jface/action/Action.html||shape="rect"]] and implementing the {{code language="none"}}run(){{/code}}method. 334 +1*. It is convenient to add actions as anonymous nested classes. 335 +1*. The first action shall have the text "L". When it is run, it shall move the head to the left (to the top in the table viewer), if the head is not already at position 0. 336 +1*. The second action shall have the text "R". When it is run, it shall move the head to the right. 337 +1*. You should call {{code language="none"}}tableViewer.refresh(){{/code}} after any change to the {{code language="none"}}tape.headPosition{{/code}} variable. 338 + 339 +== Test the View == 340 + 341 +If you open an instance of the simple text editor and open the Tape view, the view should correctly display the editor's text on a tape, and the L and R buttons should move the tape head. 342 + 226 226 = Creating an Extension Point = 227 227 228 - WRITE THISSECTION345 +For the final part of the tutorial, we will now use the extension point mechanism of Eclipse to add some behavior to our Turing Machines. An //extension point// is basically a well-defined point where other plug-ins can register to add functionality. The extension point is basically defined by an XML Schema file that defines an interface; other plug-ins may access this interface using XML code in their {{code language="none"}}plugin.xml{{/code}} file, so-called //extensions//. Our extension point will provide an interface for classes that define behavior of a Turing Machine, and we will call them head controllers (programs that control the tape head). 229 229 347 +== Defining a Command Class == 348 + 349 +We will start by defining a class representing a command that will be passed to a selected head controller. 350 + 351 +1. Add a class {{code language="none"}}HeadCommand{{/code}} to the package {{code language="none"}}de.cau.cs.rtprak.login.simple.controller{{/code}}. 352 +1. Add a nested public static enumeration {{code language="none"}}Action{{/code}} with values {{code language="none"}}WRITE{{/code}}, {{code language="none"}}ERASE{{/code}}, and {{code language="none"}}NULL{{/code}}. 353 +1. Add a nested public static enumeration {{code language="none"}}Direction{{/code}} with values {{code language="none"}}LEFT{{/code}}, {{code language="none"}}RIGHT{{/code}}, and {{code language="none"}}NONE{{/code}}. 354 +1. ((( 355 +Add the following private fields: 356 + 357 +{{code language="java"}} 358 +private Action action; 359 +private Direction direction; 360 +private char newChar; 361 +{{/code}} 362 +))) 363 +1. Add a constructor to initialize the fields. 364 +1. Add getter methods to access the fields. 365 + 366 +== Defining the Controller Interface == 367 + 368 +We will now define an interface that all head controllers will have to implement: 369 + 370 +1. Add an interface {{code language="none"}}IHeadController{{/code}} in the package {{code language="none"}}de.cau.cs.rtprak.login.simple.controller{{/code}}. 371 +1. ((( 372 +Add the following methods to the interface: 373 + 374 +{{code language="java"}} 375 +/** 376 + * Calculate the next command depending on the currently seen character. 377 + * @param character the currently seen character 378 + * @return the next command specifying which character to write and 379 + * which direction to move the head 380 + */ 381 +HeadCommand nextCommand(char character); 382 + 383 +/** 384 + * Reset the internal state of the head controller. 385 + */ 386 +void reset(); 387 +{{/code}} 388 +))) 389 + 390 +== Defining the Extension Point == 391 + 392 +We will now define the extension point that head controllers will be registered at. 393 + 394 +1. Open the {{code language="none"}}plugin.xml{{/code}} file in the //Plugin Manifest Editor// and switch to the //Extension Points// tab. 395 +1. Click the //Add// button and enter {{code language="none"}}de.cau.cs.rtprak.login.simple.headControllers{{/code}} as the extension point's ID, and {{code language="none"}}Head Controllers{{/code}} as its name. Shorten the schema file's file name to {{code language="none"}}schema/headControllers.exsd{{/code}}. Make sure that //Edit extension point schema when done// is checked and click //Finish//. 396 +1. Eclipse will now have opened the new schema file in the //Extension Point Schema Editor//, a graphical editor similar to the //Plugin Manifest Editor// that provides a way to define things that might be easier than directly editing the text files. 397 +1. In the new editor, open the //Definition// tab. 398 +1. Add a new element named {{code language="none"}}controller{{/code}}. 399 +1. Add three new attributes to the {{code language="none"}}controller{{/code}} element:\\ 400 +1*. First attribute: name {{code language="none"}}id{{/code}}, use {{code language="none"}}required{{/code}}, type {{code language="none"}}string{{/code}}, translatable {{code language="none"}}false{{/code}}. 401 +1*. Second attribute: name {{code language="none"}}name{{/code}}, use {{code language="none"}}required{{/code}}, type {{code language="none"}}string{{/code}}, translatable {{code language="none"}}true{{/code}}. 402 +1*. Third attribute: name {{code language="none"}}class{{/code}}, use {{code language="none"}}required{{/code}}, type {{code language="none"}}java{{/code}}, implements {{code language="none"}}de.cau.cs.rtprak.login.simple.controller.IHeadController{{/code}}. This is the attribute that will tell us which Java class actually implements the controller that is to be registered at our extension point. To make sure that we know how to speak to the class, we require it to implement the interface we defined for head controllers. 403 +1. Add a sequence to the {{code language="none"}}extension{{/code}} element. Right-click the sequence and click //New// -> //controller//. Set the //Min Occurrences// of the sequence to 0, and set //Max Occurrences// to be //Unbounded//. 404 +1. Save the editor and switch back to the //Plugin Manifest Editor//. 405 +1. On the Runtime tab, add {{code language="none"}}de.cau.cs.rtprak.login.simple.controller{{/code}} to the list of packages exported by the plug-in. This is necessary because plug-ins that want to provide extensions for the extension point must provide a class that implements {{code language="none"}}IHeadController{{/code}}. For this to work, those plug-ins must have access to that interface; thus, we have to export the package containing it. 406 + 407 +== Accessing the Extension Point == 408 + 409 +We will now add a class that will be in charge of loading all extensions registered at our new extension point. 410 + 411 +1. ((( 412 +Add a class {{code language="none"}}HeadControllers{{/code}} to the package {{code language="none"}}de.cau.cs.rtprak.login.simple.controller{{/code}}. Add the following code: 413 + 414 +{{code language="java"}} 415 +/** 416 + * Class that gathers extension data from the 'headControllers' extension point 417 + * and publishes this data using the singleton pattern. 418 + * @author msp 419 + */ 420 +public class HeadControllers { 421 + /** Identifier of the extension point */ 422 + public final static String EXTENSION_POINT_ID = "de.cau.cs.rtprak.groupx.simple.headControllers"; 423 + /** The singleton instance of the {@code HeadControllers} class */ 424 + public final static HeadControllers INSTANCE = new HeadControllers(); 425 + /** list of head controller ids with associated names. */ 426 + private List<String[]> controllerNames = new LinkedList<String[]>(); 427 + /** map of controller ids to their runtime instances. */ 428 + private Map<String, IHeadController> controllerMap = new HashMap<String, IHeadController>(); 429 + /** 430 + * Creates an instance of this class and gathers extension data. 431 + */ 432 + HeadControllers() { 433 + IConfigurationElement[] elements = Platform.getExtensionRegistry() 434 + .getConfigurationElementsFor(EXTENSION_POINT_ID); 435 + for (IConfigurationElement element : elements) { 436 + if ("controller".equals(element.getName())) { 437 + String id = element.getAttribute("id"); 438 + String name = element.getAttribute("name"); 439 + if (id != null && name != null) { 440 + try { 441 + IHeadController controller = (IHeadController)element 442 + .createExecutableExtension("class"); 443 + controllerNames.add(new String[] {id, name}); 444 + controllerMap.put(id, controller); 445 + } 446 + catch (CoreException exception) { 447 + StatusManager.getManager().handle(exception, Activator.PLUGIN_ID); 448 + } 449 + } 450 + } 451 + } 452 + } 453 + 454 + /** 455 + * Returns a list of controller ids and names. The arrays in the list are 456 + * all of size 2: the first element is an id, and the second element is the 457 + * associated name. The controller name is a user-friendly string to be 458 + * displayed in the UI. 459 + * @return a list of controller ids and names 460 + */ 461 + public List<String[]> getControllerNames() { 462 + return controllerNames; 463 + } 464 + 465 + /** 466 + * Returns the head controller instance for the given id. 467 + * @param id identifier of a head controller 468 + * @return the associated controller 469 + */ 470 + public IHeadController getController(final String id) { 471 + return controllerMap.get(id); 472 + } 473 +} 474 +{{/code}} 475 +))) 476 + 477 +== Adding Support for Head Controllers to the View == 478 + 479 +We will now have to add support for head controllers to our view. 480 + 481 +1. Open the {{code language="none"}}TapeViewPart{{/code}} class and add the private fields {{code language="none"}}checkedControllerAction{{/code}} of type [[IAction>>url:http://help.eclipse.org/juno/topic/org.eclipse.platform.doc.isv/reference/api/org/eclipse/jface/action/IAction.html||shape="rect"]] and {{code language="none"}}currentController{{/code}} of type {{code language="none"}}IHeadController{{/code}}. 482 +1. ((( 483 +Add a list of registered head controllers to the view's menu (which can be opened using the small white triangle) in the {{code language="none"}}createPartControl(){{/code}} method: 484 + 485 +{{code language="java"}} 486 +IMenuManager menuManager = getViewSite().getActionBars().getMenuManager(); 487 +for (String[] controllerName : HeadControllers.INSTANCE.getControllerNames()) { 488 + final String id = controllerName[0]; 489 + String name = controllerName[1]; 490 + Action action = new Action(name, IAction.AS_RADIO_BUTTON) { 491 + public void run() { 492 + if (checkedControllerAction != null) { 493 + checkedControllerAction.setChecked(false); 494 + } 495 + this.setChecked(true); 496 + checkedControllerAction = this; 497 + currentController = HeadControllers.INSTANCE.getController(id); 498 + } 499 + }; 500 + if (checkedControllerAction == null) { 501 + action.run(); 502 + } 503 + menuManager.add(action); 504 +} 505 +{{/code}} 506 +))) 507 +1. ((( 508 +Implement the following method in the {{code language="none"}}TuringTape{{/code}} class: 509 + 510 +{{code language="java"}} 511 +public void execute(final IHeadController controller) 512 +{{/code}} 513 + 514 +The method shall have the following properties: 515 + 516 +\\ 517 + 518 +* Determine the character at the current head position using 519 + 520 +{{code language="none"}} 521 +getCharacter(getHeadPosition()) 522 +{{/code}}. 523 +* Call 524 + 525 +{{code language="none"}} 526 +controller.nextCommand() 527 +{{/code}} with the current character as parameter. 528 +* Depending on the action in the returned head command, either write the returned new character to the current position in text ( 529 + 530 +{{code language="none"}} 531 +WRITE 532 +{{/code}}), or write the blank symbol ( 533 + 534 +{{code language="none"}} 535 +ERASE 536 +{{/code}}), or do nothing. If the current position exceeds the end of the text, append enough blank characters up to the current position, then append the new character. 537 +* Depending on the direction in the returned head command, either move the head to the left (but no further than position 0), or to the right, or do nothing. 538 +))) 539 +1. Copy the files [[attach:step.gif]]and [[attach:reset.gif]]to the icons folder. 540 +1. Add an action to the toolbar of the Tape view with text {{code language="none"}}Step{{/code}} and icon {{code language="none"}}step.png{{/code}} which does the following:\\ 541 +1*. Check whether the current head controller is not {{code language="none"}}null{{/code}}, than call {{code language="none"}}tape.execute(currentController){{/code}}. 542 +1*. Refresh the table viewer with its {{code language="none"}}refresh(){{/code}} method. 543 +1*. ((( 544 +Note: actions don't need images, but only image descriptors. Thus, to set the action's icon to {{code language="none"}}step.png{{/code}}, you can use something like the following: 545 + 546 +{{code language="java"}} 547 +Activator.imageDescriptorFromPlugin(Activator.PLUGIN_ID, "path_to_icon"); 548 +{{/code}} 549 +))) 550 +1. Add another action with text Reset and icon reset.png which does the following:\\ 551 +1*. Check whether the current head controller is not {{code language="none"}}null{{/code}}, then call the {{code language="none"}}reset(){{/code}} method on {{code language="none"}}currentController{{/code}}. 552 +1*. Set the current head position to 1. 553 +1*. Refresh the table viewer with its {{code language="none"}}refresh(){{/code}} method. 554 + 230 230 231 231 232 232 ... ... @@ -234,3 +234,25 @@ 234 234 235 235 236 236 562 + 563 + 564 + 565 + 566 + 567 + 568 + 569 + 570 + 571 + 572 + 573 + 574 + 575 + 576 + 577 + 578 + 579 + 580 + 581 + 582 + 583 +
- Confluence.Code.ConfluencePageClass[0]
-
- Id
-
... ... @@ -1,1 +1,1 @@ 1 -2982 2951 +2982314 - URL
-
... ... @@ -1,1 +1,1 @@ 1 -https://rtsys.informatik.uni-kiel.de/confluence//wiki/spaces/WS12EclPract/pages/2982 295/The Plug-in Architecture of Eclipse1 +https://rtsys.informatik.uni-kiel.de/confluence//wiki/spaces/WS12EclPract/pages/2982314/The Plug-in Architecture of Eclipse