public MergeSourceViewer( SourceViewer sourceViewer, ResourceBundle bundle, ICompareContainer container) { Assert.isNotNull(sourceViewer); fSourceViewer = sourceViewer; fResourceBundle = bundle; fContainer = container; MenuManager menu = new MenuManager(); menu.setRemoveAllWhenShown(true); menu.addMenuListener(this); StyledText te = getSourceViewer().getTextWidget(); te.setMenu(menu.createContextMenu(te)); fContainer.registerContextMenu(menu, getSourceViewer()); // for listening to editor show/hide line number preference value fPreferenceChangeListener = new IPropertyChangeListener() { public void propertyChange(PropertyChangeEvent event) { MergeSourceViewer.this.handlePropertyChangeEvent(event); } }; EditorsUI.getPreferenceStore().addPropertyChangeListener(fPreferenceChangeListener); fShowLineNumber = EditorsUI.getPreferenceStore() .getBoolean(AbstractDecoratedTextEditorPreferenceConstants.EDITOR_LINE_NUMBER_RULER); if (fShowLineNumber) { updateLineNumberRuler(); } IOperationHistory history = getHistory(); if (history != null) history.addOperationHistoryListener(this); // don't add save when in a dialog, IWorkbenchPart is null in dialog containers fAddSaveAction = fContainer.getWorkbenchPart() != null; }
/** * The <code>AbstractTextEditor</code> implementation of this <code>IWorkbenchPart</code> method * may be extended by subclasses. Subclasses must call <code>super.dispose()</code>. * * <p>Note that many methods may return <code>null</code> after the editor is disposed. */ @Override public void dispose() { if (fTitleImage != null) { fTitleImage.dispose(); fTitleImage = null; } disposeDocumentProvider(); if (fSourceViewer != null) { fSourceViewer = null; } if (fConfiguration != null) fConfiguration = null; IOperationHistory history = OperationHistoryFactory.getOperationHistory(); if (history != null) { if (fNonLocalOperationApprover != null) history.removeOperationApprover(fNonLocalOperationApprover); if (fLinearUndoViolationApprover != null) history.removeOperationApprover(fLinearUndoViolationApprover); } fNonLocalOperationApprover = null; fLinearUndoViolationApprover = null; super.dispose(); }
/** * The viewer is no longer part of the UI, it's a wrapper only. The disposal doesn't take place * while releasing the editor pane's children. The method have to be called it manually. The * wrapped viewer is disposed as a regular viewer, while disposing the UI. */ public void dispose() { getSourceViewer().removeTextListener(this); getSourceViewer().removeSelectionChangedListener(this); EditorsUI.getPreferenceStore().removePropertyChangeListener(fPreferenceChangeListener); IOperationHistory history = getHistory(); if (history != null) history.removeOperationHistoryListener(this); }
/** * Adds an undone NullOperation (i.e. has no effects on executing, undoing or redoing) to the * Eclipse IOperationHistory to be sure that redoing is possible. This is necessary for the UI * Redo possibility being activated. */ protected void simulateUndo() { IUndoableOperation auxOp = new NullOperation(); try { auxOp.addContext(context); eclipseHistory.execute(auxOp, null, null); eclipseHistory.undo(context, null, null); if (!eclipseHistory.canRedo(context)) log.error("Simulating Undo failed"); } catch (ExecutionException e) { log.error("Simulating Undo failed"); } }
@Override public void sessionStarted(ISarosSession newSarosSession) { undoHistory.clear(); newSarosSession.addActivityProvider(UndoManager.this); enabled = preferences.isConcurrentUndoActivated(); eclipseHistory.addOperationApprover(operationBlocker); UndoManager.this.sarosSession = newSarosSession; }
/** * Execute the passed Runnable within a command * * @param label * @param command */ public static void exec(TransactionalEditingDomain domain, String label, final Runnable command) { // do works, undo does not (but is selectable in Papyrus Model explorer) IOperationHistory history = OperationHistoryFactory.getOperationHistory(); try { history.execute( new AbstractTransactionalCommand(domain, label, Collections.EMPTY_LIST) { @Override public CommandResult doExecuteWithResult(IProgressMonitor dummy, IAdaptable info) { command.run(); return CommandResult.newOKCommandResult(); } }, null, null); } catch (ExecutionException e) { e.printStackTrace(); } }
@Override public void dispose() { OperationHistoryFactory.getOperationHistory().removeOperationHistoryListener(historyListener); sessionManager.removeSarosSessionListener(sessionListener); editorManager.removeActivityListener(activityListener); enabled = false; eclipseHistory.removeOperationApprover(operationBlocker); editorManager.removeSharedEditorListener(sharedEditorListener); }
@Override public void sessionEnded(ISarosSession oldSarosSession) { oldSarosSession.removeActivityProvider(UndoManager.this); undoHistory.clear(); enabled = false; eclipseHistory.removeOperationApprover(operationBlocker); UndoManager.this.sarosSession = null; currentLocalCompositeOperation = null; currentLocalAtomicOperation = null; }
@Test public void testBug357516_bookmark() throws Exception { IResource resource = editor.getResource(); HashMap<String, Object> attributes = new HashMap<String, Object>(); attributes.put(IMarker.MESSAGE, CUSTOM_MARKER_TEST_MESSAGE); attributes.put(IMarker.LINE_NUMBER, 1); attributes.put(IMarker.LOCATION, resource.getFullPath().toPortableString()); IUndoableOperation operation = new CreateMarkersOperation( IMarker.BOOKMARK, attributes, resource, CUSTOM_MARKER_TEST_MESSAGE); IOperationHistory operationHistory = PlatformUI.getWorkbench().getOperationSupport().getOperationHistory(); try { operationHistory.execute(operation, null, null); } catch (ExecutionException x) { fail(x.getMessage()); } String hoverInfo = hover.getHoverInfo(editor.getInternalSourceViewer(), 0); assertNotNull(hoverInfo); assertTrue(hoverInfo.contains(CUSTOM_MARKER_TEST_MESSAGE)); }
/* * Determine whether the operation in question is still valid. */ private IStatus proceedWithOperation( final IUndoableOperation operation, final IOperationHistory history, final IAdaptable uiInfo, final int doing) { // return immediately if the operation is not relevant if (!operation.hasContext(context)) { return Status.OK_STATUS; } // if the operation does not support advanced validation, // then we assume it is valid. if (doing == EXECUTING) { if (!(operation instanceof IAdvancedUndoableOperation2)) { return Status.OK_STATUS; } } else { if (!(operation instanceof IAdvancedUndoableOperation)) { return Status.OK_STATUS; } } // The next two methods make a number of UI calls, so we wrap the // whole thing up in a syncExec. final IStatus[] status = new IStatus[1]; Workbench.getInstance() .getDisplay() .syncExec( new Runnable() { public void run() { // Compute the undoable or redoable status status[0] = computeOperationStatus(operation, history, uiInfo, doing); // Report non-OK statuses to the user. In some cases, the user // may choose to proceed, and the returned status will be // different than what is reported. if (!status[0].isOK()) { status[0] = reportAndInterpretStatus(status[0], uiInfo, operation, doing); } } }); // If the operation is still not OK, inform the history that the // operation has changed, since it was previously believed to be valid. // We rely here on the ability of an IAdvancedUndoableOperation to // correctly report canUndo() and canRedo() once the undoable and // redoable status have been computed. if (!status[0].isOK()) { history.operationChanged(operation); } return status[0]; }
/** * 设置是否添加到记忆库 * * @param selectedRowIds * @param state "yes" or "no"; */ public void changeSendToTmState(List<String> selectedRowIds, String state) { IOperationHistory operationHistory = OperationHistoryFactory.getOperationHistory(); try { operationHistory.execute( new SendTOTmOperation( "send-to-tm", xliffEditor.getTable(), selectedRowIds, xliffEditor.getXLFHandler(), state), null, null); } catch (ExecutionException e) { LOGGER.error("", e); MessageDialog.openError( xliffEditor.getSite().getShell(), Messages.getString("utils.NattableUtil.msgTitle2"), e.getMessage()); e.printStackTrace(); } }
@Override public void dispose() { IOperationHistory history = getHistory(); if (history != null) { history.removeOperationHistoryListener(historyListener); } if (isInvalid()) { return; } site.getPage().removePartListener(partListener); site = null; progressDialog = null; // We do not flush the history for our undo context because it may be // used elsewhere. It is up to clients to clean up the history // appropriately. // We do null out the context to signify that this handler is no longer // accessing the history. undoContext = null; }
public void start() { if (getActiveLinkedMode() != null) { // for safety; should already be handled in RenameDartElementAction fgActiveLinkedMode.startFullDialog(); return; } ISourceViewer viewer = fEditor.getViewer(); IDocument document = viewer.getDocument(); fOriginalSelection = viewer.getSelectedRange(); int offset = fOriginalSelection.x; try { fLinkedPositionGroup = new LinkedPositionGroup(); prepareElement(); if (fDartElement == null) { return; } if (viewer instanceof ITextViewerExtension6) { IUndoManager undoManager = ((ITextViewerExtension6) viewer).getUndoManager(); if (undoManager instanceof IUndoManagerExtension) { IUndoManagerExtension undoManagerExtension = (IUndoManagerExtension) undoManager; IUndoContext undoContext = undoManagerExtension.getUndoContext(); IOperationHistory operationHistory = OperationHistoryFactory.getOperationHistory(); fStartingUndoOperation = operationHistory.getUndoOperation(undoContext); } } fOriginalName = nameNode.getName(); final int pos = nameNode.getOffset(); final List<ASTNode> sameNodes = Lists.newArrayList(); nameNode .getRoot() .accept( new RecursiveASTVisitor<Void>() { @Override public Void visitSimpleIdentifier(SimpleIdentifier node) { Element element = node.getElement(); element = getCanonicalElement(element); if (Objects.equal(element, fDartElement)) { sameNodes.add(node); } return super.visitSimpleIdentifier(node); } }); // TODO: copied from LinkedNamesAssistProposal#apply(..): // sort for iteration order, starting with the node @ offset Collections.sort( sameNodes, new Comparator<ASTNode>() { @Override public int compare(ASTNode o1, ASTNode o2) { return rank(o1) - rank(o2); } /** * Returns the absolute rank of an <code>ASTNode</code>. Nodes preceding <code>pos * </code> are ranked last. * * @param node the node to compute the rank for * @return the rank of the node with respect to the invocation offset */ private int rank(ASTNode node) { int relativeRank = node.getOffset() + node.getLength() - pos; if (relativeRank < 0) { return Integer.MAX_VALUE + relativeRank; } else { return relativeRank; } } }); for (int i = 0; i < sameNodes.size(); i++) { ASTNode elem = sameNodes.get(i); LinkedPosition linkedPosition = new LinkedPosition(document, elem.getOffset(), elem.getLength(), i); if (i == 0) { fNamePosition = linkedPosition; } fLinkedPositionGroup.addPosition(linkedPosition); } fLinkedModeModel = new LinkedModeModel(); fLinkedModeModel.addGroup(fLinkedPositionGroup); fLinkedModeModel.forceInstall(); fLinkedModeModel.addLinkingListener(new EditorHighlightingSynchronizer(fEditor)); fLinkedModeModel.addLinkingListener(new EditorSynchronizer()); LinkedModeUI ui = new EditorLinkedModeUI(fLinkedModeModel, viewer); ui.setExitPosition(viewer, offset, 0, Integer.MAX_VALUE); ui.setExitPolicy(new ExitPolicy(document)); ui.enter(); viewer.setSelectedRange( fOriginalSelection.x, fOriginalSelection.y); // by default, full word is selected; restore original selection if (viewer instanceof IEditingSupportRegistry) { IEditingSupportRegistry registry = (IEditingSupportRegistry) viewer; registry.register(fFocusEditingSupport); } openSecondaryPopup(); // startAnimation(); fgActiveLinkedMode = this; } catch (BadLocationException e) { DartToolsPlugin.log(e); } }
public void start() { if (getActiveLinkedMode() != null) { // for safety; should already be handled in RenameDartElementAction fgActiveLinkedMode.startFullDialog(); return; } ISourceViewer viewer = fEditor.getViewer(); IDocument document = viewer.getDocument(); fOriginalSelection = viewer.getSelectedRange(); int offset = fOriginalSelection.x; try { DartUnit root = ASTProvider.getASTProvider().getAST(getCompilationUnit(), ASTProvider.WAIT_YES, null); fLinkedPositionGroup = new LinkedPositionGroup(); DartNode selectedNode = NodeFinder.perform(root, fOriginalSelection.x, fOriginalSelection.y); if (!(selectedNode instanceof DartIdentifier)) { return; // TODO: show dialog } DartIdentifier nameNode = (DartIdentifier) selectedNode; if (viewer instanceof ITextViewerExtension6) { IUndoManager undoManager = ((ITextViewerExtension6) viewer).getUndoManager(); if (undoManager instanceof IUndoManagerExtension) { IUndoManagerExtension undoManagerExtension = (IUndoManagerExtension) undoManager; IUndoContext undoContext = undoManagerExtension.getUndoContext(); IOperationHistory operationHistory = OperationHistoryFactory.getOperationHistory(); fStartingUndoOperation = operationHistory.getUndoOperation(undoContext); } } fOriginalName = nameNode.getName(); final int pos = nameNode.getSourceInfo().getOffset(); DartNode[] sameNodes = LinkedNodeFinder.findByNode(root, nameNode); // TODO: copied from LinkedNamesAssistProposal#apply(..): // sort for iteration order, starting with the node @ offset Arrays.sort( sameNodes, new Comparator<DartNode>() { @Override public int compare(DartNode o1, DartNode o2) { return rank(o1) - rank(o2); } /** * Returns the absolute rank of an <code>DartNode</code>. Nodes preceding <code>pos * </code> are ranked last. * * @param node the node to compute the rank for * @return the rank of the node with respect to the invocation offset */ private int rank(DartNode node) { int relativeRank = node.getSourceInfo().getOffset() + node.getSourceInfo().getLength() - pos; if (relativeRank < 0) { return Integer.MAX_VALUE + relativeRank; } else { return relativeRank; } } }); for (int i = 0; i < sameNodes.length; i++) { DartNode elem = sameNodes[i]; LinkedPosition linkedPosition = new LinkedPosition( document, elem.getSourceInfo().getOffset(), elem.getSourceInfo().getLength(), i); if (i == 0) { fNamePosition = linkedPosition; } fLinkedPositionGroup.addPosition(linkedPosition); } fLinkedModeModel = new LinkedModeModel(); fLinkedModeModel.addGroup(fLinkedPositionGroup); fLinkedModeModel.forceInstall(); fLinkedModeModel.addLinkingListener(new EditorHighlightingSynchronizer(fEditor)); fLinkedModeModel.addLinkingListener(new EditorSynchronizer()); LinkedModeUI ui = new EditorLinkedModeUI(fLinkedModeModel, viewer); ui.setExitPosition(viewer, offset, 0, Integer.MAX_VALUE); ui.setExitPolicy(new ExitPolicy(document)); ui.enter(); viewer.setSelectedRange( fOriginalSelection.x, fOriginalSelection.y); // by default, full word is selected; restore original selection if (viewer instanceof IEditingSupportRegistry) { IEditingSupportRegistry registry = (IEditingSupportRegistry) viewer; registry.register(fFocusEditingSupport); } openSecondaryPopup(); // startAnimation(); fgActiveLinkedMode = this; } catch (BadLocationException e) { DartToolsPlugin.log(e); } }
private int getActionRedoPos() { IOperationHistory history = getOperationHistory(); return history.getRedoHistory(page.getUndoContext()).length; }
/** 合并文本段 ; */ public void mergeSegment() { XLFHandler handler = xliffEditor.getXLFHandler(); List<String> lstRowId = xliffEditor.getSelectedRowIds(); List<String> lstAllRowId = xliffEditor.getXLFHandler().getAllRowIds(); Shell shell = xliffEditor.getSite().getShell(); if (lstRowId.size() < 2) { MessageDialog.openInformation( shell, Messages.getString("utils.NattableUtil.mergeSegment.msgTitle"), Messages.getString("utils.NattableUtil.mergeSegment.msg1")); return; } Collections.sort(lstRowId, new SortRowIdComparator()); Collections.sort(lstAllRowId, new SortRowIdComparator()); String rowId1 = lstRowId.get(0); String fileName = RowIdUtil.getFileNameByRowId(rowId1); if (fileName == null) { return; } if (handler.isLocked(rowId1)) { MessageDialog.openInformation( shell, Messages.getString("utils.NattableUtil.mergeSegment.msgTitle"), Messages.getString("utils.NattableUtil.mergeSegment.msg3")); return; } for (int i = 1; i < lstRowId.size(); i++) { String rowId = lstRowId.get(i); if (handler.isLocked(rowId)) { MessageDialog.openInformation( shell, Messages.getString("utils.NattableUtil.mergeSegment.msgTitle"), Messages.getString("utils.NattableUtil.mergeSegment.msg3")); return; } String fileName2 = RowIdUtil.getFileNameByRowId(rowId); // 数组集合必须在一个文件中才能合并 if (fileName2 == null || !fileName.equals(fileName2)) { MessageDialog.openInformation( shell, Messages.getString("utils.NattableUtil.mergeSegment.msgTitle"), Messages.getString("utils.NattableUtil.mergeSegment.msg4")); return; } // 判断所选文本段是否连续 String strCurTuId = RowIdUtil.getTUIdByRowId(rowId); String strPreTuId = RowIdUtil.getTUIdByRowId(lstRowId.get(i - 1)); if (strCurTuId == null || strPreTuId == null) { return; } if ((lstAllRowId.indexOf(rowId) - lstAllRowId.indexOf(lstRowId.get(i - 1))) != 1) { MessageDialog.openInformation( shell, Messages.getString("utils.NattableUtil.mergeSegment.msgTitle"), Messages.getString("utils.NattableUtil.mergeSegment.msg5")); return; } else { String curOriginal = RowIdUtil.getOriginalByRowId(rowId); String preOriginal = RowIdUtil.getOriginalByRowId(lstRowId.get(i - 1)); if (!curOriginal.equals(preOriginal)) { MessageDialog.openInformation( shell, Messages.getString("utils.NattableUtil.mergeSegment.msgTitle"), Messages.getString("utils.NattableUtil.mergeSegment.msg5")); return; } } } // Bug #2373:选择全部文本段合并后,无显示内容 if (lstRowId.size() == xliffEditor.getXLFHandler().getRowIds().size()) { xliffEditor.jumpToRow(0); } MergeSegmentOperation mergeOper = new MergeSegmentOperation("merge segment", xliffEditor, handler, lstRowId); IOperationHistory operationHistory = OperationHistoryFactory.getOperationHistory(); try { operationHistory.execute(mergeOper, null, null); } catch (Exception e) { LOGGER.error("", e); } }