From 1a7b077010fd3879a3e0af2056ecbcd25acc97dd Mon Sep 17 00:00:00 2001 From: Alois Zoitl Date: Wed, 2 Oct 2024 00:18:08 +0200 Subject: [PATCH] Fixed most warnings in text example. --- org.eclipse.gef.examples.text/plugin.xml | 2 +- .../examples/text/GraphicalTextViewer.java | 34 +++--- .../eclipse/gef/examples/text/TextEditor.java | 108 ++++++++---------- .../actions/ResizeFontContributionItem.java | 4 +- .../text/actions/StyleRetargetAction.java | 2 +- .../examples/text/edit/CompoundTextPart.java | 23 ++-- .../examples/text/edit/ContainerTreePart.java | 17 +-- .../gef/examples/text/edit/TextEditPart.java | 1 - .../gef/examples/text/edit/TextFlowPart.java | 5 +- .../gef/examples/text/figures/Images.java | 14 ++- .../gef/examples/text/model/Container.java | 29 +++-- .../gef/examples/text/model/Style.java | 30 +++-- .../text/model/commands/ChangeString.java | 2 +- .../model/commands/CompoundTextCommand.java | 37 +++--- .../model/commands/SingleEditCommand.java | 1 - .../examples/text/requests/TextRequest.java | 9 +- .../text/wizard/NewFileWizardPage.java | 9 +- 17 files changed, 152 insertions(+), 175 deletions(-) diff --git a/org.eclipse.gef.examples.text/plugin.xml b/org.eclipse.gef.examples.text/plugin.xml index 84e8c48a4..18ff32b98 100644 --- a/org.eclipse.gef.examples.text/plugin.xml +++ b/org.eclipse.gef.examples.text/plugin.xml @@ -119,7 +119,7 @@ schemeId="org.eclipse.ui.defaultAcceleratorConfiguration"> 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 368fa9258..9da706a69 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 @@ -30,8 +30,6 @@ import org.eclipse.gef.EditDomain; import org.eclipse.gef.EditPart; import org.eclipse.gef.commands.CommandStack; -import org.eclipse.gef.commands.CommandStackEvent; -import org.eclipse.gef.commands.CommandStackEventListener; import org.eclipse.gef.ui.parts.ScrollingGraphicalViewer; /** @@ -50,6 +48,7 @@ public class GraphicalTextViewer extends ScrollingGraphicalViewer { * @deprecated in 3.2. @TODO:Pratik remove this method and all references to it. * Use getSelectionModel() instead. */ + @Deprecated public SelectionRange getSelectionRange() { if (selectionModel != null) { return selectionModel.getSelectionRange(); @@ -58,7 +57,7 @@ public SelectionRange getSelectionRange() { } public void revealCaret() { - Assert.isNotNull(getControl(), "The control has not been created yet."); + Assert.isNotNull(getControl(), "The control has not been created yet."); //$NON-NLS-1$ Caret caret = getFigureCanvas().getCaret(); if (caret == null || !caret.isVisible()) { return; @@ -95,6 +94,7 @@ public void revealCaret() { * @deprecated in 3.2. @TODO:Pratik remove this method and all references to it. * Use setSelectionModel() instead. */ + @Deprecated public void setSelectionRange(SelectionRange newRange) { // @TODO:Pratik change all these setSelection() methods so that they // don't affect @@ -167,21 +167,18 @@ public void select(EditPart editpart) { @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; - } - TextCommand command = (TextCommand) event.getCommand(); - if (command != null) { - if (event.getDetail() == CommandStack.POST_EXECUTE) { - setSelectionRange(command.getExecuteSelectionRange(GraphicalTextViewer.this)); - } else if (event.getDetail() == CommandStack.POST_REDO) { - setSelectionRange(command.getRedoSelectionRange(GraphicalTextViewer.this)); - } else if (event.getDetail() == CommandStack.POST_UNDO) { - setSelectionRange(command.getUndoSelectionRange(GraphicalTextViewer.this)); - } + getEditDomain().getCommandStack().addCommandStackEventListener(event -> { + if (!(event.getCommand() instanceof TextCommand) || getSelectionRange() == null) { + return; + } + TextCommand command = (TextCommand) event.getCommand(); + if (command != null) { + if (event.getDetail() == CommandStack.POST_EXECUTE) { + setSelectionRange(command.getExecuteSelectionRange(GraphicalTextViewer.this)); + } else if (event.getDetail() == CommandStack.POST_REDO) { + setSelectionRange(command.getRedoSelectionRange(GraphicalTextViewer.this)); + } else if (event.getDetail() == CommandStack.POST_UNDO) { + setSelectionRange(command.getUndoSelectionRange(GraphicalTextViewer.this)); } } }); @@ -227,6 +224,7 @@ public List getSelectedEditParts() { * @deprecated * @see org.eclipse.gef.ui.parts.AbstractEditPartViewer#primGetSelectedEditParts() */ + @Deprecated @Override protected List primGetSelectedEditParts() { if (selectionModel != null) { 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 4da30e17f..ba21cabdc 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 @@ -47,8 +47,6 @@ import org.eclipse.gef.MouseWheelHandler; import org.eclipse.gef.MouseWheelZoomHandler; import org.eclipse.gef.commands.CommandStack; -import org.eclipse.gef.commands.CommandStackEvent; -import org.eclipse.gef.commands.CommandStackEventListener; import org.eclipse.gef.editparts.ScalableRootEditPart; import org.eclipse.gef.tools.SelectionTool; import org.eclipse.gef.ui.actions.ActionRegistry; @@ -82,32 +80,25 @@ public class TextEditor extends GraphicalEditor { private Container doc; - private StyleService styleService = new StyleService(); + private final StyleService styleService = new StyleService(); private static final class TextEditorEditPartFactory implements EditPartFactory { @Override public EditPart createEditPart(EditPart context, Object model) { if (model instanceof Container cont) { - switch (cont.getType()) { - case Container.TYPE_ROOT: - return new DocumentPart(cont); - case Container.TYPE_IMPORT_DECLARATIONS: - return new ImportsPart(cont); - - case Container.TYPE_COMMENT, Container.TYPE_PARAGRAPH: - return new BlockTextPart(cont); - case Container.TYPE_INLINE: - return new InlineTextPart(cont); - default: - throw new RuntimeException("unknown model type"); //$NON-NLS-1$ - } - } else if (model instanceof TextRun textRun) { - switch (textRun.getType()) { - case TextRun.TYPE_IMPORT: + return switch (cont.getType()) { + case Container.TYPE_ROOT -> new DocumentPart(cont); + case Container.TYPE_IMPORT_DECLARATIONS -> new ImportsPart(cont); + case Container.TYPE_COMMENT, Container.TYPE_PARAGRAPH -> new BlockTextPart(cont); + case Container.TYPE_INLINE -> new InlineTextPart(cont); + default -> throw new RuntimeException("unknown model type"); //$NON-NLS-1$ + }; + } + if (model instanceof TextRun textRun) { + if (textRun.getType() == TextRun.TYPE_IMPORT) { return new ImportPart(textRun); - default: - return new TextFlowPart(textRun); } + return new TextFlowPart(textRun); } throw new RuntimeException("unexpected model object"); //$NON-NLS-1$ } @@ -123,14 +114,11 @@ public TextOutlinePage(EditPartViewer viewer) { EditPartViewer treeViewer = getViewer(); treeViewer.setEditDomain(domain); getSelectionSynchronizer().addViewer(treeViewer); - treeViewer.setEditPartFactory(new EditPartFactory() { - @Override - public EditPart createEditPart(EditPart context, Object model) { - if (model instanceof Container cont) { - return new ContainerTreePart(cont); - } - return new TextRunTreePart(model); + treeViewer.setEditPartFactory((context, model) -> { + if (model instanceof Container cont) { + return new ContainerTreePart(cont); } + return new TextRunTreePart(model); }); } @@ -252,14 +240,11 @@ protected void initializeGraphicalViewer() { */ @Override public void doSave(IProgressMonitor monitor) { - try { - ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); - ObjectOutputStream objStream = new ObjectOutputStream(outputStream); + try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); + ObjectOutputStream objStream = new ObjectOutputStream(outputStream);) { objStream.writeObject(doc); - objStream.close(); IFile file = ((IFileEditorInput) getEditorInput()).getFile(); file.setContents(new ByteArrayInputStream(outputStream.toByteArray()), true, false, monitor); - outputStream.close(); getCommandStack().markSaveLocation(); } catch (Exception e) { e.printStackTrace(); @@ -271,6 +256,7 @@ public void doSave(IProgressMonitor monitor) { */ @Override public void doSaveAs() { + // currently not implemented } /** @@ -279,19 +265,16 @@ public void doSaveAs() { @Override public void init(IEditorSite site, IEditorInput input) throws PartInitException { setEditDomain(new DefaultEditDomain(this)); - getCommandStack().addCommandStackEventListener(new CommandStackEventListener() { - @Override - public void stackChanged(CommandStackEvent event) { - TextCommand command = (TextCommand) event.getCommand(); - if (command != null) { - GraphicalTextViewer textViewer = (GraphicalTextViewer) getGraphicalViewer(); - if (event.getDetail() == CommandStack.POST_EXECUTE) { - textViewer.setSelectionRange(command.getExecuteSelectionRange(textViewer)); - } else if (event.getDetail() == CommandStack.POST_REDO) { - textViewer.setSelectionRange(command.getRedoSelectionRange(textViewer)); - } else if (event.getDetail() == CommandStack.POST_UNDO) { - textViewer.setSelectionRange(command.getUndoSelectionRange(textViewer)); - } + getCommandStack().addCommandStackEventListener(event -> { + TextCommand command = (TextCommand) event.getCommand(); + if (command != null) { + GraphicalTextViewer textViewer = (GraphicalTextViewer) getGraphicalViewer(); + if (event.getDetail() == CommandStack.POST_EXECUTE) { + textViewer.setSelectionRange(command.getExecuteSelectionRange(textViewer)); + } else if (event.getDetail() == CommandStack.POST_REDO) { + textViewer.setSelectionRange(command.getRedoSelectionRange(textViewer)); + } else if (event.getDetail() == CommandStack.POST_UNDO) { + textViewer.setSelectionRange(command.getUndoSelectionRange(textViewer)); } } }); @@ -318,11 +301,8 @@ protected void setInput(IEditorInput input) { super.setInput(input); IFile file = ((IFileEditorInput) input).getFile(); - try { - InputStream is = file.getContents(false); - ObjectInputStream ois = new ObjectInputStream(is); + try (InputStream is = file.getContents(false); ObjectInputStream ois = new ObjectInputStream(is);) { doc = (Container) ois.readObject(); - ois.close(); } catch (EOFException eofe) { // file was empty (as in the case of a new file); do nothing } catch (Exception e) { @@ -338,25 +318,29 @@ protected void setInput(IEditorInput input) { doc.add(imports); 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 " //$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$ + 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 + Contributors: + IBM Corporation - initial API and implementation + \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)""")); //$NON-NLS-1$ doc.add(block); Container code = new Block(Container.TYPE_PARAGRAPH); 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" //$NON-NLS-1$ //$NON-NLS-2$ - + " System.out.println(\"Counting: \" + i); //$NON-NLS-1$\n\n" + "}", //$NON-NLS-1$ //$NON-NLS-2$ + code.add(new TextRun(""" + public void countToANumber(int limit) { + for (int i = 0; i < limit; i++) + System.out.println("Counting: " + i); //$NON-NLS-1$ + + }""", //$NON-NLS-1$ TextRun.TYPE_CODE)); - // } } setPartName(file.getName()); 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 6e239357c..9dec26110 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 @@ -24,8 +24,8 @@ */ public class ResizeFontContributionItem extends StyleComboContributionItem { - private static final String[] INIT_SIZES = new String[] { "8", "9", "10", "11", "12", "14", "16", "18", "20", "22", - "24", "26", "28", "36", "48", "72" }; + private static final String[] INIT_SIZES = { "8", "9", "10", "11", "12", "14", "16", "18", "20", "22", //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$ //$NON-NLS-7$ //$NON-NLS-8$ //$NON-NLS-9$ //$NON-NLS-10$ + "24", "26", "28", "36", "48", "72" }; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$ public ResizeFontContributionItem(IPartService service) { super(service); diff --git a/org.eclipse.gef.examples.text/src/org/eclipse/gef/examples/text/actions/StyleRetargetAction.java b/org.eclipse.gef.examples.text/src/org/eclipse/gef/examples/text/actions/StyleRetargetAction.java index 1deb2f85f..6212f9e2a 100644 --- a/org.eclipse.gef.examples.text/src/org/eclipse/gef/examples/text/actions/StyleRetargetAction.java +++ b/org.eclipse.gef.examples.text/src/org/eclipse/gef/examples/text/actions/StyleRetargetAction.java @@ -22,7 +22,7 @@ public class StyleRetargetAction extends RetargetAction { public StyleRetargetAction(String styleID) { - super(styleID, "", IAction.AS_CHECK_BOX); + super(styleID, "", IAction.AS_CHECK_BOX); //$NON-NLS-1$ BooleanStyleAction.configureStyleAction(this); } 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 81ddccd0a..a09992456 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,7 +24,6 @@ import org.eclipse.draw2d.text.FlowPage; import org.eclipse.draw2d.text.InlineFlow; -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; @@ -115,7 +114,7 @@ protected List getModelChildren() { } /** - * @see TextEditPart#getTextLocation(int, TextLocation) + * @see TextEditPart#getTextLocation(CaretRequest, SearchResult) */ @Override public void getTextLocation(CaretRequest search, SearchResult result) { @@ -144,7 +143,7 @@ public void getTextLocation(CaretRequest search, SearchResult result) { @Override public void propertyChange(PropertyChangeEvent evt) { - if (evt.getPropertyName().equals("children")) { //$NON-NLS-1$ + if (evt.getPropertyName().equals(Container.CHILDREN_PROPERTY)) { refreshChildren(); } } @@ -154,7 +153,8 @@ protected void searchBackward(CaretRequest search, SearchResult result) { boolean wasRecursive = search.isRecursive; search.setRecursive(true); while (childIndex >= 0) { - TextEditPart part = getChildren().get(childIndex--); + TextEditPart part = getChildren().get(childIndex); + childIndex--; part.getTextLocation(search, result); if (result.location != null) { return; @@ -176,7 +176,8 @@ protected void searchForward(CaretRequest search, SearchResult result) { boolean wasRecursive = search.isRecursive; search.setRecursive(true); while (childIndex < childCount) { - TextEditPart part = getChildren().get(childIndex++); + TextEditPart part = getChildren().get(childIndex); + childIndex++; part.getTextLocation(search, result); if (result.location != null) { return; @@ -207,7 +208,8 @@ protected void searchLineAbove(CaretRequest search, SearchResult result) { boolean wasRecursive = search.isRecursive; search.setRecursive(true); while (childIndex >= 0) { - part = getChildren().get(childIndex--); + part = getChildren().get(childIndex); + childIndex--; part.getTextLocation(search, result); if (result.bestMatchFound) { return; @@ -225,7 +227,8 @@ protected void searchLineBegin(CaretRequest search, SearchResult result) { int childCount = getChildren().size(); search.setRecursive(true); while (childIndex < childCount) { - TextEditPart newPart = getChildren().get(childIndex++); + TextEditPart newPart = getChildren().get(childIndex); + childIndex++; newPart.getTextLocation(search, result); if (result.location != null) { return; @@ -252,7 +255,8 @@ protected void searchLineBelow(CaretRequest search, SearchResult result) { boolean wasRecursive = search.isRecursive; search.setRecursive(true); while (childIndex < childCount) { - TextEditPart part = getChildren().get(childIndex++); + TextEditPart part = getChildren().get(childIndex); + childIndex++; part.getTextLocation(search, result); if (result.bestMatchFound) { return; @@ -270,7 +274,8 @@ protected void searchLineEnd(CaretRequest search, SearchResult result) { TextEditPart child; search.setRecursive(true); while (childIndex >= 0) { - child = getChildren().get(childIndex--); + child = getChildren().get(childIndex); + childIndex--; child.getTextLocation(search, result); if (result.location != null) { return; 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 5fb7efbf8..f8d49e8f7 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 @@ -37,7 +37,7 @@ protected List getModelChildren() { @Override public void propertyChange(PropertyChangeEvent evt) { - if (evt.getPropertyName().equals("children")) { //$NON-NLS-1$ + if (evt.getPropertyName().equals(Container.CHILDREN_PROPERTY)) { refreshChildren(); } } @@ -71,24 +71,19 @@ protected void refreshVisuals() { case Container.TYPE_INLINE: Style style = getModel().getStyle(); - if (style.isSet(Style.PROPERTY_FONT_SIZE)) - { + if (style.isSet(Style.PROPERTY_FONT_SIZE)) { label.append(""); //$NON-NLS-1$ } - if (style.isSet(Style.PROPERTY_BOLD)) - { + if (style.isSet(Style.PROPERTY_BOLD)) { label.append(""); //$NON-NLS-1$ } - if (style.isSet(Style.PROPERTY_ITALIC)) - { + if (style.isSet(Style.PROPERTY_ITALIC)) { label.append(""); //$NON-NLS-1$ } - if (style.isSet(Style.PROPERTY_UNDERLINE)) - { + if (style.isSet(Style.PROPERTY_UNDERLINE)) { label.append(""); //$NON-NLS-1$ } - if (style.isSet(Style.PROPERTY_FONT)) - { + if (style.isSet(Style.PROPERTY_FONT)) { label.append(""); //$NON-NLS-1$ } break; diff --git a/org.eclipse.gef.examples.text/src/org/eclipse/gef/examples/text/edit/TextEditPart.java b/org.eclipse.gef.examples.text/src/org/eclipse/gef/examples/text/edit/TextEditPart.java index 95d38386b..e2b2ccae0 100644 --- a/org.eclipse.gef.examples.text/src/org/eclipse/gef/examples/text/edit/TextEditPart.java +++ b/org.eclipse.gef.examples.text/src/org/eclipse/gef/examples/text/edit/TextEditPart.java @@ -27,7 +27,6 @@ public interface TextEditPart extends GraphicalEditPart { /** * @since 3.1 - * @return */ boolean acceptsCaret(); diff --git a/org.eclipse.gef.examples.text/src/org/eclipse/gef/examples/text/edit/TextFlowPart.java b/org.eclipse.gef.examples.text/src/org/eclipse/gef/examples/text/edit/TextFlowPart.java index 35900c320..7d65b3d78 100644 --- a/org.eclipse.gef.examples.text/src/org/eclipse/gef/examples/text/edit/TextFlowPart.java +++ b/org.eclipse.gef.examples.text/src/org/eclipse/gef/examples/text/edit/TextFlowPart.java @@ -47,6 +47,7 @@ public TextFlowPart(TextRun model) { @Override protected void createEditPolicies() { + // we don't have special edit policies } @Override @@ -75,7 +76,7 @@ public CaretInfo getCaretPlacement(int offset, boolean trailing) { } else { // @TODO:Pratik this should never happen trailing = false; - new RuntimeException("unexpected condition").printStackTrace(); + new RuntimeException("unexpected condition").printStackTrace(); //$NON-NLS-1$ } } return getTextFlow().getCaretPlacement(offset, trailing); @@ -114,7 +115,7 @@ public void getTextLocation(CaretRequest search, SearchResult result) { @Override public void propertyChange(PropertyChangeEvent evt) { - if (evt.getPropertyName().equals("text")) { + if (evt.getPropertyName().equals("text")) { //$NON-NLS-1$ refreshVisuals(); } } 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 48bacde9c..fa24cb3dd 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,7 +21,7 @@ /** * @since 3.1 */ -public class Images { +public final class Images { public static final Image IMPORTS = createImage("imports.gif"); //$NON-NLS-1$ public static final Image IMPORT = createImage("importstatement.gif"); //$NON-NLS-1$ @@ -30,13 +30,15 @@ public class Images { public static final Image PROTECTED = createImage("protected.gif"); //$NON-NLS-1$ private static Image createImage(String name) { - InputStream stream = Images.class.getResourceAsStream(name); - Image image = new Image(null, stream); - try { - stream.close(); + try (InputStream stream = Images.class.getResourceAsStream(name)) { + return new Image(null, stream); } catch (IOException exc) { } - return image; + return null; + } + + private Images() { + throw new UnsupportedOperationException("Utility class shall not be instantiated!"); //$NON-NLS-1$ } } 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 b0077ab71..cf712d7f0 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 @@ -22,6 +22,7 @@ */ public abstract class Container extends ModelElement { + public static final String CHILDREN_PROPERTY = "children"; //$NON-NLS-1$ public static final int TYPE_BULLETED_LIST = 1; public static final int TYPE_COMMENT = 2; public static final int TYPE_IMPORT_DECLARATIONS = 3; @@ -32,8 +33,8 @@ public abstract class Container extends ModelElement { private static final long serialVersionUID = 1; - private List children = new ArrayList<>(); - private Style style = new Style(); + private final List children = new ArrayList<>(); + private final Style style = new Style(); protected Container(int type) { this.type = type; @@ -49,7 +50,7 @@ public void add(ModelElement child, int index) { index = children.size(); } children.add(index, child); - firePropertyChange("children", null, child); //$NON-NLS-1$ + firePropertyChange(CHILDREN_PROPERTY, null, child); } /** @@ -57,7 +58,7 @@ public void add(ModelElement child, int index) { * * @since 3.1 * @param child - * @return + * @return true if this is a child or a transitive child of this container */ public boolean contains(ModelElement child) { do { @@ -74,14 +75,11 @@ public List getChildren() { } public int getChildType() { - switch (getType()) { - case TYPE_IMPORT_DECLARATIONS: - return TextRun.TYPE_IMPORT; - case TYPE_BULLETED_LIST: - return TextRun.TYPE_BULLET; - default: - return 0; - } + return switch (getType()) { + case TYPE_IMPORT_DECLARATIONS -> TextRun.TYPE_IMPORT; + case TYPE_BULLETED_LIST -> TextRun.TYPE_BULLET; + default -> 0; + }; } public Style getStyle() { @@ -92,14 +90,13 @@ public int remove(ModelElement child) { int index = children.indexOf(child); children.remove(child); child.setParent(null); - firePropertyChange("children", child, null); //$NON-NLS-1$ + firePropertyChange(CHILDREN_PROPERTY, child, null); return index; } public void removeAll(Collection children) { - if (children.removeAll(children)) - { - firePropertyChange("children", children, null); //$NON-NLS-1$ + if (children.removeAll(children)) { + firePropertyChange(CHILDREN_PROPERTY, children, null); } } diff --git a/org.eclipse.gef.examples.text/src/org/eclipse/gef/examples/text/model/Style.java b/org.eclipse.gef.examples.text/src/org/eclipse/gef/examples/text/model/Style.java index 73076a0ad..27277525c 100644 --- a/org.eclipse.gef.examples.text/src/org/eclipse/gef/examples/text/model/Style.java +++ b/org.eclipse.gef.examples.text/src/org/eclipse/gef/examples/text/model/Style.java @@ -58,7 +58,7 @@ public String getFontFamily() { if (parentStyle != null) { return parentStyle.getFontFamily(); } - return ""; + return ""; //$NON-NLS-1$ } public int getFontHeight() { @@ -94,26 +94,23 @@ public boolean isItalic() { } public boolean isSet(String property) { - if (PROPERTY_BOLD.equals(property)) { + switch (property) { + case PROPERTY_BOLD: return bold; - } - if (PROPERTY_FONT_SIZE.equals(property)) { + case PROPERTY_FONT_SIZE: return fontHeight != -1; - } - if (PROPERTY_ITALIC.equals(property)) { + case PROPERTY_ITALIC: return italic; - } - if (PROPERTY_UNDERLINE.equals(property)) { + case PROPERTY_UNDERLINE: return underline; - } - if (PROPERTY_FONT.equals(property)) { + case PROPERTY_FONT: return fontFamily != null; - } - if (PROPERTY_ALIGNMENT.equals(property)) { + case PROPERTY_ALIGNMENT: return alignment != PositionConstants.NONE; - } - if (PROPERTY_ORIENTATION.equals(property)) { + case PROPERTY_ORIENTATION: return orientation != SWT.NONE; + default: + break; } return false; } @@ -128,8 +125,7 @@ public void setAlignment(int value) { } if (value != PositionConstants.ALWAYS_RIGHT && value != PositionConstants.CENTER && value != PositionConstants.RIGHT && value != PositionConstants.NONE - && value != PositionConstants.LEFT && value != PositionConstants.ALWAYS_LEFT) - { + && value != PositionConstants.LEFT && value != PositionConstants.ALWAYS_LEFT) { throw new IllegalArgumentException( "Alignment must be LEFT, CENTER, RIGHT, ALWAYS_LEFT, ALWAYS_RIGHT or NONE."); //$NON-NLS-1$ } @@ -178,7 +174,7 @@ public void setOrientation(int value) { return; } if (value != SWT.RIGHT_TO_LEFT && value != SWT.LEFT_TO_RIGHT && value != SWT.NONE) { - throw new IllegalArgumentException("Orientation must LTR, RTL or NONE."); + throw new IllegalArgumentException("Orientation must LTR, RTL or NONE."); //$NON-NLS-1$ } int oldValue = orientation; orientation = value; 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 6350bd993..2e42809d3 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 @@ -42,7 +42,7 @@ public ChangeString(TextRun run, String c, int offset, boolean overwrite) { /** * @since 3.1 - * @param char1 + * @param append */ public void appendText(String append) { Assert.isTrue(pending == null); 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 dc8e6c8b1..90883b92b 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 @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2005, 2010 IBM Corporation and others. + * Copyright (c) 2005, 2024 IBM Corporation and others. * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License 2.0 which is available at @@ -24,11 +24,10 @@ public class CompoundTextCommand extends Command implements TextCommand, AppendableCommand { - private List pending = new ArrayList(); - private List applied = new ArrayList(); + private final List pending = new ArrayList<>(); + private final List applied = new ArrayList<>(); public CompoundTextCommand() { - super(); } public CompoundTextCommand(String label) { @@ -36,8 +35,8 @@ public CompoundTextCommand(String label) { } public void add(TextCommand command) { - if (command != null) { - pending.add(command); + if (command instanceof Command cmd) { + pending.add(cmd); } } @@ -51,8 +50,7 @@ public boolean canExecutePending() { if (pending.isEmpty()) { return false; } - for (Object element : pending) { - Command cmd = (Command) element; + for (Command cmd : pending) { if (cmd == null) { return false; } @@ -65,8 +63,8 @@ public boolean canExecutePending() { @Override public void dispose() { - for (Object element : applied) { - ((Command) element).dispose(); + for (Command element : applied) { + element.dispose(); } flushPending(); } @@ -78,8 +76,7 @@ public void execute() { @Override public void executePending() { - for (int i = 0; i < pending.size(); i++) { - Command cmd = (Command) pending.get(i); + for (Command cmd : pending) { cmd.execute(); applied.add(cmd); } @@ -96,7 +93,11 @@ public SelectionRange getExecuteSelectionRange(GraphicalTextViewer viewer) { if (applied.isEmpty()) { return null; } - return ((TextCommand) applied.get(applied.size() - 1)).getExecuteSelectionRange(viewer); + return getAppliedTextCommand(applied.size() - 1).getExecuteSelectionRange(viewer); + } + + private TextCommand getAppliedTextCommand(int i) { + return (TextCommand) applied.get(i); } @Override @@ -104,7 +105,7 @@ public SelectionRange getRedoSelectionRange(GraphicalTextViewer viewer) { if (applied.isEmpty()) { return null; } - return ((TextCommand) applied.get(applied.size() - 1)).getExecuteSelectionRange(viewer); + return getAppliedTextCommand(applied.size() - 1).getExecuteSelectionRange(viewer); } @Override @@ -112,20 +113,20 @@ public SelectionRange getUndoSelectionRange(GraphicalTextViewer viewer) { if (applied.isEmpty()) { return null; } - return ((TextCommand) applied.get(0)).getUndoSelectionRange(viewer); + return getAppliedTextCommand(0).getUndoSelectionRange(viewer); } @Override public void redo() { - for (Object element : applied) { - ((Command) element).redo(); + for (Command element : applied) { + element.redo(); } } @Override public void undo() { for (int i = applied.size() - 1; i >= 0; i--) { - ((Command) applied.get(i)).undo(); + applied.get(i).undo(); } } 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 d2294fe2e..cf63d2734 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 @@ -28,7 +28,6 @@ public class SingleEditCommand extends ExampleTextCommand { private final ModelLocation start; /** - * @param label * @since 3.1 */ public SingleEditCommand(MiniEdit edit, ModelLocation start, ModelLocation end) { diff --git a/org.eclipse.gef.examples.text/src/org/eclipse/gef/examples/text/requests/TextRequest.java b/org.eclipse.gef.examples.text/src/org/eclipse/gef/examples/text/requests/TextRequest.java index 4ab7704b0..4e6f15b08 100644 --- a/org.eclipse.gef.examples.text/src/org/eclipse/gef/examples/text/requests/TextRequest.java +++ b/org.eclipse.gef.examples.text/src/org/eclipse/gef/examples/text/requests/TextRequest.java @@ -55,15 +55,14 @@ public class TextRequest extends Request { public static final Object REQ_UNINDENT = new Object(); - private AppendableCommand previous; + private final AppendableCommand previous; - private SelectionRange range; - private String text; + private final SelectionRange range; + private final String text; private String[] styleKeys; private Object[] styleValues; /** - * @param type * @since 3.1 */ public TextRequest(SelectionRange range, String text, AppendableCommand previous) { @@ -118,7 +117,7 @@ public String getText() { return text; } - public void setStyles(String keys[], Object values[]) { + public void setStyles(String[] keys, Object[] values) { this.styleKeys = keys; this.styleValues = values; } 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 7cbb47e74..005b59030 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 @@ -42,18 +42,19 @@ public class NewFileWizardPage extends WizardPage { private Text fileText; - private ISelection selection; + private final ISelection selection; /** * Constructor for SampleNewWizardPage. * - * @param pageName */ public NewFileWizardPage(ISelection selection) { 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$ + setDescription(""" + This wizard creates a GEF-based WYSIWYG text document with \ + a *.text. extension. Choose a container and file name for the new\ + resource."""); //$NON-NLS-1$ this.selection = selection; }