From 21a67b28c34dc6308c1272b798b15ccd8fde6fb5 Mon Sep 17 00:00:00 2001 From: Alois Zoitl Date: Mon, 12 Jun 2023 00:00:58 +0200 Subject: [PATCH] Cleaned text example and a bit of digraph2 --- .../editpart/Digraph2NodeEditPart.java | 6 +- .../factory/Digraph2EditPartFactory.java | 3 +- .../examples/text/GraphicalTextViewer.java | 27 ++++---- .../gef/examples/text/SelectionModel.java | 44 ++++++------- .../eclipse/gef/examples/text/TextEditor.java | 31 +++++----- .../gef/examples/text/TextLocation.java | 5 +- .../text/actions/BooleanStyleAction.java | 27 ++++---- .../actions/ChangeFontContributionItem.java | 12 ++-- .../text/actions/MultiStyleAction.java | 4 +- .../actions/ResizeFontContributionItem.java | 5 +- .../actions/StyleComboContributionItem.java | 27 +++++--- .../examples/text/actions/StyleService.java | 15 ++--- .../actions/TextActionBarContributor.java | 5 +- .../examples/text/edit/BlockEditPolicy.java | 62 +++++++++---------- .../examples/text/edit/CompoundTextPart.java | 11 ++-- .../examples/text/edit/ContainerTreePart.java | 47 +++++++------- .../gef/examples/text/edit/DocumentPart.java | 23 +++---- .../examples/text/edit/ExampleTreePart.java | 4 +- .../gef/examples/text/edit/FontCache.java | 18 +++--- .../examples/text/edit/TextRunTreePart.java | 8 ++- .../examples/text/figures/BulletBorder.java | 4 +- .../examples/text/figures/CommentPage.java | 8 ++- .../gef/examples/text/figures/Images.java | 10 +-- .../examples/text/figures/StyleConstants.java | 6 +- .../gef/examples/text/figures/TreeBorder.java | 4 +- .../examples/text/figures/TreeItemBorder.java | 5 +- .../gef/examples/text/model/Block.java | 3 +- .../gef/examples/text/model/CanvasStyle.java | 4 +- .../gef/examples/text/model/Container.java | 20 +++--- .../examples/text/model/InlineContainer.java | 3 +- .../gef/examples/text/model/ModelUtil.java | 16 +++-- .../examples/text/model/TestModelUtil.java | 26 ++++---- .../gef/examples/text/model/TextRun.java | 12 ++-- .../model/commands/ApplyBooleanStyle.java | 6 +- .../text/model/commands/ApplyMultiStyle.java | 9 ++- .../text/model/commands/ChangeString.java | 9 ++- .../model/commands/CompoundEditCommand.java | 42 ++++++------- .../model/commands/CompoundTextCommand.java | 13 +++- .../model/commands/ConvertElementCommand.java | 10 ++- .../model/commands/ExampleTextCommand.java | 5 +- .../model/commands/InsertModelElement.java | 4 ++ .../model/commands/MergeWithPrevious.java | 20 +++--- .../model/commands/NestElementCommand.java | 18 +++--- .../model/commands/ProcessMacroCommand.java | 7 ++- .../model/commands/PromoteElementCommand.java | 9 ++- .../text/model/commands/RemoveRange.java | 7 ++- .../text/model/commands/RemoveText.java | 9 ++- .../model/commands/SingleEditCommand.java | 10 ++- .../text/model/commands/SubdivideElement.java | 11 +++- .../text/tools/SelectionRangeDragTracker.java | 13 ++-- .../gef/examples/text/tools/TextTool.java | 57 ++++++++++------- .../text/wizard/NewFileWizardPage.java | 61 ++++++++---------- .../text/wizard/TextEditorWizard.java | 25 ++++---- 53 files changed, 485 insertions(+), 365 deletions(-) diff --git a/org.eclipse.gef.examples.digraph2/src/org/eclipse/gef/examples/digraph2/editpart/Digraph2NodeEditPart.java b/org.eclipse.gef.examples.digraph2/src/org/eclipse/gef/examples/digraph2/editpart/Digraph2NodeEditPart.java index 057b84128..85187b723 100644 --- a/org.eclipse.gef.examples.digraph2/src/org/eclipse/gef/examples/digraph2/editpart/Digraph2NodeEditPart.java +++ b/org.eclipse.gef.examples.digraph2/src/org/eclipse/gef/examples/digraph2/editpart/Digraph2NodeEditPart.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2007 IBM Corporation and others. + * Copyright (c) 2007, 2023 IBM Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at @@ -74,6 +74,7 @@ protected List getModelTargetConnections() { * @see org.eclipse.gef.NodeEditPart#getSourceConnectionAnchor(org.eclipse.gef. * ConnectionEditPart) */ + @Override public ConnectionAnchor getSourceConnectionAnchor(ConnectionEditPart connection) { return this.sourceAnchor; } @@ -82,6 +83,7 @@ public ConnectionAnchor getSourceConnectionAnchor(ConnectionEditPart connection) * @see org.eclipse.gef.NodeEditPart#getSourceConnectionAnchor(org.eclipse.gef. * Request) */ + @Override public ConnectionAnchor getSourceConnectionAnchor(Request request) { return this.sourceAnchor; } @@ -90,6 +92,7 @@ public ConnectionAnchor getSourceConnectionAnchor(Request request) { * @see org.eclipse.gef.NodeEditPart#getTargetConnectionAnchor(org.eclipse.gef. * ConnectionEditPart) */ + @Override public ConnectionAnchor getTargetConnectionAnchor(ConnectionEditPart connection) { return this.targetAnchor; } @@ -98,6 +101,7 @@ public ConnectionAnchor getTargetConnectionAnchor(ConnectionEditPart connection) * @see org.eclipse.gef.NodeEditPart#getTargetConnectionAnchor(org.eclipse.gef. * Request) */ + @Override public ConnectionAnchor getTargetConnectionAnchor(Request request) { return this.targetAnchor; } diff --git a/org.eclipse.gef.examples.digraph2/src/org/eclipse/gef/examples/digraph2/factory/Digraph2EditPartFactory.java b/org.eclipse.gef.examples.digraph2/src/org/eclipse/gef/examples/digraph2/factory/Digraph2EditPartFactory.java index cf4a86a9f..69ae22f66 100644 --- a/org.eclipse.gef.examples.digraph2/src/org/eclipse/gef/examples/digraph2/factory/Digraph2EditPartFactory.java +++ b/org.eclipse.gef.examples.digraph2/src/org/eclipse/gef/examples/digraph2/factory/Digraph2EditPartFactory.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2007 IBM Corporation and others. + * Copyright (c) 2007, 2023 IBM Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at @@ -31,6 +31,7 @@ public class Digraph2EditPartFactory implements EditPartFactory { * @see org.eclipse.gef.EditPartFactory#createEditPart(org.eclipse.gef.EditPart, * java.lang.Object) */ + @Override public EditPart createEditPart(EditPart context, Object model) { EditPart editPart = null; if (model instanceof Digraph2Graph) { diff --git a/org.eclipse.gef.examples.text/src/org/eclipse/gef/examples/text/GraphicalTextViewer.java b/org.eclipse.gef.examples.text/src/org/eclipse/gef/examples/text/GraphicalTextViewer.java index 192d05d54..c82b15db5 100644 --- a/org.eclipse.gef.examples.text/src/org/eclipse/gef/examples/text/GraphicalTextViewer.java +++ b/org.eclipse.gef.examples.text/src/org/eclipse/gef/examples/text/GraphicalTextViewer.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2004, 2010 IBM Corporation and others. + * Copyright (c) 2004, 2023 IBM Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at @@ -16,7 +16,6 @@ import java.util.List; import org.eclipse.core.runtime.Assert; -import org.eclipse.draw2d.LightweightSystem; import org.eclipse.draw2d.Viewport; import org.eclipse.draw2d.geometry.Rectangle; import org.eclipse.gef.EditDomain; @@ -118,6 +117,7 @@ public void setSelectionModel(SelectionModel selection) { fireSelectionChanged(); } + @Override public void appendSelection(EditPart editpart) { if (focusPart != editpart) setFocus(null); @@ -127,19 +127,22 @@ public void appendSelection(EditPart editpart) { select(editpart); } + @Override public void deselect(EditPart editpart) { if (selectionModel != null) setSelectionModel(selectionModel.getExcludedSelection(editpart)); } + @Override public void deselectAll() { setSelectionModel(null); } + @Override public void select(EditPart editpart) { if (focusPart != editpart) setFocus(null); - ArrayList list = new ArrayList(); + ArrayList list = new ArrayList<>(); list.add(editpart); setSelectionModel(createSelectionModel(null, null, list, null)); } @@ -147,9 +150,11 @@ public void select(EditPart editpart) { // @TODO:Pratik Hack. This shouldn't be here. CommandStack should be doing // this automatically. // You can make that change once you remove the GraphicalTextViewer class. + @Override public void setEditDomain(EditDomain domain) { super.setEditDomain(domain); getEditDomain().getCommandStack().addCommandStackEventListener(new CommandStackEventListener() { + @Override public void stackChanged(CommandStackEvent event) { if (!(event.getCommand() instanceof TextCommand) || getSelectionRange() == null) return; @@ -166,6 +171,7 @@ else if (event.getDetail() == CommandStack.POST_UNDO) }); } + @Override public void setSelection(ISelection newSelection) { if (newSelection != null) setSelectionModel(createSelectionModel(newSelection, null, null, null)); @@ -173,13 +179,14 @@ public void setSelection(ISelection newSelection) { setSelectionModel(null); } + @Override public ISelection getSelection() { if (selectionModel != null) return selectionModel.getSelection(); return new StructuredSelection(getContents()); } - protected SelectionModel createSelectionModel(ISelection selection, SelectionRange range, List parts, + private static SelectionModel createSelectionModel(ISelection selection, SelectionRange range, List parts, EditPart container) { if (selection instanceof IStructuredSelection) return new SelectionModel(selection); @@ -189,7 +196,8 @@ protected SelectionModel createSelectionModel(ISelection selection, SelectionRan /** * @see org.eclipse.gef.ui.parts.AbstractEditPartViewer#getSelectedEditParts() */ - public List getSelectedEditParts() { + @Override + public List getSelectedEditParts() { return primGetSelectedEditParts(); } @@ -200,18 +208,15 @@ public List getSelectedEditParts() { * @deprecated * @see org.eclipse.gef.ui.parts.AbstractEditPartViewer#primGetSelectedEditParts() */ - protected List primGetSelectedEditParts() { + @Override + protected List primGetSelectedEditParts() { if (selectionModel != null) return selectionModel.getSelectedEditParts(); - return Collections.EMPTY_LIST; + return Collections.emptyList(); } public boolean isTextSelected() { return selectionModel != null && selectionModel.isTextSelected(); } - protected LightweightSystem getLightweightSystem() { - return super.getLightweightSystem(); - } - } \ No newline at end of file diff --git a/org.eclipse.gef.examples.text/src/org/eclipse/gef/examples/text/SelectionModel.java b/org.eclipse.gef.examples.text/src/org/eclipse/gef/examples/text/SelectionModel.java index b6782b4b8..47351ecde 100644 --- a/org.eclipse.gef.examples.text/src/org/eclipse/gef/examples/text/SelectionModel.java +++ b/org.eclipse.gef.examples.text/src/org/eclipse/gef/examples/text/SelectionModel.java @@ -33,23 +33,23 @@ public class SelectionModel { private final SelectionRange selectionRange; private final EditPart selectionContainer; - private final List constantSelection; + private final List constantSelection; + @SuppressWarnings("unchecked") public SelectionModel(ISelection selection) { - this(null, selection instanceof IStructuredSelection ? ((IStructuredSelection) selection).toList() : null, - null); + this(null, selection instanceof IStructuredSelection structSel ? structSel.toList() : null, null); } - public SelectionModel(SelectionRange range, List selectedParts, EditPart container) { + public SelectionModel(SelectionRange range, List selectedParts, EditPart container) { selectionRange = range; selectionContainer = container; - constantSelection = selectedParts == null ? Collections.EMPTY_LIST + constantSelection = selectedParts == null ? Collections.emptyList() : Collections.unmodifiableList(selectedParts); } protected void applySelectedParts() { if (!constantSelection.isEmpty()) { - Iterator itr = constantSelection.iterator(); + Iterator itr = constantSelection.iterator(); while (true) { EditPart part = (EditPart) itr.next(); if (!itr.hasNext()) { @@ -64,7 +64,7 @@ protected void applySelectedParts() { protected void applySelectionRange() { SelectionRange range = getSelectionRange(); if (range != null) { - List currentSelection = range.getSelectedParts(); + List currentSelection = range.getSelectedParts(); for (int i = 0; i < currentSelection.size(); i++) { TextEditPart textpart = (TextEditPart) currentSelection.get(i); textpart.setSelection(0, textpart.getLength()); @@ -85,23 +85,20 @@ public void deselect() { } protected void deselectSelectedParts() { - for (Iterator itr = constantSelection.iterator(); itr.hasNext();) - ((EditPart) itr.next()).setSelected(EditPart.SELECTED_NONE); + constantSelection.forEach(ep -> ep.setSelected(EditPart.SELECTED_NONE)); } protected void deselectSelectionRange() { SelectionRange range = getSelectionRange(); if (range != null) { - List selection = range.getSelectedParts(); - for (int i = 0; i < selection.size(); i++) - ((TextEditPart) selection.get(i)).setSelection(-1, -1); + range.getSelectedParts().forEach(ep -> ((TextEditPart) ep).setSelection(-1, -1)); } } + @Override public boolean equals(Object obj) { boolean result = obj == this; - if (!result && obj instanceof SelectionModel) { - SelectionModel other = (SelectionModel) obj; + if (!result && obj instanceof SelectionModel other) { EditPart otherContainer = other.getSelectionContainer(); SelectionRange otherRange = other.getSelectionRange(); result = constantSelection.equals(other.getSelectedEditParts()) @@ -113,16 +110,14 @@ public boolean equals(Object obj) { } public SelectionModel getAppendedSelection(EditPart newPart) { - ArrayList list = new ArrayList(constantSelection); + ArrayList list = new ArrayList<>(constantSelection); list.remove(newPart); list.add(newPart); return new SelectionModel(selectionRange, list, selectionContainer); } public SelectionModel getExcludedSelection(EditPart exclude) { - // if (constantSelection.isEmpty()) - // return this; - ArrayList list = new ArrayList(constantSelection); + ArrayList list = new ArrayList<>(constantSelection); list.remove(exclude); return new SelectionModel(selectionRange, list, selectionContainer); } @@ -130,10 +125,10 @@ public SelectionModel getExcludedSelection(EditPart exclude) { public EditPart getFocusPart() { if (constantSelection.isEmpty()) return null; - return (EditPart) constantSelection.get(constantSelection.size() - 1); + return constantSelection.get(constantSelection.size() - 1); } - public List getSelectedEditParts() { + public List getSelectedEditParts() { return constantSelection; } @@ -158,12 +153,9 @@ public void applySelection(SelectionModel old) { // Convert to HashSet to optimize performance. if (!old.getSelectedEditParts().isEmpty()) { - Collection editparts = new HashSet(constantSelection); - for (Iterator itr = old.getSelectedEditParts().iterator(); itr.hasNext();) { - EditPart part = (EditPart) itr.next(); - if (!editparts.contains(part)) - part.setSelected(EditPart.SELECTED_NONE); - } + Collection editparts = new HashSet<>(constantSelection); + old.getSelectedEditParts().stream().filter(part -> !editparts.contains(part)) + .forEach(part -> part.setSelected(EditPart.SELECTED_NONE)); } applySelectedParts(); diff --git a/org.eclipse.gef.examples.text/src/org/eclipse/gef/examples/text/TextEditor.java b/org.eclipse.gef.examples.text/src/org/eclipse/gef/examples/text/TextEditor.java index b2276a948..8f55f3d6b 100644 --- a/org.eclipse.gef.examples.text/src/org/eclipse/gef/examples/text/TextEditor.java +++ b/org.eclipse.gef.examples.text/src/org/eclipse/gef/examples/text/TextEditor.java @@ -120,8 +120,8 @@ public TextOutlinePage(EditPartViewer viewer) { treeViewer.setEditPartFactory(new EditPartFactory() { @Override public EditPart createEditPart(EditPart context, Object model) { - if (model instanceof Container) - return new ContainerTreePart(model); + if (model instanceof Container cont) + return new ContainerTreePart(cont); return new TextRunTreePart(model); } }); @@ -322,30 +322,29 @@ protected void setInput(IEditorInput input) { if (doc == null) { doc = new Block(Container.TYPE_ROOT); Container preface = new Block(Container.TYPE_PARAGRAPH); - preface.add(new TextRun("package org.eclipse.gef.examples.text")); + preface.add(new TextRun("package org.eclipse.gef.examples.text")); //$NON-NLS-1$ doc.add(preface); Container imports = new Block(Container.TYPE_IMPORT_DECLARATIONS); doc.add(imports); - imports.add(new TextRun("org.eclipse.draw2d", TextRun.TYPE_IMPORT)); - imports.add(new TextRun("org.eclipse.gef", TextRun.TYPE_IMPORT)); + imports.add(new TextRun("org.eclipse.draw2d", TextRun.TYPE_IMPORT)); //$NON-NLS-1$ + imports.add(new TextRun("org.eclipse.gef", TextRun.TYPE_IMPORT)); //$NON-NLS-1$ // for (int i = 0; i < 400; i++) { Container block = new Block(Container.TYPE_COMMENT); block.add( - new TextRun("Copyright (c) 2005 IBM Corporation and others. All rights reserved. This program and " - + "the accompanying materials are made available under the terms of the Eclipse Public " - + "License v1.0 which accompanies this distribution, and is available at " - + "http://www.eclipse.org/legal/epl-v10.html (\u7325\u7334\u7329\u7325\u7334\u7329\u7325\u7334\u7329\u7325\u7334\u7329\u7325\u7334\u7329\u7325\u7334\u7329\u7325\u7334\u7329\u7325\u7334\u7329\u7325\u7334\u7329\u7325\u7334\u7329\u7325\u7334\u7329\u7325\u7334\u7329\u7325\u7334\u7329\u7325\u7334\u7329\u7325\u7334\u7329\u7325\u7334\u7329\u7325\u7334\u7329\u7325\u7334\u7329\u7325\u7334\u7329\u7325\u7334\u7329\u7325\u7334\u7329\u7325\u7334\u7329\u7325\u7334\u7329\u7325\u7334\u7329\u7325\u7334\u7329\u7325\u7334\u7329)\r\n" - + "Contributors:\n IBM Corporation - initial API and implementation\n" - + "\u0630\u0628\u063a \u0634\u0635\u062c\u062d (Saeed Anwar) - \u0634\u0635\u062c\u062d " - + "\u0638\u0635\u0634\u0637\u0635\u0639\u0633 \u0635\u0639\u0633\u0640 \u0630\u0628\u063a (Bug 113700)")); + new TextRun("Copyright (c) 2005 IBM Corporation and others. All rights reserved. This program and " //$NON-NLS-1$ + + "the accompanying materials are made available under the terms of the Eclipse Public " //$NON-NLS-1$ + + "License v1.0 which accompanies this distribution, and is available at " //$NON-NLS-1$ + + "http://www.eclipse.org/legal/epl-v10.html (\u7325\u7334\u7329\u7325\u7334\u7329\u7325\u7334\u7329\u7325\u7334\u7329\u7325\u7334\u7329\u7325\u7334\u7329\u7325\u7334\u7329\u7325\u7334\u7329\u7325\u7334\u7329\u7325\u7334\u7329\u7325\u7334\u7329\u7325\u7334\u7329\u7325\u7334\u7329\u7325\u7334\u7329\u7325\u7334\u7329\u7325\u7334\u7329\u7325\u7334\u7329\u7325\u7334\u7329\u7325\u7334\u7329\u7325\u7334\u7329\u7325\u7334\u7329\u7325\u7334\u7329\u7325\u7334\u7329\u7325\u7334\u7329\u7325\u7334\u7329\u7325\u7334\u7329)\r\n" //$NON-NLS-1$ + + "Contributors:\n IBM Corporation - initial API and implementation\n" //$NON-NLS-1$ + + "\u0630\u0628\u063a \u0634\u0635\u062c\u062d (Saeed Anwar) - \u0634\u0635\u062c\u062d " //$NON-NLS-1$ + + "\u0638\u0635\u0634\u0637\u0635\u0639\u0633 \u0635\u0639\u0633\u0640 \u0630\u0628\u063a (Bug 113700)")); //$NON-NLS-1$ doc.add(block); Container code = new Block(Container.TYPE_PARAGRAPH); - code.getStyle().setFontFamily("Courier New"); + code.getStyle().setFontFamily("Courier New"); //$NON-NLS-1$ doc.add(code); - code.add(new TextRun( - "public void countToANumber(int limit) {\n" + " for (int i = 0; i < limit; i++)\n" - + " System.out.println(\"Counting: \" + i); //$NON-NLS-1$\n\n" + "}", + code.add(new TextRun("public void countToANumber(int limit) {\n" + " for (int i = 0; i < limit; i++)\n" //$NON-NLS-1$ //$NON-NLS-2$ + + " System.out.println(\"Counting: \" + i); //$NON-NLS-1$\n\n" + "}", //$NON-NLS-1$ //$NON-NLS-2$ TextRun.TYPE_CODE)); // } } diff --git a/org.eclipse.gef.examples.text/src/org/eclipse/gef/examples/text/TextLocation.java b/org.eclipse.gef.examples.text/src/org/eclipse/gef/examples/text/TextLocation.java index 17e18be1b..a69050953 100644 --- a/org.eclipse.gef.examples.text/src/org/eclipse/gef/examples/text/TextLocation.java +++ b/org.eclipse.gef.examples.text/src/org/eclipse/gef/examples/text/TextLocation.java @@ -35,9 +35,9 @@ public TextLocation(TextEditPart part, int offset) { /** * @see java.lang.Object#equals(java.lang.Object) */ + @Override public boolean equals(Object obj) { - if (obj instanceof TextLocation) { - TextLocation other = (TextLocation) obj; + if (obj instanceof TextLocation other) { return other.offset == offset && other.part == part; } return false; @@ -46,6 +46,7 @@ public boolean equals(Object obj) { /** * @see java.lang.Object#hashCode() */ + @Override public int hashCode() { return part.hashCode() << 11 ^ offset; } diff --git a/org.eclipse.gef.examples.text/src/org/eclipse/gef/examples/text/actions/BooleanStyleAction.java b/org.eclipse.gef.examples.text/src/org/eclipse/gef/examples/text/actions/BooleanStyleAction.java index 7ff05ce40..06f167369 100644 --- a/org.eclipse.gef.examples.text/src/org/eclipse/gef/examples/text/actions/BooleanStyleAction.java +++ b/org.eclipse.gef.examples.text/src/org/eclipse/gef/examples/text/actions/BooleanStyleAction.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2004, 2010 IBM Corporation and others. + * Copyright (c) 2004, 2023 IBM Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at @@ -23,11 +23,9 @@ public class BooleanStyleAction extends Action { protected String property; protected StyleService service; - private StyleListener styleListener = new StyleListener() { - public void styleChanged(String styleID) { - if (styleID == null || styleID.equals(getId())) - refresh(); - } + private StyleListener styleListener = styleID -> { + if (styleID == null || styleID.equals(getId())) + refresh(); }; public BooleanStyleAction(StyleService service, String styleID, String property) { @@ -45,34 +43,35 @@ static void configureStyleAction(IAction a) { String styleID = a.getId(); a.setActionDefinitionId(styleID); if (styleID.equals(TextActionConstants.STYLE_BOLD)) { - a.setText("Bold"); + a.setText("Bold"); //$NON-NLS-1$ a.setImageDescriptor(InternalImages.DESC_BOLD); } else if (styleID.equals(TextActionConstants.STYLE_ITALIC)) { - a.setText("Italics"); + a.setText("Italics"); //$NON-NLS-1$ a.setImageDescriptor(InternalImages.DESC_ITALIC); } else if (styleID.equals(TextActionConstants.STYLE_UNDERLINE)) { - a.setText("Underline"); + a.setText("Underline"); //$NON-NLS-1$ a.setImageDescriptor(InternalImages.DESC_UNDERLINE); } else if (styleID.equals(TextActionConstants.BLOCK_ALIGN_CENTER)) { - a.setText("Center"); + a.setText("Center"); //$NON-NLS-1$ a.setImageDescriptor(InternalImages.DESC_BLOCK_ALIGN_CENTER); } else if (styleID.equals(TextActionConstants.BLOCK_ALIGN_LEFT)) { - a.setText("Left"); + a.setText("Left"); //$NON-NLS-1$ a.setImageDescriptor(InternalImages.DESC_BLOCK_ALIGN_LEFT); } else if (styleID.equals(TextActionConstants.BLOCK_ALIGN_RIGHT)) { - a.setText("Right"); + a.setText("Right"); //$NON-NLS-1$ a.setImageDescriptor(InternalImages.DESC_BLOCK_ALIGN_RIGHT); } else if (styleID.equals(TextActionConstants.BLOCK_LTR)) { - a.setText("Left to Right"); + a.setText("Left to Right"); //$NON-NLS-1$ a.setImageDescriptor(InternalImages.DESC_BLOCK_LTR); } else if (styleID.equals(TextActionConstants.BLOCK_RTL)) { - a.setText("Right to Left"); + a.setText("Right to Left"); //$NON-NLS-1$ a.setImageDescriptor(InternalImages.DESC_BLOCK_RTL); } else { throw new RuntimeException("The given style ID was not recognized"); //$NON-NLS-1$ } } + @Override public void run() { service.setStyle(property, isChecked() ? Boolean.TRUE : Boolean.FALSE); } diff --git a/org.eclipse.gef.examples.text/src/org/eclipse/gef/examples/text/actions/ChangeFontContributionItem.java b/org.eclipse.gef.examples.text/src/org/eclipse/gef/examples/text/actions/ChangeFontContributionItem.java index 027c9b615..20ce29dd2 100644 --- a/org.eclipse.gef.examples.text/src/org/eclipse/gef/examples/text/actions/ChangeFontContributionItem.java +++ b/org.eclipse.gef.examples.text/src/org/eclipse/gef/examples/text/actions/ChangeFontContributionItem.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2004, 2010 IBM Corporation and others. + * Copyright (c) 2004, 2023 IBM Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at @@ -28,10 +28,9 @@ public class ChangeFontContributionItem extends StyleComboContributionItem { private static final String[] FONT_NAMES; static { - Set set = new HashSet(); - FontData[] fonts = Display.getCurrent().getFontList(null, true); - for (int i = 0; i < fonts.length; i++) - set.add(fonts[i].getName()); + Set set = new HashSet<>(); + for (FontData font : Display.getCurrent().getFontList(null, true)) + set.add(font.getName()); FONT_NAMES = new String[set.size()]; set.toArray(FONT_NAMES); Arrays.sort(FONT_NAMES); @@ -41,14 +40,17 @@ public ChangeFontContributionItem(IPartService service) { super(service); } + @Override protected String[] getItems() { return FONT_NAMES; } + @Override protected String getProperty() { return Style.PROPERTY_FONT; } + @Override protected void handleWidgetSelected(SelectionEvent e) { int index = findIndexOf(combo.getText()); if (index >= 0 && !getItems()[index].equals(styleService.getStyle(getProperty()))) diff --git a/org.eclipse.gef.examples.text/src/org/eclipse/gef/examples/text/actions/MultiStyleAction.java b/org.eclipse.gef.examples.text/src/org/eclipse/gef/examples/text/actions/MultiStyleAction.java index 598c9ed17..603905be8 100644 --- a/org.eclipse.gef.examples.text/src/org/eclipse/gef/examples/text/actions/MultiStyleAction.java +++ b/org.eclipse.gef.examples.text/src/org/eclipse/gef/examples/text/actions/MultiStyleAction.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2005, 2010 IBM Corporation and others. + * Copyright (c) 2005, 2023 IBM Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at @@ -19,10 +19,12 @@ public MultiStyleAction(StyleService service, String styleID, String property, O this.value = value; } + @Override public void run() { service.setStyle(property, isChecked() ? value : Integer.valueOf(0)); } + @Override public void refresh() { setChecked(value.equals(service.getStyle(property))); setEnabled(service.getStyleState(property).equals(StyleService.STATE_EDITABLE)); diff --git a/org.eclipse.gef.examples.text/src/org/eclipse/gef/examples/text/actions/ResizeFontContributionItem.java b/org.eclipse.gef.examples.text/src/org/eclipse/gef/examples/text/actions/ResizeFontContributionItem.java index 1261169f9..f69ec3e69 100644 --- a/org.eclipse.gef.examples.text/src/org/eclipse/gef/examples/text/actions/ResizeFontContributionItem.java +++ b/org.eclipse.gef.examples.text/src/org/eclipse/gef/examples/text/actions/ResizeFontContributionItem.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2004, 2010 IBM Corporation and others. + * Copyright (c) 2004, 2023 IBM Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at @@ -27,14 +27,17 @@ public ResizeFontContributionItem(IPartService service) { super(service); } + @Override protected String[] getItems() { return INIT_SIZES; } + @Override protected String getProperty() { return Style.PROPERTY_FONT_SIZE; } + @Override protected void handleWidgetSelected(SelectionEvent e) { Integer fontSize = null; try { diff --git a/org.eclipse.gef.examples.text/src/org/eclipse/gef/examples/text/actions/StyleComboContributionItem.java b/org.eclipse.gef.examples.text/src/org/eclipse/gef/examples/text/actions/StyleComboContributionItem.java index 9655f24b7..2da5cc5ee 100644 --- a/org.eclipse.gef.examples.text/src/org/eclipse/gef/examples/text/actions/StyleComboContributionItem.java +++ b/org.eclipse.gef.examples.text/src/org/eclipse/gef/examples/text/actions/StyleComboContributionItem.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2004, 2010 IBM Corporation and others. + * Copyright (c) 2004, 2023 IBM Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at @@ -37,37 +37,44 @@ public abstract class StyleComboContributionItem extends ContributionItem { protected ToolItem toolItem; protected StyleService styleService; protected IPartService partService; - protected StyleListener styleListener = new StyleListener() { - public void styleChanged(String styleID) { - if (styleID == null || styleID.equals(getProperty())) - refresh(); - } + protected StyleListener styleListener = styleID -> { + if (styleID == null || styleID.equals(getProperty())) + refresh(); }; + protected IPartListener2 partListener = new IPartListener2() { + @Override public void partActivated(IWorkbenchPartReference partRef) { IWorkbenchPart part = partRef.getPart(false); if (part instanceof TextEditor) setStyleService(part.getAdapter(StyleService.class)); } + @Override public void partBroughtToTop(IWorkbenchPartReference partRef) { } + @Override public void partClosed(IWorkbenchPartReference partRef) { } + @Override public void partDeactivated(IWorkbenchPartReference partRef) { } + @Override public void partOpened(IWorkbenchPartReference partRef) { } + @Override public void partHidden(IWorkbenchPartReference partRef) { } + @Override public void partVisible(IWorkbenchPartReference partRef) { } + @Override public void partInputChanged(IWorkbenchPartReference partRef) { } }; @@ -88,19 +95,23 @@ public StyleComboContributionItem(IPartService service) { protected Control createControl(Composite parent) { combo = new Combo(parent, SWT.DROP_DOWN); combo.addSelectionListener(new SelectionListener() { + @Override public void widgetDefaultSelected(SelectionEvent e) { handleWidgetSelected(e); } + @Override public void widgetSelected(SelectionEvent e) { handleWidgetSelected(e); } }); combo.addFocusListener(new FocusListener() { + @Override public void focusGained(FocusEvent e) { // do nothing } + @Override public void focusLost(FocusEvent e) { refresh(); } @@ -118,6 +129,7 @@ public void focusLost(FocusEvent e) { * * @see org.eclipse.jface.action.IContributionItem#dispose() */ + @Override public void dispose() { if (styleService != null) styleService.removeStyleListener(styleListener); @@ -132,6 +144,7 @@ public void dispose() { * @param parent The ToolBar to add the new control to * @param index Index */ + @Override public void fill(ToolBar parent, int index) { toolItem = new ToolItem(parent, SWT.SEPARATOR, index); Control control = createControl(parent); @@ -167,7 +180,7 @@ protected void refresh() { Object style = styleService.getStyle(getProperty()); String value = style.toString(); if (StyleService.UNDEFINED.equals(style)) - value = ""; + value = ""; //$NON-NLS-1$ int index = findIndexOf(value); if (index >= 0) combo.select(index); diff --git a/org.eclipse.gef.examples.text/src/org/eclipse/gef/examples/text/actions/StyleService.java b/org.eclipse.gef.examples.text/src/org/eclipse/gef/examples/text/actions/StyleService.java index 636fcaee4..91e2f2c96 100644 --- a/org.eclipse.gef.examples.text/src/org/eclipse/gef/examples/text/actions/StyleService.java +++ b/org.eclipse.gef.examples.text/src/org/eclipse/gef/examples/text/actions/StyleService.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2004, 2010 IBM Corporation and others. + * Copyright (c) 2004, 2023 IBM Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at @@ -23,13 +23,9 @@ public class StyleService { public static final Object STATE_READ_ONLY = new Object(); public static final Object UNDEFINED = new Object(); - private List listeners = new ArrayList(); + private List listeners = new ArrayList<>(); private StyleProvider provider; - private StyleListener providerListener = new StyleListener() { - public void styleChanged(String styleID) { - propogateChange(styleID); - } - }; + private StyleListener providerListener = StyleService.this::propogateChange; /** * Constructs a new StyleService object @@ -58,10 +54,7 @@ public Object getStyleState(String styleID) { * @param styleID */ protected void propogateChange(String styleID) { - for (int i = 0; i < listeners.size(); i++) { - StyleListener listener = (StyleListener) listeners.get(i); - listener.styleChanged(styleID); - } + listeners.forEach(listener -> listener.styleChanged(styleID)); } /** diff --git a/org.eclipse.gef.examples.text/src/org/eclipse/gef/examples/text/actions/TextActionBarContributor.java b/org.eclipse.gef.examples.text/src/org/eclipse/gef/examples/text/actions/TextActionBarContributor.java index 722693da2..153796946 100644 --- a/org.eclipse.gef.examples.text/src/org/eclipse/gef/examples/text/actions/TextActionBarContributor.java +++ b/org.eclipse.gef.examples.text/src/org/eclipse/gef/examples/text/actions/TextActionBarContributor.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2004, 2010 IBM Corporation and others. + * Copyright (c) 2004, 2023 IBM Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at @@ -23,6 +23,7 @@ public class TextActionBarContributor extends ActionBarContributor { /** * @see ActionBarContributor#buildActions() */ + @Override protected void buildActions() { addRetargetAction(new StyleRetargetAction(TextActionConstants.STYLE_BOLD)); addRetargetAction(new StyleRetargetAction(TextActionConstants.STYLE_ITALIC)); @@ -34,6 +35,7 @@ protected void buildActions() { addRetargetAction(new StyleRetargetAction(TextActionConstants.BLOCK_RTL)); } + @Override public void contributeToToolBar(IToolBarManager toolbar) { toolbar.add(new ChangeFontContributionItem(getPage())); toolbar.add(new ResizeFontContributionItem(getPage())); @@ -53,6 +55,7 @@ public void contributeToToolBar(IToolBarManager toolbar) { /** * @see ActionBarContributor#declareGlobalActionKeys() */ + @Override protected void declareGlobalActionKeys() { } diff --git a/org.eclipse.gef.examples.text/src/org/eclipse/gef/examples/text/edit/BlockEditPolicy.java b/org.eclipse.gef.examples.text/src/org/eclipse/gef/examples/text/edit/BlockEditPolicy.java index 8e2c8fa5b..92fcb4f20 100644 --- a/org.eclipse.gef.examples.text/src/org/eclipse/gef/examples/text/edit/BlockEditPolicy.java +++ b/org.eclipse.gef.examples.text/src/org/eclipse/gef/examples/text/edit/BlockEditPolicy.java @@ -11,8 +11,6 @@ package org.eclipse.gef.examples.text.edit; -import java.util.Iterator; - import org.eclipse.core.runtime.Assert; import org.eclipse.gef.EditPart; import org.eclipse.gef.Request; @@ -46,29 +44,29 @@ */ public class BlockEditPolicy extends GraphicalEditPolicy { - private Command checkForConversion(TextLocation location) { + private static Command checkForConversion(TextLocation location) { TextRun run = (TextRun) location.part.getModel(); String prefix = run.getText().substring(0, location.offset); - if (prefix.endsWith("")) { + if (prefix.endsWith("")) { //$NON-NLS-1$ Container converted = new InlineContainer(Container.TYPE_INLINE); converted.getStyle().setBold(true); - TextRun boldText = new TextRun("BOLD"); + TextRun boldText = new TextRun("BOLD"); //$NON-NLS-1$ converted.add(boldText); ProcessMacroCommand command = new ProcessMacroCommand(run, location.offset - 3, location.offset, converted, new ModelLocation(boldText, 0)); command.setEndLocation(new ModelLocation(boldText, 1)); return command; - } else if (prefix.equals("()")) { + } else if (prefix.equals("()")) { //$NON-NLS-1$ ConvertElementCommand command; Container list = new Block(Container.TYPE_BULLETED_LIST); - TextRun bullet = new TextRun("", TextRun.TYPE_BULLET); + TextRun bullet = new TextRun("", TextRun.TYPE_BULLET); //$NON-NLS-1$ list.add(bullet); command = new ConvertElementCommand(run, 0, 2, list, new ModelLocation(bullet, 0)); return command; - } else if (prefix.equals("import")) { + } else if (prefix.equals("import")) { //$NON-NLS-1$ ConvertElementCommand command; Container imports = new Block(Container.TYPE_IMPORT_DECLARATIONS); - TextRun statement = new TextRun("", TextRun.TYPE_IMPORT); + TextRun statement = new TextRun("", TextRun.TYPE_IMPORT); //$NON-NLS-1$ imports.add(statement); command = new ConvertElementCommand(run, 0, 6, imports, new ModelLocation(statement, 0)); return command; @@ -76,12 +74,12 @@ private Command checkForConversion(TextLocation location) { return null; } - private Command getBackspaceCommand(TextRequest request) { + private static Command getBackspaceCommand(TextRequest request) { TextLocation where = request.getSelectionRange().begin; CompoundEditCommand command = (CompoundEditCommand) request.getPreviousCommand(); if (command == null) - command = new CompoundEditCommand("Backspace"); + command = new CompoundEditCommand("Backspace"); //$NON-NLS-1$ TextRun run = (TextRun) where.part.getModel(); MiniEdit remove; @@ -100,6 +98,7 @@ private Command getBackspaceCommand(TextRequest request) { return command; } + @Override public Command getCommand(Request request) { if (TextRequest.REQ_STYLE == request.getType()) return getTextStyleApplication((TextRequest) request); @@ -119,24 +118,24 @@ public Command getCommand(Request request) { return null; } - private Command getTextStyleApplication(TextRequest request) { + private static Command getTextStyleApplication(TextRequest request) { SelectionRange range = request.getSelectionRange(); ModelLocation start = new ModelLocation((TextRun) range.begin.part.getModel(), range.begin.offset); ModelLocation end = new ModelLocation((TextRun) range.end.part.getModel(), range.end.offset); - CompoundEditCommand command = new CompoundEditCommand("Set Style"); + CompoundEditCommand command = new CompoundEditCommand("Set Style"); //$NON-NLS-1$ command.setBeginLocation(start); command.setEndLocation(end); String styleID = request.getStyleKeys()[0]; if (Style.PROPERTY_ALIGNMENT.equals(styleID) || Style.PROPERTY_ORIENTATION.equals(styleID)) { Object value = request.getStyleValues()[0]; - for (Iterator iter = range.getLeafParts().iterator(); iter.hasNext();) { - // TODO optimize by ensuring that runs in the same container - // don't cause - // that container's style to be set multiple times - TextRun run = (TextRun) ((TextEditPart) iter.next()).getModel(); + + range.getLeafParts().forEach(ep -> { + // TODO optimize by ensuring that runs in the same container don't cause that + // container's style to be set multiple times + TextRun run = (TextRun) ep.getModel(); command.pendEdit(new ApplyMultiStyle(run.getBlockContainer(), styleID, value)); - } + }); } else if (!range.isEmpty()) { command.pendEdit(new ApplyBooleanStyle(start, end, request.getStyleKeys(), request.getStyleValues())); } @@ -144,17 +143,17 @@ private Command getTextStyleApplication(TextRequest request) { return command; } - private Command getIndentCommand(TextRequest request) { + private static Command getIndentCommand(TextRequest request) { SelectionRange range = request.getSelectionRange(); return new NestElementCommand(range.begin.part, range.begin.offset); } - private Command getUnindentCommand(TextRequest request) { + private static Command getUnindentCommand(TextRequest request) { SelectionRange range = request.getSelectionRange(); return new PromoteElementCommand(range.begin.part, range.begin.offset); } - private Command getDeleteCommand(TextRequest request) { + private static Command getDeleteCommand(TextRequest request) { TextLocation where = request.getSelectionRange().begin; if (where.offset == where.part.getLength()) return null; @@ -162,7 +161,7 @@ private Command getDeleteCommand(TextRequest request) { MiniEdit remove = new RemoveText(run, where.offset, where.offset + 1); CompoundEditCommand command = (CompoundEditCommand) request.getPreviousCommand(); if (command == null) { - command = new CompoundEditCommand("Delete"); + command = new CompoundEditCommand("Delete"); //$NON-NLS-1$ command.setBeginLocation(new ModelLocation(run, where.offset)); command.setEndLocation(new ModelLocation(run, where.offset + 1)); } @@ -170,7 +169,7 @@ private Command getDeleteCommand(TextRequest request) { return command; } - private MiniEdit getMergeBackspaceEdit(TextRequest request) { + private static MiniEdit getMergeBackspaceEdit(TextRequest request) { TextEditPart part = request.getSelectionRange().begin.part; MergeWithPrevious edit = new MergeWithPrevious(part); if (edit.canApply()) @@ -178,7 +177,7 @@ private MiniEdit getMergeBackspaceEdit(TextRequest request) { return null; } - private Command getNewlineCommand(TextRequest request) { + private static Command getNewlineCommand(TextRequest request) { TextLocation where = request.getSelectionRange().end; TextRun run = (TextRun) where.part.getModel(); SubdivideElement edit = new SubdivideElement(run, where.offset); @@ -187,25 +186,26 @@ private Command getNewlineCommand(TextRequest request) { if (request.getPreviousCommand() instanceof CompoundEditCommand) command = (CompoundEditCommand) request.getPreviousCommand(); else - command = new CompoundEditCommand("typing"); + command = new CompoundEditCommand("typing"); //$NON-NLS-1$ command.pendEdit(edit); return command; } + @Override public EditPart getTargetEditPart(Request request) { if (request instanceof TextRequest) return getHost(); return null; } - private Command getChangeTextCommand(TextRequest request) { + private static Command getChangeTextCommand(TextRequest request) { CompoundEditCommand command = null; - if (request.getPreviousCommand() instanceof CompoundEditCommand) - command = (CompoundEditCommand) request.getPreviousCommand(); + if (request.getPreviousCommand() instanceof CompoundEditCommand cec) + command = cec; SelectionRange range = request.getSelectionRange(); - if (range.isEmpty() && request.getText().equals(" ")) { + if (range.isEmpty() && request.getText().equals(" ")) { //$NON-NLS-1$ Command result = checkForConversion(request.getSelectionRange().begin); if (result != null) return result; @@ -213,7 +213,7 @@ private Command getChangeTextCommand(TextRequest request) { TextRun rangeBegin = (TextRun) range.begin.part.getModel(); if (command == null) { TextRun rangeEnd = (TextRun) range.end.part.getModel(); - command = new CompoundEditCommand("typing"); + command = new CompoundEditCommand("typing"); //$NON-NLS-1$ command.setBeginLocation(new ModelLocation(rangeBegin, range.begin.offset)); if (!range.isEmpty()) { diff --git a/org.eclipse.gef.examples.text/src/org/eclipse/gef/examples/text/edit/CompoundTextPart.java b/org.eclipse.gef.examples.text/src/org/eclipse/gef/examples/text/edit/CompoundTextPart.java index d55fac689..c17f5b7d8 100644 --- a/org.eclipse.gef.examples.text/src/org/eclipse/gef/examples/text/edit/CompoundTextPart.java +++ b/org.eclipse.gef.examples.text/src/org/eclipse/gef/examples/text/edit/CompoundTextPart.java @@ -24,6 +24,7 @@ import org.eclipse.gef.examples.text.TextLocation; import org.eclipse.gef.examples.text.figures.CommentPage; import org.eclipse.gef.examples.text.model.Container; +import org.eclipse.gef.examples.text.model.ModelElement; import org.eclipse.gef.examples.text.requests.CaretRequest; import org.eclipse.gef.examples.text.requests.SearchResult; @@ -32,7 +33,7 @@ */ public abstract class CompoundTextPart extends AbstractTextPart { - public CompoundTextPart(Container model) { + protected CompoundTextPart(Container model) { setModel(model); } @@ -70,7 +71,7 @@ protected IFigure createFigure() { figure.setBorder(new MarginBorder(4)); break; default: - throw new RuntimeException("unexpected container"); + throw new RuntimeException("unexpected container"); //$NON-NLS-1$ } return figure; } @@ -83,7 +84,7 @@ public void deactivate() { @Override public CaretInfo getCaretPlacement(int offset, boolean trailing) { - throw new RuntimeException("This part cannot place the caret"); + throw new RuntimeException("This part cannot place the caret"); //$NON-NLS-1$ } @SuppressWarnings("unchecked") @@ -106,7 +107,7 @@ public Container getModel() { } @Override - protected List getModelChildren() { + protected List getModelChildren() { return getModel().getChildren(); } @@ -136,7 +137,7 @@ public void getTextLocation(CaretRequest search, SearchResult result) { @Override public void propertyChange(PropertyChangeEvent evt) { - if (evt.getPropertyName().equals("children")) + if (evt.getPropertyName().equals("children")) //$NON-NLS-1$ refreshChildren(); } diff --git a/org.eclipse.gef.examples.text/src/org/eclipse/gef/examples/text/edit/ContainerTreePart.java b/org.eclipse.gef.examples.text/src/org/eclipse/gef/examples/text/edit/ContainerTreePart.java index a2fb6619e..1b95f64b5 100644 --- a/org.eclipse.gef.examples.text/src/org/eclipse/gef/examples/text/edit/ContainerTreePart.java +++ b/org.eclipse.gef.examples.text/src/org/eclipse/gef/examples/text/edit/ContainerTreePart.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2004, 2010 IBM Corporation and others. + * Copyright (c) 2004, 2023 IBM Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at @@ -14,6 +14,7 @@ import java.util.List; import org.eclipse.gef.examples.text.model.Container; +import org.eclipse.gef.examples.text.model.ModelElement; import org.eclipse.gef.examples.text.model.Style; import org.eclipse.swt.widgets.TreeItem; @@ -22,62 +23,66 @@ */ public class ContainerTreePart extends ExampleTreePart { - public ContainerTreePart(Object model) { + public ContainerTreePart(Container model) { setModel(model); } - protected List getModelChildren() { - return ((Container) getModel()).getChildren(); + @Override + protected List getModelChildren() { + return getModel().getChildren(); } + @Override public void propertyChange(PropertyChangeEvent evt) { - if (evt.getPropertyName().equals("children")) + if (evt.getPropertyName().equals("children")) //$NON-NLS-1$ refreshChildren(); } - private Container getContainer() { - return (Container) getModel(); + @Override + public Container getModel() { + return (Container) super.getModel(); } + @Override protected void refreshChildren() { super.refreshChildren(); - if (getWidget() instanceof TreeItem) { - TreeItem item = (TreeItem) getWidget(); + if (getWidget() instanceof TreeItem item) { item.setExpanded(true); } } + @Override protected void refreshVisuals() { StringBuffer label = new StringBuffer(); - switch (getContainer().getType()) { + switch (getModel().getType()) { case Container.TYPE_BULLETED_LIST: - label.append(""); + label.append(""); //$NON-NLS-1$ break; case Container.TYPE_COMMENT: - label.append(""); + label.append(""); //$NON-NLS-1$ break; case Container.TYPE_IMPORT_DECLARATIONS: - label.append(""); + label.append(""); //$NON-NLS-1$ break; case Container.TYPE_INLINE: - Style style = getContainer().getStyle(); + Style style = getModel().getStyle(); if (style.isSet(Style.PROPERTY_FONT_SIZE)) - label.append(""); + label.append(""); //$NON-NLS-1$ if (style.isSet(Style.PROPERTY_BOLD)) - label.append(""); + label.append(""); //$NON-NLS-1$ if (style.isSet(Style.PROPERTY_ITALIC)) - label.append(""); + label.append(""); //$NON-NLS-1$ if (style.isSet(Style.PROPERTY_UNDERLINE)) - label.append(""); + label.append(""); //$NON-NLS-1$ if (style.isSet(Style.PROPERTY_FONT)) - label.append(""); + label.append(""); //$NON-NLS-1$ break; case Container.TYPE_PARAGRAPH: - label.append("Paragraph"); + label.append("Paragraph"); //$NON-NLS-1$ break; default: - label.append("Unknown container"); + label.append("Unknown container"); //$NON-NLS-1$ break; } setWidgetText(label.toString()); diff --git a/org.eclipse.gef.examples.text/src/org/eclipse/gef/examples/text/edit/DocumentPart.java b/org.eclipse.gef.examples.text/src/org/eclipse/gef/examples/text/edit/DocumentPart.java index 398a5bd5a..23d745752 100644 --- a/org.eclipse.gef.examples.text/src/org/eclipse/gef/examples/text/edit/DocumentPart.java +++ b/org.eclipse.gef.examples.text/src/org/eclipse/gef/examples/text/edit/DocumentPart.java @@ -13,9 +13,8 @@ import java.util.Iterator; -import org.eclipse.draw2d.geometry.Point; +import org.eclipse.gef.EditPart; import org.eclipse.gef.examples.text.SelectionRange; -import org.eclipse.gef.examples.text.TextLocation; import org.eclipse.gef.examples.text.actions.StyleService; import org.eclipse.gef.examples.text.model.Container; import org.eclipse.gef.examples.text.model.Style; @@ -34,7 +33,7 @@ public DocumentPart(Container model) { @Override protected void createEditPolicies() { - installEditPolicy("Text Editing", new BlockEditPolicy()); + installEditPolicy("Text Editing", new BlockEditPolicy()); //$NON-NLS-1$ } @Override @@ -44,10 +43,6 @@ public T getAdapter(final Class key) { return super.getAdapter(key); } - public TextLocation getLocation(Point absolute) { - return null; - } - @Override public void getTextLocation(CaretRequest search, SearchResult result) { if (search.getType() == CaretRequest.DOCUMENT) { @@ -67,7 +62,7 @@ public Object getStyleState(String styleID, SelectionRange range) { @Override public Object getStyleValue(String styleID, SelectionRange range) { if (styleID.equals(Style.PROPERTY_BOLD)) { - for (Iterator iter = range.getLeafParts().iterator(); iter.hasNext();) { + for (Iterator iter = range.getLeafParts().iterator(); iter.hasNext();) { TextRun run = (TextRun) ((TextEditPart) iter.next()).getModel(); if (!run.getContainer().getStyle().isBold()) return Boolean.FALSE; @@ -75,7 +70,7 @@ public Object getStyleValue(String styleID, SelectionRange range) { return Boolean.TRUE; } else if (styleID.equals(Style.PROPERTY_FONT_SIZE)) { int fontHeight = -1; - for (Iterator iter = range.getLeafParts().iterator(); iter.hasNext();) { + for (Iterator iter = range.getLeafParts().iterator(); iter.hasNext();) { TextRun run = (TextRun) ((TextEditPart) iter.next()).getModel(); if (fontHeight == -1) fontHeight = run.getContainer().getStyle().getFontHeight(); @@ -85,7 +80,7 @@ else if (fontHeight != run.getContainer().getStyle().getFontHeight()) return Integer.valueOf(fontHeight); } else if (styleID.equals(Style.PROPERTY_FONT)) { String fontName = null; - for (Iterator iter = range.getLeafParts().iterator(); iter.hasNext();) { + for (Iterator iter = range.getLeafParts().iterator(); iter.hasNext();) { TextRun run = (TextRun) ((TextEditPart) iter.next()).getModel(); if (fontName == null) fontName = run.getContainer().getStyle().getFontFamily(); @@ -94,14 +89,14 @@ else if (!fontName.equals(run.getContainer().getStyle().getFontFamily())) } return fontName; } else if (styleID.equals(Style.PROPERTY_ITALIC)) { - for (Iterator iter = range.getLeafParts().iterator(); iter.hasNext();) { + for (Iterator iter = range.getLeafParts().iterator(); iter.hasNext();) { TextRun run = (TextRun) ((TextEditPart) iter.next()).getModel(); if (!run.getContainer().getStyle().isItalic()) return Boolean.FALSE; } return Boolean.TRUE; } else if (styleID.equals(Style.PROPERTY_UNDERLINE)) { - for (Iterator iter = range.getLeafParts().iterator(); iter.hasNext();) { + for (Iterator iter = range.getLeafParts().iterator(); iter.hasNext();) { TextRun run = (TextRun) ((TextEditPart) iter.next()).getModel(); if (!run.getContainer().getStyle().isUnderline()) return Boolean.FALSE; @@ -109,7 +104,7 @@ else if (!fontName.equals(run.getContainer().getStyle().getFontFamily())) return Boolean.TRUE; } else if (Style.PROPERTY_ALIGNMENT.equals(styleID)) { int alignment = 0; - for (Iterator iter = range.getLeafParts().iterator(); iter.hasNext();) { + for (Iterator iter = range.getLeafParts().iterator(); iter.hasNext();) { TextRun run = (TextRun) ((TextEditPart) iter.next()).getModel(); Style style = run.getBlockContainer().getStyle(); if (alignment == 0) @@ -120,7 +115,7 @@ else if (!fontName.equals(run.getContainer().getStyle().getFontFamily())) return Integer.valueOf(alignment); } else if (Style.PROPERTY_ORIENTATION.equals(styleID)) { int orientation = 0; - for (Iterator iter = range.getLeafParts().iterator(); iter.hasNext();) { + for (Iterator iter = range.getLeafParts().iterator(); iter.hasNext();) { TextRun run = (TextRun) ((TextEditPart) iter.next()).getModel(); Style style = run.getBlockContainer().getStyle(); if (orientation == 0) diff --git a/org.eclipse.gef.examples.text/src/org/eclipse/gef/examples/text/edit/ExampleTreePart.java b/org.eclipse.gef.examples.text/src/org/eclipse/gef/examples/text/edit/ExampleTreePart.java index 0d162b815..40dd1eaed 100644 --- a/org.eclipse.gef.examples.text/src/org/eclipse/gef/examples/text/edit/ExampleTreePart.java +++ b/org.eclipse.gef.examples.text/src/org/eclipse/gef/examples/text/edit/ExampleTreePart.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2004, 2010 IBM Corporation and others. + * Copyright (c) 2004, 2023 IBM Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at @@ -21,12 +21,14 @@ */ public abstract class ExampleTreePart extends AbstractTreeEditPart implements PropertyChangeListener { + @Override public void activate() { super.activate(); ModelElement model = (ModelElement) getModel(); model.addPropertyChangeListener(this); } + @Override public void deactivate() { ModelElement model = (ModelElement) getModel(); model.removePropertyChangeListener(this); diff --git a/org.eclipse.gef.examples.text/src/org/eclipse/gef/examples/text/edit/FontCache.java b/org.eclipse.gef.examples.text/src/org/eclipse/gef/examples/text/edit/FontCache.java index f21f57c5b..54ddeeb77 100644 --- a/org.eclipse.gef.examples.text/src/org/eclipse/gef/examples/text/edit/FontCache.java +++ b/org.eclipse.gef.examples.text/src/org/eclipse/gef/examples/text/edit/FontCache.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2004, 2010 IBM Corporation and others. + * Copyright (c) 2004, 2023 IBM Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at @@ -10,7 +10,8 @@ *******************************************************************************/ package org.eclipse.gef.examples.text.edit; -import java.util.Hashtable; +import java.util.HashMap; +import java.util.Map; import org.eclipse.swt.SWT; import org.eclipse.swt.graphics.Font; @@ -22,7 +23,7 @@ */ public class FontCache { - private static Hashtable table = new Hashtable(); + private static Map table = new HashMap<>(); private FontCache() { } @@ -36,7 +37,7 @@ public static void checkIn(Font font) { FontData fd = font.getFontData()[0]; Key key = new Key(fd.getName(), fd.getHeight(), (fd.getStyle() & SWT.BOLD) != 0, (fd.getStyle() & SWT.ITALIC) != 0); - Value value = (Value) table.get(key); + Value value = table.get(key); value.refCount--; if (value.refCount == 0) { table.remove(key); @@ -46,7 +47,7 @@ public static void checkIn(Font font) { public static Font checkOut(String fontName, int fontHeight, boolean isBold, boolean isItalic) { Key key = new Key(fontName, fontHeight, isBold, isItalic); - Value value = (Value) table.get(key); + Value value = table.get(key); if (value == null) { value = new Value(); int style = (isBold ? SWT.BOLD : SWT.NONE) | (isItalic ? SWT.ITALIC : SWT.NONE); @@ -57,7 +58,7 @@ public static Font checkOut(String fontName, int fontHeight, boolean isBold, boo return value.font; } - private static class Key { + private static final class Key { private String fontName; private int height; private boolean isBold, isItalic; @@ -69,16 +70,17 @@ private Key(String fontName, int height, boolean isBold, boolean isItalic) { this.isItalic = isItalic; } + @Override public boolean equals(Object obj) { boolean result = obj == this; - if (!result && (obj instanceof Key)) { - Key key = (Key) obj; + if (!result && (obj instanceof Key key)) { result = fontName.equalsIgnoreCase(key.fontName) && height == key.height && isBold == key.isBold && isItalic == key.isItalic; } return result; } + @Override public int hashCode() { return fontName.toLowerCase().hashCode() + height; } diff --git a/org.eclipse.gef.examples.text/src/org/eclipse/gef/examples/text/edit/TextRunTreePart.java b/org.eclipse.gef.examples.text/src/org/eclipse/gef/examples/text/edit/TextRunTreePart.java index 0c8083afb..8ed434bcc 100644 --- a/org.eclipse.gef.examples.text/src/org/eclipse/gef/examples/text/edit/TextRunTreePart.java +++ b/org.eclipse.gef.examples.text/src/org/eclipse/gef/examples/text/edit/TextRunTreePart.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2004, 2010 IBM Corporation and others. + * Copyright (c) 2004, 2023 IBM Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at @@ -24,16 +24,18 @@ public TextRunTreePart(Object model) { setModel(model); } + @Override public void propertyChange(PropertyChangeEvent evt) { - if (evt.getPropertyName().equals("text")) + if (evt.getPropertyName().equals("text")) //$NON-NLS-1$ refreshVisuals(); } + @Override protected void refreshVisuals() { TextRun run = (TextRun) getModel(); String s = run.getText(); if (s.length() > 50) - s = s.substring(0, 50) + "..."; + s = s.substring(0, 50) + "..."; //$NON-NLS-1$ setWidgetText(s); } diff --git a/org.eclipse.gef.examples.text/src/org/eclipse/gef/examples/text/figures/BulletBorder.java b/org.eclipse.gef.examples.text/src/org/eclipse/gef/examples/text/figures/BulletBorder.java index bea92f48d..95dea07f3 100644 --- a/org.eclipse.gef.examples.text/src/org/eclipse/gef/examples/text/figures/BulletBorder.java +++ b/org.eclipse.gef.examples.text/src/org/eclipse/gef/examples/text/figures/BulletBorder.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2004, 2010 IBM Corporation and others. + * Copyright (c) 2004, 2023 IBM Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at @@ -25,11 +25,13 @@ public class BulletBorder extends ListItemBorder { private static final String BULLET = " \u25cf "; + @Override public Insets getInsets(IFigure figure) { FigureUtilities.getTextExtents(BULLET, figure.getFont(), Dimension.SINGLETON); return new Insets(0, Dimension.SINGLETON.width, 0, 0); } + @Override public void paintBorder(IFigure figure, Graphics graphics, Insets insets) { Rectangle r = getPaintRectangle(figure, insets); graphics.drawString(BULLET, r.x, r.y); diff --git a/org.eclipse.gef.examples.text/src/org/eclipse/gef/examples/text/figures/CommentPage.java b/org.eclipse.gef.examples.text/src/org/eclipse/gef/examples/text/figures/CommentPage.java index 3963e36d4..d56bc0af4 100644 --- a/org.eclipse.gef.examples.text/src/org/eclipse/gef/examples/text/figures/CommentPage.java +++ b/org.eclipse.gef.examples.text/src/org/eclipse/gef/examples/text/figures/CommentPage.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2004, 2010 IBM Corporation and others. + * Copyright (c) 2004, 2023 IBM Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at @@ -20,8 +20,8 @@ public class CommentPage extends BlockFlow { - private static final Image BEGIN = new Image(null, CommentPage.class.getResourceAsStream("javadoc_begin.gif")); - private static final Image END = new Image(null, CommentPage.class.getResourceAsStream("javadoc_end.gif")); + private static final Image BEGIN = new Image(null, CommentPage.class.getResourceAsStream("javadoc_begin.gif")); //$NON-NLS-1$ + private static final Image END = new Image(null, CommentPage.class.getResourceAsStream("javadoc_end.gif")); //$NON-NLS-1$ private static final Insets INSETS = new Insets(9, 15, 12, 10); private static final Color COMMENT_FG = ColorConstants.darkBlue; @@ -29,10 +29,12 @@ public CommentPage() { setForegroundColor(COMMENT_FG); } + @Override public Insets getInsets() { return INSETS; } + @Override protected void paintFigure(Graphics g) { g.drawImage(BEGIN, bounds.x, bounds.y); g.drawImage(END, bounds.x + 6, bounds.bottom() - 10); diff --git a/org.eclipse.gef.examples.text/src/org/eclipse/gef/examples/text/figures/Images.java b/org.eclipse.gef.examples.text/src/org/eclipse/gef/examples/text/figures/Images.java index 7e5b31b07..bcd0d7fcb 100644 --- a/org.eclipse.gef.examples.text/src/org/eclipse/gef/examples/text/figures/Images.java +++ b/org.eclipse.gef.examples.text/src/org/eclipse/gef/examples/text/figures/Images.java @@ -21,11 +21,11 @@ */ public class Images { - public static final Image IMPORTS = createImage("imports.gif"); - public static final Image IMPORT = createImage("importstatement.gif"); - public static final Image PUBLIC = createImage("public.gif"); - public static final Image PRIVATE = createImage("private.gif"); - public static final Image PROTECTED = createImage("protected.gif"); + public static final Image IMPORTS = createImage("imports.gif"); //$NON-NLS-1$ + public static final Image IMPORT = createImage("importstatement.gif"); //$NON-NLS-1$ + public static final Image PUBLIC = createImage("public.gif"); //$NON-NLS-1$ + public static final Image PRIVATE = createImage("private.gif"); //$NON-NLS-1$ + public static final Image PROTECTED = createImage("protected.gif"); //$NON-NLS-1$ private static Image createImage(String name) { InputStream stream = Images.class.getResourceAsStream(name); diff --git a/org.eclipse.gef.examples.text/src/org/eclipse/gef/examples/text/figures/StyleConstants.java b/org.eclipse.gef.examples.text/src/org/eclipse/gef/examples/text/figures/StyleConstants.java index fe91f077b..0a767363c 100644 --- a/org.eclipse.gef.examples.text/src/org/eclipse/gef/examples/text/figures/StyleConstants.java +++ b/org.eclipse.gef.examples.text/src/org/eclipse/gef/examples/text/figures/StyleConstants.java @@ -17,8 +17,8 @@ public class StyleConstants { public static final Color CODE_FG = new Color(null, 0, 0, 32); - public static final Font COURIER = new Font(null, "Courier New", 10, 0); - public static final Font COURIER_BOLD = new Font(null, "Courier New", 10, SWT.BOLD); - public static final Font COMMENT = new Font(null, "Arial", 10, 0); + public static final Font COURIER = new Font(null, "Courier New", 10, 0); //$NON-NLS-1$ + public static final Font COURIER_BOLD = new Font(null, "Courier New", 10, SWT.BOLD); //$NON-NLS-1$ + public static final Font COMMENT = new Font(null, "Arial", 10, 0); //$NON-NLS-1$ } diff --git a/org.eclipse.gef.examples.text/src/org/eclipse/gef/examples/text/figures/TreeBorder.java b/org.eclipse.gef.examples.text/src/org/eclipse/gef/examples/text/figures/TreeBorder.java index fabfbffac..b5e1f3eb3 100644 --- a/org.eclipse.gef.examples.text/src/org/eclipse/gef/examples/text/figures/TreeBorder.java +++ b/org.eclipse.gef.examples.text/src/org/eclipse/gef/examples/text/figures/TreeBorder.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2004, 2010 IBM Corporation and others. + * Copyright (c) 2004, 2023 IBM Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at @@ -34,6 +34,7 @@ public TreeBorder(Image image, String text) { this.text = text; } + @Override public Insets getInsets(IFigure figure) { if (insets == null) { FigureUtilities.getTextExtents(text, figure.getFont(), Dimension.SINGLETON); @@ -42,6 +43,7 @@ public Insets getInsets(IFigure figure) { return insets; } + @Override public void paint(IFigure figure, Graphics g, Insets insets) { Rectangle where = getPaintRectangle(figure, insets); diff --git a/org.eclipse.gef.examples.text/src/org/eclipse/gef/examples/text/figures/TreeItemBorder.java b/org.eclipse.gef.examples.text/src/org/eclipse/gef/examples/text/figures/TreeItemBorder.java index 484a9e5ad..51b71547d 100644 --- a/org.eclipse.gef.examples.text/src/org/eclipse/gef/examples/text/figures/TreeItemBorder.java +++ b/org.eclipse.gef.examples.text/src/org/eclipse/gef/examples/text/figures/TreeItemBorder.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2004, 2010 IBM Corporation and others. + * Copyright (c) 2004, 2023 IBM Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at @@ -36,6 +36,7 @@ public TreeItemBorder(Image image) { setImage(image); } + @Override public Insets getInsets(IFigure figure) { return imgInsets; } @@ -44,10 +45,12 @@ public Image getImage() { return image; } + @Override public Dimension getPreferredSize(IFigure f) { return imageSize; } + @Override public void paintBorder(IFigure figure, Graphics graphics, Insets insets) { if (image == null) return; diff --git a/org.eclipse.gef.examples.text/src/org/eclipse/gef/examples/text/model/Block.java b/org.eclipse.gef.examples.text/src/org/eclipse/gef/examples/text/model/Block.java index a986ca682..93c14d007 100644 --- a/org.eclipse.gef.examples.text/src/org/eclipse/gef/examples/text/model/Block.java +++ b/org.eclipse.gef.examples.text/src/org/eclipse/gef/examples/text/model/Block.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2004, 2010 IBM Corporation and others. + * Copyright (c) 2004, 2023 IBM Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at @@ -29,6 +29,7 @@ public Block(int type) { /** * @see org.eclipse.gef.examples.text.model.Container#newContainer() */ + @Override Container newContainer() { return new Block(getType()); } diff --git a/org.eclipse.gef.examples.text/src/org/eclipse/gef/examples/text/model/CanvasStyle.java b/org.eclipse.gef.examples.text/src/org/eclipse/gef/examples/text/model/CanvasStyle.java index 4673e3cb6..135c4594c 100644 --- a/org.eclipse.gef.examples.text/src/org/eclipse/gef/examples/text/model/CanvasStyle.java +++ b/org.eclipse.gef.examples.text/src/org/eclipse/gef/examples/text/model/CanvasStyle.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2004, 2010 IBM Corporation and others. + * Copyright (c) 2004, 2023 IBM Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at @@ -22,10 +22,12 @@ public CanvasStyle(Control canvas) { this.canvas = canvas; } + @Override public String getFontFamily() { return canvas.getFont().getFontData()[0].getName(); } + @Override public int getFontHeight() { return canvas.getFont().getFontData()[0].getHeight(); } diff --git a/org.eclipse.gef.examples.text/src/org/eclipse/gef/examples/text/model/Container.java b/org.eclipse.gef.examples.text/src/org/eclipse/gef/examples/text/model/Container.java index caf42cebc..9492b2521 100644 --- a/org.eclipse.gef.examples.text/src/org/eclipse/gef/examples/text/model/Container.java +++ b/org.eclipse.gef.examples.text/src/org/eclipse/gef/examples/text/model/Container.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2004, 2010 IBM Corporation and others. + * Copyright (c) 2004, 2023 IBM Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at @@ -30,10 +30,10 @@ public abstract class Container extends ModelElement { private static final long serialVersionUID = 1; - private List children = new ArrayList(); + private List children = new ArrayList<>(); private Style style = new Style(); - public Container(int type) { + protected Container(int type) { this.type = type; } @@ -46,7 +46,7 @@ public void add(ModelElement child, int index) { if (index == -1) index = children.size(); children.add(index, child); - firePropertyChange("children", null, child); + firePropertyChange("children", null, child); //$NON-NLS-1$ } /** @@ -65,7 +65,7 @@ public boolean contains(ModelElement child) { return false; } - public List getChildren() { + public List getChildren() { return children; } @@ -88,15 +88,16 @@ public int remove(ModelElement child) { int index = children.indexOf(child); children.remove(child); child.setParent(null); - firePropertyChange("children", child, null); + firePropertyChange("children", child, null); //$NON-NLS-1$ return index; } - public void removeAll(Collection children) { + public void removeAll(Collection children) { if (children.removeAll(children)) - firePropertyChange("children", children, null); + firePropertyChange("children", children, null); //$NON-NLS-1$ } + @Override public void setParent(Container container) { super.setParent(container); if (container == null) @@ -108,6 +109,7 @@ public void setParent(Container container) { /** * @see org.eclipse.gef.examples.text.model.ModelElement#size() */ + @Override public int size() { return getChildren().size(); } @@ -116,7 +118,7 @@ public int size() { public Container subdivideContainer(int offset) { Container result = newContainer(); - List reparent = getChildren().subList(offset, getChildren().size()); + List reparent = getChildren().subList(offset, getChildren().size()); removeAll(reparent); result.getChildren().addAll(reparent); return result; diff --git a/org.eclipse.gef.examples.text/src/org/eclipse/gef/examples/text/model/InlineContainer.java b/org.eclipse.gef.examples.text/src/org/eclipse/gef/examples/text/model/InlineContainer.java index d29224314..6fe9895cd 100644 --- a/org.eclipse.gef.examples.text/src/org/eclipse/gef/examples/text/model/InlineContainer.java +++ b/org.eclipse.gef.examples.text/src/org/eclipse/gef/examples/text/model/InlineContainer.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2004, 2010 IBM Corporation and others. + * Copyright (c) 2004, 2023 IBM Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at @@ -29,6 +29,7 @@ public InlineContainer(int type) { /** * @see org.eclipse.gef.examples.text.model.Container#newContainer() */ + @Override Container newContainer() { return new InlineContainer(getType()); } diff --git a/org.eclipse.gef.examples.text/src/org/eclipse/gef/examples/text/model/ModelUtil.java b/org.eclipse.gef.examples.text/src/org/eclipse/gef/examples/text/model/ModelUtil.java index a8947f6fe..0f915e606 100644 --- a/org.eclipse.gef.examples.text/src/org/eclipse/gef/examples/text/model/ModelUtil.java +++ b/org.eclipse.gef.examples.text/src/org/eclipse/gef/examples/text/model/ModelUtil.java @@ -17,20 +17,18 @@ /** * @since 3.1 */ -public class ModelUtil { +public final class ModelUtil { - public static List getModelSpan(ModelElement start, int startIndex, ModelElement end, int endIndex) { + public static List getModelSpan(ModelElement start, int startIndex, ModelElement end, int endIndex) { Container ancestor = start.getContainer(); while (!ancestor.contains(end)) ancestor = ancestor.getContainer(); - List result = new ArrayList(); - - List children; + List result = new ArrayList<>(); Container branch = start.getContainer(); while (branch != ancestor) { - children = branch.getChildren(); + List children = branch.getChildren(); int branchIndex = children.indexOf(start); if (startIndex == 0) { // We should either delete the child, or the entire branch @@ -52,11 +50,11 @@ public static List getModelSpan(ModelElement start, int startIndex, ModelElement } } - List rightSide = new ArrayList(); + List rightSide = new ArrayList<>(); branch = end.getContainer(); while (branch != ancestor) { - children = branch.getChildren(); + List children = branch.getChildren(); // The index to the right side of end. int branchIndex = children.indexOf(end) + 1; if (endIndex == end.size()) { @@ -78,7 +76,7 @@ public static List getModelSpan(ModelElement start, int startIndex, ModelElement endIndex = branchIndex - 1; } } - children = ancestor.getChildren(); + List children = ancestor.getChildren(); if (startIndex == 0) startIndex = children.indexOf(start); else diff --git a/org.eclipse.gef.examples.text/src/org/eclipse/gef/examples/text/model/TestModelUtil.java b/org.eclipse.gef.examples.text/src/org/eclipse/gef/examples/text/model/TestModelUtil.java index 0c6de1554..3c57e0c3c 100644 --- a/org.eclipse.gef.examples.text/src/org/eclipse/gef/examples/text/model/TestModelUtil.java +++ b/org.eclipse.gef.examples.text/src/org/eclipse/gef/examples/text/model/TestModelUtil.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2004, 2010 IBM Corporation and others. + * Copyright (c) 2004, 2023 IBM Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at @@ -20,17 +20,17 @@ */ public class TestModelUtil extends TestCase { - static List result; + static List result; public void testNestedBegin() { Container doc = new Block(Container.TYPE_ROOT); Container branch1 = new Block(0); - TextRun start = new TextRun("12345"); + TextRun start = new TextRun("12345"); //$NON-NLS-1$ doc.add(branch1); branch1.add(start); - TextRun end = new TextRun("ABCDE"); + TextRun end = new TextRun("ABCDE"); //$NON-NLS-1$ doc.add(end); for (int i = 0; i < 3; i++) { @@ -45,10 +45,10 @@ public void testNestedBegin() { result = ModelUtil.getModelSpan(start, 1, end, 3); assertTrue(result.isEmpty()); - doc.add(new TextRun("bogus"), 0); + doc.add(new TextRun("bogus"), 0); //$NON-NLS-1$ } - TextRun middle = new TextRun("I'm in the middle"); + TextRun middle = new TextRun("I'm in the middle"); //$NON-NLS-1$ doc.add(middle, doc.getChildren().indexOf(end)); result = ModelUtil.getModelSpan(start, 0, end, 5); @@ -62,11 +62,11 @@ public void testNestedBegin() { public void testNestedEnd() { Container doc = new Block(Container.TYPE_ROOT); Container branch1 = new Block(0); - TextRun run123 = new TextRun("12345"); + TextRun run123 = new TextRun("12345"); //$NON-NLS-1$ branch1.add(run123); - TextRun runABC = new TextRun("ABCDE"); + TextRun runABC = new TextRun("ABCDE"); //$NON-NLS-1$ doc.add(runABC); doc.add(branch1); @@ -81,11 +81,11 @@ public void testNestedEnd() { compareList(result, new Object[] { runABC }); assertTrue(ModelUtil.getModelSpan(runABC, 1, run123, 4).isEmpty()); - doc.add(new TextRun("bogus"), 0); - doc.add(new TextRun("bogus"), doc.getChildren().size()); + doc.add(new TextRun("bogus"), 0); //$NON-NLS-1$ + doc.add(new TextRun("bogus"), doc.getChildren().size()); //$NON-NLS-1$ } - TextRun middle = new TextRun("I'm in the middle"); + TextRun middle = new TextRun("I'm in the middle"); //$NON-NLS-1$ doc.add(middle, doc.getChildren().indexOf(branch1)); result = ModelUtil.getModelSpan(runABC, 0, run123, 5); @@ -95,8 +95,8 @@ public void testNestedEnd() { compareList(result, new Object[] { middle }); } - private void compareList(List result, Object array[]) { - Object compare[] = result.toArray(); + private static void compareList(List result, Object[] array) { + Object[] compare = result.toArray(); assertEquals(array.length, compare.length); for (int i = 0; i < compare.length; i++) assertEquals(compare[i], array[i]); diff --git a/org.eclipse.gef.examples.text/src/org/eclipse/gef/examples/text/model/TextRun.java b/org.eclipse.gef.examples.text/src/org/eclipse/gef/examples/text/model/TextRun.java index e55a42143..bd19f6b7e 100644 --- a/org.eclipse.gef.examples.text/src/org/eclipse/gef/examples/text/model/TextRun.java +++ b/org.eclipse.gef.examples.text/src/org/eclipse/gef/examples/text/model/TextRun.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2004, 2010 IBM Corporation and others. + * Copyright (c) 2004, 2023 IBM Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at @@ -61,13 +61,13 @@ public String getText() { public void insertText(String someText, int offset) { text = text.substring(0, offset) + someText + text.substring(offset, text.length()); - firePropertyChange("text", null, text); + firePropertyChange("text", null, text); //$NON-NLS-1$ } public String overwriteText(String someText, int offset) { String result = text.substring(offset, Math.min(offset + someText.length(), text.length())); text = text.substring(0, offset) + someText + text.substring(offset + result.length()); - firePropertyChange("text", null, text); + firePropertyChange("text", null, text); //$NON-NLS-1$ return result; } @@ -76,22 +76,24 @@ public String removeRange(int offset, int length) { Assert.isTrue(offset + length <= text.length()); String result = text.substring(offset, offset + length); text = text.substring(0, offset) + text.substring(offset + length); - firePropertyChange("text", null, text); + firePropertyChange("text", null, text); //$NON-NLS-1$ return result; } public void setText(String text) { this.text = text; - firePropertyChange("text", null, text); + firePropertyChange("text", null, text); //$NON-NLS-1$ } /** * @see org.eclipse.gef.examples.text.model.ModelElement#size() */ + @Override public int size() { return getText().length(); } + @Override public String toString() { return text; } diff --git a/org.eclipse.gef.examples.text/src/org/eclipse/gef/examples/text/model/commands/ApplyBooleanStyle.java b/org.eclipse.gef.examples.text/src/org/eclipse/gef/examples/text/model/commands/ApplyBooleanStyle.java index 2cfd8389d..e2f27aa9e 100644 --- a/org.eclipse.gef.examples.text/src/org/eclipse/gef/examples/text/model/commands/ApplyBooleanStyle.java +++ b/org.eclipse.gef.examples.text/src/org/eclipse/gef/examples/text/model/commands/ApplyBooleanStyle.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2004, 2010 IBM Corporation and others. + * Copyright (c) 2004, 2023 IBM Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at @@ -43,6 +43,7 @@ public ApplyBooleanStyle(ModelLocation start, ModelLocation end, String keys[], this.values = values; } + @Override public void apply() { right = begin.subdivideRun(endOffset); if (right.getText().length() == 0) @@ -73,14 +74,17 @@ else if (key.equals(Style.PROPERTY_FONT)) parent.remove(begin); } + @Override public boolean canApply() { return true; } + @Override public ModelLocation getResultingLocation() { return new ModelLocation(middle, middle.size()); } + @Override public void rollback() { int index = parent.getChildren().indexOf(container); parent.remove(container); diff --git a/org.eclipse.gef.examples.text/src/org/eclipse/gef/examples/text/model/commands/ApplyMultiStyle.java b/org.eclipse.gef.examples.text/src/org/eclipse/gef/examples/text/model/commands/ApplyMultiStyle.java index 011050f5b..a2286fa2b 100644 --- a/org.eclipse.gef.examples.text/src/org/eclipse/gef/examples/text/model/commands/ApplyMultiStyle.java +++ b/org.eclipse.gef.examples.text/src/org/eclipse/gef/examples/text/model/commands/ApplyMultiStyle.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2005, 2010 IBM Corporation and others. + * Copyright (c) 2005, 2023 IBM Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at @@ -16,7 +16,8 @@ public class ApplyMultiStyle extends MiniEdit { - private int oldValue, newValue = -1; + private int oldValue; + private int newValue = -1; private String styleID; private Style style; @@ -26,10 +27,12 @@ public ApplyMultiStyle(Container c, String property, Object value) { newValue = ((Integer) value).intValue(); } + @Override public boolean canApply() { return newValue != -1; } + @Override public void apply() { if (Style.PROPERTY_ALIGNMENT.equals(styleID)) { oldValue = style.getAlignment(); @@ -40,10 +43,12 @@ public void apply() { } } + @Override public ModelLocation getResultingLocation() { return null; } + @Override public void rollback() { if (Style.PROPERTY_ALIGNMENT.equals(styleID)) style.setAlignment(oldValue); diff --git a/org.eclipse.gef.examples.text/src/org/eclipse/gef/examples/text/model/commands/ChangeString.java b/org.eclipse.gef.examples.text/src/org/eclipse/gef/examples/text/model/commands/ChangeString.java index 9fb7d13ea..30b2acfd3 100644 --- a/org.eclipse.gef.examples.text/src/org/eclipse/gef/examples/text/model/commands/ChangeString.java +++ b/org.eclipse.gef.examples.text/src/org/eclipse/gef/examples/text/model/commands/ChangeString.java @@ -22,7 +22,7 @@ public class ChangeString extends MiniEdit { private String pending; - private char insertedChars[]; + private char[] insertedChars; private final int offset; @@ -49,6 +49,7 @@ public void appendText(String append) { /** * @see org.eclipse.gef.commands.Command#execute() */ + @Override public void apply() { if (overwrite) overwrittenText = run.overwriteText(pending, offset); @@ -58,6 +59,7 @@ public void apply() { pending = null; } + @Override public boolean canApply() { return pending != null; } @@ -70,17 +72,19 @@ public void commitPending() { overwrittenText += run.overwriteText(pending, offset + insertedChars.length); else run.insertText(pending, offset + insertedChars.length); - char old[] = insertedChars; + char[] old = insertedChars; insertedChars = new char[old.length + 1]; System.arraycopy(old, 0, insertedChars, 0, old.length); insertedChars[insertedChars.length - 1] = pending.toCharArray()[0]; pending = null; } + @Override public ModelLocation getResultingLocation() { return new ModelLocation(run, offset + insertedChars.length); } + @Override public void reapply() { if (overwrite) overwrittenText = run.overwriteText(new String(insertedChars), offset); @@ -88,6 +92,7 @@ public void reapply() { run.insertText(new String(insertedChars), offset); } + @Override public void rollback() { if (overwrite) { run.overwriteText(overwrittenText, offset); diff --git a/org.eclipse.gef.examples.text/src/org/eclipse/gef/examples/text/model/commands/CompoundEditCommand.java b/org.eclipse.gef.examples.text/src/org/eclipse/gef/examples/text/model/commands/CompoundEditCommand.java index 7ad1848df..a4b3a62e8 100644 --- a/org.eclipse.gef.examples.text/src/org/eclipse/gef/examples/text/model/commands/CompoundEditCommand.java +++ b/org.eclipse.gef.examples.text/src/org/eclipse/gef/examples/text/model/commands/CompoundEditCommand.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2004, 2010 IBM Corporation and others. + * Copyright (c) 2004, 2023 IBM Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at @@ -28,11 +28,11 @@ public class CompoundEditCommand extends ExampleTextCommand implements Appendabl private ModelLocation beginLocation; - List edits = new ArrayList(); + private List edits = new ArrayList<>(); private ModelLocation endLocation; - ArrayList pending; + private List pending; /** * @since 3.1 @@ -45,50 +45,51 @@ public CompoundEditCommand(String label) { /** * @see org.eclipse.gef.commands.Command#execute() */ + @Override public void execute() { executePending(); } + @Override public boolean canExecute() { return canExecutePending(); } + @Override public boolean canExecutePending() { - if (pending == null || pending.size() == 0) + if (pending == null || pending.isEmpty()) return false; - for (int i = 0; i < pending.size(); i++) { - MiniEdit edit = (MiniEdit) pending.get(i); - if (edit == null || !edit.canApply()) - return false; - } - return true; + + return pending.stream().noneMatch(edit -> (edit == null || !edit.canApply())); } + @Override public void executePending() { Assert.isNotNull(pending); - for (int i = 0; i < pending.size(); i++) { - MiniEdit edit = (MiniEdit) pending.get(i); - edit.apply(); - } + pending.forEach(MiniEdit::apply); edits.addAll(pending); pending = null; } + @Override public void flushPending() { pending = null; } + @Override public SelectionRange getExecuteSelectionRange(GraphicalTextViewer viewer) { - ModelLocation loc = ((MiniEdit) edits.get(edits.size() - 1)).getResultingLocation(); + ModelLocation loc = edits.get(edits.size() - 1).getResultingLocation(); if (loc == null) return getUndoSelectionRange(viewer); return new SelectionRange(lookupModel(viewer, loc.model), loc.offset); } + @Override public SelectionRange getRedoSelectionRange(GraphicalTextViewer viewer) { return getExecuteSelectionRange(viewer); } + @Override public SelectionRange getUndoSelectionRange(GraphicalTextViewer viewer) { TextEditPart begin = lookupModel(viewer, beginLocation.model); if (endLocation == null) @@ -99,18 +100,16 @@ public SelectionRange getUndoSelectionRange(GraphicalTextViewer viewer) { public void pendEdit(MiniEdit edit) { if (pending == null) - pending = new ArrayList(2); + pending = new ArrayList<>(2); pending.add(edit); } /** * @see org.eclipse.gef.commands.Command#redo() */ + @Override public void redo() { - for (int i = 0; i < edits.size(); i++) { - MiniEdit edit = (MiniEdit) edits.get(i); - edit.reapply(); - } + edits.forEach(MiniEdit::reapply); } public void setEndLocation(ModelLocation endLocation) { @@ -124,9 +123,10 @@ public void setBeginLocation(ModelLocation startLocation) { /** * @see org.eclipse.gef.commands.Command#undo() */ + @Override public void undo() { for (int i = edits.size() - 1; i >= 0; i--) { - MiniEdit edit = (MiniEdit) edits.get(i); + MiniEdit edit = edits.get(i); edit.rollback(); } } diff --git a/org.eclipse.gef.examples.text/src/org/eclipse/gef/examples/text/model/commands/CompoundTextCommand.java b/org.eclipse.gef.examples.text/src/org/eclipse/gef/examples/text/model/commands/CompoundTextCommand.java index 1821f4351..37017573a 100644 --- a/org.eclipse.gef.examples.text/src/org/eclipse/gef/examples/text/model/commands/CompoundTextCommand.java +++ b/org.eclipse.gef.examples.text/src/org/eclipse/gef/examples/text/model/commands/CompoundTextCommand.java @@ -37,12 +37,14 @@ public void add(TextCommand command) { pending.add(command); } + @Override public boolean canExecute() { return canExecutePending(); } + @Override public boolean canExecutePending() { - if (pending.size() == 0) + if (pending.isEmpty()) return false; for (int i = 0; i < pending.size(); i++) { Command cmd = (Command) pending.get(i); @@ -54,16 +56,19 @@ public boolean canExecutePending() { return true; } + @Override public void dispose() { for (int i = 0; i < applied.size(); i++) ((Command) applied.get(i)).dispose(); flushPending(); } + @Override public void execute() { executePending(); } + @Override public void executePending() { for (int i = 0; i < pending.size(); i++) { Command cmd = (Command) pending.get(i); @@ -73,33 +78,39 @@ public void executePending() { flushPending(); } + @Override public void flushPending() { pending.clear(); } + @Override public SelectionRange getExecuteSelectionRange(GraphicalTextViewer viewer) { if (applied.isEmpty()) return null; return ((TextCommand) applied.get(applied.size() - 1)).getExecuteSelectionRange(viewer); } + @Override public SelectionRange getRedoSelectionRange(GraphicalTextViewer viewer) { if (applied.isEmpty()) return null; return ((TextCommand) applied.get(applied.size() - 1)).getExecuteSelectionRange(viewer); } + @Override public SelectionRange getUndoSelectionRange(GraphicalTextViewer viewer) { if (applied.isEmpty()) return null; return ((TextCommand) applied.get(0)).getUndoSelectionRange(viewer); } + @Override public void redo() { for (int i = 0; i < applied.size(); i++) ((Command) applied.get(i)).redo(); } + @Override public void undo() { for (int i = applied.size() - 1; i >= 0; i--) ((Command) applied.get(i)).undo(); diff --git a/org.eclipse.gef.examples.text/src/org/eclipse/gef/examples/text/model/commands/ConvertElementCommand.java b/org.eclipse.gef.examples.text/src/org/eclipse/gef/examples/text/model/commands/ConvertElementCommand.java index c067b9504..64136c64a 100644 --- a/org.eclipse.gef.examples.text/src/org/eclipse/gef/examples/text/model/commands/ConvertElementCommand.java +++ b/org.eclipse.gef.examples.text/src/org/eclipse/gef/examples/text/model/commands/ConvertElementCommand.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2004, 2010 IBM Corporation and others. + * Copyright (c) 2004, 2023 IBM Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at @@ -24,7 +24,7 @@ public class ConvertElementCommand extends ExampleTextCommand { private final TextRun text; - private final char removed[]; + private final char[] removed; private final int offset; private final ModelElement converted; private final ModelLocation caret; @@ -33,7 +33,7 @@ public class ConvertElementCommand extends ExampleTextCommand { * @since 3.1 */ public ConvertElementCommand(TextRun text, int begin, int end, ModelElement converted, ModelLocation caret) { - super("bogus"); + super("bogus"); //$NON-NLS-1$ this.text = text; this.offset = begin; this.converted = converted; @@ -41,6 +41,7 @@ public ConvertElementCommand(TextRun text, int begin, int end, ModelElement conv removed = text.getText().substring(begin, end).toCharArray(); } + @Override public void execute() { text.removeRange(offset, removed.length); Container container = text.getContainer(); @@ -48,14 +49,17 @@ public void execute() { container.add(converted); } + @Override public SelectionRange getRedoSelectionRange(GraphicalTextViewer viewer) { return null; } + @Override public SelectionRange getExecuteSelectionRange(GraphicalTextViewer viewer) { return new SelectionRange(lookupModel(viewer, caret.model), caret.offset); } + @Override public SelectionRange getUndoSelectionRange(GraphicalTextViewer viewer) { return null; } diff --git a/org.eclipse.gef.examples.text/src/org/eclipse/gef/examples/text/model/commands/ExampleTextCommand.java b/org.eclipse.gef.examples.text/src/org/eclipse/gef/examples/text/model/commands/ExampleTextCommand.java index 478bb6741..f7d16024f 100644 --- a/org.eclipse.gef.examples.text/src/org/eclipse/gef/examples/text/model/commands/ExampleTextCommand.java +++ b/org.eclipse.gef.examples.text/src/org/eclipse/gef/examples/text/model/commands/ExampleTextCommand.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2004, 2010 IBM Corporation and others. + * Copyright (c) 2004, 2023 IBM Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at @@ -22,10 +22,11 @@ */ public abstract class ExampleTextCommand extends Command implements TextCommand { - public ExampleTextCommand(String label) { + protected ExampleTextCommand(String label) { super(label); } + @SuppressWarnings("static-method") // allow subclasses to override public boolean canExecutePending() { return false; } diff --git a/org.eclipse.gef.examples.text/src/org/eclipse/gef/examples/text/model/commands/InsertModelElement.java b/org.eclipse.gef.examples.text/src/org/eclipse/gef/examples/text/model/commands/InsertModelElement.java index 1bff6dae9..8810cfff8 100644 --- a/org.eclipse.gef.examples.text/src/org/eclipse/gef/examples/text/model/commands/InsertModelElement.java +++ b/org.eclipse.gef.examples.text/src/org/eclipse/gef/examples/text/model/commands/InsertModelElement.java @@ -29,18 +29,22 @@ public InsertModelElement(Container parent, int offset, ModelElement child, Mode this.location = location; } + @Override public void apply() { parent.add(child, offset); } + @Override public boolean canApply() { return true; } + @Override public ModelLocation getResultingLocation() { return location; } + @Override public void rollback() { parent.remove(child); } diff --git a/org.eclipse.gef.examples.text/src/org/eclipse/gef/examples/text/model/commands/MergeWithPrevious.java b/org.eclipse.gef.examples.text/src/org/eclipse/gef/examples/text/model/commands/MergeWithPrevious.java index 109e4490f..4c330dec2 100644 --- a/org.eclipse.gef.examples.text/src/org/eclipse/gef/examples/text/model/commands/MergeWithPrevious.java +++ b/org.eclipse.gef.examples.text/src/org/eclipse/gef/examples/text/model/commands/MergeWithPrevious.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2004, 2010 IBM Corporation and others. + * Copyright (c) 2004, 2023 IBM Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at @@ -24,9 +24,9 @@ */ public class MergeWithPrevious extends MiniEdit { - private int index; - private Container container; - private TextRun run; + private final int index; + private final Container container; + private final TextRun run; public MergeWithPrevious(TextEditPart part) { run = (TextRun) part.getModel(); @@ -35,30 +35,34 @@ public MergeWithPrevious(TextEditPart part) { } private TextRun getPreviousTextRun() { - ModelElement candidate = (ModelElement) container.getChildren().get(index - 1); - while (candidate instanceof Container) { - candidate = (ModelElement) ((Container) candidate).getChildren().get(((Container) candidate).size() - 1); + ModelElement candidate = container.getChildren().get(index - 1); + while (candidate instanceof Container cont) { + candidate = cont.getChildren().get(cont.size() - 1); } return (TextRun) candidate; } + @Override public void apply() { container.remove(run); TextRun previous = getPreviousTextRun(); previous.insertText(run.getText(), previous.size()); } + @Override public boolean canApply() { return index > 0; } + @Override public ModelLocation getResultingLocation() { TextRun previous = getPreviousTextRun(); return new ModelLocation(previous, previous.size() - run.size()); } + @Override public void rollback() { - throw new RuntimeException("not implemented"); + throw new RuntimeException("not implemented"); //$NON-NLS-1$ } } diff --git a/org.eclipse.gef.examples.text/src/org/eclipse/gef/examples/text/model/commands/NestElementCommand.java b/org.eclipse.gef.examples.text/src/org/eclipse/gef/examples/text/model/commands/NestElementCommand.java index 7a8e21b53..2f6e941b3 100644 --- a/org.eclipse.gef.examples.text/src/org/eclipse/gef/examples/text/model/commands/NestElementCommand.java +++ b/org.eclipse.gef.examples.text/src/org/eclipse/gef/examples/text/model/commands/NestElementCommand.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2004, 2010 IBM Corporation and others. + * Copyright (c) 2004, 2023 IBM Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at @@ -22,41 +22,45 @@ */ public class NestElementCommand extends ExampleTextCommand { - private TextRun run; - private Container newParent; - int index; - private int caretOffset; + private final TextRun run; + private final int index; + private final int caretOffset; public NestElementCommand(TextEditPart part, int caretOffset) { - super("indent"); + super("indent"); //$NON-NLS-1$ this.caretOffset = caretOffset; run = (TextRun) part.getModel(); index = run.getContainer().getChildren().indexOf(run); } + @Override public void execute() { Container container = run.getContainer(); - newParent = (Container) container.getChildren().get(index - 1); + Container newParent = (Container) container.getChildren().get(index - 1); container.remove(run); run.setType(newParent.getChildType()); newParent.add(run); } + @Override public boolean canExecute() { if (index == 0) return false; return run.getContainer().getChildren().get(index - 1) instanceof Container; } + @Override public SelectionRange getExecuteSelectionRange(GraphicalTextViewer viewer) { return new SelectionRange(lookupModel(viewer, run), caretOffset); } + @Override public SelectionRange getRedoSelectionRange(GraphicalTextViewer viewer) { return getExecuteSelectionRange(viewer); } + @Override public SelectionRange getUndoSelectionRange(GraphicalTextViewer viewer) { return getExecuteSelectionRange(viewer); } diff --git a/org.eclipse.gef.examples.text/src/org/eclipse/gef/examples/text/model/commands/ProcessMacroCommand.java b/org.eclipse.gef.examples.text/src/org/eclipse/gef/examples/text/model/commands/ProcessMacroCommand.java index 9cacb5e4e..e2055c72c 100644 --- a/org.eclipse.gef.examples.text/src/org/eclipse/gef/examples/text/model/commands/ProcessMacroCommand.java +++ b/org.eclipse.gef.examples.text/src/org/eclipse/gef/examples/text/model/commands/ProcessMacroCommand.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2004, 2010 IBM Corporation and others. + * Copyright (c) 2004, 2023 IBM Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at @@ -26,7 +26,7 @@ public class ProcessMacroCommand extends CompoundEditCommand { * @since 3.1 */ public ProcessMacroCommand(TextRun run, int begin, int end, ModelElement substitution, ModelLocation loc) { - super("$$conversion"); + super("$$conversion"); //$NON-NLS-1$ RemoveRange removal = new RemoveRange(run, begin, run, end); pendEdit(removal); SubdivideElement subdivide = new SubdivideElement(run, begin); @@ -39,6 +39,7 @@ public ProcessMacroCommand(TextRun run, int begin, int end, ModelElement substit /** * @see org.eclipse.gef.examples.text.TextCommand#getRedoSelectionRange(org.eclipse.gef.examples.text.GraphicalTextViewer) */ + @Override public SelectionRange getRedoSelectionRange(GraphicalTextViewer viewer) { return null; } @@ -46,6 +47,7 @@ public SelectionRange getRedoSelectionRange(GraphicalTextViewer viewer) { /** * @see org.eclipse.gef.examples.text.TextCommand#getExecuteSelectionRange(org.eclipse.gef.examples.text.GraphicalTextViewer) */ + @Override public SelectionRange getExecuteSelectionRange(GraphicalTextViewer viewer) { return super.getExecuteSelectionRange(viewer); } @@ -53,6 +55,7 @@ public SelectionRange getExecuteSelectionRange(GraphicalTextViewer viewer) { /** * @see org.eclipse.gef.examples.text.TextCommand#getUndoSelectionRange(org.eclipse.gef.examples.text.GraphicalTextViewer) */ + @Override public SelectionRange getUndoSelectionRange(GraphicalTextViewer viewer) { return null; } diff --git a/org.eclipse.gef.examples.text/src/org/eclipse/gef/examples/text/model/commands/PromoteElementCommand.java b/org.eclipse.gef.examples.text/src/org/eclipse/gef/examples/text/model/commands/PromoteElementCommand.java index f302a645b..8340294ab 100644 --- a/org.eclipse.gef.examples.text/src/org/eclipse/gef/examples/text/model/commands/PromoteElementCommand.java +++ b/org.eclipse.gef.examples.text/src/org/eclipse/gef/examples/text/model/commands/PromoteElementCommand.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2004, 2010 IBM Corporation and others. + * Copyright (c) 2004, 2023 IBM Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at @@ -31,12 +31,13 @@ public class PromoteElementCommand extends ExampleTextCommand { private int caretOffset; public PromoteElementCommand(TextEditPart part, int caretOffset) { - super("decrease indentation"); + super("decrease indentation"); //$NON-NLS-1$ this.caretOffset = caretOffset; run = (TextRun) part.getModel(); oldParent = run.getContainer(); } + @Override public void execute() { index = oldParent.getChildren().indexOf(run); Container newParent = oldParent.getContainer(); @@ -48,19 +49,23 @@ public void execute() { oldParent.getContainer().remove(oldParent); } + @Override public boolean canExecute() { // Is there a container into which the run can be promoted. return oldParent.getContainer() != null; } + @Override public SelectionRange getExecuteSelectionRange(GraphicalTextViewer viewer) { return new SelectionRange(lookupModel(viewer, run), caretOffset); } + @Override public SelectionRange getRedoSelectionRange(GraphicalTextViewer viewer) { return getExecuteSelectionRange(viewer); } + @Override public SelectionRange getUndoSelectionRange(GraphicalTextViewer viewer) { return getExecuteSelectionRange(viewer); } diff --git a/org.eclipse.gef.examples.text/src/org/eclipse/gef/examples/text/model/commands/RemoveRange.java b/org.eclipse.gef.examples.text/src/org/eclipse/gef/examples/text/model/commands/RemoveRange.java index 8231004a7..c6e2c0045 100644 --- a/org.eclipse.gef.examples.text/src/org/eclipse/gef/examples/text/model/commands/RemoveRange.java +++ b/org.eclipse.gef.examples.text/src/org/eclipse/gef/examples/text/model/commands/RemoveRange.java @@ -47,7 +47,7 @@ public RemoveRange(TextRun begin, int so, TextRun end, int eo) { if (begin == end) { clipBeginning = new RemoveText(begin, so, eo); } else { - List remove = ModelUtil.getModelSpan(begin, so, end, eo); + List remove = ModelUtil.getModelSpan(begin, so, end, eo); removed = new ModelElement[remove.size()]; removedFrom = new Container[removed.length]; @@ -64,6 +64,7 @@ public RemoveRange(TextRun begin, int so, TextRun end, int eo) { } } + @Override public void apply() { if (removed != null) { removalIndices = new int[removed.length]; @@ -77,10 +78,12 @@ public void apply() { clipEnding.apply(); } + @Override public boolean canApply() { return true; } + @Override public ModelLocation getResultingLocation() { return clipBeginning.getResultingLocation(); } @@ -88,10 +91,12 @@ public ModelLocation getResultingLocation() { /** * @see org.eclipse.gef.examples.text.model.commands.MiniEdit#reapply() */ + @Override public void reapply() { apply(); } + @Override public void rollback() { if (removed != null) for (int i = 0; i < removed.length; i++) diff --git a/org.eclipse.gef.examples.text/src/org/eclipse/gef/examples/text/model/commands/RemoveText.java b/org.eclipse.gef.examples.text/src/org/eclipse/gef/examples/text/model/commands/RemoveText.java index ff81e4818..6620662f9 100644 --- a/org.eclipse.gef.examples.text/src/org/eclipse/gef/examples/text/model/commands/RemoveText.java +++ b/org.eclipse.gef.examples.text/src/org/eclipse/gef/examples/text/model/commands/RemoveText.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2004, 2010 IBM Corporation and others. + * Copyright (c) 2004, 2023 IBM Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at @@ -18,7 +18,7 @@ public class RemoveText extends MiniEdit { int offset; - char chars[]; + char[] chars; private final TextRun run; @@ -28,22 +28,27 @@ public RemoveText(TextRun run, int begin, int end) { this.chars = run.getText().substring(offset, end).toCharArray(); } + @Override public void apply() { run.removeRange(offset, chars.length); } + @Override public boolean canApply() { return chars != null && chars.length != 0; } + @Override public ModelLocation getResultingLocation() { return new ModelLocation(run, offset); } + @Override public void reapply() { apply(); } + @Override public void rollback() { run.insertText(new String(chars), offset); } diff --git a/org.eclipse.gef.examples.text/src/org/eclipse/gef/examples/text/model/commands/SingleEditCommand.java b/org.eclipse.gef.examples.text/src/org/eclipse/gef/examples/text/model/commands/SingleEditCommand.java index fa05efdda..aeabfad9e 100644 --- a/org.eclipse.gef.examples.text/src/org/eclipse/gef/examples/text/model/commands/SingleEditCommand.java +++ b/org.eclipse.gef.examples.text/src/org/eclipse/gef/examples/text/model/commands/SingleEditCommand.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2004, 2010 IBM Corporation and others. + * Copyright (c) 2004, 2023 IBM Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at @@ -30,16 +30,18 @@ public class SingleEditCommand extends ExampleTextCommand { * @since 3.1 */ public SingleEditCommand(MiniEdit edit, ModelLocation start, ModelLocation end) { - super(""); + super(""); //$NON-NLS-1$ this.edit = edit; this.start = start; this.end = end; } + @Override public void execute() { edit.apply(); } + @Override public SelectionRange getExecuteSelectionRange(GraphicalTextViewer viewer) { ModelLocation loc = edit.getResultingLocation(); if (loc != null) @@ -47,20 +49,24 @@ public SelectionRange getExecuteSelectionRange(GraphicalTextViewer viewer) { return getUndoSelectionRange(viewer); } + @Override public SelectionRange getRedoSelectionRange(GraphicalTextViewer viewer) { return getExecuteSelectionRange(viewer); } + @Override public SelectionRange getUndoSelectionRange(GraphicalTextViewer viewer) { TextLocation startLoc = new TextLocation(lookupModel(viewer, start.model), start.offset); TextLocation endLoc = new TextLocation(lookupModel(viewer, end.model), end.offset); return new SelectionRange(startLoc, endLoc); } + @Override public void redo() { edit.reapply(); } + @Override public void undo() { edit.rollback(); } diff --git a/org.eclipse.gef.examples.text/src/org/eclipse/gef/examples/text/model/commands/SubdivideElement.java b/org.eclipse.gef.examples.text/src/org/eclipse/gef/examples/text/model/commands/SubdivideElement.java index 06d2f6d44..e62392614 100644 --- a/org.eclipse.gef.examples.text/src/org/eclipse/gef/examples/text/model/commands/SubdivideElement.java +++ b/org.eclipse.gef.examples.text/src/org/eclipse/gef/examples/text/model/commands/SubdivideElement.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2004, 2010 IBM Corporation and others. + * Copyright (c) 2004, 2023 IBM Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at @@ -30,28 +30,33 @@ public SubdivideElement(TextRun run, int offset) { this.offset = offset; } + @Override public void apply() { inserted = run.subdivideRun(offset); int index = run.getContainer().getChildren().indexOf(run); run.getContainer().add(inserted, index + 1); } + @Override public boolean canApply() { return true; } + @Override public void reapply() { - throw new RuntimeException("Need to implement"); + throw new RuntimeException("Need to implement"); //$NON-NLS-1$ } + @Override public ModelLocation getResultingLocation() { return new ModelLocation(inserted, 0); } + @Override public void rollback() { inserted.getContainer().remove(inserted); run.insertText(inserted.getText(), run.size()); - inserted.setText(""); + inserted.setText(""); //$NON-NLS-1$ } } diff --git a/org.eclipse.gef.examples.text/src/org/eclipse/gef/examples/text/tools/SelectionRangeDragTracker.java b/org.eclipse.gef.examples.text/src/org/eclipse/gef/examples/text/tools/SelectionRangeDragTracker.java index 680713dc3..ceb8ff99f 100644 --- a/org.eclipse.gef.examples.text/src/org/eclipse/gef/examples/text/tools/SelectionRangeDragTracker.java +++ b/org.eclipse.gef.examples.text/src/org/eclipse/gef/examples/text/tools/SelectionRangeDragTracker.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2004, 2010 IBM Corporation and others. + * Copyright (c) 2004, 2023 IBM Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at @@ -50,6 +50,7 @@ public SelectionRangeDragTracker(TextEditPart part) { this.textSource = part; } + @Override protected Cursor calculateCursor() { return Cursors.IBEAM; } @@ -123,15 +124,15 @@ private void doWordSelect() { private void doWordSwipe() { } + @Override protected String getCommandName() { - return "Drop Text Request"; + return "Drop Text Request"; //$NON-NLS-1$ } private SearchResult getCurrentTextLocation() { SearchResult result = new SearchResult(); EditPart part = getCurrentViewer().findObjectAt(getLocation()); - if (part instanceof TextEditPart) { - TextEditPart textPart = (TextEditPart) part; + if (part instanceof TextEditPart textPart) { if (textPart.acceptsCaret()) { CaretRequest request = new CaretRequest(); request.setType(CaretRequest.LOCATION); @@ -150,6 +151,7 @@ private TextEditPart getSource() { return textSource; } + @Override protected boolean handleButtonDown(int button) { if (button == 1) { SearchResult result = getCurrentTextLocation(); @@ -161,6 +163,7 @@ protected boolean handleButtonDown(int button) { return super.handleButtonDown(button); } + @Override protected boolean handleDoubleClick(int button) { if (button == 1) { doWordSelect(); @@ -170,6 +173,7 @@ protected boolean handleDoubleClick(int button) { return super.handleDoubleClick(button); } + @Override protected boolean handleDragInProgress() { // $TODO during a swipe, the viewer should not be firing selection // changes the whole time. @@ -182,6 +186,7 @@ protected boolean handleDragInProgress() { return super.handleDragInProgress(); } + @Override protected boolean handleDragStarted() { return stateTransition(STATE_START, STATE_SWIPE); } diff --git a/org.eclipse.gef.examples.text/src/org/eclipse/gef/examples/text/tools/TextTool.java b/org.eclipse.gef.examples.text/src/org/eclipse/gef/examples/text/tools/TextTool.java index af438074a..87c3344f4 100644 --- a/org.eclipse.gef.examples.text/src/org/eclipse/gef/examples/text/tools/TextTool.java +++ b/org.eclipse.gef.examples.text/src/org/eclipse/gef/examples/text/tools/TextTool.java @@ -47,7 +47,6 @@ import org.eclipse.gef.tools.SelectionTool; import org.eclipse.gef.tools.ToolUtilities; import org.eclipse.jface.viewers.ISelectionChangedListener; -import org.eclipse.jface.viewers.SelectionChangedEvent; import org.eclipse.swt.SWT; import org.eclipse.swt.custom.ST; import org.eclipse.swt.events.KeyEvent; @@ -67,35 +66,33 @@ public class TextTool extends SelectionTool implements StyleProvider { private static final int MODE_DEL = 3; private static final int MODE_TYPE = 1; private static final String KEY_OVERWRITE = "gef.texttool.overwrite"; //$NON-NLS-1$ - private CommandStackEventListener commandListener = new CommandStackEventListener() { - public void stackChanged(org.eclipse.gef.commands.CommandStackEvent event) { - if ((event.getDetail() & CommandStack.POST_MASK) != 0) { - fireStyleChanges(); - discardCaretLocation(); - } + private CommandStackEventListener commandListener = event -> { + if ((event.getDetail() & CommandStack.POST_MASK) != 0) { + fireStyleChanges(); + discardCaretLocation(); } }; private StyleListener listener; private AppendableCommand pendingCommand; - private ISelectionChangedListener selectionListener = new ISelectionChangedListener() { - public void selectionChanged(SelectionChangedEvent event) { - fireStyleChanges(); - getCaret().setVisible(getSelectionRange() != null); - queueCaretRefresh(true); - } + private ISelectionChangedListener selectionListener = event -> { + fireStyleChanges(); + getCaret().setVisible(getSelectionRange() != null); + queueCaretRefresh(true); }; private UpdateListener updateListener = new UpdateListener() { + @Override public void notifyPainting(Rectangle damage, Map dirtyRegions) { queueCaretRefresh(false); } + @Override public void notifyValidating() { } }; - private List styleKeys = new ArrayList(); + private List styleKeys = new ArrayList<>(); // @TODO:Pratik StyleService cannot be final private final StyleService styleService; - private List styleValues = new ArrayList(); + private List styleValues = new ArrayList<>(); private CaretRefresh caretRefresh; private int textInputMode, caretXLoc; private boolean isMirrored, xCaptured, overwrite; @@ -117,15 +114,16 @@ public TextTool(StyleService service) { * !isInState(STATE_INITIAL) && e.character == SWT.ESC; } */ + @Override public void addStyleListener(StyleListener listener) { Assert.isTrue(this.listener == null); this.listener = listener; } + @Override protected Cursor calculateCursor() { EditPart target = getTargetEditPart(); - if (target instanceof TextEditPart) { - TextEditPart textTarget = (TextEditPart) target; + if (target instanceof TextEditPart textTarget) { if (textTarget.acceptsCaret()) return Cursors.IBEAM; } @@ -378,8 +376,7 @@ private void doSelect(Object type, boolean isForward, boolean append, Point loc) SelectionRange range = getSelectionRange(); if (range == null) { - if (viewer.getContents() instanceof TextEditPart) { - TextEditPart tep = (TextEditPart) viewer.getContents(); + if (viewer.getContents() instanceof TextEditPart tep) { if (tep.acceptsCaret()) tep.getTextLocation(search, result); } @@ -513,8 +510,8 @@ private UpdateManager getUpdateManager() { EditPartViewer viewer = getCurrentViewer(); if (viewer != null) { EditPart root = viewer.getRootEditPart(); - if (root instanceof GraphicalEditPart) - return ((GraphicalEditPart) root).getFigure().getUpdateManager(); + if (root instanceof GraphicalEditPart gep) + return gep.getFigure().getUpdateManager(); } return null; } @@ -531,6 +528,7 @@ private Object getSelectionStyle(String styleID, boolean isState) { return manager.getStyleValue(styleID, getSelectionRange()); } + @Override public Object getStyle(String styleID) { for (int i = 0; i < styleKeys.size(); i++) if (styleID.equals(styleKeys.get(i))) @@ -538,6 +536,7 @@ public Object getStyle(String styleID) { return getSelectionStyle(styleID, false); } + @Override public Object getStyleState(String styleID) { return getSelectionStyle(styleID, true); } @@ -546,7 +545,8 @@ private TextEditPart getTextTarget(Request request) { SelectionRange range = getSelectionRange(); if (range == null) return null; - EditPart target, candidate = ToolUtilities.findCommonAncestor(range.begin.part, range.end.part); + EditPart target; + EditPart candidate = ToolUtilities.findCommonAncestor(range.begin.part, range.end.part); do { target = candidate.getTargetEditPart(request); @@ -559,23 +559,27 @@ GraphicalTextViewer getTextualViewer() { return (GraphicalTextViewer) getCurrentViewer(); } + @Override protected boolean handleButtonDown(int button) { discardCaretLocation(); return super.handleButtonDown(button); } + @Override protected boolean handleCommandStackChanged() { setTextInputMode(0); discardCaretLocation(); return super.handleCommandStackChanged(); } + @Override protected boolean handleFocusGained() { if (getSelectionRange() == null) doSelect(CaretRequest.DOCUMENT, false, false, null); return super.handleFocusGained(); } + @Override protected boolean handleKeyDown(KeyEvent e) { if (isInState(STATE_INITIAL) && getTextualViewer().isTextSelected()) doKeyDown(e); @@ -585,12 +589,14 @@ protected boolean handleKeyDown(KeyEvent e) { return true; } + @Override protected void handleKeyTraversed(TraverseEvent event) { if ((event.detail == SWT.TRAVERSE_TAB_PREVIOUS || event.detail == SWT.TRAVERSE_TAB_NEXT) && (event.stateMask & SWT.CTRL) == 0) event.doit = false; } + @Override protected boolean handleMove() { super.handleMove(); refreshCursor(); @@ -598,7 +604,6 @@ protected boolean handleMove() { } private boolean handleTextEdit(TextRequest edit) { - GraphicalTextViewer viewer = getTextualViewer(); EditPart target = getTextTarget(edit); Command insert = null; @@ -620,6 +625,7 @@ private boolean handleTextEdit(TextRequest edit) { if (!pendingCommand.canExecutePending()) return false; pendingCommand.executePending(); + GraphicalTextViewer viewer = getTextualViewer(); viewer.setSelectionRange(((TextCommand) pendingCommand).getExecuteSelectionRange(viewer)); } @@ -697,11 +703,13 @@ void queueCaretRefresh(boolean revealAfterwards) { caretRefresh.enableReveal(revealAfterwards); } + @Override public void removeStyleListener(StyleListener listener) { Assert.isTrue(this.listener == listener); this.listener = null; } + @Override public void setDragTracker(DragTracker newDragTracker) { if (getDragTracker() == newDragTracker) return; @@ -709,6 +717,7 @@ public void setDragTracker(DragTracker newDragTracker) { super.setDragTracker(newDragTracker); } + @Override public void setStyle(String styleID, Object newValue) { // Check for cancellations: lookup old style and remove any pending ones Object oldValue = getSelectionStyle(styleID, false); @@ -741,6 +750,7 @@ public void setStyle(String styleID, Object newValue) { } } + @Override public void setViewer(EditPartViewer viewer) { EditPartViewer currentViewer = getCurrentViewer(); if (viewer == currentViewer || viewer == null) @@ -799,6 +809,7 @@ public CaretRefresh(boolean reveal) { enableReveal(reveal); } + @Override public void run() { refreshCaret(); caretRefresh = null; diff --git a/org.eclipse.gef.examples.text/src/org/eclipse/gef/examples/text/wizard/NewFileWizardPage.java b/org.eclipse.gef.examples.text/src/org/eclipse/gef/examples/text/wizard/NewFileWizardPage.java index e23786bbe..8792320e2 100644 --- a/org.eclipse.gef.examples.text/src/org/eclipse/gef/examples/text/wizard/NewFileWizardPage.java +++ b/org.eclipse.gef.examples.text/src/org/eclipse/gef/examples/text/wizard/NewFileWizardPage.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2005, 2010 IBM Corporation and others. + * Copyright (c) 2005, 2023 IBM Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at @@ -19,8 +19,6 @@ import org.eclipse.jface.viewers.IStructuredSelection; import org.eclipse.jface.wizard.WizardPage; import org.eclipse.swt.SWT; -import org.eclipse.swt.events.ModifyEvent; -import org.eclipse.swt.events.ModifyListener; import org.eclipse.swt.events.SelectionAdapter; import org.eclipse.swt.events.SelectionEvent; import org.eclipse.swt.layout.GridData; @@ -49,16 +47,17 @@ public class NewFileWizardPage extends WizardPage { * @param pageName */ public NewFileWizardPage(ISelection selection) { - super("wizardPage"); - setTitle("GEF WYSIWYG Text Document"); - setDescription("This wizard creates a GEF-based WYSIWYG text document with " - + "a *.text. extension. Choose a container and file name for the new" + " resource."); + super("wizardPage"); //$NON-NLS-1$ + setTitle("GEF WYSIWYG Text Document"); //$NON-NLS-1$ + setDescription("This wizard creates a GEF-based WYSIWYG text document with " //$NON-NLS-1$ + + "a *.text. extension. Choose a container and file name for the new" + " resource."); //$NON-NLS-1$ //$NON-NLS-2$ this.selection = selection; } /** * @see IDialogPage#createControl(Composite) */ + @Override public void createControl(Composite parent) { Composite container = new Composite(parent, SWT.NULL); GridLayout layout = new GridLayout(); @@ -66,35 +65,28 @@ public void createControl(Composite parent) { layout.numColumns = 3; layout.verticalSpacing = 9; Label label = new Label(container, SWT.NULL); - label.setText("&Container:"); + label.setText("&Container:"); //$NON-NLS-1$ containerText = new Text(container, SWT.BORDER | SWT.SINGLE); GridData gd = new GridData(GridData.FILL_HORIZONTAL); containerText.setLayoutData(gd); - containerText.addModifyListener(new ModifyListener() { - public void modifyText(ModifyEvent e) { - dialogChanged(); - } - }); + containerText.addModifyListener(e -> dialogChanged()); Button button = new Button(container, SWT.PUSH); - button.setText("Browse..."); + button.setText("Browse..."); //$NON-NLS-1$ button.addSelectionListener(new SelectionAdapter() { + @Override public void widgetSelected(SelectionEvent e) { handleBrowse(); } }); label = new Label(container, SWT.NULL); - label.setText("&File name:"); + label.setText("&File name:"); //$NON-NLS-1$ fileText = new Text(container, SWT.BORDER | SWT.SINGLE); gd = new GridData(GridData.FILL_HORIZONTAL); fileText.setLayoutData(gd); - fileText.addModifyListener(new ModifyListener() { - public void modifyText(ModifyEvent e) { - dialogChanged(); - } - }); + fileText.addModifyListener(e -> dialogChanged()); initialize(); dialogChanged(); setControl(container); @@ -104,21 +96,20 @@ public void modifyText(ModifyEvent e) { * Tests if the current workbench selection is a suitable container to use. */ private void initialize() { - if (selection != null && selection.isEmpty() == false && selection instanceof IStructuredSelection) { - IStructuredSelection ssel = (IStructuredSelection) selection; + if (selection != null && selection.isEmpty() == false && selection instanceof IStructuredSelection ssel) { if (ssel.size() > 1) return; Object obj = ssel.getFirstElement(); - if (obj instanceof IResource) { + if (obj instanceof IResource res) { IContainer container; - if (obj instanceof IContainer) - container = (IContainer) obj; + if (obj instanceof IContainer cont) + container = cont; else - container = ((IResource) obj).getParent(); + container = res.getParent(); containerText.setText(container.getFullPath().toString()); } } - fileText.setText("new_file.text"); + fileText.setText("new_file.text"); //$NON-NLS-1$ } /** @@ -127,7 +118,7 @@ private void initialize() { */ private void handleBrowse() { ContainerSelectionDialog dialog = new ContainerSelectionDialog(getShell(), - ResourcesPlugin.getWorkspace().getRoot(), false, "Select new file container"); + ResourcesPlugin.getWorkspace().getRoot(), false, "Select new file container"); //$NON-NLS-1$ if (dialog.open() == ContainerSelectionDialog.OK) { Object[] result = dialog.getResult(); if (result.length == 1) { @@ -144,30 +135,30 @@ private void dialogChanged() { String fileName = getFileName(); if (getContainerName().length() == 0) { - updateStatus("File container must be specified"); + updateStatus("File container must be specified"); //$NON-NLS-1$ return; } if (container == null || (container.getType() & (IResource.PROJECT | IResource.FOLDER)) == 0) { - updateStatus("File container must exist"); + updateStatus("File container must exist"); //$NON-NLS-1$ return; } if (!container.isAccessible()) { - updateStatus("Project must be writable"); + updateStatus("Project must be writable"); //$NON-NLS-1$ return; } if (fileName.length() == 0) { - updateStatus("File name must be specified"); + updateStatus("File name must be specified"); //$NON-NLS-1$ return; } if (fileName.replace('\\', '/').indexOf('/', 1) > 0) { - updateStatus("File name must be valid"); + updateStatus("File name must be valid"); //$NON-NLS-1$ return; } int dotLoc = fileName.lastIndexOf('.'); if (dotLoc != -1) { String ext = fileName.substring(dotLoc + 1); - if (ext.equalsIgnoreCase("text") == false) { - updateStatus("File extension must be \"text\""); + if (ext.equalsIgnoreCase("text") == false) { //$NON-NLS-1$ + updateStatus("File extension must be \"text\""); //$NON-NLS-1$ return; } } diff --git a/org.eclipse.gef.examples.text/src/org/eclipse/gef/examples/text/wizard/TextEditorWizard.java b/org.eclipse.gef.examples.text/src/org/eclipse/gef/examples/text/wizard/TextEditorWizard.java index 05868aca7..a74383241 100644 --- a/org.eclipse.gef.examples.text/src/org/eclipse/gef/examples/text/wizard/TextEditorWizard.java +++ b/org.eclipse.gef.examples.text/src/org/eclipse/gef/examples/text/wizard/TextEditorWizard.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2005, 2010 IBM Corporation and others. + * Copyright (c) 2005, 2023 IBM Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at @@ -61,6 +61,7 @@ public TextEditorWizard() { /** * Adding the page to the wizard. */ + @Override public void addPages() { page = new NewFileWizardPage(selection); addPage(page); @@ -70,10 +71,12 @@ public void addPages() { * This method is called when 'Finish' button is pressed in the wizard. We will * create an operation and run it using wizard as execution context. */ + @Override public boolean performFinish() { final String containerName = page.getContainerName(); final String fileName = page.getFileName(); IRunnableWithProgress op = new IRunnableWithProgress() { + @Override public void run(IProgressMonitor monitor) throws InvocationTargetException { try { doFinish(containerName, fileName, monitor); @@ -90,7 +93,7 @@ public void run(IProgressMonitor monitor) throws InvocationTargetException { return false; } catch (InvocationTargetException e) { Throwable realException = e.getTargetException(); - MessageDialog.openError(getShell(), "Error", realException.getMessage()); + MessageDialog.openError(getShell(), "Error", realException.getMessage()); //$NON-NLS-1$ return false; } return true; @@ -102,27 +105,26 @@ public void run(IProgressMonitor monitor) throws InvocationTargetException { */ private void doFinish(String containerName, String fileName, IProgressMonitor monitor) throws CoreException { // create a sample file - monitor.beginTask("Creating " + fileName, 2); + monitor.beginTask("Creating " + fileName, 2); //$NON-NLS-1$ IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot(); IResource resource = root.findMember(new Path(containerName)); if (!resource.exists() || !(resource instanceof IContainer)) { - throwCoreException("Container \"" + containerName + "\" does not exist."); + throwCoreException("Container \"" + containerName + "\" does not exist."); //$NON-NLS-1$ //$NON-NLS-2$ } IContainer container = (IContainer) resource; final IFile file = container.getFile(new Path(fileName)); - try { - InputStream stream = openContentStream(); + try (InputStream stream = openContentStream()) { if (file.exists()) { file.setContents(stream, true, true, monitor); } else { file.create(stream, true, monitor); } - stream.close(); } catch (IOException e) { } monitor.worked(1); - monitor.setTaskName("Opening file for editing..."); + monitor.setTaskName("Opening file for editing..."); //$NON-NLS-1$ getShell().getDisplay().asyncExec(new Runnable() { + @Override public void run() { IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage(); try { @@ -134,12 +136,12 @@ public void run() { monitor.worked(1); } - private InputStream openContentStream() { + private static InputStream openContentStream() { return new ByteArrayInputStream(new byte[0]); } - private void throwCoreException(String message) throws CoreException { - IStatus status = new Status(IStatus.ERROR, "org.eclipse.gef.examples.text", IStatus.OK, message, null); + private static void throwCoreException(String message) throws CoreException { + IStatus status = new Status(IStatus.ERROR, "org.eclipse.gef.examples.text", IStatus.OK, message, null); //$NON-NLS-1$ throw new CoreException(status); } @@ -149,6 +151,7 @@ private void throwCoreException(String message) throws CoreException { * * @see IWorkbenchWizard#init(IWorkbench, IStructuredSelection) */ + @Override public void init(IWorkbench workbench, IStructuredSelection selection) { this.selection = selection; }