Skip to content

Commit

Permalink
Fixed most warnings in text example.
Browse files Browse the repository at this point in the history
  • Loading branch information
azoitl authored and ptziegler committed Oct 7, 2024
1 parent fa41058 commit 18760db
Show file tree
Hide file tree
Showing 17 changed files with 152 additions and 175 deletions.
2 changes: 1 addition & 1 deletion org.eclipse.gef.examples.text/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@
schemeId="org.eclipse.ui.defaultAcceleratorConfiguration">
</key>
<key
contextId="org.ecipse.gef.textContext"
contextId="org.eclipse.gef.textContext"
sequence="M1+U"
commandId="org.eclipse.gef.text.underline"
schemeId="org.eclipse.ui.defaultAcceleratorConfiguration">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -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();
Expand All @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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));
}
}
});
Expand Down Expand Up @@ -227,6 +224,7 @@ public List<EditPart> getSelectedEditParts() {
* @deprecated
* @see org.eclipse.gef.ui.parts.AbstractEditPartViewer#primGetSelectedEditParts()
*/
@Deprecated
@Override
protected List<EditPart> primGetSelectedEditParts() {
if (selectionModel != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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$
}
Expand All @@ -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);
});
}

Expand Down Expand Up @@ -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();
Expand All @@ -271,6 +256,7 @@ public void doSave(IProgressMonitor monitor) {
*/
@Override
public void doSaveAs() {
// currently not implemented
}

/**
Expand All @@ -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));
}
}
});
Expand All @@ -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) {
Expand All @@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -115,7 +114,7 @@ protected List<ModelElement> getModelChildren() {
}

/**
* @see TextEditPart#getTextLocation(int, TextLocation)
* @see TextEditPart#getTextLocation(CaretRequest, SearchResult)
*/
@Override
public void getTextLocation(CaretRequest search, SearchResult result) {
Expand Down Expand Up @@ -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();
}
}
Expand All @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand Down
Loading

0 comments on commit 18760db

Please sign in to comment.