Skip to content

Commit

Permalink
Cleaned text example and a bit of digraph2
Browse files Browse the repository at this point in the history
  • Loading branch information
azoitl committed Jul 3, 2023
1 parent 6841c7e commit 21a67b2
Show file tree
Hide file tree
Showing 53 changed files with 485 additions and 365 deletions.
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -74,6 +74,7 @@ protected List<Digraph2Edge> getModelTargetConnections() {
* @see org.eclipse.gef.NodeEditPart#getSourceConnectionAnchor(org.eclipse.gef.
* ConnectionEditPart)
*/
@Override
public ConnectionAnchor getSourceConnectionAnchor(ConnectionEditPart connection) {
return this.sourceAnchor;
}
Expand All @@ -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;
}
Expand All @@ -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;
}
Expand All @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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;
Expand Down Expand Up @@ -118,6 +117,7 @@ public void setSelectionModel(SelectionModel selection) {
fireSelectionChanged();
}

@Override
public void appendSelection(EditPart editpart) {
if (focusPart != editpart)
setFocus(null);
Expand All @@ -127,29 +127,34 @@ 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<EditPart> list = new ArrayList<>();
list.add(editpart);
setSelectionModel(createSelectionModel(null, null, list, null));
}

// @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;
Expand All @@ -166,20 +171,22 @@ else if (event.getDetail() == CommandStack.POST_UNDO)
});
}

@Override
public void setSelection(ISelection newSelection) {
if (newSelection != null)
setSelectionModel(createSelectionModel(newSelection, null, null, null));
else
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<EditPart> parts,
EditPart container) {
if (selection instanceof IStructuredSelection)
return new SelectionModel(selection);
Expand All @@ -189,7 +196,8 @@ protected SelectionModel createSelectionModel(ISelection selection, SelectionRan
/**
* @see org.eclipse.gef.ui.parts.AbstractEditPartViewer#getSelectedEditParts()
*/
public List getSelectedEditParts() {
@Override
public List<EditPart> getSelectedEditParts() {
return primGetSelectedEditParts();
}

Expand All @@ -200,18 +208,15 @@ public List getSelectedEditParts() {
* @deprecated
* @see org.eclipse.gef.ui.parts.AbstractEditPartViewer#primGetSelectedEditParts()
*/
protected List primGetSelectedEditParts() {
@Override
protected List<EditPart> 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();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -33,23 +33,23 @@ public class SelectionModel {

private final SelectionRange selectionRange;
private final EditPart selectionContainer;
private final List constantSelection;
private final List<EditPart> 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<EditPart> 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<EditPart> itr = constantSelection.iterator();
while (true) {
EditPart part = (EditPart) itr.next();
if (!itr.hasNext()) {
Expand All @@ -64,7 +64,7 @@ protected void applySelectedParts() {
protected void applySelectionRange() {
SelectionRange range = getSelectionRange();
if (range != null) {
List currentSelection = range.getSelectedParts();
List<EditPart> currentSelection = range.getSelectedParts();
for (int i = 0; i < currentSelection.size(); i++) {
TextEditPart textpart = (TextEditPart) currentSelection.get(i);
textpart.setSelection(0, textpart.getLength());
Expand All @@ -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())
Expand All @@ -113,27 +110,25 @@ public boolean equals(Object obj) {
}

public SelectionModel getAppendedSelection(EditPart newPart) {
ArrayList list = new ArrayList(constantSelection);
ArrayList<EditPart> 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<EditPart> list = new ArrayList<>(constantSelection);
list.remove(exclude);
return new SelectionModel(selectionRange, list, selectionContainer);
}

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<EditPart> getSelectedEditParts() {
return constantSelection;
}

Expand All @@ -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<EditPart> editparts = new HashSet<>(constantSelection);
old.getSelectedEditParts().stream().filter(part -> !editparts.contains(part))
.forEach(part -> part.setSelected(EditPart.SELECTED_NONE));
}
applySelectedParts();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
});
Expand Down Expand Up @@ -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));
// }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -46,6 +46,7 @@ public boolean equals(Object obj) {
/**
* @see java.lang.Object#hashCode()
*/
@Override
public int hashCode() {
return part.hashCode() << 11 ^ offset;
}
Expand Down
Loading

0 comments on commit 21a67b2

Please sign in to comment.