|
Added
Link Here
|
| 1 |
/******************************************************************************* |
| 2 |
* Copyright (c) 2007 IBM Corporation and others. |
| 3 |
* All rights reserved. This program and the accompanying materials |
| 4 |
* are made available under the terms of the Eclipse Public License v1.0 |
| 5 |
* which accompanies this distribution, and is available at |
| 6 |
* http://www.eclipse.org/legal/epl-v10.html |
| 7 |
* |
| 8 |
* Contributors: |
| 9 |
* IBM Corporation - initial API and implementation |
| 10 |
*******************************************************************************/ |
| 11 |
|
| 12 |
package org.eclipse.pde.internal.ui.editor.ctxhelp; |
| 13 |
|
| 14 |
import java.util.*; |
| 15 |
import java.util.List; |
| 16 |
import org.eclipse.core.resources.*; |
| 17 |
import org.eclipse.core.runtime.IPath; |
| 18 |
import org.eclipse.core.runtime.Path; |
| 19 |
import org.eclipse.jface.action.*; |
| 20 |
import org.eclipse.jface.dialogs.MessageDialog; |
| 21 |
import org.eclipse.jface.viewers.*; |
| 22 |
import org.eclipse.pde.core.IModelChangedEvent; |
| 23 |
import org.eclipse.pde.internal.core.ictxhelp.ICtxHelpConstants; |
| 24 |
import org.eclipse.pde.internal.core.text.IDocumentElementNode; |
| 25 |
import org.eclipse.pde.internal.core.text.ctxhelp.*; |
| 26 |
import org.eclipse.pde.internal.ui.PDEPlugin; |
| 27 |
import org.eclipse.pde.internal.ui.PDEUIMessages; |
| 28 |
import org.eclipse.pde.internal.ui.editor.*; |
| 29 |
import org.eclipse.pde.internal.ui.editor.actions.CollapseAction; |
| 30 |
import org.eclipse.pde.internal.ui.editor.plugin.FormFilteredTree; |
| 31 |
import org.eclipse.pde.internal.ui.editor.toc.HelpEditorUtil; |
| 32 |
import org.eclipse.pde.internal.ui.parts.TreePart; |
| 33 |
import org.eclipse.pde.internal.ui.util.PDELabelUtility; |
| 34 |
import org.eclipse.swt.SWT; |
| 35 |
import org.eclipse.swt.dnd.*; |
| 36 |
import org.eclipse.swt.events.DisposeEvent; |
| 37 |
import org.eclipse.swt.events.DisposeListener; |
| 38 |
import org.eclipse.swt.graphics.Cursor; |
| 39 |
import org.eclipse.swt.widgets.*; |
| 40 |
import org.eclipse.ui.PartInitException; |
| 41 |
import org.eclipse.ui.PlatformUI; |
| 42 |
import org.eclipse.ui.actions.ActionFactory; |
| 43 |
import org.eclipse.ui.actions.ContributionItemFactory; |
| 44 |
import org.eclipse.ui.dialogs.PatternFilter; |
| 45 |
import org.eclipse.ui.forms.widgets.FormToolkit; |
| 46 |
import org.eclipse.ui.forms.widgets.Section; |
| 47 |
import org.eclipse.ui.ide.IDE; |
| 48 |
import org.eclipse.ui.keys.IBindingService; |
| 49 |
|
| 50 |
/** |
| 51 |
* TocTreeSection - The section that displays the TOC |
| 52 |
* tree structure and any buttons used to manipulate it. |
| 53 |
* This is the main section that the user will interact |
| 54 |
* with the TOC through. |
| 55 |
*/ |
| 56 |
public class CtxHelpTreeSection extends TreeSection { |
| 57 |
private CtxHelpModel fModel; |
| 58 |
private TreeViewer fTree; |
| 59 |
private FormFilteredTree fFilteredTree; |
| 60 |
|
| 61 |
/* The indices for each button attached to the Tree Viewer. |
| 62 |
* This type of UI form does not permit direct access to each particular |
| 63 |
* button. However, using these indices, one can perform any typical SWT |
| 64 |
* operation on any button. |
| 65 |
*/ |
| 66 |
private static final int F_BUTTON_ADD_CONTEXT = 0; |
| 67 |
private static final int F_BUTTON_ADD_TOPIC = 2; |
| 68 |
private static final int F_BUTTON_ADD_COMMAND = 3; |
| 69 |
private static final int F_BUTTON_REMOVE = 5; |
| 70 |
private static final int F_BUTTON_UP = 6; |
| 71 |
private static final int F_BUTTON_DOWN = 7; |
| 72 |
|
| 73 |
private static final int F_UP_FLAG = -1; |
| 74 |
private static final int F_DOWN_FLAG = 1; |
| 75 |
|
| 76 |
// The action that collapses down the TOC tree |
| 77 |
private CollapseAction fCollapseAction; |
| 78 |
|
| 79 |
// The actions that will add each type of TOC object |
| 80 |
private CtxHelpAbstractAddAction fAddContextAction; |
| 81 |
private CtxHelpAbstractAddAction fAddTopicAction; |
| 82 |
private CtxHelpAbstractAddAction fAddCommandAction; |
| 83 |
|
| 84 |
// The object removal action |
| 85 |
private CtxHelpRemoveAction fRemoveObjectAction; |
| 86 |
|
| 87 |
// The action for opening a link from the context menu |
| 88 |
private OpenLinkAction fOpenLinkAction; |
| 89 |
|
| 90 |
// The adapter that will listen for drag events in the tree |
| 91 |
private CtxHelpDragAdapter fDragAdapter; |
| 92 |
|
| 93 |
/** If items are dragged and dropped within this tree, then |
| 94 |
* this flag inhibits reselection on the removal (drag) action, |
| 95 |
* thus ensuring that the selected objects are the ones that were |
| 96 |
* dropped. |
| 97 |
*/ |
| 98 |
private boolean fDragFromHere; |
| 99 |
|
| 100 |
class OpenLinkAction extends Action { |
| 101 |
|
| 102 |
private CtxHelpTopic fOpenTarget; |
| 103 |
|
| 104 |
public OpenLinkAction() { |
| 105 |
super(PDEUIMessages.Actions_open_label); |
| 106 |
} |
| 107 |
|
| 108 |
public void setTarget(CtxHelpTopic target) { |
| 109 |
fOpenTarget = target; |
| 110 |
} |
| 111 |
|
| 112 |
public void run() { |
| 113 |
if (fOpenTarget != null) { |
| 114 |
open(fOpenTarget); |
| 115 |
} |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
/** |
| 120 |
* Constructs a new TOC tree section. |
| 121 |
* |
| 122 |
* @param formPage The page that will hold this new tree section |
| 123 |
* @param parent The parent composite in the page that will contain the section widgets |
| 124 |
*/ |
| 125 |
public CtxHelpTreeSection(PDEFormPage formPage, Composite parent) { |
| 126 |
|
| 127 |
/* Create a new section with a description area, and some buttons. |
| 128 |
* The null entries in the String array will become blank space |
| 129 |
* separators between the buttons. |
| 130 |
*/ |
| 131 |
super(formPage, parent, Section.DESCRIPTION, new String[] {"Add Context", null, "Add Topic", "Add Command", null, PDEUIMessages.TocPage_remove, PDEUIMessages.TocPage_up, PDEUIMessages.TocPage_down}); |
| 132 |
} |
| 133 |
|
| 134 |
/* (non-Javadoc) |
| 135 |
* @see org.eclipse.pde.internal.ui.editor.PDESection#createClient(org.eclipse.ui.forms.widgets.Section, org.eclipse.ui.forms.widgets.FormToolkit) |
| 136 |
*/ |
| 137 |
protected void createClient(Section section, FormToolkit toolkit) { |
| 138 |
// Get the model |
| 139 |
fModel = (CtxHelpModel) getPage().getModel(); |
| 140 |
|
| 141 |
// Create a container in the section |
| 142 |
Composite container = createClientContainer(section, 2, toolkit); |
| 143 |
// Create a TOC tree in the new container |
| 144 |
createTree(container, toolkit); |
| 145 |
toolkit.paintBordersFor(container); |
| 146 |
section.setText(PDEUIMessages.TocTreeSection_title); |
| 147 |
section.setDescription(PDEUIMessages.TocTreeSection_sectionDescription); |
| 148 |
section.setClient(container); |
| 149 |
|
| 150 |
createCommands(); |
| 151 |
|
| 152 |
initializeTreeViewer(); |
| 153 |
createSectionToolbar(section, toolkit); |
| 154 |
|
| 155 |
// Create the adapted listener for the filter entry field |
| 156 |
fFilteredTree.createUIListenerEntryFilter(this); |
| 157 |
} |
| 158 |
|
| 159 |
private void createCommands() { |
| 160 |
fAddContextAction = new CtxHelpAbstractAddAction() { |
| 161 |
public void run() { |
| 162 |
if (fParentObject != null) { |
| 163 |
CtxHelpContext context = fParentObject.getModel().getFactory().createContext(); |
| 164 |
String id = PDELabelUtility.generateName(getChildNames(), "Help_Context"); |
| 165 |
context.setID(id); |
| 166 |
addChild(context); |
| 167 |
} |
| 168 |
} |
| 169 |
}; |
| 170 |
fAddContextAction.setText("Context"); |
| 171 |
fAddTopicAction = new CtxHelpAbstractAddAction() { |
| 172 |
public void run() { |
| 173 |
if (fParentObject != null) { |
| 174 |
CtxHelpTopic topic = fParentObject.getModel().getFactory().createTopic(); |
| 175 |
String label = PDELabelUtility.generateName(getChildNames(), "Help_Topic"); |
| 176 |
topic.setLabel(label); |
| 177 |
addChild(topic); |
| 178 |
} |
| 179 |
} |
| 180 |
}; |
| 181 |
fAddTopicAction.setText("Topic"); |
| 182 |
fAddCommandAction = new CtxHelpAbstractAddAction() { |
| 183 |
public void run() { |
| 184 |
if (fParentObject != null) { |
| 185 |
CtxHelpCommand command = fParentObject.getModel().getFactory().createCommand(); |
| 186 |
String label = PDELabelUtility.generateName(getChildNames(), "Help_Command"); |
| 187 |
command.setLabel(label); |
| 188 |
addChild(command); |
| 189 |
} |
| 190 |
} |
| 191 |
}; |
| 192 |
fAddCommandAction.setText("Command"); |
| 193 |
|
| 194 |
fRemoveObjectAction = new CtxHelpRemoveAction(); |
| 195 |
|
| 196 |
fOpenLinkAction = new OpenLinkAction(); |
| 197 |
} |
| 198 |
|
| 199 |
/** |
| 200 |
* Adds a link (with hand cursor) for tree 'Collapse All' action, |
| 201 |
* which collapses the TOC tree down to the second level |
| 202 |
* |
| 203 |
* @param section The section that the toolbar will belong to |
| 204 |
* @param toolkit The toolkit that will be used to make the toolbar |
| 205 |
*/ |
| 206 |
private void createSectionToolbar(Section section, FormToolkit toolkit) { |
| 207 |
ToolBarManager toolBarManager = new ToolBarManager(SWT.FLAT); |
| 208 |
ToolBar toolbar = toolBarManager.createControl(section); |
| 209 |
|
| 210 |
final Cursor handCursor = new Cursor(Display.getCurrent(), SWT.CURSOR_HAND); |
| 211 |
toolbar.setCursor(handCursor); |
| 212 |
// Cursor needs to be explicitly disposed |
| 213 |
toolbar.addDisposeListener(new DisposeListener() { |
| 214 |
public void widgetDisposed(DisposeEvent e) { |
| 215 |
if ((handCursor != null) && (handCursor.isDisposed() == false)) { |
| 216 |
handCursor.dispose(); |
| 217 |
} |
| 218 |
} |
| 219 |
}); |
| 220 |
|
| 221 |
// Add collapse action to the tool bar |
| 222 |
fCollapseAction = new CollapseAction(fTree, PDEUIMessages.ExtensionsPage_collapseAll, 1, fModel.getCtxHelpRoot()); |
| 223 |
toolBarManager.add(fCollapseAction); |
| 224 |
|
| 225 |
toolBarManager.update(true); |
| 226 |
section.setTextClient(toolbar); |
| 227 |
} |
| 228 |
|
| 229 |
/** |
| 230 |
* Create the tree widget that will contain the TOC |
| 231 |
* |
| 232 |
* @param container The container of the tree widget |
| 233 |
* @param toolkit The toolkit used to create the tree |
| 234 |
*/ |
| 235 |
private void createTree(Composite container, FormToolkit toolkit) { |
| 236 |
TreePart treePart = getTreePart(); |
| 237 |
createViewerPartControl(container, SWT.MULTI, 2, toolkit); |
| 238 |
|
| 239 |
fTree = treePart.getTreeViewer(); |
| 240 |
fTree.setContentProvider(new CtxHelpContentProvider()); |
| 241 |
fTree.setLabelProvider(PDEPlugin.getDefault().getLabelProvider()); |
| 242 |
|
| 243 |
PDEPlugin.getDefault().getLabelProvider().connect(this); |
| 244 |
|
| 245 |
createTreeListeners(); |
| 246 |
initDragAndDrop(); |
| 247 |
} |
| 248 |
|
| 249 |
/** |
| 250 |
* Initialize the section's drag and drop capabilities |
| 251 |
*/ |
| 252 |
private void initDragAndDrop() { |
| 253 |
int ops = DND.DROP_COPY; |
| 254 |
if (isEditable()) { |
| 255 |
ops |= DND.DROP_MOVE; |
| 256 |
} |
| 257 |
|
| 258 |
//Content dragged from the tree viewer can be treated as model objects (TocObjects) |
| 259 |
//or as text (XML representation of the TocObjects) |
| 260 |
Transfer[] dragTransfers = new Transfer[] {ModelDataTransfer.getInstance(), TextTransfer.getInstance()}; |
| 261 |
fDragAdapter = new CtxHelpDragAdapter(this); |
| 262 |
fTree.addDragSupport(ops, dragTransfers, fDragAdapter); |
| 263 |
|
| 264 |
if (isEditable()) { //Model objects and files can be dropped onto the viewer |
| 265 |
//TODO: Consider allowing drops/pastes of pure XML text |
| 266 |
Transfer[] dropTransfers = new Transfer[] {ModelDataTransfer.getInstance(), FileTransfer.getInstance()}; |
| 267 |
fTree.addDropSupport(ops | DND.DROP_DEFAULT, dropTransfers, new CtxHelpDropAdapter(fTree, this)); |
| 268 |
} |
| 269 |
} |
| 270 |
|
| 271 |
/** |
| 272 |
* Create the action listeners for the tree. |
| 273 |
*/ |
| 274 |
private void createTreeListeners() { |
| 275 |
// Create listener for the outline view 'link with editor' toggle button |
| 276 |
fTree.addPostSelectionChangedListener(getPage().getPDEEditor().new PDEFormEditorChangeListener()); |
| 277 |
} |
| 278 |
|
| 279 |
/** |
| 280 |
* Initialize the tree viewer widget and its buttons. |
| 281 |
*/ |
| 282 |
private void initializeTreeViewer() { |
| 283 |
if (fModel == null) { |
| 284 |
return; |
| 285 |
} |
| 286 |
|
| 287 |
CtxHelpRoot root = fModel.getCtxHelpRoot(); |
| 288 |
fTree.setInput(root); |
| 289 |
|
| 290 |
// Buttons must be disabled if file is ineditable |
| 291 |
getTreePart().setButtonEnabled(F_BUTTON_ADD_CONTEXT, isEditable()); |
| 292 |
getTreePart().setButtonEnabled(F_BUTTON_ADD_COMMAND, isEditable()); |
| 293 |
getTreePart().setButtonEnabled(F_BUTTON_ADD_TOPIC, isEditable()); |
| 294 |
|
| 295 |
// Set to false because initial node selected is the root TOC node |
| 296 |
getTreePart().setButtonEnabled(F_BUTTON_REMOVE, false); |
| 297 |
// Set to false because initial node selected is the root TOC node |
| 298 |
getTreePart().setButtonEnabled(F_BUTTON_UP, false); |
| 299 |
// Set to false because initial node selected is the root TOC node |
| 300 |
getTreePart().setButtonEnabled(F_BUTTON_DOWN, false); |
| 301 |
|
| 302 |
//Initially, the root TOC element is selected |
| 303 |
// fTree.setSelection(new StructuredSelection(root), true); |
| 304 |
// fTree.expandToLevel(0); |
| 305 |
} |
| 306 |
|
| 307 |
/* (non-Javadoc) |
| 308 |
* @see org.eclipse.ui.forms.AbstractFormPart#setFormInput(java.lang.Object) |
| 309 |
*/ |
| 310 |
public boolean setFormInput(Object object) { |
| 311 |
// This method allows the outline view to select items in the tree |
| 312 |
// (Invoked by org.eclipse.ui.forms.editor.IFormPage.selectReveal(Object object)) |
| 313 |
|
| 314 |
if (object instanceof CtxHelpObject) { // Select the item in the tree |
| 315 |
fTree.setSelection(new StructuredSelection(object), true); |
| 316 |
|
| 317 |
// Verify that something was actually selected |
| 318 |
ISelection selection = fTree.getSelection(); |
| 319 |
if (selection != null && !selection.isEmpty()) { |
| 320 |
return true; |
| 321 |
} |
| 322 |
} |
| 323 |
|
| 324 |
return false; |
| 325 |
} |
| 326 |
|
| 327 |
/** |
| 328 |
* @return the selection of the tree section |
| 329 |
*/ |
| 330 |
public ISelection getSelection() { |
| 331 |
return fTree.getSelection(); |
| 332 |
} |
| 333 |
|
| 334 |
/** |
| 335 |
* @param selection the new selection for the tree section |
| 336 |
*/ |
| 337 |
public void setSelection(ISelection selection) { |
| 338 |
fTree.setSelection(selection); |
| 339 |
} |
| 340 |
|
| 341 |
/** |
| 342 |
* Fire a selection change event and refresh the viewer's selection |
| 343 |
*/ |
| 344 |
public void fireSelection() { |
| 345 |
fTree.setSelection(fTree.getSelection()); |
| 346 |
} |
| 347 |
|
| 348 |
/* (non-Javadoc) |
| 349 |
* @see org.eclipse.pde.internal.ui.editor.TreeSection#selectionChanged(org.eclipse.jface.viewers.IStructuredSelection) |
| 350 |
*/ |
| 351 |
protected void selectionChanged(IStructuredSelection selection) { |
| 352 |
getPage().getPDEEditor().setSelection(selection); |
| 353 |
updateButtons(); |
| 354 |
} |
| 355 |
|
| 356 |
/** |
| 357 |
* Update the buttons in the section based on the current selection |
| 358 |
*/ |
| 359 |
public void updateButtons() { |
| 360 |
|
| 361 |
if (!fModel.isEditable()) { |
| 362 |
return; |
| 363 |
} |
| 364 |
|
| 365 |
IStructuredSelection selection = (IStructuredSelection) fTree.getSelection(); |
| 366 |
CtxHelpObject firstSelectedObject = (CtxHelpObject) selection.getFirstElement(); |
| 367 |
|
| 368 |
// Add Context |
| 369 |
getTreePart().setButtonEnabled(F_BUTTON_ADD_CONTEXT, true); |
| 370 |
|
| 371 |
// Add Topic |
| 372 |
boolean enableAdd = false; |
| 373 |
if (firstSelectedObject != null) { |
| 374 |
if (firstSelectedObject.canAddSibling(ICtxHelpConstants.TYPE_TOPIC)) { |
| 375 |
enableAdd = true; |
| 376 |
} else if (firstSelectedObject.canAddChild(ICtxHelpConstants.TYPE_TOPIC)) { |
| 377 |
enableAdd = true; |
| 378 |
} |
| 379 |
} |
| 380 |
getTreePart().setButtonEnabled(F_BUTTON_ADD_TOPIC, enableAdd); |
| 381 |
|
| 382 |
// Add Command |
| 383 |
enableAdd = false; |
| 384 |
if (firstSelectedObject != null) { |
| 385 |
if (firstSelectedObject.canAddSibling(ICtxHelpConstants.TYPE_COMMAND)) { |
| 386 |
enableAdd = true; |
| 387 |
} else if (firstSelectedObject.canAddChild(ICtxHelpConstants.TYPE_COMMAND)) { |
| 388 |
enableAdd = true; |
| 389 |
} |
| 390 |
} |
| 391 |
getTreePart().setButtonEnabled(F_BUTTON_ADD_COMMAND, enableAdd); |
| 392 |
|
| 393 |
// Remove button |
| 394 |
getTreePart().setButtonEnabled(F_BUTTON_REMOVE, getRemovableObjectFromSelection(selection).size() > 0); |
| 395 |
|
| 396 |
// Up and Down buttons |
| 397 |
boolean canMoveUp = true; |
| 398 |
boolean canMoveDown = true; |
| 399 |
if (firstSelectedObject == null || firstSelectedObject.getType() == ICtxHelpConstants.TYPE_ROOT || firstSelectedObject.getType() == ICtxHelpConstants.TYPE_DESCRIPTION || selection.size() > 1) { |
| 400 |
canMoveUp = false; |
| 401 |
canMoveDown = false; |
| 402 |
} else { |
| 403 |
CtxHelpObject parent = firstSelectedObject.getParent(); |
| 404 |
if (parent != null) { |
| 405 |
if (parent.isFirstChildNode(firstSelectedObject, firstSelectedObject.getClass())) { |
| 406 |
canMoveUp = false; |
| 407 |
} |
| 408 |
if (parent.isLastChildNode(firstSelectedObject, firstSelectedObject.getClass())) { |
| 409 |
canMoveDown = false; |
| 410 |
} |
| 411 |
} |
| 412 |
} |
| 413 |
getTreePart().setButtonEnabled(F_BUTTON_UP, canMoveUp); |
| 414 |
getTreePart().setButtonEnabled(F_BUTTON_DOWN, canMoveDown); |
| 415 |
} |
| 416 |
|
| 417 |
/* (non-Javadoc) |
| 418 |
* @see org.eclipse.pde.internal.ui.editor.StructuredViewerSection#fillContextMenu(org.eclipse.jface.action.IMenuManager) |
| 419 |
*/ |
| 420 |
protected void fillContextMenu(IMenuManager manager) { |
| 421 |
// Get the current selection |
| 422 |
IStructuredSelection selection = (IStructuredSelection) fTree.getSelection(); |
| 423 |
Object object = selection.getFirstElement(); |
| 424 |
// Has to be null or a CtxHelpObject object |
| 425 |
CtxHelpObject firstSelectedObject = (CtxHelpObject) object; |
| 426 |
|
| 427 |
if (firstSelectedObject != null) { |
| 428 |
MenuManager submenu = new MenuManager(PDEUIMessages.Menus_new_label); |
| 429 |
|
| 430 |
// Populate the "New" sub-menu |
| 431 |
boolean addMenu = false; |
| 432 |
if (updateAddContextActionWithSelection(firstSelectedObject)) { |
| 433 |
submenu.add(fAddContextAction); |
| 434 |
addMenu = true; |
| 435 |
} |
| 436 |
if (updateAddTopicActionWithSelection(firstSelectedObject)) { |
| 437 |
submenu.add(fAddTopicAction); |
| 438 |
addMenu = true; |
| 439 |
} |
| 440 |
if (updateAddCommandActionWithSelection(firstSelectedObject)) { |
| 441 |
submenu.add(fAddCommandAction); |
| 442 |
addMenu = true; |
| 443 |
} |
| 444 |
if (addMenu) { |
| 445 |
manager.add(submenu); |
| 446 |
manager.add(new Separator()); |
| 447 |
} |
| 448 |
} |
| 449 |
|
| 450 |
if (firstSelectedObject instanceof CtxHelpTopic && ((CtxHelpTopic) firstSelectedObject).getLocation() != null) { |
| 451 |
fOpenLinkAction.setTarget((CtxHelpTopic) firstSelectedObject); |
| 452 |
manager.add(fOpenLinkAction); |
| 453 |
fillContextMenuShowInAction(manager); |
| 454 |
manager.add(new Separator()); |
| 455 |
} |
| 456 |
|
| 457 |
// Add clipboard actions |
| 458 |
getPage().getPDEEditor().getContributor().contextMenuAboutToShow(manager); |
| 459 |
manager.add(new Separator()); |
| 460 |
|
| 461 |
if (updateRemoveActionWithSelection(selection)) { |
| 462 |
manager.add(fRemoveObjectAction); |
| 463 |
manager.add(new Separator()); |
| 464 |
} |
| 465 |
|
| 466 |
} |
| 467 |
|
| 468 |
private void fillContextMenuShowInAction(IMenuManager manager) { |
| 469 |
String showInLabel = PDEUIMessages.PluginsView_showIn; |
| 470 |
|
| 471 |
// Add a label for the keybinding for Show In action, if one exists |
| 472 |
IBindingService bindingService = (IBindingService) PlatformUI.getWorkbench().getAdapter(IBindingService.class); |
| 473 |
if (bindingService != null) { |
| 474 |
String keyBinding = bindingService.getBestActiveBindingFormattedFor("org.eclipse.ui.navigate.showInQuickMenu"); //$NON-NLS-1$ |
| 475 |
if (keyBinding != null) { |
| 476 |
showInLabel += '\t' + keyBinding; |
| 477 |
} |
| 478 |
} |
| 479 |
|
| 480 |
// Add the "Show In" action and its contributions |
| 481 |
IMenuManager showInMenu = new MenuManager(showInLabel); |
| 482 |
showInMenu.add(ContributionItemFactory.VIEWS_SHOW_IN.create(getPage().getSite().getWorkbenchWindow())); |
| 483 |
|
| 484 |
manager.add(showInMenu); |
| 485 |
} |
| 486 |
|
| 487 |
private boolean updateAddContextActionWithSelection(CtxHelpObject selectedObject) { |
| 488 |
if (selectedObject != null && selectedObject.canAddSibling(ICtxHelpConstants.TYPE_CONTEXT)) { |
| 489 |
fAddContextAction.setParentObject(selectedObject.getParent()); |
| 490 |
fAddContextAction.setTargetObject(selectedObject); |
| 491 |
fAddContextAction.setEnabled(fModel.isEditable()); |
| 492 |
return true; |
| 493 |
} else if (selectedObject != null && selectedObject.canAddChild(ICtxHelpConstants.TYPE_CONTEXT)) { |
| 494 |
fAddContextAction.setParentObject(selectedObject); |
| 495 |
fAddContextAction.setTargetObject(null); |
| 496 |
fAddContextAction.setEnabled(fModel.isEditable()); |
| 497 |
return true; |
| 498 |
} else if (fModel.getCtxHelpRoot().canAddChild(ICtxHelpConstants.TYPE_CONTEXT)) { |
| 499 |
fAddContextAction.setParentObject(fModel.getCtxHelpRoot()); |
| 500 |
fAddContextAction.setTargetObject(null); |
| 501 |
fAddContextAction.setEnabled(fModel.isEditable()); |
| 502 |
return true; |
| 503 |
} |
| 504 |
return false; |
| 505 |
} |
| 506 |
|
| 507 |
private boolean updateAddTopicActionWithSelection(CtxHelpObject selectedObject) { |
| 508 |
if (selectedObject != null) { |
| 509 |
if (selectedObject.canAddSibling(ICtxHelpConstants.TYPE_TOPIC)) { |
| 510 |
fAddTopicAction.setParentObject(selectedObject.getParent()); |
| 511 |
fAddTopicAction.setTargetObject(selectedObject); |
| 512 |
fAddTopicAction.setEnabled(fModel.isEditable()); |
| 513 |
return true; |
| 514 |
} else if (selectedObject.canAddChild(ICtxHelpConstants.TYPE_TOPIC)) { |
| 515 |
fAddTopicAction.setParentObject(selectedObject); |
| 516 |
fAddTopicAction.setTargetObject(null); |
| 517 |
fAddTopicAction.setEnabled(fModel.isEditable()); |
| 518 |
return true; |
| 519 |
} |
| 520 |
} |
| 521 |
return false; |
| 522 |
} |
| 523 |
|
| 524 |
private boolean updateAddCommandActionWithSelection(CtxHelpObject selectedObject) { |
| 525 |
if (selectedObject != null) { |
| 526 |
if (selectedObject.canAddSibling(ICtxHelpConstants.TYPE_COMMAND)) { |
| 527 |
fAddCommandAction.setParentObject(selectedObject.getParent()); |
| 528 |
fAddCommandAction.setTargetObject(selectedObject); |
| 529 |
fAddCommandAction.setEnabled(fModel.isEditable()); |
| 530 |
return true; |
| 531 |
} else if (selectedObject.canAddChild(ICtxHelpConstants.TYPE_COMMAND)) { |
| 532 |
fAddCommandAction.setParentObject(selectedObject); |
| 533 |
fAddCommandAction.setTargetObject(null); |
| 534 |
fAddCommandAction.setEnabled(fModel.isEditable()); |
| 535 |
return true; |
| 536 |
} |
| 537 |
} |
| 538 |
return false; |
| 539 |
} |
| 540 |
|
| 541 |
private boolean updateRemoveActionWithSelection(IStructuredSelection selection) { |
| 542 |
List objectsToRemove = getRemovableObjectFromSelection(selection); |
| 543 |
fRemoveObjectAction.setToRemove((CtxHelpObject[]) objectsToRemove.toArray(new CtxHelpObject[objectsToRemove.size()])); |
| 544 |
fRemoveObjectAction.setEnabled(fModel.isEditable()); |
| 545 |
return objectsToRemove.size() > 0; |
| 546 |
} |
| 547 |
|
| 548 |
private List getRemovableObjectFromSelection(IStructuredSelection selection) { |
| 549 |
List objectsToRemove = new ArrayList(); |
| 550 |
for (Iterator iterator = selection.iterator(); iterator.hasNext();) { |
| 551 |
Object currentObject = iterator.next(); |
| 552 |
if (currentObject instanceof CtxHelpObject && ((CtxHelpObject) currentObject).canBeRemoved()) { |
| 553 |
objectsToRemove.add(currentObject); |
| 554 |
} |
| 555 |
} |
| 556 |
return objectsToRemove; |
| 557 |
} |
| 558 |
|
| 559 |
/* (non-Javadoc) |
| 560 |
* @see org.eclipse.pde.internal.ui.editor.StructuredViewerSection#canPaste(java.lang.Object, java.lang.Object[]) |
| 561 |
*/ |
| 562 |
protected boolean canPaste(Object targetObject, Object[] sourceObjects) { |
| 563 |
return true; |
| 564 |
} |
| 565 |
|
| 566 |
/* (non-Javadoc) |
| 567 |
* @see org.eclipse.pde.internal.ui.editor.PDESection#doGlobalAction(java.lang.String) |
| 568 |
*/ |
| 569 |
public boolean doGlobalAction(String actionId) { |
| 570 |
boolean cutAction = actionId.equals(ActionFactory.CUT.getId()); |
| 571 |
|
| 572 |
if (cutAction || actionId.equals(ActionFactory.DELETE.getId())) { |
| 573 |
updateRemoveActionWithSelection((IStructuredSelection) fTree.getSelection()); |
| 574 |
fRemoveObjectAction.run(); |
| 575 |
return !cutAction; |
| 576 |
} |
| 577 |
|
| 578 |
if (actionId.equals(ActionFactory.PASTE.getId())) { |
| 579 |
doPaste(); |
| 580 |
return true; |
| 581 |
} |
| 582 |
|
| 583 |
return false; |
| 584 |
} |
| 585 |
|
| 586 |
/* (non-Javadoc) |
| 587 |
* @see org.eclipse.pde.internal.ui.editor.StructuredViewerSection#doPaste(java.lang.Object, java.lang.Object[]) |
| 588 |
*/ |
| 589 |
protected void doPaste(Object targetObject, Object[] sourceObjects) { |
| 590 |
performDrop(targetObject, sourceObjects, ViewerDropAdapter.LOCATION_ON); |
| 591 |
} |
| 592 |
|
| 593 |
/* (non-Javadoc) |
| 594 |
* @see org.eclipse.pde.internal.ui.editor.TreeSection#handleDoubleClick(org.eclipse.jface.viewers.IStructuredSelection) |
| 595 |
*/ |
| 596 |
protected void handleDoubleClick(IStructuredSelection selection) { |
| 597 |
Object selected = selection.getFirstElement(); |
| 598 |
if (selected instanceof CtxHelpTopic) { |
| 599 |
open((CtxHelpTopic) selected); |
| 600 |
} else if (selected instanceof CtxHelpObject) { |
| 601 |
fTree.expandToLevel(selected, 1); |
| 602 |
} |
| 603 |
} |
| 604 |
|
| 605 |
/** |
| 606 |
* Opens a document with the specified path |
| 607 |
* |
| 608 |
* @param path a path to a resource, relative to this TOC's root project |
| 609 |
*/ |
| 610 |
private void open(CtxHelpTopic topic) { |
| 611 |
IPath resourcePath = topic.getLocation(); |
| 612 |
if (!isEditable() || resourcePath == null || resourcePath.isEmpty()) { |
| 613 |
MessageDialog.openWarning(PDEPlugin.getActiveWorkbenchShell(), PDEUIMessages.WindowImagesSection_open, PDEUIMessages.WindowImagesSection_emptyPath); |
| 614 |
return; |
| 615 |
} |
| 616 |
|
| 617 |
IResource resource = findResource(resourcePath); |
| 618 |
if (resource != null && resource instanceof IFile) { |
| 619 |
openResource(resource); |
| 620 |
} else { |
| 621 |
MessageDialog.openWarning(PDEPlugin.getActiveWorkbenchShell(), PDEUIMessages.WindowImagesSection_open, PDEUIMessages.WindowImagesSection_warning); |
| 622 |
} |
| 623 |
} |
| 624 |
|
| 625 |
public IFile openFile(String stringPath) { |
| 626 |
IPath path = new Path(stringPath); |
| 627 |
if (isEditable()) { |
| 628 |
if (!path.isEmpty()) { |
| 629 |
IResource page = findResource(path); |
| 630 |
if (page != null && page instanceof IFile) { |
| 631 |
openResource(page); |
| 632 |
return null; |
| 633 |
} |
| 634 |
} |
| 635 |
|
| 636 |
// TODO Consider having a new wizard |
| 637 |
// return showNewWizard(path, isTOCFile); |
| 638 |
} |
| 639 |
|
| 640 |
return null; |
| 641 |
} |
| 642 |
|
| 643 |
// private IFile showNewWizard(String path, boolean tocWizard) { |
| 644 |
// TocHTMLWizard wizard; |
| 645 |
// if (tocWizard) { |
| 646 |
// wizard = new NewTocFileWizard(); |
| 647 |
// } else { |
| 648 |
// wizard = new TocHTMLWizard(); |
| 649 |
// } |
| 650 |
// |
| 651 |
// // By default, the file will be created in the same project as the TOC |
| 652 |
// IResource selectedFolder = fModel.getUnderlyingResource().getProject(); |
| 653 |
// String filename = null; |
| 654 |
// |
| 655 |
// // Find the folder associated with the specified path |
| 656 |
// IPath initialFolder = new Path(path.trim()); |
| 657 |
// if (!initialFolder.isEmpty()) { |
| 658 |
// IPath newPath = selectedFolder.getFullPath().append(initialFolder); |
| 659 |
// |
| 660 |
// IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot(); |
| 661 |
// IResource newFolder = root.findMember(newPath); |
| 662 |
// |
| 663 |
// if (newFolder == null) { |
| 664 |
// if (!newPath.hasTrailingSeparator()) { |
| 665 |
// filename = newPath.lastSegment(); |
| 666 |
// } |
| 667 |
// } |
| 668 |
// |
| 669 |
// while (newFolder == null && !newPath.isEmpty()) { |
| 670 |
// newPath = newPath.removeLastSegments(1); |
| 671 |
// newFolder = root.findMember(newPath); |
| 672 |
// } |
| 673 |
// |
| 674 |
// if (newFolder != null) { |
| 675 |
// selectedFolder = newFolder; |
| 676 |
// } |
| 677 |
// } |
| 678 |
// |
| 679 |
// // Select the project in the wizard |
| 680 |
// wizard.init(PlatformUI.getWorkbench(), new StructuredSelection(selectedFolder)); |
| 681 |
// |
| 682 |
// // Create the dialog for the wizard |
| 683 |
// WizardDialog dialog = new WizardDialog(PDEPlugin.getActiveWorkbenchShell(), wizard); |
| 684 |
// dialog.create(); |
| 685 |
// // Get the wizard page |
| 686 |
// IWizardPage wizardPage; |
| 687 |
// wizardPage = wizard.getStartingPage(); |
| 688 |
// if (!(wizardPage instanceof WizardNewFileCreationPage)) { |
| 689 |
// return null; |
| 690 |
// } |
| 691 |
// |
| 692 |
// WizardNewFileCreationPage page = (WizardNewFileCreationPage) wizardPage; |
| 693 |
// if (filename != null) { |
| 694 |
// page.setFileName(filename); |
| 695 |
// // Inhibit the error message when the wizard is first opened |
| 696 |
// page.setErrorMessage(null); |
| 697 |
// } |
| 698 |
// |
| 699 |
// if (dialog.open() == Window.OK) { |
| 700 |
// return wizard.getNewResource(); |
| 701 |
// } |
| 702 |
// |
| 703 |
// return null; |
| 704 |
// } |
| 705 |
// |
| 706 |
private IResource findResource(IPath resourcePath) { |
| 707 |
IProject pluginProject = fModel.getUnderlyingResource().getProject(); |
| 708 |
return pluginProject.findMember(resourcePath); |
| 709 |
} |
| 710 |
|
| 711 |
private void openResource(IResource resource) { |
| 712 |
IPath path = resource.getFullPath(); |
| 713 |
if (isFileValidInContext(path)) { |
| 714 |
try { |
| 715 |
IDE.openEditor(PDEPlugin.getActivePage(), (IFile) resource, true); |
| 716 |
} catch (PartInitException e) { //suppress exception |
| 717 |
} |
| 718 |
} |
| 719 |
} |
| 720 |
|
| 721 |
private boolean isFileValidInContext(IPath path) { |
| 722 |
String message = null; |
| 723 |
|
| 724 |
if (HelpEditorUtil.hasValidPageExtension(path)) { |
| 725 |
return true; |
| 726 |
} |
| 727 |
message = PDEUIMessages.TocPage_invalidHTMLFile; |
| 728 |
MessageDialog.openWarning(PDEPlugin.getActiveWorkbenchShell(), PDEUIMessages.WindowImagesSection_open, message); |
| 729 |
return false; |
| 730 |
} |
| 731 |
|
| 732 |
/** |
| 733 |
* Perform a drop of the specified objects on the target in the widget |
| 734 |
* |
| 735 |
* @param currentTarget The object that the drop will occur near/on |
| 736 |
* @param dropped The dropped objects |
| 737 |
* @param location The location of the drop relative to the target |
| 738 |
* |
| 739 |
* @return true iff the drop was successful |
| 740 |
*/ |
| 741 |
public boolean performDrop(Object currentTarget, Object dropped, int location) { |
| 742 |
if (dropped instanceof Object[]) { |
| 743 |
CtxHelpObject target = (CtxHelpObject) currentTarget; |
| 744 |
// Determine the object that the dropped objects will be the |
| 745 |
// children of |
| 746 |
|
| 747 |
CtxHelpObject targetParent = determineParent(target, location); |
| 748 |
|
| 749 |
if (location == CtxHelpDropAdapter.LOCATION_JUST_AFTER && targetParent == target && !target.getChildren().isEmpty() && fTree.getExpandedState(target)) { |
| 750 |
// If the drop occurs just after a parentable object |
| 751 |
// and it is expanded, then insert the dropped items |
| 752 |
// as the first children of the parent |
| 753 |
location = ViewerDropAdapter.LOCATION_BEFORE; |
| 754 |
target = (CtxHelpObject) target.getChildren().get(0); |
| 755 |
} |
| 756 |
|
| 757 |
if (targetParent != null) { // Get the TocObject versions of the dropped objects |
| 758 |
ArrayList objectsToAdd = getObjectsToAdd((Object[]) dropped, targetParent); |
| 759 |
|
| 760 |
if (objectsToAdd != null && !objectsToAdd.isEmpty()) { |
| 761 |
if (fDragAdapter.getDraggedElements() != null && fDragAdapter.getDraggedElements().size() == 1 && currentTarget == fDragAdapter.getDraggedElements().get(0)) { // Last-minute check: ignore drops of an object onto/near itself |
| 762 |
// to avoid unnecessarily dirtying the page |
| 763 |
return false; |
| 764 |
} |
| 765 |
|
| 766 |
boolean insertBefore = (location == ViewerDropAdapter.LOCATION_BEFORE); |
| 767 |
|
| 768 |
// Add the objects |
| 769 |
handleMultiAddAction(objectsToAdd, target, insertBefore, targetParent); |
| 770 |
return true; |
| 771 |
} |
| 772 |
} |
| 773 |
} |
| 774 |
return false; |
| 775 |
} |
| 776 |
|
| 777 |
/** |
| 778 |
* Determine the parent object that a drop will occur under, |
| 779 |
* based on the relative location of the drop and the ability |
| 780 |
* of the target to be a parent |
| 781 |
* |
| 782 |
* @param dropTarget The target that the drop occurs near/on |
| 783 |
* @param dropLocation The location of the drop relative to the target |
| 784 |
* @return |
| 785 |
*/ |
| 786 |
private CtxHelpObject determineParent(CtxHelpObject dropTarget, int dropLocation) { |
| 787 |
//We must determine what object will be the parent of the |
| 788 |
//dropped objects. This is done by looking at the drop location |
| 789 |
//and drop target type |
| 790 |
|
| 791 |
if (dropTarget == null || dropTarget.getType() == ICtxHelpConstants.TYPE_ROOT) { |
| 792 |
//Since the TOC root has no parent, it must be the target parent |
| 793 |
return fModel.getCtxHelpRoot(); |
| 794 |
} |
| 795 |
switch (dropLocation) { |
| 796 |
case CtxHelpDropAdapter.LOCATION_JUST_AFTER : { //if the drop occured after an expanded node |
| 797 |
//and all of its children, |
| 798 |
//make the drop target's parent the target parent object |
| 799 |
if (!fTree.getExpandedState(dropTarget)) { |
| 800 |
return dropTarget.getParent(); |
| 801 |
} |
| 802 |
//otherwise, the target parent is the drop target, |
| 803 |
//since the drop occured between it and its first child |
| 804 |
} |
| 805 |
case ViewerDropAdapter.LOCATION_ON : { //the drop location is directly on the drop target |
| 806 |
return dropTarget; |
| 807 |
} |
| 808 |
case ViewerDropAdapter.LOCATION_BEFORE : |
| 809 |
case ViewerDropAdapter.LOCATION_AFTER : { //if the drop is before or after the drop target, |
| 810 |
//make the drop target's parent the target parent object |
| 811 |
return dropTarget.getParent(); |
| 812 |
} |
| 813 |
} |
| 814 |
return null; |
| 815 |
} |
| 816 |
|
| 817 |
/** |
| 818 |
* Get the TocObject representations of a group of dropped objects. |
| 819 |
* |
| 820 |
* @param droppings The objects that are dropped; can be file path Strings or |
| 821 |
* deserialized TocObjects |
| 822 |
* |
| 823 |
* @param targetParent The designated parent of the dropped objects |
| 824 |
* |
| 825 |
* @return a list of the (reconnected) TocObject representations of the dropped objects |
| 826 |
*/ |
| 827 |
private ArrayList getObjectsToAdd(Object[] droppings, CtxHelpObject targetParent) { |
| 828 |
ArrayList ctxHelpObjects = new ArrayList(droppings.length); |
| 829 |
|
| 830 |
if (fDragAdapter.getDraggedElements() != null) { // If there are items in the drag adapter, then the current drag must be from |
| 831 |
// this section |
| 832 |
fDragFromHere = fDragAdapter.getDraggedElements().size() == droppings.length; |
| 833 |
} |
| 834 |
|
| 835 |
for (int i = 0; i < droppings.length; ++i) { |
| 836 |
// TODO Finish drop |
| 837 |
// if (droppings[i] instanceof String) { |
| 838 |
// IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot(); |
| 839 |
// |
| 840 |
// // If the array contains Strings, we treat them as file paths |
| 841 |
// Path path = new Path((String) droppings[i]); |
| 842 |
// IFile file = root.getFileForLocation(path); |
| 843 |
// if (file == null) { |
| 844 |
// continue; |
| 845 |
// } |
| 846 |
// |
| 847 |
// // If the path is to a valid TOC file |
| 848 |
// // and it isn't the file in this model |
| 849 |
// // then make a link |
| 850 |
// if (TocExtensionUtil.isTOCFile(path) && !TocExtensionUtil.isCurrentResource(path, fModel)) { |
| 851 |
// tocObjects.add(makeNewTocLink(targetParent, file)); |
| 852 |
// } |
| 853 |
// // If the path is to a file with an HTML page extension, make a topic |
| 854 |
// else if (TocExtensionUtil.hasValidPageExtension(path)) { |
| 855 |
// TocTopic topic = makeNewTocTopic(targetParent, file); |
| 856 |
// String title = generateTitle(targetParent, path); |
| 857 |
// |
| 858 |
// topic.setFieldLabel(title); |
| 859 |
// tocObjects.add(topic); |
| 860 |
// } |
| 861 |
// } else |
| 862 |
if (droppings[i] instanceof CtxHelpObject) { |
| 863 |
ArrayList dragged = fDragAdapter.getDraggedElements(); |
| 864 |
if (fDragFromHere) { |
| 865 |
CtxHelpObject draggedObj = (CtxHelpObject) dragged.get(i); |
| 866 |
|
| 867 |
//Nesting an object inside itself or its children |
| 868 |
//is so stupid and ridiculous that I get a headache |
| 869 |
//just thinking about it. Thus, this drag is not going to complete. |
| 870 |
if (targetParent.descendsFrom(draggedObj)) { |
| 871 |
return null; |
| 872 |
} |
| 873 |
} |
| 874 |
|
| 875 |
//Reconnect this TocObject, since it was deserialized |
| 876 |
((CtxHelpObject) droppings[i]).reconnect(targetParent, fModel); |
| 877 |
ctxHelpObjects.add(droppings[i]); |
| 878 |
} |
| 879 |
} |
| 880 |
|
| 881 |
return ctxHelpObjects; |
| 882 |
} |
| 883 |
|
| 884 |
/** |
| 885 |
* Generate the title of a Topic created via dragging in an HTML page. |
| 886 |
* Use the title of the HTML page, or generate a name based on the target |
| 887 |
* parent if no title exists. |
| 888 |
* |
| 889 |
* @param targetParent The designated parent of this topic |
| 890 |
* @param path The path to the HTML file |
| 891 |
* |
| 892 |
* @return The generated name of the Topic. |
| 893 |
*/ |
| 894 |
// private String generateTitle(TocTopic targetParent, Path path) { |
| 895 |
// String title = TocHTMLTitleUtil.findTitle(path.toFile()); |
| 896 |
// if (title == null) { |
| 897 |
// int numChildren = targetParent.getChildren().size(); |
| 898 |
// TocObject[] children = (TocObject[]) targetParent.getChildren().toArray(new TocObject[numChildren]); |
| 899 |
// |
| 900 |
// String[] tocObjectNames = new String[children.length]; |
| 901 |
// |
| 902 |
// for (int j = 0; j < numChildren; ++j) { |
| 903 |
// tocObjectNames[j] = children[j].getName(); |
| 904 |
// } |
| 905 |
// |
| 906 |
// title = PDELabelUtility.generateName(tocObjectNames, PDEUIMessages.TocPage_TocTopic); |
| 907 |
// } |
| 908 |
// return title; |
| 909 |
// } |
| 910 |
/* (non-Javadoc) |
| 911 |
* @see org.eclipse.pde.internal.ui.editor.StructuredViewerSection#buttonSelected(int) |
| 912 |
*/ |
| 913 |
protected void buttonSelected(int index) { |
| 914 |
IStructuredSelection selection = (IStructuredSelection) fTree.getSelection(); |
| 915 |
Object object = selection.getFirstElement(); |
| 916 |
CtxHelpObject firstSelectedObject = (CtxHelpObject) object; |
| 917 |
switch (index) { |
| 918 |
case F_BUTTON_ADD_CONTEXT : |
| 919 |
updateAddContextActionWithSelection(firstSelectedObject); |
| 920 |
fAddContextAction.run(); |
| 921 |
break; |
| 922 |
case F_BUTTON_ADD_TOPIC : |
| 923 |
updateAddTopicActionWithSelection(firstSelectedObject); |
| 924 |
fAddTopicAction.run(); |
| 925 |
break; |
| 926 |
case F_BUTTON_ADD_COMMAND : |
| 927 |
updateAddCommandActionWithSelection(firstSelectedObject); |
| 928 |
fAddCommandAction.run(); |
| 929 |
break; |
| 930 |
case F_BUTTON_REMOVE : |
| 931 |
updateRemoveActionWithSelection(selection); |
| 932 |
fRemoveObjectAction.run(); |
| 933 |
break; |
| 934 |
case F_BUTTON_UP : |
| 935 |
handleMoveAction(F_UP_FLAG); |
| 936 |
break; |
| 937 |
case F_BUTTON_DOWN : |
| 938 |
handleMoveAction(F_DOWN_FLAG); |
| 939 |
break; |
| 940 |
} |
| 941 |
} |
| 942 |
|
| 943 |
/** |
| 944 |
* Handle the addition of multiple initialized objects to the TOC. |
| 945 |
* |
| 946 |
* @param objectsToAdd The objects to be added |
| 947 |
* @param target The target to add these objects relative to |
| 948 |
* @param insertBefore Whether or not the insertion occurs before the target |
| 949 |
* @param targetParent The parent object of the newly added objects |
| 950 |
*/ |
| 951 |
private void handleMultiAddAction(List objectsToAdd, CtxHelpObject target, boolean insertBefore, CtxHelpObject targetParent) { |
| 952 |
CtxHelpObject[] tocObjects = (CtxHelpObject[]) objectsToAdd.toArray(new CtxHelpObject[objectsToAdd.size()]); |
| 953 |
if (tocObjects == null) |
| 954 |
return; |
| 955 |
|
| 956 |
for (int i = 0; i < tocObjects.length; ++i) { |
| 957 |
if (tocObjects[i] != null) { |
| 958 |
if (targetParent != null && targetParent.canBeParent() && targetParent.canAddChild(tocObjects[i].getType())) { |
| 959 |
if (target != null && target != targetParent) { // Add the object as a direct sibling of the target |
| 960 |
targetParent.addChild(tocObjects[i], target, insertBefore); |
| 961 |
} else { // Add the object as the last child of the target parent |
| 962 |
targetParent.addChild(tocObjects[i]); |
| 963 |
} |
| 964 |
} |
| 965 |
} |
| 966 |
} |
| 967 |
} |
| 968 |
|
| 969 |
// |
| 970 |
// /** |
| 971 |
// * Remove the selected objects from the TOC tree |
| 972 |
// */ |
| 973 |
// private void handleDeleteAction() { |
| 974 |
// ArrayList objects = new ArrayList(((IStructuredSelection) fTree.getSelection()).toList()); |
| 975 |
// boolean beep = false; |
| 976 |
// |
| 977 |
// // Iterate through the list of selected objects, removing ones |
| 978 |
// // that cannot be removed |
| 979 |
// for (Iterator i = objects.iterator(); i.hasNext();) { |
| 980 |
// Object object = i.next(); |
| 981 |
// if (object instanceof TocObject) { |
| 982 |
// TocObject tocObject = (TocObject) object; |
| 983 |
// |
| 984 |
// if (!tocObject.canBeRemoved()) { |
| 985 |
// i.remove(); |
| 986 |
// beep = true; |
| 987 |
// } |
| 988 |
// } |
| 989 |
// } |
| 990 |
// |
| 991 |
// if (beep) { // If any object cannot be removed, beep to notify the user |
| 992 |
// Display.getCurrent().beep(); |
| 993 |
// } |
| 994 |
// |
| 995 |
// // Remove the remaining objects |
| 996 |
// handleRemove(objects); |
| 997 |
// } |
| 998 |
// |
| 999 |
// /** |
| 1000 |
// * Remove the items listed from the TOC. |
| 1001 |
// * |
| 1002 |
// * @param itemsToRemove The list of items to remove from the TOC |
| 1003 |
// */ |
| 1004 |
// public void handleRemove(List itemsToRemove) { |
| 1005 |
// if (!itemsToRemove.isEmpty()) { // Target the objects for removal |
| 1006 |
// fRemoveObjectAction.setToRemove((TocObject[]) itemsToRemove.toArray(new TocObject[itemsToRemove.size()])); |
| 1007 |
// |
| 1008 |
// // Run the removal action |
| 1009 |
// fRemoveObjectAction.run(); |
| 1010 |
// } |
| 1011 |
// } |
| 1012 |
|
| 1013 |
/** |
| 1014 |
* Handle the dragging of objects out of this TOC. |
| 1015 |
* |
| 1016 |
* @param itemsDragged The items dragged out of the TOC |
| 1017 |
*/ |
| 1018 |
public void handleDrag(List itemsDragged) { |
| 1019 |
// TODO Handle multi drag, with instanceof checks |
| 1020 |
CtxHelpObject object = (CtxHelpObject) itemsDragged.get(0); |
| 1021 |
if (object != null && object.canBeRemoved()) { |
| 1022 |
if (updateRemoveActionWithSelection(new StructuredSelection(itemsDragged))) { |
| 1023 |
fRemoveObjectAction.run(); |
| 1024 |
} |
| 1025 |
} |
| 1026 |
|
| 1027 |
// The drag is finished, so there is no intra-editor DND operation occuring now |
| 1028 |
fDragFromHere = false; |
| 1029 |
} |
| 1030 |
|
| 1031 |
/** |
| 1032 |
* Move an object within the TOC. |
| 1033 |
* |
| 1034 |
* @param positionFlag The direction that the object will move |
| 1035 |
*/ |
| 1036 |
private void handleMoveAction(int positionFlag) { |
| 1037 |
IStructuredSelection sel = (IStructuredSelection) fTree.getSelection(); |
| 1038 |
|
| 1039 |
Object object = sel.getFirstElement(); |
| 1040 |
if (object == null) { |
| 1041 |
return; |
| 1042 |
} else if (object instanceof CtxHelpObject) { |
| 1043 |
CtxHelpObject ctxHelpObject = (CtxHelpObject) object; |
| 1044 |
CtxHelpObject parent = ctxHelpObject.getParent(); |
| 1045 |
if (parent != null) { |
| 1046 |
parent.moveChild(ctxHelpObject, positionFlag); |
| 1047 |
fTree.setSelection(new StructuredSelection(ctxHelpObject), true); |
| 1048 |
} |
| 1049 |
} |
| 1050 |
|
| 1051 |
} |
| 1052 |
|
| 1053 |
/* (non-Javadoc) |
| 1054 |
* @see org.eclipse.pde.internal.ui.editor.PDESection#modelChanged(org.eclipse.pde.core.IModelChangedEvent) |
| 1055 |
*/ |
| 1056 |
public void modelChanged(IModelChangedEvent event) { |
| 1057 |
// No need to call super, world changed event handled here |
| 1058 |
if (event.getChangeType() == IModelChangedEvent.WORLD_CHANGED) { |
| 1059 |
handleModelEventWorldChanged(event); |
| 1060 |
} else if (event.getChangeType() == IModelChangedEvent.INSERT) { |
| 1061 |
handleModelInsertType(event); |
| 1062 |
} else if (event.getChangeType() == IModelChangedEvent.REMOVE) { |
| 1063 |
handleModelRemoveType(event); |
| 1064 |
} else if ((event.getChangeType() == IModelChangedEvent.CHANGE) && (event.getChangedProperty().equals(IDocumentElementNode.F_PROPERTY_CHANGE_TYPE_SWAP))) { |
| 1065 |
handleModelChangeTypeSwap(event); |
| 1066 |
} else if (event.getChangeType() == IModelChangedEvent.CHANGE) { |
| 1067 |
handleModelChangeType(event); |
| 1068 |
} |
| 1069 |
} |
| 1070 |
|
| 1071 |
/** |
| 1072 |
* @param event |
| 1073 |
*/ |
| 1074 |
private void handleModelChangeTypeSwap(IModelChangedEvent event) { |
| 1075 |
// Swap event |
| 1076 |
// Get the changed object |
| 1077 |
Object[] objects = event.getChangedObjects(); |
| 1078 |
CtxHelpObject object = (CtxHelpObject) objects[0]; |
| 1079 |
|
| 1080 |
if (object != null) { // Update the element in the tree viewer |
| 1081 |
fTree.refresh(object); |
| 1082 |
} |
| 1083 |
} |
| 1084 |
|
| 1085 |
/** |
| 1086 |
* The model is stale, refresh the UI |
| 1087 |
* |
| 1088 |
* @param event The world-change event |
| 1089 |
*/ |
| 1090 |
private void handleModelEventWorldChanged(IModelChangedEvent event) { |
| 1091 |
markStale(); |
| 1092 |
} |
| 1093 |
|
| 1094 |
/** |
| 1095 |
* Handle insertions in the model |
| 1096 |
* @param event the insertion event |
| 1097 |
*/ |
| 1098 |
private void handleModelInsertType(IModelChangedEvent event) { |
| 1099 |
// Insert event |
| 1100 |
Object[] objects = event.getChangedObjects(); |
| 1101 |
CtxHelpObject object = (CtxHelpObject) objects[0]; |
| 1102 |
if (object != null) { |
| 1103 |
if (object.getType() != ICtxHelpConstants.TYPE_ROOT) { |
| 1104 |
// Refresh the parent element in the tree viewer |
| 1105 |
// TODO: Can we get away with an update instead of a refresh here? |
| 1106 |
fTree.refresh(object.getParent()); |
| 1107 |
// Select the new object in the tree, unless it is a description node |
| 1108 |
if (!(object instanceof CtxHelpDescription)) { |
| 1109 |
fTree.setSelection(new StructuredSelection(object), true); |
| 1110 |
} |
| 1111 |
} |
| 1112 |
} |
| 1113 |
} |
| 1114 |
|
| 1115 |
/** |
| 1116 |
* Handle removals in the model |
| 1117 |
* |
| 1118 |
* @param event the removal event |
| 1119 |
*/ |
| 1120 |
private void handleModelRemoveType(IModelChangedEvent event) { |
| 1121 |
// Remove event |
| 1122 |
Object[] objects = event.getChangedObjects(); |
| 1123 |
if (objects[0] != null) { |
| 1124 |
CtxHelpObject object = (CtxHelpObject) objects[0]; |
| 1125 |
fTree.remove(object); |
| 1126 |
CtxHelpObject nextSelection = fRemoveObjectAction.getNextSelection(); |
| 1127 |
if (nextSelection != null) { |
| 1128 |
if (nextSelection.equals(object.getParent())) { |
| 1129 |
fTree.refresh(object.getParent()); |
| 1130 |
} |
| 1131 |
if (!fDragFromHere) { |
| 1132 |
fTree.setSelection(new StructuredSelection(nextSelection), true); |
| 1133 |
} |
| 1134 |
fRemoveObjectAction.clearNextSelection(); |
| 1135 |
} |
| 1136 |
} |
| 1137 |
} |
| 1138 |
|
| 1139 |
/** |
| 1140 |
* Handle an update to a TocObject's properties |
| 1141 |
* @param event the update event |
| 1142 |
*/ |
| 1143 |
private void handleModelChangeType(IModelChangedEvent event) { |
| 1144 |
// Get the changed object |
| 1145 |
Object[] objects = event.getChangedObjects(); |
| 1146 |
CtxHelpObject object = (CtxHelpObject) objects[0]; |
| 1147 |
|
| 1148 |
if (object != null) { // Update the element in the tree viewer |
| 1149 |
fTree.update(object, null); |
| 1150 |
} |
| 1151 |
} |
| 1152 |
|
| 1153 |
public void refresh() { |
| 1154 |
CtxHelpModel model = (CtxHelpModel) getPage().getModel(); |
| 1155 |
fTree.setInput(model); |
| 1156 |
fTree.expandToLevel(2); |
| 1157 |
fTree.setSelection(new StructuredSelection(model.getCtxHelpRoot()), true); |
| 1158 |
getManagedForm().fireSelectionChanged(this, fTree.getSelection()); |
| 1159 |
super.refresh(); |
| 1160 |
} |
| 1161 |
|
| 1162 |
/* (non-Javadoc) |
| 1163 |
* @see org.eclipse.pde.internal.ui.editor.TreeSection#createTreeViewer(org.eclipse.swt.widgets.Composite, int) |
| 1164 |
*/ |
| 1165 |
protected TreeViewer createTreeViewer(Composite parent, int style) { |
| 1166 |
fFilteredTree = new FormFilteredTree(parent, style, new PatternFilter()); |
| 1167 |
parent.setData("filtered", Boolean.TRUE); //$NON-NLS-1$ |
| 1168 |
return fFilteredTree.getViewer(); |
| 1169 |
} |
| 1170 |
|
| 1171 |
/* (non-Javadoc) |
| 1172 |
* @see org.eclipse.ui.forms.AbstractFormPart#dispose() |
| 1173 |
*/ |
| 1174 |
public void dispose() { |
| 1175 |
PDEPlugin.getDefault().getLabelProvider().disconnect(this); |
| 1176 |
super.dispose(); |
| 1177 |
} |
| 1178 |
} |